diff options
author | kmoore | 2010-02-15 17:31:14 +0000 |
---|---|---|
committer | kmoore | 2010-02-15 17:31:14 +0000 |
commit | ee46ae6f23ecbc526bd49120af303629bfedf68e (patch) | |
tree | efd2572335169357ddd60cb9bb151fb28ef67a47 /jpa/tests | |
parent | c0f64210215f0d7dd0a87d4c244a08d6a6d07cba (diff) | |
download | webtools.dali-ee46ae6f23ecbc526bd49120af303629bfedf68e.tar.gz webtools.dali-ee46ae6f23ecbc526bd49120af303629bfedf68e.tar.xz webtools.dali-ee46ae6f23ecbc526bd49120af303629bfedf68e.zip |
refactored column validation and added new validation
Diffstat (limited to 'jpa/tests')
12 files changed, 3114 insertions, 16 deletions
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmEmbeddedMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmEmbeddedMappingTests.java index 1b4f4b2543..92ee2ed8d0 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmEmbeddedMappingTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmEmbeddedMappingTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Oracle. All rights reserved. + * Copyright (c) 2007, 2010 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. @@ -79,7 +79,7 @@ public class OrmEmbeddedMappingTests extends ContextModelTestCase sb.append(CR); sb.append(" @Embedded"); sb.append(CR); - sb.append(" @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))"); + sb.append(" @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))").append(CR); sb.append(CR); sb.append(" private Address address;").append(CR); sb.append(CR); diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java index adb6ec613e..8e3ac0cef9 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/java/GenericJavaElementCollectionMapping2_0Tests.java @@ -13,7 +13,10 @@ import java.util.Iterator; import java.util.ListIterator; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jpt.core.MappingKeys; +import org.eclipse.jpt.core.context.AttributeOverride; +import org.eclipse.jpt.core.context.AttributeOverrideContainer; import org.eclipse.jpt.core.context.BasicMapping; +import org.eclipse.jpt.core.context.Embeddable; import org.eclipse.jpt.core.context.EmbeddedIdMapping; import org.eclipse.jpt.core.context.EmbeddedMapping; import org.eclipse.jpt.core.context.FetchType; @@ -22,18 +25,25 @@ import org.eclipse.jpt.core.context.ManyToManyMapping; import org.eclipse.jpt.core.context.ManyToOneMapping; import org.eclipse.jpt.core.context.OneToManyMapping; import org.eclipse.jpt.core.context.PersistentAttribute; +import org.eclipse.jpt.core.context.PersistentType; import org.eclipse.jpt.core.context.TransientMapping; import org.eclipse.jpt.core.context.TypeMapping; import org.eclipse.jpt.core.context.VersionMapping; +import org.eclipse.jpt.core.context.java.JavaAttributeOverride; +import org.eclipse.jpt.core.context.java.JavaAttributeOverrideContainer; import org.eclipse.jpt.core.context.java.JavaPersistentType; import org.eclipse.jpt.core.context.persistence.ClassRef; import org.eclipse.jpt.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.core.jpa2.context.OrderColumn2_0; import org.eclipse.jpt.core.jpa2.context.Orderable2_0; +import org.eclipse.jpt.core.jpa2.context.java.JavaElementCollectionMapping2_0; import org.eclipse.jpt.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.core.jpa2.resource.java.MapKeyClass2_0Annotation; +import org.eclipse.jpt.core.resource.java.AttributeOverrideAnnotation; +import org.eclipse.jpt.core.resource.java.AttributeOverridesAnnotation; import org.eclipse.jpt.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.core.resource.java.ColumnAnnotation; import org.eclipse.jpt.core.resource.java.EmbeddedAnnotation; import org.eclipse.jpt.core.resource.java.EmbeddedIdAnnotation; import org.eclipse.jpt.core.resource.java.IdAnnotation; @@ -43,6 +53,7 @@ import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; import org.eclipse.jpt.core.resource.java.ManyToManyAnnotation; import org.eclipse.jpt.core.resource.java.ManyToOneAnnotation; import org.eclipse.jpt.core.resource.java.MapKeyAnnotation; +import org.eclipse.jpt.core.resource.java.NestableAnnotation; import org.eclipse.jpt.core.resource.java.OneToManyAnnotation; import org.eclipse.jpt.core.resource.java.TransientAnnotation; import org.eclipse.jpt.core.resource.java.VersionAnnotation; @@ -54,6 +65,9 @@ import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0ContextModelTestCase { + public static final String EMBEDDABLE_TYPE_NAME = "Address"; + public static final String FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME = PACKAGE_NAME + "." + EMBEDDABLE_TYPE_NAME; + private ICompilationUnit createTestEntityWithElementCollectionMapping() throws Exception { return this.createTestType(new DefaultAnnotationWriter() { @Override @@ -950,4 +964,445 @@ public class GenericJavaElementCollectionMapping2_0Tests extends Generic2_0Conte assertEquals("addresses_ORDER", orderColumn.getDefaultName()); assertEquals(TYPE_NAME + "_addresses", orderColumn.getTable()); } + + public void testGetValueColumn() throws Exception { + createTestEntityWithElementCollectionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getSpecifiedMapping(); + + assertNull(elementCollectionMapping.getValueColumn().getSpecifiedName()); + assertEquals("id", elementCollectionMapping.getValueColumn().getName()); + assertEquals(TYPE_NAME + "_id", elementCollectionMapping.getValueColumn().getTable()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + ColumnAnnotation column = (ColumnAnnotation) attributeResource.addAnnotation(JPA.COLUMN); + column.setName("foo"); + getJpaProject().synchronizeContextModel(); + + assertEquals("foo", elementCollectionMapping.getValueColumn().getSpecifiedName()); + assertEquals("foo", elementCollectionMapping.getValueColumn().getName()); + assertEquals("id", elementCollectionMapping.getValueColumn().getDefaultName()); + } + + public void testSpecifiedAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + JavaAttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + ListIterator<JavaAttributeOverride> specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + + assertFalse(specifiedAttributeOverrides.hasNext()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.moveAnnotation(1, 0, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertFalse(specifiedAttributeOverrides.hasNext()); + } + + public void testVirtualAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + assertEquals("addresses", attributeResource.getName()); + assertNull(attributeResource.getAnnotation(AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(attributeResource.getAnnotation(AttributeOverridesAnnotation.ANNOTATION_NAME)); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + AttributeOverride defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME +"_addresses", defaultAttributeOverride.getColumn().getTable()); + assertEquals(null, defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(true, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(255, defaultAttributeOverride.getColumn().getLength()); + assertEquals(0, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(0, defaultAttributeOverride.getColumn().getScale()); + + + ListIterator<ClassRef> classRefs = getPersistenceUnit().specifiedClassRefs(); + classRefs.next(); + Embeddable embeddable = (Embeddable) classRefs.next().getJavaPersistentType().getMapping(); + + BasicMapping cityMapping = (BasicMapping) embeddable.getPersistentType().getAttributeNamed("city").getMapping(); + cityMapping.getColumn().setSpecifiedName("FOO"); + cityMapping.getColumn().setSpecifiedTable("BAR"); + cityMapping.getColumn().setColumnDefinition("COLUMN_DEF"); + cityMapping.getColumn().setSpecifiedInsertable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedUpdatable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + cityMapping.getColumn().setSpecifiedNullable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedLength(Integer.valueOf(5)); + cityMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(6)); + cityMapping.getColumn().setSpecifiedScale(Integer.valueOf(7)); + + assertEquals("addresses", attributeResource.getName()); + assertNull(attributeResource.getAnnotation(AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(attributeResource.getAnnotation(AttributeOverridesAnnotation.ANNOTATION_NAME)); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("FOO", defaultAttributeOverride.getColumn().getName()); + assertEquals("BAR", defaultAttributeOverride.getColumn().getTable()); + assertEquals("COLUMN_DEF", defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(false, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(5, defaultAttributeOverride.getColumn().getLength()); + assertEquals(6, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(7, defaultAttributeOverride.getColumn().getScale()); + + cityMapping.getColumn().setSpecifiedName(null); + cityMapping.getColumn().setSpecifiedTable(null); + cityMapping.getColumn().setColumnDefinition(null); + cityMapping.getColumn().setSpecifiedInsertable(null); + cityMapping.getColumn().setSpecifiedUpdatable(null); + cityMapping.getColumn().setSpecifiedUnique(null); + cityMapping.getColumn().setSpecifiedNullable(null); + cityMapping.getColumn().setSpecifiedLength(null); + cityMapping.getColumn().setSpecifiedPrecision(null); + cityMapping.getColumn().setSpecifiedScale(null); + defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME +"_addresses", defaultAttributeOverride.getColumn().getTable()); + assertEquals(null, defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(true, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(255, defaultAttributeOverride.getColumn().getLength()); + assertEquals(0, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(0, defaultAttributeOverride.getColumn().getScale()); + + AttributeOverrideAnnotation annotation = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + annotation.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testSpecifiedAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(0, attributeOverrideContainer.specifiedAttributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + + assertEquals(2, attributeOverrideContainer.specifiedAttributeOverridesSize()); + } + + public void testAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + + assertEquals(6, attributeOverrideContainer.attributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(6, attributeOverrideContainer.attributeOverridesSize()); + } + + public void testVirtualAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + getJpaProject().synchronizeContextModel(); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("state.name"); + getJpaProject().synchronizeContextModel(); + assertEquals(2, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testAttributeOverrideSetVirtual() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + + attributeOverrideContainer.specifiedAttributeOverrides().next().setVirtual(true); + attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + + assertEquals("state.abbr", attributeOverrideContainer.virtualAttributeOverrides().next().getName()); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverrideContainer.specifiedAttributeOverrides().next().setVirtual(true); + attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + assertFalse(attributeOverrides.hasNext()); + + Iterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + assertEquals("state.abbr", virtualAttributeOverrides.next().getName()); + assertEquals("zip", virtualAttributeOverrides.next().getName()); + assertEquals("city", virtualAttributeOverrides.next().getName()); + assertEquals("state.name", virtualAttributeOverrides.next().getName()); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testAttributeOverrideSetVirtual2() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + ListIterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + virtualAttributeOverrides.next(); + virtualAttributeOverrides.next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + attributeResource.moveAnnotation(1, 0, AttributeOverridesAnnotation.ANNOTATION_NAME); + + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testNestedVirtualAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ListIterator<ClassRef> specifiedClassRefs = getPersistenceUnit().specifiedClassRefs(); + PersistentType persistentType = specifiedClassRefs.next().getJavaPersistentType(); + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentType.getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + ListIterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + AttributeOverride virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("city", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", virtualAttributeOverride.getName()); + assertEquals(false, virtualAttributeOverrides.hasNext()); + + + PersistentType addressPersistentType = specifiedClassRefs.next().getJavaPersistentType(); + EmbeddedMapping nestedEmbeddedMapping = (EmbeddedMapping) addressPersistentType.getAttributeNamed("state").getMapping(); + AttributeOverrideContainer nestedAttributeOverrideContainer = nestedEmbeddedMapping.getAttributeOverrideContainer(); + assertEquals(2, nestedAttributeOverrideContainer.virtualAttributeOverridesSize()); + virtualAttributeOverrides = nestedAttributeOverrideContainer.virtualAttributeOverrides(); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("name", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("abbr", virtualAttributeOverride.getName()); + + PersistentType statePersistentType = specifiedClassRefs.next().getJavaPersistentType(); + BasicMapping abbrMapping = (BasicMapping) statePersistentType.getAttributeNamed("abbr").getMapping(); + abbrMapping.getColumn().setSpecifiedName("BLAH"); + abbrMapping.getColumn().setSpecifiedTable("BLAH_TABLE"); + abbrMapping.getColumn().setColumnDefinition("COLUMN_DEFINITION"); + abbrMapping.getColumn().setSpecifiedInsertable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedUpdatable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + abbrMapping.getColumn().setSpecifiedNullable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedLength(Integer.valueOf(5)); + abbrMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(6)); + abbrMapping.getColumn().setSpecifiedScale(Integer.valueOf(7)); + + //check the nested embedded (Address.state) attribute override to verify it is getting settings from the specified column on State.abbr + virtualAttributeOverride = ((EmbeddedMapping) addressPersistentType.getAttributeNamed("state").getMapping()).getAttributeOverrideContainer().getAttributeOverrideNamed("abbr"); + assertEquals("abbr", virtualAttributeOverride.getName()); + assertEquals("BLAH", virtualAttributeOverride.getColumn().getName()); + assertEquals("BLAH_TABLE", virtualAttributeOverride.getColumn().getTable()); + assertEquals("COLUMN_DEFINITION", virtualAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, virtualAttributeOverride.getColumn().isInsertable()); + assertEquals(false, virtualAttributeOverride.getColumn().isUpdatable()); + assertEquals(true, virtualAttributeOverride.getColumn().isUnique()); + assertEquals(false, virtualAttributeOverride.getColumn().isNullable()); + assertEquals(5, virtualAttributeOverride.getColumn().getLength()); + assertEquals(6, virtualAttributeOverride.getColumn().getPrecision()); + assertEquals(7, virtualAttributeOverride.getColumn().getScale()); + } } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmElementCollectionMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmElementCollectionMapping2_0Tests.java index a53aa1eb5d..6b2c4269bf 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmElementCollectionMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmElementCollectionMapping2_0Tests.java @@ -10,10 +10,12 @@ package org.eclipse.jpt.core.tests.internal.jpa2.context.orm; import java.util.Iterator; +import java.util.ListIterator; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jpt.core.JptCorePlugin; import org.eclipse.jpt.core.MappingKeys; import org.eclipse.jpt.core.context.BasicMapping; +import org.eclipse.jpt.core.context.Column; import org.eclipse.jpt.core.context.EmbeddedIdMapping; import org.eclipse.jpt.core.context.EmbeddedMapping; import org.eclipse.jpt.core.context.FetchType; @@ -23,6 +25,11 @@ import org.eclipse.jpt.core.context.ManyToOneMapping; import org.eclipse.jpt.core.context.OneToManyMapping; import org.eclipse.jpt.core.context.TransientMapping; import org.eclipse.jpt.core.context.VersionMapping; +import org.eclipse.jpt.core.context.java.JavaBasicMapping; +import org.eclipse.jpt.core.context.orm.OrmAttributeOverride; +import org.eclipse.jpt.core.context.orm.OrmAttributeOverrideContainer; +import org.eclipse.jpt.core.context.orm.OrmColumn; +import org.eclipse.jpt.core.context.orm.OrmEntity; import org.eclipse.jpt.core.context.orm.OrmOneToManyMapping; import org.eclipse.jpt.core.context.orm.OrmPersistentAttribute; import org.eclipse.jpt.core.context.orm.OrmPersistentType; @@ -47,6 +54,9 @@ import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; @SuppressWarnings("nls") public class GenericOrmElementCollectionMapping2_0Tests extends Generic2_0ContextModelTestCase { + private static final String ATTRIBUTE_OVERRIDE_NAME = "city"; + private static final String ATTRIBUTE_OVERRIDE_COLUMN_NAME = "E_CITY"; + public GenericOrmElementCollectionMapping2_0Tests(String name) { super(name); } @@ -117,7 +127,7 @@ public class GenericOrmElementCollectionMapping2_0Tests extends Generic2_0Contex return this.createTestType(new DefaultAnnotationWriter() { @Override public Iterator<String> imports() { - return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.FETCH_TYPE); + return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.FETCH_TYPE, JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); } @Override public void appendTypeAnnotationTo(StringBuilder sb) { @@ -129,6 +139,7 @@ public class GenericOrmElementCollectionMapping2_0Tests extends Generic2_0Contex sb.append(CR); sb.append(" @ElementCollection"); sb.append(CR); + sb.append(" @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))"); sb.append(" private java.util.Collection<Address> addresses;").append(CR); sb.append(CR); sb.append(" @Id"); @@ -931,4 +942,354 @@ public class GenericOrmElementCollectionMapping2_0Tests extends Generic2_0Contex assertEquals(TYPE_NAME + "_addresses", orderable.getOrderColumn().getTable()); assertEquals("FOO", orderable.getOrderColumn().getName()); } + + public void testVirtualValueColumnDefaults() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + //virtual attrubte in orm.xml, java attribute has no value Column annotation + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + assertEquals("addresses", ormColumn.getSpecifiedName()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + assertEquals(null, ormColumn.getColumnDefinition()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedUnique()); + assertEquals(Column.DEFAULT_LENGTH, ormColumn.getSpecifiedLength().intValue()); + assertEquals(Column.DEFAULT_PRECISION, ormColumn.getSpecifiedPrecision().intValue()); + assertEquals(Column.DEFAULT_SCALE, ormColumn.getSpecifiedScale().intValue()); + + //set Column annotation in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getValueColumn().setSpecifiedName("FOO"); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("FOO_TABLE"); + javaElementCollectionMapping.getValueColumn().setColumnDefinition("COLUMN_DEFINITION"); + javaElementCollectionMapping.getValueColumn().setSpecifiedInsertable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUpdatable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedNullable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUnique(Boolean.TRUE); + javaElementCollectionMapping.getValueColumn().setSpecifiedLength(Integer.valueOf(45)); + javaElementCollectionMapping.getValueColumn().setSpecifiedPrecision(Integer.valueOf(46)); + javaElementCollectionMapping.getValueColumn().setSpecifiedScale(Integer.valueOf(47)); + + assertEquals("FOO", ormColumn.getSpecifiedName()); + assertEquals("FOO_TABLE", ormColumn.getSpecifiedTable()); + assertEquals("COLUMN_DEFINITION", ormColumn.getColumnDefinition()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedUnique()); + assertEquals(Integer.valueOf(45), ormColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(46), ormColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(47), ormColumn.getSpecifiedScale()); + + + //set metadata-complete, orm.xml virtual column ignores java column annotation + ormPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.TRUE); + addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + //no longer an element collection mapping + assertEquals(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY, addressesPersistentAttribute.getMappingKey()); + } + + public void testNullColumnDefaults() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "addresses"); + + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + + //set Column annotation in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getValueColumn().setSpecifiedName("FOO"); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("FOO_TABLE"); + javaElementCollectionMapping.getValueColumn().setColumnDefinition("COLUMN_DEFINITION"); + javaElementCollectionMapping.getValueColumn().setSpecifiedInsertable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUpdatable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedNullable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUnique(Boolean.TRUE); + javaElementCollectionMapping.getValueColumn().setSpecifiedLength(Integer.valueOf(45)); + javaElementCollectionMapping.getValueColumn().setSpecifiedPrecision(Integer.valueOf(46)); + javaElementCollectionMapping.getValueColumn().setSpecifiedScale(Integer.valueOf(47)); + + + assertEquals("addresses", ormColumn.getDefaultName()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getDefaultTable()); + assertEquals(true, ormColumn.isDefaultInsertable()); + assertEquals(true, ormColumn.isDefaultUpdatable()); + assertEquals(true, ormColumn.isDefaultNullable()); + assertEquals(false, ormColumn.isDefaultUnique()); + assertEquals(Column.DEFAULT_LENGTH, ormColumn.getDefaultLength()); + assertEquals(Column.DEFAULT_PRECISION, ormColumn.getDefaultPrecision()); + assertEquals(Column.DEFAULT_SCALE, ormColumn.getDefaultScale()); + assertNull(ormColumn.getSpecifiedName()); + assertNull(ormColumn.getSpecifiedTable()); + assertNull(ormColumn.getColumnDefinition()); + assertNull(ormColumn.getSpecifiedInsertable()); + assertNull(ormColumn.getSpecifiedUpdatable()); + assertNull(ormColumn.getSpecifiedNullable()); + assertNull(ormColumn.getSpecifiedUnique()); + assertNull(ormColumn.getSpecifiedLength()); + assertNull(ormColumn.getSpecifiedPrecision()); + assertNull(ormColumn.getSpecifiedScale()); + } + + public void testVirtualValueColumnTable() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + //virtual attribute in orm.xml, java attribute has no Column annotation + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + + //entity table should have no affect on the collection table default name + ((OrmEntity) ormPersistentType.getMapping()).getTable().setSpecifiedName("ORM_TABLE"); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + + //set Column table element in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getCollectionTable().setSpecifiedName("JAVA_COLLECTION_TABLE"); + assertEquals("JAVA_COLLECTION_TABLE", ormColumn.getSpecifiedTable()); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("JAVA_TABLE"); + assertEquals("JAVA_TABLE", ormColumn.getSpecifiedTable()); + + //make name persistent attribute not virtual + addressesPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "addresses"); + addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + ormColumn = addressesVirtualMapping.getValueColumn(); + assertNull(ormColumn.getSpecifiedTable()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getDefaultTable()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + XmlElementCollection_2_0 elementCollectionResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + + elementCollectionResource.getAttributeOverrides().get(0).setName("FOO"); + elementCollectionResource.getAttributeOverrides().get(1).setName("BAR"); + elementCollectionResource.getAttributeOverrides().get(2).setName("BAZ"); + + assertEquals(3, elementCollectionResource.getAttributeOverrides().size()); + + attributeOverrideContainer.moveSpecifiedAttributeOverride(2, 0); + ListIterator<OrmAttributeOverride> attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAR", elementCollectionResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", elementCollectionResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", elementCollectionResource.getAttributeOverrides().get(2).getName()); + + + attributeOverrideContainer.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAZ", elementCollectionResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAR", elementCollectionResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", elementCollectionResource.getAttributeOverrides().get(2).getName()); + } + + public void testUpdateAttributeOverrides() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + XmlElementCollection_2_0 elementCollectionResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + + elementCollectionResource.getAttributeOverrides().get(0).setName("FOO"); + elementCollectionResource.getAttributeOverrides().get(1).setName("BAR"); + elementCollectionResource.getAttributeOverrides().get(2).setName("BAZ"); + + ListIterator<OrmAttributeOverride> attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().move(2, 0); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().move(0, 1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(0); + assertFalse(attributeOverrideContainer.specifiedAttributeOverrides().hasNext()); + } + + public void testElementCollectionMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityWithEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(3, ormPersistentType.virtualAttributesSize()); + + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.specifiedAttributes().next(); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + + assertEquals("foo", ormElementCollectionMapping.getName()); + + + assertFalse(attributeOverrideContainer.specifiedAttributeOverrides().hasNext()); + assertFalse(attributeOverrideContainer.virtualAttributeOverrides().hasNext()); + } + + public void testVirtualAttributeOverrides() throws Exception { + createTestEntityWithEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + OrmPersistentType persistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + OrmPersistentType persistentType2 = getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + OrmPersistentType persistentType3 = getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State"); + + //embedded mapping is virtual, specified attribute overrides should exist + OrmPersistentAttribute ormPersistentAttribute = persistentType.getAttributeNamed("addresses"); + OrmElementCollectionMapping2_0 elementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.specifiedAttributeOverridesSize()); + ListIterator<OrmAttributeOverride> specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + OrmAttributeOverride attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentAttribute.getJavaPersistentAttribute().getMapping(); + Column javaAttributeOverrideColumn = javaElementCollectionMapping.getValueAttributeOverrideContainer().specifiedAttributeOverrides().next().getColumn(); + + javaAttributeOverrideColumn.setSpecifiedName("FOO_COLUMN"); + javaAttributeOverrideColumn.setSpecifiedTable("FOO_TABLE"); + javaAttributeOverrideColumn.setColumnDefinition("COLUMN_DEF"); + javaAttributeOverrideColumn.setSpecifiedInsertable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedUpdatable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedUnique(Boolean.TRUE); + javaAttributeOverrideColumn.setSpecifiedNullable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedLength(Integer.valueOf(5)); + javaAttributeOverrideColumn.setSpecifiedPrecision(Integer.valueOf(6)); + javaAttributeOverrideColumn.setSpecifiedScale(Integer.valueOf(7)); + + JavaBasicMapping javaBasicMapping = (JavaBasicMapping) persistentType3.getJavaPersistentType().getAttributeNamed("name").getMapping(); + javaBasicMapping.getColumn().setSpecifiedName("MY_STATE_COLUMN"); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.specifiedAttributeOverridesSize()); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + assertEquals("FOO_COLUMN", attributeOverride.getColumn().getSpecifiedName()); + assertEquals("FOO_TABLE", attributeOverride.getColumn().getSpecifiedTable()); + assertEquals("COLUMN_DEF", attributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, attributeOverride.getColumn().isInsertable()); + assertEquals(false, attributeOverride.getColumn().isUpdatable()); + assertEquals(true, attributeOverride.getColumn().isUnique()); + assertEquals(false, attributeOverride.getColumn().isNullable()); + assertEquals(5, attributeOverride.getColumn().getLength()); + assertEquals(6, attributeOverride.getColumn().getPrecision()); + assertEquals(7, attributeOverride.getColumn().getScale()); + + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + assertEquals("MY_STATE_COLUMN", attributeOverride.getColumn().getSpecifiedName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + + + + //embedded mapping is specified, virtual attribute overrides should exist + persistentType.getAttributeNamed("addresses").makeSpecified(); + elementCollectionMapping = (OrmElementCollectionMapping2_0) persistentType.getAttributeNamed("addresses").getMapping(); + attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.specifiedAttributeOverridesSize()); + ListIterator<OrmAttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + assertEquals("MY_STATE_COLUMN", attributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME + "_addresses", attributeOverride.getColumn().getTable()); + assertEquals(null, attributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, attributeOverride.getColumn().isInsertable()); + assertEquals(true, attributeOverride.getColumn().isUpdatable()); + assertEquals(false, attributeOverride.getColumn().isUnique()); + assertEquals(true, attributeOverride.getColumn().isNullable()); + assertEquals(255, attributeOverride.getColumn().getLength()); + assertEquals(0, attributeOverride.getColumn().getPrecision()); + assertEquals(0, attributeOverride.getColumn().getScale()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + assertEquals(TYPE_NAME + "_addresses", attributeOverride.getColumn().getDefaultTable()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + + //set one of the virtual attribute overrides to specified, verify others are still virtual + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(1, attributeOverrideContainer.specifiedAttributeOverridesSize()); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals("city", attributeOverrideContainer.specifiedAttributeOverrides().next().getName()); + virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + } + }
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmEmbeddedMapping2_0Tests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmEmbeddedMapping2_0Tests.java index afd04c66ee..48aa892ea2 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmEmbeddedMapping2_0Tests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jpa2/context/orm/GenericOrmEmbeddedMapping2_0Tests.java @@ -1529,7 +1529,7 @@ public class GenericOrmEmbeddedMapping2_0Tests extends Generic2_0ContextModelTes getOrmXmlResource().save(null); embeddedMapping = (OrmEmbeddedMapping2_0) ormPersistentType.getAttributeNamed("myEmbedded").getMapping(); overrideContainer = embeddedMapping.getAssociationOverrideContainer(); - virtualAssociationOverride = CollectionTools.get(overrideContainer.virtualAssociationOverrides(), 1); + virtualAssociationOverride = overrideContainer.specifiedAssociationOverrides().next(); assertEquals("addresses", virtualAssociationOverride.getName()); joiningStrategy = ((AssociationOverrideRelationshipReference2_0) virtualAssociationOverride.getRelationshipReference()).getJoinTableJoiningStrategy(); joinTable = joiningStrategy.getJoinTable(); diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmManyToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmManyToManyMappingTests.java index fcaa90b546..f39d3cf265 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmManyToManyMappingTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmManyToManyMappingTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Oracle. All rights reserved. + * Copyright (c) 2008, 2010 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. @@ -215,10 +215,10 @@ public class EclipseLinkOrmManyToManyMappingTests assertEquals(true, oneToMany.getPersistentAttribute().isVirtual()); ormJoinTable = oneToMany.getRelationshipReference().getJoinTableJoiningStrategy().getJoinTable(); assertEquals("Department_Employee", ormJoinTable.getName()); - assertEquals("Department_id", ormJoinTable.specifiedJoinColumns().next().getName()); - assertEquals("id", ormJoinTable.specifiedJoinColumns().next().getReferencedColumnName()); - assertEquals("employees_empId", ormJoinTable.specifiedInverseJoinColumns().next().getName()); - assertEquals("empId", ormJoinTable.specifiedInverseJoinColumns().next().getReferencedColumnName()); + assertEquals("Department_id", ormJoinTable.getDefaultJoinColumn().getName()); + assertEquals("id", ormJoinTable.getDefaultJoinColumn().getReferencedColumnName()); + assertEquals("employees_empId", ormJoinTable.getDefaultInverseJoinColumn().getName()); + assertEquals("empId", ormJoinTable.getDefaultInverseJoinColumn().getReferencedColumnName()); //set metadata-complete to false, add mapping to orm.xml verify JoinTable info is not taken from the java departmentPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.FALSE); diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmOneToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmOneToManyMappingTests.java index 7cc6d2e7c9..1d83418bbe 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmOneToManyMappingTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink/core/tests/internal/context/orm/EclipseLinkOrmOneToManyMappingTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Oracle. All rights reserved. + * Copyright (c) 2008, 2010 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. @@ -404,10 +404,10 @@ public class EclipseLinkOrmOneToManyMappingTests assertEquals(true, oneToMany.getPersistentAttribute().isVirtual()); ormJoinTable = oneToMany.getRelationshipReference().getJoinTableJoiningStrategy().getJoinTable(); assertEquals("Department_Employee", ormJoinTable.getName()); - assertEquals("Department_id", ormJoinTable.specifiedJoinColumns().next().getName()); - assertEquals("id", ormJoinTable.specifiedJoinColumns().next().getReferencedColumnName()); - assertEquals("employees_empId", ormJoinTable.specifiedInverseJoinColumns().next().getName()); - assertEquals("empId", ormJoinTable.specifiedInverseJoinColumns().next().getReferencedColumnName()); + assertEquals("Department_id", ormJoinTable.getDefaultJoinColumn().getName()); + assertEquals("id", ormJoinTable.getDefaultJoinColumn().getReferencedColumnName()); + assertEquals("employees_empId", ormJoinTable.getDefaultInverseJoinColumn().getName()); + assertEquals("empId", ormJoinTable.getDefaultInverseJoinColumn().getReferencedColumnName()); //set metadata-complete to false, add mapping to orm.xml verify JoinTable info is not taken from the java departmentPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.FALSE); diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaCollectionTableTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaCollectionTableTests.java new file mode 100644 index 0000000000..daf10785d4 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaCollectionTableTests.java @@ -0,0 +1,722 @@ +/******************************************************************************* + * Copyright (c) 2010 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.eclipselink2_0.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.core.context.JoinColumn; +import org.eclipse.jpt.core.context.UniqueConstraint; +import org.eclipse.jpt.core.context.java.JavaUniqueConstraint; +import org.eclipse.jpt.core.jpa2.context.CollectionTable2_0; +import org.eclipse.jpt.core.jpa2.context.java.JavaElementCollectionMapping2_0; +import org.eclipse.jpt.core.jpa2.resource.java.CollectionTable2_0Annotation; +import org.eclipse.jpt.core.jpa2.resource.java.JPA2_0; +import org.eclipse.jpt.core.resource.java.JPA; +import org.eclipse.jpt.core.resource.java.JavaResourcePersistentAttribute; +import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; +import org.eclipse.jpt.core.resource.java.UniqueConstraintAnnotation; +import org.eclipse.jpt.eclipselink2_0.core.tests.internal.context.EclipseLink2_0ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +@SuppressWarnings("nls") +public class EclipseLink2_0JavaCollectionTableTests extends EclipseLink2_0ContextModelTestCase +{ + public EclipseLink2_0JavaCollectionTableTests(String name) { + super(name); + } + + private ICompilationUnit createTestEntityWithElementCollection() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ElementCollection").append(CR); + } + }); + } + + private ICompilationUnit createTestEntityWithValidElementCollection() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ElementCollection").append(CR); + sb.append(" private Collection<String> projects;").append(CR); + } + }); + } + + + public void testUpdateSpecifiedName() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedName()); + assertNull(resourceCollectionTable); + + + //set name in the resource model, verify context model updated + attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable.setName("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedName()); + assertEquals("FOO", resourceCollectionTable.getName()); + + //set name to null in the resource model + resourceCollectionTable.setName(null); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedName()); + assertNull(resourceCollectionTable.getName()); + + resourceCollectionTable.setName("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedName()); + assertEquals("FOO", resourceCollectionTable.getName()); + + attributeResource.removeAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedName()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testModifySpecifiedName() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable3 = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedName()); + assertNull(resourceCollectionTable3); + + //set name in the context model, verify resource model modified + collectionTable.setSpecifiedName("foo"); + resourceCollectionTable3 = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals("foo", collectionTable.getSpecifiedName()); + assertEquals("foo", resourceCollectionTable3.getName()); + + //set name to null in the context model + collectionTable.setSpecifiedName(null); + assertNull(collectionTable.getSpecifiedName()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testDefaultName() throws Exception { + createTestEntityWithValidElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + assertEquals(TYPE_NAME + "_projects", collectionTable.getDefaultName()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + + //add the collection table annotation, verify default collection table name is the same + attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals(TYPE_NAME + "_projects", collectionTable.getDefaultName()); + assertNotNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + + //set the entity name, verify default collection table name updates + getJavaEntity().setSpecifiedName("Foo"); + assertEquals("Foo_projects", collectionTable.getDefaultName()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedSchema()); + assertNull(resourceCollectionTable); + + + //set schema in the resource model, verify context model updated + attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable.setSchema("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedSchema()); + assertEquals("FOO", resourceCollectionTable.getSchema()); + + //set schema to null in the resource model + resourceCollectionTable.setSchema(null); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedSchema()); + assertNull(resourceCollectionTable.getSchema()); + + resourceCollectionTable.setSchema("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedSchema()); + assertEquals("FOO", resourceCollectionTable.getSchema()); + + attributeResource.removeAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedSchema()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testModifySpecifiedSchema() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedSchema()); + assertNull(resourceCollectionTable); + + //set schema in the context model, verify resource model modified + collectionTable.setSpecifiedSchema("foo"); + resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals("foo", collectionTable.getSpecifiedSchema()); + assertEquals("foo", resourceCollectionTable.getSchema()); + + //set schema to null in the context model + collectionTable.setSpecifiedSchema(null); + assertNull(collectionTable.getSpecifiedSchema()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedCatalog()); + assertNull(resourceCollectionTable); + + + //set catalog in the resource model, verify context model updated + attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + resourceCollectionTable.setCatalog("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedCatalog()); + assertEquals("FOO", resourceCollectionTable.getCatalog()); + + //set catalog to null in the resource model + resourceCollectionTable.setCatalog(null); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedCatalog()); + assertNull(resourceCollectionTable.getCatalog()); + + resourceCollectionTable.setCatalog("FOO"); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", collectionTable.getSpecifiedCatalog()); + assertEquals("FOO", resourceCollectionTable.getCatalog()); + + attributeResource.removeAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + getJpaProject().synchronizeContextModel(); + assertNull(collectionTable.getSpecifiedCatalog()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testModifySpecifiedCatalog() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertNull(collectionTable.getSpecifiedCatalog()); + assertNull(resourceCollectionTable); + + //set catalog in the context model, verify resource model modified + collectionTable.setSpecifiedCatalog("foo"); + resourceCollectionTable = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals("foo", collectionTable.getSpecifiedCatalog()); + assertEquals("foo", resourceCollectionTable.getCatalog()); + + //set catalog to null in the context model + collectionTable.setSpecifiedCatalog(null); + assertNull(collectionTable.getSpecifiedCatalog()); + assertNull(attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME)); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + + JoinColumn joinColumn = collectionTable.addSpecifiedJoinColumn(0); + joinColumn.setSpecifiedName("FOO"); + + CollectionTable2_0Annotation joinTableResource = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertEquals("FOO", joinTableResource.joinColumnAt(0).getName()); + + JoinColumn joinColumn2 = collectionTable.addSpecifiedJoinColumn(0); + joinColumn2.setSpecifiedName("BAR"); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(1).getName()); + + JoinColumn joinColumn3 = collectionTable.addSpecifiedJoinColumn(1); + joinColumn3.setSpecifiedName("BAZ"); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + + ListIterator<JoinColumn> joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + collectionTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + collectionTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + collectionTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + CollectionTable2_0Annotation joinTableResource = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals(3, joinTableResource.joinColumnsSize()); + + collectionTable.removeSpecifiedJoinColumn(0); + assertEquals(2, joinTableResource.joinColumnsSize()); + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + + collectionTable.removeSpecifiedJoinColumn(0); + assertEquals(1, joinTableResource.joinColumnsSize()); + assertEquals("BAZ", joinTableResource.joinColumnAt(0).getName()); + + collectionTable.removeSpecifiedJoinColumn(0); + assertEquals(0, joinTableResource.joinColumnsSize()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + collectionTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + collectionTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + collectionTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + CollectionTable2_0Annotation joinTableResource = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + assertEquals(3, joinTableResource.joinColumnsSize()); + + + collectionTable.moveSpecifiedJoinColumn(2, 0); + ListIterator<JoinColumn> joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + + + collectionTable.moveSpecifiedJoinColumn(0, 1); + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAR", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + } + + public void testUpdateJoinColumns() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + CollectionTable2_0Annotation joinTableResource = (CollectionTable2_0Annotation) attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + joinTableResource.addJoinColumn(0); + joinTableResource.addJoinColumn(1); + joinTableResource.addJoinColumn(2); + + joinTableResource.joinColumnAt(0).setName("FOO"); + joinTableResource.joinColumnAt(1).setName("BAR"); + joinTableResource.joinColumnAt(2).setName("BAZ"); + getJpaProject().synchronizeContextModel(); + + ListIterator<JoinColumn> joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.moveJoinColumn(2, 0); + getJpaProject().synchronizeContextModel(); + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.moveJoinColumn(0, 1); + getJpaProject().synchronizeContextModel(); + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(1); + getJpaProject().synchronizeContextModel(); + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(1); + getJpaProject().synchronizeContextModel(); + joinColumns = collectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(0); + getJpaProject().synchronizeContextModel(); + assertFalse(collectionTable.specifiedJoinColumns().hasNext()); + } + + public void testGetDefaultJoinColumn() { + //TODO + } + + public void testSpecifiedJoinColumnsSize() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + assertEquals(0, collectionTable.specifiedJoinColumnsSize()); + + collectionTable.addSpecifiedJoinColumn(0); + assertEquals(1, collectionTable.specifiedJoinColumnsSize()); + + collectionTable.removeSpecifiedJoinColumn(0); + assertEquals(0, collectionTable.specifiedJoinColumnsSize()); + } + + public void testUniqueConstraints() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + ListIterator<JavaUniqueConstraint> uniqueConstraints = collectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + joinTableAnnotation.addUniqueConstraint(0).addColumnName(0, "foo"); + joinTableAnnotation.addUniqueConstraint(0).addColumnName(0, "bar"); + getJpaProject().synchronizeContextModel(); + + uniqueConstraints = collectionTable.uniqueConstraints(); + assertTrue(uniqueConstraints.hasNext()); + assertEquals("bar", uniqueConstraints.next().columnNames().next()); + assertEquals("foo", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testUniqueConstraintsSize() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + + assertEquals(0, collectionTable.uniqueConstraintsSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + joinTableAnnotation.addUniqueConstraint(0).addColumnName(0, "foo"); + joinTableAnnotation.addUniqueConstraint(1).addColumnName(0, "bar"); + + getJpaProject().synchronizeContextModel(); + + assertEquals(2, collectionTable.uniqueConstraintsSize()); + } + + public void testAddUniqueConstraint() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + collectionTable.addUniqueConstraint(0).addColumnName(0, "BAR"); + collectionTable.addUniqueConstraint(0).addColumnName(0, "BAZ"); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + ListIterator<UniqueConstraintAnnotation> uniqueConstraints = joinTableAnnotation.uniqueConstraints(); + + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testAddUniqueConstraint2() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + collectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + collectionTable.addUniqueConstraint(0).addColumnName(0, "BAZ"); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + ListIterator<UniqueConstraintAnnotation> uniqueConstraints = joinTableAnnotation.uniqueConstraints(); + + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testRemoveUniqueConstraint() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + collectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + collectionTable.addUniqueConstraint(2).addColumnName(0, "BAZ"); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertEquals(3, joinTableAnnotation.uniqueConstraintsSize()); + + collectionTable.removeUniqueConstraint(1); + + ListIterator<UniqueConstraintAnnotation> uniqueConstraintAnnotations = joinTableAnnotation.uniqueConstraints(); + assertEquals("FOO", uniqueConstraintAnnotations.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraintAnnotations.next().columnNames().next()); + assertFalse(uniqueConstraintAnnotations.hasNext()); + + Iterator<UniqueConstraint> uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + + collectionTable.removeUniqueConstraint(1); + uniqueConstraintAnnotations = joinTableAnnotation.uniqueConstraints(); + assertEquals("FOO", uniqueConstraintAnnotations.next().columnNames().next()); + assertFalse(uniqueConstraintAnnotations.hasNext()); + + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + + collectionTable.removeUniqueConstraint(0); + uniqueConstraintAnnotations = joinTableAnnotation.uniqueConstraints(); + assertFalse(uniqueConstraintAnnotations.hasNext()); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testMoveUniqueConstraint() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + collectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + collectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + collectionTable.addUniqueConstraint(2).addColumnName(0, "BAZ"); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.getAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + assertEquals(3, joinTableAnnotation.uniqueConstraintsSize()); + + + collectionTable.moveUniqueConstraint(2, 0); + ListIterator<UniqueConstraint> uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + + ListIterator<UniqueConstraintAnnotation> uniqueConstraintAnnotations = joinTableAnnotation.uniqueConstraints(); + assertEquals("BAR", uniqueConstraintAnnotations.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraintAnnotations.next().columnNames().next()); + assertEquals("FOO", uniqueConstraintAnnotations.next().columnNames().next()); + + + collectionTable.moveUniqueConstraint(0, 1); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + + uniqueConstraintAnnotations = joinTableAnnotation.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraintAnnotations.next().columnNames().next()); + assertEquals("BAR", uniqueConstraintAnnotations.next().columnNames().next()); + assertEquals("FOO", uniqueConstraintAnnotations.next().columnNames().next()); + } + + public void testUpdateUniqueConstraints() throws Exception { + createTestEntityWithElementCollection(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().attributes().next().getMapping(); + CollectionTable2_0 collectionTable = elementCollectionMapping.getCollectionTable(); + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + CollectionTable2_0Annotation joinTableAnnotation = (CollectionTable2_0Annotation) attributeResource.addAnnotation(CollectionTable2_0Annotation.ANNOTATION_NAME); + + joinTableAnnotation.addUniqueConstraint(0).addColumnName("FOO"); + joinTableAnnotation.addUniqueConstraint(1).addColumnName("BAR"); + joinTableAnnotation.addUniqueConstraint(2).addColumnName("BAZ"); + getJpaProject().synchronizeContextModel(); + + + ListIterator<UniqueConstraint> uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + joinTableAnnotation.moveUniqueConstraint(2, 0); + getJpaProject().synchronizeContextModel(); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + joinTableAnnotation.moveUniqueConstraint(0, 1); + getJpaProject().synchronizeContextModel(); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + joinTableAnnotation.removeUniqueConstraint(1); + getJpaProject().synchronizeContextModel(); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + joinTableAnnotation.removeUniqueConstraint(1); + getJpaProject().synchronizeContextModel(); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + joinTableAnnotation.removeUniqueConstraint(0); + getJpaProject().synchronizeContextModel(); + uniqueConstraints = collectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaElementCollectionMappingTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaElementCollectionMappingTests.java index 0917ac661d..3c12cf146f 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaElementCollectionMappingTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/EclipseLink2_0JavaElementCollectionMappingTests.java @@ -13,7 +13,10 @@ import java.util.Iterator; import java.util.ListIterator; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jpt.core.MappingKeys; +import org.eclipse.jpt.core.context.AttributeOverride; +import org.eclipse.jpt.core.context.AttributeOverrideContainer; import org.eclipse.jpt.core.context.BasicMapping; +import org.eclipse.jpt.core.context.Embeddable; import org.eclipse.jpt.core.context.EmbeddedIdMapping; import org.eclipse.jpt.core.context.EmbeddedMapping; import org.eclipse.jpt.core.context.FetchType; @@ -22,18 +25,25 @@ import org.eclipse.jpt.core.context.ManyToManyMapping; import org.eclipse.jpt.core.context.ManyToOneMapping; import org.eclipse.jpt.core.context.OneToManyMapping; import org.eclipse.jpt.core.context.PersistentAttribute; +import org.eclipse.jpt.core.context.PersistentType; import org.eclipse.jpt.core.context.TransientMapping; import org.eclipse.jpt.core.context.TypeMapping; import org.eclipse.jpt.core.context.VersionMapping; +import org.eclipse.jpt.core.context.java.JavaAttributeOverride; +import org.eclipse.jpt.core.context.java.JavaAttributeOverrideContainer; import org.eclipse.jpt.core.context.java.JavaPersistentType; import org.eclipse.jpt.core.context.persistence.ClassRef; import org.eclipse.jpt.core.jpa2.context.ElementCollectionMapping2_0; import org.eclipse.jpt.core.jpa2.context.OrderColumn2_0; import org.eclipse.jpt.core.jpa2.context.Orderable2_0; +import org.eclipse.jpt.core.jpa2.context.java.JavaElementCollectionMapping2_0; import org.eclipse.jpt.core.jpa2.resource.java.ElementCollection2_0Annotation; import org.eclipse.jpt.core.jpa2.resource.java.JPA2_0; import org.eclipse.jpt.core.jpa2.resource.java.MapKeyClass2_0Annotation; +import org.eclipse.jpt.core.resource.java.AttributeOverrideAnnotation; +import org.eclipse.jpt.core.resource.java.AttributeOverridesAnnotation; import org.eclipse.jpt.core.resource.java.BasicAnnotation; +import org.eclipse.jpt.core.resource.java.ColumnAnnotation; import org.eclipse.jpt.core.resource.java.EmbeddedAnnotation; import org.eclipse.jpt.core.resource.java.EmbeddedIdAnnotation; import org.eclipse.jpt.core.resource.java.IdAnnotation; @@ -43,6 +53,7 @@ import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; import org.eclipse.jpt.core.resource.java.ManyToManyAnnotation; import org.eclipse.jpt.core.resource.java.ManyToOneAnnotation; import org.eclipse.jpt.core.resource.java.MapKeyAnnotation; +import org.eclipse.jpt.core.resource.java.NestableAnnotation; import org.eclipse.jpt.core.resource.java.OneToManyAnnotation; import org.eclipse.jpt.core.resource.java.TransientAnnotation; import org.eclipse.jpt.core.resource.java.VersionAnnotation; @@ -54,6 +65,8 @@ import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; public class EclipseLink2_0JavaElementCollectionMappingTests extends EclipseLink2_0ContextModelTestCase { + public static final String EMBEDDABLE_TYPE_NAME = "Address"; + public static final String FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME = PACKAGE_NAME + "." + EMBEDDABLE_TYPE_NAME; private ICompilationUnit createTestEntityWithElementCollectionMapping() throws Exception { return this.createTestType(new DefaultAnnotationWriter() { @@ -953,4 +966,444 @@ public class EclipseLink2_0JavaElementCollectionMappingTests assertEquals("addresses_ORDER", orderColumn.getDefaultName()); assertEquals(TYPE_NAME + "_addresses", orderColumn.getTable()); } + + public void testGetValueColumn() throws Exception { + createTestEntityWithElementCollectionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getSpecifiedMapping(); + + assertNull(elementCollectionMapping.getValueColumn().getSpecifiedName()); + assertEquals("id", elementCollectionMapping.getValueColumn().getName()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + ColumnAnnotation column = (ColumnAnnotation) attributeResource.addAnnotation(JPA.COLUMN); + column.setName("foo"); + getJpaProject().synchronizeContextModel(); + + assertEquals("foo", elementCollectionMapping.getValueColumn().getSpecifiedName()); + assertEquals("foo", elementCollectionMapping.getValueColumn().getName()); + assertEquals("id", elementCollectionMapping.getValueColumn().getDefaultName()); + } + + public void testSpecifiedAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + JavaElementCollectionMapping2_0 elementCollectionMapping = (JavaElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + JavaAttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + ListIterator<JavaAttributeOverride> specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + + assertFalse(specifiedAttributeOverrides.hasNext()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.moveAnnotation(1, 0, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + getJpaProject().synchronizeContextModel(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + getJpaProject().synchronizeContextModel(); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertFalse(specifiedAttributeOverrides.hasNext()); + } + + public void testVirtualAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + assertEquals("addresses", attributeResource.getName()); + assertNull(attributeResource.getAnnotation(AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(attributeResource.getAnnotation(AttributeOverridesAnnotation.ANNOTATION_NAME)); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + AttributeOverride defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME +"_addresses", defaultAttributeOverride.getColumn().getTable()); + assertEquals(null, defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(true, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(255, defaultAttributeOverride.getColumn().getLength()); + assertEquals(0, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(0, defaultAttributeOverride.getColumn().getScale()); + + + ListIterator<ClassRef> classRefs = getPersistenceUnit().specifiedClassRefs(); + classRefs.next(); + Embeddable embeddable = (Embeddable) classRefs.next().getJavaPersistentType().getMapping(); + + BasicMapping cityMapping = (BasicMapping) embeddable.getPersistentType().getAttributeNamed("city").getMapping(); + cityMapping.getColumn().setSpecifiedName("FOO"); + cityMapping.getColumn().setSpecifiedTable("BAR"); + cityMapping.getColumn().setColumnDefinition("COLUMN_DEF"); + cityMapping.getColumn().setSpecifiedInsertable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedUpdatable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + cityMapping.getColumn().setSpecifiedNullable(Boolean.FALSE); + cityMapping.getColumn().setSpecifiedLength(Integer.valueOf(5)); + cityMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(6)); + cityMapping.getColumn().setSpecifiedScale(Integer.valueOf(7)); + + assertEquals("addresses", attributeResource.getName()); + assertNull(attributeResource.getAnnotation(AttributeOverrideAnnotation.ANNOTATION_NAME)); + assertNull(attributeResource.getAnnotation(AttributeOverridesAnnotation.ANNOTATION_NAME)); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("FOO", defaultAttributeOverride.getColumn().getName()); + assertEquals("BAR", defaultAttributeOverride.getColumn().getTable()); + assertEquals("COLUMN_DEF", defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(false, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(5, defaultAttributeOverride.getColumn().getLength()); + assertEquals(6, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(7, defaultAttributeOverride.getColumn().getScale()); + + cityMapping.getColumn().setSpecifiedName(null); + cityMapping.getColumn().setSpecifiedTable(null); + cityMapping.getColumn().setColumnDefinition(null); + cityMapping.getColumn().setSpecifiedInsertable(null); + cityMapping.getColumn().setSpecifiedUpdatable(null); + cityMapping.getColumn().setSpecifiedUnique(null); + cityMapping.getColumn().setSpecifiedNullable(null); + cityMapping.getColumn().setSpecifiedLength(null); + cityMapping.getColumn().setSpecifiedPrecision(null); + cityMapping.getColumn().setSpecifiedScale(null); + defaultAttributeOverride = attributeOverrideContainer.virtualAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME +"_addresses", defaultAttributeOverride.getColumn().getTable()); + assertEquals(null, defaultAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, defaultAttributeOverride.getColumn().isInsertable()); + assertEquals(true, defaultAttributeOverride.getColumn().isUpdatable()); + assertEquals(false, defaultAttributeOverride.getColumn().isUnique()); + assertEquals(true, defaultAttributeOverride.getColumn().isNullable()); + assertEquals(255, defaultAttributeOverride.getColumn().getLength()); + assertEquals(0, defaultAttributeOverride.getColumn().getPrecision()); + assertEquals(0, defaultAttributeOverride.getColumn().getScale()); + + AttributeOverrideAnnotation annotation = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + annotation.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testSpecifiedAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(0, attributeOverrideContainer.specifiedAttributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + + assertEquals(2, attributeOverrideContainer.specifiedAttributeOverridesSize()); + } + + public void testAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + getJpaProject().synchronizeContextModel(); + + assertEquals(6, attributeOverrideContainer.attributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(6, attributeOverrideContainer.attributeOverridesSize()); + } + + public void testVirtualAttributeOverridesSize() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverrideAnnotation attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + getJpaProject().synchronizeContextModel(); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("city"); + getJpaProject().synchronizeContextModel(); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverride = (AttributeOverrideAnnotation) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("state.name"); + getJpaProject().synchronizeContextModel(); + assertEquals(2, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testAttributeOverrideSetVirtual() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + + attributeOverrideContainer.specifiedAttributeOverrides().next().setVirtual(true); + attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + + assertEquals("state.abbr", attributeOverrideContainer.virtualAttributeOverrides().next().getName()); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + + attributeOverrideContainer.specifiedAttributeOverrides().next().setVirtual(true); + attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + assertFalse(attributeOverrides.hasNext()); + + Iterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + assertEquals("state.abbr", virtualAttributeOverrides.next().getName()); + assertEquals("zip", virtualAttributeOverrides.next().getName()); + assertEquals("city", virtualAttributeOverrides.next().getName()); + assertEquals("state.name", virtualAttributeOverrides.next().getName()); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + } + + public void testAttributeOverrideSetVirtual2() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + ListIterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + virtualAttributeOverrides.next(); + virtualAttributeOverrides.next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); + JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); + + attributeResource.moveAnnotation(1, 0, AttributeOverridesAnnotation.ANNOTATION_NAME); + + Iterator<NestableAnnotation> attributeOverrides = attributeResource.annotations(AttributeOverrideAnnotation.ANNOTATION_NAME, AttributeOverridesAnnotation.ANNOTATION_NAME); + + assertEquals("state.name", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertEquals("city", ((AttributeOverrideAnnotation) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testNestedVirtualAttributeOverrides() throws Exception { + createTestEntityWithGenericEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".State"); + + ListIterator<ClassRef> specifiedClassRefs = getPersistenceUnit().specifiedClassRefs(); + PersistentType persistentType = specifiedClassRefs.next().getJavaPersistentType(); + ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentType.getAttributeNamed("addresses").getMapping(); + AttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + ListIterator<AttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + AttributeOverride virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("city", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", virtualAttributeOverride.getName()); + assertEquals(false, virtualAttributeOverrides.hasNext()); + + + PersistentType addressPersistentType = specifiedClassRefs.next().getJavaPersistentType(); + EmbeddedMapping nestedEmbeddedMapping = (EmbeddedMapping) addressPersistentType.getAttributeNamed("state").getMapping(); + AttributeOverrideContainer nestedAttributeOverrideContainer = nestedEmbeddedMapping.getAttributeOverrideContainer(); + assertEquals(2, nestedAttributeOverrideContainer.virtualAttributeOverridesSize()); + virtualAttributeOverrides = nestedAttributeOverrideContainer.virtualAttributeOverrides(); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("name", virtualAttributeOverride.getName()); + virtualAttributeOverride = virtualAttributeOverrides.next(); + assertEquals("abbr", virtualAttributeOverride.getName()); + + PersistentType statePersistentType = specifiedClassRefs.next().getJavaPersistentType(); + BasicMapping abbrMapping = (BasicMapping) statePersistentType.getAttributeNamed("abbr").getMapping(); + abbrMapping.getColumn().setSpecifiedName("BLAH"); + abbrMapping.getColumn().setSpecifiedTable("BLAH_TABLE"); + abbrMapping.getColumn().setColumnDefinition("COLUMN_DEFINITION"); + abbrMapping.getColumn().setSpecifiedInsertable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedUpdatable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + abbrMapping.getColumn().setSpecifiedNullable(Boolean.FALSE); + abbrMapping.getColumn().setSpecifiedLength(Integer.valueOf(5)); + abbrMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(6)); + abbrMapping.getColumn().setSpecifiedScale(Integer.valueOf(7)); + + //check the nested embedded (Address.state) attribute override to verify it is getting settings from the specified column on State.abbr + virtualAttributeOverride = ((EmbeddedMapping) addressPersistentType.getAttributeNamed("state").getMapping()).getAttributeOverrideContainer().getAttributeOverrideNamed("abbr"); + assertEquals("abbr", virtualAttributeOverride.getName()); + assertEquals("BLAH", virtualAttributeOverride.getColumn().getName()); + assertEquals("BLAH_TABLE", virtualAttributeOverride.getColumn().getTable()); + assertEquals("COLUMN_DEFINITION", virtualAttributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, virtualAttributeOverride.getColumn().isInsertable()); + assertEquals(false, virtualAttributeOverride.getColumn().isUpdatable()); + assertEquals(true, virtualAttributeOverride.getColumn().isUnique()); + assertEquals(false, virtualAttributeOverride.getColumn().isNullable()); + assertEquals(5, virtualAttributeOverride.getColumn().getLength()); + assertEquals(6, virtualAttributeOverride.getColumn().getPrecision()); + assertEquals(7, virtualAttributeOverride.getColumn().getScale()); + } } diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/JptEclipseLink2_0JavaContextModelTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/JptEclipseLink2_0JavaContextModelTests.java index 018f0875ef..95cac0fda0 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/JptEclipseLink2_0JavaContextModelTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/java/JptEclipseLink2_0JavaContextModelTests.java @@ -17,6 +17,7 @@ public class JptEclipseLink2_0JavaContextModelTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(JptEclipseLink2_0JavaContextModelTests.class.getName()); + suite.addTestSuite(EclipseLink2_0JavaCollectionTableTests.class); suite.addTestSuite(EclipseLink2_0JavaElementCollectionMappingTests.class); suite.addTestSuite(EclipseLink2_0JavaEntityTests.class); suite.addTestSuite(EclipseLink2_0JavaManyToManyMappingTests.class); diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmCollectionTableTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmCollectionTableTests.java new file mode 100644 index 0000000000..9ed05a7538 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmCollectionTableTests.java @@ -0,0 +1,746 @@ +/******************************************************************************* + * Copyright (c) 2010 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.eclipselink2_0.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.core.JptCorePlugin; +import org.eclipse.jpt.core.MappingKeys; +import org.eclipse.jpt.core.context.java.JavaEntity; +import org.eclipse.jpt.core.context.java.JavaJoinColumn; +import org.eclipse.jpt.core.context.java.JavaPersistentAttribute; +import org.eclipse.jpt.core.context.orm.OrmEntity; +import org.eclipse.jpt.core.context.orm.OrmJoinColumn; +import org.eclipse.jpt.core.context.orm.OrmPersistentAttribute; +import org.eclipse.jpt.core.context.orm.OrmPersistentType; +import org.eclipse.jpt.core.context.orm.OrmUniqueConstraint; +import org.eclipse.jpt.core.jpa2.MappingKeys2_0; +import org.eclipse.jpt.core.jpa2.context.java.JavaCollectionTable2_0; +import org.eclipse.jpt.core.jpa2.context.java.JavaElementCollectionMapping2_0; +import org.eclipse.jpt.core.jpa2.context.orm.OrmCollectionTable2_0; +import org.eclipse.jpt.core.jpa2.context.orm.OrmElementCollectionMapping2_0; +import org.eclipse.jpt.core.jpa2.resource.java.JPA2_0; +import org.eclipse.jpt.core.resource.java.JPA; +import org.eclipse.jpt.core.resource.orm.OrmFactory; +import org.eclipse.jpt.core.resource.orm.XmlCollectionTable; +import org.eclipse.jpt.core.resource.orm.XmlUniqueConstraint; +import org.eclipse.jpt.core.resource.orm.v2_0.XmlElementCollection_2_0; +import org.eclipse.jpt.core.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +@SuppressWarnings("nls") +public class EclipseLink2_0OrmCollectionTableTests extends EclipseLink2_0OrmContextModelTestCase +{ + public EclipseLink2_0OrmCollectionTableTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + getXmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + getPersistenceXmlResource().save(null); + } + + private ICompilationUnit createTestEntityWithValidElementCollection() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ElementCollection").append(CR); + sb.append(" private Collection<String> projects;").append(CR); + sb.append("@Id").append(CR); + } + }); + } + + public void testUpdateSpecifiedName() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 elementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = elementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedName()); + assertNull(resourceElementCollection.getCollectionTable()); + + + //set name in the resource model, verify context model updated + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + resourceElementCollection.getCollectionTable().setName("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedName()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getName()); + + //set name to null in the resource model + resourceElementCollection.getCollectionTable().setName(null); + assertNull(ormCollectionTable.getSpecifiedName()); + assertNull(resourceElementCollection.getCollectionTable().getName()); + + resourceElementCollection.getCollectionTable().setName("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedName()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getName()); + + resourceElementCollection.setCollectionTable(null); + assertNull(ormCollectionTable.getSpecifiedName()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testModifySpecifiedName() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 elementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = elementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedName()); + assertNull(resourceElementCollection.getCollectionTable()); + + //set name in the context model, verify resource model modified + ormCollectionTable.setSpecifiedName("foo"); + assertEquals("foo", ormCollectionTable.getSpecifiedName()); + assertEquals("foo", resourceElementCollection.getCollectionTable().getName()); + + //set name to null in the context model + ormCollectionTable.setSpecifiedName(null); + assertNull(ormCollectionTable.getSpecifiedName()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testVirtualCollectionTable() throws Exception { + createTestEntityWithValidElementCollection(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.getAttributeNamed("projects"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertTrue(ormPersistentAttribute.isVirtual()); + assertEquals(TYPE_NAME + "_projects", ormCollectionTable.getSpecifiedName()); + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertEquals(0, ormCollectionTable.specifiedJoinColumnsSize()); + OrmJoinColumn ormJoinColumn = ormCollectionTable.getDefaultJoinColumn(); + assertEquals(TYPE_NAME + "_id", ormJoinColumn.getDefaultName()); + assertEquals("id", ormJoinColumn.getDefaultReferencedColumnName()); + + JavaPersistentAttribute javaPersistentAttribute = ormPersistentAttribute.getJavaPersistentAttribute(); + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) javaPersistentAttribute.getMapping(); + JavaCollectionTable2_0 javaCollectionTable = javaElementCollectionMapping.getCollectionTable(); + javaCollectionTable.setSpecifiedName("FOO"); + javaCollectionTable.setSpecifiedCatalog("CATALOG"); + javaCollectionTable.setSpecifiedSchema("SCHEMA"); + JavaJoinColumn javaJoinColumn = javaCollectionTable.addSpecifiedJoinColumn(0); + javaJoinColumn.setSpecifiedName("NAME"); + javaJoinColumn.setSpecifiedReferencedColumnName("REFERENCED_NAME"); + + assertEquals("FOO", ormCollectionTable.getSpecifiedName()); + assertEquals("CATALOG", ormCollectionTable.getSpecifiedCatalog()); + assertEquals("SCHEMA", ormCollectionTable.getSpecifiedSchema()); + assertEquals(1, ormCollectionTable.specifiedJoinColumnsSize()); + ormJoinColumn = ormCollectionTable.specifiedJoinColumns().next(); + assertEquals("NAME", ormJoinColumn.getSpecifiedName()); + assertEquals("REFERENCED_NAME", ormJoinColumn.getSpecifiedReferencedColumnName()); + } + + public void testUpdateDefaultNameFromJavaTable() throws Exception { + createTestEntityWithValidElementCollection(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "projects"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + assertEquals(TYPE_NAME + "_projects", ormCollectionTable.getDefaultName()); + + ((JavaEntity) ormPersistentType.getJavaPersistentType().getMapping()).setSpecifiedName("Foo"); + assertEquals("Foo_projects", ormCollectionTable.getDefaultName()); + + ((OrmEntity) ormPersistentType.getMapping()).setSpecifiedName("Bar"); + assertEquals("Bar_projects", ormCollectionTable.getDefaultName()); + + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("projects").getMapping(); + javaElementCollectionMapping.getCollectionTable().setSpecifiedName("JAVA_COLLECTION_TABLE"); + + assertEquals("Bar_projects", ormCollectionTable.getDefaultName()); + + + //set metadata-complete to true, will ignore java annotation settings + getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertEquals("Bar_projects", ormCollectionTable.getDefaultName()); + + + getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + //remove element collection mapping from the orm.xml file + ormPersistentAttribute.makeVirtual(); + //ormPersistentType.getMapping().setSpecifiedMetadataComplete(null); + ormPersistentAttribute = ormPersistentType.getAttributeNamed("projects"); + ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + assertTrue(ormPersistentAttribute.isVirtual()); + assertEquals("JAVA_COLLECTION_TABLE", ormCollectionTable.getSpecifiedName());//specifiedName since this is a virtual mapping now + + javaElementCollectionMapping.getCollectionTable().setSpecifiedName(null); + assertEquals("Bar_projects", ormCollectionTable.getSpecifiedName()); + assertEquals("Bar_projects", ormCollectionTable.getDefaultName()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertNull(resourceElementCollection.getCollectionTable()); + + //set schema in the resource model, verify context model updated + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + resourceElementCollection.getCollectionTable().setSchema("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedSchema()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getSchema()); + + //set Schema to null in the resource model + resourceElementCollection.getCollectionTable().setSchema(null); + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertNull(resourceElementCollection.getCollectionTable().getSchema()); + + resourceElementCollection.getCollectionTable().setSchema("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedSchema()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getSchema()); + + resourceElementCollection.setCollectionTable(null); + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testModifySpecifiedSchema() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertNull(resourceElementCollection.getCollectionTable()); + + //set Schema in the context model, verify resource model modified + ormCollectionTable.setSpecifiedSchema("foo"); + assertEquals("foo", ormCollectionTable.getSpecifiedSchema()); + assertEquals("foo", resourceElementCollection.getCollectionTable().getSchema()); + + //set Schema to null in the context model + ormCollectionTable.setSpecifiedSchema(null); + assertNull(ormCollectionTable.getSpecifiedSchema()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(resourceElementCollection.getCollectionTable()); + + //set Catalog in the resource model, verify context model updated + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + resourceElementCollection.getCollectionTable().setCatalog("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedCatalog()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getCatalog()); + + //set Catalog to null in the resource model + resourceElementCollection.getCollectionTable().setCatalog(null); + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(resourceElementCollection.getCollectionTable().getCatalog()); + + resourceElementCollection.getCollectionTable().setCatalog("FOO"); + assertEquals("FOO", ormCollectionTable.getSpecifiedCatalog()); + assertEquals("FOO", resourceElementCollection.getCollectionTable().getCatalog()); + + resourceElementCollection.setCollectionTable(null); + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testModifySpecifiedCatalog() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(resourceElementCollection.getCollectionTable()); + + //set Catalog in the context model, verify resource model modified + ormCollectionTable.setSpecifiedCatalog("foo"); + assertEquals("foo", ormCollectionTable.getSpecifiedCatalog()); + assertEquals("foo", resourceElementCollection.getCollectionTable().getCatalog()); + + //set Catalog to null in the context model + ormCollectionTable.setSpecifiedCatalog(null); + assertNull(ormCollectionTable.getSpecifiedCatalog()); + assertNull(resourceElementCollection.getCollectionTable()); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + OrmJoinColumn joinColumn = ormCollectionTable.addSpecifiedJoinColumn(0); + joinColumn.setSpecifiedName("FOO"); + + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + assertEquals("FOO", resourceCollectionTable.getJoinColumns().get(0).getName()); + + OrmJoinColumn joinColumn2 = ormCollectionTable.addSpecifiedJoinColumn(0); + joinColumn2.setSpecifiedName("BAR"); + + assertEquals("BAR", resourceCollectionTable.getJoinColumns().get(0).getName()); + assertEquals("FOO", resourceCollectionTable.getJoinColumns().get(1).getName()); + + OrmJoinColumn joinColumn3 = ormCollectionTable.addSpecifiedJoinColumn(1); + joinColumn3.setSpecifiedName("BAZ"); + + assertEquals("BAR", resourceCollectionTable.getJoinColumns().get(0).getName()); + assertEquals("BAZ", resourceCollectionTable.getJoinColumns().get(1).getName()); + assertEquals("FOO", resourceCollectionTable.getJoinColumns().get(2).getName()); + + ListIterator<OrmJoinColumn> joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + ormCollectionTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + ormCollectionTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + ormCollectionTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + assertEquals(3, resourceCollectionTable.getJoinColumns().size()); + + ormCollectionTable.removeSpecifiedJoinColumn(0); + assertEquals(2, resourceCollectionTable.getJoinColumns().size()); + assertEquals("BAR", resourceCollectionTable.getJoinColumns().get(0).getName()); + assertEquals("BAZ", resourceCollectionTable.getJoinColumns().get(1).getName()); + + ormCollectionTable.removeSpecifiedJoinColumn(0); + assertEquals(1, resourceCollectionTable.getJoinColumns().size()); + assertEquals("BAZ", resourceCollectionTable.getJoinColumns().get(0).getName()); + + ormCollectionTable.removeSpecifiedJoinColumn(0); + assertEquals(0, resourceCollectionTable.getJoinColumns().size()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + ormCollectionTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + ormCollectionTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + ormCollectionTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + assertEquals(3, resourceCollectionTable.getJoinColumns().size()); + + + ormCollectionTable.moveSpecifiedJoinColumn(2, 0); + ListIterator<OrmJoinColumn> joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", resourceCollectionTable.getJoinColumns().get(0).getName()); + assertEquals("BAZ", resourceCollectionTable.getJoinColumns().get(1).getName()); + assertEquals("FOO", resourceCollectionTable.getJoinColumns().get(2).getName()); + + + ormCollectionTable.moveSpecifiedJoinColumn(0, 1); + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", resourceCollectionTable.getJoinColumns().get(0).getName()); + assertEquals("BAR", resourceCollectionTable.getJoinColumns().get(1).getName()); + assertEquals("FOO", resourceCollectionTable.getJoinColumns().get(2).getName()); + } + + + public void testUpdateJoinColumns() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + resourceCollectionTable.getJoinColumns().add(OrmFactory.eINSTANCE.createXmlJoinColumn()); + resourceCollectionTable.getJoinColumns().add(OrmFactory.eINSTANCE.createXmlJoinColumn()); + resourceCollectionTable.getJoinColumns().add(OrmFactory.eINSTANCE.createXmlJoinColumn()); + + resourceCollectionTable.getJoinColumns().get(0).setName("FOO"); + resourceCollectionTable.getJoinColumns().get(1).setName("BAR"); + resourceCollectionTable.getJoinColumns().get(2).setName("BAZ"); + + ListIterator<OrmJoinColumn> joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + resourceCollectionTable.getJoinColumns().move(2, 0); + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + resourceCollectionTable.getJoinColumns().move(0, 1); + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + resourceCollectionTable.getJoinColumns().remove(1); + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + resourceCollectionTable.getJoinColumns().remove(1); + joinColumns = ormCollectionTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + resourceCollectionTable.getJoinColumns().remove(0); + assertFalse(ormCollectionTable.specifiedJoinColumns().hasNext()); + } + + public void testUniqueConstraints() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + ListIterator<OrmUniqueConstraint> uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + + XmlUniqueConstraint uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(0, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "foo"); + + uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(0, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "bar"); + + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertTrue(uniqueConstraints.hasNext()); + assertEquals("bar", uniqueConstraints.next().columnNames().next()); + assertEquals("foo", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testUniqueConstraintsSize() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + assertEquals(0, ormCollectionTable.uniqueConstraintsSize()); + + XmlUniqueConstraint uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(0, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "foo"); + + uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(1, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "bar"); + + assertEquals(2, ormCollectionTable.uniqueConstraintsSize()); + } + + public void testAddUniqueConstraint() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "BAR"); + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "BAZ"); + + ListIterator<XmlUniqueConstraint> uniqueConstraints = resourceCollectionTable.getUniqueConstraints().listIterator(); + + assertEquals("BAZ", uniqueConstraints.next().getColumnNames().get(0)); + assertEquals("BAR", uniqueConstraints.next().getColumnNames().get(0)); + assertEquals("FOO", uniqueConstraints.next().getColumnNames().get(0)); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testAddUniqueConstraint2() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + ormCollectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "BAZ"); + + ListIterator<XmlUniqueConstraint> uniqueConstraints = resourceCollectionTable.getUniqueConstraints().listIterator(); + + assertEquals("BAZ", uniqueConstraints.next().getColumnNames().get(0)); + assertEquals("FOO", uniqueConstraints.next().getColumnNames().get(0)); + assertEquals("BAR", uniqueConstraints.next().getColumnNames().get(0)); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testRemoveUniqueConstraint() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + ormCollectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + ormCollectionTable.addUniqueConstraint(2).addColumnName(0, "BAZ"); + + assertEquals(3, resourceCollectionTable.getUniqueConstraints().size()); + + ormCollectionTable.removeUniqueConstraint(1); + + ListIterator<XmlUniqueConstraint> uniqueConstraintResources = resourceCollectionTable.getUniqueConstraints().listIterator(); + assertEquals("FOO", uniqueConstraintResources.next().getColumnNames().get(0)); + assertEquals("BAZ", uniqueConstraintResources.next().getColumnNames().get(0)); + assertFalse(uniqueConstraintResources.hasNext()); + + Iterator<OrmUniqueConstraint> uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + + ormCollectionTable.removeUniqueConstraint(1); + uniqueConstraintResources = resourceCollectionTable.getUniqueConstraints().listIterator(); + assertEquals("FOO", uniqueConstraintResources.next().getColumnNames().get(0)); + assertFalse(uniqueConstraintResources.hasNext()); + + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + + ormCollectionTable.removeUniqueConstraint(0); + uniqueConstraintResources = resourceCollectionTable.getUniqueConstraints().listIterator(); + assertFalse(uniqueConstraintResources.hasNext()); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testMoveUniqueConstraint() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + ormCollectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + ormCollectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + ormCollectionTable.addUniqueConstraint(2).addColumnName(0, "BAZ"); + + assertEquals(3, resourceCollectionTable.getUniqueConstraints().size()); + + + ormCollectionTable.moveUniqueConstraint(2, 0); + ListIterator<OrmUniqueConstraint> uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + + ListIterator<XmlUniqueConstraint> uniqueConstraintResources = resourceCollectionTable.getUniqueConstraints().listIterator(); + assertEquals("BAR", uniqueConstraintResources.next().getColumnNames().get(0)); + assertEquals("BAZ", uniqueConstraintResources.next().getColumnNames().get(0)); + assertEquals("FOO", uniqueConstraintResources.next().getColumnNames().get(0)); + + + ormCollectionTable.moveUniqueConstraint(0, 1); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + + uniqueConstraintResources = resourceCollectionTable.getUniqueConstraints().listIterator(); + assertEquals("BAZ", uniqueConstraintResources.next().getColumnNames().get(0)); + assertEquals("BAR", uniqueConstraintResources.next().getColumnNames().get(0)); + assertEquals("FOO", uniqueConstraintResources.next().getColumnNames().get(0)); + } + + public void testUpdateUniqueConstraints() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + XmlElementCollection_2_0 resourceElementCollection = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + resourceElementCollection.setCollectionTable(OrmFactory.eINSTANCE.createXmlCollectionTable()); + XmlCollectionTable resourceCollectionTable = resourceElementCollection.getCollectionTable(); + + XmlUniqueConstraint uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(0, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "FOO"); + + uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(1, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "BAR"); + + uniqueConstraintResource = OrmFactory.eINSTANCE.createXmlUniqueConstraint(); + resourceCollectionTable.getUniqueConstraints().add(2, uniqueConstraintResource); + uniqueConstraintResource.getColumnNames().add(0, "BAZ"); + + + ListIterator<OrmUniqueConstraint> uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + resourceCollectionTable.getUniqueConstraints().move(2, 0); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + resourceCollectionTable.getUniqueConstraints().move(0, 1); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + resourceCollectionTable.getUniqueConstraints().remove(1); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + resourceCollectionTable.getUniqueConstraints().remove(1); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + resourceCollectionTable.getUniqueConstraints().remove(0); + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + } + + public void testUniqueConstraintsFromJava() throws Exception { + createTestEntityWithValidElementCollection(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentType.attributes().next().getMapping(); + OrmCollectionTable2_0 ormCollectionTable = ormElementCollectionMapping.getCollectionTable(); + + assertTrue(ormElementCollectionMapping.getPersistentAttribute().isVirtual()); + + ListIterator<OrmUniqueConstraint> uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + + JavaElementCollectionMapping2_0 javaElementCollectionMapping2_0 = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().attributes().next().getMapping(); + JavaCollectionTable2_0 javaCollectionTable = javaElementCollectionMapping2_0.getCollectionTable(); + + javaCollectionTable.addUniqueConstraint(0).addColumnName(0, "FOO"); + javaCollectionTable.addUniqueConstraint(1).addColumnName(0, "BAR"); + javaCollectionTable.addUniqueConstraint(2).addColumnName(0, "BAZ"); + + uniqueConstraints = ormCollectionTable.uniqueConstraints(); + assertTrue(uniqueConstraints.hasNext()); + assertEquals("FOO", uniqueConstraints.next().columnNames().next()); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + + ormElementCollectionMapping.getPersistentAttribute().makeSpecified(); + + ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentType.attributes().next().getMapping(); + assertEquals(0, ormElementCollectionMapping.getCollectionTable().uniqueConstraintsSize()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmElementCollectionMappingTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmElementCollectionMappingTests.java index baa8655754..a4a8c70ed2 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmElementCollectionMappingTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/EclipseLink2_0OrmElementCollectionMappingTests.java @@ -10,9 +10,11 @@ package org.eclipse.jpt.eclipselink2_0.core.tests.internal.context.orm; import java.util.Iterator; +import java.util.ListIterator; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jpt.core.MappingKeys; import org.eclipse.jpt.core.context.BasicMapping; +import org.eclipse.jpt.core.context.Column; import org.eclipse.jpt.core.context.EmbeddedIdMapping; import org.eclipse.jpt.core.context.EmbeddedMapping; import org.eclipse.jpt.core.context.FetchType; @@ -22,6 +24,11 @@ import org.eclipse.jpt.core.context.ManyToOneMapping; import org.eclipse.jpt.core.context.OneToManyMapping; import org.eclipse.jpt.core.context.TransientMapping; import org.eclipse.jpt.core.context.VersionMapping; +import org.eclipse.jpt.core.context.java.JavaBasicMapping; +import org.eclipse.jpt.core.context.orm.OrmAttributeOverride; +import org.eclipse.jpt.core.context.orm.OrmAttributeOverrideContainer; +import org.eclipse.jpt.core.context.orm.OrmColumn; +import org.eclipse.jpt.core.context.orm.OrmEntity; import org.eclipse.jpt.core.context.orm.OrmOneToManyMapping; import org.eclipse.jpt.core.context.orm.OrmPersistentAttribute; import org.eclipse.jpt.core.context.orm.OrmPersistentType; @@ -43,6 +50,9 @@ import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; @SuppressWarnings("nls") public class EclipseLink2_0OrmElementCollectionMappingTests extends EclipseLink2_0OrmContextModelTestCase { + private static final String ATTRIBUTE_OVERRIDE_NAME = "city"; + private static final String ATTRIBUTE_OVERRIDE_COLUMN_NAME = "E_CITY"; + public EclipseLink2_0OrmElementCollectionMappingTests(String name) { super(name); } @@ -104,7 +114,7 @@ public class EclipseLink2_0OrmElementCollectionMappingTests extends EclipseLink2 return this.createTestType(new DefaultAnnotationWriter() { @Override public Iterator<String> imports() { - return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.FETCH_TYPE); + return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.FETCH_TYPE, JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); } @Override public void appendTypeAnnotationTo(StringBuilder sb) { @@ -116,6 +126,7 @@ public class EclipseLink2_0OrmElementCollectionMappingTests extends EclipseLink2 sb.append(CR); sb.append(" @ElementCollection"); sb.append(CR); + sb.append(" @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))"); sb.append(" private java.util.Collection<Address> addresses;").append(CR); sb.append(CR); sb.append(" @Id"); @@ -917,4 +928,352 @@ public class EclipseLink2_0OrmElementCollectionMappingTests extends EclipseLink2 assertEquals(TYPE_NAME + "_addresses", orderable.getOrderColumn().getTable()); assertEquals("FOO", orderable.getOrderColumn().getName()); } + + public void testVirtualValueColumnDefaults() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + //virtual attrubte in orm.xml, java attribute has no value Column annotation + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + assertEquals("addresses", ormColumn.getSpecifiedName()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + assertEquals(null, ormColumn.getColumnDefinition()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedUnique()); + assertEquals(Column.DEFAULT_LENGTH, ormColumn.getSpecifiedLength().intValue()); + assertEquals(Column.DEFAULT_PRECISION, ormColumn.getSpecifiedPrecision().intValue()); + assertEquals(Column.DEFAULT_SCALE, ormColumn.getSpecifiedScale().intValue()); + + //set Column annotation in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getValueColumn().setSpecifiedName("FOO"); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("FOO_TABLE"); + javaElementCollectionMapping.getValueColumn().setColumnDefinition("COLUMN_DEFINITION"); + javaElementCollectionMapping.getValueColumn().setSpecifiedInsertable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUpdatable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedNullable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUnique(Boolean.TRUE); + javaElementCollectionMapping.getValueColumn().setSpecifiedLength(Integer.valueOf(45)); + javaElementCollectionMapping.getValueColumn().setSpecifiedPrecision(Integer.valueOf(46)); + javaElementCollectionMapping.getValueColumn().setSpecifiedScale(Integer.valueOf(47)); + + assertEquals("FOO", ormColumn.getSpecifiedName()); + assertEquals("FOO_TABLE", ormColumn.getSpecifiedTable()); + assertEquals("COLUMN_DEFINITION", ormColumn.getColumnDefinition()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, ormColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, ormColumn.getSpecifiedUnique()); + assertEquals(Integer.valueOf(45), ormColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(46), ormColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(47), ormColumn.getSpecifiedScale()); + + + //set metadata-complete, orm.xml virtual column ignores java column annotation + ormPersistentType.getMapping().setSpecifiedMetadataComplete(Boolean.TRUE); + addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + //no longer an element collection mapping + assertEquals(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY, addressesPersistentAttribute.getMappingKey()); + } + + public void testNullColumnDefaults() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "addresses"); + + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + + //set Column annotation in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getValueColumn().setSpecifiedName("FOO"); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("FOO_TABLE"); + javaElementCollectionMapping.getValueColumn().setColumnDefinition("COLUMN_DEFINITION"); + javaElementCollectionMapping.getValueColumn().setSpecifiedInsertable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUpdatable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedNullable(Boolean.FALSE); + javaElementCollectionMapping.getValueColumn().setSpecifiedUnique(Boolean.TRUE); + javaElementCollectionMapping.getValueColumn().setSpecifiedLength(Integer.valueOf(45)); + javaElementCollectionMapping.getValueColumn().setSpecifiedPrecision(Integer.valueOf(46)); + javaElementCollectionMapping.getValueColumn().setSpecifiedScale(Integer.valueOf(47)); + + + assertEquals("addresses", ormColumn.getDefaultName()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getDefaultTable()); + assertEquals(true, ormColumn.isDefaultInsertable()); + assertEquals(true, ormColumn.isDefaultUpdatable()); + assertEquals(true, ormColumn.isDefaultNullable()); + assertEquals(false, ormColumn.isDefaultUnique()); + assertEquals(Column.DEFAULT_LENGTH, ormColumn.getDefaultLength()); + assertEquals(Column.DEFAULT_PRECISION, ormColumn.getDefaultPrecision()); + assertEquals(Column.DEFAULT_SCALE, ormColumn.getDefaultScale()); + assertNull(ormColumn.getSpecifiedName()); + assertNull(ormColumn.getSpecifiedTable()); + assertNull(ormColumn.getColumnDefinition()); + assertNull(ormColumn.getSpecifiedInsertable()); + assertNull(ormColumn.getSpecifiedUpdatable()); + assertNull(ormColumn.getSpecifiedNullable()); + assertNull(ormColumn.getSpecifiedUnique()); + assertNull(ormColumn.getSpecifiedLength()); + assertNull(ormColumn.getSpecifiedPrecision()); + assertNull(ormColumn.getSpecifiedScale()); + } + + public void testVirtualValueColumnTable() throws Exception { + createTestEntityWithGenericBasicElementCollectionMapping(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + //virtual attribute in orm.xml, java attribute has no Column annotation + OrmPersistentAttribute addressesPersistentAttribute = ormPersistentType.virtualAttributes().next(); + OrmElementCollectionMapping2_0 addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + OrmColumn ormColumn = addressesVirtualMapping.getValueColumn(); + + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + + //entity table should have no affect on the collection table default name + ((OrmEntity) ormPersistentType.getMapping()).getTable().setSpecifiedName("ORM_TABLE"); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getSpecifiedTable()); + + //set Column table element in Java + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentType.getJavaPersistentType().getAttributeNamed("addresses").getMapping(); + javaElementCollectionMapping.getCollectionTable().setSpecifiedName("JAVA_COLLECTION_TABLE"); + assertEquals("JAVA_COLLECTION_TABLE", ormColumn.getSpecifiedTable()); + javaElementCollectionMapping.getValueColumn().setSpecifiedTable("JAVA_TABLE"); + assertEquals("JAVA_TABLE", ormColumn.getSpecifiedTable()); + + //make name persistent attribute not virtual + addressesPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "addresses"); + addressesVirtualMapping = (OrmElementCollectionMapping2_0) addressesPersistentAttribute.getMapping(); + ormColumn = addressesVirtualMapping.getValueColumn(); + assertNull(ormColumn.getSpecifiedTable()); + assertEquals(TYPE_NAME + "_addresses", ormColumn.getDefaultTable()); + } + public void testMoveSpecifiedAttributeOverride() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + XmlElementCollection_2_0 elementCollectionResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + + elementCollectionResource.getAttributeOverrides().get(0).setName("FOO"); + elementCollectionResource.getAttributeOverrides().get(1).setName("BAR"); + elementCollectionResource.getAttributeOverrides().get(2).setName("BAZ"); + + assertEquals(3, elementCollectionResource.getAttributeOverrides().size()); + + attributeOverrideContainer.moveSpecifiedAttributeOverride(2, 0); + ListIterator<OrmAttributeOverride> attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAR", elementCollectionResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", elementCollectionResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", elementCollectionResource.getAttributeOverrides().get(2).getName()); + + + attributeOverrideContainer.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAZ", elementCollectionResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAR", elementCollectionResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", elementCollectionResource.getAttributeOverrides().get(2).getName()); + } + + public void testUpdateAttributeOverrides() throws Exception { + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "elementCollectionMapping"); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + XmlElementCollection_2_0 elementCollectionResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getElementCollections().get(0); + + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + elementCollectionResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createXmlAttributeOverride()); + + elementCollectionResource.getAttributeOverrides().get(0).setName("FOO"); + elementCollectionResource.getAttributeOverrides().get(1).setName("BAR"); + elementCollectionResource.getAttributeOverrides().get(2).setName("BAZ"); + + ListIterator<OrmAttributeOverride> attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().move(2, 0); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().move(0, 1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(1); + attributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + elementCollectionResource.getAttributeOverrides().remove(0); + assertFalse(attributeOverrideContainer.specifiedAttributeOverrides().hasNext()); + } + + public void testElementCollectionMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityWithEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + + OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + ormPersistentType.addSpecifiedAttribute(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(3, ormPersistentType.virtualAttributesSize()); + + OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.specifiedAttributes().next(); + OrmElementCollectionMapping2_0 ormElementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = ormElementCollectionMapping.getValueAttributeOverrideContainer(); + + assertEquals("foo", ormElementCollectionMapping.getName()); + + + assertFalse(attributeOverrideContainer.specifiedAttributeOverrides().hasNext()); + assertFalse(attributeOverrideContainer.virtualAttributeOverrides().hasNext()); + } + + public void testVirtualAttributeOverrides() throws Exception { + createTestEntityWithEmbeddableElementCollectionMapping(); + createTestTargetEmbeddableAddress(); + createTestEmbeddableState(); + + OrmPersistentType persistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + OrmPersistentType persistentType2 = getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + OrmPersistentType persistentType3 = getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State"); + + //embedded mapping is virtual, specified attribute overrides should exist + OrmPersistentAttribute ormPersistentAttribute = persistentType.getAttributeNamed("addresses"); + OrmElementCollectionMapping2_0 elementCollectionMapping = (OrmElementCollectionMapping2_0) ormPersistentAttribute.getMapping(); + OrmAttributeOverrideContainer attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.specifiedAttributeOverridesSize()); + ListIterator<OrmAttributeOverride> specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + OrmAttributeOverride attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + + JavaElementCollectionMapping2_0 javaElementCollectionMapping = (JavaElementCollectionMapping2_0) ormPersistentAttribute.getJavaPersistentAttribute().getMapping(); + Column javaAttributeOverrideColumn = javaElementCollectionMapping.getValueAttributeOverrideContainer().specifiedAttributeOverrides().next().getColumn(); + + javaAttributeOverrideColumn.setSpecifiedName("FOO_COLUMN"); + javaAttributeOverrideColumn.setSpecifiedTable("FOO_TABLE"); + javaAttributeOverrideColumn.setColumnDefinition("COLUMN_DEF"); + javaAttributeOverrideColumn.setSpecifiedInsertable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedUpdatable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedUnique(Boolean.TRUE); + javaAttributeOverrideColumn.setSpecifiedNullable(Boolean.FALSE); + javaAttributeOverrideColumn.setSpecifiedLength(Integer.valueOf(5)); + javaAttributeOverrideColumn.setSpecifiedPrecision(Integer.valueOf(6)); + javaAttributeOverrideColumn.setSpecifiedScale(Integer.valueOf(7)); + + JavaBasicMapping javaBasicMapping = (JavaBasicMapping) persistentType3.getJavaPersistentType().getAttributeNamed("name").getMapping(); + javaBasicMapping.getColumn().setSpecifiedName("MY_STATE_COLUMN"); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.specifiedAttributeOverridesSize()); + specifiedAttributeOverrides = attributeOverrideContainer.specifiedAttributeOverrides(); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + assertEquals("FOO_COLUMN", attributeOverride.getColumn().getSpecifiedName()); + assertEquals("FOO_TABLE", attributeOverride.getColumn().getSpecifiedTable()); + assertEquals("COLUMN_DEF", attributeOverride.getColumn().getColumnDefinition()); + assertEquals(false, attributeOverride.getColumn().isInsertable()); + assertEquals(false, attributeOverride.getColumn().isUpdatable()); + assertEquals(true, attributeOverride.getColumn().isUnique()); + assertEquals(false, attributeOverride.getColumn().isNullable()); + assertEquals(5, attributeOverride.getColumn().getLength()); + assertEquals(6, attributeOverride.getColumn().getPrecision()); + assertEquals(7, attributeOverride.getColumn().getScale()); + + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + assertEquals("MY_STATE_COLUMN", attributeOverride.getColumn().getSpecifiedName()); + attributeOverride = specifiedAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + + + + //embedded mapping is specified, virtual attribute overrides should exist + persistentType.getAttributeNamed("addresses").makeSpecified(); + elementCollectionMapping = (OrmElementCollectionMapping2_0) persistentType.getAttributeNamed("addresses").getMapping(); + attributeOverrideContainer = elementCollectionMapping.getValueAttributeOverrideContainer(); + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(4, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals(0, attributeOverrideContainer.specifiedAttributeOverridesSize()); + ListIterator<OrmAttributeOverride> virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("city", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + assertEquals("MY_STATE_COLUMN", attributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME + "_addresses", attributeOverride.getColumn().getTable()); + assertEquals(null, attributeOverride.getColumn().getColumnDefinition()); + assertEquals(true, attributeOverride.getColumn().isInsertable()); + assertEquals(true, attributeOverride.getColumn().isUpdatable()); + assertEquals(false, attributeOverride.getColumn().isUnique()); + assertEquals(true, attributeOverride.getColumn().isNullable()); + assertEquals(255, attributeOverride.getColumn().getLength()); + assertEquals(0, attributeOverride.getColumn().getPrecision()); + assertEquals(0, attributeOverride.getColumn().getScale()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + assertEquals(TYPE_NAME + "_addresses", attributeOverride.getColumn().getDefaultTable()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + + //set one of the virtual attribute overrides to specified, verify others are still virtual + attributeOverrideContainer.virtualAttributeOverrides().next().setVirtual(false); + + assertEquals(4, attributeOverrideContainer.attributeOverridesSize()); + assertEquals(1, attributeOverrideContainer.specifiedAttributeOverridesSize()); + assertEquals(3, attributeOverrideContainer.virtualAttributeOverridesSize()); + assertEquals("city", attributeOverrideContainer.specifiedAttributeOverrides().next().getName()); + virtualAttributeOverrides = attributeOverrideContainer.virtualAttributeOverrides(); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.name", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("state.abbr", attributeOverride.getName()); + attributeOverride = virtualAttributeOverrides.next(); + assertEquals("zip", attributeOverride.getName()); + } }
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/JptEclipseLink2_0OrmContextModelTests.java b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/JptEclipseLink2_0OrmContextModelTests.java index dd36269174..484105f77c 100644 --- a/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/JptEclipseLink2_0OrmContextModelTests.java +++ b/jpa/tests/org.eclipse.jpt.eclipselink.core.tests/src/org/eclipse/jpt/eclipselink2_0/core/tests/internal/context/orm/JptEclipseLink2_0OrmContextModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2009 Oracle. All rights reserved. +* Copyright (c) 2009, 2010 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. @@ -17,6 +17,7 @@ public class JptEclipseLink2_0OrmContextModelTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(JptEclipseLink2_0OrmContextModelTests.class.getName()); + suite.addTestSuite(EclipseLink2_0OrmCollectionTableTests.class); suite.addTestSuite(EclipseLink2_0OrmElementCollectionMappingTests.class); suite.addTestSuite(EclipseLink2_0OrmEntityTests.class); suite.addTestSuite(EclipseLink2_0OrmManyToManyMappingTests.class); |