blob: 17f39f6873568d86fcb1846067bf0721cbbd760a [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.MappingKeys;
import org.eclipse.jpt.core.context.PersistentAttribute;
import org.eclipse.jpt.core.context.java.JavaBasicMapping;
import org.eclipse.jpt.core.context.java.JavaEmbeddedMapping;
import org.eclipse.jpt.core.context.java.JavaIdMapping;
import org.eclipse.jpt.core.resource.java.BasicAnnotation;
import org.eclipse.jpt.core.resource.java.EmbeddedAnnotation;
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.tests.internal.context.ContextModelTestCase;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
public class JavaPersistentAttributeTests extends ContextModelTestCase
{
private void createEntityAnnotation() throws Exception{
this.createAnnotationAndMembers("Entity", "String name() default \"\";");
}
private IType createTestEntityAnnotatedField() throws Exception {
createEntityAnnotation();
this.createAnnotationAndMembers("Id", "");
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");
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Id");
}
});
}
private IType createTestEntityAnnotatedMethod() throws Exception {
createEntityAnnotation();
this.createAnnotationAndMembers("Id", "");
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");
}
@Override
public void appendGetIdMethodAnnotationTo(StringBuilder sb) {
sb.append("@Id");
}
});
}
public JavaPersistentAttributeTests(String name) {
super(name);
}
public void testGetName() throws Exception {
createTestType();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals("id", persistentAttribute.getName());
}
public void testGetMapping() throws Exception {
createTestEntityAnnotatedMethod();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertTrue(persistentAttribute.getMapping() instanceof JavaIdMapping);
persistentAttribute.setSpecifiedMappingKey(null);
assertTrue(persistentAttribute.getMapping() instanceof JavaBasicMapping);
}
public void testGetSpecifiedMapping() throws Exception {
createTestEntityAnnotatedMethod();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertTrue(persistentAttribute.getSpecifiedMapping() instanceof JavaIdMapping);
persistentAttribute.setSpecifiedMappingKey(null);
assertNull(persistentAttribute.getSpecifiedMapping());
}
public void testGetSpecifiedMappingNull() throws Exception {
createTestType();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertNull(persistentAttribute.getSpecifiedMapping());
assertNotNull(persistentAttribute.getMapping());
}
public void testMappingKey() throws Exception {
createTestEntityAnnotatedMethod();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
persistentAttribute.setSpecifiedMappingKey(null);
assertEquals(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
}
public void testDefaultMappingKey() throws Exception {
createTestEntityAnnotatedMethod();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
assertEquals(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getDefaultMappingKey());
}
public void testSetSpecifiedMappingKey() throws Exception {
createTestType();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertNull(persistentAttribute.getSpecifiedMapping());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY);
JavaResourcePersistentType typeResource = jpaProject().getJavaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.attributes().next();
assertNotNull(attributeResource.getMappingAnnotation());
assertTrue(attributeResource.getMappingAnnotation() instanceof EmbeddedAnnotation);
assertEquals(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
assertTrue(persistentAttribute.getSpecifiedMapping() instanceof JavaEmbeddedMapping);
}
public void testSetSpecifiedMappingKey2() throws Exception {
createTestEntityAnnotatedField();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY);
JavaResourcePersistentType typeResource = jpaProject().getJavaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.attributes().next();
assertNotNull(attributeResource.getMappingAnnotation());
assertTrue(attributeResource.getMappingAnnotation() instanceof EmbeddedAnnotation);
assertEquals(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
assertTrue(persistentAttribute.getSpecifiedMapping() instanceof JavaEmbeddedMapping);
}
public void testSetSpecifiedMappingKeyNull() throws Exception {
createTestEntityAnnotatedMethod();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
persistentAttribute.setSpecifiedMappingKey(MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY);
JavaResourcePersistentType typeResource = jpaProject().getJavaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.attributes().next();
assertNull(attributeResource.getMappingAnnotation());
assertNull(attributeResource.getMappingAnnotation(IdAnnotation.ANNOTATION_NAME));
assertNull(persistentAttribute.getSpecifiedMapping());
}
public void testGetMappingKeyMappingChangeInResourceModel() throws Exception {
createTestEntityAnnotatedField();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertEquals(MappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
JavaResourcePersistentType typeResource = jpaProject().getJavaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.attributes().next();
attributeResource.setMappingAnnotation(EmbeddedAnnotation.ANNOTATION_NAME);
assertEquals(MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getMappingKey());
}
public void testGetMappingKeyMappingChangeInResourceModel2() throws Exception {
createTestType();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = javaPersistentType().attributes().next();
assertNull(persistentAttribute.getSpecifiedMapping());
JavaResourcePersistentType typeResource = jpaProject().getJavaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.attributes().next();
attributeResource.setMappingAnnotation(BasicAnnotation.ANNOTATION_NAME);
assertEquals(MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getSpecifiedMapping().getKey());
}
}