blob: f62d1dc65a9f9c2b4b2cbe6b36a0fadc6b5139d8 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 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.resource.java.JPA;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.eclipselink.core.context.ChangeTracking;
import org.eclipse.jpt.eclipselink.core.context.ChangeTrackingType;
import org.eclipse.jpt.eclipselink.core.context.Customizer;
import org.eclipse.jpt.eclipselink.core.context.EclipseLinkEmbeddable;
import org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingAnnotation;
import org.eclipse.jpt.eclipselink.core.resource.java.CustomizerAnnotation;
import org.eclipse.jpt.eclipselink.core.resource.java.EclipseLinkJPA;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
@SuppressWarnings("nls")
public class EclipseLinkJavaEmbeddableTests extends EclipseLinkJavaContextModelTestCase
{
private void createCustomizerAnnotation() throws Exception {
this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "Customizer", "Class value();");
}
private void createChangeTrackingAnnotation() throws Exception{
createChangeTrackingTypeEnum();
this.createAnnotationAndMembers(EclipseLinkJPA.PACKAGE, "ChangeTracking", "ChangeTrackingType value() default ChangeTrackingType.AUTO");
}
private void createChangeTrackingTypeEnum() throws Exception {
this.createEnumAndMembers(ECLIPSELINK_ANNOTATIONS_PACKAGE_NAME, "ChangeTrackingType", "ATTRIBUTE, OBJECT, DEFERRED, AUTO;");
}
private ICompilationUnit createTestEmbeddableWithConvertAndCustomizerClass() throws Exception {
createCustomizerAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.EMBEDDABLE, EclipseLinkJPA.CUSTOMIZER);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Embeddable").append(CR);
sb.append(" @Customizer(Foo.class");
}
});
}
private ICompilationUnit createTestEmbeddableWithChangeTracking() throws Exception {
createChangeTrackingAnnotation();
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.EMBEDDABLE, EclipseLinkJPA.CHANGE_TRACKING);
}
@Override
public void appendTypeAnnotationTo(StringBuilder sb) {
sb.append("@Embeddable").append(CR);
sb.append(" @ChangeTracking").append(CR);
}
});
}
public EclipseLinkJavaEmbeddableTests(String name) {
super(name);
}
public void testGetCustomizerClass() throws Exception {
createTestEmbeddableWithConvertAndCustomizerClass();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
Customizer customizer = ((EclipseLinkEmbeddable) javaPersistentType().getMapping()).getCustomizer();
assertEquals("Foo", customizer.getSpecifiedCustomizerClass());
}
public void testSetCustomizerClass() throws Exception {
createTestEmbeddableWithConvertAndCustomizerClass();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
Customizer customizer = ((EclipseLinkEmbeddable) javaPersistentType().getMapping()).getCustomizer();
assertEquals("Foo", customizer.getSpecifiedCustomizerClass());
customizer.setSpecifiedCustomizerClass("Bar");
assertEquals("Bar", customizer.getSpecifiedCustomizerClass());
JavaResourcePersistentType typeResource = jpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
CustomizerAnnotation customizerAnnotation = (CustomizerAnnotation) typeResource.getSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
assertEquals("Bar", customizerAnnotation.getValue());
customizer.setSpecifiedCustomizerClass(null);
assertEquals(null, customizer.getSpecifiedCustomizerClass());
customizerAnnotation = (CustomizerAnnotation) typeResource.getSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
assertEquals(null, customizerAnnotation);
customizer.setSpecifiedCustomizerClass("Bar");
assertEquals("Bar", customizer.getSpecifiedCustomizerClass());
customizerAnnotation = (CustomizerAnnotation) typeResource.getSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
assertEquals("Bar", customizerAnnotation.getValue());
}
public void testGetCustomizerClassUpdatesFromResourceModelChange() throws Exception {
createTestEmbeddableWithConvertAndCustomizerClass();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
EclipseLinkEmbeddable embeddable = (EclipseLinkEmbeddable) javaPersistentType().getMapping();
Customizer customizer = embeddable.getCustomizer();
assertEquals("Foo", customizer.getSpecifiedCustomizerClass());
JavaResourcePersistentType typeResource = jpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
CustomizerAnnotation customizerAnnotation = (CustomizerAnnotation) typeResource.getSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
customizerAnnotation.setValue("Bar");
assertEquals("Bar", customizer.getSpecifiedCustomizerClass());
typeResource.removeSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
assertEquals(null, customizer.getSpecifiedCustomizerClass());
customizerAnnotation = (CustomizerAnnotation) typeResource.addSupportingAnnotation(CustomizerAnnotation.ANNOTATION_NAME);
assertEquals(null, customizer.getSpecifiedCustomizerClass());
customizerAnnotation.setValue("FooBar");
assertEquals("FooBar", customizer.getSpecifiedCustomizerClass());
}
public void testGetChangeTracking() throws Exception {
createTestEmbeddableWithChangeTracking();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
EclipseLinkEmbeddable embeddable = (EclipseLinkEmbeddable) javaPersistentType().getMapping();
ChangeTracking contextChangeTracking = embeddable.getChangeTracking();
JavaResourcePersistentType typeResource = jpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
ChangeTrackingAnnotation resourceChangeTracking = (ChangeTrackingAnnotation) typeResource.getSupportingAnnotation(ChangeTrackingAnnotation.ANNOTATION_NAME);
// base annotated, test context value
assertNull(resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
// change resource to ATTRIBUTE specifically, test context
resourceChangeTracking.setValue(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.ATTRIBUTE);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.ATTRIBUTE, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.ATTRIBUTE, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.ATTRIBUTE, contextChangeTracking.getSpecifiedType());
// change resource to OBJECT specifically, test context
resourceChangeTracking.setValue(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.OBJECT);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.OBJECT, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.OBJECT, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.OBJECT, contextChangeTracking.getSpecifiedType());
// change resource to DEFERRED specifically, test context
resourceChangeTracking.setValue(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.DEFERRED);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.DEFERRED, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.DEFERRED, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.DEFERRED, contextChangeTracking.getSpecifiedType());
// change resource to AUTO specifically, test context
resourceChangeTracking.setValue(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.AUTO);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.AUTO, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
// remove value from resource, test context
resourceChangeTracking.setValue(null);
assertNull(resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
// remove annotation, text context
typeResource.removeSupportingAnnotation(ChangeTrackingAnnotation.ANNOTATION_NAME);
assertNull(resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getType());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getDefaultType());
assertNull(contextChangeTracking.getSpecifiedType());
}
public void testSetChangeTracking() throws Exception {
createTestEmbeddableWithChangeTracking();
addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
EclipseLinkEmbeddable embeddable = (EclipseLinkEmbeddable) javaPersistentType().getMapping();
ChangeTracking contextChangeTracking = embeddable.getChangeTracking();
JavaResourcePersistentType typeResource = jpaProject().getJavaResourcePersistentType(FULLY_QUALIFIED_TYPE_NAME);
ChangeTrackingAnnotation resourceChangeTracking = (ChangeTrackingAnnotation) typeResource.getSupportingAnnotation(ChangeTrackingAnnotation.ANNOTATION_NAME);
// base annotated, test resource value
assertNull(resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
// change context to AUTO specifically, test resource
contextChangeTracking.setSpecifiedType(ChangeTrackingType.AUTO);
assertNull(resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
// change context to ATTRIBUTE specifically, test resource
contextChangeTracking.setSpecifiedType(ChangeTrackingType.ATTRIBUTE);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.ATTRIBUTE, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.ATTRIBUTE, contextChangeTracking.getSpecifiedType());
// change context to OBJECT specifically, test resource
contextChangeTracking.setSpecifiedType(ChangeTrackingType.OBJECT);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.OBJECT, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.OBJECT, contextChangeTracking.getSpecifiedType());
// change context to DEFERRED specifically, test resource
contextChangeTracking.setSpecifiedType(ChangeTrackingType.DEFERRED);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.DEFERRED, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.DEFERRED, contextChangeTracking.getSpecifiedType());
// change context to null, test resource
contextChangeTracking.setSpecifiedType(null);
assertNull(typeResource.getSupportingAnnotation(ChangeTrackingAnnotation.ANNOTATION_NAME));
assertNull(contextChangeTracking.getSpecifiedType());
// change context to AUTO specifically (this time from no annotation), test resource
contextChangeTracking.setSpecifiedType(ChangeTrackingType.AUTO);
resourceChangeTracking = (ChangeTrackingAnnotation) typeResource.getSupportingAnnotation(ChangeTrackingAnnotation.ANNOTATION_NAME);
assertEquals(org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingType.AUTO, resourceChangeTracking.getValue());
assertEquals(ChangeTrackingType.AUTO, contextChangeTracking.getSpecifiedType());
}
}