kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2007, 2011 Oracle. All rights reserved. |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 3 | * This program and the accompanying materials are made available under the |
| 4 | * terms of the Eclipse Public License v1.0, which accompanies this distribution |
| 5 | * and is available at http://www.eclipse.org/legal/epl-v10.html. |
| 6 | * |
| 7 | * Contributors: |
| 8 | * Oracle - initial API and implementation |
| 9 | ******************************************************************************/ |
| 10 | package org.eclipse.jpt.jpa.core.tests.internal.context.java; |
| 11 | |
| 12 | import java.util.Iterator; |
| 13 | import org.eclipse.jdt.core.ICompilationUnit; |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 14 | import org.eclipse.jpt.common.core.resource.java.JavaResourceType; |
| 15 | import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 16 | import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject.SourceWriter; |
| 17 | import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; |
| 18 | import org.eclipse.jpt.jpa.core.MappingKeys; |
| 19 | import org.eclipse.jpt.jpa.core.context.Embeddable; |
| 20 | import org.eclipse.jpt.jpa.core.context.Entity; |
| 21 | import org.eclipse.jpt.jpa.core.context.IdClassReference; |
| 22 | import org.eclipse.jpt.jpa.core.context.MappedSuperclass; |
| 23 | import org.eclipse.jpt.jpa.core.internal.context.java.JavaNullTypeMapping; |
| 24 | import org.eclipse.jpt.jpa.core.resource.java.IdClassAnnotation; |
| 25 | import org.eclipse.jpt.jpa.core.resource.java.JPA; |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 26 | import org.eclipse.jpt.jpa.core.resource.java.MappedSuperclassAnnotation; |
| 27 | import org.eclipse.jpt.jpa.core.tests.internal.context.ContextModelTestCase; |
| 28 | |
| 29 | @SuppressWarnings("nls") |
| 30 | public class JavaMappedSuperclassTests extends ContextModelTestCase |
| 31 | { |
| 32 | public JavaMappedSuperclassTests(String name) { |
| 33 | super(name); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | private ICompilationUnit createTestMappedSuperclass() throws Exception { |
| 38 | return this.createTestType(new DefaultAnnotationWriter() { |
| 39 | @Override |
| 40 | public Iterator<String> imports() { |
| 41 | return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS); |
| 42 | } |
| 43 | @Override |
| 44 | public void appendTypeAnnotationTo(StringBuilder sb) { |
| 45 | sb.append("@MappedSuperclass"); |
| 46 | } |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | private void createTestIdClass() throws Exception { |
| 51 | SourceWriter sourceWriter = new SourceWriter() { |
| 52 | public void appendSourceTo(StringBuilder sb) { |
| 53 | sb.append(CR); |
| 54 | sb.append("public class ").append("TestTypeId").append(" "); |
| 55 | sb.append("{}").append(CR); |
| 56 | } |
| 57 | }; |
| 58 | this.javaProject.createCompilationUnit(PACKAGE_NAME, "TestTypeId.java", sourceWriter); |
| 59 | } |
| 60 | |
| 61 | public void testMorphToEntity() throws Exception { |
| 62 | createTestMappedSuperclass(); |
| 63 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 64 | |
| 65 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 66 | mappedSuperclass.getIdClassReference().setSpecifiedIdClassName("myIdClass"); |
| 67 | |
| 68 | getJavaPersistentType().setMappingKey(MappingKeys.ENTITY_TYPE_MAPPING_KEY); |
| 69 | assertTrue(getJavaPersistentType().getMapping() instanceof Entity); |
| 70 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 71 | JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); |
| 72 | assertNull(resourceType.getAnnotation(MappedSuperclassAnnotation.ANNOTATION_NAME)); |
| 73 | assertNotNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public void testMorphToEmbeddable() throws Exception { |
| 77 | createTestMappedSuperclass(); |
| 78 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 79 | |
| 80 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 81 | mappedSuperclass.getIdClassReference().setSpecifiedIdClassName("myIdClass"); |
| 82 | |
| 83 | getJavaPersistentType().setMappingKey(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); |
| 84 | assertTrue(getJavaPersistentType().getMapping() instanceof Embeddable); |
| 85 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 86 | JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); |
| 87 | assertNull(resourceType.getAnnotation(MappedSuperclassAnnotation.ANNOTATION_NAME)); |
| 88 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public void testMorphToNull() throws Exception { |
| 92 | createTestMappedSuperclass(); |
| 93 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 94 | |
| 95 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 96 | mappedSuperclass.getIdClassReference().setSpecifiedIdClassName("myIdClass"); |
| 97 | |
| 98 | getJavaPersistentType().setMappingKey(MappingKeys.NULL_TYPE_MAPPING_KEY); |
| 99 | assertTrue(getJavaPersistentType().getMapping() instanceof JavaNullTypeMapping); |
| 100 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 101 | JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); |
| 102 | assertNull(resourceType.getAnnotation(MappedSuperclassAnnotation.ANNOTATION_NAME)); |
| 103 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public void testMappedSuperclass() throws Exception { |
| 107 | createTestMappedSuperclass(); |
| 108 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 109 | assertTrue(getJavaPersistentType().getMapping() instanceof MappedSuperclass); |
| 110 | } |
| 111 | |
| 112 | public void testOverridableAttributeNames() throws Exception { |
| 113 | createTestMappedSuperclass(); |
| 114 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 115 | |
| 116 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 117 | Iterator<String> overridableAttributeNames = mappedSuperclass.getOverridableAttributeNames().iterator(); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 118 | assertEquals("id", overridableAttributeNames.next()); |
| 119 | assertEquals("name", overridableAttributeNames.next()); |
| 120 | assertFalse(overridableAttributeNames.hasNext()); |
| 121 | } |
| 122 | |
| 123 | public void testOverridableAssociationNames() throws Exception { |
| 124 | createTestMappedSuperclass(); |
| 125 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 126 | |
| 127 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 128 | Iterator<String> overridableAssociationNames = mappedSuperclass.getOverridableAssociationNames().iterator(); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 129 | assertFalse(overridableAssociationNames.hasNext()); |
| 130 | } |
| 131 | |
| 132 | public void testTableNameIsInvalid() throws Exception { |
| 133 | createTestMappedSuperclass(); |
| 134 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 135 | |
| 136 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 137 | |
| 138 | assertFalse(mappedSuperclass.tableNameIsInvalid(FULLY_QUALIFIED_TYPE_NAME)); |
| 139 | assertFalse(mappedSuperclass.tableNameIsInvalid("FOO")); |
| 140 | } |
| 141 | |
| 142 | public void testAssociatedTables() throws Exception { |
| 143 | createTestMappedSuperclass(); |
| 144 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 145 | |
| 146 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 147 | |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 148 | assertFalse(mappedSuperclass.getAssociatedTables().iterator().hasNext()); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | public void testAssociatedTablesIncludingInherited() throws Exception { |
| 152 | createTestMappedSuperclass(); |
| 153 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 154 | |
| 155 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 156 | |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 157 | assertFalse(mappedSuperclass.getAllAssociatedTables().iterator().hasNext()); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public void testAssociatedTableNamesIncludingInherited() throws Exception { |
| 161 | createTestMappedSuperclass(); |
| 162 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 163 | |
| 164 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 165 | |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 166 | assertFalse(mappedSuperclass.getAllAssociatedTableNames().iterator().hasNext()); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public void testAllOverridableAttributeNames() throws Exception { |
| 170 | createTestMappedSuperclass(); |
| 171 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 172 | |
| 173 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 174 | Iterator<String> overridableAttributeNames = mappedSuperclass.getOverridableAttributeNames().iterator(); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 175 | assertEquals("id", overridableAttributeNames.next()); |
| 176 | assertEquals("name", overridableAttributeNames.next()); |
| 177 | assertFalse(overridableAttributeNames.hasNext()); |
| 178 | } |
| 179 | |
| 180 | //TODO need to create a subclass mappedSuperclass and test this |
| 181 | public void testAllOverridableAssociationNames() throws Exception { |
| 182 | createTestMappedSuperclass(); |
| 183 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 184 | |
| 185 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
kmoore | 3d505d2 | 2011-08-02 13:24:47 +0000 | [diff] [blame] | 186 | Iterator<String> overridableAssociationNames = mappedSuperclass.getOverridableAssociationNames().iterator(); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 187 | assertFalse(overridableAssociationNames.hasNext()); |
| 188 | } |
| 189 | |
| 190 | public void testAttributeMappingKeyAllowed() throws Exception { |
| 191 | createTestMappedSuperclass(); |
| 192 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 193 | |
| 194 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 195 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY)); |
| 196 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY)); |
| 197 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY)); |
| 198 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY)); |
| 199 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY)); |
| 200 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY)); |
| 201 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY)); |
| 202 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY)); |
| 203 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY)); |
| 204 | assertTrue(mappedSuperclass.attributeMappingKeyAllowed(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY)); |
| 205 | } |
| 206 | |
| 207 | public void testUpdateIdClass() throws Exception { |
| 208 | createTestMappedSuperclass(); |
| 209 | createTestIdClass(); |
| 210 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 211 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 212 | JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 213 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 214 | IdClassReference idClassRef = mappedSuperclass.getIdClassReference(); |
| 215 | |
| 216 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
| 217 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 218 | assertNull(idClassRef.getIdClass()); |
| 219 | |
| 220 | IdClassAnnotation idClassAnnotation = (IdClassAnnotation) resourceType.addAnnotation(IdClassAnnotation.ANNOTATION_NAME); |
| 221 | this.getJpaProject().synchronizeContextModel(); |
| 222 | assertNotNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
| 223 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 224 | assertNull(idClassRef.getIdClass()); |
| 225 | |
| 226 | // test setting id class name to nonexistent class. test class name is set, but class is null |
| 227 | String nonExistentIdClassName = PACKAGE_NAME + ".Foo"; |
| 228 | idClassAnnotation.setValue(nonExistentIdClassName); |
| 229 | this.getJpaProject().synchronizeContextModel(); |
| 230 | assertEquals(nonExistentIdClassName, ((IdClassAnnotation) resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)).getValue()); |
| 231 | assertEquals(nonExistentIdClassName, idClassRef.getSpecifiedIdClassName()); |
| 232 | assertNull(idClassRef.getIdClass()); |
| 233 | |
| 234 | // test setting id class name to existent class. test class name is set and class is not null |
| 235 | String existentIdClassName = PACKAGE_NAME + ".TestTypeId"; |
| 236 | idClassAnnotation.setValue(existentIdClassName); |
| 237 | this.getJpaProject().synchronizeContextModel(); |
| 238 | assertEquals(existentIdClassName, ((IdClassAnnotation) resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)).getValue()); |
| 239 | assertEquals(existentIdClassName, idClassRef.getSpecifiedIdClassName()); |
| 240 | assertNotNull(idClassRef.getIdClass()); |
| 241 | |
| 242 | //test setting @IdClass value to null, IdClass annotation is removed |
| 243 | idClassRef.setSpecifiedIdClassName(null); |
| 244 | this.getJpaProject().synchronizeContextModel(); |
| 245 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
| 246 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 247 | assertNull(idClassRef.getIdClass()); |
| 248 | |
| 249 | //reset @IdClass value and then remove @IdClass |
| 250 | idClassAnnotation = (IdClassAnnotation) resourceType.addAnnotation(IdClassAnnotation.ANNOTATION_NAME); |
| 251 | idClassAnnotation.setValue(existentIdClassName); |
| 252 | resourceType.removeAnnotation(IdClassAnnotation.ANNOTATION_NAME); |
| 253 | getJpaProject().synchronizeContextModel(); |
| 254 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
| 255 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 256 | assertNull(idClassRef.getIdClass()); |
| 257 | } |
| 258 | |
| 259 | public void testModifyIdClass() throws Exception { |
| 260 | createTestMappedSuperclass(); |
| 261 | createTestIdClass(); |
| 262 | addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| 263 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 264 | JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 265 | MappedSuperclass mappedSuperclass = (MappedSuperclass) getJavaPersistentType().getMapping(); |
| 266 | IdClassReference idClassRef = mappedSuperclass.getIdClassReference(); |
| 267 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 268 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 269 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 270 | assertNull(idClassRef.getIdClass()); |
| 271 | |
| 272 | String nonExistentIdClassName = PACKAGE_NAME + ".Foo"; |
| 273 | idClassRef.setSpecifiedIdClassName(nonExistentIdClassName); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 274 | assertEquals(nonExistentIdClassName, ((IdClassAnnotation) resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)).getValue()); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 275 | assertEquals(nonExistentIdClassName, idClassRef.getSpecifiedIdClassName()); |
| 276 | assertNull(idClassRef.getIdClass()); |
| 277 | |
| 278 | String existentIdClassName = PACKAGE_NAME + ".TestTypeId"; |
| 279 | idClassRef.setSpecifiedIdClassName(existentIdClassName); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 280 | assertEquals(existentIdClassName, ((IdClassAnnotation) resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)).getValue()); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 281 | assertEquals(existentIdClassName, idClassRef.getSpecifiedIdClassName()); |
| 282 | assertNotNull(idClassRef.getIdClass()); |
| 283 | |
| 284 | idClassRef.setSpecifiedIdClassName(null); |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 285 | assertNull(resourceType.getAnnotation(IdClassAnnotation.ANNOTATION_NAME)); |
kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 286 | assertNull(idClassRef.getSpecifiedIdClassName()); |
| 287 | assertNull(idClassRef.getIdClass()); |
| 288 | } |
| 289 | } |