View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.provider;
17  
18  import org.apache.log4j.BasicConfigurator;
19  import org.apache.log4j.Level;
20  import org.apache.log4j.Logger;
21  import org.junit.Assert;
22  import org.junit.Before;
23  import org.junit.BeforeClass;
24  import org.junit.Test;
25  import org.kuali.rice.krad.data.metadata.DataObjectAttribute;
26  import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
27  import org.kuali.rice.krad.data.provider.impl.CompositeMetadataProviderImpl;
28  import org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl;
29  import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
30  import org.kuali.rice.krad.data.provider.spring.SpringMetadataProviderImpl;
31  
32  import javax.persistence.EntityManagerFactory;
33  import javax.persistence.Persistence;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  public class CompositeMetadataProviderImplTest {
38  
39  	static EclipseLinkJpaMetadataProviderImpl metadataProvider;
40  	CompositeMetadataProviderImpl compositeProvider;
41  	SpringMetadataProviderImpl springProvider;
42  
43  	@BeforeClass
44  	public static void setUpBeforeClass() throws Exception {
45  		BasicConfigurator.configure();
46  		Logger.getLogger(CompositeMetadataProviderImpl.class).setLevel(Level.DEBUG);
47  		Logger.getLogger(SpringMetadataProviderImpl.class).setLevel(Level.DEBUG);
48  		metadataProvider = new EclipseLinkJpaMetadataProviderImpl();
49  		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
50  		metadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
51  	}
52  
53  	@Before
54  	public void setUp() throws Exception {
55  		springProvider = new SpringMetadataProviderImpl();
56  		List<String> springFileLocations = new ArrayList<String>();
57  		springFileLocations.add("classpath:org/kuali/rice/krad/data/provider/spring/krad-metadata-parent-beans.xml");
58  		springFileLocations.add("classpath:org/kuali/rice/krad/data/provider/spring/*.xml");
59  		springProvider.setResourceLocations(springFileLocations);
60  		ArrayList<MetadataProvider> providers = new ArrayList<MetadataProvider>();
61  		providers.add(metadataProvider);
62  		providers.add(springProvider);
63  		compositeProvider = new CompositeMetadataProviderImpl();
64  		compositeProvider.setProviders(providers);
65  	}
66  
67  	@Test
68  	public void testProvideMetadata() {
69  		Assert.assertNotNull("Metadata map should not be null", compositeProvider.provideMetadata());
70  		Assert.assertFalse("Metadata map should not have been empty", compositeProvider.provideMetadata().isEmpty());
71  		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(TestDataObject.class);
72  		Assert.assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
73  		Assert.assertEquals("Label not read properly from metadata provider", "A Spring-Provided Label",
74  				metadata.getLabel());
75  		Assert.assertEquals("backing object name not read properly from metadata provider", "ANOTHER_TABLE_NAME_T",
76  				metadata.getBackingObjectName());
77  		Assert.assertFalse("Attributes should still be present from the OJB metadata", metadata.getAttributes()
78  				.isEmpty());
79  		System.err.println(metadata);
80  	}
81  
82  	@Test
83  	public void testMergedSpringAttribute() {
84  		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(TestDataObject.class);
85  		Assert.assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
86  		List<DataObjectAttribute> attributes = metadata.getAttributes();
87  		Assert.assertFalse("Attributes should still be present from the OJB metadata", attributes.isEmpty());
88  		boolean listContainsStringProperty = false;
89  		boolean listContainsNewProperty = false;
90  		for (DataObjectAttribute attr : attributes) {
91  			if (attr.getName().equals("stringProperty")) {
92  				listContainsStringProperty = true;
93  			}
94  			if (attr.getName().equals("nonPersistedProperty")) {
95  				listContainsNewProperty = true;
96  			}
97  		}
98  		System.err.println(attributes);
99  		Assert.assertTrue("stringProperty should still have been in the attribute list", listContainsStringProperty);
100 		Assert.assertTrue("nonPersistedProperty should have been added to the attribute list", listContainsNewProperty);
101 
102 		Assert.assertNotNull("getAttribute(stringProperty) should not have returned null",
103 				metadata.getAttribute("stringProperty"));
104 		Assert.assertNotNull("getAttribute(nonPersistedProperty) should not have returned null",
105 				metadata.getAttribute("nonPersistedProperty"));
106 		Assert.assertNotNull("getAttribute(nonStandardDataType) should not have returned null",
107 				metadata.getAttribute("nonStandardDataType"));
108 
109 		Assert.assertEquals("getAttribute(nonPersistedProperty) label incorrect", "Attribute Added via Spring",
110 				metadata.getAttribute("nonPersistedProperty").getLabel());
111 		Assert.assertEquals("getAttribute(nonStandardDataType) label incorrect", "Non Standard Label-Spring", metadata
112 				.getAttribute("nonStandardDataType").getLabel());
113 
114 		// test another property of the attribute to see that not changed (proxied to the embedded metadata)
115 		Assert.assertEquals("getAttribute(nonStandardDataType) backing object incorrect", "NON_STANDARD", metadata
116 				.getAttribute("nonStandardDataType").getBackingObjectName());
117 
118 	}
119 
120 	@Test
121 	public void testMergedSpringAttribute_Remove() {
122 		DataObjectMetadata metadata = compositeProvider.provideMetadata().get(TestDataObject.class);
123 		Assert.assertNotNull("Metadata should have been retrieved for TestDataObject", metadata);
124 		List<DataObjectAttribute> attributes = metadata.getAttributes();
125 		Assert.assertFalse("Attributes should still be present from the OJB metadata", attributes.isEmpty());
126 
127 		boolean listContainsDateProperty = false;
128 		for (DataObjectAttribute attr : attributes) {
129 			if (attr.getName().equals("dateProperty")) {
130 				listContainsDateProperty = true;
131 			}
132 		}
133 		System.err.println(attributes);
134 		Assert.assertFalse("dateProperty should not have been in the attribute list", listContainsDateProperty);
135 		Assert.assertNull("getAttribute(dateProperty) should have returned null", metadata.getAttribute("dateProperty"));
136 	}
137 }