diff options
author | kmoore | 2011-08-04 00:34:05 +0000 |
---|---|---|
committer | kmoore | 2011-08-04 00:34:05 +0000 |
commit | 77be67e4785ca5ec392718b531bec67f75c5762c (patch) | |
tree | 9a99ae8de9c37769fb7ea4ea3472f005e6b3eb4a /jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa | |
parent | f5282012d8d545c3133391f0611bba8f534030ed (diff) | |
download | webtools.dali-77be67e4785ca5ec392718b531bec67f75c5762c.tar.gz webtools.dali-77be67e4785ca5ec392718b531bec67f75c5762c.tar.xz webtools.dali-77be67e4785ca5ec392718b531bec67f75c5762c.zip |
Bug 353575 - some supported annotations removed when changing attribute/type mapping
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa')
11 files changed, 2815 insertions, 23 deletions
diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/Generic2_0JavaContextModelTests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/Generic2_0JavaContextModelTests.java index 8903efdb92..2f80cfeb5f 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/Generic2_0JavaContextModelTests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/Generic2_0JavaContextModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 Oracle. All rights reserved. + * Copyright (c) 2009, 2011 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -19,11 +19,14 @@ public class Generic2_0JavaContextModelTests public static Test suite() { TestSuite suite = new TestSuite(Generic2_0JavaContextModelTests.class.getPackage().getName()); suite.addTestSuite(GenericJavaAssociationOverride2_0Tests.class); + suite.addTestSuite(GenericJavaBasicMapping2_0Tests.class); suite.addTestSuite(GenericJavaCascade2_0Tests.class); suite.addTestSuite(GenericJavaCollectionTable2_0Tests.class); suite.addTestSuite(GenericJavaElementCollectionMapping2_0Tests.class); suite.addTestSuite(GenericJavaEntity2_0Tests.class); suite.addTestSuite(GenericJavaEmbeddedMapping2_0Tests.class); + suite.addTestSuite(GenericJavaEmbeddedIdMapping2_0Tests.class); + suite.addTestSuite(GenericJavaIdMapping2_0Tests.class); suite.addTestSuite(GenericJavaManyToManyMapping2_0Tests.class); suite.addTestSuite(GenericJavaManyToOneMapping2_0Tests.class); suite.addTestSuite(GenericJavaOneToManyMapping2_0Tests.class); @@ -31,6 +34,7 @@ public class Generic2_0JavaContextModelTests suite.addTestSuite(GenericJavaPersistentAttribute2_0Tests.class); suite.addTestSuite(GenericJavaPersistentType2_0Tests.class); suite.addTestSuite(GenericJavaSequenceGenerator2_0Tests.class); + suite.addTestSuite(GenericJavaVersionMapping2_0Tests.class); return suite; } diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaBasicMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaBasicMapping2_0Tests.java new file mode 100644 index 0000000000..f957f6b0a7 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaBasicMapping2_0Tests.java @@ -0,0 +1,417 @@ +/******************************************************************************* + * Copyright (c) 2011 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceField; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; +import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.EnumType; +import org.eclipse.jpt.jpa.core.context.EnumeratedConverter; +import org.eclipse.jpt.jpa.core.context.FetchType; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; +import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TemporalConverter; +import org.eclipse.jpt.jpa.core.context.TemporalType; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ColumnAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EnumeratedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.LobAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TemporalAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; +import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; + +@SuppressWarnings("nls") +public class GenericJavaBasicMapping2_0Tests extends Generic2_0ContextModelTestCase +{ + + private ICompilationUnit createTestEntityWithBasicMapping() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic").append(CR); + } + }); + } + + public GenericJavaBasicMapping2_0Tests(String name) { + super(name); + } + + public void testBasicMorphToDefaultBasic() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setConverter(EnumeratedConverter.class); + ((EnumeratedConverter) basicMapping.getConverter()).setSpecifiedEnumType(EnumType.STRING); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(EnumType.STRING, ((EnumeratedConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getEnumType()); + + assertNull(((BasicMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); + assertNull(((BasicMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToId() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) basicMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IdMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((IdMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToVersion() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) basicMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((VersionMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((VersionMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToEmbedded() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToEmbeddedId() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToTransient() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToOneToOne() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + +//TODO assertEquals(FetchType.EAGER, ((OneToOneMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertEquals(Boolean.FALSE, ((OneToOneMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToOneToMany() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + +//TODO assertEquals(FetchType.EAGER, ((OneToManyMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + } + public void testBasicMorphToManyToOne() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + +//TODO assertEquals(FetchType.EAGER, ((ManyToOneMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertEquals(Boolean.FALSE, ((ManyToOneMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToManyToMany() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + +// assertEquals(FetchType.EAGER, ((ManyToManyMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + } + + public void testBasicMorphToElementCollection() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + resourceField.addAnnotation(TemporalAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(LobAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(EnumeratedAnnotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + +// assertEquals(FetchType.EAGER, ((ElementCollectionMapping2_0) persistentAttribute.getMapping()).getSpecifiedFetch()); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(LobAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EnumeratedAnnotation.ANNOTATION_NAME)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java index 2267409676..395b7d260f 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java @@ -46,10 +46,12 @@ import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.OrderColumn2_0; import org.eclipse.jpt.jpa.core.jpa2.context.Orderable2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaElementCollectionMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyClass2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyColumn2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.AssociationOverrideAnnotation; import org.eclipse.jpt.jpa.core.resource.java.AttributeOverrideAnnotation; import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; import org.eclipse.jpt.jpa.core.resource.java.ColumnAnnotation; @@ -346,16 +348,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToDefault() throws Exception { @@ -364,14 +372,20 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToVersionMapping() throws Exception { @@ -380,16 +394,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToIdMapping() throws Exception { @@ -398,16 +418,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof IdMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToEmbeddedMapping() throws Exception { @@ -416,16 +442,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToEmbeddedIdMapping() throws Exception { @@ -434,16 +466,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToTransientMapping() throws Exception { @@ -452,16 +490,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToManyToOneMapping() throws Exception { @@ -470,16 +514,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToOneToManyMapping() throws Exception { @@ -488,16 +538,22 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToManyToManyMapping() throws Exception { @@ -506,16 +562,23 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(elementCollectionMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testUpdateSpecifiedTargetEntity() throws Exception { diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedIdMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedIdMapping2_0Tests.java new file mode 100644 index 0000000000..a8557f089f --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedIdMapping2_0Tests.java @@ -0,0 +1,368 @@ +/******************************************************************************* + * Copyright (c) 2011 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; +import org.eclipse.jpt.common.core.resource.java.JavaResourceField; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; +import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; +import org.eclipse.jpt.jpa.core.context.java.JavaEmbeddedMapping; +import org.eclipse.jpt.jpa.core.internal.jpa1.context.java.GenericJavaNullAttributeMapping; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.AttributeOverrideAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; +import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; + +@SuppressWarnings("nls") +public class GenericJavaEmbeddedIdMapping2_0Tests extends Generic2_0ContextModelTestCase +{ + + public static final String EMBEDDABLE_TYPE_NAME = "MyEmbeddable"; + public static final String FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME = PACKAGE_NAME + "." + EMBEDDABLE_TYPE_NAME; + + private ICompilationUnit createTestEntityWithEmbeddedIdMapping() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDED_ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@EmbeddedId").append(CR); + sb.append(CR); + sb.append(" private " + EMBEDDABLE_TYPE_NAME +" myEmbeddedId;").append(CR); + sb.append(CR); + } + }); + } + + private void createEmbeddableType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.EMBEDDABLE); + sb.append(";"); + sb.append(CR); + sb.append("@Embeddable"); + sb.append(CR); + sb.append("public class ").append(EMBEDDABLE_TYPE_NAME).append(" {"); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" "); + sb.append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, EMBEDDABLE_TYPE_NAME + ".java", sourceWriter); + } + + + public GenericJavaEmbeddedIdMapping2_0Tests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(((JavaEmbeddedMapping) persistentAttribute.getMapping()).getAttributeOverrideContainer().getOverrides().iterator().hasNext()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testDefaultEmbeddedIdMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof GenericJavaNullAttributeMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IdMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + EmbeddedIdMapping embeddedIdMapping = (EmbeddedIdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + + assertNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + } +} diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedMapping2_0Tests.java index f404e5a4a8..194856a99c 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaEmbeddedMapping2_0Tests.java @@ -55,8 +55,12 @@ import org.eclipse.jpt.jpa.core.context.java.JavaVirtualAssociationOverride; import org.eclipse.jpt.jpa.core.context.java.JavaVirtualAttributeOverride; import org.eclipse.jpt.jpa.core.context.persistence.ClassRef; import org.eclipse.jpt.jpa.core.internal.jpa1.context.java.GenericJavaNullAttributeMapping; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.VirtualOverrideRelationship2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaEmbeddedMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.jpa.core.resource.java.AssociationOverrideAnnotation; import org.eclipse.jpt.jpa.core.resource.java.AttributeOverrideAnnotation; import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; @@ -266,17 +270,22 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributeNamed("myEmbedded"); JavaEmbeddedMapping embeddedMapping = (JavaEmbeddedMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); assertFalse(persistentAttribute.getMapping().isDefault()); - JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); - JavaResourceField resourceField = resourceType.getFields().iterator().next(); assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToDefault() throws Exception { @@ -290,6 +299,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); @@ -297,7 +308,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertTrue(persistentAttribute.getMapping().isDefault()); assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testDefaultEmbeddedMapping() throws Exception { @@ -399,6 +412,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); @@ -406,7 +421,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToTransientMapping() throws Exception { @@ -419,6 +436,7 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); @@ -426,7 +444,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToIdMapping() throws Exception { @@ -439,6 +459,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); @@ -446,7 +468,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToEmbeddedIdMapping() throws Exception { @@ -459,6 +483,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); @@ -466,7 +492,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToOneToOneMapping() throws Exception { @@ -479,6 +507,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); @@ -486,7 +516,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToOneToManyMapping() throws Exception { @@ -499,6 +531,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); @@ -507,6 +541,7 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToManyToOneMapping() throws Exception { @@ -519,6 +554,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); @@ -526,7 +563,9 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testMorphToManyToManyMapping() throws Exception { @@ -539,6 +578,8 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); JavaResourceField resourceField = resourceType.getFields().iterator().next(); resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); assertFalse(embeddedMapping.isDefault()); persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); @@ -546,7 +587,33 @@ public class GenericJavaEmbeddedMapping2_0Tests extends Generic2_0ContextModelTe assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributeNamed("myEmbedded"); + JavaEmbeddedMapping embeddedMapping = (JavaEmbeddedMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + resourceField.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE); + resourceField.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + + assertNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); assertNotNull(resourceField.getAnnotation(0, AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, AssociationOverrideAnnotation.ANNOTATION_NAME)); } public void testSpecifiedAttributeOverrides() throws Exception { diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaIdMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaIdMapping2_0Tests.java new file mode 100644 index 0000000000..263c0dde49 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaIdMapping2_0Tests.java @@ -0,0 +1,423 @@ +/******************************************************************************* + * Copyright (c) 2011 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceField; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; +import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; +import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TemporalConverter; +import org.eclipse.jpt.jpa.core.context.TemporalType; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ColumnAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.GeneratedValueAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.SequenceGeneratorAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TableGeneratorAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TemporalAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; +import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; + +@SuppressWarnings("nls") +public class GenericJavaIdMapping2_0Tests extends Generic2_0ContextModelTestCase +{ + + private ICompilationUnit createTestEntityWithIdMapping() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + } + }); + } + + public GenericJavaIdMapping2_0Tests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((VersionMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((VersionMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME));//OneToOne with Id annotation is valid in JPA 2.0 + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME));//ManyToOne with Id annotation is valid in JPA 2.0 + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } + + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + IdMapping idMapping = (IdMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) idMapping.getConverter()).setTemporalType(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.getGeneratorContainer().addTableGenerator(); + idMapping.getGeneratorContainer().addSequenceGenerator(); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + + assertNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TableGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(SequenceGeneratorAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(GeneratedValueAnnotation.ANNOTATION_NAME)); + } +} diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToManyMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToManyMapping2_0Tests.java index fcdcf70534..40d500277c 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToManyMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToManyMapping2_0Tests.java @@ -18,30 +18,55 @@ import org.eclipse.jpt.common.core.resource.java.JavaResourceType; import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject.SourceWriter; import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; import org.eclipse.jpt.jpa.core.context.AttributeMapping; import org.eclipse.jpt.jpa.core.context.BasicMapping; import org.eclipse.jpt.jpa.core.context.Embeddable; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; import org.eclipse.jpt.jpa.core.context.Entity; +import org.eclipse.jpt.jpa.core.context.IdMapping; import org.eclipse.jpt.jpa.core.context.JoinTableRelationshipStrategy; import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; import org.eclipse.jpt.jpa.core.context.PersistentAttribute; import org.eclipse.jpt.jpa.core.context.PersistentType; import org.eclipse.jpt.jpa.core.context.ReadOnlyAttributeOverride; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; import org.eclipse.jpt.jpa.core.context.java.JavaAttributeOverride; import org.eclipse.jpt.jpa.core.context.java.JavaAttributeOverrideContainer; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType; import org.eclipse.jpt.jpa.core.context.java.JavaVirtualAttributeOverride; import org.eclipse.jpt.jpa.core.context.persistence.ClassRef; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.ManyToManyMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.OrderColumn2_0; import org.eclipse.jpt.jpa.core.jpa2.context.Orderable2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaManyToManyMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyClass2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyColumn2_0Annotation; import org.eclipse.jpt.jpa.core.resource.java.AttributeOverrideAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.JoinTableAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; import org.eclipse.jpt.jpa.core.resource.java.MapKeyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OrderByAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; @SuppressWarnings("nls") @@ -314,6 +339,268 @@ public class GenericJavaManyToManyMapping2_0Tests this.javaProject.createCompilationUnit(PACKAGE_NAME, "PropertyInfo.java", sourceWriter); } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithValidManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToManyMapping manyToManyMapping = (ManyToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + manyToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + manyToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } public void testCandidateMappedByAttributeNames() throws Exception { createTestEntityWithValidManyToManyMapping(); diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToOneMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToOneMapping2_0Tests.java index 89fbbd6135..8d38365920 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToOneMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaManyToOneMapping2_0Tests.java @@ -16,21 +16,47 @@ import org.eclipse.jpt.common.core.resource.java.JavaResourceType; import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; import org.eclipse.jpt.jpa.core.MappingKeys; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.JoinColumnRelationshipStrategy; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; import org.eclipse.jpt.jpa.core.context.java.JavaBasicMapping; import org.eclipse.jpt.jpa.core.context.java.JavaIdMapping; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType; import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentType; import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyPersistentAttribute; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.ManyToOneMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.ManyToOneRelationship2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaDerivedIdentity2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaManyToOneMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOneToOneMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapsId2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.JoinColumnAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; @SuppressWarnings("nls") @@ -106,6 +132,257 @@ public class GenericJavaManyToOneMapping2_0Tests } } + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + ManyToOneMapping manyToOneMapping = (ManyToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = manyToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + public void testUpdateId() throws Exception { createTestEntityWithIdDerivedIdentity(); addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToManyMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToManyMapping2_0Tests.java index 08c34867c0..498dd1085d 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToManyMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToManyMapping2_0Tests.java @@ -19,15 +19,24 @@ import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject.SourceWriter; import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable; import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; import org.eclipse.jpt.jpa.core.context.AttributeMapping; import org.eclipse.jpt.jpa.core.context.BasicMapping; import org.eclipse.jpt.jpa.core.context.Embeddable; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; import org.eclipse.jpt.jpa.core.context.Entity; +import org.eclipse.jpt.jpa.core.context.IdMapping; import org.eclipse.jpt.jpa.core.context.JoinColumn; import org.eclipse.jpt.jpa.core.context.JoinColumnRelationship; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; import org.eclipse.jpt.jpa.core.context.PersistentAttribute; import org.eclipse.jpt.jpa.core.context.ReadOnlyAttributeOverride; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; import org.eclipse.jpt.jpa.core.context.java.JavaAttributeOverride; import org.eclipse.jpt.jpa.core.context.java.JavaAttributeOverrideContainer; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType; @@ -40,14 +49,26 @@ import org.eclipse.jpt.jpa.core.jpa2.context.Orderable2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOneToManyMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOrphanRemovable2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOrphanRemovalHolder2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyClass2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapKeyColumn2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.OneToMany2_0Annotation; import org.eclipse.jpt.jpa.core.resource.java.AttributeOverrideAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.JoinTableAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; import org.eclipse.jpt.jpa.core.resource.java.MapKeyAnnotation; import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OrderByAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; @SuppressWarnings("nls") @@ -340,6 +361,244 @@ public class GenericJavaOneToManyMapping2_0Tests this.javaProject.createCompilationUnit(PACKAGE_NAME, "PropertyInfo.java", sourceWriter); } + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithValidOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + oneToManyMapping.getOrderable().setSpecifiedOrderBy("asdf"); + oneToManyMapping.getRelationship().getJoinTableStrategy().getJoinTable().setSpecifiedName("FOO"); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(JoinTableAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(OrderByAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + public void testCandidateMappedByAttributeNames() throws Exception { createTestEntityWithValidOneToManyMapping(); createTestTargetEntityAddress(); diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToOneMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToOneMapping2_0Tests.java index e0549f4fdd..bd18b5f42f 100644 --- a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToOneMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaOneToOneMapping2_0Tests.java @@ -19,12 +19,24 @@ import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable; import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; import org.eclipse.jpt.jpa.core.MappingKeys; import org.eclipse.jpt.jpa.core.context.AttributeMapping; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.JoinColumnRelationshipStrategy; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; import org.eclipse.jpt.jpa.core.context.OneToOneMapping; import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute; import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType; import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentType; import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyPersistentAttribute; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.OneToOneMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.OneToOneRelationship2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaDerivedIdentity2_0; @@ -32,11 +44,23 @@ import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaManyToOneMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOneToOneMapping2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOrphanRemovable2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOrphanRemovalHolder2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.jpa.core.jpa2.resource.java.MapsId2_0Annotation; import org.eclipse.jpt.jpa.core.jpa2.resource.java.OneToOne2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.JoinColumnAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; @SuppressWarnings("nls") @@ -265,6 +289,257 @@ public class GenericJavaOneToOneMapping2_0Tests this.javaProject.createCompilationUnit(PACKAGE_NAME, "State.java", sourceWriter); } + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof BasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof VersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithValidOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + OneToOneMapping oneToOneMapping = (OneToOneMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + JoinColumnRelationshipStrategy joinColumns = oneToOneMapping.getRelationship().getJoinColumnStrategy(); + joinColumns.addSpecifiedJoinColumn(0); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + assertFalse(persistentAttribute.getMapping().isDefault()); + + assertNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(0, JoinColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + } + public void testUpdateId() throws Exception { createTestEntityWithIdDerivedIdentity(); addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); diff --git a/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaVersionMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaVersionMapping2_0Tests.java new file mode 100644 index 0000000000..5fd0a4ff32 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.jpa.core.tests/src/org/eclipse/jpt/jpa/core/tests/internal/jpa2/context/java/GenericJavaVersionMapping2_0Tests.java @@ -0,0 +1,352 @@ +/******************************************************************************* + * Copyright (c) 2011 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceField; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind; +import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator; +import org.eclipse.jpt.jpa.core.MappingKeys; +import org.eclipse.jpt.jpa.core.context.BasicMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping; +import org.eclipse.jpt.jpa.core.context.EmbeddedMapping; +import org.eclipse.jpt.jpa.core.context.IdMapping; +import org.eclipse.jpt.jpa.core.context.ManyToManyMapping; +import org.eclipse.jpt.jpa.core.context.ManyToOneMapping; +import org.eclipse.jpt.jpa.core.context.OneToManyMapping; +import org.eclipse.jpt.jpa.core.context.OneToOneMapping; +import org.eclipse.jpt.jpa.core.context.PersistentAttribute; +import org.eclipse.jpt.jpa.core.context.TemporalConverter; +import org.eclipse.jpt.jpa.core.context.TemporalType; +import org.eclipse.jpt.jpa.core.context.TransientMapping; +import org.eclipse.jpt.jpa.core.context.VersionMapping; +import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation; +import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation; +import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ColumnAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TemporalAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation; +import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation; +import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; + +@SuppressWarnings("nls") +public class GenericJavaVersionMapping2_0Tests extends Generic2_0ContextModelTestCase +{ + private ICompilationUnit createTestEntityWithVersionMapping() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.VERSION); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Version").append(CR); + } + }); + } + + public GenericJavaVersionMapping2_0Tests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IdMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((TemporalConverter) ((IdMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType()); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof TransientMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } + + public void testMorphToElementCollectionMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next(); + VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping(); + JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE); + JavaResourceField resourceField = resourceType.getFields().iterator().next(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setConverter(TemporalConverter.class); + ((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME); + resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0); + + assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME)); + assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME)); + } +} |