blob: de13a3ce307d9a11eb4c530d05c72e5e292cbe0c [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011, 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.jaxb.eclipselink.core.tests.internal.resource.java;
import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAttribute;
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.iterator.IteratorTools;
import org.eclipse.jpt.jaxb.eclipselink.core.resource.java.ELJaxb;
import org.eclipse.jpt.jaxb.eclipselink.core.resource.java.XmlInverseReferenceAnnotation;
@SuppressWarnings("nls")
public class XmlInverseReferenceAnnotationTests
extends ELJaxbJavaResourceModelTestCase {
public XmlInverseReferenceAnnotationTests(String name) {
super(name);
}
private ICompilationUnit createTestXmlInverseReference()
throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return IteratorTools.iterator(ELJaxb.XML_INVERSE_REFERENCE);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@XmlInverseReference");
}
});
}
private ICompilationUnit createTestXmlInverseReferenceWithMappedBy()
throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return IteratorTools.iterator(ELJaxb.XML_INVERSE_REFERENCE);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@XmlInverseReference(mappedBy = \"foo\")");
}
});
}
private XmlInverseReferenceAnnotation getXmlInverseReferenceAnnotation(JavaResourceAttribute resourceAttribute) {
return (XmlInverseReferenceAnnotation) resourceAttribute.getAnnotation(ELJaxb.XML_INVERSE_REFERENCE);
}
public void testGetNull() throws Exception {
ICompilationUnit cu = createTestXmlInverseReference();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceAttribute = getField(resourceType, 0);
XmlInverseReferenceAnnotation annotation = getXmlInverseReferenceAnnotation(resourceAttribute);
assertTrue(annotation != null);
assertNull(annotation.getMappedBy());
}
public void testGetMappedBy() throws Exception {
ICompilationUnit cu = createTestXmlInverseReferenceWithMappedBy();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceAttribute = getField(resourceType, 0);
XmlInverseReferenceAnnotation annotation = getXmlInverseReferenceAnnotation(resourceAttribute);
assertEquals("foo", annotation.getMappedBy());
}
public void testSetMappedBy() throws Exception {
ICompilationUnit cu = createTestXmlInverseReference();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceAttribute = getField(resourceType, 0);
XmlInverseReferenceAnnotation annotation = getXmlInverseReferenceAnnotation(resourceAttribute);
assertNull(annotation.getMappedBy());
annotation.setMappedBy("bar");
assertEquals("bar", annotation.getMappedBy());
assertSourceContains("@XmlInverseReference(mappedBy = \"bar\")", cu);
annotation.setMappedBy("");
assertEquals("", annotation.getMappedBy());
assertSourceContains("@XmlInverseReference(mappedBy = \"\")", cu);
annotation.setMappedBy(null);
assertNull(annotation.getMappedBy());
assertSourceContains("@XmlInverseReference", cu);
assertSourceDoesNotContain("mappedBy", cu);
}
}