| /******************************************************************************* |
| * 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.eclipselink2_0.core.tests.internal.context.java; |
| |
| import java.util.Iterator; |
| |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jpt.core.context.PersistentAttribute; |
| import org.eclipse.jpt.core.jpa2.context.OneToManyMapping2_0; |
| import org.eclipse.jpt.core.jpa2.context.java.JavaOrphanRemovable2_0; |
| import org.eclipse.jpt.core.jpa2.context.java.JavaOrphanRemovalHolder2_0; |
| import org.eclipse.jpt.core.jpa2.resource.java.OneToMany2_0Annotation; |
| 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.eclipselink2_0.core.tests.internal.context.EclipseLink2_0ContextModelTestCase; |
| import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; |
| |
| /** |
| * EclipseLink2_0JavaOneToManyMappingTests |
| */ |
| public class EclipseLink2_0JavaOneToManyMappingTests |
| extends EclipseLink2_0ContextModelTestCase |
| { |
| public EclipseLink2_0JavaOneToManyMappingTests(String name) { |
| super(name); |
| } |
| |
| private ICompilationUnit createTestEntity() throws Exception { |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID, "java.util.Collection"); |
| } |
| @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 Collection<Address> addresses;").append(CR); |
| sb.append(CR); |
| sb.append(" @Id").append(CR); |
| } |
| }); |
| } |
| |
| private ICompilationUnit createTestEntityWithValidOneToManyMappingOrphanRemovalSpecified() throws Exception { |
| return this.createTestType(new DefaultAnnotationWriter() { |
| @Override |
| public Iterator<String> imports() { |
| return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID, "java.util.Collection"); |
| } |
| @Override |
| public void appendTypeAnnotationTo(StringBuilder sb) { |
| sb.append("@Entity").append(CR); |
| } |
| |
| @Override |
| public void appendIdFieldAnnotationTo(StringBuilder sb) { |
| sb.append(CR); |
| sb.append(" @OneToMany(orphanRemoval=false)").append(CR); |
| sb.append(" private Collection<Address> addresses;").append(CR); |
| sb.append(CR); |
| sb.append(" @Id").append(CR); |
| } |
| }); |
| } |
| |
| private JavaOrphanRemovable2_0 getOrphanRemovalOf(OneToManyMapping2_0 oneToManyMapping) { |
| return ((JavaOrphanRemovalHolder2_0) oneToManyMapping).getOrphanRemoval(); |
| } |
| |
| public void testDefaultOneToManyGetDefaultOrphanRemoval() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getMapping(); |
| assertEquals(false, this.getOrphanRemovalOf(oneToManyMapping).isDefaultOrphanRemoval()); |
| } |
| |
| public void testSpecifiedOneToManyGetDefaultOrphanRemoval() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getSpecifiedMapping(); |
| assertEquals(false, this.getOrphanRemovalOf(oneToManyMapping).isDefaultOrphanRemoval()); |
| } |
| |
| public void testGetOrphanRemoval() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getSpecifiedMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| |
| |
| assertEquals(false, mappingsOrphanRemoval.isOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(Boolean.TRUE); |
| assertEquals(true, mappingsOrphanRemoval.isOrphanRemoval()); |
| } |
| |
| public void testGetSpecifiedOrphanRemoval() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getSpecifiedMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| |
| assertNull(mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| OneToMany2_0Annotation oneToMany = (OneToMany2_0Annotation) attributeResource.getAnnotation(JPA.ONE_TO_MANY); |
| oneToMany.setOrphanRemoval(Boolean.FALSE); |
| |
| assertEquals(Boolean.FALSE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| } |
| |
| public void testGetSpecifiedOrphanRemoval2() throws Exception { |
| this.createTestEntityWithValidOneToManyMappingOrphanRemovalSpecified(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getSpecifiedMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| |
| assertEquals(Boolean.FALSE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| } |
| |
| public void testSetSpecifiedOrphanRemoval() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| assertNull(mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(Boolean.TRUE); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| OneToMany2_0Annotation oneToMany = (OneToMany2_0Annotation) attributeResource.getAnnotation(JPA.ONE_TO_MANY); |
| |
| assertEquals(Boolean.TRUE, oneToMany.getOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(null); |
| assertNotNull(attributeResource.getAnnotation(JPA.ONE_TO_MANY)); // .getElement); |
| } |
| |
| public void testSetSpecifiedOrphanRemoval2() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| assertNull(mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(Boolean.TRUE); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| OneToMany2_0Annotation oneToMany = (OneToMany2_0Annotation) attributeResource.getAnnotation(JPA.ONE_TO_MANY); |
| |
| assertEquals(Boolean.TRUE, oneToMany.getOrphanRemoval()); |
| |
| oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getMapping(); |
| assertEquals(Boolean.TRUE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| mappingsOrphanRemoval.setSpecifiedOrphanRemoval(null); |
| assertNotNull(attributeResource.getAnnotation(JPA.ONE_TO_MANY)); |
| |
| oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getMapping(); |
| } |
| |
| public void testGetSpecifiedOrphanRemovalUpdatesFromResourceModelChange() throws Exception { |
| this.createTestEntity(); |
| this.addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); |
| |
| PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next(); |
| OneToManyMapping2_0 oneToManyMapping = (OneToManyMapping2_0) persistentAttribute.getSpecifiedMapping(); |
| JavaOrphanRemovable2_0 mappingsOrphanRemoval = this.getOrphanRemovalOf(oneToManyMapping); |
| |
| assertNull(mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME); |
| JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next(); |
| OneToMany2_0Annotation oneToMany = (OneToMany2_0Annotation) attributeResource.getAnnotation(JPA.ONE_TO_MANY); |
| oneToMany.setOrphanRemoval(Boolean.FALSE); |
| |
| assertEquals(Boolean.FALSE, mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| |
| oneToMany.setOrphanRemoval(null); |
| assertNull(mappingsOrphanRemoval.getSpecifiedOrphanRemoval()); |
| assertSame(oneToManyMapping, persistentAttribute.getSpecifiedMapping()); |
| |
| oneToMany.setOrphanRemoval(Boolean.FALSE); |
| attributeResource.setPrimaryAnnotation(null, new String[0]); |
| |
| assertNull(persistentAttribute.getSpecifiedMapping()); |
| } |
| |
| } |