blob: 5fd0a4ff3202ed79e7e8433a2de9458297d7e19b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 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.jpa.core.tests.internal.jpa2.context.java;
import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.Kind;
import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.BasicMapping;
import org.eclipse.jpt.jpa.core.context.EmbeddedIdMapping;
import org.eclipse.jpt.jpa.core.context.EmbeddedMapping;
import org.eclipse.jpt.jpa.core.context.IdMapping;
import org.eclipse.jpt.jpa.core.context.ManyToManyMapping;
import org.eclipse.jpt.jpa.core.context.ManyToOneMapping;
import org.eclipse.jpt.jpa.core.context.OneToManyMapping;
import org.eclipse.jpt.jpa.core.context.OneToOneMapping;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.TemporalConverter;
import org.eclipse.jpt.jpa.core.context.TemporalType;
import org.eclipse.jpt.jpa.core.context.TransientMapping;
import org.eclipse.jpt.jpa.core.context.VersionMapping;
import org.eclipse.jpt.jpa.core.jpa2.MappingKeys2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.ElementCollectionMapping2_0;
import org.eclipse.jpt.jpa.core.jpa2.resource.java.Access2_0Annotation;
import org.eclipse.jpt.jpa.core.jpa2.resource.java.ElementCollection2_0Annotation;
import org.eclipse.jpt.jpa.core.resource.java.BasicAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.ColumnAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.core.resource.java.ManyToManyAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.ManyToOneAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.OneToManyAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.OneToOneAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.TemporalAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.TransientAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.VersionAnnotation;
import org.eclipse.jpt.jpa.core.tests.internal.jpa2.context.Generic2_0ContextModelTestCase;
@SuppressWarnings("nls")
public class GenericJavaVersionMapping2_0Tests extends Generic2_0ContextModelTestCase
{
private ICompilationUnit createTestEntityWithVersionMapping() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.VERSION);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Version").append(CR);
}
});
}
public GenericJavaVersionMapping2_0Tests(String name) {
super(name);
}
public void testMorphToBasicMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType());
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(BasicAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToDefault() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((BasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((TemporalConverter) ((BasicMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType());
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToIdMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((IdMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((TemporalConverter) ((IdMapping) persistentAttribute.getMapping()).getConverter()).getTemporalType());
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(IdAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToEmbeddedMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof EmbeddedMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(EmbeddedAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToTransientMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof TransientMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(TransientAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToEmbeddedIdMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof EmbeddedIdMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(EmbeddedIdAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToOneToOneMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof OneToOneMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(OneToOneAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToOneToManyMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof OneToManyMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(OneToManyAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToManyToOneMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ManyToOneMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ManyToOneAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToManyToManyMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ManyToManyMapping);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ManyToManyAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testMorphToElementCollectionMapping() throws Exception {
createTestEntityWithVersionMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().getAttributes().iterator().next();
VersionMapping versionMapping = (VersionMapping) persistentAttribute.getMapping();
JavaResourceType resourceType = (JavaResourceType) getJpaProject().getJavaResourceType(FULLY_QUALIFIED_TYPE_NAME, Kind.TYPE);
JavaResourceField resourceField = resourceType.getFields().iterator().next();
assertFalse(versionMapping.isDefault());
versionMapping.getColumn().setSpecifiedName("FOO");
versionMapping.setConverter(TemporalConverter.class);
((TemporalConverter) versionMapping.getConverter()).setTemporalType(TemporalType.TIME);
resourceField.addAnnotation(Access2_0Annotation.ANNOTATION_NAME);
assertFalse(versionMapping.isDefault());
persistentAttribute.setMappingKey(MappingKeys2_0.ELEMENT_COLLECTION_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ElementCollectionMapping2_0);
assertNull(resourceField.getAnnotation(VersionAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ElementCollection2_0Annotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(Access2_0Annotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(ColumnAnnotation.ANNOTATION_NAME));
assertNotNull(resourceField.getAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
}