blob: 8c568a50fe03fece456b06c32ad7a758d11b5a07 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2011 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.jpa.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.JavaResourceMember;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.resource.java.NestableAnnotation;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.core.resource.java.JoinColumnAnnotation;
@SuppressWarnings("nls")
public class JoinColumnsTests extends JpaJavaResourceModelTestCase {
private static final String COLUMN_NAME = "MY_COLUMN";
private static final String COLUMN_TABLE = "MY_TABLE";
private static final String COLUMN_COLUMN_DEFINITION = "COLUMN_DEFINITION";
private static final String COLUMN_REFERENCED_COLUMN_NAME = "MY_REF_COLUMN_NAME";
public JoinColumnsTests(String name) {
super(name);
}
private ICompilationUnit createTestJoinColumns() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn)");
}
});
}
private ICompilationUnit createTestJoinColumnWithName() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn(name = \"" + COLUMN_NAME + "\"))");
}
});
}
private ICompilationUnit createTestJoinColumnWithTable() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn(table = \"" + COLUMN_TABLE + "\"))");
}
});
}
private ICompilationUnit createTestJoinColumnWithReferencedColumnName() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn(referencedColumnName = \"" + COLUMN_REFERENCED_COLUMN_NAME + "\"))");
}
});
}
private ICompilationUnit createTestJoinColumnWithColumnDefinition() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn(columnDefinition = \"" + COLUMN_COLUMN_DEFINITION + "\"))");
}
});
}
private ICompilationUnit createTestJoinColumnWithBooleanElement(final String booleanElement) throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumns(@JoinColumn(" + booleanElement + " = true))");
}
});
}
private ICompilationUnit createTestJoinColumn() throws Exception {
return this.createTestType(new DefaultAnnotationWriter() {
@Override
public Iterator<String> imports() {
return new ArrayIterator<String>(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS);
}
@Override
public void appendIdFieldAnnotationTo(StringBuilder sb) {
sb.append("@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\")");
}
});
}
private JoinColumnAnnotation joinColumnAt(JavaResourceMember resourceMember, int index) {
return (JoinColumnAnnotation) CollectionTools.get(resourceMember.getAnnotations(JPA.JOIN_COLUMN), index);
}
public void testGetName() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithName();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertEquals(COLUMN_NAME, column.getName());
}
public void testGetNull() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getName());
assertNull(column.getNullable());
assertNull(column.getInsertable());
assertNull(column.getUnique());
assertNull(column.getUpdatable());
assertNull(column.getTable());
assertNull(column.getColumnDefinition());
}
public void testSetName() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getName());
column.setName("Foo");
assertEquals("Foo", column.getName());
assertSourceContains("@JoinColumns(@JoinColumn(name = \"Foo\"))", cu);
}
public void testSetNameNull() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithName();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertEquals(COLUMN_NAME, column.getName());
column.setName(null);
assertNull(column.getName());
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetTable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithTable();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertEquals(COLUMN_TABLE, column.getTable());
}
public void testSetTable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getTable());
column.setTable("Foo");
assertEquals("Foo", column.getTable());
assertSourceContains("@JoinColumns(@JoinColumn(table = \"Foo\"))", cu);
column.setTable(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetReferencedColumnName() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithReferencedColumnName();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertEquals(COLUMN_REFERENCED_COLUMN_NAME, column.getReferencedColumnName());
}
public void testSetReferencedColumnName() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getReferencedColumnName());
column.setReferencedColumnName("Foo");
assertEquals("Foo", column.getReferencedColumnName());
assertSourceContains("@JoinColumns(@JoinColumn(referencedColumnName = \"Foo\"))", cu);
column.setReferencedColumnName(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetColumnDefinition() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithColumnDefinition();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition());
}
public void testSetColumnDefinition() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getColumnDefinition());
column.setColumnDefinition("Foo");
assertEquals("Foo", column.getColumnDefinition());
assertSourceContains("@JoinColumns(@JoinColumn(columnDefinition = \"Foo\"))", cu);
column.setColumnDefinition(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetUnique() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithBooleanElement("unique");
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertTrue(column.getUnique().booleanValue());
}
public void testSetUnique() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getUnique());
column.setUnique(Boolean.FALSE);
assertFalse(column.getUnique().booleanValue());
assertSourceContains("@JoinColumns(@JoinColumn(unique = false))", cu);
column.setUnique(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetNullable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithBooleanElement("nullable");
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertTrue(column.getNullable().booleanValue());
}
public void testSetNullable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getNullable());
column.setNullable(Boolean.FALSE);
assertFalse(column.getNullable().booleanValue());
assertSourceContains("@JoinColumns(@JoinColumn(nullable = false))", cu);
column.setNullable(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetInsertable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithBooleanElement("insertable");
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertTrue(column.getInsertable().booleanValue());
}
public void testSetInsertable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getInsertable());
column.setInsertable(Boolean.FALSE);
assertFalse(column.getInsertable().booleanValue());
assertSourceContains("@JoinColumns(@JoinColumn(insertable = false))", cu);
column.setInsertable(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testGetUpdatable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumnWithBooleanElement("updatable");
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertTrue(column.getUpdatable().booleanValue());
}
public void testSetUpdatable() throws Exception {
ICompilationUnit cu = this.createTestJoinColumns();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
JoinColumnAnnotation column = this.joinColumnAt(resourceField, 0);
assertNotNull(column);
assertNull(column.getUpdatable());
column.setUpdatable(Boolean.FALSE);
assertFalse(column.getUpdatable().booleanValue());
assertSourceContains("@JoinColumns(@JoinColumn(updatable = false))", cu);
column.setUpdatable(null);
assertSourceDoesNotContain("@JoinColumn(", cu);
}
public void testAddJoinColumnCopyExisting() throws Exception {
ICompilationUnit cu = createTestJoinColumn();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
String expected1 = "@JoinColumns({";
String expected2 = "@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"),";
String expected3 = "@JoinColumn(name = \"FOO\") })";
JoinColumnAnnotation joinColumn = (JoinColumnAnnotation) resourceField.addAnnotation(1, JPA.JOIN_COLUMN);
joinColumn.setName("FOO");
assertSourceContains(expected1, cu);
assertSourceContains(expected2, cu);
assertSourceContains(expected3, cu);
assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN));
assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN));
assertEquals(2, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN));
}
public void testAddJoinColumnToBeginningOfList() throws Exception {
ICompilationUnit cu = createTestJoinColumn();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
String expected1 = "@JoinColumns({";
String expected2 = "@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"),";
String expected3 = "@JoinColumn(name = \"FOO\") })";
JoinColumnAnnotation joinColumn = (JoinColumnAnnotation) resourceField.addAnnotation(1, JPA.JOIN_COLUMN);
joinColumn.setName("FOO");
assertSourceContains(expected1, cu);
assertSourceContains(expected2, cu);
assertSourceContains(expected3, cu);
expected2 = "@JoinColumn(name = \"BAZ\"),";
expected3 = "@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn(name = \"FOO\") })";
joinColumn = (JoinColumnAnnotation) resourceField.addAnnotation(0, JPA.JOIN_COLUMN);
joinColumn.setName("BAZ");
assertSourceContains(expected1, cu);
assertSourceContains(expected2, cu);
assertSourceContains(expected3, cu);
Iterator<NestableAnnotation> joinColumns = resourceField.getAnnotations(JPA.JOIN_COLUMN).iterator();
assertEquals("BAZ", ((JoinColumnAnnotation) joinColumns.next()).getName());
assertEquals("BAR", ((JoinColumnAnnotation) joinColumns.next()).getName());
assertEquals("FOO", ((JoinColumnAnnotation) joinColumns.next()).getName());
assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN));
assertNotNull(resourceField.getAnnotation(0, JPA.JOIN_COLUMN));
assertEquals(3, resourceField.getAnnotationsSize(JPA.JOIN_COLUMN));
}
public void testRemoveJoinColumnCopyExisting() throws Exception {
ICompilationUnit cu = createTestJoinColumn();
JavaResourceType resourceType = buildJavaResourceType(cu);
JavaResourceField resourceField = CollectionTools.get(resourceType.getFields(), 0);
String expected1 = "@JoinColumns({";
String expected2 = "@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"),";
String expected3 = "@JoinColumn(name = \"FOO\") })";
JoinColumnAnnotation joinColumn = (JoinColumnAnnotation) resourceField.addAnnotation(1, JPA.JOIN_COLUMN);
joinColumn.setName("FOO");
assertSourceContains(expected1, cu);
assertSourceContains(expected2, cu);
assertSourceContains(expected3, cu);
expected1 = "@JoinColumn(name = \"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\")";
resourceField.removeAnnotation(1, JPA.JOIN_COLUMN);
assertSourceContains(expected1, cu);
assertSourceDoesNotContain("@JoinColumns", cu);
}
}