blob: 222d4a535b30b387b8ba70a128372ea56ac1cdec [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008, 2013 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0, which accompanies this distribution
* and is available at https://www.eclipse.org/legal/epl-2.0/.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.jpa.eclipselink.core.tests.internal.resource.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.utility.internal.iterable.EmptyIterable;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;
import org.eclipse.jpt.jpa.core.resource.java.FetchType;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLink;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.TransformationAnnotation;
@SuppressWarnings("nls")
public class TransformationAnnotationTests extends EclipseLinkJavaResourceModelTestCase {
public TransformationAnnotationTests(String name) {
super(name);
}
private ICompilationUnit createTestTransformation() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return IteratorTools.iterator(EclipseLink.TRANSFORMATION);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Transformation");
}
});
}
private ICompilationUnit createTestTransformationWithOptional() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return IteratorTools.iterator(EclipseLink.TRANSFORMATION);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Transformation(optional = true)");
}
});
}
private ICompilationUnit createTestTransformationWithFetch() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return IteratorTools.iterator(EclipseLink.TRANSFORMATION, JPA.FETCH_TYPE);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@Transformation(fetch = FetchType.EAGER)");
}
});
}
public void testTransformationAnnotation() throws Exception {
ICompilationUnit cu = this.createTestTransformation();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
assertNotNull(resourceField.getAnnotation(EclipseLink.TRANSFORMATION));
resourceField.setPrimaryAnnotation(null, EmptyIterable.<String>instance());
assertNull(resourceField.getAnnotation(EclipseLink.TRANSFORMATION));
resourceField.setPrimaryAnnotation(EclipseLink.TRANSFORMATION, EmptyIterable.<String>instance());
assertNotNull(resourceField.getAnnotation(EclipseLink.TRANSFORMATION));
}
public void testGetOptional() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithOptional();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(Boolean.TRUE, transformation.getOptional());
}
public void testSetOptional() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithOptional();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(Boolean.TRUE, transformation.getOptional());
transformation.setOptional(Boolean.FALSE);
assertEquals(Boolean.FALSE, transformation.getOptional());
assertSourceContains("@Transformation(optional = false)", cu);
}
public void testSetOptionalNull() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithOptional();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(Boolean.TRUE, transformation.getOptional());
transformation.setOptional(null);
assertNull(transformation.getOptional());
assertSourceContains("@Transformation", cu);
assertSourceDoesNotContain("optional", cu);
}
public void testGetFetch() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithFetch();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(FetchType.EAGER, transformation.getFetch());
}
public void testSetFetch() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithFetch();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(FetchType.EAGER, transformation.getFetch());
transformation.setFetch(FetchType.LAZY);
assertEquals(FetchType.LAZY, transformation.getFetch());
assertSourceContains("@Transformation(fetch = LAZY)", cu);
}
public void testSetFetchNull() throws Exception {
ICompilationUnit cu = this.createTestTransformationWithFetch();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = IterableTools.get(resourceType.getFields(), 0);
TransformationAnnotation transformation = (TransformationAnnotation) resourceField.getAnnotation(EclipseLink.TRANSFORMATION);
assertEquals(FetchType.EAGER, transformation.getFetch());
transformation.setFetch(null);
assertNull(transformation.getFetch());
assertSourceContains("@Transformation", cu);
assertSourceDoesNotContain("fetch", cu);
}
}