blob: d0b46a02a36b29a53a2c30811b311a317c10feda [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007 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.context.java;
import java.util.Iterator;
import org.eclipse.jdt.core.IType;
import org.eclipse.jpt.core.internal.IMappingKeys;
import org.eclipse.jpt.core.internal.context.base.IBasicMapping;
import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping;
import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping;
import org.eclipse.jpt.core.internal.context.base.IIdMapping;
import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping;
import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping;
import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping;
import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping;
import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute;
import org.eclipse.jpt.core.internal.context.base.ITransientMapping;
import org.eclipse.jpt.core.internal.context.base.IVersionMapping;
import org.eclipse.jpt.core.internal.context.base.TemporalType;
import org.eclipse.jpt.core.internal.resource.java.Basic;
import org.eclipse.jpt.core.internal.resource.java.Column;
import org.eclipse.jpt.core.internal.resource.java.Embedded;
import org.eclipse.jpt.core.internal.resource.java.EmbeddedId;
import org.eclipse.jpt.core.internal.resource.java.GeneratedValue;
import org.eclipse.jpt.core.internal.resource.java.Id;
import org.eclipse.jpt.core.internal.resource.java.JPA;
import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource;
import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource;
import org.eclipse.jpt.core.internal.resource.java.ManyToMany;
import org.eclipse.jpt.core.internal.resource.java.ManyToOne;
import org.eclipse.jpt.core.internal.resource.java.OneToMany;
import org.eclipse.jpt.core.internal.resource.java.OneToOne;
import org.eclipse.jpt.core.internal.resource.java.SequenceGenerator;
import org.eclipse.jpt.core.internal.resource.java.TableGenerator;
import org.eclipse.jpt.core.internal.resource.java.Temporal;
import org.eclipse.jpt.core.internal.resource.java.Transient;
import org.eclipse.jpt.core.internal.resource.java.Version;
import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
public class JavaIdMappingTests extends ContextModelTestCase
{
private void createEntityAnnotation() throws Exception{
this.createAnnotationAndMembers("Entity", "String name() default \"\";");
}
private void createIdAnnotation() throws Exception{
this.createAnnotationAndMembers("Id", "");
}
private void createGeneratedValueAnnotation() throws Exception{
this.createAnnotationAndMembers("GeneratedValue", "");
}
private void createTemporalAnnotation() throws Exception{
this.createAnnotationAndMembers("Temporal", "TemporalType value();");
}
private IType createTestEntityWithIdMapping() throws Exception {
createEntityAnnotation();
createIdAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ID);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Id").append(CR);
}
});
}
private IType createTestEntityWithTemporal() throws Exception {
createEntityAnnotation();
createTemporalAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.TEMPORAL, JPA.TEMPORAL_TYPE);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Id").append(CR);
sb.append("@Temporal(TemporalType.TIMESTAMP)").append(CR);
}
});
}
private IType createTestEntityWithIdMappingGeneratedValue() throws Exception {
createEntityAnnotation();
createIdAnnotation();
createGeneratedValueAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.GENERATED_VALUE);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Id").append(CR);
sb.append("@GeneratedValue").append(CR);
}
});
}
public JavaIdMappingTests(String name) {
super(name);
}
public void testMorphToBasicMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToDefault() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToVersionMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY);
assertEquals("FOO", ((IVersionMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName());
assertEquals(TemporalType.TIME, ((IVersionMapping) persistentAttribute.getMapping()).getTemporal());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToEmbeddedMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToTransientMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToEmbeddedIdMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToOneToOneMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToOneToManyMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToManyToOneMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testMorphToManyToManyMapping() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping();
assertFalse(idMapping.isDefault());
idMapping.getColumn().setSpecifiedName("FOO");
idMapping.setTemporal(TemporalType.TIME);
idMapping.addGeneratedValue();
idMapping.addTableGenerator();
idMapping.addSequenceGenerator();
assertFalse(idMapping.isDefault());
persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY);
assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME));
assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Column.ANNOTATION_NAME));
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME));
assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME));
}
public void testGetTemporal() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getTemporal());
}
public void testGetTemporal2() throws Exception {
createTestEntityWithTemporal();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertEquals(TemporalType.TIMESTAMP, idMapping.getTemporal());
}
public void testSetTemporal() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getTemporal());
idMapping.setTemporal(TemporalType.TIME);
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
Temporal temporal = (Temporal) attributeResource.annotation(Temporal.ANNOTATION_NAME);
assertEquals(org.eclipse.jpt.core.internal.resource.java.TemporalType.TIME, temporal.getValue());
idMapping.setTemporal(null);
assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME));
}
public void testGetTemporalUpdatesFromResourceModelChange() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getTemporal());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
Temporal temporal = (Temporal) attributeResource.addAnnotation(Temporal.ANNOTATION_NAME);
temporal.setValue(org.eclipse.jpt.core.internal.resource.java.TemporalType.DATE);
assertEquals(TemporalType.DATE, idMapping.getTemporal());
attributeResource.removeAnnotation(Temporal.ANNOTATION_NAME);
assertNull(idMapping.getTemporal());
assertFalse(idMapping.isDefault());
assertSame(idMapping, persistentAttribute.getSpecifiedMapping());
}
public void testGetColumn() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getColumn().getSpecifiedName());
assertEquals("id", idMapping.getColumn().getName());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN);
column.setName("foo");
assertEquals("foo", idMapping.getColumn().getSpecifiedName());
assertEquals("foo", idMapping.getColumn().getName());
assertEquals("id", idMapping.getColumn().getDefaultName());
}
public void testGetSequenceGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getSequenceGenerator());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.SEQUENCE_GENERATOR);
assertNotNull(idMapping.getSequenceGenerator());
assertEquals(1, CollectionTools.size(attributeResource.annotations()));
}
public void testAddSequenceGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getSequenceGenerator());
idMapping.addSequenceGenerator();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNotNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR));
assertNotNull(idMapping.getSequenceGenerator());
//try adding another sequence generator, should get an IllegalStateException
try {
idMapping.addSequenceGenerator();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
public void testRemoveSequenceGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.SEQUENCE_GENERATOR);
idMapping.removeSequenceGenerator();
assertNull(idMapping.getSequenceGenerator());
assertNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR));
//try removing the sequence generator again, should get an IllegalStateException
try {
idMapping.removeSequenceGenerator();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
public void testGetTableGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getTableGenerator());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.TABLE_GENERATOR);
assertNotNull(idMapping.getTableGenerator());
assertEquals(1, CollectionTools.size(attributeResource.annotations()));
}
public void testAddTableGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getTableGenerator());
idMapping.addTableGenerator();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNotNull(attributeResource.annotation(JPA.TABLE_GENERATOR));
assertNotNull(idMapping.getTableGenerator());
//try adding another table generator, should get an IllegalStateException
try {
idMapping.addTableGenerator();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
public void testRemoveTableGenerator() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.TABLE_GENERATOR);
idMapping.removeTableGenerator();
assertNull(idMapping.getTableGenerator());
assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR));
//try removing the table generator again, should get an IllegalStateException
try {
idMapping.removeTableGenerator();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
public void testGetGeneratedValue() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getGeneratedValue());
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.GENERATED_VALUE);
assertNotNull(idMapping.getGeneratedValue());
assertEquals(1, CollectionTools.size(attributeResource.annotations()));
}
public void testGetGeneratedValue2() throws Exception {
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
createTestEntityWithIdMappingGeneratedValue();
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNotNull(idMapping.getGeneratedValue());
assertEquals(1, CollectionTools.size(attributeResource.annotations()));
}
public void testAddGeneratedValue() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
assertNull(idMapping.getGeneratedValue());
idMapping.addGeneratedValue();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
assertNotNull(attributeResource.annotation(JPA.GENERATED_VALUE));
assertNotNull(idMapping.getGeneratedValue());
//try adding another generated value, should get an IllegalStateException
try {
idMapping.addGeneratedValue();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
public void testRemoveGeneratedValue() throws Exception {
createTestEntityWithIdMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping();
JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaPersistentAttributeResource attributeResource = typeResource.attributes().next();
attributeResource.addAnnotation(JPA.GENERATED_VALUE);
idMapping.removeGeneratedValue();
assertNull(idMapping.getGeneratedValue());
assertNull(attributeResource.annotation(JPA.GENERATED_VALUE));
//try removing the generatedValue again, should get an IllegalStateException
try {
idMapping.removeGeneratedValue();
} catch (IllegalStateException e) {
return;
}
fail("IllegalStateException not thrown");
}
}