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.jpa.eclipselink;
17  
18  import org.apache.log4j.BasicConfigurator;
19  import org.apache.log4j.Level;
20  import org.apache.log4j.Logger;
21  import org.eclipse.persistence.descriptors.ClassDescriptor;
22  import org.eclipse.persistence.internal.helper.DatabaseField;
23  import org.eclipse.persistence.mappings.DatabaseMapping;
24  import org.eclipse.persistence.mappings.DirectToFieldMapping;
25  import org.eclipse.persistence.mappings.OneToOneMapping;
26  import org.eclipse.persistence.mappings.converters.Converter;
27  import org.eclipse.persistence.mappings.converters.ConverterClass;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
32  import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectExtension;
33  import org.kuali.rice.krad.data.provider.MetadataProvider;
34  import org.kuali.rice.krad.data.provider.annotation.impl.AnnotationMetadataProviderImpl;
35  import org.kuali.rice.krad.data.provider.impl.CompositeMetadataProviderImpl;
36  
37  import javax.persistence.EntityManagerFactory;
38  import javax.persistence.Persistence;
39  import java.lang.reflect.Field;
40  import java.util.ArrayList;
41  import java.util.Map;
42  
43  import static org.junit.Assert.*;
44  
45  public class EclipseLinkAnnotationMetadataProviderImplTest {
46  
47  	static EclipseLinkJpaMetadataProviderImpl jpaMetadataProvider;
48  	CompositeMetadataProviderImpl compositeProvider;
49  	AnnotationMetadataProviderImpl annotationMetadataProvider;
50  
51  	@BeforeClass
52  	public static void setUpBeforeClass() throws Exception {
53  		BasicConfigurator.configure();
54  		Logger.getLogger(CompositeMetadataProviderImpl.class).setLevel(Level.DEBUG);
55  		Logger.getLogger(AnnotationMetadataProviderImpl.class).setLevel(Level.DEBUG);
56  		Logger.getLogger(EclipseLinkJpaMetadataProviderImpl.class).setLevel(Level.DEBUG);
57  		jpaMetadataProvider = new EclipseLinkJpaMetadataProviderImpl();
58  		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
59  		jpaMetadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
60  	}
61  
62  	@Before
63  	public void setUp() throws Exception {
64  		if (annotationMetadataProvider == null) {
65  			annotationMetadataProvider = new AnnotationMetadataProviderImpl();
66  			ArrayList<MetadataProvider> providers = new ArrayList<MetadataProvider>();
67  			providers.add(jpaMetadataProvider);
68  			providers.add(annotationMetadataProvider);
69  			compositeProvider = new CompositeMetadataProviderImpl();
70  			compositeProvider.setProviders(providers);
71  			compositeProvider.provideMetadata();
72  		}
73  	}
74  
75  	@Test
76  	public void testConvertersEstabished_directAssignment() throws Exception {
77  		ClassDescriptor classDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObject.class);
78  		DatabaseMapping attribute = classDescriptor.getMappingForAttributeName("nonStandardDataType");
79  		assertEquals("attribute data type mismatch", DirectToFieldMapping.class, attribute.getClass());
80  		Converter converter = ((org.eclipse.persistence.mappings.DirectToFieldMapping) attribute).getConverter();
81  		assertNotNull("converter not assigned", converter);
82  		assertEquals("Mismatch - converter should have been the EclipseLink JPA wrapper class", ConverterClass.class,
83                  converter.getClass());
84  		Field f = ConverterClass.class.getDeclaredField("attributeConverterClassName");
85  		f.setAccessible(true);
86  		String attributeConverterClassName = (String) f.get(converter);
87  		assertNotNull("attributeConverterClassName missing", attributeConverterClassName);
88  		assertEquals("Converter class incorrect", "org.kuali.rice.krad.data.jpa.testbo.NonStandardDataTypeConverter",
89                  attributeConverterClassName);
90  	}
91  
92  	@Test
93  	public void testConvertersEstabished_autoApply() throws Exception {
94  		ClassDescriptor classDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObject.class);
95  		DatabaseMapping attribute = classDescriptor.getMappingForAttributeName("currencyProperty");
96  		assertEquals("attribute data type mismatch", DirectToFieldMapping.class, attribute.getClass());
97  		Converter converter = ((org.eclipse.persistence.mappings.DirectToFieldMapping) attribute).getConverter();
98  		assertNotNull("converter not assigned", converter);
99  		assertEquals("Mismatch - converter should have been the EclipseLink JPA wrapper class", ConverterClass.class,
100                 converter.getClass());
101 		Field f = ConverterClass.class.getDeclaredField("attributeConverterClassName");
102 		f.setAccessible(true);
103 		String attributeConverterClassName = (String) f.get(converter);
104 		assertNotNull("attributeConverterClassName missing", attributeConverterClassName);
105 		assertEquals("Converter class incorrect", "org.kuali.rice.krad.data.jpa.converters.KualiDecimalConverter",
106                 attributeConverterClassName);
107 	}
108 
109 	@Test
110 	public void testConvertersEstabished_autoApply_Boolean() throws Exception {
111 		ClassDescriptor classDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObject.class);
112 		DatabaseMapping attribute = classDescriptor.getMappingForAttributeName("booleanProperty");
113 		assertEquals("attribute data type mismatch", DirectToFieldMapping.class, attribute.getClass());
114 		Converter converter = ((org.eclipse.persistence.mappings.DirectToFieldMapping) attribute).getConverter();
115 		assertNotNull("converter not assigned", converter);
116 		assertEquals("Mismatch - converter should have been the EclipseLink JPA wrapper class", ConverterClass.class,
117                 converter.getClass());
118 		Field f = ConverterClass.class.getDeclaredField("attributeConverterClassName");
119 		f.setAccessible(true);
120 		String attributeConverterClassName = (String) f.get(converter);
121 		assertNotNull("attributeConverterClassName missing", attributeConverterClassName);
122 		assertEquals("Converter class incorrect", "org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter",
123                 attributeConverterClassName);
124 	}
125 
126 	@Test
127 	public void testExtensionAttribute_eclipselink_data() {
128 		ClassDescriptor classDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObject.class);
129 		ClassDescriptor referenceDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObjectExtension.class);
130 		assertNotNull("A classDescriptor should have been retrieved from JPA for TestDataObject", classDescriptor);
131 		assertNotNull("A classDescriptor should have been retrieved from JPA for TestDataObjectExtension",
132                 referenceDescriptor);
133 		DatabaseMapping databaseMapping = classDescriptor.getMappingForAttributeName("extension");
134         assertNotNull("extension mapping missing from metamodel", databaseMapping);
135         assertTrue("Should be a OneToOne mapping", databaseMapping instanceof OneToOneMapping);
136         OneToOneMapping mapping = (OneToOneMapping)databaseMapping;
137 
138         assertEquals("Should be mapped by primaryKeyProperty", "primaryKeyProperty", mapping.getMappedBy());
139         Map<DatabaseField, DatabaseField> databaseFields = mapping.getSourceToTargetKeyFields();
140         assertEquals(1, databaseFields.size());
141         for (DatabaseField sourceField : databaseFields.keySet()) {
142             DatabaseField targetField = databaseFields.get(sourceField);
143             assertEquals("PK_PROP", sourceField.getName());
144             assertEquals("PK_PROP", targetField.getName());
145         }
146 
147 		assertNotNull("Reference descriptor missing from relationship", mapping.getReferenceDescriptor());
148 		assertEquals("Reference descriptor should be the one for TestDataObjectExtension", referenceDescriptor,
149                 mapping.getReferenceDescriptor());
150 
151 		assertNotNull("selection query relationship missing", mapping.getSelectionQuery());
152 		assertNotNull("selection query missing name", mapping.getSelectionQuery().getName());
153 		assertEquals("selection query name incorrect", "extension", mapping.getSelectionQuery().getName());
154 		assertNotNull("selection query reference class", mapping.getSelectionQuery().getReferenceClass());
155 		assertEquals("selection query reference class incorrect", TestDataObjectExtension.class,
156                 mapping.getSelectionQuery().getReferenceClass());
157 		assertNotNull("selection query reference class name", mapping.getSelectionQuery().getReferenceClassName());
158 		assertNotNull("selection query source mapping missing", mapping.getSelectionQuery().getSourceMapping());
159 		assertEquals("selection query source mapping incorrect", mapping,
160                 mapping.getSelectionQuery().getSourceMapping());
161 	}
162 }