blob: caca95b2d2777581efd8201e56d0196264cdfd45 [file] [log] [blame]
/*******************************************************************************
* 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.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);
}
});
}
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, 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, 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();
assertEquals(false, oneToManyMapping.isOrphanRemoval());
oneToManyMapping.setSpecifiedOrphanRemoval(Boolean.TRUE);
assertEquals(true, oneToManyMapping.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();
assertNull(oneToManyMapping.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, oneToManyMapping.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();
assertEquals(Boolean.FALSE, oneToManyMapping.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();
assertNull(oneToManyMapping.getSpecifiedOrphanRemoval());
oneToManyMapping.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.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();
assertNull(oneToManyMapping.getSpecifiedOrphanRemoval());
oneToManyMapping.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, oneToManyMapping.getSpecifiedOrphanRemoval());
oneToManyMapping.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();
assertNull(oneToManyMapping.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, oneToManyMapping.getSpecifiedOrphanRemoval());
oneToMany.setOrphanRemoval(null);
assertNull(oneToManyMapping.getSpecifiedOrphanRemoval());
assertSame(oneToManyMapping, persistentAttribute.getSpecifiedMapping());
oneToMany.setOrphanRemoval(Boolean.FALSE);
attributeResource.setPrimaryAnnotation(null, new String[0]);
assertNull(persistentAttribute.getSpecifiedMapping());
}
}