blob: 6e5b8b7719c410f3fc9973abc1cdaa11f642a245 [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.core.tests.internal.jpa2.context.java;
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.EmbeddedIdMapping;
import org.eclipse.jpt.core.context.EmbeddedMapping;
import org.eclipse.jpt.core.context.FetchType;
import org.eclipse.jpt.core.context.IdMapping;
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.TransientMapping;
import org.eclipse.jpt.core.context.TypeMapping;
import org.eclipse.jpt.core.context.VersionMapping;
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.resource.java.ElementCollection2_0Annotation;
import org.eclipse.jpt.core.jpa2.resource.java.JPA2_0;
import org.eclipse.jpt.core.resource.java.BasicAnnotation;
import org.eclipse.jpt.core.resource.java.EmbeddedAnnotation;
import org.eclipse.jpt.core.resource.java.EmbeddedIdAnnotation;
import org.eclipse.jpt.core.resource.java.IdAnnotation;
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.ManyToManyAnnotation;
import org.eclipse.jpt.core.resource.java.ManyToOneAnnotation;
import org.eclipse.jpt.core.resource.java.OneToManyAnnotation;
import org.eclipse.jpt.core.resource.java.TransientAnnotation;
import org.eclipse.jpt.core.resource.java.VersionAnnotation;
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 GenericJavaElementCollectionMapping2_0Tests extends Generic2_0ContextModelTestCase
{
private ICompilationUnit createTestEntityWithElementCollectionMapping() 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").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@ElementCollection").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithGenericElementCollectionMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @ElementCollection").append(CR);
sb.append(" private java.util.Collection<Address> addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithNonGenericElementCollectionMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @ElementCollection").append(CR);
sb.append(" private java.util.Collection addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithValidGenericMapElementCollectionMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @ElementCollection").append(CR);
sb.append(" private java.util.Map<Integer, Address> addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithValidNonGenericMapElementCollectionMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @ElementCollection").append(CR);
sb.append(" private java.util.Map addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
private void createTestTargetEmbeddableAddress() 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("import ");
sb.append(JPA.ID);
sb.append(";");
sb.append(CR);
sb.append("import ");
sb.append(JPA.EMBEDDED);
sb.append(";");
sb.append(CR);
sb.append("@Embeddable");
sb.append(CR);
sb.append("public class ").append("Address").append(" ");
sb.append("{").append(CR);
sb.append(CR);
sb.append(" private String city;").append(CR);
sb.append(CR);
sb.append(" @Embedded").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 createTestEntityWithGenericElementCollectionBasicType() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA2_0.ELEMENT_COLLECTION, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append(CR);
sb.append(" @ElementCollection").append(CR);
sb.append(" private java.util.Collection<String> addresses;").append(CR);
sb.append(CR);
sb.append(" @Id").append(CR);
}
});
}
public GenericJavaElementCollectionMapping2_0Tests(String name) {
super(name);
}
public void testMorphToBasicMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof BasicMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(BasicAnnotation.ANNOTATION_NAME));
}
public void testMorphToDefault() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY);
assertNull(persistentAttribute.getSpecifiedMapping());
assertTrue(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
}
public void testMorphToVersionMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof VersionMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
}
public void testMorphToIdMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IdMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(IdAnnotation.ANNOTATION_NAME));
}
public void testMorphToEmbeddedMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME));
}
public void testMorphToEmbeddedIdMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME));
}
public void testMorphToTransientMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof TransientMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(TransientAnnotation.ANNOTATION_NAME));
}
public void testMorphToManyToOneMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME));
}
public void testMorphToOneToManyMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME));
}
public void testMorphToManyToManyMapping() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertFalse(elementCollectionMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping);
assertFalse(persistentAttribute.getMapping().isDefault());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
assertNull(attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(attributeResource.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME));
}
public void testUpdateSpecifiedTargetEntity() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
ElementCollection2_0Annotation elementCollection = (ElementCollection2_0Annotation) attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME);
assertNull(elementCollectionMapping.getSpecifiedTargetClass());
assertNull(elementCollection.getTargetClass());
//set target class in the resource model, verify context model updated
elementCollection.setTargetClass("newTargetClass");
assertEquals("newTargetClass", elementCollectionMapping.getSpecifiedTargetClass());
assertEquals("newTargetClass", elementCollection.getTargetClass());
//set target class to null in the resource model
elementCollection.setTargetClass(null);
assertNull(elementCollectionMapping.getSpecifiedTargetClass());
assertNull(elementCollection.getTargetClass());
}
public void testModifySpecifiedTargetClass() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
ElementCollection2_0Annotation elementCollection = (ElementCollection2_0Annotation) attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME);
assertNull(elementCollectionMapping.getSpecifiedTargetClass());
assertNull(elementCollection.getTargetClass());
//set target class in the context model, verify resource model updated
elementCollectionMapping.setSpecifiedTargetClass("newTargetClass");
assertEquals("newTargetClass", elementCollectionMapping.getSpecifiedTargetClass());
assertEquals("newTargetClass", elementCollection.getTargetClass());
//set target class to null in the context model
elementCollectionMapping.setSpecifiedTargetClass(null);
assertNull(elementCollectionMapping.getSpecifiedTargetClass());
assertNull(elementCollection.getTargetClass());
}
public void testDefaultTargetClass() throws Exception {
createTestEntityWithGenericElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
//targetEntity not in the persistence unit, default still set, handled by validation
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
//add targetEntity to the persistence unit
addXmlClassRef(PACKAGE_NAME + ".Address");
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
//test default still the same when specified target entity it set
elementCollectionMapping.setSpecifiedTargetClass("foo");
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
ListIterator<ClassRef> classRefs = getPersistenceUnit().specifiedClassRefs();
classRefs.next();
ClassRef addressClassRef = classRefs.next();
JavaPersistentType addressPersistentType = addressClassRef.getJavaPersistentType();
//test target is not an Embeddable, default target entity still exists, this case handled with validation
addressPersistentType.setMappingKey(MappingKeys.NULL_TYPE_MAPPING_KEY);
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
}
public void testDefaultTargetClassNonGenericCollection() throws Exception {
createTestEntityWithNonGenericElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
addXmlClassRef(PACKAGE_NAME + ".Address");
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertNull(elementCollectionMapping.getDefaultTargetClass());
}
public void testDefaultTargetClassGenericCollection() throws Exception {
createTestEntityWithGenericElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
addXmlClassRef(PACKAGE_NAME + ".Address");
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
}
public void testDefaultTargetClassNonGenericMap() throws Exception {
createTestEntityWithValidNonGenericMapElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
addXmlClassRef(PACKAGE_NAME + ".Address");
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertNull(elementCollectionMapping.getDefaultTargetClass());
}
public void testDefaultTargetClassGenericMap() throws Exception {
createTestEntityWithValidGenericMapElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
addXmlClassRef(PACKAGE_NAME + ".Address");
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getDefaultTargetClass());
}
public void testTargetClass() throws Exception {
createTestEntityWithGenericElementCollectionMapping();
createTestTargetEmbeddableAddress();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getTargetClass());
elementCollectionMapping.setSpecifiedTargetClass("foo");
assertEquals("foo", elementCollectionMapping.getTargetClass());
elementCollectionMapping.setSpecifiedTargetClass(null);
assertEquals(PACKAGE_NAME + ".Address", elementCollectionMapping.getTargetClass());
}
public void testResolvedTargetEmbeddable() throws Exception {
createTestEntityWithGenericElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
//target embeddable not in the persistence unit
assertNull(elementCollectionMapping.getResolvedTargetEmbeddable());
//add target embeddable to the persistence unit, now target embeddable should resolve
createTestTargetEmbeddableAddress();
addXmlClassRef(PACKAGE_NAME + ".Address");
ListIterator<ClassRef> classRefs = getPersistenceUnit().specifiedClassRefs();
classRefs.next();
ClassRef addressClassRef = classRefs.next();
TypeMapping addressTypeMapping = addressClassRef.getJavaPersistentType().getMapping();
assertEquals(addressTypeMapping, elementCollectionMapping.getResolvedTargetEmbeddable());
//test default still the same when specified target entity it set
elementCollectionMapping.setSpecifiedTargetClass("foo");
assertNull(elementCollectionMapping.getResolvedTargetEmbeddable());
elementCollectionMapping.setSpecifiedTargetClass(PACKAGE_NAME + ".Address");
assertEquals(addressTypeMapping, elementCollectionMapping.getResolvedTargetEmbeddable());
elementCollectionMapping.setSpecifiedTargetClass(null);
assertEquals(addressTypeMapping, elementCollectionMapping.getResolvedTargetEmbeddable());
}
public void testResolvedTargetEmbeddableWithBasicType() throws Exception {
createTestEntityWithGenericElementCollectionBasicType();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
//target is a basic type, so resolved target embeddable is null
assertNull(elementCollectionMapping.getResolvedTargetEmbeddable());
}
public void testUpdateSpecifiedFetch() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
ElementCollection2_0Annotation elementCollection = (ElementCollection2_0Annotation) attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME);
assertNull(elementCollectionMapping.getSpecifiedFetch());
assertNull(elementCollection.getFetch());
//set fetch in the resource model, verify context model updated
elementCollection.setFetch(org.eclipse.jpt.core.resource.java.FetchType.EAGER);
assertEquals(FetchType.EAGER, elementCollectionMapping.getSpecifiedFetch());
assertEquals(org.eclipse.jpt.core.resource.java.FetchType.EAGER, elementCollection.getFetch());
elementCollection.setFetch(org.eclipse.jpt.core.resource.java.FetchType.LAZY);
assertEquals(FetchType.LAZY, elementCollectionMapping.getSpecifiedFetch());
assertEquals(org.eclipse.jpt.core.resource.java.FetchType.LAZY, elementCollection.getFetch());
//set fetch to null in the resource model
elementCollection.setFetch(null);
assertNull(elementCollectionMapping.getSpecifiedFetch());
assertNull(elementCollection.getFetch());
}
public void testModifySpecifiedFetch() throws Exception {
createTestEntityWithElementCollectionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
ElementCollectionMapping2_0 elementCollectionMapping = (ElementCollectionMapping2_0) persistentAttribute.getMapping();
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
ElementCollection2_0Annotation elementCollection = (ElementCollection2_0Annotation) attributeResource.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME);
assertNull(elementCollectionMapping.getSpecifiedFetch());
assertNull(elementCollection.getFetch());
//set fetch in the context model, verify resource model updated
elementCollectionMapping.setSpecifiedFetch(FetchType.EAGER);
assertEquals(FetchType.EAGER, elementCollectionMapping.getSpecifiedFetch());
assertEquals(org.eclipse.jpt.core.resource.java.FetchType.EAGER, elementCollection.getFetch());
elementCollectionMapping.setSpecifiedFetch(FetchType.LAZY);
assertEquals(FetchType.LAZY, elementCollectionMapping.getSpecifiedFetch());
assertEquals(org.eclipse.jpt.core.resource.java.FetchType.LAZY, elementCollection.getFetch());
//set fetch to null in the context model
elementCollectionMapping.setSpecifiedFetch(null);
assertNull(elementCollectionMapping.getSpecifiedFetch());
assertNull(elementCollection.getFetch());
}
}