| /******************************************************************************* |
| * Copyright (c) 2009 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.core.tests.internal.jpa2.context.orm; |
| |
| import java.util.Iterator; |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jpt.core.MappingKeys; |
| import org.eclipse.jpt.core.context.AttributeMapping; |
| import org.eclipse.jpt.core.context.OneToManyMapping; |
| import org.eclipse.jpt.core.context.PersistentAttribute; |
| import org.eclipse.jpt.core.context.orm.OrmPersistentAttribute; |
| import org.eclipse.jpt.core.context.orm.OrmPersistentType; |
| import org.eclipse.jpt.core.jpa2.context.OneToManyMapping2_0; |
| import org.eclipse.jpt.core.jpa2.context.orm.OrmOneToManyMapping2_0; |
| import org.eclipse.jpt.core.jpa2.context.orm.OrmOrphanRemovable2_0; |
| import org.eclipse.jpt.core.jpa2.context.orm.OrmOrphanRemovalHolder2_0; |
| import org.eclipse.jpt.core.resource.java.JPA; |
| import org.eclipse.jpt.core.resource.orm.XmlOneToMany; |
| import org.eclipse.jpt.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase; |
| import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; |
| import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; |
| |
| @SuppressWarnings("nls") |
| public class GenericOrmOneToManyMapping2_0Tests |
| extends Generic2_0ContextModelTestCase |
| { |
| public GenericOrmOneToManyMapping2_0Tests(String name) { |
| super(name); |
| } |
| |
| |
| private void createTestTargetEntityAddress() throws Exception { |
| SourceWriter sourceWriter = new SourceWriter() { |
| public void appendSourceTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append("import "); |
| sb.append(JPA.ENTITY); |
| sb.append(";"); |
| sb.append(CR); |
| sb.append("import "); |
| sb.append(JPA.ID); |
| sb.append(";"); |
| sb.append(CR); |
| sb.append(CR); |
| sb.append("@Entity"); |
| sb.append(CR); |
| sb.append("public class ").append("Address").append(" "); |
| sb.append("{").append(CR); |
| sb.append(CR); |
| sb.append(" @Id").append(CR); |
| sb.append(" private int id;").append(CR); |
| sb.append(CR); |
| sb.append(" private String city;").append(CR); |
| sb.append(CR); |
| sb.append(" private State state;").append(CR); |
| sb.append(CR); |
| sb.append(" private int zip;").append(CR); |
| sb.append(CR); |
| sb.append("}").append(CR); |
| } |
| }; |
| this.javaProject.createCompilationUnit(PACKAGE_NAME, "Address.java", sourceWriter); |
| } |
| |
| private ICompilationUnit createTestEntityWithValidOneToManyMapping() throws Exception { |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity").append(CR); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append(" @OneToMany").append(CR); |
| sb.append(" private java.util.Collection<Address> addresses;").append(CR); |
| sb.append(CR); |
| sb.append(" @Id").append(CR); |
| } |
| }); |
| } |
| |
| private void createTestEmbeddableState() throws Exception { |
| SourceWriter sourceWriter = new SourceWriter() { |
| public void appendSourceTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append("import "); |
| sb.append(JPA.EMBEDDABLE); |
| sb.append(";"); |
| sb.append(CR); |
| sb.append("@Embeddable"); |
| sb.append(CR); |
| sb.append("public class ").append("State").append(" "); |
| sb.append("{").append(CR); |
| sb.append(CR); |
| sb.append(" private String foo;").append(CR); |
| sb.append(CR); |
| sb.append(" private Address address;").append(CR); |
| sb.append(CR); |
| sb.append("}").append(CR); |
| } |
| }; |
| this.javaProject.createCompilationUnit(PACKAGE_NAME, "State.java", sourceWriter); |
| } |
| |
| public void testCandidateMappedByAttributeNames() throws Exception { |
| createTestEntityWithValidOneToManyMapping(); |
| createTestTargetEntityAddress(); |
| createTestEmbeddableState(); |
| OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); |
| getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); |
| getEntityMappings().addPersistentType(MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".State"); |
| ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "addresses"); |
| |
| addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| addXmlClassRef(PACKAGE_NAME + ".Address"); |
| addXmlClassRef(PACKAGE_NAME + ".State"); |
| |
| PersistentAttribute persistentAttribute = ormPersistentType.attributes().next(); |
| OneToManyMapping oneToManyMapping = (OneToManyMapping) persistentAttribute.getMapping(); |
| |
| Iterator<String> attributeNames = |
| oneToManyMapping.getRelationshipReference().getMappedByJoiningStrategy().candidateMappedByAttributeNames(); |
| assertEquals("id", attributeNames.next()); |
| assertEquals("city", attributeNames.next()); |
| assertEquals("state", attributeNames.next()); |
| assertEquals("state.foo", attributeNames.next()); |
| assertEquals("state.address", attributeNames.next()); |
| assertEquals("zip", attributeNames.next()); |
| assertFalse(attributeNames.hasNext()); |
| |
| oneToManyMapping.setSpecifiedTargetEntity("foo"); |
| attributeNames = |
| oneToManyMapping.getRelationshipReference().getMappedByJoiningStrategy().candidateMappedByAttributeNames(); |
| assertFalse(attributeNames.hasNext()); |
| |
| oneToManyMapping.setSpecifiedTargetEntity(null); |
| attributeNames = |
| oneToManyMapping.getRelationshipReference().getMappedByJoiningStrategy().candidateMappedByAttributeNames(); |
| assertEquals("id", attributeNames.next()); |
| assertEquals("city", attributeNames.next()); |
| assertEquals("state", attributeNames.next()); |
| assertEquals("state.foo", attributeNames.next()); |
| assertEquals("state.address", attributeNames.next()); |
| assertEquals("zip", attributeNames.next()); |
| assertFalse(attributeNames.hasNext()); |
| |
| AttributeMapping stateFooMapping = oneToManyMapping.getResolvedTargetEntity().resolveAttributeMapping("state.foo"); |
| assertEquals("foo", stateFooMapping.getName()); |
| } |
| |
| private OrmOrphanRemovable2_0 getOrphanRemovalOf(OneToManyMapping2_0 oneToManyMapping) { |
| return ((OrmOrphanRemovalHolder2_0) oneToManyMapping).getOrphanRemoval(); |
| } |
| |
| public void testUpdateSpecifiedOrphanRemoval() throws Exception { |
| OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); |
| OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); |
| OrmOneToManyMapping2_0 ormOneToManyMapping = (OrmOneToManyMapping2_0) ormPersistentAttribute.getMapping(); |
| OrmOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(ormOneToManyMapping); |
| XmlOneToMany oneToManyResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); |
| |
| assertEquals(null, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| assertEquals(null, oneToManyResource.getOrphanRemoval()); |
| |
| //set enumerated in the resource model, verify context model updated |
| oneToManyResource.setOrphanRemoval(Boolean.TRUE); |
| assertEquals(Boolean.TRUE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| assertEquals(Boolean.TRUE, oneToManyResource.getOrphanRemoval()); |
| |
| oneToManyResource.setOrphanRemoval(Boolean.FALSE); |
| assertEquals(Boolean.FALSE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| assertEquals(Boolean.FALSE, oneToManyResource.getOrphanRemoval()); |
| } |
| |
| public void testModifySpecifiedOrphanRemoval() throws Exception { |
| OrmPersistentType ormPersistentType = getEntityMappings().addPersistentType(MappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); |
| OrmPersistentAttribute ormPersistentAttribute = ormPersistentType.addSpecifiedAttribute(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); |
| OrmOneToManyMapping2_0 ormOneToManyMapping = (OrmOneToManyMapping2_0) ormPersistentAttribute.getMapping(); |
| OrmOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(ormOneToManyMapping); |
| XmlOneToMany oneToManyResource = getXmlEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); |
| |
| assertEquals(null, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| assertEquals(null, oneToManyResource.getOrphanRemoval()); |
| |
| //set enumerated in the context model, verify resource model updated |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(Boolean.TRUE); |
| assertEquals(Boolean.TRUE, oneToManyResource.getOrphanRemoval()); |
| assertEquals(Boolean.TRUE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(Boolean.FALSE); |
| assertEquals(Boolean.FALSE, oneToManyResource.getOrphanRemoval()); |
| assertEquals(Boolean.FALSE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| } |
| } |