blob: aeef312a7f74430727c87215d1b4bdf6860e6e45 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2008 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.eclipselink.core.tests.internal.context.java;
import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.core.context.BasicMapping;
import org.eclipse.jpt.core.context.Converter;
import org.eclipse.jpt.core.context.PersistentAttribute;
import org.eclipse.jpt.core.context.TemporalConverter;
import org.eclipse.jpt.core.context.TemporalType;
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.TemporalAnnotation;
import org.eclipse.jpt.eclipselink.core.context.EclipseLinkBasicMapping;
import org.eclipse.jpt.eclipselink.core.context.Convert;
import org.eclipse.jpt.eclipselink.core.context.Mutable;
import org.eclipse.jpt.eclipselink.core.internal.context.persistence.EclipseLinkPersistenceUnit;
import org.eclipse.jpt.eclipselink.core.resource.java.ConvertAnnotation;
import org.eclipse.jpt.eclipselink.core.resource.java.EclipseLinkJPA;
import org.eclipse.jpt.eclipselink.core.resource.java.MutableAnnotation;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
@SuppressWarnings("nls")
public class EclipseLinkJavaBasicMappingTests extends EclipseLinkJavaContextModelTestCase
{
private void createConvertAnnotation() throws Exception{
this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "Convert", "String value() default \"none\";");
}
private void createMutableAnnotation() throws Exception{
this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "Mutable", "boolean value() default true");
}
private ICompilationUnit createTestEntityWithBasicMapping() throws Exception {
createConvertAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Basic").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithConvert() throws Exception {
createConvertAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, EclipseLinkJPA.CONVERT);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Convert(\"class-instance\")").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithMutableBasic() throws Exception {
createMutableAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, EclipseLinkJPA.MUTABLE);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Basic").append(CR);
sb.append("@Mutable").append(CR);
}
});
}
private ICompilationUnit createTestEntityWithMutableBasicDate() throws Exception {
createMutableAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, EclipseLinkJPA.MUTABLE, "java.util.Date");
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Entity").append(CR);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Basic").append(CR);
sb.append(" @Mutable").append(CR);
sb.append(" private Date myDate;").append(CR);
sb.append(CR);
sb.append(" ");
}
});
}
public EclipseLinkJavaBasicMappingTests(String name) {
super(name);
}
public void testGetConvert() throws Exception {
createTestEntityWithBasicMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
attributeResource.addSupportingAnnotation(ConvertAnnotation.ANNOTATION_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
BasicMapping basicMapping = (BasicMapping) persistentAttribute.getSpecifiedMapping();
assertEquals(Convert.ECLIPSE_LINK_CONVERTER, basicMapping.getConverter().getType());
}
public void testGetConvert2() throws Exception {
createTestEntityWithConvert();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
BasicMapping basicMapping = (BasicMapping) persistentAttribute.getMapping();
assertEquals(Convert.ECLIPSE_LINK_CONVERTER, basicMapping.getConverter().getType());
assertEquals(Convert.CLASS_INSTANCE_CONVERTER, ((Convert) basicMapping.getConverter()).getConverterName());
}
public void testSetConvert() throws Exception {
createTestEntityWithBasicMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
BasicMapping basicMapping = (BasicMapping) persistentAttribute.getSpecifiedMapping();
assertEquals(Converter.NO_CONVERTER, basicMapping.getConverter().getType());
basicMapping.setSpecifiedConverter(Converter.TEMPORAL_CONVERTER);
((TemporalConverter) basicMapping.getSpecifiedConverter()).setTemporalType(TemporalType.TIME);
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
TemporalAnnotation temporal = (TemporalAnnotation) attributeResource.getSupportingAnnotation(TemporalAnnotation.ANNOTATION_NAME);
assertEquals(org.eclipse.jpt.core.resource.java.TemporalType.TIME, temporal.getValue());
basicMapping.setSpecifiedConverter(null);
assertNull(attributeResource.getSupportingAnnotation(TemporalAnnotation.ANNOTATION_NAME));
}
public void testGetConvertUpdatesFromResourceModelChange() throws Exception {
createTestEntityWithBasicMapping();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
BasicMapping basicMapping = (BasicMapping) persistentAttribute.getSpecifiedMapping();
assertEquals(Converter.NO_CONVERTER, basicMapping.getConverter().getType());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
ConvertAnnotation convert = (ConvertAnnotation) attributeResource.addSupportingAnnotation(ConvertAnnotation.ANNOTATION_NAME);
convert.setValue("foo");
assertEquals(Convert.ECLIPSE_LINK_CONVERTER, basicMapping.getConverter().getType());
assertEquals("foo", ((Convert) basicMapping.getConverter()).getConverterName());
attributeResource.removeSupportingAnnotation(ConvertAnnotation.ANNOTATION_NAME);
assertEquals(Converter.NO_CONVERTER, basicMapping.getConverter().getType());
assertFalse(basicMapping.isDefault());
assertSame(basicMapping, persistentAttribute.getSpecifiedMapping());
}
public void testGetSpecifiedMutable() throws Exception {
createTestEntityWithMutableBasic();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
EclipseLinkBasicMapping basicMapping = (EclipseLinkBasicMapping) persistentAttribute.getSpecifiedMapping();
Mutable mutable = basicMapping.getMutable();
assertEquals(Boolean.TRUE, mutable.getSpecifiedMutable());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
MutableAnnotation mutableAnnotation = (MutableAnnotation) attributeResource.getSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
mutableAnnotation.setValue(Boolean.TRUE);
assertEquals(Boolean.TRUE, mutable.getSpecifiedMutable());
mutableAnnotation.setValue(null);
assertEquals(Boolean.TRUE, mutable.getSpecifiedMutable());
mutableAnnotation.setValue(Boolean.FALSE);
assertEquals(Boolean.FALSE, mutable.getSpecifiedMutable());
attributeResource.removeSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertEquals(null, mutable.getSpecifiedMutable());
attributeResource.addSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertEquals(Boolean.TRUE, mutable.getSpecifiedMutable());
}
public void testSetSpecifiedMutable() throws Exception {
createTestEntityWithMutableBasic();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
EclipseLinkBasicMapping basicMapping = (EclipseLinkBasicMapping) persistentAttribute.getSpecifiedMapping();
Mutable mutable = basicMapping.getMutable();
assertEquals(Boolean.TRUE, mutable.getSpecifiedMutable());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
MutableAnnotation mutableAnnotation = (MutableAnnotation) attributeResource.getSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertEquals(null, mutableAnnotation.getValue());
mutable.setSpecifiedMutable(Boolean.TRUE);
assertEquals(null, mutableAnnotation.getValue());
mutable.setSpecifiedMutable(null);
mutableAnnotation = (MutableAnnotation) attributeResource.getSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertEquals(null, mutableAnnotation);
mutable.setSpecifiedMutable(Boolean.FALSE);
mutableAnnotation = (MutableAnnotation) attributeResource.getSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertEquals(Boolean.FALSE, mutableAnnotation.getValue());
mutable.setSpecifiedMutable(Boolean.TRUE);
assertEquals(null, mutableAnnotation.getValue());
}
public void testIsDefaultMutable() throws Exception {
createTestEntityWithMutableBasic();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
EclipseLinkBasicMapping basicMapping = (EclipseLinkBasicMapping) persistentAttribute.getSpecifiedMapping();
Mutable mutable = basicMapping.getMutable();
assertTrue(mutable.isDefaultMutable());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
attributeResource.removeSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertTrue(mutable.isDefaultMutable());
mutable.setSpecifiedMutable(Boolean.FALSE);
assertTrue(mutable.isDefaultMutable());
//set mutable default to false in the persistence unit properties, verify default in java still true since this is not a Date/Calendar
((EclipseLinkPersistenceUnit) getPersistenceUnit()).getOptions().setTemporalMutable(Boolean.FALSE);
assertTrue(mutable.isDefaultMutable());
}
public void testIsDefaultMutableForDate() throws Exception {
createTestEntityWithMutableBasicDate();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
EclipseLinkBasicMapping basicMapping = (EclipseLinkBasicMapping) persistentAttribute.getSpecifiedMapping();
Mutable mutable = basicMapping.getMutable();
assertFalse(mutable.isDefaultMutable());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
attributeResource.removeSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertFalse(mutable.isDefaultMutable());
mutable.setSpecifiedMutable(Boolean.TRUE);
assertFalse(mutable.isDefaultMutable());
//set mutable default to false in the persistence unit properties, verify default in java still true since this is not a Date/Calendar
((EclipseLinkPersistenceUnit) getPersistenceUnit()).getOptions().setTemporalMutable(Boolean.TRUE);
assertTrue(mutable.isDefaultMutable());
((EclipseLinkPersistenceUnit) getPersistenceUnit()).getOptions().setTemporalMutable(Boolean.FALSE);
assertFalse(mutable.isDefaultMutable());
((EclipseLinkPersistenceUnit) getPersistenceUnit()).getOptions().setTemporalMutable(null);
assertFalse(mutable.isDefaultMutable());
}
public void testIsMutable() throws Exception {
createTestEntityWithMutableBasic();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
PersistentAttribute persistentAttribute = getJavaPersistentType().attributes().next();
EclipseLinkBasicMapping basicMapping = (EclipseLinkBasicMapping) persistentAttribute.getSpecifiedMapping();
Mutable mutable = basicMapping.getMutable();
assertTrue(mutable.isMutable());
JavaResourcePersistentType typeResource = getJpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
JavaResourcePersistentAttribute attributeResource = typeResource.persistableAttributes().next();
attributeResource.removeSupportingAnnotation(MutableAnnotation.ANNOTATION_NAME);
assertTrue(mutable.isMutable());
mutable.setSpecifiedMutable(Boolean.TRUE);
assertTrue(mutable.isMutable());
}
}