diff options
author | tle | 2008-02-03 16:40:54 +0000 |
---|---|---|
committer | tle | 2008-02-03 16:40:54 +0000 |
commit | 7f7b20a5d66bf69e88734ead5036e4d6664aa381 (patch) | |
tree | 97dacecfda81e183389ab1c0adb3a83b953823ad /jpa/tests/org.eclipse.jpt.core.tests | |
parent | 145c144380950bd4b49fd4597a1a0695cf7c8b7b (diff) | |
download | webtools.dali-7f7b20a5d66bf69e88734ead5036e4d6664aa381.tar.gz webtools.dali-7f7b20a5d66bf69e88734ead5036e4d6664aa381.tar.xz webtools.dali-7f7b20a5d66bf69e88734ead5036e4d6664aa381.zip |
Merged jpt_2_0_exp branch into Head.v20080202_Merged
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.core.tests')
139 files changed, 46912 insertions, 479 deletions
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/.settings/org.eclipse.core.resources.prefs b/jpa/tests/org.eclipse.jpt.core.tests/.settings/org.eclipse.core.resources.prefs index 88638b5d88..eb348782fa 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/.settings/org.eclipse.core.resources.prefs +++ b/jpa/tests/org.eclipse.jpt.core.tests/.settings/org.eclipse.core.resources.prefs @@ -1,3 +1,3 @@ -#Sun May 27 15:10:20 EDT 2007 +#Tue Jan 15 11:10:55 EST 2008 eclipse.preferences.version=1 encoding/<project>=ISO-8859-1 diff --git a/jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF b/jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF index c9ccb4ebfb..48f02cedcb 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF +++ b/jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF @@ -10,19 +10,30 @@ Require-Bundle: org.eclipse.core.resources, org.eclipse.emf.ecore, org.eclipse.emf.ecore.xmi, org.eclipse.jdt.core, + org.eclipse.jdt.ui, org.eclipse.jem, + org.eclipse.jem.util, org.eclipse.jpt.core, - org.eclipse.jpt.core.tests.extension.resource, + org.eclipse.jpt.db, org.eclipse.jpt.utility, + org.eclipse.jpt.utility.tests, org.eclipse.wst.common.emf, + org.eclipse.wst.common.emfworkbench.integration, org.eclipse.wst.common.frameworks, org.eclipse.wst.common.modulecore, org.eclipse.wst.common.project.facet.core, - org.junit + org.junit, + org.eclipse.wst.xml.core, + org.eclipse.wst.validation Bundle-RequiredExecutionEnvironment: J2SE-1.5 Export-Package: org.eclipse.jpt.core.tests.internal;x-friends:="org.eclipse.jpt.ui.tests", - org.eclipse.jpt.core.tests.internal.content.java.mappings;x-internal:=true, + org.eclipse.jpt.core.tests.internal.context;x-internal:=true, + org.eclipse.jpt.core.tests.internal.context.java;x-internal:=true, + org.eclipse.jpt.core.tests.internal.context.orm;x-internal:=true, + org.eclipse.jpt.core.tests.internal.context.persistence;x-internal:=true, org.eclipse.jpt.core.tests.internal.jdtutility;x-internal:=true, org.eclipse.jpt.core.tests.internal.model;x-internal:=true, org.eclipse.jpt.core.tests.internal.platform;x-internal:=true, - org.eclipse.jpt.core.tests.internal.projects;x-friends:="org.eclipse.jpt.ui.tests" + org.eclipse.jpt.core.tests.internal.projects;x-friends:="org.eclipse.jpt.ui.tests", + org.eclipse.jpt.core.tests.internal.resource;x-internal:=true, + org.eclipse.jpt.core.tests.internal.resource.java;x-internal:=true diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JptCoreTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JptCoreTests.java index fbd57c9799..c1c7c27159 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JptCoreTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JptCoreTests.java @@ -9,13 +9,12 @@ ******************************************************************************/ package org.eclipse.jpt.core.tests.internal; -import org.eclipse.jpt.core.tests.internal.content.java.mappings.JptCoreContentJavaMappingsTests; -import org.eclipse.jpt.core.tests.internal.jdtutility.JptCoreJdtUtilityTests; -import org.eclipse.jpt.core.tests.internal.model.JptCoreModelTests; -import org.eclipse.jpt.core.tests.internal.platform.JptCorePlatformTests; - import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.jpt.core.tests.internal.context.JptCoreContextModelTests; +import org.eclipse.jpt.core.tests.internal.jdtutility.JptCoreJdtUtilityTests; +import org.eclipse.jpt.core.tests.internal.model.JptCoreModelTests; +import org.eclipse.jpt.core.tests.internal.resource.JptCoreResourceModelTests; /** * Runs MOST JPT Core Tests. Currently we do not have a jpa.jar checked into cvs. @@ -32,10 +31,10 @@ public class JptCoreTests { public static Test suite(boolean all) { String quantity = all ? "All" : "Most"; TestSuite suite = new TestSuite(quantity + " JPT Core Tests"); - suite.addTest(JptCoreContentJavaMappingsTests.suite(all)); suite.addTest(JptCoreJdtUtilityTests.suite(all)); suite.addTest(JptCoreModelTests.suite(all)); - suite.addTest(JptCorePlatformTests.suite(all)); + suite.addTest(JptCoreResourceModelTests.suite(all)); + suite.addTest(JptCoreContextModelTests.suite(all)); return suite; } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JavaEntityTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JavaEntityTests.java deleted file mode 100644 index bbb313fc29..0000000000 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JavaEntityTests.java +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 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.core.tests.internal.content.java.mappings; - -import java.util.Iterator; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.jpt.core.internal.IMappingKeys; -import org.eclipse.jpt.core.internal.content.java.IJavaTypeMapping; -import org.eclipse.jpt.core.internal.content.java.mappings.JPA; -import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; - -public class JavaEntityTests extends JpaJavaTestCase { - - public JavaEntityTests(String name) { - super(name); - } - - private void createTestEntity1() throws CoreException { - this.createTestType(new DefaultAnnotationWriter() { - @Override - public Iterator<String> imports() { - return new ArrayIterator<String>(JPA.ENTITY); - } - @Override - public void appendTypeAnnotationTo(StringBuilder sb) { - sb.append("@Entity"); - } - }); - this.synchPersistenceXml(); - } - -// public void testXXX() throws Exception { -// this.createTestEntity1(); -// this.assertSourceDoesNotContain("@Id"); -// -// IWorkbench wb = PlatformUI.getWorkbench(); -// IWorkbenchWindow[] windows = wb.getWorkbenchWindows(); -// IWorkbenchWindow window = windows[0]; -// IWorkbenchPage[] pages = window.getPages(); -// IWorkbenchPage page = pages[0]; -// IFile file = (IFile) this.javaProject.getProject().findMember(FILE_PATH); -// IDE.openEditor(page, file); -// TestThread t = new TestThread(); -// t.start(); -// -//// while (t.isAlive()) { -//// Thread.sleep(50); -//// } -//// assertFalse("see console", t.exceptionCaught); -// } - -// private class TestThread extends Thread { -// boolean exceptionCaught = false; -// TestThread() { -// super(); -// } -// @Override -// public void run() { -// try { -// JavaEntityTests.this.xxx(); -// } catch (Exception ex) { -// this.exceptionCaught = true; -// throw new RuntimeException(ex); -// } -// } -// } -// -// void xxx() throws Exception { -// this.jpaProject().getJpaProject().setThreadLocalModifySharedDocumentCommandExecutor(SynchronousUiCommandExecutor.instance()); -// JavaPersistentAttribute attribute = this.javaPersistentAttributeNamed("id"); -// attribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); -// this.assertSourceContains("@Id"); -// } -// -// // TODO move to JavaPersistentAttributeTests -// public void testSetSpecifiedMappingKey() throws Exception { -// this.createTestEntity1(); -// this.assertSourceDoesNotContain("@Id"); -// JavaPersistentAttribute attribute = this.javaPersistentAttributeNamed("id"); -// attribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); -// this.assertSourceContains("@Id"); -// } -// - public void testGetName() throws Exception { - this.createTestEntity1(); - IJavaTypeMapping typeMapping = this.javaPersistentTypeNamed(FULLY_QUALIFIED_TYPE_NAME).getMapping(); - assertEquals(TYPE_NAME, typeMapping.getName()); - } - - public void testGetKey() throws Exception { - this.createTestEntity1(); - IJavaTypeMapping typeMapping = this.javaPersistentTypeNamed(FULLY_QUALIFIED_TYPE_NAME).getMapping(); - assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, typeMapping.getKey()); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaJavaTestCase.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaJavaTestCase.java deleted file mode 100644 index bfeccb381b..0000000000 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaJavaTestCase.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 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.core.tests.internal.content.java.mappings; - -import java.util.Iterator; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.jpt.core.internal.content.java.JavaPersistentAttribute; -import org.eclipse.jpt.core.internal.content.java.JavaPersistentType; -import org.eclipse.jpt.core.internal.jdtutility.Type; -import org.eclipse.jpt.core.internal.synch.SynchronizeClassesJob; -import org.eclipse.jpt.core.tests.internal.jdtutility.AnnotationTestCase; -import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject; -import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; -import org.eclipse.jpt.core.tests.internal.projects.TestPlatformProject; -import org.eclipse.jpt.utility.internal.ClassTools; - -public abstract class JpaJavaTestCase extends AnnotationTestCase { - - public JpaJavaTestCase(String name) { - super(name); - } - - @Override - protected TestJavaProject buildJavaProject(String projectName, boolean autoBuild) throws Exception { - return new TestJpaProject(TestPlatformProject.uniqueProjectName(projectName), autoBuild); - } - - protected TestJpaProject jpaProject() { - return (TestJpaProject) this.javaProject; - } - - protected JavaPersistentType javaPersistentTypeNamed(String typeName) { - for (Iterator<JavaPersistentType> stream = this.jpaProject().getJpaProject().javaPersistentTypes(); stream.hasNext(); ) { - JavaPersistentType jpt = stream.next(); - if (jpt.fullyQualifiedTypeName().equals(typeName)) { - return jpt; - } - } - throw new IllegalArgumentException("missing type: " + typeName); - } - - protected JavaPersistentAttribute javaPersistentAttributeNamed(String attributeName) { - return this.javaPersistentAttributeNamed(attributeName, FULLY_QUALIFIED_TYPE_NAME); - } - - protected JavaPersistentAttribute javaPersistentAttributeNamed(String attributeName, String typeName) { - for (JavaPersistentAttribute attribute : this.javaPersistentTypeNamed(typeName).getAttributes()) { - if (attribute.getName().equals(attributeName)) { - return attribute; - } - } - throw new IllegalArgumentException("missing attribute: " + typeName + "." + attributeName); - } - - protected Type typeNamed(String typeName) { - return this.javaPersistentTypeNamed(typeName).getType(); - } - - protected void synchPersistenceXml() { - SynchronizeClassesJob job = new SynchronizeClassesJob(this.jpaProject().getProject().getFile("src/META-INF/persistence.xml")); - ClassTools.executeMethod(job, "run", IProgressMonitor.class, new NullProgressMonitor()); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JptCoreContentJavaMappingsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JptCoreContentJavaMappingsTests.java deleted file mode 100644 index 5efc901f07..0000000000 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JptCoreContentJavaMappingsTests.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 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.core.tests.internal.content.java.mappings; - -import junit.framework.Test; -import junit.framework.TestSuite; - -public class JptCoreContentJavaMappingsTests { - - public static Test suite() { - return suite(true); - } - - public static Test suite(boolean all) { - TestSuite suite = new TestSuite(JptCoreContentJavaMappingsTests.class.getPackage().getName()); - if (all) { - suite.addTestSuite(JavaEntityTests.class); - } - return suite; - } - - private JptCoreContentJavaMappingsTests() { - super(); - throw new UnsupportedOperationException(); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/ContextModelTestCase.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/ContextModelTestCase.java new file mode 100644 index 0000000000..cecb1d0997 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/ContextModelTestCase.java @@ -0,0 +1,192 @@ +/******************************************************************************* + * 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.core.tests.internal.context; + +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IJpaProject; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.IBaseJpaContent; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.IPersistenceUnit; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.context.orm.EntityMappings; +import org.eclipse.jpt.core.internal.resource.orm.OrmArtifactEdit; +import org.eclipse.jpt.core.internal.resource.orm.OrmResource; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceArtifactEdit; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceResource; +import org.eclipse.jpt.core.internal.resource.persistence.XmlJavaClassRef; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistence; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnit; +import org.eclipse.jpt.core.tests.internal.jdtutility.AnnotationTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject; +import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; +import org.eclipse.wst.common.internal.emfworkbench.WorkbenchResourceHelper; + +public abstract class ContextModelTestCase extends AnnotationTestCase +{ + protected static final String BASE_PROJECT_NAME = "ContextModelTestProject"; + + protected PersistenceArtifactEdit persistenceArtifactEdit; + protected OrmArtifactEdit ormArtifactEdit; + + + protected ContextModelTestCase(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.persistenceArtifactEdit = PersistenceArtifactEdit.getArtifactEditForWrite(getJavaProject().getProject()); + this.ormArtifactEdit = OrmArtifactEdit.getArtifactEditForWrite(getJavaProject().getProject()); + waitForWorkspaceJobs(); + } + + @Override + protected void deleteAllProjects() throws Exception{ + //don't delete projects, creating a new one with a new name + //workspace will be deleted next time tests are run. + //not saying this is the ultimate solution, but it will work for now + //until we can figure out how to properly delete projects in tests + } + + @Override + protected void tearDown() throws Exception { + //at least delete the project from the workspace since, deleting from the file system doesn't work well. + //tests run too slow otherwise because so many projects are created in the workspace + this.persistenceArtifactEdit.dispose(); + this.persistenceArtifactEdit = null; + this.ormArtifactEdit.dispose(); + this.ormArtifactEdit = null; + getJavaProject().getProject().delete(false, true, null); + super.tearDown(); + } + + @Override + protected TestJavaProject buildJavaProject(boolean autoBuild) throws Exception { + return buildJpaProject(BASE_PROJECT_NAME, autoBuild); + } + + protected TestJpaProject buildJpaProject(String projectName, boolean autoBuild) + throws Exception { + return TestJpaProject.buildJpaProject(projectName, autoBuild); + } + + protected IJpaProject jpaProject() { + return getJavaProject().getJpaProject(); + } + + protected void waitForWorkspaceJobs() { + // This job will not finish running until the workspace jobs are done + Job waitJob = new Job("Wait job") { + @Override + protected IStatus run(IProgressMonitor monitor) { + return Status.OK_STATUS; + } + }; + waitJob.setRule(ResourcesPlugin.getWorkspace().getRoot()); + waitJob.schedule(); + try { + waitJob.join(); + } catch (InterruptedException ex) { + // the job thread was interrupted during a wait - ignore + } + } + + protected PersistenceResource persistenceResource() { + return this.persistenceArtifactEdit.getResource(); + } + + protected OrmResource ormResource() { + return this.ormArtifactEdit.getResource(JptCorePlugin.ormXmlDeploymentURI(jpaProject().project())); + } + + protected XmlPersistence xmlPersistence() { + return persistenceResource().getPersistence(); + } + + protected EntityMappings entityMappings() { + return persistenceUnit().mappingFileRefs().next().getOrmXml().getEntityMappings(); + } + + protected XmlPersistenceUnit xmlPersistenceUnit() { + return persistenceResource().getPersistence().getPersistenceUnits().get(0); + } + + protected IPersistenceUnit persistenceUnit() { + return jpaContent().getPersistenceXml().getPersistence().persistenceUnits().next(); + } + + protected IClassRef classRef() { + return persistenceUnit().classRefs().next(); + } + + protected IJavaPersistentType javaPersistentType() { + return classRef().getJavaPersistentType(); + } + + protected IEntity javaEntity() { + return (IEntity) javaPersistentType().getMapping(); + } + + protected void addXmlClassRef(String className) { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass(className); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + } + + protected void removeXmlClassRef(String className) { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + XmlJavaClassRef xmlJavaClassRefToRemove = null; + for (XmlJavaClassRef xmlJavaClassRef : xmlPersistenceUnit.getClasses()) { + if (xmlJavaClassRef.getJavaClass().equals(className)) { + xmlJavaClassRefToRemove = xmlJavaClassRef; + } + } + if (xmlJavaClassRefToRemove == null) { + throw new IllegalArgumentException(); + } + xmlPersistenceUnit.getClasses().remove(xmlJavaClassRefToRemove); + } + + protected IBaseJpaContent jpaContent() { + return (IBaseJpaContent) getJavaProject().getJpaProject().contextModel(); + } + + @Override + protected TestJpaProject getJavaProject() { + return (TestJpaProject) super.getJavaProject(); + } + + protected void deleteResource(Resource resource) throws CoreException { + System.gc(); + WorkbenchResourceHelper.deleteResource(resource); + waitForWorkspaceJobs(); + } + + protected IType createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception { + return this.javaProject.createType("javax.persistence", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }"); + } + + protected IType createEnumAndMembers(String enumName, String enumBody) throws Exception { + return this.javaProject.createType("javax.persistence", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/JptCoreContextModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/JptCoreContextModelTests.java new file mode 100644 index 0000000000..25ed426183 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/JptCoreContextModelTests.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.eclipse.jpt.core.tests.internal.context.java.JptCoreContextJavaModelTests; +import org.eclipse.jpt.core.tests.internal.context.orm.JptCoreOrmContextModelTests; +import org.eclipse.jpt.core.tests.internal.context.persistence.JptCorePersistenceContextModelTests; + +public class JptCoreContextModelTests extends TestCase +{ + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptCoreContextModelTests.class.getName()); + suite.addTest(JptCorePersistenceContextModelTests.suite(all)); + suite.addTest(JptCoreOrmContextModelTests.suite(all)); + suite.addTest(JptCoreContextJavaModelTests.suite(all)); + return suite; + } + + private JptCoreContextModelTests() { + super(); + throw new UnsupportedOperationException(); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAssociationOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAssociationOverrideTests.java new file mode 100644 index 0000000000..7cad910aed --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAssociationOverrideTests.java @@ -0,0 +1,179 @@ +/******************************************************************************* + * 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IAssociationOverride; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverride; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaAssociationOverrideTests extends ContextModelTestCase +{ + private static final String ASSOCIATION_OVERRIDE_NAME = "MY_ASSOCIATION_OVERRIDE_NAME"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createMappedSuperclassAnnotation() throws Exception{ + this.createAnnotationAndMembers("MappedSuperclass", ""); + } + + private void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", + "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + } + + private void createAssociationOverrideAnnotation() throws Exception { + createJoinColumnAnnotation(); + this.createAnnotationAndMembers("AssociationOverride", + "String name();" + + "JoinColumn[] joinColumns();"); + } + + private IType createTestMappedSuperclass() throws Exception { + createMappedSuperclassAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS, JPA.ONE_TO_ONE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(" @OneToOne"); + sb.append(CR); + sb.append(" private int address;").append(CR); + sb.append(CR); + sb.append(" "); + } + }); + } + + private IType createTestEntityWithAssociationOverride() throws Exception { + createEntityAnnotation(); + createAssociationOverrideAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ASSOCIATION_OVERRIDE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\")"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + + + public JavaAssociationOverrideTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestEntityWithAssociationOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAssociationOverride specifiedAssociationOverride = javaEntity().specifiedAssociationOverrides().next(); + assertEquals(ASSOCIATION_OVERRIDE_NAME, specifiedAssociationOverride.getName()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AssociationOverride associationOverrideResource = (AssociationOverride) typeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + associationOverrideResource.setName("FOO"); + specifiedAssociationOverride = javaEntity().specifiedAssociationOverrides().next(); + assertEquals("FOO", specifiedAssociationOverride.getName()); + } + + public void testSetName() throws Exception { + createTestEntityWithAssociationOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAssociationOverride specifiedAssociationOverride = javaEntity().specifiedAssociationOverrides().next(); + assertEquals(ASSOCIATION_OVERRIDE_NAME, specifiedAssociationOverride.getName()); + + specifiedAssociationOverride.setName("FOO"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AssociationOverride associationOverrideResource = (AssociationOverride) typeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + assertEquals("FOO", associationOverrideResource.getName()); + } + + public void testDefaultName() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(1, CollectionTools.size(entity.defaultAssociationOverrides())); + + IAssociationOverride associationOverride = entity.defaultAssociationOverrides().next(); + assertEquals("address", associationOverride.getName()); + } + + public void testIsVirtual() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(1, CollectionTools.size(entity.defaultAssociationOverrides())); + + IAssociationOverride associationOverride = entity.defaultAssociationOverrides().next(); + assertTrue(associationOverride.isVirtual()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAttributeOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAttributeOverrideTests.java new file mode 100644 index 0000000000..c9db451c0b --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaAttributeOverrideTests.java @@ -0,0 +1,262 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IAttributeOverride; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IColumn; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaAttributeOverrideTests extends ContextModelTestCase +{ + private static final String ATTRIBUTE_OVERRIDE_NAME = "MY_ATTRIBUTE_OVERRIDE_NAME"; + private static final String ATTRIBUTE_OVERRIDE_COLUMN_NAME = "MY_ATTRIBUTE_OVERRIDE_COLUMN_NAME"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createMappedSuperclassAnnotation() throws Exception{ + this.createAnnotationAndMembers("MappedSuperclass", ""); + } + + private void createColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private void createAttributeOverrideAnnotation() throws Exception { + createColumnAnnotation(); + this.createAnnotationAndMembers("AttributeOverride", + "String name();" + + "Column column();"); + } + + private IType createTestMappedSuperclass() throws Exception { + createMappedSuperclassAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + } + }); + } + + private IType createTestEntityWithAttributeOverride() throws Exception { + createEntityAnnotation(); + createAttributeOverrideAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + + + public JavaAttributeOverrideTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestEntityWithAttributeOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAttributeOverride specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, specifiedAttributeOverride.getName()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AttributeOverride attributeOverrideResource = (AttributeOverride) typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + + attributeOverrideResource.setName("FOO"); + specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + assertEquals("FOO", specifiedAttributeOverride.getName()); + } + + public void testSetName() throws Exception { + createTestEntityWithAttributeOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAttributeOverride specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, specifiedAttributeOverride.getName()); + + specifiedAttributeOverride.setName("FOO"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AttributeOverride attributeOverrideResource = (AttributeOverride) typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + + assertEquals("FOO", attributeOverrideResource.getName()); + } + + public void testColumnGetName() throws Exception { + createTestEntityWithAttributeOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAttributeOverride specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + IColumn column = specifiedAttributeOverride.getColumn(); + assertEquals(ATTRIBUTE_OVERRIDE_COLUMN_NAME, column.getName()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AttributeOverride attributeOverrideResource = (AttributeOverride) typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column columnResource = attributeOverrideResource.getColumn(); + columnResource.setName("FOO"); + + + column = javaEntity().specifiedAttributeOverrides().next().getColumn(); + assertEquals("FOO", column.getName()); + } + + public void testColumnSetName() throws Exception { + createTestEntityWithAttributeOverride(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IAttributeOverride specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + IColumn column = specifiedAttributeOverride.getColumn(); + assertEquals(ATTRIBUTE_OVERRIDE_COLUMN_NAME, column.getName()); + + column.setSpecifiedName("FOO"); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + AttributeOverride attributeOverrideResource = (AttributeOverride) typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column columnResource = attributeOverrideResource.getColumn(); + + assertEquals("FOO", columnResource.getName()); + + column.setSpecifiedName(null); + + attributeOverrideResource = (AttributeOverride) typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + assertNull(attributeOverrideResource.getColumn()); + assertNotNull(specifiedAttributeOverride.getColumn()); + } + + public void testColumnDefaultName() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(2, CollectionTools.size(entity.defaultAttributeOverrides())); + + IAttributeOverride attributeOverride = entity.defaultAttributeOverrides().next(); + assertEquals("id", attributeOverride.getColumn().getDefaultName()); + + + IJavaPersistentType mappedSuperclass = CollectionTools.list(persistenceUnit().classRefs()).get(1).getJavaPersistentType(); + IBasicMapping basicMapping = (IBasicMapping) mappedSuperclass.attributeNamed("id").getMapping(); + basicMapping.getColumn().setSpecifiedName("FOO"); + + attributeOverride = entity.defaultAttributeOverrides().next(); + assertEquals("FOO", attributeOverride.getColumn().getDefaultName()); + } + + public void testColumnDefaultTableName() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(2, CollectionTools.size(entity.defaultAttributeOverrides())); + + IAttributeOverride attributeOverride = entity.defaultAttributeOverrides().next(); + assertEquals("AnnotationTestTypeChild", attributeOverride.getColumn().getDefaultTable()); + + + IJavaPersistentType mappedSuperclass = CollectionTools.list(persistenceUnit().classRefs()).get(1).getJavaPersistentType(); + IBasicMapping basicMapping = (IBasicMapping) mappedSuperclass.attributeNamed("id").getMapping(); + basicMapping.getColumn().setSpecifiedTable("BAR"); + + attributeOverride = entity.defaultAttributeOverrides().next(); + assertEquals("BAR", attributeOverride.getColumn().getDefaultTable()); + } + + public void testDefaultName() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(2, CollectionTools.size(entity.defaultAttributeOverrides())); + + IAttributeOverride attributeOverride = entity.defaultAttributeOverrides().next(); + assertEquals("id", attributeOverride.getName()); + } + + public void testIsVirtual() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + assertEquals("AnnotationTestTypeChild", entity.getName()); + assertEquals(2, CollectionTools.size(entity.defaultAttributeOverrides())); + + IAttributeOverride attributeOverride = entity.defaultAttributeOverrides().next(); + assertTrue(attributeOverride.isVirtual()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaBasicMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaBasicMappingTests.java new file mode 100644 index 0000000000..6eef141ea9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaBasicMappingTests.java @@ -0,0 +1,1000 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.EnumType; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Enumerated; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.Lob; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Temporal; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaBasicMappingTests extends ContextModelTestCase +{ + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createBasicAnnotation() throws Exception{ + this.createAnnotationAndMembers("Basic", "FetchType fetch() default EAGER; boolean optional() default true;"); + } + + private void createLobAnnotation() throws Exception{ + this.createAnnotationAndMembers("Lob", ""); + } + + private void createEnumeratedAnnotation() throws Exception{ + this.createAnnotationAndMembers("Enumerated", "EnumType value() default ORDINAL;"); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithBasicMapping() throws Exception { + createEntityAnnotation(); + createBasicAnnotation(); + + 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 IType createTestEntityWithBasicMappingFetchOptionalSpecified() throws Exception { + createEntityAnnotation(); + createBasicAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, JPA.FETCH_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic(fetch=FetchType.EAGER, optional=false)").append(CR); + } + }); + } + + private IType createTestEntityWithLob() throws Exception { + createEntityAnnotation(); + createLobAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.LOB); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Lob").append(CR); + } + }); + } + + private IType createTestEntityWithEnumerated() throws Exception { + createEntityAnnotation(); + createEnumeratedAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ENUMERATED, JPA.ENUM_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Enumerated(EnumType.STRING)").append(CR); + } + }); + } + + private IType createTestEntityWithTemporal() throws Exception { + createEntityAnnotation(); + createTemporalAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TEMPORAL, JPA.TEMPORAL_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Temporal(TemporalType.TIMESTAMP)").append(CR); + } + }); + } + + public JavaBasicMappingTests(String name) { + super(name); + } + + public void testDefaultBasicGetDefaultFetch() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertEquals(FetchType.EAGER, basicMapping.getDefaultFetch()); + } + + public void testSpecifiedBasicGetDefaultFetch() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertEquals(FetchType.EAGER, basicMapping.getDefaultFetch()); + } + + public void testGetFetch() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(FetchType.EAGER, basicMapping.getFetch()); + + basicMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, basicMapping.getFetch()); + } + + public void testGetSpecifiedFetch() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getSpecifiedFetch()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + basic.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY); + + assertEquals(FetchType.LAZY, basicMapping.getSpecifiedFetch()); + } + + public void testGetSpecifiedFetch2() throws Exception { + createTestEntityWithBasicMappingFetchOptionalSpecified(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(FetchType.EAGER, basicMapping.getSpecifiedFetch()); + } + + public void testSetSpecifiedFetch() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(basicMapping.getSpecifiedFetch()); + + basicMapping.setSpecifiedFetch(FetchType.LAZY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, basic.getFetch()); + + basicMapping.setSpecifiedFetch(null); + assertNotNull(attributeResource.mappingAnnotation(JPA.BASIC)); + } + + public void testSetSpecifiedFetch2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + createOrmXmlFile(); + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertNull(basicMapping.getSpecifiedFetch()); + assertTrue(basicMapping.isDefault()); + + basicMapping.setSpecifiedFetch(FetchType.LAZY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.properties().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, basic.getFetch()); + + basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertEquals(FetchType.LAZY, basicMapping.getSpecifiedFetch()); + assertFalse(basicMapping.isDefault()); + + basicMapping.setSpecifiedFetch(null); + assertNotNull(attributeResource.mappingAnnotation(JPA.BASIC)); + + basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + } + + protected void createOrmXmlFile() throws Exception { + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testSetBasicRemovedFromResourceModel() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertFalse(basicMapping.isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.setMappingAnnotation(null); + + assertNotSame(basicMapping, persistentAttribute.getMapping()); + + basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertTrue(basicMapping.isDefault()); + assertEquals("FOO", basicMapping.getColumn().getSpecifiedName()); + + + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToDefaultBasic() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setLob(true); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal()); + assertTrue(((IBasicMapping) persistentAttribute.getMapping()).isLob()); + assertEquals(EnumType.ORDINAL, ((IBasicMapping) persistentAttribute.getMapping()).getEnumerated()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(((IBasicMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); + assertNull(((IBasicMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToId() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setLob(true); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IIdMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IIdMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToVersion() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IVersionMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IVersionMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToEmbedded() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToEmbeddedId() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToTransient() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToOneToOne() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); +//TODO assertEquals(FetchType.EAGER, ((IOneToOneMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertEquals(Boolean.FALSE, ((IOneToOneMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToOneToMany() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); +//TODO assertEquals(FetchType.EAGER, ((IOneToManyMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + public void testBasicMorphToManyToOne() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); +//TODO assertEquals(FetchType.EAGER, ((IManyToOneMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertEquals(Boolean.FALSE, ((IManyToOneMapping) persistentAttribute.getMapping()).getSpecifiedOptional()); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testBasicMorphToManyToMany() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + basicMapping.getColumn().setSpecifiedName("FOO"); + basicMapping.setTemporal(TemporalType.TIME); + basicMapping.setLob(true); + basicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + basicMapping.setSpecifiedFetch(FetchType.EAGER); + basicMapping.setSpecifiedOptional(Boolean.FALSE); + assertFalse(basicMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); +//TODO assertEquals(FetchType.EAGER, ((IManyToManyMapping) persistentAttribute.getMapping()).getSpecifiedFetch()); +// assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testDefaultBasicGetDefaultOptional() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertEquals(Boolean.TRUE, basicMapping.getDefaultOptional()); + } + + public void testSpecifiedBasicGetDefaultOptional() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertEquals(Boolean.TRUE, basicMapping.getDefaultOptional()); + } + + public void testGetOptional() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(Boolean.TRUE, basicMapping.getOptional()); + + basicMapping.setSpecifiedOptional(basicMapping.getOptional()); + assertEquals(Boolean.TRUE, basicMapping.getOptional()); + } + + public void testGetSpecifiedOptional() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getSpecifiedOptional()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + basic.setOptional(Boolean.FALSE); + + assertEquals(Boolean.FALSE, basicMapping.getSpecifiedOptional()); + } + + public void testGetSpecifiedOptional2() throws Exception { + createTestEntityWithBasicMappingFetchOptionalSpecified(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(Boolean.FALSE, basicMapping.getSpecifiedOptional()); + } + + public void testSetSpecifiedOptional() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(basicMapping.getSpecifiedOptional()); + + basicMapping.setSpecifiedOptional(Boolean.FALSE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + + assertEquals(Boolean.FALSE, basic.getOptional()); + + basicMapping.setSpecifiedOptional(null); + assertNotNull(attributeResource.mappingAnnotation(JPA.BASIC)); + } + + public void testSetSpecifiedOptional2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertNull(basicMapping.getSpecifiedOptional()); + assertTrue(basicMapping.isDefault()); + + basicMapping.setSpecifiedOptional(Boolean.TRUE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + + assertEquals(Boolean.TRUE, basic.getOptional()); + + basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertEquals(Boolean.TRUE, basicMapping.getSpecifiedOptional()); + assertFalse(basicMapping.isDefault()); + + basicMapping.setSpecifiedOptional(null); + assertNotNull(attributeResource.mappingAnnotation(JPA.BASIC)); + + basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertFalse(basicMapping.isDefault()); + } + + + public void testGetSpecifiedOptionalUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getSpecifiedOptional()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + basic.setOptional(Boolean.FALSE); + + assertEquals(Boolean.FALSE, basicMapping.getSpecifiedOptional()); + + basic.setOptional(null); + assertNull(basicMapping.getSpecifiedOptional()); + assertFalse(basicMapping.isDefault()); + assertSame(basicMapping, persistentAttribute.getSpecifiedMapping()); + + basic.setOptional(Boolean.FALSE); + attributeResource.setMappingAnnotation(null); + + assertNull(persistentAttribute.getSpecifiedMapping()); + assertEquals(Boolean.TRUE, ((IBasicMapping) persistentAttribute.getMapping()).getOptional()); + } + + + public void testIsLob() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertFalse(basicMapping.isLob()); + } + + public void testIsLob2() throws Exception { + createTestEntityWithLob(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + + assertTrue(basicMapping.isLob()); + } + + public void testSetLob() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + basicMapping.setLob(true); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNotNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + + basicMapping.setLob(false); + assertNull(attributeResource.annotation(Lob.ANNOTATION_NAME)); + } + + public void testIsLobUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertFalse(basicMapping.isLob()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(Lob.ANNOTATION_NAME); + + assertTrue(basicMapping.isLob()); + + attributeResource.removeAnnotation(Lob.ANNOTATION_NAME); + + assertFalse(basicMapping.isLob()); + } + + public void testDefaultBasicGetDefaultEnumerated() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + assertEquals(EnumType.ORDINAL, basicMapping.getDefaultEnumerated()); + } + + public void testSpecifiedBasicGetDefaultEnumerated() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertEquals(EnumType.ORDINAL, basicMapping.getDefaultEnumerated()); + } + + public void testGetEnumerated() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(EnumType.ORDINAL, basicMapping.getEnumerated()); + + basicMapping.setSpecifiedEnumerated(EnumType.STRING); + assertEquals(EnumType.STRING, basicMapping.getEnumerated()); + } + + public void testGetSpecifiedEnumerated() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getSpecifiedEnumerated()); + } + + public void testGetSpecifiedEnumerated2() throws Exception { + createTestEntityWithEnumerated(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + + assertEquals(EnumType.STRING, basicMapping.getSpecifiedEnumerated()); + } + + public void testSetSpecifiedEnumerated() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(basicMapping.getSpecifiedEnumerated()); + + basicMapping.setSpecifiedEnumerated(EnumType.STRING); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Enumerated enumerated = (Enumerated) attributeResource.annotation(Enumerated.ANNOTATION_NAME); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.EnumType.STRING, enumerated.getValue()); + + basicMapping.setSpecifiedEnumerated(null); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + } + + public void testGetSpecifieEnumeratedUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getSpecifiedEnumerated()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Enumerated enumerated = (Enumerated) attributeResource.addAnnotation(Enumerated.ANNOTATION_NAME); + enumerated.setValue(org.eclipse.jpt.core.internal.resource.java.EnumType.STRING); + + assertEquals(EnumType.STRING, basicMapping.getSpecifiedEnumerated()); + + enumerated.setValue(null); + assertNull(attributeResource.annotation(Enumerated.ANNOTATION_NAME)); + assertNull(basicMapping.getSpecifiedEnumerated()); + assertFalse(basicMapping.isDefault()); + assertSame(basicMapping, persistentAttribute.getSpecifiedMapping()); + } + + + public void testGetTemporal() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getTemporal()); + } + + public void testGetTemporal2() throws Exception { + createTestEntityWithTemporal(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getMapping(); + + assertEquals(TemporalType.TIMESTAMP, basicMapping.getTemporal()); + } + + public void testSetTemporal() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(basicMapping.getTemporal()); + + basicMapping.setTemporal(TemporalType.TIME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.annotation(Temporal.ANNOTATION_NAME); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.TemporalType.TIME, temporal.getValue()); + + basicMapping.setTemporal(null); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testGetTemporalUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getTemporal()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.addAnnotation(Temporal.ANNOTATION_NAME); + temporal.setValue(org.eclipse.jpt.core.internal.resource.java.TemporalType.DATE); + + assertEquals(TemporalType.DATE, basicMapping.getTemporal()); + + attributeResource.removeAnnotation(Temporal.ANNOTATION_NAME); + + assertNull(basicMapping.getTemporal()); + assertFalse(basicMapping.isDefault()); + assertSame(basicMapping, persistentAttribute.getSpecifiedMapping()); + } + + public void testGetColumn() throws Exception { + createTestEntityWithBasicMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IBasicMapping basicMapping = (IBasicMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedName()); + assertEquals("id", basicMapping.getColumn().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setName("foo"); + + assertEquals("foo", basicMapping.getColumn().getSpecifiedName()); + assertEquals("foo", basicMapping.getColumn().getName()); + assertEquals("id", basicMapping.getColumn().getDefaultName()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaColumnTests.java new file mode 100644 index 0000000000..cd17122989 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaColumnTests.java @@ -0,0 +1,849 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IAbstractColumn; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IColumn; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaColumnTests extends ContextModelTestCase +{ + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String TABLE_NAME = "MY_TABLE"; + private static final String COLUMN_DEFINITION = "MY_COLUMN_DEFINITION"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createBasicAnnotation() throws Exception{ + this.createAnnotationAndMembers("Basic", "String name() default \"\";"); + } + + private void createColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithDefaultBasicColumn() throws Exception { + createEntityAnnotation(); + createColumnAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(name=\"" + COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestEntityWithBasicColumnTableSet() throws Exception { + createEntityAnnotation(); + createColumnAnnotation(); + createBasicAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic"); + sb.append("@Column(table=\"" + TABLE_NAME + "\")"); + } + }); + } + private IType createTestEntityWithBasicColumnColumnDefinitionSet() throws Exception { + createEntityAnnotation(); + createColumnAnnotation(); + createBasicAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic"); + sb.append("@Column(columnDefinition=\"" + COLUMN_DEFINITION + "\")"); + } + }); + } + + public JavaColumnTests(String name) { + super(name); + } + + public void testGetSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedName()); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithDefaultBasicColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(COLUMN_NAME, basicMapping.getColumn().getSpecifiedName()); + } + + public void testGetDefaultNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(basicMapping.persistentAttribute().getName(), basicMapping.getColumn().getDefaultName()); + assertEquals("id", basicMapping.getColumn().getDefaultName()); + } + + public void testGetDefaultName() throws Exception { + createTestEntityWithDefaultBasicColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + assertEquals("id", basicMapping.getColumn().getDefaultName()); + + basicMapping.getColumn().setSpecifiedName("foo"); + assertEquals("id", basicMapping.getColumn().getDefaultName()); + } + + public void testGetNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals("id", basicMapping.getColumn().getName()); + } + + public void testGetName() throws Exception { + createTestEntityWithDefaultBasicColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(COLUMN_NAME, basicMapping.getColumn().getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + basicMapping.getColumn().setSpecifiedName("foo"); + + assertEquals("foo", basicMapping.getColumn().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals("foo", column.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithDefaultBasicColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + basicMapping.getColumn().setSpecifiedName(null); + + assertNull(basicMapping.getColumn().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.annotation(JPA.COLUMN)); + } + + public void testGetNameUpdatesFromResourceChange() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + + column.setName("foo"); + assertEquals("foo", basicMapping.getColumn().getSpecifiedName()); + assertEquals("foo", basicMapping.getColumn().getName()); + + column.setName(null); + assertNull(basicMapping.getColumn().getSpecifiedName()); + } + + + + + + + + + + + + + + public void testGetSpecifiedTableNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedTable()); + } + + public void testGetSpecifiedTable() throws Exception { + createTestEntityWithBasicColumnTableSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(TABLE_NAME, basicMapping.getColumn().getSpecifiedTable()); + } + + public void testGetDefaultTableSpecifiedTableNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(((IEntity) basicMapping.typeMapping()).getName(), basicMapping.getColumn().getDefaultTable()); + assertEquals(TYPE_NAME, basicMapping.getColumn().getDefaultTable()); + } + + public void testGetDefaultTable() throws Exception { + createTestEntityWithDefaultBasicColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + assertEquals(TYPE_NAME, basicMapping.getColumn().getDefaultTable()); + + basicMapping.getColumn().setSpecifiedTable("foo"); + assertEquals(TYPE_NAME, basicMapping.getColumn().getDefaultTable()); + } + + public void testGetTable() throws Exception { + createTestEntityWithBasicColumnTableSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(TABLE_NAME, basicMapping.getColumn().getTable()); + } + + public void testSetSpecifiedTable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + basicMapping.getColumn().setSpecifiedTable("foo"); + + assertEquals("foo", basicMapping.getColumn().getSpecifiedTable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals("foo", column.getTable()); + } + + public void testSetSpecifiedTableNull() throws Exception { + createTestEntityWithBasicColumnTableSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + basicMapping.getColumn().setSpecifiedTable(null); + + assertNull(basicMapping.getColumn().getSpecifiedTable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.annotation(JPA.COLUMN)); + } + + public void testGetTableUpdatesFromResourceChange() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedTable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + + column.setTable("foo"); + assertEquals("foo", basicMapping.getColumn().getSpecifiedTable()); + assertEquals("foo", basicMapping.getColumn().getTable()); + + column.setTable(null); + assertNull(basicMapping.getColumn().getSpecifiedTable()); + } + + public void testGetColumnDefinition() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(COLUMN_DEFINITION, basicMapping.getColumn().getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + createTestEntityWithBasicColumnTableSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + basicMapping.getColumn().setColumnDefinition("foo"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals("foo", column.getColumnDefinition()); + + basicMapping.getColumn().setColumnDefinition(null); + assertNull(column.getColumnDefinition()); + } + + public void testGetColumnDefinitionUpdatesFromResourceChange() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getColumnDefinition()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + + column.setColumnDefinition("foo"); + assertEquals("foo", basicMapping.getColumn().getColumnDefinition()); + + column.setColumnDefinition(null); + assertNull(basicMapping.getColumn().getColumnDefinition()); + + } + + public void testGetLength() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_LENGTH, basicMapping.getColumn().getLength()); + basicMapping.getColumn().setSpecifiedLength(Integer.valueOf(55)); + assertEquals(Integer.valueOf(55), basicMapping.getColumn().getLength()); + } + + public void testGetDefaultLength() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_LENGTH, basicMapping.getColumn().getDefaultLength()); + basicMapping.getColumn().setSpecifiedLength(Integer.valueOf(55)); + + assertEquals(IColumn.DEFAULT_LENGTH, basicMapping.getColumn().getDefaultLength()); + } + + public void testGetSpecifiedLength() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedLength()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setLength(Integer.valueOf(66)); + + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getSpecifiedLength()); + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getLength()); + + column.setLength(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedLength()); + } + + public void testSetSpecifiedLength() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedLength()); + + basicMapping.getColumn().setSpecifiedLength(Integer.valueOf(100)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(100), column.getLength()); + + basicMapping.getColumn().setSpecifiedLength(null); + + assertNull(column.getLength()); + } + + public void testGetPrecision() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_PRECISION, basicMapping.getColumn().getPrecision()); + basicMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(55)); + assertEquals(Integer.valueOf(55), basicMapping.getColumn().getPrecision()); + } + + public void testGetDefaultPrecision() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_PRECISION, basicMapping.getColumn().getDefaultPrecision()); + basicMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(55)); + + assertEquals(IColumn.DEFAULT_PRECISION, basicMapping.getColumn().getDefaultPrecision()); + } + + public void testGetSpecifiedPrecision() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedPrecision()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setPrecision(Integer.valueOf(66)); + + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getSpecifiedPrecision()); + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getPrecision()); + + column.setPrecision(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedPrecision()); + } + + public void testSetSpecifiedPrecision() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedPrecision()); + + basicMapping.getColumn().setSpecifiedPrecision(Integer.valueOf(100)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(100), column.getPrecision()); + + basicMapping.getColumn().setSpecifiedPrecision(null); + + assertNull(column.getPrecision()); + } + + public void testGetScale() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_SCALE, basicMapping.getColumn().getScale()); + basicMapping.getColumn().setSpecifiedScale(Integer.valueOf(55)); + assertEquals(Integer.valueOf(55), basicMapping.getColumn().getScale()); + } + + public void testGetDefaultScale() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IColumn.DEFAULT_SCALE, basicMapping.getColumn().getDefaultScale()); + basicMapping.getColumn().setSpecifiedScale(Integer.valueOf(55)); + + assertEquals(IColumn.DEFAULT_SCALE, basicMapping.getColumn().getDefaultScale()); + } + + public void testGetSpecifiedScale() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedScale()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setScale(Integer.valueOf(66)); + + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getSpecifiedScale()); + assertEquals(Integer.valueOf(66), basicMapping.getColumn().getScale()); + + column.setScale(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedScale()); + } + + public void testSetSpecifiedScale() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedScale()); + + basicMapping.getColumn().setSpecifiedScale(Integer.valueOf(100)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(100), column.getScale()); + + basicMapping.getColumn().setSpecifiedScale(null); + + assertNull(column.getScale()); + } + + public void testGetUnique() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_UNIQUE, basicMapping.getColumn().getUnique()); + basicMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getUnique()); + } + + public void testGetDefaultUnique() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_UNIQUE, basicMapping.getColumn().getDefaultUnique()); + basicMapping.getColumn().setSpecifiedUnique(Boolean.TRUE); + + assertEquals(IAbstractColumn.DEFAULT_UNIQUE, basicMapping.getColumn().getDefaultUnique()); + } + + public void testGetSpecifiedUnique() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedUnique()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setUnique(Boolean.TRUE); + + assertEquals(Boolean.TRUE, basicMapping.getColumn().getSpecifiedUnique()); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getUnique()); + + column.setUnique(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedUnique()); + } + + public void testSetSpecifiedUnique() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedUnique()); + + basicMapping.getColumn().setSpecifiedUnique(Boolean.FALSE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.FALSE, column.getUnique()); + + basicMapping.getColumn().setSpecifiedUnique(null); + + assertNull(column.getUnique()); + } + + public void testGetInsertable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_INSERTABLE, basicMapping.getColumn().getInsertable()); + basicMapping.getColumn().setSpecifiedInsertable(Boolean.TRUE); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getInsertable()); + } + + public void testGetDefaultInsertable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_INSERTABLE, basicMapping.getColumn().getDefaultInsertable()); + basicMapping.getColumn().setSpecifiedInsertable(Boolean.TRUE); + + assertEquals(IAbstractColumn.DEFAULT_INSERTABLE, basicMapping.getColumn().getDefaultInsertable()); + } + + public void testGetSpecifiedInsertable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedInsertable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setInsertable(Boolean.TRUE); + + assertEquals(Boolean.TRUE, basicMapping.getColumn().getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getInsertable()); + + column.setInsertable(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedInsertable()); + } + + public void testSetSpecifiedInsertable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedInsertable()); + + basicMapping.getColumn().setSpecifiedInsertable(Boolean.FALSE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.FALSE, column.getInsertable()); + + basicMapping.getColumn().setSpecifiedInsertable(null); + + assertNull(column.getInsertable()); + } + + public void testGetNullable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_NULLABLE, basicMapping.getColumn().getNullable()); + basicMapping.getColumn().setSpecifiedNullable(Boolean.TRUE); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getNullable()); + } + + public void testGetDefaultNullable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_NULLABLE, basicMapping.getColumn().getDefaultNullable()); + basicMapping.getColumn().setSpecifiedNullable(Boolean.TRUE); + + assertEquals(IAbstractColumn.DEFAULT_NULLABLE, basicMapping.getColumn().getDefaultNullable()); + } + + public void testGetSpecifiedNullable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedNullable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setNullable(Boolean.TRUE); + + assertEquals(Boolean.TRUE, basicMapping.getColumn().getSpecifiedNullable()); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getNullable()); + + column.setNullable(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedNullable()); + } + + public void testSetSpecifiedNullable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedNullable()); + + basicMapping.getColumn().setSpecifiedNullable(Boolean.FALSE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.FALSE, column.getNullable()); + + basicMapping.getColumn().setSpecifiedNullable(null); + + assertNull(column.getNullable()); + } + + public void testGetUpdatable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_UPDATABLE, basicMapping.getColumn().getUpdatable()); + basicMapping.getColumn().setSpecifiedUpdatable(Boolean.TRUE); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getUpdatable()); + } + + public void testGetDefaultUpdatable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertEquals(IAbstractColumn.DEFAULT_UPDATABLE, basicMapping.getColumn().getDefaultUpdatable()); + basicMapping.getColumn().setSpecifiedUpdatable(Boolean.TRUE); + + assertEquals(IAbstractColumn.DEFAULT_UPDATABLE, basicMapping.getColumn().getDefaultUpdatable()); + } + + public void testGetSpecifiedUpdatable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedUpdatable()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setUpdatable(Boolean.TRUE); + + assertEquals(Boolean.TRUE, basicMapping.getColumn().getSpecifiedUpdatable()); + assertEquals(Boolean.TRUE, basicMapping.getColumn().getUpdatable()); + + column.setUpdatable(null); + + assertNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(basicMapping.getColumn().getSpecifiedUpdatable()); + } + + public void testSetSpecifiedUpdatable() throws Exception { + createTestEntityWithBasicColumnColumnDefinitionSet(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IBasicMapping basicMapping = (IBasicMapping) javaPersistentType().attributes().next().getMapping(); + + assertNull(basicMapping.getColumn().getSpecifiedUpdatable()); + + basicMapping.getColumn().setSpecifiedUpdatable(Boolean.FALSE); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.FALSE, column.getUpdatable()); + + basicMapping.getColumn().setSpecifiedUpdatable(null); + + assertNull(column.getUpdatable()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaDiscriminatorColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaDiscriminatorColumnTests.java new file mode 100644 index 0000000000..a6e3b8fe29 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaDiscriminatorColumnTests.java @@ -0,0 +1,327 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.DiscriminatorType; +import org.eclipse.jpt.core.internal.context.base.IDiscriminatorColumn; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorColumn; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaDiscriminatorColumnTests extends ContextModelTestCase +{ + private static final String DISCRIMINATOR_COLUMN_NAME = "MY_DISCRIMINATOR_COLUMN"; + private static final String COLUMN_DEFINITION = "MY_COLUMN_DEFINITION"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + private void createDiscriminatorColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("DiscriminatorColumn", + "String name() default \"DTYPE\";" + + "DiscriminatorType discriminatorType() default STRING;" + + "String columnDefinition() default \"\";" + + "int length() default 31;"); + } + + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithDiscriminatorColumn() throws Exception { + createEntityAnnotation(); + createDiscriminatorColumnAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.DISCRIMINATOR_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@DiscriminatorColumn(name=\"" + DISCRIMINATOR_COLUMN_NAME + "\")"); + } + }); + } + + + public JavaDiscriminatorColumnTests(String name) { + super(name); + } + + public void testGetSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedName()); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DISCRIMINATOR_COLUMN_NAME, javaEntity().getDiscriminatorColumn().getSpecifiedName()); + } + + public void testGetDefaultNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals("DTYPE", javaEntity().getDiscriminatorColumn().getDefaultName()); + } + + public void testGetDefaultName() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals("DTYPE", javaEntity().getDiscriminatorColumn().getDefaultName()); + + javaEntity().getDiscriminatorColumn().setSpecifiedName("foo"); + assertEquals("DTYPE", javaEntity().getDiscriminatorColumn().getDefaultName()); + } + + public void testGetNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals("DTYPE", javaEntity().getDiscriminatorColumn().getName()); + } + + public void testGetName() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DISCRIMINATOR_COLUMN_NAME, javaEntity().getDiscriminatorColumn().getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().getDiscriminatorColumn().setSpecifiedName("foo"); + + assertEquals("foo", javaEntity().getDiscriminatorColumn().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals("foo", discriminatorColumn.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().getDiscriminatorColumn().setSpecifiedName(null); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertNull(discriminatorColumn); + } + + public void testGetDefaultDiscriminatorType() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DiscriminatorType.STRING, javaEntity().getDiscriminatorColumn().getDefaultDiscriminatorType()); + } + + public void testGetDiscriminatorType() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DiscriminatorType.STRING, javaEntity().getDiscriminatorColumn().getDiscriminatorType()); + + javaEntity().getDiscriminatorColumn().setSpecifiedDiscriminatorType(DiscriminatorType.CHAR); + assertEquals(DiscriminatorType.CHAR, javaEntity().getDiscriminatorColumn().getDiscriminatorType()); + } + + public void testGetSpecifiedDiscriminatorType() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + discriminatorColumn.setDiscriminatorType(org.eclipse.jpt.core.internal.resource.java.DiscriminatorType.CHAR); + + assertEquals(DiscriminatorType.CHAR, javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + } + + public void testSetSpecifiedDiscriminatorType() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + + javaEntity().getDiscriminatorColumn().setSpecifiedDiscriminatorType(DiscriminatorType.CHAR); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.DiscriminatorType.CHAR, discriminatorColumn.getDiscriminatorType()); + + javaEntity().getDiscriminatorColumn().setSpecifiedName(null); + javaEntity().getDiscriminatorColumn().setSpecifiedDiscriminatorType(null); + assertNull(typeResource.annotation(JPA.DISCRIMINATOR_COLUMN)); + } + + public void testGetDiscriminatorTypeUpdatesFromResourceChange() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.addAnnotation(JPA.DISCRIMINATOR_COLUMN); + + column.setDiscriminatorType(org.eclipse.jpt.core.internal.resource.java.DiscriminatorType.INTEGER); + assertEquals(DiscriminatorType.INTEGER, javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + assertEquals(DiscriminatorType.INTEGER, javaEntity().getDiscriminatorColumn().getDiscriminatorType()); + + column.setDiscriminatorType(null); + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedDiscriminatorType()); + assertEquals(IDiscriminatorColumn.DEFAULT_DISCRIMINATOR_TYPE, javaEntity().getDiscriminatorColumn().getDiscriminatorType()); + } + + public void testGetLength() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IDiscriminatorColumn.DEFAULT_LENGTH, javaEntity().getDiscriminatorColumn().getLength()); + javaEntity().getDiscriminatorColumn().setSpecifiedLength(Integer.valueOf(55)); + assertEquals(Integer.valueOf(55), javaEntity().getDiscriminatorColumn().getLength()); + } + + public void testGetDefaultLength() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IDiscriminatorColumn.DEFAULT_LENGTH, javaEntity().getDiscriminatorColumn().getDefaultLength()); + javaEntity().getDiscriminatorColumn().setSpecifiedLength(Integer.valueOf(55)); + + assertEquals(IDiscriminatorColumn.DEFAULT_LENGTH, javaEntity().getDiscriminatorColumn().getDefaultLength()); + } + + public void testGetSpecifiedLength() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + discriminatorColumn.setLength(Integer.valueOf(66)); + + assertEquals(Integer.valueOf(66), javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + assertEquals(Integer.valueOf(66), javaEntity().getDiscriminatorColumn().getLength()); + discriminatorColumn.setName(null); + discriminatorColumn.setLength(null); + + assertNull(typeResource.annotation(JPA.DISCRIMINATOR_COLUMN)); + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + } + + public void testSetSpecifiedLength() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + + javaEntity().getDiscriminatorColumn().setSpecifiedLength(Integer.valueOf(100)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn discriminatorColumn = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals(Integer.valueOf(100), discriminatorColumn.getLength()); + + javaEntity().getDiscriminatorColumn().setSpecifiedName(null); + javaEntity().getDiscriminatorColumn().setSpecifiedLength(null); + assertNull(typeResource.annotation(JPA.DISCRIMINATOR_COLUMN)); + } + + public void testGetLengthUpdatesFromResourceChange() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.addAnnotation(JPA.DISCRIMINATOR_COLUMN); + + column.setLength(Integer.valueOf(78)); + assertEquals(Integer.valueOf(78), javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + assertEquals(Integer.valueOf(78), javaEntity().getDiscriminatorColumn().getLength()); + + column.setLength(null); + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedLength()); + assertEquals(IDiscriminatorColumn.DEFAULT_LENGTH, javaEntity().getDiscriminatorColumn().getLength()); + } + + + public void testGetColumnDefinition() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getDiscriminatorColumn().getColumnDefinition()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + column.setColumnDefinition(COLUMN_DEFINITION); + + assertEquals(COLUMN_DEFINITION, javaEntity().getDiscriminatorColumn().getColumnDefinition()); + + column.setColumnDefinition(null); + + assertNull(javaEntity().getDiscriminatorColumn().getColumnDefinition()); + + typeResource.removeAnnotation(JPA.DISCRIMINATOR_COLUMN); + } + + public void testSetColumnDefinition() throws Exception { + createTestEntityWithDiscriminatorColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().getDiscriminatorColumn().setColumnDefinition("foo"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals("foo", column.getColumnDefinition()); + + javaEntity().getDiscriminatorColumn().setColumnDefinition(null); + column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + assertNull(column.getColumnDefinition()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddableTests.java new file mode 100644 index 0000000000..2e69d1452d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddableTests.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IEmbeddable; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.IMappedSuperclass; +import org.eclipse.jpt.core.internal.context.java.JavaNullTypeMapping; +import org.eclipse.jpt.core.internal.resource.java.Embeddable; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaEmbeddableTests extends ContextModelTestCase +{ + + private void createEmbeddableAnnotation() throws Exception { + this.createAnnotationAndMembers("Embeddable", ""); + } + + private IType createTestEmbeddable() throws Exception { + createEmbeddableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Embeddable"); + } + }); + } + + + public JavaEmbeddableTests(String name) { + super(name); + } + + public void testMorphToEntity() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IEntity); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embeddable.ANNOTATION_NAME)); + } + + public void testMorphToMappedSuperclass() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IMappedSuperclass); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embeddable.ANNOTATION_NAME)); + } + + public void testMorphToNull() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof JavaNullTypeMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.mappingAnnotation(Embeddable.ANNOTATION_NAME)); + } + + public void testEmbeddable() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + assertTrue(javaPersistentType().getMapping() instanceof IEmbeddable); + } + + public void testOverridableAttributeNames() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + Iterator<String> overridableAttributeNames = embeddable.overridableAttributeNames(); + assertFalse(overridableAttributeNames.hasNext()); + } + + public void testOverridableAssociationNames() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + Iterator<String> overridableAssociationNames = embeddable.overridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + + public void testTableNameIsInvalid() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + + assertFalse(embeddable.tableNameIsInvalid(FULLY_QUALIFIED_TYPE_NAME)); + assertFalse(embeddable.tableNameIsInvalid("FOO")); + } + + public void testAttributeMappingKeyAllowed() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + assertTrue(embeddable.attributeMappingKeyAllowed(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY)); + assertTrue(embeddable.attributeMappingKeyAllowed(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + assertFalse(embeddable.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + } + + + public void testAssociatedTables() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + + assertFalse(embeddable.associatedTables().hasNext()); + } + + public void testAssociatedTablesIncludingInherited() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + + assertFalse(embeddable.associatedTablesIncludingInherited().hasNext()); + } + + public void testAssociatedTableNamesIncludingInherited() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + + assertFalse(embeddable.associatedTableNamesIncludingInherited().hasNext()); + } + + public void testAllOverridableAttributeNames() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + Iterator<String> overridableAttributeNames = embeddable.overridableAttributeNames(); + assertFalse(overridableAttributeNames.hasNext()); + } + + //TODO need to create a subclass mappedSuperclass and test this + public void testAllOverridableAssociationNames() throws Exception { + createTestEmbeddable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEmbeddable embeddable = (IEmbeddable) javaPersistentType().getMapping(); + Iterator<String> overridableAssociationNames = embeddable.overridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedIdMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedIdMappingTests.java new file mode 100644 index 0000000000..ec89c84cfb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedIdMappingTests.java @@ -0,0 +1,615 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IAttributeOverride; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddable; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaAttributeOverride; +import org.eclipse.jpt.core.internal.context.java.JavaNullAttributeMapping; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverrides; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaEmbeddedIdMappingTests extends ContextModelTestCase +{ + + public static final String EMBEDDABLE_TYPE_NAME = "MyEmbeddable"; + public static final String FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME = PACKAGE_NAME + "." + EMBEDDABLE_TYPE_NAME; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createEmbeddableAnnotation() throws Exception{ + this.createAnnotationAndMembers("Embeddable", ""); + } + + private void createEmbeddedIdAnnotation() throws Exception{ + this.createAnnotationAndMembers("EmbeddedId", ""); + } + + private IType createTestEntityWithEmbeddedIdMapping() throws Exception { + createEntityAnnotation(); + createEmbeddedIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDED_ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@EmbeddedId").append(CR); + sb.append(CR); + sb.append(" private " + EMBEDDABLE_TYPE_NAME +" myEmbeddedId;").append(CR); + sb.append(CR); + } + }); + } + + private IType createEmbeddableType() throws Exception { + createEmbeddableAnnotation(); + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.EMBEDDABLE); + sb.append(";"); + sb.append(CR); + sb.append("@Embeddable"); + sb.append(CR); + sb.append("public class ").append(EMBEDDABLE_TYPE_NAME).append(" {"); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" "); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, EMBEDDABLE_TYPE_NAME + ".java", sourceWriter); + } + + + public JavaEmbeddedIdMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(((IEmbeddedMapping) persistentAttribute.getMapping()).attributeOverrides().hasNext()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testDefaultEmbeddedIdMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof JavaNullAttributeMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) persistentAttribute.getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedIdMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testSpecifiedAttributeOverrides() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + + ListIterator<IJavaAttributeOverride> specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + + assertFalse(specifiedAttributeOverrides.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.move(1, 0, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertFalse(specifiedAttributeOverrides.hasNext()); + } + + public void testDefaultAttributeOverrides() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertEquals("myEmbeddedId", attributeResource.getName()); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + assertEquals(2, CollectionTools.size(embeddedIdMapping.defaultAttributeOverrides())); + IAttributeOverride defaultAttributeOverride = embeddedIdMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IEmbeddable embeddable = (IEmbeddable) classRefs.next().getJavaPersistentType().getMapping(); + + IBasicMapping cityMapping = (IBasicMapping) embeddable.persistentType().attributeNamed("city").getMapping(); + cityMapping.getColumn().setSpecifiedName("FOO"); + cityMapping.getColumn().setSpecifiedTable("BAR"); + + assertEquals(2, CollectionTools.size(embeddedIdMapping.defaultAttributeOverrides())); + defaultAttributeOverride = embeddedIdMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("FOO", defaultAttributeOverride.getColumn().getName()); + assertEquals("BAR", defaultAttributeOverride.getColumn().getTable()); + + cityMapping.getColumn().setSpecifiedName(null); + cityMapping.getColumn().setSpecifiedTable(null); + defaultAttributeOverride = embeddedIdMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("city"); + assertEquals(1, CollectionTools.size(embeddedIdMapping.defaultAttributeOverrides())); + } + + public void testSpecifiedAttributeOverridesSize() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + assertEquals(0, embeddedIdMapping.specifiedAttributeOverridesSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + + assertEquals(2, embeddedIdMapping.specifiedAttributeOverridesSize()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("BAR"); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testAddSpecifiedAttributeOverride2() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedIdMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedIdMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedIdMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedIdMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertEquals(3, CollectionTools.size(attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME))); + + embeddedIdMapping.removeSpecifiedAttributeOverride(1); + + Iterator<JavaResource> attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + Iterator<IAttributeOverride> attributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + embeddedIdMapping.removeSpecifiedAttributeOverride(1); + attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + attributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + embeddedIdMapping.removeSpecifiedAttributeOverride(0); + attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertFalse(attributeOverrideResources.hasNext()); + attributeOverrides = embeddedIdMapping.specifiedAttributeOverrides(); + assertFalse(attributeOverrides.hasNext()); + + assertNull(attributeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedIdMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedIdMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + attributeResource.move(2, 0, AttributeOverrides.ANNOTATION_NAME); + + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + } + + public void testAttributeOverrideIsVirtual() throws Exception { + createTestEntityWithEmbeddedIdMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedIdMapping embeddedIdMapping = (IEmbeddedIdMapping) javaPersistentType().attributeNamed("myEmbeddedId").getMapping(); + ListIterator<IAttributeOverride> defaultAttributeOverrides = embeddedIdMapping.defaultAttributeOverrides(); + IAttributeOverride defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("state", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + + embeddedIdMapping.addSpecifiedAttributeOverride(0).setName("state"); + IAttributeOverride specifiedAttributeOverride = embeddedIdMapping.specifiedAttributeOverrides().next(); + assertFalse(specifiedAttributeOverride.isVirtual()); + + + defaultAttributeOverrides = embeddedIdMapping.defaultAttributeOverrides(); + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedMappingTests.java new file mode 100644 index 0000000000..9922dcd9c8 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEmbeddedMappingTests.java @@ -0,0 +1,618 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IAttributeOverride; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddable; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaAttributeOverride; +import org.eclipse.jpt.core.internal.context.java.JavaNullAttributeMapping; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverrides; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaEmbeddedMappingTests extends ContextModelTestCase +{ + + public static final String EMBEDDABLE_TYPE_NAME = "MyEmbeddable"; + public static final String FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME = PACKAGE_NAME + "." + EMBEDDABLE_TYPE_NAME; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createEmbeddableAnnotation() throws Exception{ + this.createAnnotationAndMembers("Embeddable", ""); + } + + private void createEmbeddedAnnotation() throws Exception{ + this.createAnnotationAndMembers("Embedded", ""); + } + + private IType createTestEntityWithEmbeddedMapping() throws Exception { + createEntityAnnotation(); + createEmbeddedAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDED); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded").append(CR); + sb.append(" private " + EMBEDDABLE_TYPE_NAME + " myEmbedded;").append(CR); + sb.append(CR); + } + }); + } + + private IType createEmbeddableType() throws Exception { + createEmbeddableAnnotation(); + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.EMBEDDABLE); + sb.append(";"); + sb.append(CR); + sb.append("@Embeddable"); + sb.append(CR); + sb.append("public class ").append(EMBEDDABLE_TYPE_NAME).append(" {"); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" "); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, EMBEDDABLE_TYPE_NAME + ".java", sourceWriter); + } + + + public JavaEmbeddedMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(((IEmbeddedMapping) persistentAttribute.getMapping()).attributeOverrides().hasNext()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testDefaultEmbeddedMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof JavaNullAttributeMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) persistentAttribute.getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0); + assertFalse(embeddedMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + } + + public void testSpecifiedAttributeOverrides() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + + ListIterator<IJavaAttributeOverride> specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + + assertFalse(specifiedAttributeOverrides.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.move(1, 0, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertFalse(specifiedAttributeOverrides.hasNext()); + } + + public void testDefaultAttributeOverrides() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertEquals("myEmbedded", attributeResource.getName()); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + assertEquals(2, CollectionTools.size(embeddedMapping.defaultAttributeOverrides())); + IAttributeOverride defaultAttributeOverride = embeddedMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IEmbeddable embeddable = (IEmbeddable) classRefs.next().getJavaPersistentType().getMapping(); + + IBasicMapping cityMapping = (IBasicMapping) embeddable.persistentType().attributeNamed("city").getMapping(); + cityMapping.getColumn().setSpecifiedName("FOO"); + cityMapping.getColumn().setSpecifiedTable("BAR"); + + assertEquals("myEmbedded", attributeResource.getName()); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + assertEquals(2, CollectionTools.size(embeddedMapping.defaultAttributeOverrides())); + defaultAttributeOverride = embeddedMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("FOO", defaultAttributeOverride.getColumn().getName()); + assertEquals("BAR", defaultAttributeOverride.getColumn().getTable()); + + cityMapping.getColumn().setSpecifiedName(null); + cityMapping.getColumn().setSpecifiedTable(null); + defaultAttributeOverride = embeddedMapping.defaultAttributeOverrides().next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertEquals("city", defaultAttributeOverride.getColumn().getName()); + assertEquals(TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + embeddedMapping.addSpecifiedAttributeOverride(0).setName("city"); + assertEquals(1, CollectionTools.size(embeddedMapping.defaultAttributeOverrides())); + } + + public void testSpecifiedAttributeOverridesSize() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + assertEquals(0, embeddedMapping.specifiedAttributeOverridesSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + + assertEquals(2, embeddedMapping.specifiedAttributeOverridesSize()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("BAR"); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testAddSpecifiedAttributeOverride2() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertEquals(3, CollectionTools.size(attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME))); + + embeddedMapping.removeSpecifiedAttributeOverride(1); + + Iterator<JavaResource> attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + Iterator<IAttributeOverride> attributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + embeddedMapping.removeSpecifiedAttributeOverride(1); + attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + attributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + embeddedMapping.removeSpecifiedAttributeOverride(0); + attributeOverrideResources = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertFalse(attributeOverrideResources.hasNext()); + attributeOverrides = embeddedMapping.specifiedAttributeOverrides(); + assertFalse(attributeOverrides.hasNext()); + + assertNull(attributeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + embeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + embeddedMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + embeddedMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + attributeResource.move(2, 0, AttributeOverrides.ANNOTATION_NAME); + + Iterator<JavaResource> attributeOverrides = attributeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + } + + public void testAttributeOverrideIsVirtual() throws Exception { + createTestEntityWithEmbeddedMapping(); + createEmbeddableType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_EMBEDDABLE_TYPE_NAME); + + IEmbeddedMapping embeddedMapping = (IEmbeddedMapping) javaPersistentType().attributeNamed("myEmbedded").getMapping(); + ListIterator<IAttributeOverride> defaultAttributeOverrides = embeddedMapping.defaultAttributeOverrides(); + IAttributeOverride defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("state", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + + embeddedMapping.addSpecifiedAttributeOverride(0).setName("state"); + IAttributeOverride specifiedAttributeOverride = embeddedMapping.specifiedAttributeOverrides().next(); + assertFalse(specifiedAttributeOverride.isVirtual()); + + + defaultAttributeOverrides = embeddedMapping.defaultAttributeOverrides(); + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("city", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEntityTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEntityTests.java new file mode 100644 index 0000000000..3fb053666a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaEntityTests.java @@ -0,0 +1,2440 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.DiscriminatorType; +import org.eclipse.jpt.core.internal.context.base.IAssociationOverride; +import org.eclipse.jpt.core.internal.context.base.IAttributeOverride; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddable; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IMappedSuperclass; +import org.eclipse.jpt.core.internal.context.base.INamedNativeQuery; +import org.eclipse.jpt.core.internal.context.base.INamedQuery; +import org.eclipse.jpt.core.internal.context.base.IPersistentType; +import org.eclipse.jpt.core.internal.context.base.IPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.context.base.ISecondaryTable; +import org.eclipse.jpt.core.internal.context.base.ITable; +import org.eclipse.jpt.core.internal.context.base.InheritanceType; +import org.eclipse.jpt.core.internal.context.java.IJavaAttributeOverride; +import org.eclipse.jpt.core.internal.context.java.IJavaEntity; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.context.java.IJavaPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.context.java.IJavaSecondaryTable; +import org.eclipse.jpt.core.internal.context.java.JavaNullTypeMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverride; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverrides; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverrides; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorColumn; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorValue; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.IdClass; +import org.eclipse.jpt.core.internal.resource.java.Inheritance; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQueries; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQuery; +import org.eclipse.jpt.core.internal.resource.java.NamedQueries; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumns; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTables; +import org.eclipse.jpt.core.internal.resource.java.SequenceGenerator; +import org.eclipse.jpt.core.internal.resource.java.Table; +import org.eclipse.jpt.core.internal.resource.java.TableGenerator; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaEntityTests extends ContextModelTestCase +{ + private static final String ENTITY_NAME = "entityName"; + private static final String TABLE_NAME = "MY_TABLE"; + private static final String DISCRIMINATOR_VALUE = "MY_DISCRIMINATOR_VALUE"; + protected static final String SUB_TYPE_NAME = "AnnotationTestTypeChild"; + protected static final String FULLY_QUALIFIED_SUB_TYPE_NAME = PACKAGE_NAME + "." + SUB_TYPE_NAME; + + + public JavaEntityTests(String name) { + super(name); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private void createMappedSuperclassAnnotation() throws Exception{ + this.createAnnotationAndMembers("MappedSuperclass", ""); + } + + private void createTableAnnotation() throws Exception { + this.createAnnotationAndMembers("Table", "String name() default \"\";"); + } + + private void createInheritanceAnnotation() throws Exception { + createInheritanceTypeEnum(); + this.createAnnotationAndMembers("Inheritance", "InheritanceType strategy() default SINGLE_TABLE;"); + } + + private void createInheritanceTypeEnum() throws Exception { + this.createEnumAndMembers("InheritanceType", "SINGLE_TABLE, JOINED, TABLE_PER_CLASS"); + } + + private void createDiscriminatorValueAnnotation() throws Exception { + this.createAnnotationAndMembers("DiscriminatorValue", "String value();"); + } + + private void createSecondaryTableAnnotation() throws Exception { + this.createAnnotationAndMembers("SecondaryTable", + "String name(); " + + "String catalog() default \"\"; " + + "String schema() default \"\"; "); +// PrimaryKeyJoinColumn[] pkJoinColumns() default {}; +// UniqueConstraint[] uniqueConstraints() default {}; + } + + private void createSecondaryTablesAnnotation() throws Exception { + createSecondaryTableAnnotation(); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", + "String name(); " + + "String referencedColumnName() default \"\"; " + + "String columnDefinition() default \"\"; "); + } + + private void createPrimaryKeyJoinColumnsAnnotation() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + this.createAnnotationAndMembers("PrimaryKeyJoinColumns", "PrimaryKeyJoinColumn[] value();"); + } + + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityAnnotationOnProperty() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestMappedSuperclass() throws Exception { + createMappedSuperclassAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS, JPA.ONE_TO_ONE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne"); + sb.append(CR); + sb.append(" private int address;").append(CR); + sb.append(CR); + sb.append(" "); + } + }); + } + + private IType createTestEntityWithName() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity(name=\"" + ENTITY_NAME + "\")"); + } + }); + } + + private IType createTestEntityWithTable() throws Exception { + createEntityAnnotation(); + createTableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@Table(name=\"" + TABLE_NAME + "\")"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + private IType createTestEntityWithInheritance() throws Exception { + createEntityAnnotation(); + createInheritanceAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.INHERITANCE, JPA.INHERITANCE_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)").append(CR); + } + }); + } + + private IType createTestEntityWithDiscriminatorValue() throws Exception { + createEntityAnnotation(); + createDiscriminatorValueAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.DISCRIMINATOR_VALUE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@DiscriminatorValue(value=\"" + DISCRIMINATOR_VALUE + "\")"); + } + }); + } + + private IType createTestEntityWithSecondaryTable() throws Exception { + createEntityAnnotation(); + createSecondaryTableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@SecondaryTable(name=\"foo\")"); + } + }); + } + + private IType createTestEntityWithSecondaryTables() throws Exception { + createEntityAnnotation(); + createSecondaryTablesAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@SecondaryTables({@SecondaryTable(name=\"foo\"), @SecondaryTable(name=\"bar\")})"); + } + }); + } + + private IType createTestEntityWithPrimaryKeyJoinColumns() throws Exception { + createEntityAnnotation(); + createPrimaryKeyJoinColumnsAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name=\"foo\"), @PrimaryKeyJoinColumn(name=\"bar\")})"); + } + }); + } + + + public void testMorphToMappedSuperclass() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = (IEntity) javaPersistentType().getMapping(); + entity.getTable().setSpecifiedName("FOO"); + entity.addSpecifiedSecondaryTable(0); + entity.addSpecifiedPrimaryKeyJoinColumn(0); + entity.addSpecifiedAttributeOverride(0); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedDiscriminatorValue("asdf"); + entity.addTableGenerator(); + entity.addSequenceGenerator(); + + javaPersistentType().setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IMappedSuperclass); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Entity.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Table.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SecondaryTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(PrimaryKeyJoinColumn.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Inheritance.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(DiscriminatorValue.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = (IEntity) javaPersistentType().getMapping(); + entity.getTable().setSpecifiedName("FOO"); + entity.addSpecifiedSecondaryTable(0); + entity.addSpecifiedPrimaryKeyJoinColumn(0); + entity.addSpecifiedAttributeOverride(0); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedDiscriminatorValue("asdf"); + entity.addTableGenerator(); + entity.addSequenceGenerator(); + + javaPersistentType().setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IEmbeddable); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.mappingAnnotation(Entity.ANNOTATION_NAME)); + assertNull(typeResource.annotation(Table.ANNOTATION_NAME)); + assertNull(typeResource.annotation(SecondaryTable.ANNOTATION_NAME)); + assertNull(typeResource.annotation(PrimaryKeyJoinColumn.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(Inheritance.ANNOTATION_NAME)); + assertNull(typeResource.annotation(DiscriminatorValue.ANNOTATION_NAME)); + assertNull(typeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(typeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + } + + public void testMorphToNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = (IEntity) javaPersistentType().getMapping(); + entity.getTable().setSpecifiedName("FOO"); + entity.addSpecifiedSecondaryTable(0); + entity.addSpecifiedPrimaryKeyJoinColumn(0); + entity.addSpecifiedAttributeOverride(0); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedDiscriminatorValue("asdf"); + entity.addTableGenerator(); + entity.addSequenceGenerator(); + + javaPersistentType().setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof JavaNullTypeMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.mappingAnnotation(Entity.ANNOTATION_NAME)); + assertNull(typeResource.annotation(Table.ANNOTATION_NAME)); + assertNull(typeResource.annotation(SecondaryTable.ANNOTATION_NAME)); + assertNull(typeResource.annotation(PrimaryKeyJoinColumn.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(Inheritance.ANNOTATION_NAME)); + assertNull(typeResource.annotation(DiscriminatorValue.ANNOTATION_NAME)); + assertNull(typeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(typeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + } + + public void testAccessNoAnnotations() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + assertEquals(AccessType.FIELD, javaPersistentType().access()); + } + + public void testAccessAnnotationsOnParent() throws Exception { + createTestEntityAnnotationOnProperty(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + + IJavaPersistentType childPersistentType = javaPersistentType(); + IPersistentType parentPersistentType = childPersistentType.parentPersistentType(); + + assertEquals(AccessType.PROPERTY, parentPersistentType.access()); + assertEquals(AccessType.PROPERTY, childPersistentType.access()); + + ((IIdMapping) parentPersistentType.attributeNamed("id").getMapping()).getColumn().setSpecifiedName("FOO"); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.properties().next().setMappingAnnotation(null); + //no mapping(Id) annotation, but still a Column annotation, so access should still be property + assertEquals(AccessType.PROPERTY, parentPersistentType.access()); + assertEquals(AccessType.PROPERTY, childPersistentType.access()); + + ((IBasicMapping) parentPersistentType.attributeNamed("id").getMapping()).getColumn().setSpecifiedName(null); + assertEquals(AccessType.FIELD, parentPersistentType.access()); + assertEquals(AccessType.FIELD, childPersistentType.access()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, parentPersistentType.access()); + assertEquals(AccessType.PROPERTY, childPersistentType.access()); + + entityMappings().setSpecifiedAccess(AccessType.FIELD); + //still accessType of PROPERTY because the java class is not specified in this orm.xml + assertEquals(AccessType.PROPERTY, parentPersistentType.access()); + assertEquals(AccessType.PROPERTY, childPersistentType.access()); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + removeXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + //only parent specified in orm.xml, i think this outcome is right?? + assertEquals(AccessType.FIELD, xmlPersistentType.javaPersistentType().access()); + assertEquals(AccessType.FIELD, childPersistentType.access()); + + XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + removeXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + //both parent and child specified in orm.xml + assertEquals(AccessType.FIELD, xmlPersistentType.javaPersistentType().access()); + assertEquals(AccessType.FIELD, childXmlPersistentType.javaPersistentType().access()); + } + + public void testAccessWithXmlSettings() throws Exception { + createTestEntityAnnotationOnProperty(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + + assertEquals(AccessType.PROPERTY, javaPersistentType().access()); + + ((IIdMapping) javaPersistentType().attributeNamed("id").getMapping()).getColumn().setSpecifiedName("FOO"); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.properties().next().setMappingAnnotation(null); + //no mapping(Id) annotation, but still a Column annotation, so access should still be property + assertEquals(AccessType.PROPERTY, javaPersistentType().access()); + + ((IBasicMapping) javaPersistentType().attributeNamed("id").getMapping()).getColumn().setSpecifiedName(null); + assertEquals(AccessType.FIELD, javaPersistentType().access()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, javaPersistentType().access()); + + entityMappings().setSpecifiedAccess(AccessType.FIELD); + //still accessType of PROPERTY because the java class is not specified in this orm.xml + assertEquals(AccessType.PROPERTY, javaPersistentType().access()); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + + //now class is specified in orm.xml, so entityMappings access setting wins over persistence-unit-defaults + assertEquals(AccessType.FIELD, xmlPersistentType.javaPersistentType().access()); + + ((XmlEntity) xmlPersistentType.getMapping()).setSpecifiedAccess(AccessType.PROPERTY); + + //accessType should be PROPERTY now, java gets the access from xml entity if it is specified + assertEquals(AccessType.PROPERTY, xmlPersistentType.javaPersistentType().access()); + } + + public void testGetSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getSpecifiedName()); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(ENTITY_NAME, javaEntity().getSpecifiedName()); + } + + public void testGetDefaultNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getDefaultName()); + } + + public void testGetDefaultName() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getDefaultName()); + } + + public void testGetNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getName()); + } + + public void testGetName() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(ENTITY_NAME, javaEntity().getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().setSpecifiedName("foo"); + + assertEquals("foo", javaEntity().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals("foo", ((Entity) typeResource.mappingAnnotation()).getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().setSpecifiedName(null); + + assertNull(javaEntity().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(((Entity) typeResource.mappingAnnotation()).getName()); + } + + public void testUpdateFromSpecifiedNameChangeInResourceModel() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Entity entity = (Entity) typeResource.mappingAnnotation(); + entity.setName("foo"); + + assertEquals("foo", javaEntity().getSpecifiedName()); + } + + public void testGetTableName() throws Exception { + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + createTestEntityWithName(); + + assertEquals(ENTITY_NAME, javaEntity().getTableName()); + } + + public void testGetTableName2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getTableName()); + } + + public void testGetTableName3() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TABLE_NAME, javaEntity().getTableName()); + } + + public void testSetTableNameWithNullTable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ITable table = javaEntity().getTable(); + assertEquals(TYPE_NAME, table.getName()); + assertSourceDoesNotContain("@Table"); + + table.setSpecifiedName(TABLE_NAME); + assertSourceContains("@Table(name=\"" + TABLE_NAME + "\")"); + + assertEquals(TABLE_NAME, javaEntity().getTableName()); + assertEquals(TABLE_NAME, table.getName()); + + table.setSpecifiedCatalog(TABLE_NAME); + } + + public void testGetInheritanceStrategy() throws Exception { + createTestEntityWithInheritance(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(InheritanceType.TABLE_PER_CLASS, javaEntity().getInheritanceStrategy()); + } + + public void testGetDefaultInheritanceStrategy() throws Exception { + createTestEntity(); + createTestSubType(); + + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNotSame(javaEntity(), javaEntity().rootEntity()); + assertEquals(InheritanceType.SINGLE_TABLE, javaEntity().getDefaultInheritanceStrategy()); + + //change root inheritance strategy, verify default is changed for child entity + javaEntity().rootEntity().setSpecifiedInheritanceStrategy(InheritanceType.TABLE_PER_CLASS); + + assertEquals(InheritanceType.SINGLE_TABLE, javaEntity().rootEntity().getDefaultInheritanceStrategy()); + assertEquals(InheritanceType.TABLE_PER_CLASS, javaEntity().getDefaultInheritanceStrategy()); + assertEquals(InheritanceType.TABLE_PER_CLASS, javaEntity().getInheritanceStrategy()); + assertNull(javaEntity().getSpecifiedInheritanceStrategy()); + } + + public void testGetSpecifiedInheritanceStrategy() throws Exception { + createTestEntityWithInheritance(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(InheritanceType.TABLE_PER_CLASS, javaEntity().getSpecifiedInheritanceStrategy()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Inheritance inheritance = (Inheritance) typeResource.annotation(Inheritance.ANNOTATION_NAME); + + inheritance.setStrategy(org.eclipse.jpt.core.internal.resource.java.InheritanceType.JOINED); + + assertEquals(InheritanceType.JOINED, javaEntity().getSpecifiedInheritanceStrategy()); + + inheritance.setStrategy(null); + + assertNull(javaEntity().getSpecifiedInheritanceStrategy()); + } + + public void testSetSpecifiedInheritanceStrategy() throws Exception { + createTestEntityWithInheritance(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + assertEquals(InheritanceType.TABLE_PER_CLASS, javaEntity().getSpecifiedInheritanceStrategy()); + + javaEntity().setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + + assertEquals(InheritanceType.JOINED, javaEntity().getSpecifiedInheritanceStrategy()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Inheritance inheritance = (Inheritance) typeResource.annotation(Inheritance.ANNOTATION_NAME); + assertEquals(org.eclipse.jpt.core.internal.resource.java.InheritanceType.JOINED, inheritance.getStrategy()); + + } + + public void testGetDiscriminatorValue() throws Exception { + createTestEntityWithDiscriminatorValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DISCRIMINATOR_VALUE, javaEntity().getDiscriminatorValue()); + } + + public void testGetDefaultDiscriminatorValue() throws Exception { + createTestEntityWithDiscriminatorValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(javaEntity().getName(), javaEntity().getDefaultDiscriminatorValue()); + + javaEntity().getDiscriminatorColumn().setSpecifiedDiscriminatorType(DiscriminatorType.INTEGER); + assertNull(javaEntity().getDefaultDiscriminatorValue()); + } + + public void testGetSpecifiedDiscriminatorValue() throws Exception { + createTestEntityWithDiscriminatorValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(DISCRIMINATOR_VALUE, javaEntity().getSpecifiedDiscriminatorValue()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorValue discriminatorValue = (DiscriminatorValue) typeResource.annotation(DiscriminatorValue.ANNOTATION_NAME); + + discriminatorValue.setValue("foo"); + + assertEquals("foo", javaEntity().getSpecifiedDiscriminatorValue()); + + discriminatorValue.setValue(null); + + assertNull(javaEntity().getSpecifiedDiscriminatorValue()); + assertNull(typeResource.annotation(DiscriminatorValue.ANNOTATION_NAME)); + } + + public void testSetSpecifiedDiscriminatorValue() throws Exception { + createTestEntityWithDiscriminatorValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + assertEquals(DISCRIMINATOR_VALUE, javaEntity().getSpecifiedDiscriminatorValue()); + + javaEntity().setSpecifiedDiscriminatorValue("foo"); + + assertEquals("foo", javaEntity().getSpecifiedDiscriminatorValue()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorValue discriminatorValue = (DiscriminatorValue) typeResource.annotation(DiscriminatorValue.ANNOTATION_NAME); + assertEquals("foo", discriminatorValue.getValue()); + } + + public void testSecondaryTables() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IJavaSecondaryTable> secondaryTables = javaEntity().secondaryTables(); + + assertTrue(secondaryTables.hasNext()); + assertEquals("foo", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + } + + public void testSecondaryTablesSize() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(1, javaEntity().secondaryTablesSize()); + } + + public void testSpecifiedSecondaryTables() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IJavaSecondaryTable> specifiedSecondaryTables = javaEntity().specifiedSecondaryTables(); + + assertTrue(specifiedSecondaryTables.hasNext()); + assertEquals("foo", specifiedSecondaryTables.next().getName()); + assertEquals("bar", specifiedSecondaryTables.next().getName()); + assertFalse(specifiedSecondaryTables.hasNext()); + } + + public void testSpecifiedSecondaryTablesSize() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(2, javaEntity().specifiedSecondaryTablesSize()); + } + + public void testAddSpecifiedSecondaryTable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("BAR"); + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> secondaryTables = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + + assertEquals("BAZ", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("BAR", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("FOO", ((SecondaryTable) secondaryTables.next()).getName()); + assertFalse(secondaryTables.hasNext()); + } + + public void testAddSpecifiedSecondaryTable2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedSecondaryTable(1).setSpecifiedName("BAR"); + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> secondaryTables = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + + assertEquals("BAZ", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("FOO", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("BAR", ((SecondaryTable) secondaryTables.next()).getName()); + assertFalse(secondaryTables.hasNext()); + } + public void testRemoveSpecifiedSecondaryTable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedSecondaryTable(1).setSpecifiedName("BAR"); + javaEntity().addSpecifiedSecondaryTable(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(3, CollectionTools.size(typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME))); + + javaEntity().removeSpecifiedSecondaryTable(1); + + Iterator<JavaResource> secondaryTableResources = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertEquals("FOO", ((SecondaryTable) secondaryTableResources.next()).getName()); + assertEquals("BAZ", ((SecondaryTable) secondaryTableResources.next()).getName()); + assertFalse(secondaryTableResources.hasNext()); + + Iterator<ISecondaryTable> secondaryTables = javaEntity().secondaryTables(); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + + javaEntity().removeSpecifiedSecondaryTable(1); + secondaryTableResources = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertEquals("FOO", ((SecondaryTable) secondaryTableResources.next()).getName()); + assertFalse(secondaryTableResources.hasNext()); + + secondaryTables = javaEntity().secondaryTables(); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + + javaEntity().removeSpecifiedSecondaryTable(0); + secondaryTableResources = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertFalse(secondaryTableResources.hasNext()); + secondaryTables = javaEntity().secondaryTables(); + assertFalse(secondaryTables.hasNext()); + + assertNull(typeResource.annotation(SecondaryTables.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedSecondaryTable() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + entity.addSpecifiedSecondaryTable(1).setSpecifiedName("BAR"); + entity.addSpecifiedSecondaryTable(2).setSpecifiedName("BAZ"); + + ListIterator<SecondaryTable> javaSecondaryTables = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaSecondaryTables)); + + + entity.moveSpecifiedSecondaryTable(2, 0); + ListIterator<ISecondaryTable> secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAR", secondaryTables.next().getSpecifiedName()); + assertEquals("BAZ", secondaryTables.next().getSpecifiedName()); + assertEquals("FOO", secondaryTables.next().getSpecifiedName()); + + javaSecondaryTables = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertEquals("BAR", javaSecondaryTables.next().getName()); + assertEquals("BAZ", javaSecondaryTables.next().getName()); + assertEquals("FOO", javaSecondaryTables.next().getName()); + + + entity.moveSpecifiedSecondaryTable(0, 1); + secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getSpecifiedName()); + assertEquals("BAR", secondaryTables.next().getSpecifiedName()); + assertEquals("FOO", secondaryTables.next().getSpecifiedName()); + + javaSecondaryTables = typeResource.annotations(SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + assertEquals("BAZ", javaSecondaryTables.next().getName()); + assertEquals("BAR", javaSecondaryTables.next().getName()); + assertEquals("FOO", javaSecondaryTables.next().getName()); + } + + public void testUpdateSpecifiedSecondaryTables() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((SecondaryTable) typeResource.addAnnotation(0, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME)).setName("FOO"); + ((SecondaryTable) typeResource.addAnnotation(1, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME)).setName("BAR"); + ((SecondaryTable) typeResource.addAnnotation(2, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<ISecondaryTable> secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + typeResource.move(2, 0, SecondaryTables.ANNOTATION_NAME); + secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + typeResource.move(0, 1, SecondaryTables.ANNOTATION_NAME); + secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + typeResource.removeAnnotation(1, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + typeResource.removeAnnotation(1, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + secondaryTables = entity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + typeResource.removeAnnotation(0, SecondaryTable.ANNOTATION_NAME, SecondaryTables.ANNOTATION_NAME); + secondaryTables = entity.specifiedSecondaryTables(); + assertFalse(secondaryTables.hasNext()); + } + + public void testAssociatedTables() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(3, CollectionTools.size(javaEntity().associatedTables())); + Iterator<ITable> associatedTables = javaEntity().associatedTables(); + ITable table1 = associatedTables.next(); + ISecondaryTable table2 = (ISecondaryTable) associatedTables.next(); + ISecondaryTable table3 = (ISecondaryTable) associatedTables.next(); + assertEquals(TYPE_NAME, table1.getName()); + assertEquals("foo", table2.getName()); + assertEquals("bar", table3.getName()); + } + + public void testAssociatedTablesIncludingInherited() throws Exception { + createTestEntityWithSecondaryTables(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity parentEntity = javaEntity().rootEntity(); + assertEquals(3, CollectionTools.size(parentEntity.associatedTablesIncludingInherited())); + Iterator<ITable> associatedTables = parentEntity.associatedTablesIncludingInherited(); + ITable table1 = associatedTables.next(); + ISecondaryTable table2 = (ISecondaryTable) associatedTables.next(); + ISecondaryTable table3 = (ISecondaryTable) associatedTables.next(); + assertEquals(TYPE_NAME, table1.getName()); + assertEquals("foo", table2.getName()); + assertEquals("bar", table3.getName()); + + IEntity childEntity = javaEntity(); + //TODO probably want this to be 3, since in this case the child descriptor really uses the + //parent table because it is single table inheritance strategy. Not sure yet how to deal with this. + assertEquals(4, CollectionTools.size(childEntity.associatedTablesIncludingInherited())); + } + + public void testAssociatedTableNamesIncludingInherited() throws Exception { + createTestEntityWithSecondaryTables(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity parentEntity = javaEntity().rootEntity(); + assertEquals(3, CollectionTools.size(parentEntity.associatedTableNamesIncludingInherited())); + Iterator<String> associatedTables = parentEntity.associatedTableNamesIncludingInherited(); + String table1 = associatedTables.next(); + String table2 = associatedTables.next(); + String table3 = associatedTables.next(); + assertEquals(TYPE_NAME, table1); + assertEquals("foo", table2); + assertEquals("bar", table3); + + IEntity childEntity = javaEntity(); + //TODO probably want this to be 3, since in this case the child descriptor really uses the + //parent table because it is single table inheritance strategy. Not sure yet how to deal with this. + assertEquals(4, CollectionTools.size(childEntity.associatedTableNamesIncludingInherited())); + } + + public void testAddSecondaryTableToResourceModel() throws Exception { + createTestEntityWithName(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable secondaryTable = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable.setName("FOO"); + + assertEquals(1, javaEntity().secondaryTablesSize()); + assertEquals("FOO", javaEntity().secondaryTables().next().getSpecifiedName()); + assertEquals("FOO", javaEntity().secondaryTables().next().getName()); + + SecondaryTable secondaryTable2 = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable2.setName("BAR"); + + assertEquals(2, javaEntity().secondaryTablesSize()); + ListIterator<ISecondaryTable> secondaryTables = javaEntity().secondaryTables(); + assertEquals("FOO", secondaryTables.next().getSpecifiedName()); + assertEquals("BAR", secondaryTables.next().getSpecifiedName()); + + SecondaryTable secondaryTable3 = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable3.setName("BAZ"); + + assertEquals(3, javaEntity().secondaryTablesSize()); + secondaryTables = javaEntity().secondaryTables(); + assertEquals("BAZ", secondaryTables.next().getSpecifiedName()); + assertEquals("FOO", secondaryTables.next().getSpecifiedName()); + assertEquals("BAR", secondaryTables.next().getSpecifiedName()); + } + + public void testRemoveSecondaryTableFromResourceModel() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedSecondaryTable(2).setSpecifiedName("baz"); + ListIterator<ISecondaryTable> secondaryTables = javaEntity().secondaryTables(); + + assertEquals(3, javaEntity().secondaryTablesSize()); + assertEquals("foo", secondaryTables.next().getSpecifiedName()); + assertEquals("bar", secondaryTables.next().getSpecifiedName()); + assertEquals("baz", secondaryTables.next().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + secondaryTables = javaEntity().secondaryTables(); + assertEquals(2, javaEntity().secondaryTablesSize()); + assertEquals("bar", secondaryTables.next().getSpecifiedName()); + assertEquals("baz", secondaryTables.next().getSpecifiedName()); + + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + secondaryTables = javaEntity().secondaryTables(); + assertEquals(1, javaEntity().secondaryTablesSize()); + assertEquals("baz", secondaryTables.next().getSpecifiedName()); + + + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + secondaryTables = javaEntity().secondaryTables(); + assertEquals(0, javaEntity().secondaryTablesSize()); + assertFalse(secondaryTables.hasNext()); + } + + public void testGetSequenceGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getSequenceGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.addAnnotation(JPA.SEQUENCE_GENERATOR); + + assertNotNull(javaEntity().getSequenceGenerator()); + assertEquals(1, CollectionTools.size(typeResource.annotations())); + } + + public void testAddSequenceGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getSequenceGenerator()); + + javaEntity().addSequenceGenerator(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertNotNull(typeResource.annotation(JPA.SEQUENCE_GENERATOR)); + assertNotNull(javaEntity().getSequenceGenerator()); + + //try adding another sequence generator, should get an IllegalStateException + try { + javaEntity().addSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveSequenceGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.addAnnotation(JPA.SEQUENCE_GENERATOR); + + javaEntity().removeSequenceGenerator(); + + assertNull(javaEntity().getSequenceGenerator()); + assertNull(typeResource.annotation(JPA.SEQUENCE_GENERATOR)); + + //try removing the sequence generator again, should get an IllegalStateException + try { + javaEntity().removeSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testGetTableGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getTableGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.addAnnotation(JPA.TABLE_GENERATOR); + + assertNotNull(javaEntity().getTableGenerator()); + assertEquals(1, CollectionTools.size(typeResource.annotations())); + } + + public void testAddTableGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getTableGenerator()); + + javaEntity().addTableGenerator(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertNotNull(typeResource.annotation(JPA.TABLE_GENERATOR)); + assertNotNull(javaEntity().getTableGenerator()); + + //try adding another table generator, should get an IllegalStateException + try { + javaEntity().addTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveTableGenerator() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.addAnnotation(JPA.TABLE_GENERATOR); + + javaEntity().removeTableGenerator(); + + assertNull(javaEntity().getTableGenerator()); + assertNull(typeResource.annotation(JPA.TABLE_GENERATOR)); + + //try removing the table generator again, should get an IllegalStateException + try { + javaEntity().removeTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testGetDiscriminatorColumn() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNotNull(javaEntity().getDiscriminatorColumn()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.addAnnotation(JPA.DISCRIMINATOR_COLUMN); + column.setName("foo"); + + assertEquals("foo", javaEntity().getDiscriminatorColumn().getSpecifiedName()); + + column.setName(null); + + assertNull(javaEntity().getDiscriminatorColumn().getSpecifiedName()); + + typeResource.removeAnnotation(JPA.DISCRIMINATOR_COLUMN); + + assertNotNull(javaEntity().getDiscriminatorColumn()); + } + + public void testSpecifiedPrimaryKeyJoinColumns() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IJavaPrimaryKeyJoinColumn> specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + + assertFalse(specifiedPkJoinColumns.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + //add an annotation to the resource model and verify the context model is updated + PrimaryKeyJoinColumn pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.addAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + pkJoinColumn.setName("FOO"); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.addAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + pkJoinColumn.setName("BAR"); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + + pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.addAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + pkJoinColumn.setName("BAZ"); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + typeResource.move(1, 0, JPA.PRIMARY_KEY_JOIN_COLUMNS); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + typeResource.removeAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + typeResource.removeAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + + typeResource.removeAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + specifiedPkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertFalse(specifiedPkJoinColumns.hasNext()); + } + + public void testDefaultPrimaryKeyJoinColumns() { + //TODO + } + + public void testSpecifiedPrimaryKeyJoinColumnsSize() throws Exception { + createTestEntityWithPrimaryKeyJoinColumns(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(2, javaEntity().specifiedPrimaryKeyJoinColumnsSize()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAR"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> pkJoinColumns = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + + assertEquals("BAZ", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("BAR", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("FOO", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertFalse(pkJoinColumns.hasNext()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> pkJoinColumns = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + + assertEquals("FOO", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("BAR", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("BAZ", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertFalse(pkJoinColumns.hasNext()); + } + public void testRemoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + javaEntity().addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(3, CollectionTools.size(typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME))); + + javaEntity().removeSpecifiedPrimaryKeyJoinColumn(1); + + Iterator<JavaResource> pkJoinColumnResources = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((PrimaryKeyJoinColumn) pkJoinColumnResources.next()).getName()); + assertEquals("BAZ", ((PrimaryKeyJoinColumn) pkJoinColumnResources.next()).getName()); + assertFalse(pkJoinColumnResources.hasNext()); + + Iterator<IPrimaryKeyJoinColumn> pkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", pkJoinColumns.next().getName()); + assertEquals("BAZ", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + + + javaEntity().removeSpecifiedPrimaryKeyJoinColumn(1); + pkJoinColumnResources = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((PrimaryKeyJoinColumn) pkJoinColumnResources.next()).getName()); + assertFalse(pkJoinColumnResources.hasNext()); + + pkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + + + javaEntity().removeSpecifiedPrimaryKeyJoinColumn(0); + pkJoinColumnResources = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertFalse(pkJoinColumnResources.hasNext()); + pkJoinColumns = javaEntity().specifiedPrimaryKeyJoinColumns(); + assertFalse(pkJoinColumns.hasNext()); + + assertNull(typeResource.annotation(PrimaryKeyJoinColumns.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + entity.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + entity.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + ListIterator<PrimaryKeyJoinColumn> javaPrimaryKeyJoinColumns = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaPrimaryKeyJoinColumns)); + + + entity.moveSpecifiedPrimaryKeyJoinColumn(2, 0); + ListIterator<IPrimaryKeyJoinColumn> primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaPrimaryKeyJoinColumns = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertEquals("BAR", javaPrimaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", javaPrimaryKeyJoinColumns.next().getName()); + assertEquals("FOO", javaPrimaryKeyJoinColumns.next().getName()); + + + entity.moveSpecifiedPrimaryKeyJoinColumn(0, 1); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaPrimaryKeyJoinColumns = typeResource.annotations(PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + assertEquals("BAZ", javaPrimaryKeyJoinColumns.next().getName()); + assertEquals("BAR", javaPrimaryKeyJoinColumns.next().getName()); + assertEquals("FOO", javaPrimaryKeyJoinColumns.next().getName()); + } + + public void testUpdateSpecifiedPrimaryKeyJoinColumns() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((PrimaryKeyJoinColumn) typeResource.addAnnotation(0, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME)).setName("FOO"); + ((PrimaryKeyJoinColumn) typeResource.addAnnotation(1, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME)).setName("BAR"); + ((PrimaryKeyJoinColumn) typeResource.addAnnotation(2, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<IPrimaryKeyJoinColumn> primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + typeResource.move(2, 0, PrimaryKeyJoinColumns.ANNOTATION_NAME); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + typeResource.move(0, 1, PrimaryKeyJoinColumns.ANNOTATION_NAME); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + typeResource.removeAnnotation(1, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + typeResource.removeAnnotation(1, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + typeResource.removeAnnotation(0, PrimaryKeyJoinColumn.ANNOTATION_NAME, PrimaryKeyJoinColumns.ANNOTATION_NAME); + primaryKeyJoinColumns = entity.specifiedPrimaryKeyJoinColumns(); + assertFalse(primaryKeyJoinColumns.hasNext()); + } + + public void testPrimaryKeyJoinColumnIsVirtual() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedPrimaryKeyJoinColumn(0); + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertFalse(specifiedPkJoinColumn.isVirtual()); + + IPrimaryKeyJoinColumn defaultPkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertTrue(defaultPkJoinColumn.isVirtual()); + } + + public void testOverridableAttributeNames() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + Iterator<String> overridableAttributeNames = javaEntity().overridableAttributeNames(); + assertFalse(overridableAttributeNames.hasNext()); + } + + public void testAllOverridableAttributeNames() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + Iterator<String> overridableAttributeNames = javaEntity().allOverridableAttributeNames(); + assertEquals("id", overridableAttributeNames.next()); + assertEquals("name", overridableAttributeNames.next()); + assertFalse(overridableAttributeNames.hasNext()); + } + + public void testOverridableAssociationNames() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + Iterator<String> overridableAssociationNames = javaEntity().overridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + //TODO add some associations to the MappedSuperclass + //add all mapping types to test which ones are overridable + public void testAllOverridableAssociationNames() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + Iterator<String> overridableAssociationNames = javaPersistentType().parentPersistentType().getMapping().allOverridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + + public void testTableNameIsInvalid() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertFalse(javaEntity().tableNameIsInvalid(TYPE_NAME)); + assertTrue(javaEntity().tableNameIsInvalid("FOO")); + + javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("BAR"); + + assertFalse(javaEntity().tableNameIsInvalid("BAR")); + } + + public void testAttributeMappingKeyAllowed() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = (IEntity) javaPersistentType().getMapping(); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + assertTrue(entity.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + } + + public void testSpecifiedAttributeOverrides() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IJavaAttributeOverride> specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + + assertFalse(specifiedAttributeOverrides.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) typeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + attributeOverride = (AttributeOverride) typeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + attributeOverride = (AttributeOverride) typeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + typeResource.move(1, 0, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("FOO", specifiedAttributeOverrides.next().getName()); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + typeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("BAZ", specifiedAttributeOverrides.next().getName()); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + typeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("BAR", specifiedAttributeOverrides.next().getName()); + assertFalse(specifiedAttributeOverrides.hasNext()); + + + typeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + specifiedAttributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertFalse(specifiedAttributeOverrides.hasNext()); + } + + public void testDefaultAttributeOverrides() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_SUB_TYPE_NAME); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IJavaEntity javaEntity = (IJavaEntity) classRefs.next().getJavaPersistentType().getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_SUB_TYPE_NAME); + assertEquals(SUB_TYPE_NAME, typeResource.getName()); + assertNull(typeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + assertEquals(2, CollectionTools.size(javaEntity.defaultAttributeOverrides())); + IAttributeOverride defaultAttributeOverride = javaEntity.defaultAttributeOverrides().next(); + assertEquals("id", defaultAttributeOverride.getName()); + assertEquals("id", defaultAttributeOverride.getColumn().getName()); + assertEquals(SUB_TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + IBasicMapping idMapping = (IBasicMapping) mappedSuperclass.persistentType().attributeNamed("id").getMapping(); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.getColumn().setSpecifiedTable("BAR"); + + assertEquals(SUB_TYPE_NAME, typeResource.getName()); + assertNull(typeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + assertEquals(2, CollectionTools.size(javaEntity.defaultAttributeOverrides())); + defaultAttributeOverride = javaEntity.defaultAttributeOverrides().next(); + assertEquals("id", defaultAttributeOverride.getName()); + assertEquals("FOO", defaultAttributeOverride.getColumn().getName()); + assertEquals("BAR", defaultAttributeOverride.getColumn().getTable()); + + idMapping.getColumn().setSpecifiedName(null); + idMapping.getColumn().setSpecifiedTable(null); + assertEquals(SUB_TYPE_NAME, typeResource.getName()); + assertNull(typeResource.annotation(AttributeOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + + defaultAttributeOverride = javaEntity.defaultAttributeOverrides().next(); + assertEquals("id", defaultAttributeOverride.getName()); + assertEquals("id", defaultAttributeOverride.getColumn().getName()); + assertEquals(SUB_TYPE_NAME, defaultAttributeOverride.getColumn().getTable()); + + javaEntity.addSpecifiedAttributeOverride(0).setName("id"); + assertEquals(1, CollectionTools.size(javaEntity.defaultAttributeOverrides())); + } + + public void testSpecifiedAttributeOverridesSize() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(0, javaEntity().specifiedAttributeOverridesSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + //add an annotation to the resource model and verify the context model is updated + AttributeOverride attributeOverride = (AttributeOverride) typeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + attributeOverride = (AttributeOverride) typeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + + assertEquals(2, javaEntity().specifiedAttributeOverridesSize()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedAttributeOverride(0).setName("FOO"); + javaEntity().addSpecifiedAttributeOverride(0).setName("BAR"); + javaEntity().addSpecifiedAttributeOverride(0).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> attributeOverrides = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testAddSpecifiedAttributeOverride2() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedAttributeOverride(0).setName("FOO"); + javaEntity().addSpecifiedAttributeOverride(1).setName("BAR"); + javaEntity().addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Iterator<JavaResource> attributeOverrides = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertFalse(attributeOverrides.hasNext()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().addSpecifiedAttributeOverride(0).setName("FOO"); + javaEntity().addSpecifiedAttributeOverride(1).setName("BAR"); + javaEntity().addSpecifiedAttributeOverride(2).setName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(3, CollectionTools.size(typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME))); + + javaEntity().removeSpecifiedAttributeOverride(1); + + Iterator<JavaResource> attributeOverrideResources = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertEquals("BAZ", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + Iterator<IAttributeOverride> attributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + javaEntity().removeSpecifiedAttributeOverride(1); + attributeOverrideResources = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("FOO", ((AttributeOverride) attributeOverrideResources.next()).getName()); + assertFalse(attributeOverrideResources.hasNext()); + + attributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + + javaEntity().removeSpecifiedAttributeOverride(0); + attributeOverrideResources = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertFalse(attributeOverrideResources.hasNext()); + attributeOverrides = javaEntity().specifiedAttributeOverrides(); + assertFalse(attributeOverrides.hasNext()); + + assertNull(typeResource.annotation(AttributeOverrides.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addSpecifiedAttributeOverride(0).setName("FOO"); + entity.addSpecifiedAttributeOverride(1).setName("BAR"); + entity.addSpecifiedAttributeOverride(2).setName("BAZ"); + + ListIterator<AttributeOverride> javaAttributeOverrides = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaAttributeOverrides)); + + + entity.moveSpecifiedAttributeOverride(2, 0); + ListIterator<IAttributeOverride> attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + javaAttributeOverrides = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("BAR", javaAttributeOverrides.next().getName()); + assertEquals("BAZ", javaAttributeOverrides.next().getName()); + assertEquals("FOO", javaAttributeOverrides.next().getName()); + + + entity.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + javaAttributeOverrides = typeResource.annotations(AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + assertEquals("BAZ", javaAttributeOverrides.next().getName()); + assertEquals("BAR", javaAttributeOverrides.next().getName()); + assertEquals("FOO", javaAttributeOverrides.next().getName()); + } + + public void testUpdateSpecifiedAttributeOverrides() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((AttributeOverride) typeResource.addAnnotation(0, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME)).setName("FOO"); + ((AttributeOverride) typeResource.addAnnotation(1, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME)).setName("BAR"); + ((AttributeOverride) typeResource.addAnnotation(2, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<IAttributeOverride> attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + typeResource.move(2, 0, AttributeOverrides.ANNOTATION_NAME); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + typeResource.move(0, 1, AttributeOverrides.ANNOTATION_NAME); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + typeResource.removeAnnotation(1, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + typeResource.removeAnnotation(1, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + typeResource.removeAnnotation(0, AttributeOverride.ANNOTATION_NAME, AttributeOverrides.ANNOTATION_NAME); + attributeOverrides = entity.specifiedAttributeOverrides(); + assertFalse(attributeOverrides.hasNext()); + } + + public void testAttributeOverrideIsVirtual() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IAttributeOverride> defaultAttributeOverrides = javaEntity().defaultAttributeOverrides(); + IAttributeOverride defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("id", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("name", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + + javaEntity().addSpecifiedAttributeOverride(0).setName("id"); + IAttributeOverride specifiedAttributeOverride = javaEntity().specifiedAttributeOverrides().next(); + assertFalse(specifiedAttributeOverride.isVirtual()); + + + defaultAttributeOverrides = javaEntity().defaultAttributeOverrides(); + defaultAttributeOverride = defaultAttributeOverrides.next(); + assertEquals("name", defaultAttributeOverride.getName()); + assertTrue(defaultAttributeOverride.isVirtual()); + assertFalse(defaultAttributeOverrides.hasNext()); + } + + public void testAddNamedQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + INamedQuery namedQuery = entity.addNamedQuery(0); + namedQuery.setName("FOO"); + + ListIterator<NamedQuery> javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("FOO", javaNamedQueries.next().getName()); + + INamedQuery namedQuery2 = entity.addNamedQuery(0); + namedQuery2.setName("BAR"); + + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + INamedQuery namedQuery3 = entity.addNamedQuery(1); + namedQuery3.setName("BAZ"); + + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + ListIterator<INamedQuery> namedQueries = entity.namedQueries(); + assertEquals(namedQuery2, namedQueries.next()); + assertEquals(namedQuery3, namedQueries.next()); + assertEquals(namedQuery, namedQueries.next()); + + namedQueries = entity.namedQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + } + + public void testRemoveNamedQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addNamedQuery(0).setName("FOO"); + entity.addNamedQuery(1).setName("BAR"); + entity.addNamedQuery(2).setName("BAZ"); + + ListIterator<NamedQuery> javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaNamedQueries)); + + entity.removeNamedQuery(0); + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals(2, CollectionTools.size(javaNamedQueries)); + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + + entity.removeNamedQuery(0); + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals(1, CollectionTools.size(javaNamedQueries)); + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAZ", javaNamedQueries.next().getName()); + + entity.removeNamedQuery(0); + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals(0, CollectionTools.size(javaNamedQueries)); + } + + public void testMoveNamedQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addNamedQuery(0).setName("FOO"); + entity.addNamedQuery(1).setName("BAR"); + entity.addNamedQuery(2).setName("BAZ"); + + ListIterator<NamedQuery> javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaNamedQueries)); + + + entity.moveNamedQuery(2, 0); + ListIterator<INamedQuery> namedQueries = entity.namedQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + + entity.moveNamedQuery(0, 1); + namedQueries = entity.namedQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + + javaNamedQueries = typeResource.annotations(NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + } + + public void testUpdateNamedQueries() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((NamedQuery) typeResource.addAnnotation(0, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME)).setName("FOO"); + ((NamedQuery) typeResource.addAnnotation(1, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME)).setName("BAR"); + ((NamedQuery) typeResource.addAnnotation(2, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<INamedQuery> namedQueries = entity.namedQueries(); + assertEquals("FOO", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.move(2, 0, NamedQueries.ANNOTATION_NAME); + namedQueries = entity.namedQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.move(0, 1, NamedQueries.ANNOTATION_NAME); + namedQueries = entity.namedQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(1, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + namedQueries = entity.namedQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(1, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + namedQueries = entity.namedQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(0, NamedQuery.ANNOTATION_NAME, NamedQueries.ANNOTATION_NAME); + namedQueries = entity.namedQueries(); + assertFalse(namedQueries.hasNext()); + } + + public void testAddNamedNativeQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + INamedNativeQuery namedNativeQuery = entity.addNamedNativeQuery(0); + namedNativeQuery.setName("FOO"); + + ListIterator<NamedNativeQuery> javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("FOO", javaNamedQueries.next().getName()); + + INamedNativeQuery namedNativeQuery2 = entity.addNamedNativeQuery(0); + namedNativeQuery2.setName("BAR"); + + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + INamedNativeQuery namedNativeQuery3 = entity.addNamedNativeQuery(1); + namedNativeQuery3.setName("BAZ"); + + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + ListIterator<INamedNativeQuery> namedQueries = entity.namedNativeQueries(); + assertEquals(namedNativeQuery2, namedQueries.next()); + assertEquals(namedNativeQuery3, namedQueries.next()); + assertEquals(namedNativeQuery, namedQueries.next()); + + namedQueries = entity.namedNativeQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + } + + public void testRemoveNamedNativeQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addNamedNativeQuery(0).setName("FOO"); + entity.addNamedNativeQuery(1).setName("BAR"); + entity.addNamedNativeQuery(2).setName("BAZ"); + + ListIterator<NamedNativeQuery> javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaNamedQueries)); + + entity.removeNamedNativeQuery(0); + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals(2, CollectionTools.size(javaNamedQueries)); + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + + entity.removeNamedNativeQuery(0); + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals(1, CollectionTools.size(javaNamedQueries)); + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAZ", javaNamedQueries.next().getName()); + + entity.removeNamedNativeQuery(0); + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals(0, CollectionTools.size(javaNamedQueries)); + } + + public void testMoveNamedNativeQuery() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addNamedNativeQuery(0).setName("FOO"); + entity.addNamedNativeQuery(1).setName("BAR"); + entity.addNamedNativeQuery(2).setName("BAZ"); + + ListIterator<NamedNativeQuery> javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaNamedQueries)); + + + entity.moveNamedNativeQuery(2, 0); + ListIterator<INamedNativeQuery> namedQueries = entity.namedNativeQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + + + entity.moveNamedNativeQuery(0, 1); + namedQueries = entity.namedNativeQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + + javaNamedQueries = typeResource.annotations(NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + assertEquals("BAZ", javaNamedQueries.next().getName()); + assertEquals("BAR", javaNamedQueries.next().getName()); + assertEquals("FOO", javaNamedQueries.next().getName()); + } + + public void testUpdateNamedNativeQueries() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((NamedNativeQuery) typeResource.addAnnotation(0, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME)).setName("FOO"); + ((NamedNativeQuery) typeResource.addAnnotation(1, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME)).setName("BAR"); + ((NamedNativeQuery) typeResource.addAnnotation(2, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<INamedNativeQuery> namedQueries = entity.namedNativeQueries(); + assertEquals("FOO", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.move(2, 0, NamedNativeQueries.ANNOTATION_NAME); + namedQueries = entity.namedNativeQueries(); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.move(0, 1, NamedNativeQueries.ANNOTATION_NAME); + namedQueries = entity.namedNativeQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("BAR", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(1, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + namedQueries = entity.namedNativeQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertEquals("FOO", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(1, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + namedQueries = entity.namedNativeQueries(); + assertEquals("BAZ", namedQueries.next().getName()); + assertFalse(namedQueries.hasNext()); + + typeResource.removeAnnotation(0, NamedNativeQuery.ANNOTATION_NAME, NamedNativeQueries.ANNOTATION_NAME); + namedQueries = entity.namedNativeQueries(); + assertFalse(namedQueries.hasNext()); + } + + public void testAddSpecifiedAssociationOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + IAssociationOverride associationOverride = entity.addSpecifiedAssociationOverride(0); + associationOverride.setName("FOO"); + + ListIterator<AssociationOverride> javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("FOO", javaAssociationOverrides.next().getName()); + + IAssociationOverride associationOverride2 = entity.addSpecifiedAssociationOverride(0); + associationOverride2.setName("BAR"); + + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAR", javaAssociationOverrides.next().getName()); + assertEquals("FOO", javaAssociationOverrides.next().getName()); + + IAssociationOverride associationOverride3 = entity.addSpecifiedAssociationOverride(1); + associationOverride3.setName("BAZ"); + + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAR", javaAssociationOverrides.next().getName()); + assertEquals("BAZ", javaAssociationOverrides.next().getName()); + assertEquals("FOO", javaAssociationOverrides.next().getName()); + + ListIterator<IAssociationOverride> associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals(associationOverride2, associationOverrides.next()); + assertEquals(associationOverride3, associationOverrides.next()); + assertEquals(associationOverride, associationOverrides.next()); + + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + } + + public void testRemoveSpecifiedAssociationOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addSpecifiedAssociationOverride(0).setName("FOO"); + entity.addSpecifiedAssociationOverride(1).setName("BAR"); + entity.addSpecifiedAssociationOverride(2).setName("BAZ"); + + ListIterator<AssociationOverride> javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaAssociationOverrides)); + + entity.removeSpecifiedAssociationOverride(0); + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals(2, CollectionTools.size(javaAssociationOverrides)); + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAR", javaAssociationOverrides.next().getName()); + assertEquals("BAZ", javaAssociationOverrides.next().getName()); + + entity.removeSpecifiedAssociationOverride(0); + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals(1, CollectionTools.size(javaAssociationOverrides)); + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAZ", javaAssociationOverrides.next().getName()); + + entity.removeSpecifiedAssociationOverride(0); + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals(0, CollectionTools.size(javaAssociationOverrides)); + } + + public void testMoveSpecifiedAssociationOverride() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + entity.addSpecifiedAssociationOverride(0).setName("FOO"); + entity.addSpecifiedAssociationOverride(1).setName("BAR"); + entity.addSpecifiedAssociationOverride(2).setName("BAZ"); + + ListIterator<AssociationOverride> javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaAssociationOverrides)); + + + entity.moveSpecifiedAssociationOverride(2, 0); + ListIterator<IAssociationOverride> associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAR", javaAssociationOverrides.next().getName()); + assertEquals("BAZ", javaAssociationOverrides.next().getName()); + assertEquals("FOO", javaAssociationOverrides.next().getName()); + + + entity.moveSpecifiedAssociationOverride(0, 1); + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + + javaAssociationOverrides = typeResource.annotations(AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + assertEquals("BAZ", javaAssociationOverrides.next().getName()); + assertEquals("BAR", javaAssociationOverrides.next().getName()); + assertEquals("FOO", javaAssociationOverrides.next().getName()); + } + + public void testUpdateSpecifiedAssociationOverrides() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + ((AssociationOverride) typeResource.addAnnotation(0, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME)).setName("FOO"); + ((AssociationOverride) typeResource.addAnnotation(1, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME)).setName("BAR"); + ((AssociationOverride) typeResource.addAnnotation(2, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<IAssociationOverride> associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("FOO", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + typeResource.move(2, 0, AssociationOverrides.ANNOTATION_NAME); + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + typeResource.move(0, 1, AssociationOverrides.ANNOTATION_NAME); + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + typeResource.removeAnnotation(1, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + typeResource.removeAnnotation(1, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + associationOverrides = entity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + typeResource.removeAnnotation(0, AssociationOverride.ANNOTATION_NAME, AssociationOverrides.ANNOTATION_NAME); + associationOverrides = entity.specifiedAssociationOverrides(); + assertFalse(associationOverrides.hasNext()); + } + + public void testAssociationOverrideIsVirtual() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IAssociationOverride> defaultAssociationOverrides = javaEntity().defaultAssociationOverrides(); + IAssociationOverride defaultAssociationOverride = defaultAssociationOverrides.next(); + assertEquals("address", defaultAssociationOverride.getName()); + assertTrue(defaultAssociationOverride.isVirtual()); + assertFalse(defaultAssociationOverrides.hasNext()); + + javaEntity().addSpecifiedAssociationOverride(0).setName("address"); + IAssociationOverride specifiedAssociationOverride = javaEntity().specifiedAssociationOverrides().next(); + assertFalse(specifiedAssociationOverride.isVirtual()); + + defaultAssociationOverrides = javaEntity().defaultAssociationOverrides(); + assertFalse(defaultAssociationOverrides.hasNext()); + } + + public void testDefaultAssociationOverrides() throws Exception { + createTestMappedSuperclass(); + createTestSubType(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(FULLY_QUALIFIED_SUB_TYPE_NAME); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IJavaEntity javaEntity = (IJavaEntity) classRefs.next().getJavaPersistentType().getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_SUB_TYPE_NAME); + assertEquals(SUB_TYPE_NAME, typeResource.getName()); + assertNull(typeResource.annotation(AssociationOverride.ANNOTATION_NAME)); + assertNull(typeResource.annotation(AssociationOverrides.ANNOTATION_NAME)); + + assertEquals(1, CollectionTools.size(javaEntity.defaultAssociationOverrides())); + IAssociationOverride defaultAssociationOverride = javaEntity.defaultAssociationOverrides().next(); + assertEquals("address", defaultAssociationOverride.getName()); + //TODO joinColumns for default association overrides +// IJoinColumn defaultJoinColumn = defaultAssociationOverride.joinColumns().next(); +// assertEquals("address", defaultJoinColumn.getName()); +// assertEquals("address", defaultJoinColumn.getReferencedColumnName()); +// assertEquals(SUB_TYPE_NAME, defaultJoinColumn.getTable()); +// +// +// IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); +// +// IOneToOneMapping addressMapping = (IOneToOneMapping) mappedSuperclass.persistentType().attributeNamed("address").getMapping(); +// IJoinColumn joinColumn = addressMapping.addSpecifiedJoinColumn(0); +// joinColumn.setSpecifiedName("FOO"); +// joinColumn.setSpecifiedReferencedColumnName("BAR"); +// joinColumn.setSpecifiedTable("BAZ"); +// +// assertEquals(SUB_TYPE_NAME, typeResource.getName()); +// assertNull(typeResource.annotation(AssociationOverride.ANNOTATION_NAME)); +// assertNull(typeResource.annotation(AssociationOverrides.ANNOTATION_NAME)); +// +// assertEquals(1, CollectionTools.size(javaEntity.defaultAssociationOverrides())); +// defaultAssociationOverride = javaEntity.defaultAssociationOverrides().next(); +// assertEquals("address", defaultAssociationOverride.getName()); +// assertEquals("FOO", defaultJoinColumn.getName()); +// assertEquals("BAR", defaultJoinColumn.getReferencedColumnName()); +// assertEquals("BAZ", defaultJoinColumn.getTable()); +// +// joinColumn.setSpecifiedName(null); +// joinColumn.setSpecifiedReferencedColumnName(null); +// joinColumn.setSpecifiedTable(null); +// assertEquals(SUB_TYPE_NAME, typeResource.getName()); +// assertNull(typeResource.annotation(AssociationOverride.ANNOTATION_NAME)); +// assertNull(typeResource.annotation(AssociationOverrides.ANNOTATION_NAME)); +// +// defaultAssociationOverride = javaEntity.defaultAssociationOverrides().next(); +// assertEquals("address", defaultJoinColumn.getName()); +// assertEquals("address", defaultJoinColumn.getReferencedColumnName()); +// assertEquals(SUB_TYPE_NAME, defaultJoinColumn.getTable()); +// +// javaEntity.addSpecifiedAssociationOverride(0).setName("address"); +// assertEquals(0, CollectionTools.size(javaEntity.defaultAssociationOverrides())); + } + + public void testSpecifiedAssociationOverrides() { + //TODO + } + + public void testUpdateIdClass() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + IdClass idClass = (IdClass) typeResource.addAnnotation(IdClass.ANNOTATION_NAME); + assertNull(javaEntity().getIdClass()); + assertNotNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + idClass.setValue("model.Foo"); + assertEquals("model.Foo", javaEntity().getIdClass()); + assertEquals("model.Foo", ((IdClass) typeResource.annotation(IdClass.ANNOTATION_NAME)).getValue()); + + //test setting @IdClass value to null, IdClass annotation is removed + idClass.setValue(null); + assertNull(javaEntity().getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + //reset @IdClass value and then remove @IdClass + idClass = (IdClass) typeResource.addAnnotation(IdClass.ANNOTATION_NAME); + idClass.setValue("model.Foo"); + typeResource.removeAnnotation(IdClass.ANNOTATION_NAME); + + assertNull(javaEntity().getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + } + + public void testModifyIdClass() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + javaEntity().setIdClass("model.Foo"); + assertEquals("model.Foo", ((IdClass) typeResource.annotation(IdClass.ANNOTATION_NAME)).getValue()); + assertEquals("model.Foo", javaEntity().getIdClass()); + + javaEntity().setIdClass(null); + assertNull(javaEntity().getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + } + +// Iterator<String> allOverridableAttributeNames(); +// +// Iterator<String> allOverridableAssociationNames(); + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaGeneratedValueTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaGeneratedValueTests.java new file mode 100644 index 0000000000..58e4a63f29 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaGeneratedValueTests.java @@ -0,0 +1,160 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.GenerationType; +import org.eclipse.jpt.core.internal.context.base.IGeneratedValue; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.resource.java.GeneratedValue; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaGeneratedValueTests extends ContextModelTestCase +{ + private static final String GENERATOR = "MY_GENERATOR"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createGeneratedValueAnnotation() throws Exception{ + this.createAnnotationAndMembers("GeneratedValue", + "GenerationType strategy() default;" + + "String generator() default \"\"; "); + } + + private IType createTestEntityWithGeneratedValue() throws Exception { + createEntityAnnotation(); + createGeneratedValueAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.GENERATED_VALUE, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + sb.append("@GeneratedValue(generator=\"" + GENERATOR + "\")"); + } + }); + } + + public JavaGeneratedValueTests(String name) { + super(name); + } + + public void testGetGenerator() throws Exception { + createTestEntityWithGeneratedValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(GENERATOR, idMapping.getGeneratedValue().getGenerator()); + + //change resource model sequenceGenerator name, verify the context model is updated + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + + generatedValue.setGenerator("foo"); + + assertEquals("foo", idMapping.getGeneratedValue().getGenerator()); + } + + public void testSetSpecifiedGenerator() throws Exception { + createTestEntityWithGeneratedValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(GENERATOR, idMapping.getGeneratedValue().getGenerator()); + + idMapping.getGeneratedValue().setSpecifiedGenerator("foo"); + + assertEquals("foo", idMapping.getGeneratedValue().getGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + + assertEquals("foo", generatedValue.getGenerator()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithGeneratedValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(GENERATOR, idMapping.getGeneratedValue().getGenerator()); + + idMapping.getGeneratedValue().setSpecifiedGenerator(null); + + assertNotNull(idMapping.getGeneratedValue()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + + assertNotNull(generatedValue); + } + + public void testGetStrategy() throws Exception { + createTestEntityWithGeneratedValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(IGeneratedValue.DEFAULT_STRATEGY, idMapping.getGeneratedValue().getStrategy()); + + //change resource model sequenceGenerator name, verify the context model is updated + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + + generatedValue.setStrategy(org.eclipse.jpt.core.internal.resource.java.GenerationType.IDENTITY); + + assertEquals(GenerationType.IDENTITY, idMapping.getGeneratedValue().getStrategy()); + assertEquals(IGeneratedValue.DEFAULT_STRATEGY, idMapping.getGeneratedValue().getDefaultStrategy()); + } + + public void testSetSpecifiedStrategy() throws Exception { + createTestEntityWithGeneratedValue(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(IGeneratedValue.DEFAULT_STRATEGY, idMapping.getGeneratedValue().getStrategy()); + + idMapping.getGeneratedValue().setSpecifiedStrategy(GenerationType.IDENTITY); + + assertEquals(GenerationType.IDENTITY, idMapping.getGeneratedValue().getStrategy()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.GenerationType.IDENTITY, generatedValue.getStrategy()); + + idMapping.getGeneratedValue().setSpecifiedStrategy(null); + + assertEquals(IGeneratedValue.DEFAULT_STRATEGY, idMapping.getGeneratedValue().getStrategy()); + generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertNotNull(generatedValue); + assertNull(generatedValue.getStrategy()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaIdMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaIdMappingTests.java new file mode 100644 index 0000000000..d0b46a02a3 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaIdMappingTests.java @@ -0,0 +1,724 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.GeneratedValue; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.SequenceGenerator; +import org.eclipse.jpt.core.internal.resource.java.TableGenerator; +import org.eclipse.jpt.core.internal.resource.java.Temporal; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaIdMappingTests extends ContextModelTestCase +{ + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception{ + this.createAnnotationAndMembers("Id", ""); + } + + private void createGeneratedValueAnnotation() throws Exception{ + this.createAnnotationAndMembers("GeneratedValue", ""); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private IType createTestEntityWithIdMapping() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + } + }); + } + + private IType createTestEntityWithTemporal() throws Exception { + createEntityAnnotation(); + createTemporalAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.TEMPORAL, JPA.TEMPORAL_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + sb.append("@Temporal(TemporalType.TIMESTAMP)").append(CR); + } + }); + } + + private IType createTestEntityWithIdMappingGeneratedValue() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + createGeneratedValueAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.GENERATED_VALUE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + sb.append("@GeneratedValue").append(CR); + } + }); + } + + public JavaIdMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IVersionMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IVersionMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getMapping(); + assertFalse(idMapping.isDefault()); + idMapping.getColumn().setSpecifiedName("FOO"); + idMapping.setTemporal(TemporalType.TIME); + idMapping.addGeneratedValue(); + idMapping.addTableGenerator(); + idMapping.addSequenceGenerator(); + assertFalse(idMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(TableGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(SequenceGenerator.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(GeneratedValue.ANNOTATION_NAME)); + } + + public void testGetTemporal() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getTemporal()); + } + + public void testGetTemporal2() throws Exception { + createTestEntityWithTemporal(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(TemporalType.TIMESTAMP, idMapping.getTemporal()); + } + + public void testSetTemporal() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(idMapping.getTemporal()); + + idMapping.setTemporal(TemporalType.TIME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.annotation(Temporal.ANNOTATION_NAME); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.TemporalType.TIME, temporal.getValue()); + + idMapping.setTemporal(null); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testGetTemporalUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getTemporal()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.addAnnotation(Temporal.ANNOTATION_NAME); + temporal.setValue(org.eclipse.jpt.core.internal.resource.java.TemporalType.DATE); + + assertEquals(TemporalType.DATE, idMapping.getTemporal()); + + attributeResource.removeAnnotation(Temporal.ANNOTATION_NAME); + + assertNull(idMapping.getTemporal()); + assertFalse(idMapping.isDefault()); + assertSame(idMapping, persistentAttribute.getSpecifiedMapping()); + } + + public void testGetColumn() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getColumn().getSpecifiedName()); + assertEquals("id", idMapping.getColumn().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setName("foo"); + + assertEquals("foo", idMapping.getColumn().getSpecifiedName()); + assertEquals("foo", idMapping.getColumn().getName()); + assertEquals("id", idMapping.getColumn().getDefaultName()); + } + + public void testGetSequenceGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getSequenceGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.SEQUENCE_GENERATOR); + + assertNotNull(idMapping.getSequenceGenerator()); + assertEquals(1, CollectionTools.size(attributeResource.annotations())); + } + + public void testAddSequenceGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getSequenceGenerator()); + + idMapping.addSequenceGenerator(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNotNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR)); + assertNotNull(idMapping.getSequenceGenerator()); + + //try adding another sequence generator, should get an IllegalStateException + try { + idMapping.addSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveSequenceGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.SEQUENCE_GENERATOR); + + + idMapping.removeSequenceGenerator(); + + assertNull(idMapping.getSequenceGenerator()); + assertNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR)); + + //try removing the sequence generator again, should get an IllegalStateException + try { + idMapping.removeSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testGetTableGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getTableGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.TABLE_GENERATOR); + + assertNotNull(idMapping.getTableGenerator()); + assertEquals(1, CollectionTools.size(attributeResource.annotations())); + } + + public void testAddTableGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getTableGenerator()); + + idMapping.addTableGenerator(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNotNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + assertNotNull(idMapping.getTableGenerator()); + + //try adding another table generator, should get an IllegalStateException + try { + idMapping.addTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveTableGenerator() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.TABLE_GENERATOR); + + + idMapping.removeTableGenerator(); + + assertNull(idMapping.getTableGenerator()); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + + //try removing the table generator again, should get an IllegalStateException + try { + idMapping.removeTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testGetGeneratedValue() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getGeneratedValue()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.GENERATED_VALUE); + + assertNotNull(idMapping.getGeneratedValue()); + assertEquals(1, CollectionTools.size(attributeResource.annotations())); + } + + public void testGetGeneratedValue2() throws Exception { + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + createTestEntityWithIdMappingGeneratedValue(); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNotNull(idMapping.getGeneratedValue()); + assertEquals(1, CollectionTools.size(attributeResource.annotations())); + } + + public void testAddGeneratedValue() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(idMapping.getGeneratedValue()); + + idMapping.addGeneratedValue(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNotNull(attributeResource.annotation(JPA.GENERATED_VALUE)); + assertNotNull(idMapping.getGeneratedValue()); + + //try adding another generated value, should get an IllegalStateException + try { + idMapping.addGeneratedValue(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveGeneratedValue() throws Exception { + createTestEntityWithIdMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IIdMapping idMapping = (IIdMapping) persistentAttribute.getSpecifiedMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.addAnnotation(JPA.GENERATED_VALUE); + + + idMapping.removeGeneratedValue(); + + assertNull(idMapping.getGeneratedValue()); + assertNull(attributeResource.annotation(JPA.GENERATED_VALUE)); + + //try removing the generatedValue again, should get an IllegalStateException + try { + idMapping.removeGeneratedValue(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaJoinTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaJoinTableTests.java new file mode 100644 index 0000000000..c328b889a6 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaJoinTableTests.java @@ -0,0 +1,851 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IJoinColumn; +import org.eclipse.jpt.core.internal.context.base.IJoinTable; +import org.eclipse.jpt.core.internal.context.java.IJavaManyToManyMapping; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinTable; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaJoinTableTests extends ContextModelTestCase +{ + public JavaJoinTableTests(String name) { + super(name); + } + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createManyToManyAnnotation() throws Exception{ + this.createAnnotationAndMembers("ManyToMany", ""); + } + + private void createJoinTableAnnotation() throws Exception{ + //TODO + this.createAnnotationAndMembers("JoinTable", + "String name() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";"); + } + + + private IType createTestEntityWithManyToMany() throws Exception { + createEntityAnnotation(); + createManyToManyAnnotation(); + createJoinTableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_MANY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany").append(CR); + } + }); + } + + + public void testUpdateSpecifiedName() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedName()); + assertNull(javaJoinTable); + + + //set name in the resource model, verify context model updated + attributeResource.addAnnotation(JoinTable.ANNOTATION_NAME); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + javaJoinTable.setName("FOO"); + assertEquals("FOO", joinTable.getSpecifiedName()); + assertEquals("FOO", javaJoinTable.getName()); + + //set name to null in the resource model + javaJoinTable.setName(null); + assertNull(joinTable.getSpecifiedName()); + assertNull(javaJoinTable.getName()); + + javaJoinTable.setName("FOO"); + assertEquals("FOO", joinTable.getSpecifiedName()); + assertEquals("FOO", javaJoinTable.getName()); + + attributeResource.removeAnnotation(JoinTable.ANNOTATION_NAME); + assertNull(joinTable.getSpecifiedName()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + + public void testModifySpecifiedName() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedName()); + assertNull(javaJoinTable); + + //set name in the context model, verify resource model modified + joinTable.setSpecifiedName("foo"); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals("foo", joinTable.getSpecifiedName()); + assertEquals("foo", javaJoinTable.getName()); + + //set name to null in the context model + joinTable.setSpecifiedName(null); + assertNull(joinTable.getSpecifiedName()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + +//// public void testUpdateDefaultNameFromJavaTable() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +//// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +//// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(null); +//// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +//// +//// xmlEntity.getTable().setSpecifiedName("Bar"); +//// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +//// } +//// +//// public void testUpdateDefaultNameNoJava() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +//// } +//// +//// public void testUpdateDefaultNameFromParent() throws Exception { +//// createTestEntity(); +//// createTestSubType(); +//// +//// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +//// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +//// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +//// +//// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +//// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +//// +//// parentXmlEntity.getTable().setSpecifiedName("FOO"); +//// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +//// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +//// +//// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +//// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +//// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +//// } + + public void testUpdateSpecifiedSchema() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedSchema()); + assertNull(javaJoinTable); + + + //set schema in the resource model, verify context model updated + attributeResource.addAnnotation(JoinTable.ANNOTATION_NAME); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + javaJoinTable.setSchema("FOO"); + assertEquals("FOO", joinTable.getSpecifiedSchema()); + assertEquals("FOO", javaJoinTable.getSchema()); + + //set schema to null in the resource model + javaJoinTable.setSchema(null); + assertNull(joinTable.getSpecifiedSchema()); + assertNull(javaJoinTable.getSchema()); + + javaJoinTable.setSchema("FOO"); + assertEquals("FOO", joinTable.getSpecifiedSchema()); + assertEquals("FOO", javaJoinTable.getSchema()); + + attributeResource.removeAnnotation(JoinTable.ANNOTATION_NAME); + assertNull(joinTable.getSpecifiedSchema()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + + public void testModifySpecifiedSchema() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedSchema()); + assertNull(javaJoinTable); + + //set schema in the context model, verify resource model modified + joinTable.setSpecifiedSchema("foo"); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals("foo", joinTable.getSpecifiedSchema()); + assertEquals("foo", javaJoinTable.getSchema()); + + //set schema to null in the context model + joinTable.setSpecifiedSchema(null); + assertNull(joinTable.getSpecifiedSchema()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + +//// public void testUpdateDefaultSchemaFromJavaTable() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.javaEntity().getTable().setSpecifiedSchema("Foo"); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(null); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.getTable().setSpecifiedName("Bar"); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// } +//// +//// public void testUpdateDefaultSchemaNoJava() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// } +//// +//// public void testUpdateDefaultSchemaFromParent() throws Exception { +//// createTestEntity(); +//// createTestSubType(); +//// +//// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +//// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +//// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +//// +//// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +//// assertNull(childXmlEntity.getTable().getDefaultSchema()); +//// +//// parentXmlEntity.getTable().setSpecifiedSchema("FOO"); +//// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +//// assertEquals("FOO", childXmlEntity.getTable().getDefaultSchema()); +//// +//// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +//// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +//// assertNull(childXmlEntity.getTable().getDefaultSchema()); +//// } +//// +//// public void testUpdateDefaultSchemaFromPersistenceUnitDefaults() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("FOO"); +//// assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.entityMappings().setSpecifiedSchema("BAR"); +//// assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.javaEntity().getTable().setSpecifiedSchema("JAVA_SCHEMA"); +//// assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.getTable().setSpecifiedName("BLAH"); +//// //xml entity now has a table element so default schema is not taken from java +//// assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); +//// +//// +//// xmlEntity.entityMappings().setSpecifiedSchema(null); +//// assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); +//// assertNull(xmlEntity.getTable().getDefaultSchema()); +//// +//// xmlEntity.getTable().setSpecifiedName(null); +//// assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); +//// } + + public void testUpdateSpecifiedCatalog() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedCatalog()); + assertNull(javaJoinTable); + + + //set catalog in the resource model, verify context model updated + attributeResource.addAnnotation(JoinTable.ANNOTATION_NAME); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + javaJoinTable.setCatalog("FOO"); + assertEquals("FOO", joinTable.getSpecifiedCatalog()); + assertEquals("FOO", javaJoinTable.getCatalog()); + + //set catalog to null in the resource model + javaJoinTable.setCatalog(null); + assertNull(joinTable.getSpecifiedCatalog()); + assertNull(javaJoinTable.getCatalog()); + + javaJoinTable.setCatalog("FOO"); + assertEquals("FOO", joinTable.getSpecifiedCatalog()); + assertEquals("FOO", javaJoinTable.getCatalog()); + + attributeResource.removeAnnotation(JoinTable.ANNOTATION_NAME); + assertNull(joinTable.getSpecifiedCatalog()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + + public void testModifySpecifiedCatalog() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertNull(joinTable.getSpecifiedCatalog()); + assertNull(javaJoinTable); + + //set catalog in the context model, verify resource model modified + joinTable.setSpecifiedCatalog("foo"); + javaJoinTable = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals("foo", joinTable.getSpecifiedCatalog()); + assertEquals("foo", javaJoinTable.getCatalog()); + + //set catalog to null in the context model + joinTable.setSpecifiedCatalog(null); + assertNull(joinTable.getSpecifiedCatalog()); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + } + +//// public void testUpdateDefaultCatalogFromJavaTable() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.javaEntity().getTable().setSpecifiedCatalog("Foo"); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +//// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.setSpecifiedMetadataComplete(null); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +//// assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.getTable().setSpecifiedName("Bar"); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// } +//// +//// public void testUpdateDefaultCatalogNoJava() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// } +//// +//// public void testUpdateDefaultCatalogFromParent() throws Exception { +//// createTestEntity(); +//// createTestSubType(); +//// +//// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +//// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +//// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +//// +//// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +//// assertNull(childXmlEntity.getTable().getDefaultCatalog()); +//// +//// parentXmlEntity.getTable().setSpecifiedCatalog("FOO"); +//// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +//// assertEquals("FOO", childXmlEntity.getTable().getDefaultCatalog()); +//// +//// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +//// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +//// assertNull(childXmlEntity.getTable().getDefaultCatalog()); +//// } +//// +//// public void testUpdateDefaultCatalogFromPersistenceUnitDefaults() throws Exception { +//// createTestEntity(); +//// +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("FOO"); +//// assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.entityMappings().setSpecifiedCatalog("BAR"); +//// assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.javaEntity().getTable().setSpecifiedCatalog("JAVA_CATALOG"); +//// assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.getTable().setSpecifiedName("BLAH"); +//// //xml entity now has a table element so default schema is not taken from java +//// assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); +//// +//// +//// xmlEntity.entityMappings().setSpecifiedCatalog(null); +//// assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); +//// assertNull(xmlEntity.getTable().getDefaultCatalog()); +//// +//// xmlEntity.getTable().setSpecifiedName(null); +//// assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); +////} +// +//// +//// public void testUpdateName() throws Exception { +//// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +//// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +//// Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); +//// assertEquals("Foo", xmlEntity.getName()); +//// +//// //set class in the resource model, verify context model updated +//// entityResource.setClassName("com.Bar"); +//// assertEquals("Bar", xmlEntity.getName()); +//// +//// entityResource.setName("Baz"); +//// assertEquals("Baz", xmlEntity.getName()); +//// +//// //set class to null in the resource model +//// entityResource.setClassName(null); +//// assertEquals("Baz", xmlEntity.getName()); +//// +//// entityResource.setName(null); +//// assertNull(xmlEntity.getName()); +//// } +// + + public void testAddSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + + IJoinColumn joinColumn = joinTable.addSpecifiedJoinColumn(0); + joinColumn.setSpecifiedName("FOO"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertEquals("FOO", joinTableResource.joinColumnAt(0).getName()); + + IJoinColumn joinColumn2 = joinTable.addSpecifiedJoinColumn(0); + joinColumn2.setSpecifiedName("BAR"); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(1).getName()); + + IJoinColumn joinColumn3 = joinTable.addSpecifiedJoinColumn(1); + joinColumn3.setSpecifiedName("BAZ"); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + + ListIterator<IJoinColumn> joinColumns = joinTable.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + joinTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + joinTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + joinTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals(3, joinTableResource.joinColumnsSize()); + + joinTable.removeSpecifiedJoinColumn(0); + assertEquals(2, joinTableResource.joinColumnsSize()); + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + + joinTable.removeSpecifiedJoinColumn(0); + assertEquals(1, joinTableResource.joinColumnsSize()); + assertEquals("BAZ", joinTableResource.joinColumnAt(0).getName()); + + joinTable.removeSpecifiedJoinColumn(0); + assertEquals(0, joinTableResource.joinColumnsSize()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + joinTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + joinTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + joinTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals(3, joinTableResource.joinColumnsSize()); + + + joinTable.moveSpecifiedJoinColumn(2, 0); + ListIterator<IJoinColumn> joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + + + joinTable.moveSpecifiedJoinColumn(0, 1); + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", joinTableResource.joinColumnAt(0).getName()); + assertEquals("BAR", joinTableResource.joinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.joinColumnAt(2).getName()); + } + + public void testUpdateJoinColumns() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable joinTableResource = (JoinTable) attributeResource.addAnnotation(JoinTable.ANNOTATION_NAME); + + joinTableResource.addJoinColumn(0); + joinTableResource.addJoinColumn(1); + joinTableResource.addJoinColumn(2); + + joinTableResource.joinColumnAt(0).setName("FOO"); + joinTableResource.joinColumnAt(1).setName("BAR"); + joinTableResource.joinColumnAt(2).setName("BAZ"); + + ListIterator<IJoinColumn> joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.moveJoinColumn(2, 0); + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.moveJoinColumn(0, 1); + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(1); + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(1); + joinColumns = joinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.removeJoinColumn(0); + assertFalse(joinTable.specifiedJoinColumns().hasNext()); + } + + public void testAddSpecifiedInverseJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + + IJoinColumn inverseJoinColumn = joinTable.addSpecifiedInverseJoinColumn(0); + inverseJoinColumn.setSpecifiedName("FOO"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + + assertEquals("FOO", joinTableResource.inverseJoinColumnAt(0).getName()); + + IJoinColumn inverseJoinColumn2 = joinTable.addSpecifiedInverseJoinColumn(0); + inverseJoinColumn2.setSpecifiedName("BAR"); + + assertEquals("BAR", joinTableResource.inverseJoinColumnAt(0).getName()); + assertEquals("FOO", joinTableResource.inverseJoinColumnAt(1).getName()); + + IJoinColumn inverseJoinColumn3 = joinTable.addSpecifiedInverseJoinColumn(1); + inverseJoinColumn3.setSpecifiedName("BAZ"); + + assertEquals("BAR", joinTableResource.inverseJoinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.inverseJoinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.inverseJoinColumnAt(2).getName()); + + ListIterator<IJoinColumn> inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals(inverseJoinColumn2, inverseJoinColumns.next()); + assertEquals(inverseJoinColumn3, inverseJoinColumns.next()); + assertEquals(inverseJoinColumn, inverseJoinColumns.next()); + + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + } + + public void testRemoveSpecifiedInverseJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + joinTable.addSpecifiedInverseJoinColumn(0).setSpecifiedName("FOO"); + joinTable.addSpecifiedInverseJoinColumn(1).setSpecifiedName("BAR"); + joinTable.addSpecifiedInverseJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals(3, joinTableResource.inverseJoinColumnsSize()); + + joinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(2, joinTableResource.inverseJoinColumnsSize()); + assertEquals("BAR", joinTableResource.inverseJoinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.inverseJoinColumnAt(1).getName()); + + joinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(1, joinTableResource.inverseJoinColumnsSize()); + assertEquals("BAZ", joinTableResource.inverseJoinColumnAt(0).getName()); + + joinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(0, joinTableResource.inverseJoinColumnsSize()); + } + + public void testMoveSpecifiedInverseJoinColumn() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + joinTable.addSpecifiedInverseJoinColumn(0).setSpecifiedName("FOO"); + joinTable.addSpecifiedInverseJoinColumn(1).setSpecifiedName("BAR"); + joinTable.addSpecifiedInverseJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = (JoinTable) attributeResource.annotation(JoinTable.ANNOTATION_NAME); + assertEquals(3, joinTableResource.inverseJoinColumnsSize()); + + + joinTable.moveSpecifiedInverseJoinColumn(2, 0); + ListIterator<IJoinColumn> inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + + assertEquals("BAR", joinTableResource.inverseJoinColumnAt(0).getName()); + assertEquals("BAZ", joinTableResource.inverseJoinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.inverseJoinColumnAt(2).getName()); + + + joinTable.moveSpecifiedInverseJoinColumn(0, 1); + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + + assertEquals("BAZ", joinTableResource.inverseJoinColumnAt(0).getName()); + assertEquals("BAR", joinTableResource.inverseJoinColumnAt(1).getName()); + assertEquals("FOO", joinTableResource.inverseJoinColumnAt(2).getName()); + } + + public void testUpdateInverseJoinColumns() throws Exception { + createTestEntityWithManyToMany(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IJavaManyToManyMapping manyToManyMapping = (IJavaManyToManyMapping) javaPersistentType().attributes().next().getMapping(); + IJoinTable joinTable = manyToManyMapping.getJoinTable(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + JoinTable joinTableResource = (JoinTable) attributeResource.addAnnotation(JoinTable.ANNOTATION_NAME); + + joinTableResource.addInverseJoinColumn(0); + joinTableResource.addInverseJoinColumn(1); + joinTableResource.addInverseJoinColumn(2); + + joinTableResource.inverseJoinColumnAt(0).setName("FOO"); + joinTableResource.inverseJoinColumnAt(1).setName("BAR"); + joinTableResource.inverseJoinColumnAt(2).setName("BAZ"); + + ListIterator<IJoinColumn> inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + joinTableResource.moveInverseJoinColumn(2, 0); + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + joinTableResource.moveInverseJoinColumn(0, 1); + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + joinTableResource.removeInverseJoinColumn(1); + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + joinTableResource.removeInverseJoinColumn(1); + inverseJoinColumns = joinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + joinTableResource.removeInverseJoinColumn(0); + assertFalse(joinTable.specifiedInverseJoinColumns().hasNext()); + } + + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToManyMappingTests.java new file mode 100644 index 0000000000..76159ef015 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToManyMappingTests.java @@ -0,0 +1,872 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.ITypeMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinTable; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.MapKey; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.OrderBy; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaManyToManyMappingTests extends ContextModelTestCase +{ + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createManyToManyAnnotation() throws Exception{ + this.createAnnotationAndMembers("ManyToMany", ""); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + private IType createTestEntityWithManyToManyMapping() throws Exception { + createEntityAnnotation(); + createManyToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_MANY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany").append(CR); + } + }); + } + + private IType createTestEntityWithValidManyToManyMapping() throws Exception { + createEntityAnnotation(); + createManyToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_MANY, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToMany").append(CR); + sb.append(" private Collection<Address> addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + private IType createTestEntityWithCollectionManyToManyMapping() throws Exception { + createEntityAnnotation(); + createManyToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_MANY, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToMany").append(CR); + sb.append(" private Collection addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + private IType createTestEntityWithNonCollectionManyToManyMapping() throws Exception { + createEntityAnnotation(); + createManyToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_MANY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToMany").append(CR); + sb.append(" private Address addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + public JavaManyToManyMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertNull(persistentAttribute.getSpecifiedMapping()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + manyToManyMapping.setOrderBy("asdf"); + manyToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(manyToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testUpdateSpecifiedTargetEntity() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + manyToMany.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", manyToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToMany.getTargetEntity()); + + //set target entity to null in the resource model + manyToMany.setTargetEntity(null); + assertNull(manyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + } + + public void testModifySpecifiedTargetEntity() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + manyToManyMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", manyToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToMany.getTargetEntity()); + + //set target entity to null in the context model + manyToManyMapping.setSpecifiedTargetEntity(null); + assertNull(manyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getSpecifiedFetch()); + assertNull(manyToMany.getFetch()); + + //set fetch in the resource model, verify context model updated + manyToMany.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER); + assertEquals(FetchType.EAGER, manyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, manyToMany.getFetch()); + + manyToMany.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY); + assertEquals(FetchType.LAZY, manyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, manyToMany.getFetch()); + + + //set fetch to null in the resource model + manyToMany.setFetch(null); + assertNull(manyToManyMapping.getSpecifiedFetch()); + assertNull(manyToMany.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getSpecifiedFetch()); + assertNull(manyToMany.getFetch()); + + //set fetch in the context model, verify resource model updated + manyToManyMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(FetchType.EAGER, manyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, manyToMany.getFetch()); + + manyToManyMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, manyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, manyToMany.getFetch()); + + + //set fetch to null in the context model + manyToManyMapping.setSpecifiedFetch(null); + assertNull(manyToManyMapping.getSpecifiedFetch()); + assertNull(manyToMany.getFetch()); + } + + public void testUpdateMappedBy() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + manyToMany.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", manyToManyMapping.getMappedBy()); + assertEquals("newMappedBy", manyToMany.getMappedBy()); + + //set mappedBy to null in the resource model + manyToMany.setMappedBy(null); + assertNull(manyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(); + + assertNull(manyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + manyToManyMapping.setMappedBy("newTargetEntity"); + assertEquals("newTargetEntity", manyToManyMapping.getMappedBy()); + assertEquals("newTargetEntity", manyToMany.getMappedBy()); + + //set mappedBy to null in the context model + manyToManyMapping.setMappedBy(null); + assertNull(manyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + } + + + public void testCandidateMappedByAttributeNames() throws Exception { + createTestEntityWithValidManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + Iterator<String> attributeNames = manyToManyMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + + manyToManyMapping.setSpecifiedTargetEntity("foo"); + attributeNames = manyToManyMapping.candidateMappedByAttributeNames(); + assertFalse(attributeNames.hasNext()); + + manyToManyMapping.setSpecifiedTargetEntity(null); + attributeNames = manyToManyMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + } + + public void testDefaultTargetEntity() throws Exception { + createTestEntityWithValidManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit, default still set, handled by validation + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getDefaultTargetEntity()); + + //add targetEntity to the persistence unit + addXmlClassRef(PACKAGE_NAME + ".Address"); + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getDefaultTargetEntity()); + + //test default still the same when specified target entity it set + manyToManyMapping.setSpecifiedTargetEntity("foo"); + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getDefaultTargetEntity()); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + IJavaPersistentType addressPersistentType = addressClassRef.getJavaPersistentType(); + + //test target is not an Entity, default target entity still exists, this case handled with validation + addressPersistentType.setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityCollectionType() throws Exception { + createTestEntityWithCollectionManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertNull(manyToManyMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityNonCollectionType() throws Exception { + createTestEntityWithNonCollectionManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertNull(manyToManyMapping.getDefaultTargetEntity()); + } + + public void testTargetEntity() throws Exception { + createTestEntityWithValidManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getTargetEntity()); + + manyToManyMapping.setSpecifiedTargetEntity("foo"); + assertEquals("foo", manyToManyMapping.getTargetEntity()); + + manyToManyMapping.setSpecifiedTargetEntity(null); + assertEquals(PACKAGE_NAME + ".Address", manyToManyMapping.getTargetEntity()); + } + + public void testResolvedTargetEntity() throws Exception { + createTestEntityWithValidManyToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit + assertNull(manyToManyMapping.getResolvedTargetEntity()); + + //add targetEntity to the persistence unit, now target entity should resolve + addXmlClassRef(PACKAGE_NAME + ".Address"); + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + ITypeMapping addressTypeMapping = addressClassRef.getJavaPersistentType().getMapping(); + assertEquals(addressTypeMapping, manyToManyMapping.getResolvedTargetEntity()); + + //test default still the same when specified target entity it set + manyToManyMapping.setSpecifiedTargetEntity("foo"); + assertNull(manyToManyMapping.getResolvedTargetEntity()); + + + manyToManyMapping.setSpecifiedTargetEntity(PACKAGE_NAME + ".Address"); + assertEquals(addressTypeMapping, manyToManyMapping.getResolvedTargetEntity()); + + + manyToManyMapping.setSpecifiedTargetEntity(null); + assertEquals(addressTypeMapping, manyToManyMapping.getResolvedTargetEntity()); + } + + + public void testUpdateMapKey() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(manyToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + + //set mapKey in the resource model, verify context model does not change + attributeResource.addAnnotation(MapKey.ANNOTATION_NAME); + assertNull(manyToManyMapping.getMapKey()); + MapKey mapKey = (MapKey) attributeResource.annotation(MapKey.ANNOTATION_NAME); + assertNotNull(mapKey); + + //set mapKey name in the resource model, verify context model updated + mapKey.setName("myMapKey"); + assertEquals("myMapKey", manyToManyMapping.getMapKey()); + assertEquals("myMapKey", mapKey.getName()); + + //set mapKey name to null in the resource model + mapKey.setName(null); + assertNull(manyToManyMapping.getMapKey()); + assertNull(mapKey.getName()); + + mapKey.setName("myMapKey"); + attributeResource.removeAnnotation(MapKey.ANNOTATION_NAME); + assertNull(manyToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + } + + public void testModifyMapKey() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(manyToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + + //set mapKey in the context model, verify resource model updated + manyToManyMapping.setMapKey("myMapKey"); + MapKey mapKey = (MapKey) attributeResource.annotation(MapKey.ANNOTATION_NAME); + assertEquals("myMapKey", manyToManyMapping.getMapKey()); + assertEquals("myMapKey", mapKey.getName()); + + //set mapKey to null in the context model + manyToManyMapping.setMapKey(null); + assertNull(manyToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + } + + public void testUpdateOrderBy() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(manyToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + + //set orderBy in the resource model, verify context model updated + attributeResource.addAnnotation(OrderBy.ANNOTATION_NAME); + OrderBy orderBy = (OrderBy) attributeResource.annotation(OrderBy.ANNOTATION_NAME); + orderBy.setValue("newOrderBy"); + assertEquals("newOrderBy", manyToManyMapping.getOrderBy()); + assertEquals("newOrderBy", orderBy.getValue()); + + //set orderBy to null in the resource model + attributeResource.removeAnnotation(OrderBy.ANNOTATION_NAME); + assertNull(manyToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testModifyOrderBy() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(manyToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + + //set mappedBy in the context model, verify resource model updated + manyToManyMapping.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", manyToManyMapping.getOrderBy()); + OrderBy orderBy = (OrderBy) attributeResource.annotation(OrderBy.ANNOTATION_NAME); + assertEquals("newOrderBy", orderBy.getValue()); + + //set mappedBy to null in the context model + manyToManyMapping.setOrderBy(null); + assertNull(manyToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testIsNoOrdering() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertTrue(manyToManyMapping.isNoOrdering()); + + manyToManyMapping.setOrderBy("foo"); + assertFalse(manyToManyMapping.isNoOrdering()); + + manyToManyMapping.setOrderBy(null); + assertTrue(manyToManyMapping.isNoOrdering()); + } + + public void testSetNoOrdering() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertTrue(manyToManyMapping.isNoOrdering()); + + manyToManyMapping.setOrderBy("foo"); + assertFalse(manyToManyMapping.isNoOrdering()); + + manyToManyMapping.setNoOrdering(); + assertTrue(manyToManyMapping.isNoOrdering()); + assertNull(manyToManyMapping.getOrderBy()); + } +//TODO +// public boolean isOrderByPk() { +// return "".equals(getOrderBy()); +// } +// +// public void setOrderByPk() { +// setOrderBy(""); +// } + + public void testIsCustomOrdering() throws Exception { + createTestEntityWithManyToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToManyMapping manyToManyMapping = (IManyToManyMapping) persistentAttribute.getMapping(); + + assertFalse(manyToManyMapping.isCustomOrdering()); + + manyToManyMapping.setOrderBy("foo"); + assertTrue(manyToManyMapping.isCustomOrdering()); + + manyToManyMapping.setOrderBy(null); + assertFalse(manyToManyMapping.isCustomOrdering()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToOneMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToOneMappingTests.java new file mode 100644 index 0000000000..a2af9759bf --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaManyToOneMappingTests.java @@ -0,0 +1,884 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IJoinColumn; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.ITypeMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaJoinColumn; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JoinColumns; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaManyToOneMappingTests extends ContextModelTestCase +{ + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createManyToOneAnnotation() throws Exception{ + this.createAnnotationAndMembers("ManyToOne", ""); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + private IType createTestEntityWithManyToOneMapping() throws Exception { + createEntityAnnotation(); + createManyToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_ONE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne").append(CR); + } + }); + } + + private IType createTestEntityWithValidManyToOneMapping() throws Exception { + createEntityAnnotation(); + createManyToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_ONE, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToOne").append(CR); + sb.append(" private Address address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + private IType createTestEntityWithCollectionManyToOneMapping() throws Exception { + createEntityAnnotation(); + createManyToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_ONE, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToOne").append(CR); + sb.append(" private Collection address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + private IType createTestEntityWithGenericizedCollectionManyToOneMapping() throws Exception { + createEntityAnnotation(); + createManyToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_ONE, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToOne").append(CR); + sb.append(" private Collection<Address> address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + public JavaManyToOneMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertNull(persistentAttribute.getSpecifiedMapping()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + manyToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(manyToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testUpdateSpecifiedTargetEntity() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(); + + assertNull(manyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + manyToOne.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", manyToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToOne.getTargetEntity()); + + //set target entity to null in the resource model + manyToOne.setTargetEntity(null); + assertNull(manyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + } + + public void testModifySpecifiedTargetEntity() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(); + + assertNull(manyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + manyToOneMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", manyToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToOne.getTargetEntity()); + + //set target entity to null in the context model + manyToOneMapping.setSpecifiedTargetEntity(null); + assertNull(manyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + } + + public void testUpdateSpecifiedOptional() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(); + + assertNull(manyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOne.getOptional()); + + //set optional in the resource model, verify context model updated + manyToOne.setOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, manyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, manyToOne.getOptional()); + + manyToOne.setOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, manyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, manyToOne.getOptional()); + + + //set optional to null in the resource model + manyToOne.setOptional(null); + assertNull(manyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOne.getOptional()); + } + + public void testModifySpecifiedOptional() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(); + + assertNull(manyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOne.getOptional()); + + //set optional in the context model, verify resource model updated + manyToOneMapping.setSpecifiedOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, manyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, manyToOne.getOptional()); + + manyToOneMapping.setSpecifiedOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, manyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, manyToOne.getOptional()); + + + //set optional to null in the context model + manyToOneMapping.setSpecifiedOptional(null); + assertNull(manyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOne.getOptional()); + } + + + public void testSpecifiedJoinColumns() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + ListIterator<IJavaJoinColumn> specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + + assertFalse(specifiedJoinColumns.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + JoinColumn joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("FOO"); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("BAR"); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + + joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("BAZ"); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.move(1, 0, JPA.JOIN_COLUMNS); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertFalse(specifiedJoinColumns.hasNext()); + } + + public void testDefaultJoinColumns() { + //TODO + } + + public void testSpecifiedJoinColumnsSize() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + assertEquals(0, manyToOneMapping.specifiedJoinColumnsSize()); + + manyToOneMapping.addSpecifiedJoinColumn(0); + assertEquals(1, manyToOneMapping.specifiedJoinColumnsSize()); + + manyToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(0, manyToOneMapping.specifiedJoinColumnsSize()); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("BAR"); + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> joinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + + assertEquals("BAZ", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAR", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("FOO", ((JoinColumn) joinColumns.next()).getName()); + assertFalse(joinColumns.hasNext()); + } + + public void testAddSpecifiedJoinColumn2() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + manyToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + manyToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> joinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + + assertEquals("FOO", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAR", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAZ", ((JoinColumn) joinColumns.next()).getName()); + assertFalse(joinColumns.hasNext()); + } + public void testRemoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + manyToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + manyToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertEquals(3, CollectionTools.size(attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME))); + + manyToOneMapping.removeSpecifiedJoinColumn(1); + + Iterator<JavaResource> joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((JoinColumn) joinColumnResources.next()).getName()); + assertEquals("BAZ", ((JoinColumn) joinColumnResources.next()).getName()); + assertFalse(joinColumnResources.hasNext()); + + Iterator<IJoinColumn> joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + + manyToOneMapping.removeSpecifiedJoinColumn(1); + joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((JoinColumn) joinColumnResources.next()).getName()); + assertFalse(joinColumnResources.hasNext()); + + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + + manyToOneMapping.removeSpecifiedJoinColumn(0); + joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertFalse(joinColumnResources.hasNext()); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertFalse(joinColumns.hasNext()); + + assertNull(attributeResource.annotation(JoinColumns.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + manyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + manyToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + manyToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentAttributeResource attributeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME).attributes().next(); + + ListIterator<JoinColumn> javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaJoinColumns)); + + + manyToOneMapping.moveSpecifiedJoinColumn(2, 0); + ListIterator<IJoinColumn> primaryKeyJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("BAR", javaJoinColumns.next().getName()); + assertEquals("BAZ", javaJoinColumns.next().getName()); + assertEquals("FOO", javaJoinColumns.next().getName()); + + + manyToOneMapping.moveSpecifiedJoinColumn(0, 1); + primaryKeyJoinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("BAZ", javaJoinColumns.next().getName()); + assertEquals("BAR", javaJoinColumns.next().getName()); + assertEquals("FOO", javaJoinColumns.next().getName()); + } + + public void testUpdateSpecifiedJoinColumns() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + JavaPersistentAttributeResource attributeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME).attributes().next(); + + ((JoinColumn) attributeResource.addAnnotation(0, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("FOO"); + ((JoinColumn) attributeResource.addAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("BAR"); + ((JoinColumn) attributeResource.addAnnotation(2, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<IJoinColumn> joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.move(2, 0, JoinColumns.ANNOTATION_NAME); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.move(0, 1, JoinColumns.ANNOTATION_NAME); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = manyToOneMapping.specifiedJoinColumns(); + assertFalse(joinColumns.hasNext()); + } + public void testJoinColumnIsVirtual() throws Exception { + createTestEntityWithManyToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + manyToOneMapping.addSpecifiedJoinColumn(0); + IJoinColumn specifiedJoinColumn = manyToOneMapping.specifiedJoinColumns().next(); + assertFalse(specifiedJoinColumn.isVirtual()); + + IJoinColumn defaultJoinColumn = manyToOneMapping.defaultJoinColumns().next(); + assertTrue(defaultJoinColumn.isVirtual()); + } + + public void testDefaultTargetEntity() throws Exception { + createTestEntityWithValidManyToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit, default still set, handled by validation + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getDefaultTargetEntity()); + + //add targetEntity to the persistence unit + addXmlClassRef(PACKAGE_NAME + ".Address"); + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getDefaultTargetEntity()); + + //test default still the same when specified target entity it set + manyToOneMapping.setSpecifiedTargetEntity("foo"); + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getDefaultTargetEntity()); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + IJavaPersistentType addressPersistentType = addressClassRef.getJavaPersistentType(); + + //test target is not an Entity, default target entity still exists, this case handled with validation + addressPersistentType.setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityCollectionType() throws Exception { + createTestEntityWithCollectionManyToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + assertNull(manyToOneMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityGenericizedCollectionType() throws Exception { + createTestEntityWithGenericizedCollectionManyToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + assertNull(manyToOneMapping.getDefaultTargetEntity()); + } + + public void testTargetEntity() throws Exception { + createTestEntityWithValidManyToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getTargetEntity()); + + manyToOneMapping.setSpecifiedTargetEntity("foo"); + assertEquals("foo", manyToOneMapping.getTargetEntity()); + + manyToOneMapping.setSpecifiedTargetEntity(null); + assertEquals(PACKAGE_NAME + ".Address", manyToOneMapping.getTargetEntity()); + } + + public void testResolvedTargetEntity() throws Exception { + createTestEntityWithValidManyToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IManyToOneMapping manyToOneMapping = (IManyToOneMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit + assertNull(manyToOneMapping.getResolvedTargetEntity()); + + //add targetEntity to the persistence unit, now target entity should resolve + addXmlClassRef(PACKAGE_NAME + ".Address"); + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + ITypeMapping addressTypeMapping = addressClassRef.getJavaPersistentType().getMapping(); + assertEquals(addressTypeMapping, manyToOneMapping.getResolvedTargetEntity()); + + //test default still the same when specified target entity it set + manyToOneMapping.setSpecifiedTargetEntity("foo"); + assertNull(manyToOneMapping.getResolvedTargetEntity()); + + + manyToOneMapping.setSpecifiedTargetEntity(PACKAGE_NAME + ".Address"); + assertEquals(addressTypeMapping, manyToOneMapping.getResolvedTargetEntity()); + + + manyToOneMapping.setSpecifiedTargetEntity(null); + assertEquals(addressTypeMapping, manyToOneMapping.getResolvedTargetEntity()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaMappedSuperclassTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaMappedSuperclassTests.java new file mode 100644 index 0000000000..f0275ba494 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaMappedSuperclassTests.java @@ -0,0 +1,243 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IEmbeddable; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.IMappedSuperclass; +import org.eclipse.jpt.core.internal.context.java.JavaNullTypeMapping; +import org.eclipse.jpt.core.internal.resource.java.IdClass; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.MappedSuperclass; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaMappedSuperclassTests extends ContextModelTestCase +{ + + private void createMappedSuperclassAnnotation() throws Exception { + this.createAnnotationAndMembers("MappedSuperclass", ""); + } + + private IType createTestMappedSuperclass() throws Exception { + createMappedSuperclassAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + } + }); + } + + + public JavaMappedSuperclassTests(String name) { + super(name); + } + + public void testMorphToEntity() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IEntity); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(MappedSuperclass.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddable() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof IEmbeddable); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(MappedSuperclass.ANNOTATION_NAME)); + } + + public void testMorphToNull() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaPersistentType().setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertTrue(javaPersistentType().getMapping() instanceof JavaNullTypeMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.mappingAnnotation(MappedSuperclass.ANNOTATION_NAME)); + } + + + public void testMappedSuperclass() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + assertTrue(javaPersistentType().getMapping() instanceof IMappedSuperclass); + } + + public void testOverridableAttributeNames() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + Iterator<String> overridableAttributeNames = mappedSuperclass.overridableAttributeNames(); + assertEquals("id", overridableAttributeNames.next()); + assertEquals("name", overridableAttributeNames.next()); + assertFalse(overridableAttributeNames.hasNext()); + } + + public void testOverridableAssociationNames() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + Iterator<String> overridableAssociationNames = mappedSuperclass.overridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + + public void testTableNameIsInvalid() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertFalse(mappedSuperclass.tableNameIsInvalid(FULLY_QUALIFIED_TYPE_NAME)); + assertFalse(mappedSuperclass.tableNameIsInvalid("FOO")); + } + + public void testAssociatedTables() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertFalse(mappedSuperclass.associatedTables().hasNext()); + } + + public void testAssociatedTablesIncludingInherited() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertFalse(mappedSuperclass.associatedTablesIncludingInherited().hasNext()); + } + + public void testAssociatedTableNamesIncludingInherited() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertFalse(mappedSuperclass.associatedTableNamesIncludingInherited().hasNext()); + } + + public void testAllOverridableAttributeNames() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + Iterator<String> overridableAttributeNames = mappedSuperclass.overridableAttributeNames(); + assertEquals("id", overridableAttributeNames.next()); + assertEquals("name", overridableAttributeNames.next()); + assertFalse(overridableAttributeNames.hasNext()); + } + + //TODO need to create a subclass mappedSuperclass and test this + public void testAllOverridableAssociationNames() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + Iterator<String> overridableAssociationNames = mappedSuperclass.overridableAssociationNames(); + assertFalse(overridableAssociationNames.hasNext()); + } + + public void testAttributeMappingKeyAllowed() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + assertTrue(mappedSuperclass.attributeMappingKeyAllowed(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY)); + } + + public void testUpdateIdClass() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertNull(mappedSuperclass.getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + IdClass idClass = (IdClass) typeResource.addAnnotation(IdClass.ANNOTATION_NAME); + assertNull(mappedSuperclass.getIdClass()); + assertNotNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + idClass.setValue("model.Foo"); + assertEquals("model.Foo", mappedSuperclass.getIdClass()); + assertEquals("model.Foo", ((IdClass) typeResource.annotation(IdClass.ANNOTATION_NAME)).getValue()); + + //test setting @IdClass value to null, IdClass annotation is removed + idClass.setValue(null); + assertNull(mappedSuperclass.getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + //reset @IdClass value and then remove @IdClass + idClass = (IdClass) typeResource.addAnnotation(IdClass.ANNOTATION_NAME); + idClass.setValue("model.Foo"); + typeResource.removeAnnotation(IdClass.ANNOTATION_NAME); + + assertNull(mappedSuperclass.getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + } + + public void testModifyIdClass() throws Exception { + createTestMappedSuperclass(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + IMappedSuperclass mappedSuperclass = (IMappedSuperclass) javaPersistentType().getMapping(); + + assertNull(mappedSuperclass.getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + + mappedSuperclass.setIdClass("model.Foo"); + assertEquals("model.Foo", ((IdClass) typeResource.annotation(IdClass.ANNOTATION_NAME)).getValue()); + assertEquals("model.Foo", mappedSuperclass.getIdClass()); + + mappedSuperclass.setIdClass(null); + assertNull(mappedSuperclass.getIdClass()); + assertNull(typeResource.annotation(IdClass.ANNOTATION_NAME)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedNativeQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedNativeQueryTests.java new file mode 100644 index 0000000000..a93b9905ae --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedNativeQueryTests.java @@ -0,0 +1,424 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.INamedNativeQuery; +import org.eclipse.jpt.core.internal.context.base.IQueryHint; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQuery; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaNamedNativeQueryTests extends ContextModelTestCase +{ + private static final String QUERY_NAME = "QUERY_NAME"; + private static final String QUERY_QUERY = "MY_QUERY"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createNamedNativeQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedNativeQuery", + "String name();" + + "String query();" + + "QueryHint[] hints() default {};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", + "String name();" + + "String value();"); + } + + private IType createTestEntityWithNamedNativeQuery() throws Exception { + createEntityAnnotation(); + createNamedNativeQueryAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@NamedNativeQuery(name=\"" + QUERY_NAME + "\", query=\"" + QUERY_QUERY + "\")"); + } + }); + } + + + + public JavaNamedNativeQueryTests(String name) { + super(name); + } + + public void testUpdateName() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(QUERY_NAME, javaNamedNativeQuery.getName()); + assertEquals(QUERY_NAME, namedNativeQuery.getName()); + + //set name to null in the resource model + javaNamedNativeQuery.setName(null); + assertNull(javaNamedNativeQuery.getName()); + assertNull(namedNativeQuery.getName()); + + //set name in the resource model, verify context model updated + javaNamedNativeQuery.setName("foo"); + assertEquals("foo", javaNamedNativeQuery.getName()); + assertEquals("foo", namedNativeQuery.getName()); + } + + public void testModifyName() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(QUERY_NAME, javaNamedNativeQuery.getName()); + assertEquals(QUERY_NAME, namedNativeQuery.getName()); + + //set name to null in the context model + namedNativeQuery.setName(null); + assertNull(javaNamedNativeQuery.getName()); + assertNull(namedNativeQuery.getName()); + + //set name in the context model, verify resource model updated + namedNativeQuery.setName("foo"); + assertEquals("foo", javaNamedNativeQuery.getName()); + assertEquals("foo", namedNativeQuery.getName()); + } + + public void testUpdateQuery() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(QUERY_QUERY, javaNamedNativeQuery.getQuery()); + assertEquals(QUERY_QUERY, namedNativeQuery.getQuery()); + + //set name to null in the resource model + javaNamedNativeQuery.setQuery(null); + assertNull(javaNamedNativeQuery.getQuery()); + assertNull(namedNativeQuery.getQuery()); + + //set name in the resource model, verify context model updated + javaNamedNativeQuery.setQuery("foo"); + assertEquals("foo", javaNamedNativeQuery.getQuery()); + assertEquals("foo", namedNativeQuery.getQuery()); + } + + public void testModifyQuery() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(QUERY_QUERY, javaNamedNativeQuery.getQuery()); + assertEquals(QUERY_QUERY, namedNativeQuery.getQuery()); + + //set name to null in the context model + namedNativeQuery.setQuery(null); + assertNull(javaNamedNativeQuery.getQuery()); + assertNull(namedNativeQuery.getQuery()); + + //set name in the context model, verify resource model updated + namedNativeQuery.setQuery("foo"); + assertEquals("foo", javaNamedNativeQuery.getQuery()); + assertEquals("foo", namedNativeQuery.getQuery()); + } + + + public void testAddHint() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + + IQueryHint queryHint = namedNativeQuery.addHint(0); + ormResource().save(null); + queryHint.setName("FOO"); + ormResource().save(null); + + assertEquals("FOO", javaNamedNativeQuery.hintAt(0).getName()); + + IQueryHint queryHint2 = namedNativeQuery.addHint(0); + ormResource().save(null); + queryHint2.setName("BAR"); + ormResource().save(null); + + assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName()); + assertEquals("FOO", javaNamedNativeQuery.hintAt(1).getName()); + + IQueryHint queryHint3 = namedNativeQuery.addHint(1); + ormResource().save(null); + queryHint3.setName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName()); + + ListIterator<IQueryHint> hints = namedNativeQuery.hints(); + assertEquals(queryHint2, hints.next()); + assertEquals(queryHint3, hints.next()); + assertEquals(queryHint, hints.next()); + + hints = namedNativeQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + } + + public void testRemoveHint() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + namedNativeQuery.addHint(0).setName("FOO"); + namedNativeQuery.addHint(1).setName("BAR"); + namedNativeQuery.addHint(2).setName("BAZ"); + + assertEquals(3, javaNamedNativeQuery.hintsSize()); + + namedNativeQuery.removeHint(0); + assertEquals(2, javaNamedNativeQuery.hintsSize()); + assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName()); + + namedNativeQuery.removeHint(0); + assertEquals(1, javaNamedNativeQuery.hintsSize()); + assertEquals("BAZ", javaNamedNativeQuery.hintAt(0).getName()); + + namedNativeQuery.removeHint(0); + assertEquals(0, javaNamedNativeQuery.hintsSize()); + } + + public void testMoveHint() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + namedNativeQuery.addHint(0).setName("FOO"); + namedNativeQuery.addHint(1).setName("BAR"); + namedNativeQuery.addHint(2).setName("BAZ"); + + assertEquals(3, javaNamedNativeQuery.hintsSize()); + + + namedNativeQuery.moveHint(2, 0); + ListIterator<IQueryHint> hints = namedNativeQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAR", javaNamedNativeQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedNativeQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName()); + + + namedNativeQuery.moveHint(0, 1); + hints = namedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAZ", javaNamedNativeQuery.hintAt(0).getName()); + assertEquals("BAR", javaNamedNativeQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedNativeQuery.hintAt(2).getName()); + } + + public void testUpdateHints() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + javaNamedNativeQuery.addHint(0); + javaNamedNativeQuery.addHint(1); + javaNamedNativeQuery.addHint(2); + + javaNamedNativeQuery.hintAt(0).setName("FOO"); + javaNamedNativeQuery.hintAt(1).setName("BAR"); + javaNamedNativeQuery.hintAt(2).setName("BAZ"); + + ListIterator<IQueryHint> hints = namedNativeQuery.hints(); + assertEquals("FOO", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedNativeQuery.moveHint(2, 0); + hints = namedNativeQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedNativeQuery.moveHint(0, 1); + hints = namedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedNativeQuery.removeHint(1); + hints = namedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedNativeQuery.removeHint(1); + hints = namedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedNativeQuery.removeHint(0); + assertFalse(namedNativeQuery.hints().hasNext()); + } + + + public void testUpdateResultClass() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(null, javaNamedNativeQuery.getResultClass()); + assertEquals(null, namedNativeQuery.getResultClass()); + + //set name in the resource model, verify context model updated + javaNamedNativeQuery.setResultClass("foo"); + assertEquals("foo", javaNamedNativeQuery.getResultClass()); + assertEquals("foo", namedNativeQuery.getResultClass()); + + //set name to null in the resource model + javaNamedNativeQuery.setResultClass(null); + assertNull(javaNamedNativeQuery.getResultClass()); + assertNull(namedNativeQuery.getResultClass()); + } + + public void testModifyResultClass() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(null, javaNamedNativeQuery.getResultClass()); + assertEquals(null, namedNativeQuery.getResultClass()); + + //set name in the context model, verify resource model updated + namedNativeQuery.setResultClass("foo"); + assertEquals("foo", javaNamedNativeQuery.getResultClass()); + assertEquals("foo", namedNativeQuery.getResultClass()); + + //set name to null in the context model + namedNativeQuery.setResultClass(null); + assertNull(javaNamedNativeQuery.getResultClass()); + assertNull(namedNativeQuery.getResultClass()); + } + + public void testUpdateResultSetMapping() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(null, javaNamedNativeQuery.getResultSetMapping()); + assertEquals(null, namedNativeQuery.getResultSetMapping()); + + //set name in the resource model, verify context model updated + javaNamedNativeQuery.setResultSetMapping("foo"); + assertEquals("foo", javaNamedNativeQuery.getResultSetMapping()); + assertEquals("foo", namedNativeQuery.getResultSetMapping()); + + //set name to null in the resource model + javaNamedNativeQuery.setResultSetMapping(null); + assertNull(javaNamedNativeQuery.getResultSetMapping()); + assertNull(namedNativeQuery.getResultSetMapping()); + } + + public void testModifyResultSetMapping() throws Exception { + createTestEntityWithNamedNativeQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedNativeQuery namedNativeQuery = entity.namedNativeQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedNativeQuery javaNamedNativeQuery = (NamedNativeQuery) typeResource.annotation(NamedNativeQuery.ANNOTATION_NAME); + + assertEquals(null, javaNamedNativeQuery.getResultSetMapping()); + assertEquals(null, namedNativeQuery.getResultSetMapping()); + + //set name in the context model, verify resource model updated + namedNativeQuery.setResultSetMapping("foo"); + assertEquals("foo", javaNamedNativeQuery.getResultSetMapping()); + assertEquals("foo", namedNativeQuery.getResultSetMapping()); + + //set name to null in the context model + namedNativeQuery.setResultSetMapping(null); + assertNull(javaNamedNativeQuery.getResultSetMapping()); + assertNull(namedNativeQuery.getResultSetMapping()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedQueryTests.java new file mode 100644 index 0000000000..b891ae238e --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaNamedQueryTests.java @@ -0,0 +1,329 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.INamedQuery; +import org.eclipse.jpt.core.internal.context.base.IQueryHint; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaNamedQueryTests extends ContextModelTestCase +{ + private static final String QUERY_NAME = "QUERY_NAME"; + private static final String QUERY_QUERY = "MY_QUERY"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createNamedQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedQuery", + "String name();" + + "String query();" + + "QueryHint[] hints() default {};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", + "String name();" + + "String value();"); + } + + private IType createTestEntityWithNamedQuery() throws Exception { + createEntityAnnotation(); + createNamedQueryAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.NAMED_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@NamedQuery(name=\"" + QUERY_NAME + "\", query=\"" + QUERY_QUERY + "\")"); + } + }); + } + + + + public JavaNamedQueryTests(String name) { + super(name); + } + + public void testUpdateName() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + assertEquals(QUERY_NAME, javaNamedQuery.getName()); + assertEquals(QUERY_NAME, namedQuery.getName()); + + //set name to null in the resource model + javaNamedQuery.setName(null); + assertNull(javaNamedQuery.getName()); + assertNull(namedQuery.getName()); + + //set name in the resource model, verify context model updated + javaNamedQuery.setName("foo"); + assertEquals("foo", javaNamedQuery.getName()); + assertEquals("foo", namedQuery.getName()); + } + + public void testModifyName() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + assertEquals(QUERY_NAME, javaNamedQuery.getName()); + assertEquals(QUERY_NAME, namedQuery.getName()); + + //set name to null in the context model + namedQuery.setName(null); + assertNull(javaNamedQuery.getName()); + assertNull(namedQuery.getName()); + + //set name in the context model, verify resource model updated + namedQuery.setName("foo"); + assertEquals("foo", javaNamedQuery.getName()); + assertEquals("foo", namedQuery.getName()); + } + + public void testUpdateQuery() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + assertEquals(QUERY_QUERY, javaNamedQuery.getQuery()); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + //set name to null in the resource model + javaNamedQuery.setQuery(null); + assertNull(javaNamedQuery.getQuery()); + assertNull(namedQuery.getQuery()); + + //set name in the resource model, verify context model updated + javaNamedQuery.setQuery("foo"); + assertEquals("foo", javaNamedQuery.getQuery()); + assertEquals("foo", namedQuery.getQuery()); + } + + public void testModifyQuery() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + assertEquals(QUERY_QUERY, javaNamedQuery.getQuery()); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + //set name to null in the context model + namedQuery.setQuery(null); + assertNull(javaNamedQuery.getQuery()); + assertNull(namedQuery.getQuery()); + + //set name in the context model, verify resource model updated + namedQuery.setQuery("foo"); + assertEquals("foo", javaNamedQuery.getQuery()); + assertEquals("foo", namedQuery.getQuery()); + } + + + public void testAddHint() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + + IQueryHint queryHint = namedQuery.addHint(0); + ormResource().save(null); + queryHint.setName("FOO"); + ormResource().save(null); + + assertEquals("FOO", javaNamedQuery.hintAt(0).getName()); + + IQueryHint queryHint2 = namedQuery.addHint(0); + ormResource().save(null); + queryHint2.setName("BAR"); + ormResource().save(null); + + assertEquals("BAR", javaNamedQuery.hintAt(0).getName()); + assertEquals("FOO", javaNamedQuery.hintAt(1).getName()); + + IQueryHint queryHint3 = namedQuery.addHint(1); + ormResource().save(null); + queryHint3.setName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", javaNamedQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedQuery.hintAt(2).getName()); + + ListIterator<IQueryHint> hints = namedQuery.hints(); + assertEquals(queryHint2, hints.next()); + assertEquals(queryHint3, hints.next()); + assertEquals(queryHint, hints.next()); + + hints = namedQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + } + + public void testRemoveHint() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1).setName("BAR"); + namedQuery.addHint(2).setName("BAZ"); + + assertEquals(3, javaNamedQuery.hintsSize()); + + namedQuery.removeHint(0); + assertEquals(2, javaNamedQuery.hintsSize()); + assertEquals("BAR", javaNamedQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedQuery.hintAt(1).getName()); + + namedQuery.removeHint(0); + assertEquals(1, javaNamedQuery.hintsSize()); + assertEquals("BAZ", javaNamedQuery.hintAt(0).getName()); + + namedQuery.removeHint(0); + assertEquals(0, javaNamedQuery.hintsSize()); + } + + public void testMoveHint() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1).setName("BAR"); + namedQuery.addHint(2).setName("BAZ"); + + assertEquals(3, javaNamedQuery.hintsSize()); + + + namedQuery.moveHint(2, 0); + ListIterator<IQueryHint> hints = namedQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAR", javaNamedQuery.hintAt(0).getName()); + assertEquals("BAZ", javaNamedQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedQuery.hintAt(2).getName()); + + + namedQuery.moveHint(0, 1); + hints = namedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAZ", javaNamedQuery.hintAt(0).getName()); + assertEquals("BAR", javaNamedQuery.hintAt(1).getName()); + assertEquals("FOO", javaNamedQuery.hintAt(2).getName()); + } + + public void testUpdateHints() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + IEntity entity = javaEntity(); + INamedQuery namedQuery = entity.namedQueries().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + NamedQuery javaNamedQuery = (NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME); + + javaNamedQuery.addHint(0); + javaNamedQuery.addHint(1); + javaNamedQuery.addHint(2); + + javaNamedQuery.hintAt(0).setName("FOO"); + javaNamedQuery.hintAt(1).setName("BAR"); + javaNamedQuery.hintAt(2).setName("BAZ"); + + ListIterator<IQueryHint> hints = namedQuery.hints(); + assertEquals("FOO", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedQuery.moveHint(2, 0); + hints = namedQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedQuery.moveHint(0, 1); + hints = namedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedQuery.removeHint(1); + hints = namedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedQuery.removeHint(1); + hints = namedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + javaNamedQuery.removeHint(0); + assertFalse(namedQuery.hints().hasNext()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToManyMappingTests.java new file mode 100644 index 0000000000..c5c997b577 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToManyMappingTests.java @@ -0,0 +1,876 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.ITypeMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinTable; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.MapKey; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.OrderBy; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaOneToManyMappingTests extends ContextModelTestCase +{ + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createOneToManyAnnotation() throws Exception{ + this.createAnnotationAndMembers("OneToMany", ""); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + private IType createTestEntityWithOneToManyMapping() throws Exception { + createEntityAnnotation(); + createOneToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany").append(CR); + } + }); + } + + private IType createTestEntityWithValidOneToManyMapping() throws Exception { + createEntityAnnotation(); + createOneToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToMany").append(CR); + sb.append(" private Collection<Address> addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + private IType createTestEntityWithCollectionOneToManyMapping() throws Exception { + createEntityAnnotation(); + createOneToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToMany").append(CR); + sb.append(" private Collection addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + private IType createTestEntityWithNonCollectionOneToManyMapping() throws Exception { + createEntityAnnotation(); + createOneToManyAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_MANY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToMany").append(CR); + sb.append(" private Address addresses;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + + public JavaOneToManyMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertNull(persistentAttribute.getSpecifiedMapping()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + oneToManyMapping.setOrderBy("asdf"); + oneToManyMapping.getJoinTable().setSpecifiedName("FOO"); + assertFalse(oneToManyMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinTable.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + + public void testUpdateSpecifiedTargetEntity() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + oneToMany.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", oneToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToMany.getTargetEntity()); + + //set target entity to null in the resource model + oneToMany.setTargetEntity(null); + assertNull(oneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + } + + public void testModifySpecifiedTargetEntity() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + oneToManyMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", oneToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToMany.getTargetEntity()); + + //set target entity to null in the context model + oneToManyMapping.setSpecifiedTargetEntity(null); + assertNull(oneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getSpecifiedFetch()); + assertNull(oneToMany.getFetch()); + + //set fetch in the resource model, verify context model updated + oneToMany.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER); + assertEquals(FetchType.EAGER, oneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, oneToMany.getFetch()); + + oneToMany.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, oneToMany.getFetch()); + + + //set fetch to null in the resource model + oneToMany.setFetch(null); + assertNull(oneToManyMapping.getSpecifiedFetch()); + assertNull(oneToMany.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getSpecifiedFetch()); + assertNull(oneToMany.getFetch()); + + //set fetch in the context model, verify resource model updated + oneToManyMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(FetchType.EAGER, oneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, oneToMany.getFetch()); + + oneToManyMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, oneToMany.getFetch()); + + + //set fetch to null in the context model + oneToManyMapping.setSpecifiedFetch(null); + assertNull(oneToManyMapping.getSpecifiedFetch()); + assertNull(oneToMany.getFetch()); + } + + public void testUpdateMappedBy() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + oneToMany.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", oneToManyMapping.getMappedBy()); + assertEquals("newMappedBy", oneToMany.getMappedBy()); + + //set mappedBy to null in the resource model + oneToMany.setMappedBy(null); + assertNull(oneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(); + + assertNull(oneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + oneToManyMapping.setMappedBy("newTargetEntity"); + assertEquals("newTargetEntity", oneToManyMapping.getMappedBy()); + assertEquals("newTargetEntity", oneToMany.getMappedBy()); + + //set mappedBy to null in the context model + oneToManyMapping.setMappedBy(null); + assertNull(oneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + } + + + public void testCandidateMappedByAttributeNames() throws Exception { + createTestEntityWithValidOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + Iterator<String> attributeNames = oneToManyMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + + oneToManyMapping.setSpecifiedTargetEntity("foo"); + attributeNames = oneToManyMapping.candidateMappedByAttributeNames(); + assertFalse(attributeNames.hasNext()); + + oneToManyMapping.setSpecifiedTargetEntity(null); + attributeNames = oneToManyMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + } + + public void testDefaultTargetEntity() throws Exception { + createTestEntityWithValidOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit, default still set, handled by validation + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getDefaultTargetEntity()); + + //add targetEntity to the persistence unit + addXmlClassRef(PACKAGE_NAME + ".Address"); + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getDefaultTargetEntity()); + + //test default still the same when specified target entity it set + oneToManyMapping.setSpecifiedTargetEntity("foo"); + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getDefaultTargetEntity()); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + IJavaPersistentType addressPersistentType = addressClassRef.getJavaPersistentType(); + + //test target is not an Entity, default target entity still exists, this case handled with validation + addressPersistentType.setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityCollectionType() throws Exception { + createTestEntityWithCollectionOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertNull(oneToManyMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityNonCollectionType() throws Exception { + createTestEntityWithNonCollectionOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertNull(oneToManyMapping.getDefaultTargetEntity()); + } + + public void testTargetEntity() throws Exception { + createTestEntityWithValidOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getTargetEntity()); + + oneToManyMapping.setSpecifiedTargetEntity("foo"); + assertEquals("foo", oneToManyMapping.getTargetEntity()); + + oneToManyMapping.setSpecifiedTargetEntity(null); + assertEquals(PACKAGE_NAME + ".Address", oneToManyMapping.getTargetEntity()); + } + + public void testResolvedTargetEntity() throws Exception { + createTestEntityWithValidOneToManyMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit + assertNull(oneToManyMapping.getResolvedTargetEntity()); + + //add targetEntity to the persistence unit, now target entity should resolve + addXmlClassRef(PACKAGE_NAME + ".Address"); + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + ITypeMapping addressTypeMapping = addressClassRef.getJavaPersistentType().getMapping(); + assertEquals(addressTypeMapping, oneToManyMapping.getResolvedTargetEntity()); + + //test default still the same when specified target entity it set + oneToManyMapping.setSpecifiedTargetEntity("foo"); + assertNull(oneToManyMapping.getResolvedTargetEntity()); + + + oneToManyMapping.setSpecifiedTargetEntity(PACKAGE_NAME + ".Address"); + assertEquals(addressTypeMapping, oneToManyMapping.getResolvedTargetEntity()); + + + oneToManyMapping.setSpecifiedTargetEntity(null); + assertEquals(addressTypeMapping, oneToManyMapping.getResolvedTargetEntity()); + } + + + public void testUpdateMapKey() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(oneToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + + //set mapKey in the resource model, verify context model does not change + attributeResource.addAnnotation(MapKey.ANNOTATION_NAME); + assertNull(oneToManyMapping.getMapKey()); + MapKey mapKey = (MapKey) attributeResource.annotation(MapKey.ANNOTATION_NAME); + assertNotNull(mapKey); + + //set mapKey name in the resource model, verify context model updated + mapKey.setName("myMapKey"); + assertEquals("myMapKey", oneToManyMapping.getMapKey()); + assertEquals("myMapKey", mapKey.getName()); + + //set mapKey name to null in the resource model + mapKey.setName(null); + assertNull(oneToManyMapping.getMapKey()); + assertNull(mapKey.getName()); + + mapKey.setName("myMapKey"); + attributeResource.removeAnnotation(MapKey.ANNOTATION_NAME); + assertNull(oneToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + } + + public void testModifyMapKey() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(oneToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + + //set mapKey in the context model, verify resource model updated + oneToManyMapping.setMapKey("myMapKey"); + MapKey mapKey = (MapKey) attributeResource.annotation(MapKey.ANNOTATION_NAME); + assertEquals("myMapKey", oneToManyMapping.getMapKey()); + assertEquals("myMapKey", mapKey.getName()); + + //set mapKey to null in the context model + oneToManyMapping.setMapKey(null); + assertNull(oneToManyMapping.getMapKey()); + assertNull(attributeResource.annotation(MapKey.ANNOTATION_NAME)); + } + + public void testUpdateOrderBy() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(oneToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + + //set orderBy in the resource model, verify context model updated + attributeResource.addAnnotation(OrderBy.ANNOTATION_NAME); + OrderBy orderBy = (OrderBy) attributeResource.annotation(OrderBy.ANNOTATION_NAME); + orderBy.setValue("newOrderBy"); + assertEquals("newOrderBy", oneToManyMapping.getOrderBy()); + assertEquals("newOrderBy", orderBy.getValue()); + + //set orderBy to null in the resource model + attributeResource.removeAnnotation(OrderBy.ANNOTATION_NAME); + assertNull(oneToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testModifyOrderBy() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertNull(oneToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + + //set mappedBy in the context model, verify resource model updated + oneToManyMapping.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", oneToManyMapping.getOrderBy()); + OrderBy orderBy = (OrderBy) attributeResource.annotation(OrderBy.ANNOTATION_NAME); + assertEquals("newOrderBy", orderBy.getValue()); + + //set mappedBy to null in the context model + oneToManyMapping.setOrderBy(null); + assertNull(oneToManyMapping.getOrderBy()); + assertNull(attributeResource.annotation(OrderBy.ANNOTATION_NAME)); + } + + public void testIsNoOrdering() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertTrue(oneToManyMapping.isNoOrdering()); + + oneToManyMapping.setOrderBy("foo"); + assertFalse(oneToManyMapping.isNoOrdering()); + + oneToManyMapping.setOrderBy(null); + assertTrue(oneToManyMapping.isNoOrdering()); + } + + public void testSetNoOrdering() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertTrue(oneToManyMapping.isNoOrdering()); + + oneToManyMapping.setOrderBy("foo"); + assertFalse(oneToManyMapping.isNoOrdering()); + + oneToManyMapping.setNoOrdering(); + assertTrue(oneToManyMapping.isNoOrdering()); + assertNull(oneToManyMapping.getOrderBy()); + } +//TODO +// public boolean isOrderByPk() { +// return "".equals(getOrderBy()); +// } +// +// public void setOrderByPk() { +// setOrderBy(""); +// } + + public void testIsCustomOrdering() throws Exception { + createTestEntityWithOneToManyMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToManyMapping oneToManyMapping = (IOneToManyMapping) persistentAttribute.getMapping(); + + assertFalse(oneToManyMapping.isCustomOrdering()); + + oneToManyMapping.setOrderBy("foo"); + assertTrue(oneToManyMapping.isCustomOrdering()); + + oneToManyMapping.setOrderBy(null); + assertFalse(oneToManyMapping.isCustomOrdering()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToOneMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToOneMappingTests.java new file mode 100644 index 0000000000..29ac3a5488 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaOneToOneMappingTests.java @@ -0,0 +1,1026 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IJoinColumn; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.ITypeMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaJoinColumn; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JoinColumns; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaOneToOneMappingTests extends ContextModelTestCase +{ + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createOneToOneAnnotation() throws Exception{ + this.createAnnotationAndMembers("OneToOne", ""); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + + private IType createTestEntityWithOneToOneMapping() throws Exception { + createEntityAnnotation(); + createOneToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_ONE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne").append(CR); + } + }); + } + + private IType createTestEntityWithValidOneToOneMapping() throws Exception { + createEntityAnnotation(); + createOneToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_ONE, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToOne").append(CR); + sb.append(" private Address address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + private IType createTestEntityWithCollectionOneToOneMapping() throws Exception { + createEntityAnnotation(); + createOneToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_ONE, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToOne").append(CR); + sb.append(" private Collection address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + private IType createTestEntityWithGenericizedCollectionOneToOneMapping() throws Exception { + createEntityAnnotation(); + createOneToOneAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_ONE, JPA.ID, "java.util.Collection"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToOne").append(CR); + sb.append(" private Collection<Address> address;").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + } + }); + } + + public JavaOneToOneMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertNull(persistentAttribute.getSpecifiedMapping()); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + oneToOneMapping.addSpecifiedJoinColumn(0); + assertFalse(oneToOneMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(JoinColumn.ANNOTATION_NAME)); + } + + + public void testUpdateSpecifiedTargetEntity() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + oneToOne.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", oneToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToOne.getTargetEntity()); + + //set target entity to null in the resource model + oneToOne.setTargetEntity(null); + assertNull(oneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + } + + public void testModifySpecifiedTargetEntity() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + oneToOneMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", oneToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToOne.getTargetEntity()); + + //set target entity to null in the context model + oneToOneMapping.setSpecifiedTargetEntity(null); + assertNull(oneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + } + + public void testUpdateMappedBy() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + oneToOne.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", oneToOneMapping.getMappedBy()); + assertEquals("newMappedBy", oneToOne.getMappedBy()); + + //set mappedBy to null in the resource model + oneToOne.setMappedBy(null); + assertNull(oneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + oneToOneMapping.setMappedBy("newTargetEntity"); + assertEquals("newTargetEntity", oneToOneMapping.getMappedBy()); + assertEquals("newTargetEntity", oneToOne.getMappedBy()); + + //set mappedBy to null in the context model + oneToOneMapping.setMappedBy(null); + assertNull(oneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + } + + public void testUpdateSpecifiedOptional() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOne.getOptional()); + + //set optional in the resource model, verify context model updated + oneToOne.setOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, oneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, oneToOne.getOptional()); + + oneToOne.setOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, oneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, oneToOne.getOptional()); + + + //set optional to null in the resource model + oneToOne.setOptional(null); + assertNull(oneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOne.getOptional()); + } + + public void testModifySpecifiedOptional() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOne.getOptional()); + + //set optional in the context model, verify resource model updated + oneToOneMapping.setSpecifiedOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, oneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, oneToOne.getOptional()); + + oneToOneMapping.setSpecifiedOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, oneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, oneToOne.getOptional()); + + + //set optional to null in the context model + oneToOneMapping.setSpecifiedOptional(null); + assertNull(oneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOne.getOptional()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOne.getFetch()); + + //set fetch in the resource model, verify context model updated + oneToOne.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER); + assertEquals(FetchType.EAGER, oneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, oneToOne.getFetch()); + + oneToOne.setFetch(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, oneToOne.getFetch()); + + + //set fetch to null in the resource model + oneToOne.setFetch(null); + assertNull(oneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOne.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(); + + assertNull(oneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOne.getFetch()); + + //set fetch in the context model, verify resource model updated + oneToOneMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(FetchType.EAGER, oneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.EAGER, oneToOne.getFetch()); + + oneToOneMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.java.FetchType.LAZY, oneToOne.getFetch()); + + + //set fetch to null in the context model + oneToOneMapping.setSpecifiedFetch(null); + assertNull(oneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOne.getFetch()); + } + + public void testSpecifiedJoinColumns() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + ListIterator<IJavaJoinColumn> specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + + assertFalse(specifiedJoinColumns.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + //add an annotation to the resource model and verify the context model is updated + JoinColumn joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("FOO"); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("BAR"); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + + joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("BAZ"); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + //move an annotation to the resource model and verify the context model is updated + attributeResource.move(1, 0, JPA.JOIN_COLUMNS); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", specifiedJoinColumns.next().getName()); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", specifiedJoinColumns.next().getName()); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", specifiedJoinColumns.next().getName()); + assertFalse(specifiedJoinColumns.hasNext()); + + + attributeResource.removeAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + specifiedJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertFalse(specifiedJoinColumns.hasNext()); + } + + public void testDefaultJoinColumns() { + //TODO + } + + public void testSpecifiedJoinColumnsSize() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + assertEquals(0, oneToOneMapping.specifiedJoinColumnsSize()); + + oneToOneMapping.addSpecifiedJoinColumn(0); + assertEquals(1, oneToOneMapping.specifiedJoinColumnsSize()); + + oneToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(0, oneToOneMapping.specifiedJoinColumnsSize()); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("BAR"); + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> joinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + + assertEquals("BAZ", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAR", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("FOO", ((JoinColumn) joinColumns.next()).getName()); + assertFalse(joinColumns.hasNext()); + } + + public void testAddSpecifiedJoinColumn2() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + oneToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + oneToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Iterator<JavaResource> joinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + + assertEquals("FOO", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAR", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAZ", ((JoinColumn) joinColumns.next()).getName()); + assertFalse(joinColumns.hasNext()); + } + public void testRemoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + oneToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + oneToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + + assertEquals(3, CollectionTools.size(attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME))); + + oneToOneMapping.removeSpecifiedJoinColumn(1); + + Iterator<JavaResource> joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((JoinColumn) joinColumnResources.next()).getName()); + assertEquals("BAZ", ((JoinColumn) joinColumnResources.next()).getName()); + assertFalse(joinColumnResources.hasNext()); + + Iterator<IJoinColumn> joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + + oneToOneMapping.removeSpecifiedJoinColumn(1); + joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("FOO", ((JoinColumn) joinColumnResources.next()).getName()); + assertFalse(joinColumnResources.hasNext()); + + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + + oneToOneMapping.removeSpecifiedJoinColumn(0); + joinColumnResources = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertFalse(joinColumnResources.hasNext()); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertFalse(joinColumns.hasNext()); + + assertNull(attributeResource.annotation(JoinColumns.ANNOTATION_NAME)); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + oneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + oneToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + oneToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentAttributeResource attributeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME).attributes().next(); + + ListIterator<JoinColumn> javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals(3, CollectionTools.size(javaJoinColumns)); + + + oneToOneMapping.moveSpecifiedJoinColumn(2, 0); + ListIterator<IJoinColumn> primaryKeyJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("BAR", javaJoinColumns.next().getName()); + assertEquals("BAZ", javaJoinColumns.next().getName()); + assertEquals("FOO", javaJoinColumns.next().getName()); + + + oneToOneMapping.moveSpecifiedJoinColumn(0, 1); + primaryKeyJoinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getSpecifiedName()); + + javaJoinColumns = attributeResource.annotations(JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + assertEquals("BAZ", javaJoinColumns.next().getName()); + assertEquals("BAR", javaJoinColumns.next().getName()); + assertEquals("FOO", javaJoinColumns.next().getName()); + } + + public void testUpdateSpecifiedJoinColumns() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + JavaPersistentAttributeResource attributeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME).attributes().next(); + + ((JoinColumn) attributeResource.addAnnotation(0, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("FOO"); + ((JoinColumn) attributeResource.addAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("BAR"); + ((JoinColumn) attributeResource.addAnnotation(2, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME)).setName("BAZ"); + + ListIterator<IJoinColumn> joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.move(2, 0, JoinColumns.ANNOTATION_NAME); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.move(0, 1, JoinColumns.ANNOTATION_NAME); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(1, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + attributeResource.removeAnnotation(0, JoinColumn.ANNOTATION_NAME, JoinColumns.ANNOTATION_NAME); + joinColumns = oneToOneMapping.specifiedJoinColumns(); + assertFalse(joinColumns.hasNext()); + } + + public void testJoinColumnIsVirtual() throws Exception { + createTestEntityWithOneToOneMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + oneToOneMapping.addSpecifiedJoinColumn(0); + IJoinColumn specifiedJoinColumn = oneToOneMapping.specifiedJoinColumns().next(); + assertFalse(specifiedJoinColumn.isVirtual()); + + IJoinColumn defaultJoinColumn = oneToOneMapping.defaultJoinColumns().next(); + assertTrue(defaultJoinColumn.isVirtual()); + } + + + public void testCandidateMappedByAttributeNames() throws Exception { + createTestEntityWithValidOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + Iterator<String> attributeNames = oneToOneMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + + oneToOneMapping.setSpecifiedTargetEntity("foo"); + attributeNames = oneToOneMapping.candidateMappedByAttributeNames(); + assertFalse(attributeNames.hasNext()); + + oneToOneMapping.setSpecifiedTargetEntity(null); + attributeNames = oneToOneMapping.candidateMappedByAttributeNames(); + assertEquals("id", attributeNames.next()); + assertEquals("city", attributeNames.next()); + assertEquals("state", attributeNames.next()); + assertEquals("zip", attributeNames.next()); + assertFalse(attributeNames.hasNext()); + } + + public void testDefaultTargetEntity() throws Exception { + createTestEntityWithValidOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit, default still set, handled by validation + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getDefaultTargetEntity()); + + //add targetEntity to the persistence unit + addXmlClassRef(PACKAGE_NAME + ".Address"); + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getDefaultTargetEntity()); + + //test default still the same when specified target entity it set + oneToOneMapping.setSpecifiedTargetEntity("foo"); + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getDefaultTargetEntity()); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + IJavaPersistentType addressPersistentType = addressClassRef.getJavaPersistentType(); + + //test target is not an Entity, default target entity still exists, this case handled with validation + addressPersistentType.setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityCollectionType() throws Exception { + createTestEntityWithCollectionOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + assertNull(oneToOneMapping.getDefaultTargetEntity()); + } + + public void testDefaultTargetEntityGenericizedCollectionType() throws Exception { + createTestEntityWithGenericizedCollectionOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".Address"); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + assertNull(oneToOneMapping.getDefaultTargetEntity()); + } + + public void testTargetEntity() throws Exception { + createTestEntityWithValidOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getTargetEntity()); + + oneToOneMapping.setSpecifiedTargetEntity("foo"); + assertEquals("foo", oneToOneMapping.getTargetEntity()); + + oneToOneMapping.setSpecifiedTargetEntity(null); + assertEquals(PACKAGE_NAME + ".Address", oneToOneMapping.getTargetEntity()); + } + + public void testResolvedTargetEntity() throws Exception { + createTestEntityWithValidOneToOneMapping(); + createTestTargetEntityAddress(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IOneToOneMapping oneToOneMapping = (IOneToOneMapping) persistentAttribute.getMapping(); + + //targetEntity not in the persistence unit + assertNull(oneToOneMapping.getResolvedTargetEntity()); + + //add targetEntity to the persistence unit, now target entity should resolve + addXmlClassRef(PACKAGE_NAME + ".Address"); + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef addressClassRef = classRefs.next(); + ITypeMapping addressTypeMapping = addressClassRef.getJavaPersistentType().getMapping(); + assertEquals(addressTypeMapping, oneToOneMapping.getResolvedTargetEntity()); + + //test default still the same when specified target entity it set + oneToOneMapping.setSpecifiedTargetEntity("foo"); + assertNull(oneToOneMapping.getResolvedTargetEntity()); + + + oneToOneMapping.setSpecifiedTargetEntity(PACKAGE_NAME + ".Address"); + assertEquals(addressTypeMapping, oneToOneMapping.getResolvedTargetEntity()); + + + oneToOneMapping.setSpecifiedTargetEntity(null); + assertEquals(addressTypeMapping, oneToOneMapping.getResolvedTargetEntity()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentAttributeTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentAttributeTests.java new file mode 100644 index 0000000000..d76e250805 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentAttributeTests.java @@ -0,0 +1,226 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.java.IJavaBasicMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.java.IJavaIdMapping; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaPersistentAttributeTests extends ContextModelTestCase +{ + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + + private IType createTestEntityAnnotatedField() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityAnnotatedMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + + public JavaPersistentAttributeTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + + assertEquals("id", persistentAttribute.getName()); + } + + public void testGetMapping() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertTrue(persistentAttribute.getMapping() instanceof IJavaIdMapping); + + persistentAttribute.setSpecifiedMappingKey(null); + assertTrue(persistentAttribute.getMapping() instanceof IJavaBasicMapping); + } + + public void testGetSpecifiedMapping() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertTrue(persistentAttribute.getSpecifiedMapping() instanceof IJavaIdMapping); + + persistentAttribute.setSpecifiedMappingKey(null); + assertNull(persistentAttribute.getSpecifiedMapping()); + } + + public void testGetSpecifiedMappingNull() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + + assertNull(persistentAttribute.getSpecifiedMapping()); + assertNotNull(persistentAttribute.getMapping()); + } + + public void testMappingKey() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + + persistentAttribute.setSpecifiedMappingKey(null); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + } + + public void testDefaultMappingKey() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.defaultMappingKey()); + } + + public void testSetSpecifiedMappingKey() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertNull(persistentAttribute.getSpecifiedMapping()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNotNull(attributeResource.mappingAnnotation()); + assertTrue(attributeResource.mappingAnnotation() instanceof Embedded); + + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + assertTrue(persistentAttribute.getSpecifiedMapping() instanceof IJavaEmbeddedMapping); + } + + public void testSetSpecifiedMappingKey2() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNotNull(attributeResource.mappingAnnotation()); + assertTrue(attributeResource.mappingAnnotation() instanceof Embedded); + + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + assertTrue(persistentAttribute.getSpecifiedMapping() instanceof IJavaEmbeddedMapping); + } + + public void testSetSpecifiedMappingKeyNull() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation()); + assertNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + + assertNull(persistentAttribute.getSpecifiedMapping()); + } + + public void testGetMappingKeyMappingChangeInResourceModel() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.setMappingAnnotation(Embedded.ANNOTATION_NAME); + + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, persistentAttribute.mappingKey()); + } + + public void testGetMappingKeyMappingChangeInResourceModel2() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + assertNull(persistentAttribute.getSpecifiedMapping()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + attributeResource.setMappingAnnotation(Basic.ANNOTATION_NAME); + + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, persistentAttribute.getSpecifiedMapping().getKey()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentTypeTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentTypeTests.java new file mode 100644 index 0000000000..3a8aac52cc --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPersistentTypeTests.java @@ -0,0 +1,701 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.IPersistentType; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentAttribute; +import org.eclipse.jpt.core.internal.context.java.IJavaPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.Embeddable; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaPersistentTypeTests extends ContextModelTestCase +{ + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityAnnotatedField() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityAnnotatedMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + private IType createTestEntityAnnotatedFieldAndMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild.java", "AnnotationTestTypeChild", new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends " + TYPE_NAME + " "); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + }); + } + + private IType createTestSubTypeWithFieldAnnotation() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild.java", "AnnotationTestTypeChild", new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends " + TYPE_NAME + " "); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubTypeWithMethodAnnotation() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild.java", "AnnotationTestTypeChild", new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends " + TYPE_NAME + " "); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubTypeNonPersistent() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild.java", "AnnotationTestTypeChild", new DefaultAnnotationWriter() { + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends " + TYPE_NAME + " "); + } + }); + } + + private IType createTestSubTypePersistentExtendsNonPersistent() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild2.java", "AnnotationTestTypeChild2", new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends AnnotationTestTypeChild "); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + public JavaPersistentTypeTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, javaPersistentType().getName()); + } + + public void testGetAccessNothingAnnotated() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(AccessType.FIELD, javaPersistentType().access()); + } + + public void testAccessField() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(AccessType.FIELD, javaPersistentType().access()); + } + + public void testAccessProperty() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(AccessType.PROPERTY, javaPersistentType().access()); + } + + public void testAccessFieldAndMethodAnnotated() throws Exception { + createTestEntityAnnotatedFieldAndMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(AccessType.FIELD, javaPersistentType().access()); + } + + public void testAccessInheritance() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubType(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef classRef = classRefs.next(); + + IJavaPersistentType javaPersistentType = classRef.getJavaPersistentType(); + assertEquals(PACKAGE_NAME + ".AnnotationTestTypeChild", javaPersistentType.getName()); + + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessInheritance2() throws Exception { + createTestEntityAnnotatedField(); + createTestSubType(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef classRef = classRefs.next(); + + IJavaPersistentType javaPersistentType = classRef.getJavaPersistentType(); + assertEquals(PACKAGE_NAME + ".AnnotationTestTypeChild", javaPersistentType.getName()); + + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + public void testAccessInheritance3() throws Exception { + createTestEntityAnnotatedField(); + createTestSubTypeWithMethodAnnotation(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef classRef = classRefs.next(); + + IJavaPersistentType javaPersistentType = classRef.getJavaPersistentType(); + assertEquals(PACKAGE_NAME + ".AnnotationTestTypeChild", javaPersistentType.getName()); + + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessInheritance4() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeWithFieldAnnotation(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef classRef = classRefs.next(); + IJavaPersistentType javaPersistentType = classRef.getJavaPersistentType(); + + assertEquals(PACKAGE_NAME + ".AnnotationTestTypeChild", javaPersistentType.getName()); + + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + //inherited class having annotations set wins over the default access set on persistence-unit-defaults + public void testAccessInheritancePersistenceUnitDefaultAccess() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubType(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.FIELD); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + classRefs.next(); + IClassRef classRef = classRefs.next(); + IJavaPersistentType javaPersistentType = classRef.getJavaPersistentType(); + + assertEquals(PACKAGE_NAME + ".AnnotationTestTypeChild", javaPersistentType.getName()); + + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessXmlNoAccessNoAnnotations() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntity(); + + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + public void testAccessXmlEntityAccessNoAnnotations() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntity(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityPersistentType.getMapping().setSpecifiedAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + + entityPersistentType.getMapping().setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessXmlPersistentUnitDefaultsAccessNoAnnotations() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntity(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessXmlEntityPropertyAccessAndFieldAnnotations() throws Exception { + //xml access set to property, field annotations, JavaPersistentType access is property + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntityAnnotatedField(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityPersistentType.getMapping().setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + } + + public void testAccessXmlEntityFieldAccessAndPropertyAnnotations() throws Exception { + //xml access set to field, property annotations, JavaPersistentType access is field + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntityAnnotatedMethod(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityPersistentType.getMapping().setSpecifiedAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + public void testAccessXmlPersistentUnitDefaultsAccessFieldAnnotations() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntityAnnotatedField(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + //inheritance wins over entity-mappings specified access + public void testAccessXmlEntityMappingsAccessWithInheritance() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childEntityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + + createTestEntityAnnotatedMethod(); + createTestSubType(); + IJavaPersistentType childJavaPersistentType = childEntityPersistentType.javaPersistentType(); + + entityMappings().setSpecifiedAccess(AccessType.FIELD); + assertEquals(AccessType.PROPERTY, entityPersistentType.javaPersistentType().access()); + assertEquals(AccessType.PROPERTY, childJavaPersistentType.access()); + } + + public void testAccessXmlMetadataCompleteFieldAnnotations() throws Exception { + //xml access set to property, so even though there are field annotations, JavaPersistentType + //access should be property + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntityAnnotatedField(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertEquals(AccessType.PROPERTY, javaPersistentType.access()); + + } + + public void testAccessNoXmlAccessXmlMetdataCompletePropertyAnnotations() throws Exception { + //xml access not set, metadata complete set. JavaPersistentType access + //is field?? + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + createTestEntityAnnotatedMethod(); + IJavaPersistentType javaPersistentType = entityPersistentType.javaPersistentType(); + + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertEquals(AccessType.FIELD, javaPersistentType.access()); + } + + public void testParentPersistentType() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeWithFieldAnnotation(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + IClassRef classRef = classRefs.next(); + IJavaPersistentType rootJavaPersistentType = classRef.getJavaPersistentType(); + + classRef = classRefs.next(); + IJavaPersistentType childJavaPersistentType = classRef.getJavaPersistentType(); + + assertEquals(rootJavaPersistentType, childJavaPersistentType.parentPersistentType()); + assertNull(rootJavaPersistentType.parentPersistentType()); + } + + public void testParentPersistentType2() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeWithFieldAnnotation(); + + //parent is not added to the persistenceUnit, so even though it has an Entity + //annotation it should not be found as the parentPersistentType + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + IJavaPersistentType javaPersistentType = classRefs.next().getJavaPersistentType(); + + assertNull(javaPersistentType.parentPersistentType()); + } + + //Entity extends Non-Entity extends Entity + public void testParentPersistentType3() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeNonPersistent(); + createTestSubTypePersistentExtendsNonPersistent(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild2"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + IClassRef classRef = classRefs.next(); + IJavaPersistentType rootJavaPersistentType = classRef.getJavaPersistentType(); + + classRef = classRefs.next(); + IJavaPersistentType childJavaPersistentType = classRef.getJavaPersistentType(); + + assertEquals(rootJavaPersistentType, childJavaPersistentType.parentPersistentType()); + assertNull(rootJavaPersistentType.parentPersistentType()); + } + + public void testInheritanceHierarchy() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeNonPersistent(); + createTestSubTypePersistentExtendsNonPersistent(); + + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild2"); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + IJavaPersistentType rootJavaPersistentType = classRefs.next().getJavaPersistentType(); + IJavaPersistentType childJavaPersistentType = classRefs.next().getJavaPersistentType(); + + Iterator<IPersistentType> inheritanceHierarchy = childJavaPersistentType.inheritanceHierarchy(); + + assertEquals(childJavaPersistentType, inheritanceHierarchy.next()); + assertEquals(rootJavaPersistentType, inheritanceHierarchy.next()); + } + + public void testInheritanceHierarchy2() throws Exception { + createTestEntityAnnotatedMethod(); + createTestSubTypeNonPersistent(); + createTestSubTypePersistentExtendsNonPersistent(); + + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild2"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IClassRef> classRefs = persistenceUnit().classRefs(); + IJavaPersistentType childJavaPersistentType = classRefs.next().getJavaPersistentType(); + IJavaPersistentType rootJavaPersistentType = classRefs.next().getJavaPersistentType(); + + Iterator<IPersistentType> inheritanceHierarchy = childJavaPersistentType.inheritanceHierarchy(); + + assertEquals(childJavaPersistentType, inheritanceHierarchy.next()); + assertEquals(rootJavaPersistentType, inheritanceHierarchy.next()); + } + + public void testGetMapping() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().getMapping().getKey()); + } + + public void testGetMappingNull() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.NULL_TYPE_MAPPING_KEY, javaPersistentType().getMapping().getKey()); + } + + public void testMappingKey() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testMappingKeyNull() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.NULL_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testSetMappingKey() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.NULL_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + + javaPersistentType().setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNotNull(typeResource.mappingAnnotation()); + assertTrue(typeResource.mappingAnnotation() instanceof Entity); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testSetMappingKey2() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + + javaPersistentType().setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNotNull(typeResource.mappingAnnotation()); + assertTrue(typeResource.mappingAnnotation() instanceof Embeddable); + + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testSetMappingKeyNull() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + + javaPersistentType().setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.mappingAnnotation()); + assertNull(typeResource.mappingAnnotation(Entity.ANNOTATION_NAME)); + + assertEquals(IMappingKeys.NULL_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testGetMappingKeyMappingChangeInResourceModel() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.setMappingAnnotation(Embeddable.ANNOTATION_NAME); + + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testGetMappingKeyMappingChangeInResourceModel2() throws Exception { + createTestType(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(IMappingKeys.NULL_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + typeResource.setMappingAnnotation(Entity.ANNOTATION_NAME); + + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, javaPersistentType().mappingKey()); + } + + public void testIsMapped() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertTrue(javaPersistentType().isMapped()); + + javaPersistentType().setMappingKey(IMappingKeys.NULL_TYPE_MAPPING_KEY); + assertFalse(javaPersistentType().isMapped()); + } + + public void testAttributes() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ListIterator<IJavaPersistentAttribute> attributes = javaPersistentType().attributes(); + + assertEquals("id", attributes.next().getName()); + assertFalse(attributes.hasNext()); + } + + public void testAttributes2() throws Exception { + createTestEntityAnnotatedFieldAndMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ListIterator<IJavaPersistentAttribute> attributes = javaPersistentType().attributes(); + + assertEquals("id", attributes.next().getName()); + assertEquals("name", attributes.next().getName()); + assertFalse(attributes.hasNext()); + } + + public void testAttributesSize() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(1, javaPersistentType().attributesSize()); + } + + public void testAttributesSize2() throws Exception { + createTestEntityAnnotatedFieldAndMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(2, javaPersistentType().attributesSize()); + } + + public void testAttributeNamed() throws Exception { + createTestEntityAnnotatedMethod(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute attribute = javaPersistentType().attributeNamed("id"); + + assertEquals("id", attribute.getName()); + assertNull(javaPersistentType().attributeNamed("name")); + assertNull(javaPersistentType().attributeNamed("foo")); + } + + public void testAttributeNamed2() throws Exception { + createTestEntityAnnotatedField(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute attribute = javaPersistentType().attributeNamed("name"); + + assertEquals("name", attribute.getName()); + + assertNull(javaPersistentType().attributeNamed("foo")); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPrimaryKeyJoinColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPrimaryKeyJoinColumnTests.java new file mode 100644 index 0000000000..412b74a123 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaPrimaryKeyJoinColumnTests.java @@ -0,0 +1,319 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.IPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaPrimaryKeyJoinColumnTests extends ContextModelTestCase +{ + private static final String PRIMARY_KEY_JOIN_COLUMN_NAME = "MY_PRIMARY_KEY_JOIN_COLUMN"; + private static final String COLUMN_DEFINITION = "MY_COLUMN_DEFINITION"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception{ + this.createAnnotationAndMembers("Id", ""); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", + "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "String columnDefinition() default \"\";"); + } + + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityWithPrimaryKeyJoinColumn() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + createPrimaryKeyJoinColumnAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@PrimaryKeyJoinColumn(name=\"" + PRIMARY_KEY_JOIN_COLUMN_NAME + "\")"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + + public JavaPrimaryKeyJoinColumnTests(String name) { + super(name); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals(PRIMARY_KEY_JOIN_COLUMN_NAME, specifiedPkJoinColumn.getSpecifiedName()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn pkJoinColumnResource = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + pkJoinColumnResource.setName("FOO"); + specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals("FOO", specifiedPkJoinColumn.getName()); + } + + public void testGetDefaultNameNoSpecifiedPrimaryKeyJoinColumns() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn pkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", pkJoinColumn.getDefaultName()); + } + + public void testGetDefaultName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn defaultPkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", defaultPkJoinColumn.getDefaultName()); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals("id", specifiedPkJoinColumn.getDefaultName()); + + //remove @Id annotation + IPersistentAttribute idAttribute = javaPersistentType().attributeNamed("id"); + idAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + + assertNull(specifiedPkJoinColumn.getDefaultName()); + } + + public void testGetName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals(PRIMARY_KEY_JOIN_COLUMN_NAME, specifiedPkJoinColumn.getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + + specifiedPkJoinColumn.setSpecifiedName("foo"); + assertEquals("foo", specifiedPkJoinColumn.getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertEquals("foo", pkJoinColumn.getName()); + + specifiedPkJoinColumn.setSpecifiedName(null); + assertNull(specifiedPkJoinColumn.getSpecifiedName()); + pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertNull(pkJoinColumn); + } + + public void testGetColumnDefinition() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertNull(specifiedPkJoinColumn.getColumnDefinition()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + column.setColumnDefinition(COLUMN_DEFINITION); + + assertEquals(COLUMN_DEFINITION, specifiedPkJoinColumn.getColumnDefinition()); + + column.setColumnDefinition(null); + + assertNull(specifiedPkJoinColumn.getColumnDefinition()); + + typeResource.removeAnnotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertEquals(0, javaEntity().specifiedPrimaryKeyJoinColumnsSize()); + } + + public void testSetColumnDefinition() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + specifiedPkJoinColumn.setColumnDefinition("foo"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertEquals("foo", column.getColumnDefinition()); + + specifiedPkJoinColumn.setColumnDefinition(null); + column = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertNull(column.getColumnDefinition()); + } + + public void testGetSpecifiedReferencedColumnName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertNull(specifiedPkJoinColumn.getSpecifiedReferencedColumnName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn pkJoinColumnResource = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + pkJoinColumnResource.setReferencedColumnName("FOO"); + specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals("FOO", specifiedPkJoinColumn.getSpecifiedReferencedColumnName()); + } + + public void testGetDefaultReferencedColumnNameNoSpecifiedPrimaryKeyJoinColumns() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn pkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", pkJoinColumn.getDefaultReferencedColumnName()); + } + + public void testGetDefaultReferencedColumnName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn defaultPkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", defaultPkJoinColumn.getDefaultReferencedColumnName()); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals("id", specifiedPkJoinColumn.getDefaultReferencedColumnName()); + + + //remove @Id annotation + IPersistentAttribute idAttribute = javaPersistentType().attributeNamed("id"); + idAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + + assertNull(specifiedPkJoinColumn.getDefaultReferencedColumnName()); + } + + public void testGetReferencedColumnName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn pkJoinColumnResource = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + pkJoinColumnResource.setReferencedColumnName("FOO"); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertEquals("FOO", specifiedPkJoinColumn.getReferencedColumnName()); + } + + public void testSetSpecifiedReferencedColumnName() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + + specifiedPkJoinColumn.setSpecifiedReferencedColumnName("foo"); + assertEquals("foo", specifiedPkJoinColumn.getSpecifiedReferencedColumnName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + PrimaryKeyJoinColumn pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertEquals("foo", pkJoinColumn.getReferencedColumnName()); + + specifiedPkJoinColumn.setSpecifiedName(null); + specifiedPkJoinColumn.setSpecifiedReferencedColumnName(null); + assertNull(specifiedPkJoinColumn.getSpecifiedReferencedColumnName()); + pkJoinColumn = (PrimaryKeyJoinColumn) typeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertNull(pkJoinColumn); + } + + public void testIsVirtual() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertFalse(specifiedPkJoinColumn.isVirtual()); + + IPrimaryKeyJoinColumn defaultPkJoinColumn = javaEntity().defaultPrimaryKeyJoinColumns().next(); + assertTrue(defaultPkJoinColumn.isVirtual()); + } + + public void testIsReferencedColumnResolved() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertFalse(specifiedPkJoinColumn.isReferencedColumnResolved()); + } + + public void testDbColumn() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertNull(specifiedPkJoinColumn.dbColumn()); + } + + public void testDbReferencedColumn() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertNull(specifiedPkJoinColumn.dbReferencedColumn()); + } + + public void testDbTable() throws Exception { + createTestEntityWithPrimaryKeyJoinColumn(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPrimaryKeyJoinColumn specifiedPkJoinColumn = javaEntity().specifiedPrimaryKeyJoinColumns().next(); + assertNull(specifiedPkJoinColumn.dbTable()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaQueryHintTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaQueryHintTests.java new file mode 100644 index 0000000000..3c3a7acdcf --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaQueryHintTests.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IEntity; +import org.eclipse.jpt.core.internal.context.base.IQueryHint; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaQueryHintTests extends ContextModelTestCase +{ + private static final String QUERY_NAME = "QUERY_NAME"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createNamedQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedQuery", + "String name();" + + "String query();" + + "QueryHint[] hints() default {};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", + "String name();" + + "String value();"); + } + + private IType createTestEntityWithNamedQuery() throws Exception { + createEntityAnnotation(); + createNamedQueryAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.NAMED_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@NamedQuery(name=\"" + QUERY_NAME + "\", hints=@QueryHint())"); + } + }); + } + + + + public JavaQueryHintTests(String name) { + super(name); + } + + public void testUpdateName() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + IQueryHint queryHint = entity.namedQueries().next().hints().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + QueryHint javaQueryHint = ((NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME)).hints().next(); + + assertNull(javaQueryHint.getName()); + assertNull(queryHint.getName()); + + //set name in the resource model, verify context model updated + javaQueryHint.setName("foo"); + assertEquals("foo", javaQueryHint.getName()); + assertEquals("foo", queryHint.getName()); + + //set name to null in the resource model + javaQueryHint.setName(null); + assertNull(javaQueryHint.getName()); + assertEquals(0, entity.namedQueries().next().hintsSize()); + } + + public void testModifyName() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + IQueryHint queryHint = entity.namedQueries().next().hints().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + QueryHint javaQueryhint = ((NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME)).hints().next(); + + assertNull(javaQueryhint.getName()); + assertNull(queryHint.getName()); + + //set name in the context model, verify resource model updated + queryHint.setName("foo"); + assertEquals("foo", javaQueryhint.getName()); + assertEquals("foo", queryHint.getName()); + + //set name to null in the context model + queryHint.setName(null); + assertNull(javaQueryhint.getName()); + assertNull(queryHint.getName()); + } + + public void testUpdateValue() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + IQueryHint queryHint = entity.namedQueries().next().hints().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + QueryHint javaQueryhint = ((NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME)).hints().next(); + + assertNull(javaQueryhint.getValue()); + assertNull(queryHint.getValue()); + + //set name in the resource model, verify context model updated + javaQueryhint.setValue("foo"); + assertEquals("foo", javaQueryhint.getValue()); + assertEquals("foo", queryHint.getValue()); + + //set name to null in the resource model + javaQueryhint.setValue(null); + assertNull(javaQueryhint.getValue()); + assertEquals(0, entity.namedQueries().next().hintsSize()); + } + + public void testModifyValue() throws Exception { + createTestEntityWithNamedQuery(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IEntity entity = javaEntity(); + IQueryHint queryHint = entity.namedQueries().next().hints().next(); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + QueryHint javaQueryhint = ((NamedQuery) typeResource.annotation(NamedQuery.ANNOTATION_NAME)).hints().next(); + + assertNull(javaQueryhint.getValue()); + assertNull(queryHint.getValue()); + + //set name in the context model, verify resource model updated + queryHint.setValue("foo"); + assertEquals("foo", javaQueryhint.getValue()); + assertEquals("foo", queryHint.getValue()); + + //set name to null in the context model + queryHint.setValue(null); + assertNull(javaQueryhint.getValue()); + assertNull(queryHint.getValue()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSecondaryTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSecondaryTableTests.java new file mode 100644 index 0000000000..945dbf2365 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSecondaryTableTests.java @@ -0,0 +1,496 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.IPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.context.base.ISecondaryTable; +import org.eclipse.jpt.core.internal.context.java.IJavaPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTable; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaSecondaryTableTests extends ContextModelTestCase +{ + private static final String TABLE_NAME = "MY_TABLE"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception{ + this.createAnnotationAndMembers("Id", ""); + } + + private void createSecondaryTableAnnotation() throws Exception{ + this.createAnnotationAndMembers("eSecondaryTable", + "String name() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";"); + } + + private void createSecondaryTablesAnnotation() throws Exception { + createSecondaryTableAnnotation(); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + } + + private IType createTestEntityWithSecondaryTable() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + createSecondaryTableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@SecondaryTable(name=\"" + TABLE_NAME + "\")"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityWithSecondaryTables() throws Exception { + createEntityAnnotation(); + createSecondaryTablesAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@SecondaryTables({@SecondaryTable(name=\"foo\"), @SecondaryTable(name=\"bar\")})"); + } + }); + } + + + + + public JavaSecondaryTableTests(String name) { + super(name); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().secondaryTables().next(); + assertEquals(TABLE_NAME, secondaryTable.getSpecifiedName()); + } + + public void testGetDefaultNameNull() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().secondaryTables().next(); + assertNull(secondaryTable.getDefaultName()); + } + + public void testGetName() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().secondaryTables().next(); + assertEquals(TABLE_NAME, secondaryTable.getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().secondaryTables().next(); + secondaryTable.setSpecifiedName("foo"); + + assertEquals("foo", javaEntity().secondaryTables().next().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals("foo", table.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().secondaryTables().next(); + secondaryTable.setSpecifiedName(null); + + assertEquals(0, javaEntity().secondaryTablesSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + } + + public void testUpdateFromSpecifiedNameChangeInResourceModel() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + table.setName("foo"); + + assertEquals("foo", javaEntity().secondaryTables().next().getSpecifiedName()); + } + + public void testUpdateFromSpecifiedCatalogChangeInResourceModel() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + ListIterator<SecondaryTable> secondaryTableResources = typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResources.next().setCatalog("foo"); + secondaryTableResources.next().setCatalog("bar"); + + ListIterator<ISecondaryTable> secondaryTsbles = javaEntity().secondaryTables(); + assertEquals("foo", secondaryTsbles.next().getSpecifiedCatalog()); + assertEquals("bar", secondaryTsbles.next().getSpecifiedCatalog()); + } + + public void testUpdateFromSpecifiedSchemaChangeInResourceModel() throws Exception { + createTestEntityWithSecondaryTables(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + ListIterator<SecondaryTable> secondaryTableResources = typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResources.next().setSchema("foo"); + secondaryTableResources.next().setSchema("bar"); + + ListIterator<ISecondaryTable> secondaryTsbles = javaEntity().secondaryTables(); + assertEquals("foo", secondaryTsbles.next().getSpecifiedSchema()); + assertEquals("bar", secondaryTsbles.next().getSpecifiedSchema()); + } + + public void testGetCatalog() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.setCatalog("myCatalog"); + + assertEquals("myCatalog", javaEntity().secondaryTables().next().getSpecifiedCatalog()); + assertEquals("myCatalog", javaEntity().secondaryTables().next().getCatalog()); + } + + public void testGetDefaultCatalog() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().secondaryTables().next().getDefaultCatalog()); + + javaEntity().secondaryTables().next().setSpecifiedCatalog("myCatalog"); + + assertNull(javaEntity().secondaryTables().next().getDefaultCatalog()); + } + + public void testSetSpecifiedCatalog() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ISecondaryTable table = javaEntity().secondaryTables().next(); + table.setSpecifiedCatalog("myCatalog"); + table.setSpecifiedName(null); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals("myCatalog", tableResource.getCatalog()); + + table.setSpecifiedCatalog(null); + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + } + + public void testGetSchema() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + tableResource.setSchema("mySchema"); + + assertEquals("mySchema", javaEntity().secondaryTables().next().getSpecifiedSchema()); + assertEquals("mySchema", javaEntity().secondaryTables().next().getSchema()); + } + + public void testGetDefaultSchema() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().secondaryTables().next().getDefaultSchema()); + + javaEntity().secondaryTables().next().setSpecifiedSchema("mySchema"); + + assertNull(javaEntity().secondaryTables().next().getDefaultSchema()); + } + + public void testSetSpecifiedSchema() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ISecondaryTable table = javaEntity().secondaryTables().next(); + table.setSpecifiedSchema("mySchema"); + table.setSpecifiedName(null); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals("mySchema", tableResource.getSchema()); + + table.setSpecifiedSchema(null); + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + } + + public void testSpecifiedPrimaryKeyJoinColumns() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + ListIterator<IJavaPrimaryKeyJoinColumn> specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + + assertFalse(specifiedPkJoinColumns.hasNext()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + //add an annotation to the resource model and verify the context model is updated + PrimaryKeyJoinColumn pkJoinColumn = tableResource.addPkJoinColumn(0); + pkJoinColumn.setName("FOO"); + specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + pkJoinColumn = tableResource.addPkJoinColumn(0); + pkJoinColumn.setName("BAR"); + specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + + //move an annotation to the resource model and verify the context model is updated + tableResource.movePkJoinColumn(1, 0); + specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + tableResource.removePkJoinColumn(0); + specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertFalse(specifiedPkJoinColumns.hasNext()); + + tableResource.removePkJoinColumn(0); + specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertFalse(specifiedPkJoinColumns.hasNext()); + } + + public void testSpecifiedPrimaryKeyJoinColumnsSize() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + assertEquals(0, secondaryTable.specifiedPrimaryKeyJoinColumnsSize()); + + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAR"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAZ"); + + assertEquals(3, secondaryTable.specifiedPrimaryKeyJoinColumnsSize()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAR"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("BAZ"); + + Iterator<IPrimaryKeyJoinColumn> specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + Iterator<PrimaryKeyJoinColumn> pkJoinColumns = tableResource.pkJoinColumns(); + + assertEquals("BAZ", pkJoinColumns.next().getName()); + assertEquals("BAR", pkJoinColumns.next().getName()); + assertEquals("FOO", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn2() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + Iterator<IPrimaryKeyJoinColumn> specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + Iterator<PrimaryKeyJoinColumn> pkJoinColumns = tableResource.pkJoinColumns(); + + assertEquals("FOO", pkJoinColumns.next().getName()); + assertEquals("BAR", pkJoinColumns.next().getName()); + assertEquals("BAZ", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + } + public void testRemoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals(3, tableResource.pkJoinColumnsSize()); + + secondaryTable.removeSpecifiedPrimaryKeyJoinColumn(1); + + Iterator<PrimaryKeyJoinColumn> pkJoinColumnResources = tableResource.pkJoinColumns(); + assertEquals("FOO", pkJoinColumnResources.next().getName()); + assertEquals("BAZ", pkJoinColumnResources.next().getName()); + assertFalse(pkJoinColumnResources.hasNext()); + + Iterator<IPrimaryKeyJoinColumn> pkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", pkJoinColumns.next().getName()); + assertEquals("BAZ", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + + + secondaryTable.removeSpecifiedPrimaryKeyJoinColumn(1); + pkJoinColumnResources = tableResource.pkJoinColumns(); + assertEquals("FOO", pkJoinColumnResources.next().getName()); + assertFalse(pkJoinColumnResources.hasNext()); + + pkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", pkJoinColumns.next().getName()); + assertFalse(pkJoinColumns.hasNext()); + + + secondaryTable.removeSpecifiedPrimaryKeyJoinColumn(0); + pkJoinColumnResources = tableResource.pkJoinColumns(); + assertFalse(pkJoinColumnResources.hasNext()); + pkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertFalse(pkJoinColumns.hasNext()); + + assertEquals(0, tableResource.pkJoinColumnsSize()); + } + + public void testMoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + Iterator<IPrimaryKeyJoinColumn> specifiedPkJoinColumns = secondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", specifiedPkJoinColumns.next().getName()); + assertEquals("BAR", specifiedPkJoinColumns.next().getName()); + assertEquals("BAZ", specifiedPkJoinColumns.next().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + SecondaryTable tableResource = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + Iterator<PrimaryKeyJoinColumn> pkJoinColumns = tableResource.pkJoinColumns(); + + assertEquals("FOO", pkJoinColumns.next().getName()); + assertEquals("BAR", pkJoinColumns.next().getName()); + assertEquals("BAZ", pkJoinColumns.next().getName()); + + + secondaryTable.moveSpecifiedPrimaryKeyJoinColumn(2, 0); + pkJoinColumns = tableResource.pkJoinColumns(); + + assertEquals("BAR", pkJoinColumns.next().getName()); + assertEquals("BAZ", pkJoinColumns.next().getName()); + assertEquals("FOO", pkJoinColumns.next().getName()); + } + + public void testPrimaryKeyJoinColumnGetDefaultName() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + IPrimaryKeyJoinColumn defaultPkJoinColumn = secondaryTable.defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", defaultPkJoinColumn.getDefaultName()); + + + //remove @Id annotation + IPersistentAttribute idAttribute = javaPersistentType().attributeNamed("id"); + idAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + + assertNull(defaultPkJoinColumn.getDefaultName()); + } + public void testPrimaryKeyJoinColumnGetDefaultReferencedColumnName() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + IPrimaryKeyJoinColumn defaultPkJoinColumn = secondaryTable.defaultPrimaryKeyJoinColumns().next(); + assertEquals("id", defaultPkJoinColumn.getDefaultReferencedColumnName()); + + //remove @Id annotation + IPersistentAttribute idAttribute = javaPersistentType().attributeNamed("id"); + idAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + + assertNull(defaultPkJoinColumn.getDefaultReferencedColumnName()); + } + + public void testPrimaryKeyJoinColumnIsVirtual() throws Exception { + createTestEntityWithSecondaryTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ISecondaryTable secondaryTable = javaEntity().specifiedSecondaryTables().next(); + + secondaryTable.addSpecifiedPrimaryKeyJoinColumn(0); + IPrimaryKeyJoinColumn specifiedPkJoinColumn = secondaryTable.specifiedPrimaryKeyJoinColumns().next(); + assertFalse(specifiedPkJoinColumn.isVirtual()); + + IPrimaryKeyJoinColumn defaultPkJoinColumn = secondaryTable.defaultPrimaryKeyJoinColumns().next(); + assertTrue(defaultPkJoinColumn.isVirtual()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSequenceGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSequenceGeneratorTests.java new file mode 100644 index 0000000000..0d0fb43b08 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaSequenceGeneratorTests.java @@ -0,0 +1,270 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IGenerator; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.ISequenceGenerator; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.SequenceGenerator; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaSequenceGeneratorTests extends ContextModelTestCase +{ + private static final String SEQUENCE_GENERATOR_NAME = "MY_SEQUENCE_GENERATOR"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createSequenceGeneratorAnnotation() throws Exception{ + this.createAnnotationAndMembers("SequenceGenerator", + "String name();" + + "String sequenceName() default \"\"; " + + "int initialValue() default 0; " + + "int allocationSize() default 50;"); + } + + private IType createTestEntityWithSequenceGenerator() throws Exception { + createEntityAnnotation(); + createSequenceGeneratorAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SEQUENCE_GENERATOR, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + sb.append("@SequenceGenerator(name=\"" + SEQUENCE_GENERATOR_NAME + "\")"); + } + }); + } + + public JavaSequenceGeneratorTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(SEQUENCE_GENERATOR_NAME, idMapping.getSequenceGenerator().getName()); + + //change resource model sequenceGenerator name, verify the context model is updated + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + sequenceGenerator.setName("foo"); + + assertEquals("foo", idMapping.getSequenceGenerator().getName()); + } + + public void testSetName() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(SEQUENCE_GENERATOR_NAME, idMapping.getSequenceGenerator().getName()); + + idMapping.getSequenceGenerator().setName("foo"); + + assertEquals("foo", idMapping.getSequenceGenerator().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + assertEquals("foo", sequenceGenerator.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(SEQUENCE_GENERATOR_NAME, idMapping.getSequenceGenerator().getName()); + + idMapping.getSequenceGenerator().setName(null); + + assertNull(idMapping.getSequenceGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + assertNull(sequenceGenerator); + } + + public void testGetInitialValue() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(ISequenceGenerator.DEFAULT_INITIAL_VALUE, idMapping.getSequenceGenerator().getInitialValue()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + sequenceGenerator.setInitialValue(Integer.valueOf(82)); + + assertEquals(Integer.valueOf(82), idMapping.getSequenceGenerator().getInitialValue()); + assertEquals(Integer.valueOf(82), idMapping.getSequenceGenerator().getSpecifiedInitialValue()); + } + + public void testGetDefaultInitialValue() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(ISequenceGenerator.DEFAULT_INITIAL_VALUE, idMapping.getSequenceGenerator().getDefaultInitialValue()); + + idMapping.getSequenceGenerator().setSpecifiedInitialValue(Integer.valueOf(82)); + + assertEquals(ISequenceGenerator.DEFAULT_INITIAL_VALUE, idMapping.getSequenceGenerator().getDefaultInitialValue()); + assertEquals(Integer.valueOf(82), idMapping.getSequenceGenerator().getSpecifiedInitialValue()); + } + + public void testSetSpecifiedInitialValue() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getSequenceGenerator().setSpecifiedInitialValue(Integer.valueOf(20)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + assertEquals(Integer.valueOf(20), sequenceGenerator.getInitialValue()); + + idMapping.getSequenceGenerator().setName(null); + idMapping.getSequenceGenerator().setSpecifiedInitialValue(null); + assertNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR)); + } + + public void testGetAllocationSize() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getSequenceGenerator().getAllocationSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + sequenceGenerator.setAllocationSize(Integer.valueOf(20)); + + assertEquals(Integer.valueOf(20), idMapping.getSequenceGenerator().getAllocationSize()); + assertEquals(Integer.valueOf(20), idMapping.getSequenceGenerator().getSpecifiedAllocationSize()); + } + + public void testGetDefaultAllocationSize() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getSequenceGenerator().getDefaultAllocationSize()); + + idMapping.getSequenceGenerator().setSpecifiedAllocationSize(Integer.valueOf(20)); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getSequenceGenerator().getDefaultAllocationSize()); + assertEquals(Integer.valueOf(20), idMapping.getSequenceGenerator().getSpecifiedAllocationSize()); + } + + public void testSetSpecifiedAllocationSize() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getSequenceGenerator().setSpecifiedAllocationSize(Integer.valueOf(25)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + assertEquals(Integer.valueOf(25), sequenceGenerator.getAllocationSize()); + + idMapping.getSequenceGenerator().setName(null); + idMapping.getSequenceGenerator().setSpecifiedAllocationSize(null); + assertNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR)); + } + + + + public void testGetSequenceName() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getSequenceGenerator().getSequenceName()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + sequenceGenerator.setSequenceName("mySequenceName"); + + assertEquals("mySequenceName", idMapping.getSequenceGenerator().getSequenceName()); + assertEquals("mySequenceName", idMapping.getSequenceGenerator().getSpecifiedSequenceName()); + } + + public void testGetDefaultSequenceName() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getSequenceGenerator().getDefaultSequenceName()); + + idMapping.getSequenceGenerator().setSpecifiedSequenceName("mySequenceName"); + + assertNull(idMapping.getSequenceGenerator().getDefaultSequenceName()); + assertEquals("mySequenceName", idMapping.getSequenceGenerator().getSpecifiedSequenceName()); + } + + public void testSetSpecifiedSequenceName() throws Exception { + createTestEntityWithSequenceGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getSequenceGenerator().setSpecifiedSequenceName("mySequenceName"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + + assertEquals("mySequenceName", sequenceGenerator.getSequenceName()); + + idMapping.getSequenceGenerator().setName(null); + idMapping.getSequenceGenerator().setSpecifiedSequenceName(null); + assertNull(attributeResource.annotation(JPA.SEQUENCE_GENERATOR)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableGeneratorTests.java new file mode 100644 index 0000000000..7ccaab6009 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableGeneratorTests.java @@ -0,0 +1,469 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.context.base.IGenerator; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.ITableGenerator; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.TableGenerator; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaTableGeneratorTests extends ContextModelTestCase +{ + private static final String TABLE_GENERATOR_NAME = "MY_TABLE_GENERATOR"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createTableGeneratorAnnotation() throws Exception{ + this.createAnnotationAndMembers("TableGenerator", + "String name(); " + + "String table() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";" + + "String pkColumnName() default \"\"; " + + "String valueColumnName() default \"\"; " + + "String pkColumnValue() default \"\"; " + + "int initialValue() default 0; " + + "int allocationSize() default 50; " + + "UniqueConstraint[] uniqueConstraints() default {};"); + } + + private IType createTestEntityWithTableGenerator() throws Exception { + createEntityAnnotation(); + createTableGeneratorAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE_GENERATOR, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id").append(CR); + sb.append("@TableGenerator(name=\"" + TABLE_GENERATOR_NAME + "\")"); + } + }); + } + + public JavaTableGeneratorTests(String name) { + super(name); + } + + public void testGetName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(TABLE_GENERATOR_NAME, idMapping.getTableGenerator().getName()); + + //change resource model tableGenerator name, verify the context model is updated + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setName("foo"); + + assertEquals("foo", idMapping.getTableGenerator().getName()); + } + + public void testSetName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(TABLE_GENERATOR_NAME, idMapping.getTableGenerator().getName()); + + idMapping.getTableGenerator().setName("foo"); + + assertEquals("foo", idMapping.getTableGenerator().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("foo", tableGenerator.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + assertEquals(TABLE_GENERATOR_NAME, idMapping.getTableGenerator().getName()); + + idMapping.getTableGenerator().setName(null); + + assertNull(idMapping.getTableGenerator()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertNull(tableGenerator); + } + + public void testGetCatalog() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getCatalog()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setCatalog("myCatalog"); + + assertEquals("myCatalog", idMapping.getTableGenerator().getCatalog()); + assertEquals("myCatalog", idMapping.getTableGenerator().getSpecifiedCatalog()); + } + + public void testGetDefaultCatalog() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getDefaultCatalog()); + + idMapping.getTableGenerator().setSpecifiedCatalog("myCatalog"); + + assertNull(idMapping.getTableGenerator().getDefaultCatalog()); + assertEquals("myCatalog", idMapping.getTableGenerator().getSpecifiedCatalog()); + } + + public void testSetSpecifiedCatalog() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedCatalog("myCatalog"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("myCatalog", tableGenerator.getCatalog()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedCatalog(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetSchema() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getSchema()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setSchema("mySchema"); + + assertEquals("mySchema", idMapping.getTableGenerator().getSchema()); + assertEquals("mySchema", idMapping.getTableGenerator().getSpecifiedSchema()); + } + + public void testGetDefaultSchema() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getDefaultSchema()); + + idMapping.getTableGenerator().setSpecifiedSchema("mySchema"); + + assertNull(idMapping.getTableGenerator().getDefaultSchema()); + assertEquals("mySchema", idMapping.getTableGenerator().getSpecifiedSchema()); + } + + public void testSetSpecifiedSchema() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedSchema("mySchema"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("mySchema", tableGenerator.getSchema()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedSchema(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetPkColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getPkColumnName()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setPkColumnName("myPkColumnName"); + + assertEquals("myPkColumnName", idMapping.getTableGenerator().getPkColumnName()); + assertEquals("myPkColumnName", idMapping.getTableGenerator().getSpecifiedPkColumnName()); + } + + public void testGetDefaultPkColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getDefaultPkColumnName()); + + idMapping.getTableGenerator().setSpecifiedPkColumnName("myPkColumnName"); + + assertNull(idMapping.getTableGenerator().getDefaultPkColumnName()); + assertEquals("myPkColumnName", idMapping.getTableGenerator().getSpecifiedPkColumnName()); + } + + public void testSetSpecifiedPkColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedPkColumnName("myPkColumnName"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("myPkColumnName", tableGenerator.getPkColumnName()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedPkColumnName(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetValueColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getValueColumnName()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setValueColumnName("myValueColumnName"); + + assertEquals("myValueColumnName", idMapping.getTableGenerator().getValueColumnName()); + assertEquals("myValueColumnName", idMapping.getTableGenerator().getSpecifiedValueColumnName()); + } + + public void testGetDefaultValueColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getDefaultValueColumnName()); + + idMapping.getTableGenerator().setSpecifiedValueColumnName("myValueColumnName"); + + assertNull(idMapping.getTableGenerator().getDefaultValueColumnName()); + assertEquals("myValueColumnName", idMapping.getTableGenerator().getSpecifiedValueColumnName()); + } + + public void testSetSpecifiedValueColumnName() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedValueColumnName("myValueColumnName"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("myValueColumnName", tableGenerator.getValueColumnName()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedValueColumnName(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetPkColumnValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getPkColumnValue()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setPkColumnValue("myPkColumnValue"); + + assertEquals("myPkColumnValue", idMapping.getTableGenerator().getPkColumnValue()); + assertEquals("myPkColumnValue", idMapping.getTableGenerator().getSpecifiedPkColumnValue()); + } + + public void testGetDefaultPkColumnValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertNull(idMapping.getTableGenerator().getDefaultPkColumnValue()); + + idMapping.getTableGenerator().setSpecifiedPkColumnValue("myPkColumnValue"); + + assertNull(idMapping.getTableGenerator().getDefaultPkColumnValue()); + assertEquals("myPkColumnValue", idMapping.getTableGenerator().getSpecifiedPkColumnValue()); + } + + public void testSetSpecifiedPkColumnValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedPkColumnValue("myPkColumnValue"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals("myPkColumnValue", tableGenerator.getPkColumnValue()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedPkColumnValue(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetInitialValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(ITableGenerator.DEFAULT_INITIAL_VALUE, idMapping.getTableGenerator().getInitialValue()); + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setInitialValue(Integer.valueOf(82)); + + assertEquals(Integer.valueOf(82), idMapping.getTableGenerator().getInitialValue()); + assertEquals(Integer.valueOf(82), idMapping.getTableGenerator().getSpecifiedInitialValue()); + } + + public void testGetDefaultInitialValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(ITableGenerator.DEFAULT_INITIAL_VALUE, idMapping.getTableGenerator().getDefaultInitialValue()); + + idMapping.getTableGenerator().setSpecifiedInitialValue(Integer.valueOf(82)); + + assertEquals(ITableGenerator.DEFAULT_INITIAL_VALUE, idMapping.getTableGenerator().getDefaultInitialValue()); + assertEquals(Integer.valueOf(82), idMapping.getTableGenerator().getSpecifiedInitialValue()); + } + + public void testSetSpecifiedInitialValue() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedInitialValue(Integer.valueOf(20)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals(Integer.valueOf(20), tableGenerator.getInitialValue()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedInitialValue(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } + + public void testGetAllocationSize() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getTableGenerator().getAllocationSize()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.setAllocationSize(Integer.valueOf(20)); + + assertEquals(Integer.valueOf(20), idMapping.getTableGenerator().getAllocationSize()); + assertEquals(Integer.valueOf(20), idMapping.getTableGenerator().getSpecifiedAllocationSize()); + } + + public void testGetDefaultAllocationSize() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getTableGenerator().getDefaultAllocationSize()); + + idMapping.getTableGenerator().setSpecifiedAllocationSize(Integer.valueOf(20)); + + assertEquals(IGenerator.DEFAULT_ALLOCATION_SIZE, idMapping.getTableGenerator().getDefaultAllocationSize()); + assertEquals(Integer.valueOf(20), idMapping.getTableGenerator().getSpecifiedAllocationSize()); + } + + public void testSetSpecifiedAllocationSize() throws Exception { + createTestEntityWithTableGenerator(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IIdMapping idMapping = (IIdMapping) javaPersistentType().attributeNamed("id").getMapping(); + idMapping.getTableGenerator().setSpecifiedAllocationSize(Integer.valueOf(25)); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + assertEquals(Integer.valueOf(25), tableGenerator.getAllocationSize()); + + idMapping.getTableGenerator().setName(null); + idMapping.getTableGenerator().setSpecifiedAllocationSize(null); + assertNull(attributeResource.annotation(JPA.TABLE_GENERATOR)); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableTests.java new file mode 100644 index 0000000000..3ae2dcbacc --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTableTests.java @@ -0,0 +1,338 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.ITable; +import org.eclipse.jpt.core.internal.context.java.IJavaEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.Table; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaTableTests extends ContextModelTestCase +{ + private static final String TABLE_NAME = "MY_TABLE"; + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + private void createTableAnnotation() throws Exception{ + this.createAnnotationAndMembers("Table", + "String name() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";"); + } + + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithTable() throws Exception { + createEntityAnnotation(); + createTableAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + sb.append("@Table(name=\"" + TABLE_NAME + "\")"); + } + }); + } + + private IType createTestSubType() throws Exception { + return this.createTestType(PACKAGE_NAME, "AnnotationTestTypeChild.java", "AnnotationTestTypeChild", new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendExtendsImplementsTo(StringBuilder sb) { + sb.append("extends " + TYPE_NAME + " "); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + }); + } + + + + public JavaTableTests(String name) { + super(name); + } + + public void testGetSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getTable().getSpecifiedName()); + } + + public void testGetSpecifiedName() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TABLE_NAME, javaEntity().getTable().getSpecifiedName()); + } + + public void testGetDefaultNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getTable().getDefaultName()); + } + + public void testGetDefaultName() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getTable().getDefaultName()); + + //test that setting the java entity name will change the table default name + javaEntity().setSpecifiedName("foo"); + assertEquals("foo", javaEntity().getTable().getDefaultName()); + } + + public void testGetDefaultNameSingleTableInheritance() throws Exception { + createTestEntity(); + createTestSubType(); + + addXmlClassRef(PACKAGE_NAME + ".AnnotationTestTypeChild"); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNotSame(javaEntity(), javaEntity().rootEntity()); + assertEquals(TYPE_NAME, javaEntity().getTable().getDefaultName()); + assertEquals(TYPE_NAME, javaEntity().rootEntity().getTable().getDefaultName()); + + //test that setting the root java entity name will change the table default name of the child + javaEntity().rootEntity().setSpecifiedName("foo"); + assertEquals("foo", javaEntity().getTable().getDefaultName()); + } + + public void testUpdateDefaultSchemaFromPersistenceUnitDefaults() throws Exception { + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + IJavaEntity javaEntity = xmlEntity.javaEntity(); + + assertNull(javaEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("FOO"); + assertEquals("FOO", javaEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().setSpecifiedSchema("BAR"); + assertEquals("BAR", javaEntity.getTable().getDefaultSchema()); + + xmlEntity.getTable().setSpecifiedSchema("XML_SCHEMA"); + assertEquals("BAR", javaEntity.getTable().getDefaultSchema()); + + entityMappings().removeXmlPersistentType(0); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + //default schema taken from persistence-unit-defaults not entity-mappings since the entity is not in an orm.xml file + assertEquals("FOO", javaEntity().getTable().getDefaultSchema()); + } + + public void testGetNameSpecifiedNameNull() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TYPE_NAME, javaEntity().getTable().getName()); + } + + public void testGetName() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertEquals(TABLE_NAME, javaEntity().getTable().getName()); + } + + public void testSetSpecifiedName() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().getTable().setSpecifiedName("foo"); + + assertEquals("foo", javaEntity().getTable().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + assertEquals("foo", table.getName()); + } + + public void testSetSpecifiedNameNull() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + javaEntity().getTable().setSpecifiedName(null); + + assertNull(javaEntity().getTable().getSpecifiedName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + assertNull(table); + } + + public void testUpdateFromSpecifiedNameChangeInResourceModel() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table table = (Table) typeResource.annotation(JPA.TABLE); + table.setName("foo"); + + assertEquals("foo", javaEntity().getTable().getSpecifiedName()); + + typeResource.removeAnnotation(JPA.TABLE); + assertNull(javaEntity().getTable().getSpecifiedName()); + } + + public void testGetCatalog() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.setCatalog("myCatalog"); + + assertEquals("myCatalog", javaEntity().getTable().getSpecifiedCatalog()); + assertEquals("myCatalog", javaEntity().getTable().getCatalog()); + } + + public void testGetDefaultCatalog() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getTable().getDefaultCatalog()); + + javaEntity().getTable().setSpecifiedCatalog("myCatalog"); + + assertNull(javaEntity().getTable().getDefaultCatalog()); + } + + public void testUpdateDefaultCatalogFromPersistenceUnitDefaults() throws Exception { + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + IJavaEntity javaEntity = xmlEntity.javaEntity(); + + assertNull(javaEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("FOO"); + assertEquals("FOO", javaEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().setSpecifiedCatalog("BAR"); + assertEquals("BAR", javaEntity.getTable().getDefaultCatalog()); + + xmlEntity.getTable().setSpecifiedCatalog("XML_CATALOG"); + assertEquals("BAR", javaEntity.getTable().getDefaultCatalog()); + + entityMappings().removeXmlPersistentType(0); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + //default catalog taken from persistence-unite-defaults not entity-mappings since the entity is not in an orm.xml file + assertEquals("FOO", javaEntity().getTable().getDefaultCatalog()); + } + + public void testSetSpecifiedCatalog() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ITable table = javaEntity().getTable(); + table.setSpecifiedCatalog("myCatalog"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table tableResource = (Table) typeResource.annotation(JPA.TABLE); + + assertEquals("myCatalog", tableResource.getCatalog()); + + table.setSpecifiedCatalog(null); + assertNull(typeResource.annotation(JPA.TABLE)); + } + + public void testGetSchema() throws Exception { + createTestEntityWithTable(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.setSchema("mySchema"); + + assertEquals("mySchema", javaEntity().getTable().getSpecifiedSchema()); + assertEquals("mySchema", javaEntity().getTable().getSchema()); + } + + public void testGetDefaultSchema() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + assertNull(javaEntity().getTable().getDefaultSchema()); + + javaEntity().getTable().setSpecifiedSchema("mySchema"); + + assertNull(javaEntity().getTable().getDefaultSchema()); + } + + public void testSetSpecifiedSchema() throws Exception { + createTestEntity(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + ITable table = javaEntity().getTable(); + table.setSpecifiedSchema("mySchema"); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + Table tableResource = (Table) typeResource.annotation(JPA.TABLE); + + assertEquals("mySchema", tableResource.getSchema()); + + table.setSpecifiedSchema(null); + assertNull(typeResource.annotation(JPA.TABLE)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTransientMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTransientMappingTests.java new file mode 100644 index 0000000000..9883e34329 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaTransientMappingTests.java @@ -0,0 +1,259 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaTransientMappingTests extends ContextModelTestCase +{ + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createTransientAnnotation() throws Exception{ + this.createAnnotationAndMembers("Transient", ""); + } + + private IType createTestEntityWithTransientMapping() throws Exception { + createEntityAnnotation(); + createTransientAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TRANSIENT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Transient").append(CR); + } + }); + } + + public JavaTransientMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertFalse(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IBasicMapping); + assertTrue(persistentAttribute.getMapping().isDefault()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + } + + public void testMorphToVersionMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IVersionMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getMapping(); + assertFalse(transientMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + } + + public void testTransientMapping() throws Exception { + createTestEntityWithTransientMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + ITransientMapping transientMapping = (ITransientMapping) persistentAttribute.getSpecifiedMapping(); + + assertNotNull(transientMapping); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaVersionMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaVersionMappingTests.java new file mode 100644 index 0000000000..535630e6ec --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JavaVersionMappingTests.java @@ -0,0 +1,413 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.context.base.IBasicMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.base.IEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.base.IIdMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IManyToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToManyMapping; +import org.eclipse.jpt.core.internal.context.base.IOneToOneMapping; +import org.eclipse.jpt.core.internal.context.base.IPersistentAttribute; +import org.eclipse.jpt.core.internal.context.base.ITransientMapping; +import org.eclipse.jpt.core.internal.context.base.IVersionMapping; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.core.internal.resource.java.Temporal; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaVersionMappingTests extends ContextModelTestCase +{ + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createVersionAnnotation() throws Exception{ + this.createAnnotationAndMembers("Version", ""); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private IType createTestEntityWithVersionMapping() throws Exception { + createEntityAnnotation(); + createVersionAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.VERSION); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Version").append(CR); + } + }); + } + + private IType createTestEntityWithTemporal() throws Exception { + createEntityAnnotation(); + createTemporalAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.VERSION, JPA.TEMPORAL, JPA.TEMPORAL_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity").append(CR); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Version").append(CR); + sb.append("@Temporal(TemporalType.TIMESTAMP)").append(CR); + } + }); + } + + public JavaVersionMappingTests(String name) { + super(name); + } + + public void testMorphToBasicMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Basic.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToDefault() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IBasicMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IBasicMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToIdMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + assertEquals("FOO", ((IIdMapping) persistentAttribute.getMapping()).getColumn().getSpecifiedName()); + assertEquals(TemporalType.TIME, ((IIdMapping) persistentAttribute.getMapping()).getTemporal()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Id.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNotNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Embedded.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToTransientMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof ITransientMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(Transient.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToEmbeddedIdMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IEmbeddedIdMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(EmbeddedId.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToOneToOneMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToOneToManyMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IOneToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(OneToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToManyToOneMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToOneMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToOne.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testMorphToManyToManyMapping() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getMapping(); + assertFalse(versionMapping.isDefault()); + versionMapping.getColumn().setSpecifiedName("FOO"); + versionMapping.setTemporal(TemporalType.TIME); + assertFalse(versionMapping.isDefault()); + + persistentAttribute.setSpecifiedMappingKey(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY); + assertTrue(persistentAttribute.getMapping() instanceof IManyToManyMapping); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + assertNull(attributeResource.mappingAnnotation(Version.ANNOTATION_NAME)); + assertNotNull(attributeResource.mappingAnnotation(ManyToMany.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Column.ANNOTATION_NAME)); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testGetTemporal() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(versionMapping.getTemporal()); + } + + public void testGetTemporal2() throws Exception { + createTestEntityWithTemporal(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getSpecifiedMapping(); + + assertEquals(TemporalType.TIMESTAMP, versionMapping.getTemporal()); + } + + public void testSetTemporal() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getSpecifiedMapping(); + assertNull(versionMapping.getTemporal()); + + versionMapping.setTemporal(TemporalType.TIME); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.annotation(Temporal.ANNOTATION_NAME); + + assertEquals(org.eclipse.jpt.core.internal.resource.java.TemporalType.TIME, temporal.getValue()); + + versionMapping.setTemporal(null); + assertNull(attributeResource.annotation(Temporal.ANNOTATION_NAME)); + } + + public void testGetTemporalUpdatesFromResourceModelChange() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(versionMapping.getTemporal()); + + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Temporal temporal = (Temporal) attributeResource.addAnnotation(Temporal.ANNOTATION_NAME); + temporal.setValue(org.eclipse.jpt.core.internal.resource.java.TemporalType.DATE); + + assertEquals(TemporalType.DATE, versionMapping.getTemporal()); + + attributeResource.removeAnnotation(Temporal.ANNOTATION_NAME); + + assertNull(versionMapping.getTemporal()); + assertFalse(versionMapping.isDefault()); + assertSame(versionMapping, persistentAttribute.getSpecifiedMapping()); + } + + public void testGetColumn() throws Exception { + createTestEntityWithVersionMapping(); + addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME); + + IPersistentAttribute persistentAttribute = javaPersistentType().attributes().next(); + IVersionMapping versionMapping = (IVersionMapping) persistentAttribute.getSpecifiedMapping(); + + assertNull(versionMapping.getColumn().getSpecifiedName()); + assertEquals("id", versionMapping.getColumn().getName()); + + JavaPersistentTypeResource typeResource = jpaProject().javaPersistentTypeResource(FULLY_QUALIFIED_TYPE_NAME); + JavaPersistentAttributeResource attributeResource = typeResource.attributes().next(); + Column column = (Column) attributeResource.addAnnotation(JPA.COLUMN); + column.setName("foo"); + + assertEquals("foo", versionMapping.getColumn().getSpecifiedName()); + assertEquals("foo", versionMapping.getColumn().getName()); + assertEquals("id", versionMapping.getColumn().getDefaultName()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JptCoreContextJavaModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JptCoreContextJavaModelTests.java new file mode 100644 index 0000000000..ed58b5da69 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/java/JptCoreContextJavaModelTests.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.java; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class JptCoreContextJavaModelTests extends TestCase +{ + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptCoreContextJavaModelTests.class.getName()); + suite.addTestSuite(JavaAssociationOverrideTests.class); + suite.addTestSuite(JavaAttributeOverrideTests.class); + suite.addTestSuite(JavaBasicMappingTests.class); + suite.addTestSuite(JavaColumnTests.class); + suite.addTestSuite(JavaDiscriminatorColumnTests.class); + suite.addTestSuite(JavaEmbeddableTests.class); + suite.addTestSuite(JavaEmbeddedIdMappingTests.class); + suite.addTestSuite(JavaEmbeddedMappingTests.class); + suite.addTestSuite(JavaEntityTests.class); + suite.addTestSuite(JavaGeneratedValueTests.class); + suite.addTestSuite(JavaIdMappingTests.class); + suite.addTestSuite(JavaJoinTableTests.class); + suite.addTestSuite(JavaMappedSuperclassTests.class); + suite.addTestSuite(JavaManyToManyMappingTests.class); + suite.addTestSuite(JavaManyToOneMappingTests.class); + suite.addTestSuite(JavaNamedQueryTests.class); + suite.addTestSuite(JavaNamedNativeQueryTests.class); + suite.addTestSuite(JavaQueryHintTests.class); + suite.addTestSuite(JavaOneToManyMappingTests.class); + suite.addTestSuite(JavaOneToOneMappingTests.class); + suite.addTestSuite(JavaPersistentTypeTests.class); + suite.addTestSuite(JavaPersistentAttributeTests.class); + suite.addTestSuite(JavaPrimaryKeyJoinColumnTests.class); + suite.addTestSuite(JavaSecondaryTableTests.class); + suite.addTestSuite(JavaSequenceGeneratorTests.class); + suite.addTestSuite(JavaTableGeneratorTests.class); + suite.addTestSuite(JavaTableTests.class); + suite.addTestSuite(JavaTransientMappingTests.class); + suite.addTestSuite(JavaVersionMappingTests.class); + return suite; + } + + private JptCoreContextJavaModelTests() { + super(); + throw new UnsupportedOperationException(); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/EntityMappingsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/EntityMappingsTests.java new file mode 100644 index 0000000000..857d7d02a5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/EntityMappingsTests.java @@ -0,0 +1,1066 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.ISequenceGenerator; +import org.eclipse.jpt.core.internal.context.base.ITableGenerator; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedNativeQuery; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedQuery; +import org.eclipse.jpt.core.internal.resource.orm.Embeddable; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.MappedSuperclass; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.orm.SequenceGenerator; +import org.eclipse.jpt.core.internal.resource.orm.TableGenerator; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class EntityMappingsTests extends ContextModelTestCase +{ + public EntityMappingsTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testGetVersion() throws Exception { + assertEquals("1.0", entityMappings().getVersion()); + } + + public void testUpdateDescription() throws Exception { + assertNull(entityMappings().getDescription()); + assertNull(ormResource().getEntityMappings().getDescription()); + + //set description in the resource model, verify context model updated + ormResource().getEntityMappings().setDescription("newDescription"); + assertEquals("newDescription", entityMappings().getDescription()); + assertEquals("newDescription", ormResource().getEntityMappings().getDescription()); + + //set description to null in the resource model + ormResource().getEntityMappings().setDescription(null); + assertNull(entityMappings().getDescription()); + assertNull(ormResource().getEntityMappings().getDescription()); + } + + public void testModifyDescription() throws Exception { + assertNull(entityMappings().getDescription()); + assertNull(ormResource().getEntityMappings().getDescription()); + + //set description in the context model, verify resource model modified + entityMappings().setDescription("newDescription"); + assertEquals("newDescription", entityMappings().getDescription()); + assertEquals("newDescription", ormResource().getEntityMappings().getDescription()); + + //set description to null in the context model + entityMappings().setDescription(null); + assertNull(entityMappings().getDescription()); + assertNull(ormResource().getEntityMappings().getDescription()); + } + + public void testUpdatePackage() throws Exception { + assertNull(entityMappings().getPackage()); + assertNull(ormResource().getEntityMappings().getPackage()); + + //set package in the resource model, verify context model updated + ormResource().getEntityMappings().setPackage("foo.model"); + assertEquals("foo.model", entityMappings().getPackage()); + assertEquals("foo.model", ormResource().getEntityMappings().getPackage()); + + //set package to null in the resource model + ormResource().getEntityMappings().setPackage(null); + assertNull(entityMappings().getPackage()); + assertNull(ormResource().getEntityMappings().getPackage()); + } + + public void testModifyPackage() throws Exception { + assertNull(entityMappings().getPackage()); + assertNull(ormResource().getEntityMappings().getPackage()); + + //set package in the context model, verify resource model modified + entityMappings().setPackage("foo.model"); + assertEquals("foo.model", entityMappings().getPackage()); + assertEquals("foo.model", ormResource().getEntityMappings().getPackage()); + + //set package to null in the context model + entityMappings().setPackage(null); + assertNull(entityMappings().getPackage()); + assertNull(ormResource().getEntityMappings().getPackage()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + //set schema in the resource model, verify context model updated + ormResource().getEntityMappings().setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", entityMappings().getSpecifiedSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getSchema()); + + //set schema to null in the resource model + ormResource().getEntityMappings().setSchema(null); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + } + + public void testModifySpecifiedSchema() throws Exception { + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + //set schema in the context model, verify resource model modified + entityMappings().setSpecifiedSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", entityMappings().getSpecifiedSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getSchema()); + + //set schema to null in the context model + entityMappings().setSpecifiedSchema(null); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + //set catalog in the resource model, verify context model updated + ormResource().getEntityMappings().setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", entityMappings().getSpecifiedCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getCatalog()); + + //set catalog to null in the resource model + ormResource().getEntityMappings().setCatalog(null); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + } + + public void testUpdateDefaultSchema() throws Exception { + assertNull(entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitDefaults persistenceUnitDefaults = OrmFactory.eINSTANCE.createPersistenceUnitDefaults(); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(persistenceUnitDefaults); + persistenceUnitDefaults.setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + + persistenceUnitDefaults.setSchema(null); + ormResource().save(null); + assertNull(entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + } + + public void testUpdateSchema() throws Exception { + assertNull(entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + ormResource().getEntityMappings().setSchema("MY_SCHEMA"); + assertNull(entityMappings().getDefaultSchema()); + assertEquals("MY_SCHEMA", entityMappings().getSchema()); + assertEquals("MY_SCHEMA", entityMappings().getSpecifiedSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getSchema()); + + ormResource().getEntityMappings().setSchema(null); + assertNull(entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("DEFAULT_SCHEMA"); + assertEquals("DEFAULT_SCHEMA", entityMappings().getDefaultSchema()); + assertEquals("DEFAULT_SCHEMA", entityMappings().getSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); + assertNull(entityMappings().getDefaultSchema()); + assertNull(entityMappings().getSchema()); + assertNull(entityMappings().getSpecifiedSchema()); + assertNull(ormResource().getEntityMappings().getSchema()); + } + + public void testModifySpecifiedCatalog() throws Exception { + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + //set catalog in the context model, verify resource model modified + entityMappings().setSpecifiedCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", entityMappings().getSpecifiedCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getCatalog()); + + //set catalog to null in the context model + entityMappings().setSpecifiedCatalog(null); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + } + + public void testUpdateDefaultCatalog() throws Exception { + assertNull(entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitDefaults persistenceUnitDefaults = OrmFactory.eINSTANCE.createPersistenceUnitDefaults(); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(persistenceUnitDefaults); + persistenceUnitDefaults.setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + + persistenceUnitDefaults.setCatalog(null); + assertNull(entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + } + + public void testUpdateCatalog() throws Exception { + assertNull(entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + ormResource().getEntityMappings().setCatalog("MY_CATALOG"); + assertNull(entityMappings().getDefaultCatalog()); + assertEquals("MY_CATALOG", entityMappings().getCatalog()); + assertEquals("MY_CATALOG", entityMappings().getSpecifiedCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getCatalog()); + + ormResource().getEntityMappings().setCatalog(null); + assertNull(entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("DEFAULT_CATALOG"); + assertEquals("DEFAULT_CATALOG", entityMappings().getDefaultCatalog()); + assertEquals("DEFAULT_CATALOG", entityMappings().getCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); + assertNull(entityMappings().getDefaultCatalog()); + assertNull(entityMappings().getCatalog()); + assertNull(entityMappings().getSpecifiedCatalog()); + assertNull(ormResource().getEntityMappings().getCatalog()); + } + + public void testUpdateSpecifiedAccess() throws Exception { + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + //set access in the resource model, verify context model updated + ormResource().getEntityMappings().setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, entityMappings().getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, ormResource().getEntityMappings().getAccess()); + + //set access to null in the resource model + ormResource().getEntityMappings().setAccess(null); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + } + + public void testModifySpecifiedAccess() throws Exception { + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + //set access in the context model, verify resource model modified + entityMappings().setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, entityMappings().getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, ormResource().getEntityMappings().getAccess()); + + //set access to null in the context model + entityMappings().setSpecifiedAccess(null); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + } + + public void testUpdateDefaultAccess() throws Exception { + assertNull(entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitDefaults persistenceUnitDefaults = OrmFactory.eINSTANCE.createPersistenceUnitDefaults(); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(persistenceUnitDefaults); + persistenceUnitDefaults.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + persistenceUnitDefaults.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertEquals(AccessType.FIELD, entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + persistenceUnitDefaults.setAccess(null); + assertNull(entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + } + + public void testUpdateAccess() throws Exception { + assertNull(entityMappings().getAccess()); + assertNull(entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + ormResource().getEntityMappings().setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertNull(entityMappings().getDefaultAccess()); + assertEquals(AccessType.FIELD, entityMappings().getAccess()); + assertEquals(AccessType.FIELD, entityMappings().getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, ormResource().getEntityMappings().getAccess()); + + ormResource().getEntityMappings().setAccess(null); + assertNull(entityMappings().getAccess()); + assertNull(entityMappings().getDefaultAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, entityMappings().getDefaultAccess()); + assertEquals(AccessType.FIELD, entityMappings().getAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(null); + assertNull(entityMappings().getDefaultAccess()); + assertNull(entityMappings().getAccess()); + assertNull(entityMappings().getSpecifiedAccess()); + assertNull(ormResource().getEntityMappings().getAccess()); + } + + + public void testUpdateXmlPersistentTypes() throws Exception { + assertFalse(entityMappings().xmlPersistentTypes().hasNext()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + //add embeddable in the resource model, verify context model updated + Embeddable embeddable = OrmFactory.eINSTANCE.createEmbeddable(); + ormResource().getEntityMappings().getEmbeddables().add(embeddable); + embeddable.setClassName("model.Foo"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo", ormResource().getEntityMappings().getEmbeddables().get(0).getClassName()); + + //add entity in the resource model, verify context model updated + Entity entity = OrmFactory.eINSTANCE.createEntity(); + ormResource().getEntityMappings().getEntities().add(entity); + entity.setClassName("model.Foo2"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo2", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo2", ormResource().getEntityMappings().getEntities().get(0).getClassName()); + + //add mapped-superclass in the resource model, verify context model updated + MappedSuperclass mappedSuperclass = OrmFactory.eINSTANCE.createMappedSuperclass(); + ormResource().getEntityMappings().getMappedSuperclasses().add(mappedSuperclass); + mappedSuperclass.setClassName("model.Foo3"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo3", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertFalse(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo3", ormResource().getEntityMappings().getMappedSuperclasses().get(0).getClassName()); + } + + + public void testAddXmlPersistentType() throws Exception { + assertFalse(entityMappings().xmlPersistentTypes().hasNext()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + //add embeddable in the context model, verify resource model modified + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, entityMappings().xmlPersistentTypes().next().getMapping().getKey()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo", ormResource().getEntityMappings().getEmbeddables().get(0).getClassName()); + + //add entity in the context model, verify resource model modified + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo2", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, entityMappings().xmlPersistentTypes().next().getMapping().getKey()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo2", ormResource().getEntityMappings().getEntities().get(0).getClassName()); + + //add mapped-superclass in the context model, verify resource model modified + entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo3"); + assertTrue(entityMappings().xmlPersistentTypes().hasNext()); + assertEquals("model.Foo3", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); + assertEquals(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, entityMappings().xmlPersistentTypes().next().getMapping().getKey()); + assertFalse(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + assertEquals("model.Foo3", ormResource().getEntityMappings().getMappedSuperclasses().get(0).getClassName()); + } + + public void testRemoveXmlPersistentType() throws Exception { + assertFalse(entityMappings().xmlPersistentTypes().hasNext()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo3"); + + ormResource().save(null); + //remove xmlPersistentType from the context model, verify resource model modified + entityMappings().removeXmlPersistentType(1); + ormResource().save(null); + assertFalse(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + entityMappings().removeXmlPersistentType(1); + ormResource().save(null); + assertFalse(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + entityMappings().removeXmlPersistentType(0); + ormResource().save(null); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + } + + public void testUpdateTableGenerators() throws Exception { + assertEquals(0, entityMappings().tableGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + ormResource().save(null); + TableGenerator tableGeneratorResource = OrmFactory.eINSTANCE.createTableGeneratorImpl(); + ormResource().getEntityMappings().getTableGenerators().add(tableGeneratorResource); + ormResource().save(null); + tableGeneratorResource.setName("FOO"); + ormResource().save(null); + + ITableGenerator tableGenerator = entityMappings().tableGenerators().next(); + assertEquals("FOO", tableGenerator.getName()); + + + TableGenerator tableGeneratorResource2 = OrmFactory.eINSTANCE.createTableGeneratorImpl(); + ormResource().getEntityMappings().getTableGenerators().add(0, tableGeneratorResource2); + tableGeneratorResource2.setName("BAR"); + ormResource().save(null); + + ListIterator<ITableGenerator> tableGenerators = entityMappings().tableGenerators(); + assertEquals("BAR", tableGenerators.next().getName()); + assertEquals("FOO", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + + + TableGenerator tableGeneratorResource3 = OrmFactory.eINSTANCE.createTableGeneratorImpl(); + ormResource().getEntityMappings().getTableGenerators().add(1, tableGeneratorResource3); + tableGeneratorResource3.setName("BAZ"); + ormResource().save(null); + + tableGenerators = entityMappings().tableGenerators(); + assertEquals("BAR", tableGenerators.next().getName()); + assertEquals("BAZ", tableGenerators.next().getName()); + assertEquals("FOO", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + + ormResource().getEntityMappings().getTableGenerators().move(2, 0); + ormResource().save(null); + tableGenerators = entityMappings().tableGenerators(); + assertEquals("BAZ", tableGenerators.next().getName()); + assertEquals("FOO", tableGenerators.next().getName()); + assertEquals("BAR", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + + + ormResource().getEntityMappings().getTableGenerators().remove(0); + ormResource().save(null); + tableGenerators = entityMappings().tableGenerators(); + assertEquals("FOO", tableGenerators.next().getName()); + assertEquals("BAR", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + + ormResource().getEntityMappings().getTableGenerators().remove(1); + ormResource().save(null); + tableGenerators = entityMappings().tableGenerators(); + assertEquals("FOO", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + + ormResource().getEntityMappings().getTableGenerators().clear(); + ormResource().save(null); + tableGenerators = entityMappings().tableGenerators(); + assertFalse(tableGenerators.hasNext()); + } + + public void testAddTableGenerator() throws Exception { + assertEquals(0, entityMappings().tableGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + ormResource().save(null); + entityMappings().addTableGenerator(0).setName("FOO"); + + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + + entityMappings().addTableGenerator(0).setName("BAR"); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + assertEquals(2, ormResource().getEntityMappings().getTableGenerators().size()); + + ListIterator<ITableGenerator> tableGenerators = entityMappings().tableGenerators(); + assertEquals("BAR", tableGenerators.next().getName()); + assertEquals("FOO", tableGenerators.next().getName()); + assertFalse(tableGenerators.hasNext()); + } + + public void testRemoveTableGenerator() throws Exception { + assertEquals(0, entityMappings().tableGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + tableGenerator.setName("FOO"); + ITableGenerator tableGenerator2 = entityMappings().addTableGenerator(1); + tableGenerator2.setName("BAR"); + ITableGenerator tableGenerator3 = entityMappings().addTableGenerator(2); + tableGenerator3.setName("BAZ"); + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getTableGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getTableGenerators().size()); + + entityMappings().removeTableGenerator(0); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + + ListIterator<ITableGenerator> tableGenerators = entityMappings().tableGenerators(); + ITableGenerator xmlTableGenerator = tableGenerators.next(); + assertEquals("BAR", xmlTableGenerator.getName()); + assertEquals(tableGenerator2, xmlTableGenerator); + xmlTableGenerator = tableGenerators.next(); + assertEquals("BAZ", xmlTableGenerator.getName()); + assertEquals(tableGenerator3, xmlTableGenerator); + assertFalse(tableGenerators.hasNext()); + + + entityMappings().removeTableGenerator(1); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + tableGenerators = entityMappings().tableGenerators(); + xmlTableGenerator = tableGenerators.next(); + assertEquals("BAR", xmlTableGenerator.getName()); + assertEquals(tableGenerator2, xmlTableGenerator); + assertFalse(tableGenerators.hasNext()); + + + entityMappings().removeTableGenerator(0); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + tableGenerators = entityMappings().tableGenerators(); + assertFalse(tableGenerators.hasNext()); + } + + public void testMoveTableGenerator() throws Exception { + assertEquals(0, entityMappings().tableGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + tableGenerator.setName("FOO"); + ITableGenerator tableGenerator2 = entityMappings().addTableGenerator(1); + tableGenerator2.setName("BAR"); + ITableGenerator tableGenerator3 = entityMappings().addTableGenerator(2); + tableGenerator3.setName("BAZ"); + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getTableGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getTableGenerators().size()); + + entityMappings().moveTableGenerator(2, 0); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getTableGenerators().size()); + + entityMappings().moveTableGenerator(0, 2); + assertEquals("FOO", ormResource().getEntityMappings().getTableGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getTableGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getTableGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getTableGenerators().size()); + } + + public void testTableGeneratorsSize() throws Exception { + assertEquals(0, entityMappings().tableGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getTableGenerators().size()); + + + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + tableGenerator.setName("FOO"); + ITableGenerator tableGenerator2 = entityMappings().addTableGenerator(1); + tableGenerator2.setName("BAR"); + ITableGenerator tableGenerator3 = entityMappings().addTableGenerator(2); + tableGenerator3.setName("BAZ"); + + assertEquals(3, entityMappings().tableGeneratorsSize()); + + ormResource().getEntityMappings().getTableGenerators().remove(0); + assertEquals(2, entityMappings().tableGeneratorsSize()); + } + + public void testUpdateSequenceGenerators() throws Exception { + assertEquals(0, entityMappings().sequenceGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + ormResource().save(null); + + SequenceGenerator sequenceGeneratorResource = OrmFactory.eINSTANCE.createSequenceGeneratorImpl(); + ormResource().getEntityMappings().getSequenceGenerators().add(sequenceGeneratorResource); + ormResource().save(null); + sequenceGeneratorResource.setName("FOO"); + ormResource().save(null); + + ISequenceGenerator sequenceGenerator = entityMappings().sequenceGenerators().next(); + assertEquals("FOO", sequenceGenerator.getName()); + + + SequenceGenerator sequenceGeneratorResource2 = OrmFactory.eINSTANCE.createSequenceGeneratorImpl(); + ormResource().getEntityMappings().getSequenceGenerators().add(0, sequenceGeneratorResource2); + sequenceGeneratorResource2.setName("BAR"); + ormResource().save(null); + + ListIterator<ISequenceGenerator> sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("BAR", sequenceGenerators.next().getName()); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + + + SequenceGenerator sequenceGeneratorResource3 = OrmFactory.eINSTANCE.createSequenceGeneratorImpl(); + ormResource().getEntityMappings().getSequenceGenerators().add(1, sequenceGeneratorResource3); + sequenceGeneratorResource3.setName("BAZ"); + ormResource().save(null); + + sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("BAR", sequenceGenerators.next().getName()); + assertEquals("BAZ", sequenceGenerators.next().getName()); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + + ormResource().getEntityMappings().getSequenceGenerators().move(2, 0); + ormResource().save(null); + sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("BAZ", sequenceGenerators.next().getName()); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertEquals("BAR", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + + + ormResource().getEntityMappings().getSequenceGenerators().remove(0); + ormResource().save(null); + sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertEquals("BAR", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + + ormResource().getEntityMappings().getSequenceGenerators().remove(1); + ormResource().save(null); + sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + + ormResource().getEntityMappings().getSequenceGenerators().clear(); + ormResource().save(null); + sequenceGenerators = entityMappings().sequenceGenerators(); + assertFalse(sequenceGenerators.hasNext()); + } + + public void testAddSequenceGenerator() throws Exception { + assertEquals(0, entityMappings().sequenceGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + + entityMappings().addSequenceGenerator(0).setName("FOO"); + + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + + entityMappings().addSequenceGenerator(0).setName("BAR"); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + assertEquals(2, ormResource().getEntityMappings().getSequenceGenerators().size()); + + ListIterator<ISequenceGenerator> sequenceGenerators = entityMappings().sequenceGenerators(); + assertEquals("BAR", sequenceGenerators.next().getName()); + assertEquals("FOO", sequenceGenerators.next().getName()); + assertFalse(sequenceGenerators.hasNext()); + } + + public void testRemoveSequenceGenerator() throws Exception { + assertEquals(0, entityMappings().sequenceGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + sequenceGenerator.setName("FOO"); + ISequenceGenerator sequenceGenerator2 = entityMappings().addSequenceGenerator(1); + sequenceGenerator2.setName("BAR"); + ISequenceGenerator sequenceGenerator3 = entityMappings().addSequenceGenerator(2); + sequenceGenerator3.setName("BAZ"); + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getSequenceGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getSequenceGenerators().size()); + + entityMappings().removeSequenceGenerator(0); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + + ListIterator<ISequenceGenerator> sequenceGenerators = entityMappings().sequenceGenerators(); + ISequenceGenerator xmlSequenceGenerator = sequenceGenerators.next(); + assertEquals("BAR", xmlSequenceGenerator.getName()); + assertEquals(sequenceGenerator2, xmlSequenceGenerator); + xmlSequenceGenerator = sequenceGenerators.next(); + assertEquals("BAZ", xmlSequenceGenerator.getName()); + assertEquals(sequenceGenerator3, xmlSequenceGenerator); + assertFalse(sequenceGenerators.hasNext()); + + + entityMappings().removeSequenceGenerator(1); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + sequenceGenerators = entityMappings().sequenceGenerators(); + xmlSequenceGenerator = sequenceGenerators.next(); + assertEquals("BAR", xmlSequenceGenerator.getName()); + assertEquals(sequenceGenerator2, xmlSequenceGenerator); + assertFalse(sequenceGenerators.hasNext()); + + + entityMappings().removeSequenceGenerator(0); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + sequenceGenerators = entityMappings().sequenceGenerators(); + assertFalse(sequenceGenerators.hasNext()); + } + + public void testMoveSequenceGenerator() throws Exception { + assertEquals(0, entityMappings().sequenceGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + sequenceGenerator.setName("FOO"); + ISequenceGenerator sequenceGenerator2 = entityMappings().addSequenceGenerator(1); + sequenceGenerator2.setName("BAR"); + ISequenceGenerator sequenceGenerator3 = entityMappings().addSequenceGenerator(2); + sequenceGenerator3.setName("BAZ"); + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getSequenceGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getSequenceGenerators().size()); + + entityMappings().moveSequenceGenerator(2, 0); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getSequenceGenerators().size()); + + entityMappings().moveSequenceGenerator(0, 2); + assertEquals("FOO", ormResource().getEntityMappings().getSequenceGenerators().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getSequenceGenerators().get(1).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getSequenceGenerators().get(2).getName()); + assertEquals(3, ormResource().getEntityMappings().getSequenceGenerators().size()); + + } + + public void testSequenceGeneratorsSize() throws Exception { + assertEquals(0, entityMappings().sequenceGeneratorsSize()); + assertEquals(0, ormResource().getEntityMappings().getSequenceGenerators().size()); + + + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + sequenceGenerator.setName("FOO"); + ISequenceGenerator sequenceGenerator2 = entityMappings().addSequenceGenerator(1); + sequenceGenerator2.setName("BAR"); + ISequenceGenerator sequenceGenerator3 = entityMappings().addSequenceGenerator(2); + sequenceGenerator3.setName("BAZ"); + + assertEquals(3, entityMappings().sequenceGeneratorsSize()); + + ormResource().getEntityMappings().getSequenceGenerators().remove(0); + assertEquals(2, entityMappings().sequenceGeneratorsSize()); + } + + + public void testAddNamedQuery() throws Exception { + XmlNamedQuery namedQuery = entityMappings().addNamedQuery(0); + namedQuery.setName("FOO"); + + assertEquals("FOO", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + + XmlNamedQuery namedQuery2 = entityMappings().addNamedQuery(0); + namedQuery2.setName("BAR"); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedQueries().get(1).getName()); + + XmlNamedQuery namedQuery3 = entityMappings().addNamedQuery(1); + namedQuery3.setName("BAZ"); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedQueries().get(2).getName()); + + ListIterator<XmlNamedQuery> namedQuerys = entityMappings().namedQueries(); + assertEquals(namedQuery2, namedQuerys.next()); + assertEquals(namedQuery3, namedQuerys.next()); + assertEquals(namedQuery, namedQuerys.next()); + + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + } + + public void testRemoveNamedQuery() throws Exception { + entityMappings().addNamedQuery(0).setName("FOO"); + entityMappings().addNamedQuery(1).setName("BAR"); + entityMappings().addNamedQuery(2).setName("BAZ"); + + assertEquals(3, ormResource().getEntityMappings().getNamedQueries().size()); + + entityMappings().removeNamedQuery(0); + assertEquals(2, ormResource().getEntityMappings().getNamedQueries().size()); + assertEquals("BAR", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedQueries().get(1).getName()); + + entityMappings().removeNamedQuery(0); + assertEquals(1, ormResource().getEntityMappings().getNamedQueries().size()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + + entityMappings().removeNamedQuery(0); + assertEquals(0, ormResource().getEntityMappings().getNamedQueries().size()); + } + + public void testMoveNamedQuery() throws Exception { + entityMappings().addNamedQuery(0).setName("FOO"); + entityMappings().addNamedQuery(1).setName("BAR"); + entityMappings().addNamedQuery(2).setName("BAZ"); + + assertEquals(3, ormResource().getEntityMappings().getNamedQueries().size()); + + + entityMappings().moveNamedQuery(2, 0); + ListIterator<XmlNamedQuery> namedQuerys = entityMappings().namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedQueries().get(2).getName()); + + + entityMappings().moveNamedQuery(0, 1); + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + + assertEquals("BAZ", ormResource().getEntityMappings().getNamedQueries().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getNamedQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedQueries().get(2).getName()); + } + + public void testUpdateNamedQueries() throws Exception { + ormResource().getEntityMappings().getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + ormResource().getEntityMappings().getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + ormResource().getEntityMappings().getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + + ormResource().getEntityMappings().getNamedQueries().get(0).setName("FOO"); + ormResource().getEntityMappings().getNamedQueries().get(1).setName("BAR"); + ormResource().getEntityMappings().getNamedQueries().get(2).setName("BAZ"); + + ListIterator<XmlNamedQuery> namedQuerys = entityMappings().namedQueries(); + assertEquals("FOO", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedQueries().move(2, 0); + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedQueries().move(0, 1); + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedQueries().remove(1); + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedQueries().remove(1); + namedQuerys = entityMappings().namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedQueries().remove(0); + assertFalse(entityMappings().namedQueries().hasNext()); + } + + public void testAddNamedNativeQuery() throws Exception { + XmlNamedNativeQuery namedNativeQuery = entityMappings().addNamedNativeQuery(0); + namedNativeQuery.setName("FOO"); + + assertEquals("FOO", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + + XmlNamedNativeQuery namedNativeQuery2 = entityMappings().addNamedNativeQuery(0); + namedNativeQuery2.setName("BAR"); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedNativeQueries().get(1).getName()); + + XmlNamedNativeQuery namedNativeQuery3 = entityMappings().addNamedNativeQuery(1); + namedNativeQuery3.setName("BAZ"); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedNativeQueries().get(2).getName()); + + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals(namedNativeQuery2, namedNativeQuerys.next()); + assertEquals(namedNativeQuery3, namedNativeQuerys.next()); + assertEquals(namedNativeQuery, namedNativeQuerys.next()); + + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + } + + public void testRemoveNamedNativeQuery() throws Exception { + entityMappings().addNamedNativeQuery(0).setName("FOO"); + entityMappings().addNamedNativeQuery(1).setName("BAR"); + entityMappings().addNamedNativeQuery(2).setName("BAZ"); + + assertEquals(3, ormResource().getEntityMappings().getNamedNativeQueries().size()); + + entityMappings().removeNamedNativeQuery(0); + assertEquals(2, ormResource().getEntityMappings().getNamedNativeQueries().size()); + assertEquals("BAR", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedNativeQueries().get(1).getName()); + + entityMappings().removeNamedNativeQuery(0); + assertEquals(1, ormResource().getEntityMappings().getNamedNativeQueries().size()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + + entityMappings().removeNamedNativeQuery(0); + assertEquals(0, ormResource().getEntityMappings().getNamedNativeQueries().size()); + } + + public void testMoveNamedNativeQuery() throws Exception { + entityMappings().addNamedNativeQuery(0).setName("FOO"); + entityMappings().addNamedNativeQuery(1).setName("BAR"); + entityMappings().addNamedNativeQuery(2).setName("BAZ"); + + assertEquals(3, ormResource().getEntityMappings().getNamedNativeQueries().size()); + + + entityMappings().moveNamedNativeQuery(2, 0); + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + + assertEquals("BAR", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", ormResource().getEntityMappings().getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedNativeQueries().get(2).getName()); + + + entityMappings().moveNamedNativeQuery(0, 1); + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + + assertEquals("BAZ", ormResource().getEntityMappings().getNamedNativeQueries().get(0).getName()); + assertEquals("BAR", ormResource().getEntityMappings().getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", ormResource().getEntityMappings().getNamedNativeQueries().get(2).getName()); + } + + public void testUpdateNamedNativeQueries() throws Exception { + ormResource().getEntityMappings().getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + ormResource().getEntityMappings().getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + ormResource().getEntityMappings().getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + + ormResource().getEntityMappings().getNamedNativeQueries().get(0).setName("FOO"); + ormResource().getEntityMappings().getNamedNativeQueries().get(1).setName("BAR"); + ormResource().getEntityMappings().getNamedNativeQueries().get(2).setName("BAZ"); + + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedNativeQueries().move(2, 0); + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedNativeQueries().move(0, 1); + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedNativeQueries().remove(1); + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedNativeQueries().remove(1); + namedNativeQuerys = entityMappings().namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + ormResource().getEntityMappings().getNamedNativeQueries().remove(0); + assertFalse(entityMappings().namedNativeQueries().hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/JptCoreOrmContextModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/JptCoreOrmContextModelTests.java new file mode 100644 index 0000000000..d84fcd30ac --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/JptCoreOrmContextModelTests.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class JptCoreOrmContextModelTests extends TestCase +{ + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptCoreOrmContextModelTests.class.getName()); + suite.addTestSuite(OrmXmlTests.class); + suite.addTestSuite(EntityMappingsTests.class); + suite.addTestSuite(PersistenceUnitMetadataTests.class); + suite.addTestSuite(PersistenceUnitDefaultsTests.class); + suite.addTestSuite(XmlPersistentTypeTests.class); + suite.addTestSuite(XmlAssociationOverrideTests.class); + suite.addTestSuite(XmlAttributeOverrideTests.class); + suite.addTestSuite(XmlBasicMappingTests.class); + suite.addTestSuite(XmlCascadeTests.class); + suite.addTestSuite(XmlColumnTests.class); + suite.addTestSuite(XmlDiscriminatorColumnTests.class); + suite.addTestSuite(XmlIdMappingTests.class); + suite.addTestSuite(XmlEmbeddableTests.class); + suite.addTestSuite(XmlEmbeddedMappingTests.class); + suite.addTestSuite(XmlEmbeddedIdMappingTests.class); + suite.addTestSuite(XmlEntityTests.class); + suite.addTestSuite(XmlGeneratedValueTests.class); + suite.addTestSuite(XmlJoinColumnTests.class); + suite.addTestSuite(XmlJoinTableTests.class); + suite.addTestSuite(XmlMappedSuperclassTests.class); + suite.addTestSuite(XmlManyToManyMappingTests.class); + suite.addTestSuite(XmlManyToOneMappingTests.class); + suite.addTestSuite(XmlNamedQueryTests.class); + suite.addTestSuite(XmlNamedNativeQueryTests.class); + suite.addTestSuite(XmlOneToManyMappingTests.class); + suite.addTestSuite(XmlOneToOneMappingTests.class); + suite.addTestSuite(XmlPrimaryKeyJoinColumnTests.class); + suite.addTestSuite(XmlQueryHintTests.class); + suite.addTestSuite(XmlSecondaryTableTests.class); + suite.addTestSuite(XmlSequenceGeneratorTests.class); + suite.addTestSuite(XmlTableGeneratorTests.class); + suite.addTestSuite(XmlTableTests.class); + suite.addTestSuite(XmlTransientMappingTests.class); + suite.addTestSuite(XmlVersionMappingTests.class); + return suite; + } + + private JptCoreOrmContextModelTests() { + throw new UnsupportedOperationException(); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmXmlTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmXmlTests.java new file mode 100644 index 0000000000..df86346e9b --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/OrmXmlTests.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.IPersistenceXml; +import org.eclipse.jpt.core.internal.context.orm.OrmXml; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.orm.OrmResource; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class OrmXmlTests extends ContextModelTestCase +{ + public OrmXmlTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + xmlPersistenceUnit().setName("foo"); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + protected IPersistenceXml persistenceXml() { + return jpaContent().getPersistenceXml(); + } + + protected OrmXml ormXml() { + return persistenceUnit().mappingFileRefs().next().getOrmXml(); + } + + public void testUpdateAddEntityMappings() throws Exception { + OrmResource ormResource = ormResource(); + ormResource.getContents().clear(); + ormResource.save(null); + + assertNull(ormXml().getEntityMappings()); + + ormResource.getContents().add(OrmFactory.eINSTANCE.createEntityMappings()); + + assertNotNull(ormXml().getEntityMappings()); + + } + + public void testModifyAddEntityMappings() { + OrmResource ormResource = ormResource(); + ormResource.getContents().remove(ormResource.getEntityMappings()); + assertNull(ormResource.getEntityMappings()); + + OrmXml ormXml = ormXml(); + assertNull(ormXml.getEntityMappings()); + + ormXml.addEntityMappings(); + + assertNotNull(ormXml.getEntityMappings()); + + boolean exceptionThrown = false; + try { + ormXml.addEntityMappings(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue("IllegalStateException was not thrown", exceptionThrown); + } + + public void testUpdateRemoveEntityMappings() throws Exception { + OrmResource ormResource = ormResource(); + + assertNotNull(ormXml().getEntityMappings()); + + ormResource.getContents().clear(); + + assertNull(ormXml().getEntityMappings()); + } + + public void testModifyRemoveEntityMappings() { + OrmXml ormXml = ormXml(); + + assertNotNull(ormXml.getEntityMappings()); + + ormXml.removeEntityMappings(); + + assertNull(ormXml.getEntityMappings()); + + boolean exceptionThrown = false; + try { + ormXml.removeEntityMappings(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue("IllegalStateException was not thrown", exceptionThrown); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitDefaultsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitDefaultsTests.java new file mode 100644 index 0000000000..f699940f3c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitDefaultsTests.java @@ -0,0 +1,369 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.orm.PersistenceUnitDefaults; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitMetadata; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class PersistenceUnitDefaultsTests extends ContextModelTestCase +{ + public PersistenceUnitDefaultsTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + protected PersistenceUnitDefaults persistenceUnitDefaults() { + return entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults(); + } + + public void testIsAllFeaturesUnset() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + PersistenceUnitMetadata persistenceUnitMetadata = OrmFactory.eINSTANCE.createPersistenceUnitMetadata(); + ormResource().getEntityMappings().setPersistenceUnitMetadata(persistenceUnitMetadata); + assertTrue(persistenceUnitMetadata.isAllFeaturesUnset()); + + org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitDefaults persistenceUnitDefaultsResource = OrmFactory.eINSTANCE.createPersistenceUnitDefaults(); + persistenceUnitMetadata.setPersistenceUnitDefaults(persistenceUnitDefaultsResource); + assertTrue(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setCascadePersist(true); + assertFalse(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setCascadePersist(false); + assertTrue(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setSchema("asdf"); + assertFalse(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setSchema(null); + assertTrue(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setCatalog("asdf"); + assertFalse(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setCatalog(null); + assertTrue(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + + persistenceUnitDefaultsResource.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY); + assertFalse(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertFalse(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + + persistenceUnitDefaultsResource.setAccess(null); + assertTrue(persistenceUnitDefaultsResource.isAllFeaturesUnset()); + } + + public void testUpdateSchema() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set schema in the resource model, verify context model updated + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", persistenceUnitDefaults.getSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + + //set schema to null in the resource model + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + } + + public void testModifySchema() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set schema in the context model, verify resource model modified + persistenceUnitDefaults.setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", persistenceUnitDefaults.getSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + + //set schema to null in the context model + persistenceUnitDefaults.setSchema(null); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + } + + public void testModifySchema2() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set schema in the context model, verify resource model modified + persistenceUnitDefaults.setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", persistenceUnitDefaults.getSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + + //set another element on the persistence-unit-defaults element so it doesn't get removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("MY_CATALOG"); + //set schema to null in the context model + persistenceUnitDefaults.setSchema(null); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + } + + public void testModifySchema3() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + //set another element on the persistence-unit-metadata element so only persistence-unit-defaults element gets removed + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + + //set schema in the context model, verify resource model modified + persistenceUnitDefaults.setSchema("MY_SCHEMA"); + assertEquals("MY_SCHEMA", persistenceUnitDefaults.getSchema()); + assertEquals("MY_SCHEMA", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getSchema()); + + //set schema to null in the context model + persistenceUnitDefaults.setSchema(null); + assertNull(persistenceUnitDefaults.getSchema()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults()); + } + + public void testUpdateCatalog() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set catalog in the resource model, verify context model updated + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", persistenceUnitDefaults.getCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + + //set catalog to null in the resource model + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + } + + public void testModifyCatalog() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set catalog in the context model, verify resource model modified + persistenceUnitDefaults.setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", persistenceUnitDefaults.getCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + + //set catalog to null in the context model + persistenceUnitDefaults.setCatalog(null); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + } + + public void testModifyCatalog2() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set catalog in the context model, verify resource model modified + persistenceUnitDefaults.setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", persistenceUnitDefaults.getCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + + //set another element on the persistence-unit-defaults element so it doesn't get removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("MY_SCHEMA"); + //set catalog to null in the context model + persistenceUnitDefaults.setCatalog(null); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + } + + public void testModifyCatalog3() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + //set another element on the persistence-unit-metadata element so only persistence-unit-defaults element gets removed + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + + //set catalog in the context model, verify resource model modified + persistenceUnitDefaults.setCatalog("MY_CATALOG"); + assertEquals("MY_CATALOG", persistenceUnitDefaults.getCatalog()); + assertEquals("MY_CATALOG", ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getCatalog()); + + //set catalog to null in the context model + persistenceUnitDefaults.setCatalog(null); + assertNull(persistenceUnitDefaults.getCatalog()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults()); + } + + public void testUpdateCascadePersist() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set cascadePersist in the resource model, verify context model updated + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCascadePersist(true); + assertTrue(persistenceUnitDefaults.isCascadePersist()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + + //set cascadePersist to null in the resource model, persistence-unit-defaults tag not removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCascadePersist(false); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertFalse(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + } + + public void testModifyCascadePersist() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set cascadePersist in the context model, verify resource model modified + persistenceUnitDefaults.setCascadePersist(true); + assertTrue(persistenceUnitDefaults.isCascadePersist()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + + //set cascadePersist to null in the context model + persistenceUnitDefaults.setCascadePersist(false); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + } + + public void testModifyCascadePersist2() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set cascadePersist in the context model, verify resource model modified + persistenceUnitDefaults.setCascadePersist(true); + assertTrue(persistenceUnitDefaults.isCascadePersist()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + + //set another element on the persistence-unit-defaults element so it doesn't get removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("MY_CATALOG"); + //set cascadePersist to null in the context model + persistenceUnitDefaults.setCascadePersist(false); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertFalse(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + } + + public void testModifyCascadePersist3() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + //set another element on the persistence-unit-metadata element so only persistence-unit-defaults element gets removed + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + + //set cascadePersist in the context model, verify resource model modified + persistenceUnitDefaults.setCascadePersist(true); + assertTrue(persistenceUnitDefaults.isCascadePersist()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().isCascadePersist()); + + //set cascadePersist to null in the context model + persistenceUnitDefaults.setCascadePersist(false); + assertFalse(persistenceUnitDefaults.isCascadePersist()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults()); + } + + + public void testUpdateAccess() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set access in the resource model, verify context model updated + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertEquals(AccessType.FIELD, persistenceUnitDefaults.getAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + + //set access to null in the resource model + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(null); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + } + + public void testModifyAccess() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set access in the context model, verify resource model modified + persistenceUnitDefaults.setAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, persistenceUnitDefaults.getAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + + //set access to null in the context model + persistenceUnitDefaults.setAccess(null); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + } + + public void testModifyAccess2() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set access in the context model, verify resource model modified + persistenceUnitDefaults.setAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, persistenceUnitDefaults.getAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + + //set another element on the persistence-unit-defaults element so it doesn't get removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCascadePersist(true); + //set access to null in the context model + persistenceUnitDefaults.setAccess(null); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + } + + public void testModifyAccess3() throws Exception { + PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitDefaults(); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + //set another element on the persistence-unit-metadata element so only persistence-unit-defaults element gets removed + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + + //set access in the context model, verify resource model modified + persistenceUnitDefaults.setAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, persistenceUnitDefaults.getAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().getAccess()); + + //set access to null in the context model + persistenceUnitDefaults.setAccess(null); + assertNull(persistenceUnitDefaults.getAccess()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitMetadataTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitMetadataTests.java new file mode 100644 index 0000000000..f42fb493fb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/PersistenceUnitMetadataTests.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.PersistenceUnitMetadata; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class PersistenceUnitMetadataTests extends ContextModelTestCase +{ + public PersistenceUnitMetadataTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + protected PersistenceUnitMetadata persistenceUnitMetadata() { + return entityMappings().getPersistenceUnitMetadata(); + } + + public void testIsAllFeaturesUnset() throws Exception { + org.eclipse.jpt.core.internal.resource.orm.PersistenceUnitMetadata persistenceUnitMetadata = OrmFactory.eINSTANCE.createPersistenceUnitMetadata(); + ormResource().getEntityMappings().setPersistenceUnitMetadata(persistenceUnitMetadata); + assertTrue(persistenceUnitMetadata.isAllFeaturesUnset()); + + persistenceUnitMetadata.setXmlMappingMetadataComplete(true); + assertFalse(persistenceUnitMetadata.isAllFeaturesUnset()); + + persistenceUnitMetadata.setXmlMappingMetadataComplete(false); + assertTrue(persistenceUnitMetadata.isAllFeaturesUnset()); + + persistenceUnitMetadata.setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + assertFalse(persistenceUnitMetadata.isAllFeaturesUnset()); + } + + public void testUpdateXmlMappingMetadataComplete() throws Exception { + PersistenceUnitMetadata persistenceUnitMetadata = entityMappings().getPersistenceUnitMetadata(); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set xmlMappingMetadataComplete in the resource model, verify context model updated + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().isXmlMappingMetadataComplete()); + + //set xmlMappingMetadataComplete to null in the resource model + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertFalse(ormResource().getEntityMappings().getPersistenceUnitMetadata().isXmlMappingMetadataComplete()); + } + + public void testModifyXmlMappingMetadataComplete() throws Exception { + PersistenceUnitMetadata persistenceUnitMetadata = entityMappings().getPersistenceUnitMetadata(); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set xmlMappingMetadataComplete in the context model, verify resource model modified + persistenceUnitMetadata.setXmlMappingMetadataComplete(true); + assertTrue(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().isXmlMappingMetadataComplete()); + + //set xmlMappingMetadataComplete to null in the context model + persistenceUnitMetadata.setXmlMappingMetadataComplete(false); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + } + + public void testModifyXmlMappingMetadataComplete2() throws Exception { + PersistenceUnitMetadata persistenceUnitMetadata = entityMappings().getPersistenceUnitMetadata(); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertNull(ormResource().getEntityMappings().getPersistenceUnitMetadata()); + + //set xmlMappingMetadataComplete in the context model, verify resource model modified + persistenceUnitMetadata.setXmlMappingMetadataComplete(true); + assertTrue(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertTrue(ormResource().getEntityMappings().getPersistenceUnitMetadata().isXmlMappingMetadataComplete()); + + //set xmlMappingMetadataComplete to null in the context model + //set another element on the persistence-unit-metadata element so it doesn't get removed + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + persistenceUnitMetadata.setXmlMappingMetadataComplete(false); + assertFalse(persistenceUnitMetadata.isXmlMappingMetadataComplete()); + assertFalse(ormResource().getEntityMappings().getPersistenceUnitMetadata().isXmlMappingMetadataComplete()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAssociationOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAssociationOverrideTests.java new file mode 100644 index 0000000000..3d93beb16a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAssociationOverrideTests.java @@ -0,0 +1,251 @@ +/******************************************************************************* + * 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlAssociationOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.AssociationOverride; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlAssociationOverrideTests extends ContextModelTestCase +{ + public XmlAssociationOverrideTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride xmlAssociationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + AssociationOverride associationOverrideResource = entityResource.getAssociationOverrides().get(0); + + assertNull(xmlAssociationOverride.getName()); + assertNull(associationOverrideResource.getName()); + assertTrue(xmlEntity.associationOverrides().hasNext()); + assertFalse(entityResource.getAssociationOverrides().isEmpty()); + + //set name in the resource model, verify context model updated + associationOverrideResource.setName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlAssociationOverride.getName()); + assertEquals("FOO", associationOverrideResource.getName()); + + //set name to null in the resource model + associationOverrideResource.setName(null); + ormResource().save(null); + assertNull(xmlAssociationOverride.getName()); + assertNull(associationOverrideResource.getName()); + + associationOverrideResource.setName("FOO"); + assertEquals("FOO", xmlAssociationOverride.getName()); + assertEquals("FOO", associationOverrideResource.getName()); + + entityResource.getAssociationOverrides().remove(0); + ormResource().save(null); + assertFalse(xmlEntity.associationOverrides().hasNext()); + assertTrue(entityResource.getAssociationOverrides().isEmpty()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride xmlAssociationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + AssociationOverride associationOverrideResource = entityResource.getAssociationOverrides().get(0); + + assertNull(xmlAssociationOverride.getName()); + assertNull(associationOverrideResource.getName()); + + //set name in the context model, verify resource model modified + xmlAssociationOverride.setName("foo"); + assertEquals("foo", xmlAssociationOverride.getName()); + assertEquals("foo", associationOverrideResource.getName()); + + //set name to null in the context model + xmlAssociationOverride.setName(null); + assertNull(xmlAssociationOverride.getName()); + assertNull(entityResource.getAssociationOverrides().get(0).getName()); + } + + + public void testAddSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride associationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + AssociationOverride associationOverrideResource = ormResource().getEntityMappings().getEntities().get(0).getAssociationOverrides().get(0); + + XmlJoinColumn joinColumn = associationOverride.addSpecifiedJoinColumn(0); + joinColumn.setSpecifiedName("FOO"); + + assertEquals("FOO", associationOverrideResource.getJoinColumns().get(0).getName()); + + XmlJoinColumn joinColumn2 = associationOverride.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", associationOverrideResource.getJoinColumns().get(0).getName()); + assertEquals("FOO", associationOverrideResource.getJoinColumns().get(1).getName()); + + XmlJoinColumn joinColumn3 = associationOverride.addSpecifiedJoinColumn(1); + ormResource().save(null); + joinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", associationOverrideResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", associationOverrideResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", associationOverrideResource.getJoinColumns().get(2).getName()); + + ListIterator<XmlJoinColumn> joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride associationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + AssociationOverride associationOverrideResource = ormResource().getEntityMappings().getEntities().get(0).getAssociationOverrides().get(0); + + associationOverride.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + associationOverride.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + associationOverride.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, associationOverrideResource.getJoinColumns().size()); + + associationOverride.removeSpecifiedJoinColumn(0); + assertEquals(2, associationOverrideResource.getJoinColumns().size()); + assertEquals("BAR", associationOverrideResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", associationOverrideResource.getJoinColumns().get(1).getName()); + + associationOverride.removeSpecifiedJoinColumn(0); + assertEquals(1, associationOverrideResource.getJoinColumns().size()); + assertEquals("BAZ", associationOverrideResource.getJoinColumns().get(0).getName()); + + associationOverride.removeSpecifiedJoinColumn(0); + assertEquals(0, associationOverrideResource.getJoinColumns().size()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride associationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + AssociationOverride associationOverrideResource = ormResource().getEntityMappings().getEntities().get(0).getAssociationOverrides().get(0); + + associationOverride.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + associationOverride.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + associationOverride.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, associationOverrideResource.getJoinColumns().size()); + + + associationOverride.moveSpecifiedJoinColumn(2, 0); + ListIterator<XmlJoinColumn> joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", associationOverrideResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", associationOverrideResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", associationOverrideResource.getJoinColumns().get(2).getName()); + + + associationOverride.moveSpecifiedJoinColumn(0, 1); + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", associationOverrideResource.getJoinColumns().get(0).getName()); + assertEquals("BAR", associationOverrideResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", associationOverrideResource.getJoinColumns().get(2).getName()); + } + + public void testUpdateJoinColumns() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAssociationOverride associationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + + AssociationOverride associationOverrideResource = ormResource().getEntityMappings().getEntities().get(0).getAssociationOverrides().get(0); + + associationOverrideResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + associationOverrideResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + associationOverrideResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + + associationOverrideResource.getJoinColumns().get(0).setName("FOO"); + associationOverrideResource.getJoinColumns().get(1).setName("BAR"); + associationOverrideResource.getJoinColumns().get(2).setName("BAZ"); + + ListIterator<XmlJoinColumn> joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + associationOverrideResource.getJoinColumns().move(2, 0); + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + associationOverrideResource.getJoinColumns().move(0, 1); + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + associationOverrideResource.getJoinColumns().remove(1); + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + associationOverrideResource.getJoinColumns().remove(1); + joinColumns = associationOverride.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + associationOverrideResource.getJoinColumns().remove(0); + assertFalse(associationOverride.specifiedJoinColumns().hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAttributeOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAttributeOverrideTests.java new file mode 100644 index 0000000000..62386c663c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlAttributeOverrideTests.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlAttributeOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlAttributeOverrideTests extends ContextModelTestCase +{ + public XmlAttributeOverrideTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAttributeOverride xmlAttributeOverride = xmlEntity.addSpecifiedAttributeOverride(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + AttributeOverride attributeOverrideResource = entityResource.getAttributeOverrides().get(0); + + assertNull(xmlAttributeOverride.getName()); + assertNull(attributeOverrideResource.getName()); + assertTrue(xmlEntity.attributeOverrides().hasNext()); + assertFalse(entityResource.getAttributeOverrides().isEmpty()); + + //set name in the resource model, verify context model updated + attributeOverrideResource.setName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlAttributeOverride.getName()); + assertEquals("FOO", attributeOverrideResource.getName()); + + //set name to null in the resource model + attributeOverrideResource.setName(null); + ormResource().save(null); + assertNull(xmlAttributeOverride.getName()); + assertNull(attributeOverrideResource.getName()); + + attributeOverrideResource.setName("FOO"); + assertEquals("FOO", xmlAttributeOverride.getName()); + assertEquals("FOO", attributeOverrideResource.getName()); + + entityResource.getAttributeOverrides().remove(0); + ormResource().save(null); + assertFalse(xmlEntity.attributeOverrides().hasNext()); + assertTrue(entityResource.getAttributeOverrides().isEmpty()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlAttributeOverride xmlAttributeOverride = xmlEntity.addSpecifiedAttributeOverride(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + AttributeOverride attributeOverrideResource = entityResource.getAttributeOverrides().get(0); + + assertNull(xmlAttributeOverride.getName()); + assertNull(attributeOverrideResource.getName()); + + //set name in the context model, verify resource model modified + xmlAttributeOverride.setName("foo"); + assertEquals("foo", xmlAttributeOverride.getName()); + assertEquals("foo", attributeOverrideResource.getName()); + + //set name to null in the context model + xmlAttributeOverride.setName(null); + assertNull(xmlAttributeOverride.getName()); + assertNull(entityResource.getAttributeOverrides().get(0).getName()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlBasicMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlBasicMappingTests.java new file mode 100644 index 0000000000..c1a5d314b5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlBasicMappingTests.java @@ -0,0 +1,568 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.EnumType; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.context.orm.XmlBasicMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Basic; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlBasicMappingTests extends ContextModelTestCase +{ + public XmlBasicMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createBasicAnnotation() throws Exception{ + this.createAnnotationAndMembers("Basic", "FetchType fetch() default EAGER; boolean optional() default true;"); + } + + private void createColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private void createLobAnnotation() throws Exception{ + this.createAnnotationAndMembers("Lob", ""); + } + + private void createEnumeratedAnnotation() throws Exception{ + this.createAnnotationAndMembers("Enumerated", "EnumType value() default ORDINAL;"); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private IType createTestEntityBasicMapping() throws Exception { + createEntityAnnotation(); + createBasicAnnotation(); + createColumnAnnotation(); + createLobAnnotation(); + createTemporalAnnotation(); + createEnumeratedAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.BASIC, JPA.FETCH_TYPE, JPA.COLUMN, JPA.LOB, JPA.TEMPORAL, JPA.TEMPORAL_TYPE, JPA.ENUMERATED, JPA.ENUM_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic(fetch=FetchType.LAZY, optional=false)"); + sb.append(CR); + sb.append(" @Column(name=\"MY_COLUMN\", unique=true, nullable=false, insertable=false, updatable=false, columnDefinition=\"COLUMN_DEFINITION\", table=\"MY_TABLE\", length=5, precision=6, scale=7)"); + sb.append(CR); + sb.append(" @Lob"); + sb.append(CR); + sb.append(" @Temporal(TemporalType.TIMESTAMP)"); + sb.append(CR); + sb.append(" @Enumerated(EnumType.STRING)"); + } + }); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertEquals("basicMapping", xmlBasicMapping.getName()); + assertEquals("basicMapping", basicResource.getName()); + + //set name in the resource model, verify context model updated + basicResource.setName("newName"); + assertEquals("newName", xmlBasicMapping.getName()); + assertEquals("newName", basicResource.getName()); + + //set name to null in the resource model + basicResource.setName(null); + assertNull(xmlBasicMapping.getName()); + assertNull(basicResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertEquals("basicMapping", xmlBasicMapping.getName()); + assertEquals("basicMapping", basicResource.getName()); + + //set name in the context model, verify resource model updated + xmlBasicMapping.setName("newName"); + assertEquals("newName", xmlBasicMapping.getName()); + assertEquals("newName", basicResource.getName()); + + //set name to null in the context model + xmlBasicMapping.setName(null); + assertNull(xmlBasicMapping.getName()); + assertNull(basicResource.getName()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedFetch()); + assertNull(basicResource.getFetch()); + + //set fetch in the resource model, verify context model updated + basicResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER); + assertEquals(FetchType.EAGER, xmlBasicMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, basicResource.getFetch()); + + basicResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY); + assertEquals(FetchType.LAZY, xmlBasicMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, basicResource.getFetch()); + + //set fetch to null in the resource model + basicResource.setFetch(null); + assertNull(xmlBasicMapping.getSpecifiedFetch()); + assertNull(basicResource.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedFetch()); + assertNull(basicResource.getFetch()); + + //set fetch in the context model, verify resource model updated + xmlBasicMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, basicResource.getFetch()); + assertEquals(FetchType.EAGER, xmlBasicMapping.getSpecifiedFetch()); + + xmlBasicMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, basicResource.getFetch()); + assertEquals(FetchType.LAZY, xmlBasicMapping.getSpecifiedFetch()); + + //set fetch to null in the context model + xmlBasicMapping.setSpecifiedFetch(null); + assertNull(basicResource.getFetch()); + assertNull(xmlBasicMapping.getSpecifiedFetch()); + } + + public void testUpdateSpecifiedEnumerated() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + assertNull(basicResource.getEnumerated()); + + //set enumerated in the resource model, verify context model updated + basicResource.setEnumerated(org.eclipse.jpt.core.internal.resource.orm.EnumType.ORDINAL); + assertEquals(EnumType.ORDINAL, xmlBasicMapping.getSpecifiedEnumerated()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.EnumType.ORDINAL, basicResource.getEnumerated()); + + basicResource.setEnumerated(org.eclipse.jpt.core.internal.resource.orm.EnumType.STRING); + assertEquals(EnumType.STRING, xmlBasicMapping.getSpecifiedEnumerated()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.EnumType.STRING, basicResource.getEnumerated()); + + //set enumerated to null in the resource model + basicResource.setEnumerated(null); + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + assertNull(basicResource.getEnumerated()); + } + + public void testModifySpecifiedEnumerated() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + assertNull(basicResource.getEnumerated()); + + //set enumerated in the context model, verify resource model updated + xmlBasicMapping.setSpecifiedEnumerated(EnumType.ORDINAL); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.EnumType.ORDINAL, basicResource.getEnumerated()); + assertEquals(EnumType.ORDINAL, xmlBasicMapping.getSpecifiedEnumerated()); + + xmlBasicMapping.setSpecifiedEnumerated(EnumType.STRING); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.EnumType.STRING, basicResource.getEnumerated()); + assertEquals(EnumType.STRING, xmlBasicMapping.getSpecifiedEnumerated()); + + //set enumerated to null in the context model + xmlBasicMapping.setSpecifiedEnumerated(null); + assertNull(basicResource.getEnumerated()); + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + } + + public void testUpdateSpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedOptional()); + assertNull(basicResource.getOptional()); + + //set enumerated in the resource model, verify context model updated + basicResource.setOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlBasicMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, basicResource.getOptional()); + + basicResource.setOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlBasicMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, basicResource.getOptional()); + + //set enumerated to null in the resource model + basicResource.setOptional(null); + assertNull(xmlBasicMapping.getSpecifiedOptional()); + assertNull(basicResource.getOptional()); + } + + public void testModifySpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + + assertNull(xmlBasicMapping.getSpecifiedOptional()); + assertNull(basicResource.getOptional()); + + //set enumerated in the context model, verify resource model updated + xmlBasicMapping.setSpecifiedOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, basicResource.getOptional()); + assertEquals(Boolean.TRUE, xmlBasicMapping.getSpecifiedOptional()); + + xmlBasicMapping.setSpecifiedOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, basicResource.getOptional()); + assertEquals(Boolean.FALSE, xmlBasicMapping.getSpecifiedOptional()); + + //set enumerated to null in the context model + xmlBasicMapping.setSpecifiedOptional(null); + assertNull(basicResource.getOptional()); + assertNull(xmlBasicMapping.getSpecifiedOptional()); + } + + public void testUpdateSpecifiedLob() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + ormResource().save(null); + + assertFalse(xmlBasicMapping.isLob()); + assertFalse(basicResource.isLob()); + + //set lob in the resource model, verify context model updated + basicResource.setLob(true); + ormResource().save(null); + assertTrue(xmlBasicMapping.isLob()); + assertTrue(basicResource.isLob()); + + //set lob to null in the resource model + basicResource.setLob(false); + ormResource().save(null); + assertFalse(xmlBasicMapping.isLob()); + assertFalse(basicResource.isLob()); + } + + public void testModifySpecifiedLob() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + ormResource().save(null); + + assertFalse(xmlBasicMapping.isLob()); + assertFalse(basicResource.isLob()); + + //set lob in the context model, verify resource model updated + xmlBasicMapping.setLob(true); + ormResource().save(null); + assertTrue(basicResource.isLob()); + assertTrue(xmlBasicMapping.isLob()); + + //set lob to false in the context model + xmlBasicMapping.setLob(false); + ormResource().save(null); + assertFalse(basicResource.isLob()); + assertFalse(xmlBasicMapping.isLob()); + } + + public void testUpdateTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + ormResource().save(null); + + assertNull(xmlBasicMapping.getTemporal()); + assertNull(basicResource.getTemporal()); + + //set temporal in the resource model, verify context model updated + basicResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE); + ormResource().save(null); + assertEquals(TemporalType.DATE, xmlBasicMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, basicResource.getTemporal()); + + basicResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME); + ormResource().save(null); + assertEquals(TemporalType.TIME, xmlBasicMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, basicResource.getTemporal()); + + basicResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(TemporalType.TIMESTAMP, xmlBasicMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, basicResource.getTemporal()); + + //set temporal to null in the resource model + basicResource.setTemporal(null); + ormResource().save(null); + assertNull(xmlBasicMapping.getTemporal()); + assertNull(basicResource.getTemporal()); + } + + public void testModifyTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + Basic basicResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getBasics().get(0); + ormResource().save(null); + + assertNull(xmlBasicMapping.getTemporal()); + assertNull(basicResource.getTemporal()); + + //set temporal in the context model, verify resource model updated + xmlBasicMapping.setTemporal(TemporalType.DATE); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, basicResource.getTemporal()); + assertEquals(TemporalType.DATE, xmlBasicMapping.getTemporal()); + + xmlBasicMapping.setTemporal(TemporalType.TIME); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, basicResource.getTemporal()); + assertEquals(TemporalType.TIME, xmlBasicMapping.getTemporal()); + + xmlBasicMapping.setTemporal(TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, basicResource.getTemporal()); + assertEquals(TemporalType.TIMESTAMP, xmlBasicMapping.getTemporal()); + + //set temporal to null in the context model + xmlBasicMapping.setTemporal(null); + ormResource().save(null); + assertNull(basicResource.getTemporal()); + assertNull(xmlBasicMapping.getTemporal()); + } + + //TODO test morphing to other mapping types + //TODO test defaults + + + public void testBasicMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityBasicMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlBasicMapping.getName()); + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + assertNull(xmlBasicMapping.getSpecifiedFetch()); + assertNull(xmlBasicMapping.getSpecifiedOptional()); + assertFalse(xmlBasicMapping.isLob()); + assertEquals(EnumType.ORDINAL, xmlBasicMapping.getEnumerated()); + assertEquals(FetchType.EAGER, xmlBasicMapping.getFetch()); + assertEquals(Boolean.TRUE, xmlBasicMapping.getOptional()); + assertNull(xmlBasicMapping.getTemporal()); + + + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("foo", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + } + + //@Basic(fetch=FetchType.LAZY, optional=false) + //@Column(name="MY_COLUMN", unique=true, nullable=false, insertable=false, updatable=false, + // columnDefinition="COLUMN_DEFINITION", table="MY_TABLE", length=5, precision=6, scale=7)"); + //@Column( + //@Lob + //@Temporal(TemporalType.TIMESTAMP) + //@Enumerated(EnumType.STRING) + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityBasicMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlBasicMapping.getName()); + assertTrue(xmlBasicMapping.isLob()); + assertEquals(EnumType.STRING, xmlBasicMapping.getSpecifiedEnumerated()); + assertEquals(FetchType.LAZY, xmlBasicMapping.getSpecifiedFetch()); + assertEquals(Boolean.FALSE, xmlBasicMapping.getSpecifiedOptional()); + assertEquals(TemporalType.TIMESTAMP, xmlBasicMapping.getTemporal()); + + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + assertEquals("MY_COLUMN", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); + assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); + assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityBasicMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlBasicMapping.getName()); + assertFalse(xmlBasicMapping.isLob()); + assertEquals(EnumType.ORDINAL, xmlBasicMapping.getSpecifiedEnumerated()); + assertEquals(FetchType.EAGER, xmlBasicMapping.getSpecifiedFetch()); + assertEquals(Boolean.TRUE, xmlBasicMapping.getSpecifiedOptional()); + assertNull(xmlBasicMapping.getTemporal()); + + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + assertEquals("id", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedScale()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityBasicMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "id"); + assertEquals(1, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("id", xmlBasicMapping.getName()); + assertNull(xmlBasicMapping.getSpecifiedEnumerated()); + assertNull(xmlBasicMapping.getSpecifiedFetch()); + assertNull(xmlBasicMapping.getSpecifiedOptional()); + assertFalse(xmlBasicMapping.isLob()); + assertEquals(EnumType.ORDINAL, xmlBasicMapping.getDefaultEnumerated()); + assertEquals(FetchType.EAGER, xmlBasicMapping.getDefaultFetch()); + assertEquals(Boolean.TRUE, xmlBasicMapping.getDefaultOptional()); + assertNull(xmlBasicMapping.getTemporal()); + + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("id", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + + } + //3 things tested above + //1. virtual mapping metadata complete=false - defaults are taken from the java annotations + //2. virtual mapping metadata complete=true - defaults are taken from java defaults,annotations ignored + //3. specified mapping (metadata complete=true/false - defaults are taken from java annotations + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlCascadeTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlCascadeTests.java new file mode 100644 index 0000000000..6bceca8fa9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlCascadeTests.java @@ -0,0 +1,280 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlCascade; +import org.eclipse.jpt.core.internal.context.orm.XmlOneToOneMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.OneToOne; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlCascadeTests extends ContextModelTestCase +{ + public XmlCascadeTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateCascadeAll() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isAll()); + assertNull(oneToOne.getCascade()); + + //set cascade in the resource model, verify context model does not change + oneToOne.setCascade(OrmFactory.eINSTANCE.createCascadeTypeImpl()); + assertEquals(false, xmlCascade.isAll()); + assertNotNull(oneToOne.getCascade()); + + //set all in the resource model, verify context model updated + oneToOne.getCascade().setCascadeAll(true); + assertEquals(true, xmlCascade.isAll()); + assertEquals(true, oneToOne.getCascade().isCascadeAll()); + + //set all to false in the resource model + oneToOne.getCascade().setCascadeAll(false); + assertEquals(false, xmlCascade.isAll()); + assertEquals(false, oneToOne.getCascade().isCascadeAll()); + } + + public void testModifyCascadeAll() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isAll()); + assertNull(oneToOne.getCascade()); + + //set all in the context model, verify resource model updated + xmlCascade.setAll(true); ormResource().save(null); + assertEquals(true, xmlCascade.isAll()); + assertEquals(true, oneToOne.getCascade().isCascadeAll()); + + //set all to false in the context model + xmlCascade.setAll(false); + assertEquals(false, xmlCascade.isAll()); + assertNull(oneToOne.getCascade()); + } + + public void testUpdateCascadePersist() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isPersist()); + assertNull(oneToOne.getCascade()); + + //set cascade in the resource model, verify context model does not change + oneToOne.setCascade(OrmFactory.eINSTANCE.createCascadeTypeImpl()); + assertEquals(false, xmlCascade.isPersist()); + assertNotNull(oneToOne.getCascade()); + + //set Persist in the resource model, verify context model updated + oneToOne.getCascade().setCascadePersist(true); + assertEquals(true, xmlCascade.isPersist()); + assertEquals(true, oneToOne.getCascade().isCascadePersist()); + + //set Persist to false in the resource model + oneToOne.getCascade().setCascadePersist(false); + assertEquals(false, xmlCascade.isPersist()); + assertEquals(false, oneToOne.getCascade().isCascadePersist()); + } + + public void testModifyCascadePersist() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isPersist()); + assertNull(oneToOne.getCascade()); + + //set Persist in the context model, verify resource model updated + xmlCascade.setPersist(true); ormResource().save(null); + assertEquals(true, xmlCascade.isPersist()); + assertEquals(true, oneToOne.getCascade().isCascadePersist()); + + //set Persist to false in the context model + xmlCascade.setPersist(false); + assertEquals(false, xmlCascade.isPersist()); + assertNull(oneToOne.getCascade()); + } + + public void testUpdateCascadeMerge() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isMerge()); + assertNull(oneToOne.getCascade()); + + //set cascade in the resource model, verify context model does not change + oneToOne.setCascade(OrmFactory.eINSTANCE.createCascadeTypeImpl()); + assertEquals(false, xmlCascade.isMerge()); + assertNotNull(oneToOne.getCascade()); + + //set Merge in the resource model, verify context model updated + oneToOne.getCascade().setCascadeMerge(true); + assertEquals(true, xmlCascade.isMerge()); + assertEquals(true, oneToOne.getCascade().isCascadeMerge()); + + //set Merge to false in the resource model + oneToOne.getCascade().setCascadeMerge(false); + assertEquals(false, xmlCascade.isMerge()); + assertEquals(false, oneToOne.getCascade().isCascadeMerge()); + } + + public void testModifyCascadeMerge() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isMerge()); + assertNull(oneToOne.getCascade()); + + //set Merge in the context model, verify resource model updated + xmlCascade.setMerge(true); ormResource().save(null); + assertEquals(true, xmlCascade.isMerge()); + assertEquals(true, oneToOne.getCascade().isCascadeMerge()); + + //set Merge to false in the context model + xmlCascade.setMerge(false); + assertEquals(false, xmlCascade.isMerge()); + assertNull(oneToOne.getCascade()); + } + + public void testUpdateCascadeRemove() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isRemove()); + assertNull(oneToOne.getCascade()); + + //set cascade in the resource model, verify context model does not change + oneToOne.setCascade(OrmFactory.eINSTANCE.createCascadeTypeImpl()); + assertEquals(false, xmlCascade.isRemove()); + assertNotNull(oneToOne.getCascade()); + + //set Remove in the resource model, verify context model updated + oneToOne.getCascade().setCascadeRemove(true); + assertEquals(true, xmlCascade.isRemove()); + assertEquals(true, oneToOne.getCascade().isCascadeRemove()); + + //set Remove to false in the resource model + oneToOne.getCascade().setCascadeRemove(false); + assertEquals(false, xmlCascade.isRemove()); + assertEquals(false, oneToOne.getCascade().isCascadeRemove()); + } + + public void testModifyCascadeRemove() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isRemove()); + assertNull(oneToOne.getCascade()); + + //set Remove in the context model, verify resource model updated + xmlCascade.setRemove(true); ormResource().save(null); + assertEquals(true, xmlCascade.isRemove()); + assertEquals(true, oneToOne.getCascade().isCascadeRemove()); + + //set Remove to false in the context model + xmlCascade.setRemove(false); + assertEquals(false, xmlCascade.isRemove()); + assertNull(oneToOne.getCascade()); + } + + public void testUpdateCascadeRefresh() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isRefresh()); + assertNull(oneToOne.getCascade()); + + //set cascade in the resource model, verify context model does not change + oneToOne.setCascade(OrmFactory.eINSTANCE.createCascadeTypeImpl()); + assertEquals(false, xmlCascade.isRefresh()); + assertNotNull(oneToOne.getCascade()); + + //set Refresh in the resource model, verify context model updated + oneToOne.getCascade().setCascadeRefresh(true); + assertEquals(true, xmlCascade.isRefresh()); + assertEquals(true, oneToOne.getCascade().isCascadeRefresh()); + + //set Refresh to false in the resource model + oneToOne.getCascade().setCascadeRefresh(false); + assertEquals(false, xmlCascade.isRefresh()); + assertEquals(false, oneToOne.getCascade().isCascadeRefresh()); + } + + public void testModifyCascadeRefresh() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + + assertEquals(false, xmlCascade.isRefresh()); + assertNull(oneToOne.getCascade()); + + //set Refresh in the context model, verify resource model updated + xmlCascade.setRefresh(true); ormResource().save(null); + assertEquals(true, xmlCascade.isRefresh()); + assertEquals(true, oneToOne.getCascade().isCascadeRefresh()); + + //set Refresh to false in the context model + xmlCascade.setRefresh(false); + assertEquals(false, xmlCascade.isRefresh()); + assertNull(oneToOne.getCascade()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlColumnTests.java new file mode 100644 index 0000000000..582f503fb9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlColumnTests.java @@ -0,0 +1,698 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlBasicMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Basic; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlColumnTests extends ContextModelTestCase +{ + public XmlColumnTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedName()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlColumn.getSpecifiedName()); + assertEquals("FOO", basic.getColumn().getName()); + + //set name to null in the resource model + basic.getColumn().setName(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(basic.getColumn().getName()); + + basic.getColumn().setName("FOO"); + assertEquals("FOO", xmlColumn.getSpecifiedName()); + assertEquals("FOO", basic.getColumn().getName()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedName()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedName("foo"); + assertEquals("foo", xmlColumn.getSpecifiedName()); + assertEquals("foo", basic.getColumn().getName()); + + //set name to null in the context model + xmlColumn.setSpecifiedName(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(basic.getColumn()); + } + + public void testUpdateColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getColumnDefinition()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setColumnDefinition("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlColumn.getColumnDefinition()); + assertEquals("FOO", basic.getColumn().getColumnDefinition()); + + //set name to null in the resource model + basic.getColumn().setColumnDefinition(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(basic.getColumn().getColumnDefinition()); + + basic.getColumn().setColumnDefinition("FOO"); + assertEquals("FOO", xmlColumn.getColumnDefinition()); + assertEquals("FOO", basic.getColumn().getColumnDefinition()); + + basic.setColumn(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(basic.getColumn()); + } + + public void testModifyColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getColumnDefinition()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setColumnDefinition("foo"); + assertEquals("foo", xmlColumn.getColumnDefinition()); + assertEquals("foo", basic.getColumn().getColumnDefinition()); + + //set name to null in the context model + xmlColumn.setColumnDefinition(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(basic.getColumn()); + } +// public void testUpdateDefaultNameFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.getTable().setSpecifiedName("FOO"); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +// } + + public void testUpdateSpecifiedTable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setTable("FOO"); + assertEquals("FOO", xmlColumn.getSpecifiedTable()); + assertEquals("FOO", basic.getColumn().getTable()); + + //set name to null in the resource model + basic.getColumn().setTable(null); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(basic.getColumn().getTable()); + + basic.getColumn().setTable("FOO"); + assertEquals("FOO", xmlColumn.getSpecifiedTable()); + assertEquals("FOO", basic.getColumn().getTable()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedTable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedTable("foo"); + assertEquals("foo", xmlColumn.getSpecifiedTable()); + assertEquals("foo", basic.getColumn().getTable()); + + //set name to null in the context model + xmlColumn.setSpecifiedTable(null); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedNullable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setNullable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, basic.getColumn().getNullable()); + + //set name to null in the resource model + basic.getColumn().setNullable(null); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(basic.getColumn().getNullable()); + + basic.getColumn().setNullable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, basic.getColumn().getNullable()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedNullable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedNullable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, basic.getColumn().getNullable()); + + //set name to null in the context model + xmlColumn.setSpecifiedNullable(null); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedUpdatable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setUpdatable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.TRUE, basic.getColumn().getUpdatable()); + + //set name to null in the resource model + basic.getColumn().setUpdatable(null); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(basic.getColumn().getUpdatable()); + + basic.getColumn().setUpdatable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, basic.getColumn().getUpdatable()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedUpdatable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedUpdatable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, basic.getColumn().getUpdatable()); + + //set name to null in the context model + xmlColumn.setSpecifiedUpdatable(null); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedInsertable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setInsertable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, basic.getColumn().getInsertable()); + + //set name to null in the resource model + basic.getColumn().setInsertable(null); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(basic.getColumn().getInsertable()); + + basic.getColumn().setInsertable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, basic.getColumn().getInsertable()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedInsertable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedInsertable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, basic.getColumn().getInsertable()); + + //set name to null in the context model + xmlColumn.setSpecifiedInsertable(null); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedUnique() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setUnique(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, basic.getColumn().getUnique()); + + //set name to null in the resource model + basic.getColumn().setUnique(null); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(basic.getColumn().getUnique()); + + basic.getColumn().setUnique(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, basic.getColumn().getUnique()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedUnique() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedUnique(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, basic.getColumn().getUnique()); + + //set name to null in the context model + xmlColumn.setSpecifiedUnique(null); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedLength() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setLength(Integer.valueOf(8)); + assertEquals(Integer.valueOf(8), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(8), basic.getColumn().getLength()); + + //set name to null in the resource model + basic.getColumn().setLength(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn().getLength()); + + basic.getColumn().setLength(Integer.valueOf(11)); + assertEquals(Integer.valueOf(11), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(11), basic.getColumn().getLength()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedLength() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedLength(Integer.valueOf(7)); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(7), basic.getColumn().getLength()); + + //set name to null in the context model + xmlColumn.setSpecifiedLength(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedPrecision() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setPrecision(Integer.valueOf(8)); + assertEquals(Integer.valueOf(8), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(8), basic.getColumn().getPrecision()); + + //set name to null in the resource model + basic.getColumn().setPrecision(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn().getPrecision()); + + basic.getColumn().setPrecision(Integer.valueOf(11)); + assertEquals(Integer.valueOf(11), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(11), basic.getColumn().getPrecision()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedPrecision() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedPrecision(Integer.valueOf(7)); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(7), basic.getColumn().getPrecision()); + + //set name to null in the context model + xmlColumn.setSpecifiedPrecision(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } + + public void testUpdateSpecifiedScale() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the resource model, verify context model updated + basic.setColumn(OrmFactory.eINSTANCE.createColumnImpl()); + basic.getColumn().setScale(Integer.valueOf(8)); + assertEquals(Integer.valueOf(8), xmlColumn.getSpecifiedScale()); + assertEquals(Integer.valueOf(8), basic.getColumn().getScale()); + + //set name to null in the resource model + basic.getColumn().setScale(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn().getScale()); + + basic.getColumn().setScale(Integer.valueOf(11)); + assertEquals(Integer.valueOf(11), xmlColumn.getSpecifiedScale()); + assertEquals(Integer.valueOf(11), basic.getColumn().getScale()); + + basic.setColumn(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } + + public void testModifySpecifiedScale() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + XmlBasicMapping xmlBasicMapping = (XmlBasicMapping) xmlPersistentAttribute.getMapping(); + XmlColumn xmlColumn = xmlBasicMapping.getColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entityResource.getAttributes().getBasics().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedScale(Integer.valueOf(7)); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); + assertEquals(Integer.valueOf(7), basic.getColumn().getScale()); + + //set name to null in the context model + xmlColumn.setSpecifiedScale(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(basic.getColumn()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlDiscriminatorColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlDiscriminatorColumnTests.java new file mode 100644 index 0000000000..1c8df58470 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlDiscriminatorColumnTests.java @@ -0,0 +1,353 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.DiscriminatorType; +import org.eclipse.jpt.core.internal.context.orm.XmlDiscriminatorColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlDiscriminatorColumnTests extends ContextModelTestCase +{ + public XmlDiscriminatorColumnTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the resource model, verify context model updated + entityResource.setDiscriminatorColumn(OrmFactory.eINSTANCE.createDiscriminatorColumn()); + entityResource.getDiscriminatorColumn().setName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlColumn.getSpecifiedName()); + assertEquals("FOO", entityResource.getDiscriminatorColumn().getName()); + + //set name to null in the resource model + entityResource.getDiscriminatorColumn().setName(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn().getName()); + + entityResource.getDiscriminatorColumn().setName("FOO"); + assertEquals("FOO", xmlColumn.getSpecifiedName()); + assertEquals("FOO", entityResource.getDiscriminatorColumn().getName()); + + entityResource.setDiscriminatorColumn(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedName("foo"); + assertEquals("foo", xmlColumn.getSpecifiedName()); + assertEquals("foo", entityResource.getDiscriminatorColumn().getName()); + + //set name to null in the context model + xmlColumn.setSpecifiedName(null); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn()); + } + +// public void testUpdateDefaultNameFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.getTable().setSpecifiedName("FOO"); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +// } + + public void testUpdateSpecifiedLength() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the resource model, verify context model updated + entityResource.setDiscriminatorColumn(OrmFactory.eINSTANCE.createDiscriminatorColumn()); + entityResource.getDiscriminatorColumn().setLength(Integer.valueOf(8)); + assertEquals(Integer.valueOf(8), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(8), entityResource.getDiscriminatorColumn().getLength()); + + //set name to null in the resource model + entityResource.getDiscriminatorColumn().setLength(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(entityResource.getDiscriminatorColumn().getLength()); + + entityResource.getDiscriminatorColumn().setLength(Integer.valueOf(11)); + assertEquals(Integer.valueOf(11), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(11), entityResource.getDiscriminatorColumn().getLength()); + + entityResource.setDiscriminatorColumn(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testModifySpecifiedLength() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setSpecifiedLength(Integer.valueOf(7)); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(7), entityResource.getDiscriminatorColumn().getLength()); + + //set name to null in the context model + xmlColumn.setSpecifiedLength(null); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testUpdateSpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getColumnDefinition()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the resource model, verify context model updated + entityResource.setDiscriminatorColumn(OrmFactory.eINSTANCE.createDiscriminatorColumn()); + entityResource.getDiscriminatorColumn().setColumnDefinition("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlColumn.getColumnDefinition()); + assertEquals("FOO", entityResource.getDiscriminatorColumn().getColumnDefinition()); + + //set name to null in the resource model + entityResource.getDiscriminatorColumn().setColumnDefinition(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(entityResource.getDiscriminatorColumn().getColumnDefinition()); + + entityResource.getDiscriminatorColumn().setColumnDefinition("FOO"); + assertEquals("FOO", xmlColumn.getColumnDefinition()); + assertEquals("FOO", entityResource.getDiscriminatorColumn().getColumnDefinition()); + + entityResource.setDiscriminatorColumn(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testModifySpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getColumnDefinition()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set name in the context model, verify resource model modified + xmlColumn.setColumnDefinition("foo"); + assertEquals("foo", xmlColumn.getColumnDefinition()); + assertEquals("foo", entityResource.getDiscriminatorColumn().getColumnDefinition()); + + //set name to null in the context model + xmlColumn.setColumnDefinition(null); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testUpdateSpecifiedDiscriminatorType() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedDiscriminatorType()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set discriminator type in the resource model, verify context model updated + entityResource.setDiscriminatorColumn(OrmFactory.eINSTANCE.createDiscriminatorColumn()); + entityResource.getDiscriminatorColumn().setDiscriminatorType(org.eclipse.jpt.core.internal.resource.orm.DiscriminatorType.STRING); + ormResource().save(null); + assertEquals(DiscriminatorType.STRING, xmlColumn.getSpecifiedDiscriminatorType()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.DiscriminatorType.STRING, entityResource.getDiscriminatorColumn().getDiscriminatorType()); + + //set discriminator type to null in the resource model + entityResource.getDiscriminatorColumn().setDiscriminatorType(null); + assertNull(xmlColumn.getSpecifiedDiscriminatorType()); + assertNull(entityResource.getDiscriminatorColumn().getDiscriminatorType()); + + entityResource.getDiscriminatorColumn().setDiscriminatorType(org.eclipse.jpt.core.internal.resource.orm.DiscriminatorType.CHAR); + assertEquals(DiscriminatorType.CHAR, xmlColumn.getSpecifiedDiscriminatorType()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.DiscriminatorType.CHAR, entityResource.getDiscriminatorColumn().getDiscriminatorType()); + + entityResource.setDiscriminatorColumn(null); + assertNull(xmlColumn.getSpecifiedDiscriminatorType()); + assertNull(entityResource.getDiscriminatorColumn()); + } + + public void testModifySpecifiedDiscriminatorType() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlDiscriminatorColumn xmlColumn = xmlEntity.getDiscriminatorColumn(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlColumn.getSpecifiedDiscriminatorType()); + assertNull(entityResource.getDiscriminatorColumn()); + + //set discriminator type in the context model, verify resource model modified + xmlColumn.setSpecifiedDiscriminatorType(DiscriminatorType.STRING); + assertEquals(DiscriminatorType.STRING, xmlColumn.getSpecifiedDiscriminatorType()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.DiscriminatorType.STRING, entityResource.getDiscriminatorColumn().getDiscriminatorType()); + + //set discriminator type to null in the context model + xmlColumn.setSpecifiedDiscriminatorType(null); + assertNull(xmlColumn.getSpecifiedDiscriminatorType()); + assertNull(entityResource.getDiscriminatorColumn()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddableTests.java new file mode 100644 index 0000000000..31d133ec6f --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddableTests.java @@ -0,0 +1,305 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.orm.XmlEmbeddable; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlMappedSuperclass; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.Embeddable; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.MappedSuperclass; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlEmbeddableTests extends ContextModelTestCase +{ + public XmlEmbeddableTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals("model.Foo", embeddableResource.getClassName()); + + //set class in the resource model, verify context model updated + embeddableResource.setClassName("com.Bar"); + assertEquals("com.Bar", xmlEmbeddable.getClass_()); + assertEquals("com.Bar", embeddableResource.getClassName()); + + //set class to null in the resource model + embeddableResource.setClassName(null); + assertNull(xmlEmbeddable.getClass_()); + assertNull(embeddableResource.getClassName()); + } + + public void testModifyClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals("model.Foo", embeddableResource.getClassName()); + + //set class in the context model, verify resource model modified + xmlEmbeddable.setClass("com.Bar"); + assertEquals("com.Bar", xmlEmbeddable.getClass_()); + assertEquals("com.Bar", embeddableResource.getClassName()); + + //set class to null in the context model + xmlEmbeddable.setClass(null); + assertNull(xmlEmbeddable.getClass_()); + assertNull(embeddableResource.getClassName()); + } + //TODO add tests for setting the className when the package is set on entity-mappings + + public void testUpdateSpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(xmlEmbeddable.getSpecifiedAccess()); + assertNull(embeddableResource.getAccess()); + + //set access in the resource model, verify context model updated + embeddableResource.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertEquals(AccessType.FIELD, xmlEmbeddable.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, embeddableResource.getAccess()); + + //set access to null in the resource model + embeddableResource.setAccess(null); + assertNull(xmlEmbeddable.getSpecifiedAccess()); + assertNull(embeddableResource.getAccess()); + } + + public void testModifySpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(xmlEmbeddable.getSpecifiedAccess()); + assertNull(embeddableResource.getAccess()); + + //set access in the context model, verify resource model modified + xmlEmbeddable.setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, xmlEmbeddable.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, embeddableResource.getAccess()); + + //set access to null in the context model + xmlEmbeddable.setSpecifiedAccess(null); + assertNull(xmlEmbeddable.getSpecifiedAccess()); + assertNull(embeddableResource.getAccess()); + } + //TODO test default access from + //underlying java + //persistence-unit-defaults + //entity-mappings + //with xml-mapping-metadata-complete set + + public void testUpdateSpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(embeddableResource.getMetadataComplete()); + + //set metadata-complete in the resource model, verify context model updated + embeddableResource.setMetadataComplete(true); + assertTrue(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertTrue(embeddableResource.getMetadataComplete()); + + //set access to false in the resource model + embeddableResource.setMetadataComplete(false); + assertFalse(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertFalse(embeddableResource.getMetadataComplete()); + + embeddableResource.setMetadataComplete(null); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + } + + public void testModifySpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(embeddableResource.getMetadataComplete()); + + //set access in the context model, verify resource model modified + xmlEmbeddable.setSpecifiedMetadataComplete(Boolean.TRUE); + assertTrue(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertTrue(embeddableResource.getMetadataComplete()); + + //set access to null in the context model + xmlEmbeddable.setSpecifiedMetadataComplete(Boolean.FALSE); + assertFalse(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertFalse(embeddableResource.getMetadataComplete()); + + xmlEmbeddable.setSpecifiedMetadataComplete(null); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + } + + public void testUpdateDefaultMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertFalse(xmlEmbeddable.isDefaultMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlEmbeddable.isDefaultMetadataComplete()); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertFalse(xmlEmbeddable.isDefaultMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + } + + public void testUpdateMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) xmlPersistentType.getMapping(); + Embeddable embeddableResource = ormResource().getEntityMappings().getEmbeddables().get(0); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertFalse(xmlEmbeddable.isMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlEmbeddable.isMetadataComplete()); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertFalse(xmlEmbeddable.isMetadataComplete()); + assertNull(xmlEmbeddable.getSpecifiedMetadataComplete()); + assertNull(embeddableResource.getMetadataComplete()); + } + + public void testMakeEmbeddableEntity() throws Exception { + XmlPersistentType embeddablePersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable embeddable = (XmlEmbeddable) embeddablePersistentType.getMapping(); + embeddable.setSpecifiedAccess(AccessType.PROPERTY); + embeddable.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + embeddablePersistentType.setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", entity.getClassName()); + assertEquals(Boolean.TRUE, entity.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, entity.getAccess()); + assertNull(entity.getDiscriminatorValue()); + assertNull(entity.getName()); + + XmlEntity xmlEntity = (XmlEntity) embeddablePersistentType.getMapping(); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals(Boolean.TRUE, xmlEntity.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEntity.getSpecifiedAccess()); + } + + //test with 2 Embeddables, make the second one an Entity so it has to move to the front of the list + public void testMakeEmbeddableEntity2() throws Exception { + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo2"); + XmlPersistentType embeddablePersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable embeddable = (XmlEmbeddable) embeddablePersistentType.getMapping(); + embeddable.setSpecifiedAccess(AccessType.PROPERTY); + embeddable.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + embeddablePersistentType.setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", entity.getClassName()); + assertEquals(Boolean.TRUE, entity.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, entity.getAccess()); + assertNull(entity.getDiscriminatorValue()); + assertNull(entity.getName()); + + XmlEntity xmlEntity = (XmlEntity) embeddablePersistentType.getMapping(); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals(Boolean.TRUE, xmlEntity.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEntity.getSpecifiedAccess()); + + ListIterator<XmlPersistentType> persistentTypes = entityMappings().xmlPersistentTypes(); + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + } + + public void testMakeEmbeddableMappedSuperclass() throws Exception { + XmlPersistentType embeddablePersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable embeddable = (XmlEmbeddable) embeddablePersistentType.getMapping(); + embeddable.setSpecifiedAccess(AccessType.PROPERTY); + embeddable.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + embeddablePersistentType.setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + ormResource().save(null); + + MappedSuperclass mappedSuperclass = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", mappedSuperclass.getClassName()); + assertEquals(Boolean.TRUE, mappedSuperclass.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, mappedSuperclass.getAccess()); + + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) embeddablePersistentType.getMapping(); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals(Boolean.TRUE, xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlMappedSuperclass.getSpecifiedAccess()); + } + + //test with 2 Embeddables, make the second one a MappedSuperclass so it has to move to the front of the list + public void testMakeEmbeddableMappedSuperclass2() throws Exception { + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo2"); + XmlPersistentType embeddablePersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlEmbeddable embeddable = (XmlEmbeddable) embeddablePersistentType.getMapping(); + embeddable.setSpecifiedAccess(AccessType.PROPERTY); + embeddable.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + embeddablePersistentType.setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + ormResource().save(null); + + MappedSuperclass mappedSuperclass = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", mappedSuperclass.getClassName()); + assertEquals(Boolean.TRUE, mappedSuperclass.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, mappedSuperclass.getAccess()); + + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) embeddablePersistentType.getMapping(); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals(Boolean.TRUE, xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlMappedSuperclass.getSpecifiedAccess()); + + ListIterator<XmlPersistentType> persistentTypes = entityMappings().xmlPersistentTypes(); + assertEquals(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedIdMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedIdMappingTests.java new file mode 100644 index 0000000000..f7d52f9cb0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedIdMappingTests.java @@ -0,0 +1,231 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlAttributeOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlEmbeddedIdMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlEmbeddedIdMappingTests extends ContextModelTestCase +{ + public XmlEmbeddedIdMappingTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + assertEquals("embeddedIdMapping", xmlEmbeddedIdMapping.getName()); + assertEquals("embeddedIdMapping", embeddedIdResource.getName()); + + //set name in the resource model, verify context model updated + embeddedIdResource.setName("newName"); + assertEquals("newName", xmlEmbeddedIdMapping.getName()); + assertEquals("newName", embeddedIdResource.getName()); + + //set name to null in the resource model + embeddedIdResource.setName(null); + assertNull(xmlEmbeddedIdMapping.getName()); + assertNull(embeddedIdResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + assertEquals("embeddedIdMapping", xmlEmbeddedIdMapping.getName()); + assertEquals("embeddedIdMapping", embeddedIdResource.getName()); + + //set name in the context model, verify resource model updated + xmlEmbeddedIdMapping.setName("newName"); + assertEquals("newName", xmlEmbeddedIdMapping.getName()); + assertEquals("newName", embeddedIdResource.getName()); + + //set name to null in the context model + xmlEmbeddedIdMapping.setName(null); + assertNull(xmlEmbeddedIdMapping.getName()); + assertNull(embeddedIdResource.getName()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + XmlAttributeOverride attributeOverride = xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(0); + ormResource().save(null); + attributeOverride.setName("FOO"); + ormResource().save(null); + + assertEquals("FOO", embeddedIdResource.getAttributeOverrides().get(0).getName()); + + XmlAttributeOverride attributeOverride2 = xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(0); + ormResource().save(null); + attributeOverride2.setName("BAR"); + ormResource().save(null); + + assertEquals("BAR", embeddedIdResource.getAttributeOverrides().get(0).getName()); + assertEquals("FOO", embeddedIdResource.getAttributeOverrides().get(1).getName()); + + XmlAttributeOverride attributeOverride3 = xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(1); + ormResource().save(null); + attributeOverride3.setName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", embeddedIdResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedIdResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedIdResource.getAttributeOverrides().get(2).getName()); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals(attributeOverride2, attributeOverrides.next()); + assertEquals(attributeOverride3, attributeOverrides.next()); + assertEquals(attributeOverride, attributeOverrides.next()); + + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + assertEquals(3, embeddedIdResource.getAttributeOverrides().size()); + + xmlEmbeddedIdMapping.removeSpecifiedAttributeOverride(0); + assertEquals(2, embeddedIdResource.getAttributeOverrides().size()); + assertEquals("BAR", embeddedIdResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedIdResource.getAttributeOverrides().get(1).getName()); + + xmlEmbeddedIdMapping.removeSpecifiedAttributeOverride(0); + assertEquals(1, embeddedIdResource.getAttributeOverrides().size()); + assertEquals("BAZ", embeddedIdResource.getAttributeOverrides().get(0).getName()); + + xmlEmbeddedIdMapping.removeSpecifiedAttributeOverride(0); + assertEquals(0, embeddedIdResource.getAttributeOverrides().size()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEmbeddedIdMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + assertEquals(3, embeddedIdResource.getAttributeOverrides().size()); + + + xmlEmbeddedIdMapping.moveSpecifiedAttributeOverride(2, 0); + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAR", embeddedIdResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedIdResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedIdResource.getAttributeOverrides().get(2).getName()); + + + xmlEmbeddedIdMapping.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAZ", embeddedIdResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAR", embeddedIdResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedIdResource.getAttributeOverrides().get(2).getName()); + } + + public void testUpdateAttributeOverrides() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, "embeddedIdMapping"); + XmlEmbeddedIdMapping xmlEmbeddedIdMapping = (XmlEmbeddedIdMapping) xmlPersistentAttribute.getMapping(); + EmbeddedId embeddedIdResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddedIds().get(0); + + embeddedIdResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + embeddedIdResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + embeddedIdResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + + embeddedIdResource.getAttributeOverrides().get(0).setName("FOO"); + embeddedIdResource.getAttributeOverrides().get(1).setName("BAR"); + embeddedIdResource.getAttributeOverrides().get(2).setName("BAZ"); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedIdResource.getAttributeOverrides().move(2, 0); + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedIdResource.getAttributeOverrides().move(0, 1); + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedIdResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedIdResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEmbeddedIdMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedIdResource.getAttributeOverrides().remove(0); + assertFalse(xmlEmbeddedIdMapping.specifiedAttributeOverrides().hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedMappingTests.java new file mode 100644 index 0000000000..c570b84592 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEmbeddedMappingTests.java @@ -0,0 +1,502 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlAttributeOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlEmbeddedMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Embedded; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlEmbeddedMappingTests extends ContextModelTestCase +{ + private static final String ATTRIBUTE_OVERRIDE_NAME = "city"; + private static final String ATTRIBUTE_OVERRIDE_COLUMN_NAME = "E_CITY"; + + public XmlEmbeddedMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createEmbeddedAnnotation() throws Exception{ + this.createAnnotationAndMembers("Embedded", ""); + } + + private void createColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private void createAttributeOverrideAnnotation() throws Exception { + createColumnAnnotation(); + this.createAnnotationAndMembers("AttributeOverride", + "String name();" + + "Column column();"); + } + + private IType createTestEntityEmbeddedMapping() throws Exception { + createEntityAnnotation(); + createEmbeddedAnnotation(); + createAttributeOverrideAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @Embedded"); + sb.append(CR); + sb.append(" @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column=@Column(name=\"" + ATTRIBUTE_OVERRIDE_COLUMN_NAME + "\"))"); + sb.append(CR); + sb.append(" private Address address;").append(CR); + sb.append(CR); + sb.append(" @Id"); + } + }); + } + + private IType createTestEmbeddableAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.EMBEDDABLE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.COLUMN); + sb.append(";"); + sb.append(CR); + sb.append(CR); + sb.append("@Embeddable"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" @Column(name=\"A_STATE\")").append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + assertEquals("embeddedMapping", xmlEmbeddedMapping.getName()); + assertEquals("embeddedMapping", embeddedResource.getName()); + + //set name in the resource model, verify context model updated + embeddedResource.setName("newName"); + assertEquals("newName", xmlEmbeddedMapping.getName()); + assertEquals("newName", embeddedResource.getName()); + + //set name to null in the resource model + embeddedResource.setName(null); + assertNull(xmlEmbeddedMapping.getName()); + assertNull(embeddedResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + assertEquals("embeddedMapping", xmlEmbeddedMapping.getName()); + assertEquals("embeddedMapping", embeddedResource.getName()); + + //set name in the context model, verify resource model updated + xmlEmbeddedMapping.setName("newName"); + assertEquals("newName", xmlEmbeddedMapping.getName()); + assertEquals("newName", embeddedResource.getName()); + + //set name to null in the context model + xmlEmbeddedMapping.setName(null); + assertNull(xmlEmbeddedMapping.getName()); + assertNull(embeddedResource.getName()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + XmlAttributeOverride attributeOverride = xmlEmbeddedMapping.addSpecifiedAttributeOverride(0); + ormResource().save(null); + attributeOverride.setName("FOO"); + ormResource().save(null); + + assertEquals("FOO", embeddedResource.getAttributeOverrides().get(0).getName()); + + XmlAttributeOverride attributeOverride2 = xmlEmbeddedMapping.addSpecifiedAttributeOverride(0); + ormResource().save(null); + attributeOverride2.setName("BAR"); + ormResource().save(null); + + assertEquals("BAR", embeddedResource.getAttributeOverrides().get(0).getName()); + assertEquals("FOO", embeddedResource.getAttributeOverrides().get(1).getName()); + + XmlAttributeOverride attributeOverride3 = xmlEmbeddedMapping.addSpecifiedAttributeOverride(1); + ormResource().save(null); + attributeOverride3.setName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", embeddedResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedResource.getAttributeOverrides().get(2).getName()); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals(attributeOverride2, attributeOverrides.next()); + assertEquals(attributeOverride3, attributeOverrides.next()); + assertEquals(attributeOverride, attributeOverrides.next()); + + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + xmlEmbeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEmbeddedMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEmbeddedMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + assertEquals(3, embeddedResource.getAttributeOverrides().size()); + + xmlEmbeddedMapping.removeSpecifiedAttributeOverride(0); + assertEquals(2, embeddedResource.getAttributeOverrides().size()); + assertEquals("BAR", embeddedResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedResource.getAttributeOverrides().get(1).getName()); + + xmlEmbeddedMapping.removeSpecifiedAttributeOverride(0); + assertEquals(1, embeddedResource.getAttributeOverrides().size()); + assertEquals("BAZ", embeddedResource.getAttributeOverrides().get(0).getName()); + + xmlEmbeddedMapping.removeSpecifiedAttributeOverride(0); + assertEquals(0, embeddedResource.getAttributeOverrides().size()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + xmlEmbeddedMapping.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEmbeddedMapping.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEmbeddedMapping.addSpecifiedAttributeOverride(2).setName("BAZ"); + + assertEquals(3, embeddedResource.getAttributeOverrides().size()); + + + xmlEmbeddedMapping.moveSpecifiedAttributeOverride(2, 0); + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAR", embeddedResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", embeddedResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedResource.getAttributeOverrides().get(2).getName()); + + + xmlEmbeddedMapping.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAZ", embeddedResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAR", embeddedResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", embeddedResource.getAttributeOverrides().get(2).getName()); + } + + public void testUpdateAttributeOverrides() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedMapping"); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + Embedded embeddedResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getEmbeddeds().get(0); + + embeddedResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + embeddedResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + embeddedResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + + embeddedResource.getAttributeOverrides().get(0).setName("FOO"); + embeddedResource.getAttributeOverrides().get(1).setName("BAR"); + embeddedResource.getAttributeOverrides().get(2).setName("BAZ"); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedResource.getAttributeOverrides().move(2, 0); + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedResource.getAttributeOverrides().move(0, 1); + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + embeddedResource.getAttributeOverrides().remove(0); + assertFalse(xmlEmbeddedMapping.specifiedAttributeOverrides().hasNext()); + } + + + public void testEmbeddedMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityEmbeddedMapping(); + createTestEmbeddableAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlEmbeddedMapping.getName()); + + + assertFalse(xmlEmbeddedMapping.specifiedAttributeOverrides().hasNext()); + assertFalse(xmlEmbeddedMapping.defaultAttributeOverrides().hasNext()); + } + + + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityEmbeddedMapping(); + createTestEmbeddableAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlEmbeddedMapping.getName()); + + assertEquals(4, xmlEmbeddedMapping.specifiedAttributeOverridesSize()); + assertEquals(0, CollectionTools.size(xmlEmbeddedMapping.defaultAttributeOverrides())); + ListIterator<XmlAttributeOverride> xmlAttributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); + + XmlAttributeOverride xmlAttributeOverride = xmlAttributeOverrides.next(); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, xmlAttributeOverride.getName()); + XmlColumn xmlColumn = xmlAttributeOverride.getColumn(); + assertEquals(ATTRIBUTE_OVERRIDE_COLUMN_NAME, xmlColumn.getSpecifiedName()); +// assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); +// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); +// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); +// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); +// assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); +// assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); +// assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); +// assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); +// assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); + + xmlAttributeOverride = xmlAttributeOverrides.next(); + assertEquals("id", xmlAttributeOverride.getName()); + xmlColumn = xmlAttributeOverride.getColumn(); + assertEquals("id", xmlColumn.getSpecifiedName()); + + xmlAttributeOverride = xmlAttributeOverrides.next(); + assertEquals("state", xmlAttributeOverride.getName()); + xmlColumn = xmlAttributeOverride.getColumn(); + assertEquals("A_STATE", xmlColumn.getSpecifiedName()); + + xmlAttributeOverride = xmlAttributeOverrides.next(); + assertEquals("zip", xmlAttributeOverride.getName()); + xmlColumn = xmlAttributeOverride.getColumn(); + assertEquals("zip", xmlColumn.getSpecifiedName()); + + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityEmbeddedMapping(); + createTestEmbeddableAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlEmbeddedMapping.getName()); + + //TODO +// assertEquals(4, xmlEmbeddedMapping.specifiedAttributeOverridesSize()); +// assertEquals(0, CollectionTools.size(xmlEmbeddedMapping.defaultAttributeOverrides())); +// ListIterator<XmlAttributeOverride> xmlAttributeOverrides = xmlEmbeddedMapping.specifiedAttributeOverrides(); +// +// XmlAttributeOverride xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals(ATTRIBUTE_OVERRIDE_NAME, xmlAttributeOverride.getName()); +// XmlColumn xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("city", xmlColumn.getSpecifiedName()); +//// assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); +//// assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); +//// assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); +//// assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); +//// assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); +//// assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("id", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("id", xmlColumn.getSpecifiedName()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("state", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("state", xmlColumn.getSpecifiedName()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("zip", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("zip", xmlColumn.getSpecifiedName()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityEmbeddedMapping(); + createTestEmbeddableAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "address"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlEmbeddedMapping xmlEmbeddedMapping = (XmlEmbeddedMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("address", xmlEmbeddedMapping.getName()); + + assertEquals(0, xmlEmbeddedMapping.specifiedAttributeOverridesSize()); + //TODO +// assertEquals(4, CollectionTools.size(xmlEmbeddedMapping.defaultAttributeOverrides())); +// ListIterator<XmlAttributeOverride> xmlAttributeOverrides = xmlEmbeddedMapping.defaultAttributeOverrides(); +// +// XmlAttributeOverride xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals(ATTRIBUTE_OVERRIDE_NAME, xmlAttributeOverride.getName()); +// XmlColumn xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("city", xmlColumn.getDefaultName()); +//// assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); +//// assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); +//// assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); +//// assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); +//// assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); +//// assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); +//// assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("id", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("id", xmlColumn.getDefaultName()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("state", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("state", xmlColumn.getDefaultName()); +// +// xmlAttributeOverride = xmlAttributeOverrides.next(); +// assertEquals("zip", xmlAttributeOverride.getName()); +// xmlColumn = xmlAttributeOverride.getColumn(); +// assertEquals("zip", xmlColumn.getDefaultName()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEntityTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEntityTests.java new file mode 100644 index 0000000000..d385300979 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlEntityTests.java @@ -0,0 +1,1901 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.InheritanceType; +import org.eclipse.jpt.core.internal.context.orm.XmlAssociationOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlAttributeOverride; +import org.eclipse.jpt.core.internal.context.orm.XmlEmbeddable; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlMappedSuperclass; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedNativeQuery; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedQuery; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlSecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Embeddable; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.MappedSuperclass; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlEntityTests extends ContextModelTestCase +{ + public XmlEntityTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntityDefaultFieldAccess() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityFieldAccess() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityPropertyAccess() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedName()); + assertNull(entityResource.getName()); + + //set name in the resource model, verify context model updated + entityResource.setName("foo"); + assertEquals("foo", xmlEntity.getSpecifiedName()); + assertEquals("foo", entityResource.getName()); + + //set name to null in the resource model + entityResource.setName(null); + assertNull(xmlEntity.getSpecifiedName()); + assertNull(entityResource.getName()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedName()); + assertNull(entityResource.getName()); + + //set name in the context model, verify resource model modified + xmlEntity.setSpecifiedName("foo"); + assertEquals("foo", xmlEntity.getSpecifiedName()); + assertEquals("foo", entityResource.getName()); + + //set name to null in the context model + xmlEntity.setSpecifiedName(null); + assertNull(xmlEntity.getSpecifiedName()); + assertNull(entityResource.getName()); + } + + public void testUpdateDefaultName() throws Exception { + createTestEntityFieldAccess(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(TYPE_NAME, xmlEntity.getDefaultName()); + + xmlEntity.javaEntity().setSpecifiedName("Foo"); + //xml default name is not affect by what is specified in java + assertEquals(TYPE_NAME, xmlEntity.getDefaultName()); + + //set class in the resource model, verify context model updated + entityResource.setClassName("com.Bar"); + assertEquals("Bar", xmlEntity.getDefaultName()); + + //set class to null in the resource model + entityResource.setClassName(null); + assertNull(xmlEntity.getDefaultName()); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("Foo", xmlEntity.getName()); + + //set class in the resource model, verify context model updated + entityResource.setClassName("com.Bar"); + assertEquals("Bar", xmlEntity.getName()); + + entityResource.setName("Baz"); + assertEquals("Baz", xmlEntity.getName()); + + //set class to null in the resource model + entityResource.setClassName(null); + assertEquals("Baz", xmlEntity.getName()); + + entityResource.setName(null); + assertNull(xmlEntity.getName()); + } + + public void testUpdateClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals("model.Foo", entityResource.getClassName()); + + //set class in the resource model, verify context model updated + entityResource.setClassName("com.Bar"); + assertEquals("com.Bar", xmlEntity.getClass_()); + assertEquals("com.Bar", entityResource.getClassName()); + + //set class to null in the resource model + entityResource.setClassName(null); + assertNull(xmlEntity.getClass_()); + assertNull(entityResource.getClassName()); + } + + public void testModifyClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals("model.Foo", entityResource.getClassName()); + + //set class in the context model, verify resource model modified + xmlEntity.setClass("com.Bar"); + assertEquals("com.Bar", xmlEntity.getClass_()); + assertEquals("com.Bar", entityResource.getClassName()); + + //set class to null in the context model + xmlEntity.setClass(null); + assertNull(xmlEntity.getClass_()); + assertNull(entityResource.getClassName()); + } + //TODO add tests for setting the className when the package is set on entity-mappings + + public void testUpdateSpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + + //set access in the resource model, verify context model updated + entityResource.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + ormResource().save(null); + assertEquals(AccessType.FIELD, xmlEntity.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, entityResource.getAccess()); + + //set access to null in the resource model + entityResource.setAccess(null); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + } + + public void testModifySpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + + //set access in the context model, verify resource model modified + xmlEntity.setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, xmlEntity.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, entityResource.getAccess()); + + //set access to null in the context model + xmlEntity.setSpecifiedAccess(null); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + } + + public void testUpdateDefaultAccessFromPersistenceUnitDefaults() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(xmlEntity.getDefaultAccess()); + assertNull(entityResource.getAccess()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setPersistenceUnitDefaults(OrmFactory.eINSTANCE.createPersistenceUnitDefaults()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(entityResource.getAccess()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(null); + assertNull(xmlEntity.getSpecifiedAccess()); + assertNull(xmlEntity.getDefaultAccess()); + assertNull(entityResource.getAccess()); + } + + public void testUpdateDefaultAccessFromJava() throws Exception { + createTestEntityDefaultFieldAccess(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + //java has no annotations, so defaultAccess in xml is null + assertNull(xmlEntity.getDefaultAccess()); + + entityMappings().setSpecifiedAccess(AccessType.FIELD); + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + //entityMappings access wins over persistence-unit-defaults access + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + + entityMappings().setSpecifiedAccess(null); + //persistence-unit-defaults access used now + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(null); + assertNull(xmlEntity.getDefaultAccess()); + + xmlPersistentType.javaPersistentType().attributeNamed("id").setSpecifiedMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY); + //java has annotations on fields now, that should win in all cases + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + + entityMappings().setSpecifiedAccess(AccessType.PROPERTY); + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + + xmlPersistentType.javaPersistentType().attributeNamed("id").setSpecifiedMappingKey(IMappingKeys.NULL_ATTRIBUTE_MAPPING_KEY); + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + } + + public void testUpdateDefaultAccessFromJavaFieldAccess() throws Exception { + createTestEntityFieldAccess(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertNull(xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertNull(xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertNull(xmlEntity.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertEquals(AccessType.FIELD, xmlEntity.getDefaultAccess()); + } + + public void testUpdateDefaultAccessFromJavaPropertyAccess() throws Exception { + createTestEntityPropertyAccess(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertNull(xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertNull(xmlEntity.getDefaultAccess()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertNull(xmlEntity.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertEquals(AccessType.PROPERTY, xmlEntity.getDefaultAccess()); + } + + public void testUpdateDefaultAccessNoUnderlyingJava() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getDefaultAccess()); + } + + public void testUpdateSpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + //set metadata-complete in the resource model, verify context model updated + entityResource.setMetadataComplete(true); + ormResource().save(null); + assertTrue(xmlEntity.getSpecifiedMetadataComplete()); + assertTrue(entityResource.getMetadataComplete()); + + //set access to false in the resource model + entityResource.setMetadataComplete(false); + ormResource().save(null); + assertFalse(xmlEntity.getSpecifiedMetadataComplete()); + assertFalse(entityResource.getMetadataComplete()); + + entityResource.setMetadataComplete(null); + ormResource().save(null); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + } + + public void testModifySpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + //set access in the context model, verify resource model modified + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertTrue(xmlEntity.getSpecifiedMetadataComplete()); + assertTrue(entityResource.getMetadataComplete()); + + //set access to null in the context model + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertFalse(xmlEntity.getSpecifiedMetadataComplete()); + assertFalse(entityResource.getMetadataComplete()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + } + + public void testUpdateDefaultMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertFalse(xmlEntity.isDefaultMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlEntity.isDefaultMetadataComplete()); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertFalse(xmlEntity.isDefaultMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + xmlEntity.setSpecifiedMetadataComplete(false); + assertFalse(xmlEntity.getSpecifiedMetadataComplete()); + assertTrue(xmlEntity.isDefaultMetadataComplete()); + assertTrue(xmlEntity.isMetadataComplete()); + } + + public void testUpdateMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertFalse(xmlEntity.isMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlEntity.isMetadataComplete()); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertFalse(xmlEntity.isMetadataComplete()); + assertNull(xmlEntity.getSpecifiedMetadataComplete()); + assertNull(entityResource.getMetadataComplete()); + } + + + public void testUpdateInheritanceStrategy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getInheritanceStrategy()); + assertNull(entityResource.getInheritance()); + + //set inheritance strategy in the resource model, verify context model updated + entityResource.setInheritance(OrmFactory.eINSTANCE.createInheritance()); + entityResource.getInheritance().setStrategy(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.TABLE_PER_CLASS); + + assertEquals(InheritanceType.TABLE_PER_CLASS, xmlEntity.getInheritanceStrategy()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.TABLE_PER_CLASS, entityResource.getInheritance().getStrategy()); + } + + public void testUpdateDefaultInheritanceStrategyFromJava() throws Exception { + createTestEntityDefaultFieldAccess(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + //no inheritance strategy specified in java so single-table is default + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.javaEntity().setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + assertEquals(InheritanceType.JOINED, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.TABLE_PER_CLASS); + ormResource().save(null); + //inheritance tag exists in xml, so it overrides anything in java + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.setSpecifiedInheritanceStrategy(null); + ormResource().save(null); + assertEquals(InheritanceType.JOINED, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertEquals(InheritanceType.JOINED, xmlEntity.getDefaultInheritanceStrategy()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + //this setting overrides the false meta-data complete found on xmlEntity + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getDefaultInheritanceStrategy()); + } + + public void testUpdateDefaultInheritanceStrategyFromParent() throws Exception { + createTestEntityDefaultFieldAccess(); + createTestSubType(); + + XmlPersistentType parentPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + XmlEntity parentXmlEntity = (XmlEntity) parentPersistentType.getMapping(); + XmlEntity childXmlEntity = (XmlEntity) childPersistentType.getMapping(); + + assertEquals(parentXmlEntity, childXmlEntity.parentEntity()); + assertEquals(InheritanceType.SINGLE_TABLE, childXmlEntity.getDefaultInheritanceStrategy()); + + //change root inheritance strategy, verify default is changed for child entity + parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.TABLE_PER_CLASS); + assertEquals(InheritanceType.SINGLE_TABLE, parentXmlEntity.getDefaultInheritanceStrategy()); + assertEquals(InheritanceType.TABLE_PER_CLASS, childXmlEntity.getDefaultInheritanceStrategy()); + assertNull(childXmlEntity.getSpecifiedInheritanceStrategy()); + + //set specified inheritance strategy in java and verify defaults in xml are correct + parentXmlEntity.setSpecifiedInheritanceStrategy(null); + parentXmlEntity.javaEntity().setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + assertEquals(InheritanceType.JOINED, parentXmlEntity.getDefaultInheritanceStrategy()); + assertEquals(InheritanceType.JOINED, childXmlEntity.getDefaultInheritanceStrategy()); + assertNull(parentXmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(childXmlEntity.getSpecifiedInheritanceStrategy()); + + parentPersistentType.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertEquals(InheritanceType.SINGLE_TABLE, parentXmlEntity.getDefaultInheritanceStrategy()); + assertEquals(InheritanceType.SINGLE_TABLE, childXmlEntity.getDefaultInheritanceStrategy()); + } + + public void testUpdateSpecifiedInheritanceStrategy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(entityResource.getInheritance()); + + //set strategy in the resource model, verify context model updated + entityResource.setInheritance(OrmFactory.eINSTANCE.createInheritance()); + entityResource.getInheritance().setStrategy(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.JOINED); + assertEquals(InheritanceType.JOINED, xmlEntity.getSpecifiedInheritanceStrategy()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.JOINED, entityResource.getInheritance().getStrategy()); + + //set strategy to null in the resource model + entityResource.getInheritance().setStrategy(null); + assertNull(xmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(entityResource.getInheritance().getStrategy()); + + entityResource.getInheritance().setStrategy(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.SINGLE_TABLE); + assertEquals(InheritanceType.SINGLE_TABLE, xmlEntity.getSpecifiedInheritanceStrategy()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.SINGLE_TABLE, entityResource.getInheritance().getStrategy()); + + entityResource.setInheritance(null); + assertNull(xmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(entityResource.getInheritance()); + } + + public void testModifySpecifiedInheritanceStrategy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(entityResource.getInheritance()); + + //set strategy in the context model, verify resource model modified + xmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.TABLE_PER_CLASS); + assertEquals(InheritanceType.TABLE_PER_CLASS, xmlEntity.getSpecifiedInheritanceStrategy()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.InheritanceType.TABLE_PER_CLASS, entityResource.getInheritance().getStrategy()); + + //set strategy to null in the context model + xmlEntity.setSpecifiedInheritanceStrategy(null); + assertNull(xmlEntity.getSpecifiedInheritanceStrategy()); + assertNull(entityResource.getInheritance()); + } + + public void testAddSpecifiedSecondaryTable() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + ormResource().save(null); + secondaryTable.setSpecifiedName("FOO"); + ormResource().save(null); + + assertEquals("FOO", entityResource.getSecondaryTables().get(0).getName()); + + XmlSecondaryTable secondaryTable2 = xmlEntity.addSpecifiedSecondaryTable(0); + ormResource().save(null); + secondaryTable2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", entityResource.getSecondaryTables().get(0).getName()); + assertEquals("FOO", entityResource.getSecondaryTables().get(1).getName()); + + XmlSecondaryTable secondaryTable3 = xmlEntity.addSpecifiedSecondaryTable(1); + ormResource().save(null); + secondaryTable3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", entityResource.getSecondaryTables().get(0).getName()); + assertEquals("BAZ", entityResource.getSecondaryTables().get(1).getName()); + assertEquals("FOO", entityResource.getSecondaryTables().get(2).getName()); + + ListIterator<XmlSecondaryTable> secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals(secondaryTable2, secondaryTables.next()); + assertEquals(secondaryTable3, secondaryTables.next()); + assertEquals(secondaryTable, secondaryTables.next()); + + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + } + + public void testRemoveSpecifiedSecondaryTable() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + xmlEntity.addSpecifiedSecondaryTable(1).setSpecifiedName("BAR"); + xmlEntity.addSpecifiedSecondaryTable(2).setSpecifiedName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getSecondaryTables().size()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertEquals(2, entityResource.getSecondaryTables().size()); + assertEquals("BAR", entityResource.getSecondaryTables().get(0).getName()); + assertEquals("BAZ", entityResource.getSecondaryTables().get(1).getName()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertEquals(1, entityResource.getSecondaryTables().size()); + assertEquals("BAZ", entityResource.getSecondaryTables().get(0).getName()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testMoveSpecifiedSecondaryTable() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + xmlEntity.addSpecifiedSecondaryTable(1).setSpecifiedName("BAR"); + xmlEntity.addSpecifiedSecondaryTable(2).setSpecifiedName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getSecondaryTables().size()); + + + xmlEntity.moveSpecifiedSecondaryTable(2, 0); + ListIterator<XmlSecondaryTable> secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + + assertEquals("BAR", entityResource.getSecondaryTables().get(0).getName()); + assertEquals("BAZ", entityResource.getSecondaryTables().get(1).getName()); + assertEquals("FOO", entityResource.getSecondaryTables().get(2).getName()); + + + xmlEntity.moveSpecifiedSecondaryTable(0, 1); + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + + assertEquals("BAZ", entityResource.getSecondaryTables().get(0).getName()); + assertEquals("BAR", entityResource.getSecondaryTables().get(1).getName()); + assertEquals("FOO", entityResource.getSecondaryTables().get(2).getName()); + } + + public void testUpdateSecondaryTables() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + + entityResource.getSecondaryTables().get(0).setName("FOO"); + entityResource.getSecondaryTables().get(1).setName("BAR"); + entityResource.getSecondaryTables().get(2).setName("BAZ"); + + ListIterator<XmlSecondaryTable> secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + entityResource.getSecondaryTables().move(2, 0); + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + entityResource.getSecondaryTables().move(0, 1); + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + entityResource.getSecondaryTables().remove(1); + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + entityResource.getSecondaryTables().remove(1); + secondaryTables = xmlEntity.specifiedSecondaryTables(); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + + entityResource.getSecondaryTables().remove(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + } + + //test adding 2 secondary tables to java entity + //override one in xmlEntity, verify other one still exists as a default + //change xml-mapping-metadata complete setting in both locations and verify defaults from java are gone + public void testDefaultSecondaryTables() throws Exception { + createTestEntityDefaultFieldAccess(); + createTestSubType(); + + XmlPersistentType parentPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + XmlEntity parentXmlEntity = (XmlEntity) parentPersistentType.getMapping(); + XmlEntity childXmlEntity = (XmlEntity) childPersistentType.getMapping(); + + childXmlEntity.javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + + assertEquals("FOO", childXmlEntity.virtualSecondaryTables().next().getName()); + assertEquals("FOO", childXmlEntity.secondaryTables().next().getName()); + assertEquals(0, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(1, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(1, childXmlEntity.secondaryTablesSize()); + + childXmlEntity.javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("BAR"); + ListIterator<XmlSecondaryTable> virtualSecondaryTables = childXmlEntity.virtualSecondaryTables(); + ListIterator<XmlSecondaryTable> secondaryTables = childXmlEntity.secondaryTables(); + assertEquals("BAR", virtualSecondaryTables.next().getName()); + assertEquals("FOO", virtualSecondaryTables.next().getName()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals(0, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(2, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(2, childXmlEntity.secondaryTablesSize()); + + childXmlEntity.addSpecifiedSecondaryTable(0).setSpecifiedName("BAZ"); + virtualSecondaryTables = childXmlEntity.virtualSecondaryTables(); + secondaryTables = childXmlEntity.secondaryTables(); + assertFalse(virtualSecondaryTables.hasNext()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertEquals(1, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(0, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(1, childXmlEntity.secondaryTablesSize()); + + childXmlEntity.addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + virtualSecondaryTables = childXmlEntity.virtualSecondaryTables(); + secondaryTables = childXmlEntity.secondaryTables(); + assertFalse(virtualSecondaryTables.hasNext()); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + assertEquals(2, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(0, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(2, childXmlEntity.secondaryTablesSize()); + + //add a specified secondary table to the parent, this will not affect virtual secondaryTables in child + parentXmlEntity.addSpecifiedSecondaryTable(0).setSpecifiedName("PARENT_TABLE"); + virtualSecondaryTables = childXmlEntity.virtualSecondaryTables(); + secondaryTables = childXmlEntity.secondaryTables(); + assertFalse(virtualSecondaryTables.hasNext()); + assertEquals("FOO", secondaryTables.next().getName()); + assertEquals("BAZ", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + assertEquals(2, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(0, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(2, childXmlEntity.secondaryTablesSize()); + + childXmlEntity.removeSpecifiedSecondaryTable(0); + childXmlEntity.removeSpecifiedSecondaryTable(0); + virtualSecondaryTables = childXmlEntity.virtualSecondaryTables(); + secondaryTables = childXmlEntity.secondaryTables(); + assertEquals("BAR", virtualSecondaryTables.next().getName()); + assertEquals("FOO", virtualSecondaryTables.next().getName()); + assertFalse(virtualSecondaryTables.hasNext()); + assertEquals("BAR", secondaryTables.next().getName()); + assertEquals("FOO", secondaryTables.next().getName()); + assertFalse(secondaryTables.hasNext()); + assertEquals(0, childXmlEntity.specifiedSecondaryTablesSize()); + assertEquals(2, childXmlEntity.virtualSecondaryTablesSize()); + assertEquals(2, childXmlEntity.secondaryTablesSize()); + } + + //test that inherited tables don't show up in this list + public void testAssociatedTables() throws Exception { + + } + + public void testAssociatedTableNamesIncludingInherited() throws Exception { + + } + + public void testTableNameIsInvalid() throws Exception { + + } + + public void testMakeEntityEmbeddable() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity entity = (XmlEntity) entityPersistentType.getMapping(); + entity.setSpecifiedAccess(AccessType.PROPERTY); + entity.setSpecifiedDiscriminatorValue("DISC_VALUE"); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedMetadataComplete(Boolean.TRUE); + entity.setSpecifiedName("ENTITY_NAME"); + ormResource().save(null); + + entityPersistentType.setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + ormResource().save(null); + + Embeddable embeddable = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", embeddable.getClassName()); + assertEquals(Boolean.TRUE, embeddable.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, embeddable.getAccess()); + + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) entityPersistentType.getMapping(); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals(Boolean.TRUE, xmlEmbeddable.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEmbeddable.getSpecifiedAccess()); + } + + //TODO test that attribute mappings are not removed when changing type mapping. + public void testMakeEntityEmbeddable2() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + XmlEntity entity = (XmlEntity) entityPersistentType.getMapping(); + entity.setSpecifiedAccess(AccessType.PROPERTY); + entity.setSpecifiedDiscriminatorValue("DISC_VALUE"); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedMetadataComplete(Boolean.TRUE); + entity.setSpecifiedName("ENTITY_NAME"); +// entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicMapping"); + ormResource().save(null); + + entityPersistentType.setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + ormResource().save(null); + + Embeddable embeddable = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", embeddable.getClassName()); + assertEquals(Boolean.TRUE, embeddable.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, embeddable.getAccess()); +// assertEquals("basicMapping", embeddable.getAttributes().getBasics().get(0).getName()); + + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) entityPersistentType.getMapping(); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals(Boolean.TRUE, xmlEmbeddable.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEmbeddable.getSpecifiedAccess()); +// assertEquals("basicMapping", xmlEmbeddable.persistentType().attributes().next().getName()); + } + + public void testMakeEntityMappedSuperclass() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity entity = (XmlEntity) entityPersistentType.getMapping(); + entity.setSpecifiedAccess(AccessType.PROPERTY); + entity.setSpecifiedDiscriminatorValue("DISC_VALUE"); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedMetadataComplete(Boolean.TRUE); + entity.setSpecifiedName("ENTITY_NAME"); + ormResource().save(null); + + entityPersistentType.setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + ormResource().save(null); + + MappedSuperclass mappedSuperclass = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", mappedSuperclass.getClassName()); + assertEquals(Boolean.TRUE, mappedSuperclass.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, mappedSuperclass.getAccess()); + + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) entityPersistentType.getMapping(); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals(Boolean.TRUE, xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlMappedSuperclass.getSpecifiedAccess()); + } + + public void testMakeEntityMappedSuperclass2() throws Exception { + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity entity = (XmlEntity) entityPersistentType.getMapping(); + entity.setSpecifiedAccess(AccessType.PROPERTY); + entity.setSpecifiedDiscriminatorValue("DISC_VALUE"); + entity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + entity.setSpecifiedMetadataComplete(Boolean.TRUE); + entity.setSpecifiedName("ENTITY_NAME"); + ormResource().save(null); + + entityPersistentType.setMappingKey(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY); + ormResource().save(null); + + MappedSuperclass mappedSuperclass = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", mappedSuperclass.getClassName()); + assertEquals(Boolean.TRUE, mappedSuperclass.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, mappedSuperclass.getAccess()); + + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) entityPersistentType.getMapping(); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals(Boolean.TRUE, xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlMappedSuperclass.getSpecifiedAccess()); + } + + + public void testAddSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getSequenceGenerator()); + assertNull(entityResource.getSequenceGenerator()); + + xmlEntity.addSequenceGenerator(); + + assertNotNull(entityResource.getSequenceGenerator()); + assertNotNull(xmlEntity.getSequenceGenerator()); + + //try adding another sequence generator, should get an IllegalStateException + try { + xmlEntity.addSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getSequenceGenerator()); + assertNull(entityResource.getSequenceGenerator()); + + xmlEntity.addSequenceGenerator(); + assertNotNull(entityResource.getSequenceGenerator()); + assertNotNull(xmlEntity.getSequenceGenerator()); + + xmlEntity.removeSequenceGenerator(); + + assertNull(xmlEntity.getSequenceGenerator()); + assertNull(entityResource.getSequenceGenerator()); + + //try removing the sequence generator again, should get an IllegalStateException + try { + xmlEntity.removeSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testUpdateSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getSequenceGenerator()); + assertNull(entityResource.getSequenceGenerator()); + + entityResource.setSequenceGenerator(OrmFactory.eINSTANCE.createSequenceGeneratorImpl()); + + assertNotNull(xmlEntity.getSequenceGenerator()); + assertNotNull(entityResource.getSequenceGenerator()); + + entityResource.setSequenceGenerator(null); + assertNull(xmlEntity.getSequenceGenerator()); + assertNull(entityResource.getSequenceGenerator()); + } + + public void testAddTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getTableGenerator()); + assertNull(entityResource.getTableGenerator()); + + xmlEntity.addTableGenerator(); + + assertNotNull(entityResource.getTableGenerator()); + assertNotNull(xmlEntity.getTableGenerator()); + + //try adding another table generator, should get an IllegalStateException + try { + xmlEntity.addTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getTableGenerator()); + assertNull(entityResource.getTableGenerator()); + + xmlEntity.addTableGenerator(); + assertNotNull(entityResource.getTableGenerator()); + assertNotNull(xmlEntity.getTableGenerator()); + + xmlEntity.removeTableGenerator(); + + assertNull(xmlEntity.getTableGenerator()); + assertNull(entityResource.getTableGenerator()); + + //try removing the table generator again, should get an IllegalStateException + try { + xmlEntity.removeTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testUpdateTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getTableGenerator()); + assertNull(entityResource.getTableGenerator()); + + entityResource.setTableGenerator(OrmFactory.eINSTANCE.createTableGeneratorImpl()); + + assertNotNull(xmlEntity.getTableGenerator()); + assertNotNull(entityResource.getTableGenerator()); + + entityResource.setTableGenerator(null); + assertNull(xmlEntity.getTableGenerator()); + assertNull(entityResource.getTableGenerator()); + } + + public void testUpdateDiscriminatorColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNotNull(xmlEntity.getDiscriminatorColumn()); + + entityResource.setDiscriminatorColumn(OrmFactory.eINSTANCE.createDiscriminatorColumn()); + entityResource.getDiscriminatorColumn().setName("FOO"); + + assertEquals("FOO", xmlEntity.getDiscriminatorColumn().getSpecifiedName()); + assertEquals("FOO", entityResource.getDiscriminatorColumn().getName()); + + entityResource.getDiscriminatorColumn().setName(null); + + assertNull(xmlEntity.getDiscriminatorColumn().getSpecifiedName()); + assertNull(entityResource.getDiscriminatorColumn().getName()); + + entityResource.setDiscriminatorColumn(null); + + assertNotNull(xmlEntity.getDiscriminatorColumn()); + } + + public void testUpdateDiscriminatorValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getSpecifiedDiscriminatorValue()); + assertNull(entityResource.getDiscriminatorValue()); + + entityResource.setDiscriminatorValue("FOO"); + + assertEquals("FOO", xmlEntity.getSpecifiedDiscriminatorValue()); + assertEquals("FOO", entityResource.getDiscriminatorValue()); + + entityResource.setDiscriminatorValue(null); + + assertNull(xmlEntity.getSpecifiedDiscriminatorValue()); + assertNull(entityResource.getDiscriminatorValue()); + } + + public void testModifyDiscriminatorValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getSpecifiedDiscriminatorValue()); + assertNull(entityResource.getDiscriminatorValue()); + + xmlEntity.setSpecifiedDiscriminatorValue("FOO"); + + assertEquals("FOO", xmlEntity.getSpecifiedDiscriminatorValue()); + assertEquals("FOO", entityResource.getDiscriminatorValue()); + + xmlEntity.setSpecifiedDiscriminatorValue(null); + + assertNull(xmlEntity.getSpecifiedDiscriminatorValue()); + assertNull(entityResource.getDiscriminatorValue()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + primaryKeyJoinColumn.setSpecifiedName("FOO"); + + assertEquals("FOO", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn2 = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + primaryKeyJoinColumn2.setSpecifiedName("BAR"); + + assertEquals("BAR", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("FOO", entityResource.getPrimaryKeyJoinColumns().get(1).getName()); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn3 = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(1); + primaryKeyJoinColumn3.setSpecifiedName("BAZ"); + + assertEquals("BAR", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", entityResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", entityResource.getPrimaryKeyJoinColumns().get(2).getName()); + + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals(primaryKeyJoinColumn2, primaryKeyJoinColumns.next()); + assertEquals(primaryKeyJoinColumn3, primaryKeyJoinColumns.next()); + assertEquals(primaryKeyJoinColumn, primaryKeyJoinColumns.next()); + + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + } + + public void testRemoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getPrimaryKeyJoinColumns().size()); + + xmlEntity.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(2, entityResource.getPrimaryKeyJoinColumns().size()); + assertEquals("BAR", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", entityResource.getPrimaryKeyJoinColumns().get(1).getName()); + + xmlEntity.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(1, entityResource.getPrimaryKeyJoinColumns().size()); + assertEquals("BAZ", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + + xmlEntity.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(0, entityResource.getPrimaryKeyJoinColumns().size()); + } + + public void testMoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + xmlEntity.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getPrimaryKeyJoinColumns().size()); + + + xmlEntity.moveSpecifiedPrimaryKeyJoinColumn(2, 0); + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + + assertEquals("BAR", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", entityResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", entityResource.getPrimaryKeyJoinColumns().get(2).getName()); + + + xmlEntity.moveSpecifiedPrimaryKeyJoinColumn(0, 1); + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + + assertEquals("BAZ", entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAR", entityResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", entityResource.getPrimaryKeyJoinColumns().get(2).getName()); + } + + public void testUpdatePrimaryKeyJoinColumns() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + entityResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + entityResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + + entityResource.getPrimaryKeyJoinColumns().get(0).setName("FOO"); + entityResource.getPrimaryKeyJoinColumns().get(1).setName("BAR"); + entityResource.getPrimaryKeyJoinColumns().get(2).setName("BAZ"); + + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + entityResource.getPrimaryKeyJoinColumns().move(2, 0); + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + entityResource.getPrimaryKeyJoinColumns().move(0, 1); + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + entityResource.getPrimaryKeyJoinColumns().remove(1); + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + entityResource.getPrimaryKeyJoinColumns().remove(1); + primaryKeyJoinColumns = xmlEntity.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + entityResource.getPrimaryKeyJoinColumns().remove(0); + assertFalse(xmlEntity.specifiedPrimaryKeyJoinColumns().hasNext()); + } + + public void testAddSpecifiedAttributeOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlAttributeOverride attributeOverride = xmlEntity.addSpecifiedAttributeOverride(0); + attributeOverride.setName("FOO"); + + assertEquals("FOO", entityResource.getAttributeOverrides().get(0).getName()); + + XmlAttributeOverride attributeOverride2 = xmlEntity.addSpecifiedAttributeOverride(0); + attributeOverride2.setName("BAR"); + + assertEquals("BAR", entityResource.getAttributeOverrides().get(0).getName()); + assertEquals("FOO", entityResource.getAttributeOverrides().get(1).getName()); + + XmlAttributeOverride attributeOverride3 = xmlEntity.addSpecifiedAttributeOverride(1); + attributeOverride3.setName("BAZ"); + + assertEquals("BAR", entityResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAttributeOverrides().get(2).getName()); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals(attributeOverride2, attributeOverrides.next()); + assertEquals(attributeOverride3, attributeOverrides.next()); + assertEquals(attributeOverride, attributeOverrides.next()); + + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + } + + public void testRemoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEntity.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEntity.addSpecifiedAttributeOverride(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getAttributeOverrides().size()); + + xmlEntity.removeSpecifiedAttributeOverride(0); + assertEquals(2, entityResource.getAttributeOverrides().size()); + assertEquals("BAR", entityResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAttributeOverrides().get(1).getName()); + + xmlEntity.removeSpecifiedAttributeOverride(0); + assertEquals(1, entityResource.getAttributeOverrides().size()); + assertEquals("BAZ", entityResource.getAttributeOverrides().get(0).getName()); + + xmlEntity.removeSpecifiedAttributeOverride(0); + assertEquals(0, entityResource.getAttributeOverrides().size()); + } + + public void testMoveSpecifiedAttributeOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedAttributeOverride(0).setName("FOO"); + xmlEntity.addSpecifiedAttributeOverride(1).setName("BAR"); + xmlEntity.addSpecifiedAttributeOverride(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getAttributeOverrides().size()); + + + xmlEntity.moveSpecifiedAttributeOverride(2, 0); + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAR", entityResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAttributeOverrides().get(2).getName()); + + + xmlEntity.moveSpecifiedAttributeOverride(0, 1); + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + + assertEquals("BAZ", entityResource.getAttributeOverrides().get(0).getName()); + assertEquals("BAR", entityResource.getAttributeOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAttributeOverrides().get(2).getName()); + } + + public void testUpdateAttributeOverrides() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + entityResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + entityResource.getAttributeOverrides().add(OrmFactory.eINSTANCE.createAttributeOverrideImpl()); + + entityResource.getAttributeOverrides().get(0).setName("FOO"); + entityResource.getAttributeOverrides().get(1).setName("BAR"); + entityResource.getAttributeOverrides().get(2).setName("BAZ"); + + ListIterator<XmlAttributeOverride> attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("FOO", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + entityResource.getAttributeOverrides().move(2, 0); + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + entityResource.getAttributeOverrides().move(0, 1); + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("BAR", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + entityResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertEquals("FOO", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + entityResource.getAttributeOverrides().remove(1); + attributeOverrides = xmlEntity.specifiedAttributeOverrides(); + assertEquals("BAZ", attributeOverrides.next().getName()); + assertFalse(attributeOverrides.hasNext()); + + entityResource.getAttributeOverrides().remove(0); + assertFalse(xmlEntity.specifiedAttributeOverrides().hasNext()); + } + + public void testAddSpecifiedAssociationOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlAssociationOverride associationOverride = xmlEntity.addSpecifiedAssociationOverride(0); + associationOverride.setName("FOO"); + + assertEquals("FOO", entityResource.getAssociationOverrides().get(0).getName()); + + XmlAssociationOverride associationOverride2 = xmlEntity.addSpecifiedAssociationOverride(0); + associationOverride2.setName("BAR"); + + assertEquals("BAR", entityResource.getAssociationOverrides().get(0).getName()); + assertEquals("FOO", entityResource.getAssociationOverrides().get(1).getName()); + + XmlAssociationOverride associationOverride3 = xmlEntity.addSpecifiedAssociationOverride(1); + associationOverride3.setName("BAZ"); + + assertEquals("BAR", entityResource.getAssociationOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAssociationOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAssociationOverrides().get(2).getName()); + + ListIterator<XmlAssociationOverride> associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals(associationOverride2, associationOverrides.next()); + assertEquals(associationOverride3, associationOverrides.next()); + assertEquals(associationOverride, associationOverrides.next()); + + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + } + + public void testRemoveSpecifiedAssociationOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedAssociationOverride(0).setName("FOO"); + xmlEntity.addSpecifiedAssociationOverride(1).setName("BAR"); + xmlEntity.addSpecifiedAssociationOverride(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getAssociationOverrides().size()); + + xmlEntity.removeSpecifiedAssociationOverride(0); + assertEquals(2, entityResource.getAssociationOverrides().size()); + assertEquals("BAR", entityResource.getAssociationOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAssociationOverrides().get(1).getName()); + + xmlEntity.removeSpecifiedAssociationOverride(0); + assertEquals(1, entityResource.getAssociationOverrides().size()); + assertEquals("BAZ", entityResource.getAssociationOverrides().get(0).getName()); + + xmlEntity.removeSpecifiedAssociationOverride(0); + assertEquals(0, entityResource.getAssociationOverrides().size()); + } + + public void testMoveSpecifiedAssociationOverride() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addSpecifiedAssociationOverride(0).setName("FOO"); + xmlEntity.addSpecifiedAssociationOverride(1).setName("BAR"); + xmlEntity.addSpecifiedAssociationOverride(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getAssociationOverrides().size()); + + + xmlEntity.moveSpecifiedAssociationOverride(2, 0); + ListIterator<XmlAssociationOverride> associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + + assertEquals("BAR", entityResource.getAssociationOverrides().get(0).getName()); + assertEquals("BAZ", entityResource.getAssociationOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAssociationOverrides().get(2).getName()); + + + xmlEntity.moveSpecifiedAssociationOverride(0, 1); + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + + assertEquals("BAZ", entityResource.getAssociationOverrides().get(0).getName()); + assertEquals("BAR", entityResource.getAssociationOverrides().get(1).getName()); + assertEquals("FOO", entityResource.getAssociationOverrides().get(2).getName()); + } + + public void testUpdateAssociationOverrides() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getAssociationOverrides().add(OrmFactory.eINSTANCE.createAssociationOverride()); + entityResource.getAssociationOverrides().add(OrmFactory.eINSTANCE.createAssociationOverride()); + entityResource.getAssociationOverrides().add(OrmFactory.eINSTANCE.createAssociationOverride()); + + entityResource.getAssociationOverrides().get(0).setName("FOO"); + entityResource.getAssociationOverrides().get(1).setName("BAR"); + entityResource.getAssociationOverrides().get(2).setName("BAZ"); + + ListIterator<XmlAssociationOverride> associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("FOO", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + entityResource.getAssociationOverrides().move(2, 0); + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + entityResource.getAssociationOverrides().move(0, 1); + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("BAR", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + entityResource.getAssociationOverrides().remove(1); + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertEquals("FOO", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + entityResource.getAssociationOverrides().remove(1); + associationOverrides = xmlEntity.specifiedAssociationOverrides(); + assertEquals("BAZ", associationOverrides.next().getName()); + assertFalse(associationOverrides.hasNext()); + + entityResource.getAssociationOverrides().remove(0); + assertFalse(xmlEntity.specifiedAssociationOverrides().hasNext()); + } + + + public void testAddNamedQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlNamedQuery namedQuery = xmlEntity.addNamedQuery(0); + namedQuery.setName("FOO"); + + assertEquals("FOO", entityResource.getNamedQueries().get(0).getName()); + + XmlNamedQuery namedQuery2 = xmlEntity.addNamedQuery(0); + namedQuery2.setName("BAR"); + + assertEquals("BAR", entityResource.getNamedQueries().get(0).getName()); + assertEquals("FOO", entityResource.getNamedQueries().get(1).getName()); + + XmlNamedQuery namedQuery3 = xmlEntity.addNamedQuery(1); + namedQuery3.setName("BAZ"); + + assertEquals("BAR", entityResource.getNamedQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedQueries().get(2).getName()); + + ListIterator<XmlNamedQuery> namedQuerys = xmlEntity.namedQueries(); + assertEquals(namedQuery2, namedQuerys.next()); + assertEquals(namedQuery3, namedQuerys.next()); + assertEquals(namedQuery, namedQuerys.next()); + + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + } + + public void testRemoveNamedQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addNamedQuery(0).setName("FOO"); + xmlEntity.addNamedQuery(1).setName("BAR"); + xmlEntity.addNamedQuery(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getNamedQueries().size()); + + xmlEntity.removeNamedQuery(0); + assertEquals(2, entityResource.getNamedQueries().size()); + assertEquals("BAR", entityResource.getNamedQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedQueries().get(1).getName()); + + xmlEntity.removeNamedQuery(0); + assertEquals(1, entityResource.getNamedQueries().size()); + assertEquals("BAZ", entityResource.getNamedQueries().get(0).getName()); + + xmlEntity.removeNamedQuery(0); + assertEquals(0, entityResource.getNamedQueries().size()); + } + + public void testMoveNamedQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addNamedQuery(0).setName("FOO"); + xmlEntity.addNamedQuery(1).setName("BAR"); + xmlEntity.addNamedQuery(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getNamedQueries().size()); + + + xmlEntity.moveNamedQuery(2, 0); + ListIterator<XmlNamedQuery> namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + + assertEquals("BAR", entityResource.getNamedQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedQueries().get(2).getName()); + + + xmlEntity.moveNamedQuery(0, 1); + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + + assertEquals("BAZ", entityResource.getNamedQueries().get(0).getName()); + assertEquals("BAR", entityResource.getNamedQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedQueries().get(2).getName()); + } + + public void testUpdateNamedQueries() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + entityResource.getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + entityResource.getNamedQueries().add(OrmFactory.eINSTANCE.createNamedQuery()); + + entityResource.getNamedQueries().get(0).setName("FOO"); + entityResource.getNamedQueries().get(1).setName("BAR"); + entityResource.getNamedQueries().get(2).setName("BAZ"); + + ListIterator<XmlNamedQuery> namedQuerys = xmlEntity.namedQueries(); + assertEquals("FOO", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + entityResource.getNamedQueries().move(2, 0); + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + entityResource.getNamedQueries().move(0, 1); + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("BAR", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + entityResource.getNamedQueries().remove(1); + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertEquals("FOO", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + entityResource.getNamedQueries().remove(1); + namedQuerys = xmlEntity.namedQueries(); + assertEquals("BAZ", namedQuerys.next().getName()); + assertFalse(namedQuerys.hasNext()); + + entityResource.getNamedQueries().remove(0); + assertFalse(xmlEntity.namedQueries().hasNext()); + } + + public void testAddNamedNativeQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + XmlNamedNativeQuery namedNativeQuery = xmlEntity.addNamedNativeQuery(0); + namedNativeQuery.setName("FOO"); + + assertEquals("FOO", entityResource.getNamedNativeQueries().get(0).getName()); + + XmlNamedNativeQuery namedNativeQuery2 = xmlEntity.addNamedNativeQuery(0); + namedNativeQuery2.setName("BAR"); + + assertEquals("BAR", entityResource.getNamedNativeQueries().get(0).getName()); + assertEquals("FOO", entityResource.getNamedNativeQueries().get(1).getName()); + + XmlNamedNativeQuery namedNativeQuery3 = xmlEntity.addNamedNativeQuery(1); + namedNativeQuery3.setName("BAZ"); + + assertEquals("BAR", entityResource.getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedNativeQueries().get(2).getName()); + + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals(namedNativeQuery2, namedNativeQuerys.next()); + assertEquals(namedNativeQuery3, namedNativeQuerys.next()); + assertEquals(namedNativeQuery, namedNativeQuerys.next()); + + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + } + + public void testRemoveNamedNativeQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addNamedNativeQuery(0).setName("FOO"); + xmlEntity.addNamedNativeQuery(1).setName("BAR"); + xmlEntity.addNamedNativeQuery(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getNamedNativeQueries().size()); + + xmlEntity.removeNamedNativeQuery(0); + assertEquals(2, entityResource.getNamedNativeQueries().size()); + assertEquals("BAR", entityResource.getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedNativeQueries().get(1).getName()); + + xmlEntity.removeNamedNativeQuery(0); + assertEquals(1, entityResource.getNamedNativeQueries().size()); + assertEquals("BAZ", entityResource.getNamedNativeQueries().get(0).getName()); + + xmlEntity.removeNamedNativeQuery(0); + assertEquals(0, entityResource.getNamedNativeQueries().size()); + } + + public void testMoveNamedNativeQuery() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + xmlEntity.addNamedNativeQuery(0).setName("FOO"); + xmlEntity.addNamedNativeQuery(1).setName("BAR"); + xmlEntity.addNamedNativeQuery(2).setName("BAZ"); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertEquals(3, entityResource.getNamedNativeQueries().size()); + + + xmlEntity.moveNamedNativeQuery(2, 0); + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + + assertEquals("BAR", entityResource.getNamedNativeQueries().get(0).getName()); + assertEquals("BAZ", entityResource.getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedNativeQueries().get(2).getName()); + + + xmlEntity.moveNamedNativeQuery(0, 1); + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + + assertEquals("BAZ", entityResource.getNamedNativeQueries().get(0).getName()); + assertEquals("BAR", entityResource.getNamedNativeQueries().get(1).getName()); + assertEquals("FOO", entityResource.getNamedNativeQueries().get(2).getName()); + } + + public void testUpdateNamedNativeQueries() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + entityResource.getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + entityResource.getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + entityResource.getNamedNativeQueries().add(OrmFactory.eINSTANCE.createNamedNativeQuery()); + + entityResource.getNamedNativeQueries().get(0).setName("FOO"); + entityResource.getNamedNativeQueries().get(1).setName("BAR"); + entityResource.getNamedNativeQueries().get(2).setName("BAZ"); + + ListIterator<XmlNamedNativeQuery> namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + entityResource.getNamedNativeQueries().move(2, 0); + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + entityResource.getNamedNativeQueries().move(0, 1); + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("BAR", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + entityResource.getNamedNativeQueries().remove(1); + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertEquals("FOO", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + entityResource.getNamedNativeQueries().remove(1); + namedNativeQuerys = xmlEntity.namedNativeQueries(); + assertEquals("BAZ", namedNativeQuerys.next().getName()); + assertFalse(namedNativeQuerys.hasNext()); + + entityResource.getNamedNativeQueries().remove(0); + assertFalse(xmlEntity.namedNativeQueries().hasNext()); + } + + public void testUpdateIdClass() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getIdClass()); + assertNull(entityResource.getIdClass()); + + entityResource.setIdClass(OrmFactory.eINSTANCE.createIdClass()); + + assertNull(xmlEntity.getIdClass()); + assertNotNull(entityResource.getIdClass()); + + entityResource.getIdClass().setClassName("model.Foo"); + assertEquals("model.Foo", xmlEntity.getIdClass()); + assertEquals("model.Foo", entityResource.getIdClass().getClassName()); + + //test setting @IdClass value to null, id-class tag is not removed + entityResource.getIdClass().setClassName(null); + assertNull(xmlEntity.getIdClass()); + assertNotNull(entityResource.getIdClass()); + + //reset @IdClass value and then remove id-class tag + entityResource.setIdClass(OrmFactory.eINSTANCE.createIdClass()); + entityResource.getIdClass().setClassName("model.Foo"); + entityResource.setIdClass(null); + + assertNull(xmlEntity.getIdClass()); + assertNull(entityResource.getIdClass()); + } + + public void testModifyIdClass() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + assertNull(xmlEntity.getIdClass()); + assertNull(entityResource.getIdClass()); + + xmlEntity.setIdClass("model.Foo"); + assertEquals("model.Foo", entityResource.getIdClass().getClassName()); + assertEquals("model.Foo", xmlEntity.getIdClass()); + + xmlEntity.setIdClass(null); + assertNull(xmlEntity.getIdClass()); + assertNull(entityResource.getIdClass()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlGeneratedValueTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlGeneratedValueTests.java new file mode 100644 index 0000000000..bb9778689c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlGeneratedValueTests.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlGeneratedValue; +import org.eclipse.jpt.core.internal.context.orm.XmlIdMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.GeneratedValue; +import org.eclipse.jpt.core.internal.resource.orm.Id; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlGeneratedValueTests extends ContextModelTestCase +{ + public XmlGeneratedValueTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + XmlGeneratedValue xmlGeneratedValue = xmlIdMapping.addGeneratedValue(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + GeneratedValue generatedValueResource = idResource.getGeneratedValue(); + + //set generator in the resource model, verify context model updated + generatedValueResource.setGenerator("FOO"); + assertEquals("FOO", xmlGeneratedValue.getSpecifiedGenerator()); + assertEquals("FOO", generatedValueResource.getGenerator()); + + //set name to null in the resource model + generatedValueResource.setGenerator(null); + assertNull(xmlGeneratedValue.getSpecifiedGenerator()); + assertNull(generatedValueResource.getGenerator()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + XmlGeneratedValue xmlGeneratedValue = xmlIdMapping.addGeneratedValue(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + GeneratedValue generatedValueResource = idResource.getGeneratedValue(); + + //set name in the context model, verify resource model modified + xmlGeneratedValue.setSpecifiedGenerator("FOO"); + assertEquals("FOO", generatedValueResource.getGenerator()); + assertEquals("FOO", xmlGeneratedValue.getSpecifiedGenerator()); + + //set name to null in the context model + xmlGeneratedValue.setSpecifiedGenerator(null); + assertNull(generatedValueResource.getGenerator()); + assertNull(xmlGeneratedValue.getSpecifiedGenerator()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlIdMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlIdMappingTests.java new file mode 100644 index 0000000000..420cfbd6fa --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlIdMappingTests.java @@ -0,0 +1,620 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.GenerationType; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.context.orm.XmlColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlGeneratedValue; +import org.eclipse.jpt.core.internal.context.orm.XmlIdMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlSequenceGenerator; +import org.eclipse.jpt.core.internal.context.orm.XmlTableGenerator; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Id; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlIdMappingTests extends ContextModelTestCase +{ + public XmlIdMappingTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception{ + this.createAnnotationAndMembers("Id", ""); + } + + private void createColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private void createGeneratedValueAnnotation() throws Exception{ + this.createAnnotationAndMembers("GeneratedValue", + "GenerationType strategy() default AUTO;" + + "String generator() default \"\"; "); + } + + + private void createSequenceGeneratorAnnotation() throws Exception{ + this.createAnnotationAndMembers("SequenceGenerator", + "String name();" + + "String sequenceName() default \"\"; " + + "int initialValue() default 0; " + + "int allocationSize() default 50;"); + } + + private void createTableGeneratorAnnotation() throws Exception{ + this.createAnnotationAndMembers("TableGenerator", + "String name(); " + + "String table() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";" + + "String pkColumnName() default \"\"; " + + "String valueColumnName() default \"\"; " + + "String pkColumnValue() default \"\"; " + + "int initialValue() default 0; " + + "int allocationSize() default 50; " + + "UniqueConstraint[] uniqueConstraints() default {};"); + } + + private IType createTestEntityIdMapping() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + createColumnAnnotation(); + createTemporalAnnotation(); + createGeneratedValueAnnotation(); + createSequenceGeneratorAnnotation(); + createTableGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.COLUMN, JPA.TEMPORAL, JPA.TEMPORAL_TYPE, JPA.GENERATED_VALUE, JPA.GENERATION_TYPE, JPA.TABLE_GENERATOR, JPA.SEQUENCE_GENERATOR); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + sb.append(CR); + sb.append(" @Column(name=\"MY_COLUMN\", unique=true, nullable=false, insertable=false, updatable=false, columnDefinition=\"COLUMN_DEFINITION\", table=\"MY_TABLE\", length=5, precision=6, scale=7)"); + sb.append(CR); + sb.append(" @Temporal(TemporalType.TIMESTAMP)"); + sb.append(CR); + sb.append(" @GeneratedValue(strategy=GenerationType.TABLE, generator=\"myTableGenerator\")"); + sb.append(CR); + sb.append(" @TableGenerator(name=\"myTableGenerator\", table=\"myTable\", catalog=\"myCatalog\", schema=\"mySchema\", pkColumnName=\"myPkColumnName\", valueColumnName=\"myValueColumnName\", pkColumnValue=\"myPkColumnValue\", initialValue=1, allocationSize=1)"); + sb.append(CR); + sb.append(" @SequenceGenerator(name=\"mySequenceGenerator\")"); + } + }); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertEquals("idMapping", xmlIdMapping.getName()); + assertEquals("idMapping", idResource.getName()); + + //set name in the resource model, verify context model updated + idResource.setName("newName"); + assertEquals("newName", xmlIdMapping.getName()); + assertEquals("newName", idResource.getName()); + + //set name to null in the resource model + idResource.setName(null); + assertNull(xmlIdMapping.getName()); + assertNull(idResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertEquals("idMapping", xmlIdMapping.getName()); + assertEquals("idMapping", idResource.getName()); + + //set name in the context model, verify resource model updated + xmlIdMapping.setName("newName"); + assertEquals("newName", xmlIdMapping.getName()); + assertEquals("newName", idResource.getName()); + + //set name to null in the context model + xmlIdMapping.setName(null); + assertNull(xmlIdMapping.getName()); + assertNull(idResource.getName()); + } + + public void testUpdateTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + ormResource().save(null); + + assertNull(xmlIdMapping.getTemporal()); + assertNull(idResource.getTemporal()); + + //set temporal in the resource model, verify context model updated + idResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE); + ormResource().save(null); + assertEquals(TemporalType.DATE, xmlIdMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, idResource.getTemporal()); + + idResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME); + ormResource().save(null); + assertEquals(TemporalType.TIME, xmlIdMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, idResource.getTemporal()); + + idResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(TemporalType.TIMESTAMP, xmlIdMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, idResource.getTemporal()); + + //set temporal to null in the resource model + idResource.setTemporal(null); + ormResource().save(null); + assertNull(xmlIdMapping.getTemporal()); + assertNull(idResource.getTemporal()); + } + + public void testModifyTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + ormResource().save(null); + + assertNull(xmlIdMapping.getTemporal()); + assertNull(idResource.getTemporal()); + + //set temporal in the context model, verify resource model updated + xmlIdMapping.setTemporal(TemporalType.DATE); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, idResource.getTemporal()); + assertEquals(TemporalType.DATE, xmlIdMapping.getTemporal()); + + xmlIdMapping.setTemporal(TemporalType.TIME); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, idResource.getTemporal()); + assertEquals(TemporalType.TIME, xmlIdMapping.getTemporal()); + + xmlIdMapping.setTemporal(TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, idResource.getTemporal()); + assertEquals(TemporalType.TIMESTAMP, xmlIdMapping.getTemporal()); + + //set temporal to null in the context model + xmlIdMapping.setTemporal(null); + ormResource().save(null); + assertNull(idResource.getTemporal()); + assertNull(xmlIdMapping.getTemporal()); + } + + //TODO test morphing to other mapping types + //TODO test defaults + + + public void testAddSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(idResource.getSequenceGenerator()); + + xmlIdMapping.addSequenceGenerator(); + + assertNotNull(idResource.getSequenceGenerator()); + assertNotNull(xmlIdMapping.getSequenceGenerator()); + + //try adding another sequence generator, should get an IllegalStateException + try { + xmlIdMapping.addSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(idResource.getSequenceGenerator()); + + xmlIdMapping.addSequenceGenerator(); + assertNotNull(idResource.getSequenceGenerator()); + assertNotNull(xmlIdMapping.getSequenceGenerator()); + + xmlIdMapping.removeSequenceGenerator(); + + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(idResource.getSequenceGenerator()); + + //try removing the sequence generator again, should get an IllegalStateException + try { + xmlIdMapping.removeSequenceGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testUpdateSequenceGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(idResource.getSequenceGenerator()); + + idResource.setSequenceGenerator(OrmFactory.eINSTANCE.createSequenceGeneratorImpl()); + + assertNotNull(xmlIdMapping.getSequenceGenerator()); + assertNotNull(idResource.getSequenceGenerator()); + + idResource.setSequenceGenerator(null); + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(idResource.getSequenceGenerator()); + } + + public void testAddTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getTableGenerator()); + assertNull(idResource.getTableGenerator()); + + xmlIdMapping.addTableGenerator(); + + assertNotNull(idResource.getTableGenerator()); + assertNotNull(xmlIdMapping.getTableGenerator()); + + //try adding another table generator, should get an IllegalStateException + try { + xmlIdMapping.addTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getTableGenerator()); + assertNull(idResource.getTableGenerator()); + + xmlIdMapping.addTableGenerator(); + assertNotNull(idResource.getTableGenerator()); + assertNotNull(xmlIdMapping.getTableGenerator()); + + xmlIdMapping.removeTableGenerator(); + + assertNull(xmlIdMapping.getTableGenerator()); + assertNull(idResource.getTableGenerator()); + + //try removing the table generator again, should get an IllegalStateException + try { + xmlIdMapping.removeTableGenerator(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testUpdateTableGenerator() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getTableGenerator()); + assertNull(idResource.getTableGenerator()); + + idResource.setTableGenerator(OrmFactory.eINSTANCE.createTableGeneratorImpl()); + + assertNotNull(xmlIdMapping.getTableGenerator()); + assertNotNull(idResource.getTableGenerator()); + + idResource.setTableGenerator(null); + assertNull(xmlIdMapping.getTableGenerator()); + assertNull(idResource.getTableGenerator()); + } + + public void testAddGeneratedValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(idResource.getGeneratedValue()); + + xmlIdMapping.addGeneratedValue(); + + assertNotNull(idResource.getGeneratedValue()); + assertNotNull(xmlIdMapping.getGeneratedValue()); + + //try adding another sequence generator, should get an IllegalStateException + try { + xmlIdMapping.addGeneratedValue(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testRemoveGeneratedValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(idResource.getGeneratedValue()); + + xmlIdMapping.addGeneratedValue(); + assertNotNull(idResource.getGeneratedValue()); + assertNotNull(xmlIdMapping.getGeneratedValue()); + + xmlIdMapping.removeGeneratedValue(); + + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(idResource.getGeneratedValue()); + + //try removing the sequence generator again, should get an IllegalStateException + try { + xmlIdMapping.removeGeneratedValue(); + } catch (IllegalStateException e) { + return; + } + fail("IllegalStateException not thrown"); + } + + public void testUpdateGeneratedValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idMapping"); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + Id idResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getIds().get(0); + + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(idResource.getGeneratedValue()); + + idResource.setGeneratedValue(OrmFactory.eINSTANCE.createGeneratedValueImpl()); + + assertNotNull(xmlIdMapping.getGeneratedValue()); + assertNotNull(idResource.getGeneratedValue()); + + idResource.setGeneratedValue(null); + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(idResource.getGeneratedValue()); + } + + + public void testIdMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityIdMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlIdMapping.getName()); + assertNull(xmlIdMapping.getTemporal()); + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(xmlIdMapping.getTableGenerator()); + + + XmlColumn xmlColumn = xmlIdMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("foo", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + } + + //@Basic(fetch=FetchType.LAZY, optional=false) + //@Column(name="MY_COLUMN", unique=true, nullable=false, insertable=false, updatable=false, + // columnDefinition="COLUMN_DEFINITION", table="MY_TABLE", length=5, precision=6, scale=7)"); + //@Column( + //@Lob + //@Temporal(TemporalType.TIMESTAMP) + //@Enumerated(EnumType.STRING) + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityIdMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlIdMapping.getName()); + assertEquals(TemporalType.TIMESTAMP, xmlIdMapping.getTemporal()); + + XmlColumn xmlColumn = xmlIdMapping.getColumn(); + assertEquals("MY_COLUMN", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); + assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); + assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); + + XmlGeneratedValue xmlGeneratedValue = xmlIdMapping.getGeneratedValue(); + assertEquals("myTableGenerator", xmlGeneratedValue.getSpecifiedGenerator()); + assertEquals(GenerationType.TABLE, xmlGeneratedValue.getSpecifiedStrategy()); + + XmlTableGenerator xmlTableGenerator = xmlIdMapping.getTableGenerator(); + assertEquals("myTableGenerator", xmlTableGenerator.getName()); + assertEquals("myTable", xmlTableGenerator.getSpecifiedTable()); + assertEquals("myCatalog", xmlTableGenerator.getSpecifiedCatalog()); + assertEquals("mySchema", xmlTableGenerator.getSpecifiedSchema()); + assertEquals("myPkColumnName", xmlTableGenerator.getSpecifiedPkColumnName()); + assertEquals("myPkColumnValue", xmlTableGenerator.getSpecifiedPkColumnValue()); + assertEquals("myValueColumnName", xmlTableGenerator.getSpecifiedValueColumnName()); + assertEquals(Integer.valueOf(1), xmlTableGenerator.getSpecifiedInitialValue()); + assertEquals(Integer.valueOf(1), xmlTableGenerator.getSpecifiedAllocationSize()); + + XmlSequenceGenerator xmlSequenceGenerator = xmlIdMapping.getSequenceGenerator(); + assertEquals("mySequenceGenerator", xmlSequenceGenerator.getName()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityIdMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlIdMapping.getName()); + assertNull(xmlIdMapping.getTemporal()); + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(xmlIdMapping.getTableGenerator()); + + XmlColumn xmlColumn = xmlIdMapping.getColumn(); + assertEquals("id", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedScale()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityIdMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "id"); + assertEquals(1, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlIdMapping xmlIdMapping = (XmlIdMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("id", xmlIdMapping.getName()); + assertNull(xmlIdMapping.getTemporal()); + assertNull(xmlIdMapping.getGeneratedValue()); + assertNull(xmlIdMapping.getSequenceGenerator()); + assertNull(xmlIdMapping.getTableGenerator()); + + XmlColumn xmlColumn = xmlIdMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("id", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + } + + //3 things tested above + //1. virtual mapping metadata complete=false - defaults are taken from the java annotations + //2. virtual mapping metadata complete=true - defaults are taken from java defaults,annotations ignored + //3. specified mapping (metadata complete=true/false - defaults are taken from java annotations + + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinColumnTests.java new file mode 100644 index 0000000000..7ef1c90c6a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinColumnTests.java @@ -0,0 +1,564 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinTable; +import org.eclipse.jpt.core.internal.context.orm.XmlManyToManyMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.JoinColumn; +import org.eclipse.jpt.core.internal.resource.orm.JoinTable; +import org.eclipse.jpt.core.internal.resource.orm.ManyToMany; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlJoinColumnTests extends ContextModelTestCase +{ + public XmlJoinColumnTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getSpecifiedName()); + assertNull(joinColumnResource.getName()); + + //set name in the resource model, verify context model updated + joinColumnResource.setName("FOO"); + assertEquals("FOO", joinColumn.getSpecifiedName()); + assertEquals("FOO", joinColumnResource.getName()); + + //set name to null in the resource model + joinColumnResource.setName(null); + assertNull(joinColumn.getSpecifiedName()); + assertNull(joinColumnResource.getName()); + + joinColumnResource.setName("FOO"); + assertEquals("FOO", joinColumn.getSpecifiedName()); + assertEquals("FOO", joinColumnResource.getName()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getSpecifiedName()); + assertNull(joinColumnResource.getName()); + + //set name in the context model, verify resource model modified + joinColumn.setSpecifiedName("foo"); + assertEquals("foo", joinColumn.getSpecifiedName()); + assertEquals("foo", joinColumnResource.getName()); + + //set name to null in the context model + joinColumn.setSpecifiedName(null); + assertNull(joinColumn.getSpecifiedName()); + assertNull(joinTableResource.getJoinColumns().get(0).getName()); + } + + public void testUpdateSpecifiedReferencedColumnName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getSpecifiedReferencedColumnName()); + assertNull(joinColumnResource.getReferencedColumnName()); + + //set name in the resource model, verify context model updated + joinColumnResource.setReferencedColumnName("FOO"); + ormResource().save(null); + assertEquals("FOO", joinColumn.getSpecifiedReferencedColumnName()); + assertEquals("FOO", joinColumnResource.getReferencedColumnName()); + + //set name to null in the resource model + joinColumnResource.setReferencedColumnName(null); + assertNull(joinColumn.getSpecifiedReferencedColumnName()); + assertNull(joinColumnResource.getReferencedColumnName()); + + joinColumnResource.setReferencedColumnName("FOO"); + assertEquals("FOO", joinColumn.getSpecifiedReferencedColumnName()); + assertEquals("FOO", joinColumnResource.getReferencedColumnName()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedReferencedColumnName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getSpecifiedReferencedColumnName()); + assertNull(joinColumnResource.getReferencedColumnName()); + + //set name in the context model, verify resource model modified + joinColumn.setSpecifiedReferencedColumnName("foo"); + assertEquals("foo", joinColumn.getSpecifiedReferencedColumnName()); + assertEquals("foo", joinColumnResource.getReferencedColumnName()); + + //set name to null in the context model + joinColumn.setSpecifiedReferencedColumnName(null); + assertNull(joinColumn.getSpecifiedReferencedColumnName()); + assertNull(joinTableResource.getJoinColumns().get(0).getReferencedColumnName()); + } + +// public void testUpdateDefaultNameFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.getTable().setSpecifiedName("FOO"); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +// } + + + public void testUpdateSpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getColumnDefinition()); + assertNull(joinColumnResource.getColumnDefinition()); + + //set name in the resource model, verify context model updated + joinColumnResource.setColumnDefinition("FOO"); + assertEquals("FOO", joinColumn.getColumnDefinition()); + assertEquals("FOO", joinColumnResource.getColumnDefinition()); + + //set name to null in the resource model + joinColumnResource.setColumnDefinition(null); + assertNull(joinColumn.getColumnDefinition()); + assertNull(joinColumnResource.getColumnDefinition()); + + joinColumnResource.setColumnDefinition("FOO"); + assertEquals("FOO", joinColumn.getColumnDefinition()); + assertEquals("FOO", joinColumnResource.getColumnDefinition()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(joinColumn.getColumnDefinition()); + assertNull(joinColumnResource.getColumnDefinition()); + + //set name in the context model, verify resource model modified + joinColumn.setColumnDefinition("foo"); + assertEquals("foo", joinColumn.getColumnDefinition()); + assertEquals("foo", joinColumnResource.getColumnDefinition()); + + //set name to null in the context model + joinColumn.setColumnDefinition(null); + assertNull(joinColumn.getColumnDefinition()); + assertNull(joinTableResource.getJoinColumns().get(0).getColumnDefinition()); + } + + public void testUpdateSpecifiedTable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedTable()); + assertNull(joinColumnResource.getTable()); + + //set table in the resource model, verify context model updated + joinColumnResource.setTable("FOO"); + assertEquals("FOO", xmlJoinColumn.getSpecifiedTable()); + assertEquals("FOO", joinColumnResource.getTable()); + + //set table to null in the resource model + joinColumnResource.setTable(null); + assertNull(xmlJoinColumn.getSpecifiedTable()); + assertNull(joinColumnResource.getTable()); + + joinColumnResource.setTable("FOO"); + assertEquals("FOO", xmlJoinColumn.getSpecifiedTable()); + assertEquals("FOO", joinColumnResource.getTable()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedTable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedTable()); + assertNull(joinColumnResource.getTable()); + + //set table in the context model, verify resource model modified + xmlJoinColumn.setSpecifiedTable("foo"); + assertEquals("foo", xmlJoinColumn.getSpecifiedTable()); + assertEquals("foo", joinColumnResource.getTable()); + + //set table to null in the context model + xmlJoinColumn.setSpecifiedTable(null); + assertNull(xmlJoinColumn.getSpecifiedTable()); + assertNull(joinTableResource.getJoinColumns().get(0).getTable()); + } + + public void testUpdateSpecifiedNullable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedNullable()); + assertNull(joinColumnResource.getNullable()); + + //set nullable in the resource model, verify context model updated + joinColumnResource.setNullable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, joinColumnResource.getNullable()); + + //set nullable to null in the resource model + joinColumnResource.setNullable(null); + assertNull(xmlJoinColumn.getSpecifiedNullable()); + assertNull(joinColumnResource.getNullable()); + + joinColumnResource.setNullable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, joinColumnResource.getNullable()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedNullable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedNullable()); + assertNull(joinColumnResource.getNullable()); + + //set nullable in the context model, verify resource model modified + xmlJoinColumn.setSpecifiedNullable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, joinColumnResource.getNullable()); + + //set nullable to null in the context model + xmlJoinColumn.setSpecifiedNullable(null); + assertNull(xmlJoinColumn.getSpecifiedNullable()); + assertNull(joinTableResource.getJoinColumns().get(0).getNullable()); + } + + public void testUpdateSpecifiedUpdatable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(joinColumnResource.getUpdatable()); + + //set updatable in the resource model, verify context model updated + joinColumnResource.setUpdatable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.TRUE, joinColumnResource.getUpdatable()); + + //set updatable to null in the resource model + joinColumnResource.setUpdatable(null); + assertNull(xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(joinColumnResource.getUpdatable()); + + joinColumnResource.setUpdatable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, joinColumnResource.getUpdatable()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedUpdatable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(joinColumnResource.getUpdatable()); + + //set updatable in the context model, verify resource model modified + xmlJoinColumn.setSpecifiedUpdatable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUpdatable()); + assertEquals(Boolean.FALSE, joinColumnResource.getUpdatable()); + + //set updatable to null in the context model + xmlJoinColumn.setSpecifiedUpdatable(null); + assertNull(xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(joinTableResource.getJoinColumns().get(0).getUpdatable()); + } + + public void testUpdateSpecifiedInsertable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedInsertable()); + assertNull(joinColumnResource.getInsertable()); + + //set insertable in the resource model, verify context model updated + joinColumnResource.setInsertable(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, joinColumnResource.getInsertable()); + + //set insertable to null in the resource model + joinColumnResource.setInsertable(null); + assertNull(xmlJoinColumn.getSpecifiedInsertable()); + assertNull(joinColumnResource.getInsertable()); + + joinColumnResource.setInsertable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, joinColumnResource.getInsertable()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedInsertable() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedInsertable()); + assertNull(joinColumnResource.getInsertable()); + + //set insertable in the context model, verify resource model modified + xmlJoinColumn.setSpecifiedInsertable(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, joinColumnResource.getInsertable()); + + //set insertable to null in the context model + xmlJoinColumn.setSpecifiedInsertable(null); + assertNull(xmlJoinColumn.getSpecifiedInsertable()); + assertNull(joinTableResource.getJoinColumns().get(0).getInsertable()); + } + + public void testUpdateSpecifiedUnique() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedUnique()); + assertNull(joinColumnResource.getUnique()); + + //set unique in the resource model, verify context model updated + joinColumnResource.setUnique(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, joinColumnResource.getUnique()); + + //set unique to null in the resource model + joinColumnResource.setUnique(null); + assertNull(xmlJoinColumn.getSpecifiedUnique()); + assertNull(joinColumnResource.getUnique()); + + joinColumnResource.setUnique(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, joinColumnResource.getUnique()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.joinColumns().hasNext()); + assertTrue(joinTableResource.getJoinColumns().isEmpty()); + } + + public void testModifySpecifiedUnique() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + XmlJoinColumn xmlJoinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + JoinTable joinTableResource = manyToMany.getJoinTable(); + JoinColumn joinColumnResource = joinTableResource.getJoinColumns().get(0); + + assertNull(xmlJoinColumn.getSpecifiedUnique()); + assertNull(joinColumnResource.getUnique()); + + //set unique in the context model, verify resource model modified + xmlJoinColumn.setSpecifiedUnique(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, joinColumnResource.getUnique()); + + //set unique to null in the context model + xmlJoinColumn.setSpecifiedUnique(null); + assertNull(xmlJoinColumn.getSpecifiedUnique()); + assertNull(joinTableResource.getJoinColumns().get(0).getUnique()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinTableTests.java new file mode 100644 index 0000000000..67d3cb2f2a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlJoinTableTests.java @@ -0,0 +1,781 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinTable; +import org.eclipse.jpt.core.internal.context.orm.XmlManyToManyMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.JoinTable; +import org.eclipse.jpt.core.internal.resource.orm.ManyToMany; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlJoinTableTests extends ContextModelTestCase +{ + public XmlJoinTableTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedName()); + assertNull(manyToMany.getJoinTable()); + + + //set name in the resource model, verify context model updated + manyToMany.setJoinTable(OrmFactory.eINSTANCE.createJoinTableImpl()); + manyToMany.getJoinTable().setName("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedName()); + assertEquals("FOO", manyToMany.getJoinTable().getName()); + + //set name to null in the resource model + manyToMany.getJoinTable().setName(null); + assertNull(xmlJoinTable.getSpecifiedName()); + assertNull(manyToMany.getJoinTable().getName()); + + manyToMany.getJoinTable().setName("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedName()); + assertEquals("FOO", manyToMany.getJoinTable().getName()); + + manyToMany.setJoinTable(null); + assertNull(xmlJoinTable.getSpecifiedName()); + assertNull(manyToMany.getJoinTable()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedName()); + assertNull(manyToMany.getJoinTable()); + + //set name in the context model, verify resource model modified + xmlJoinTable.setSpecifiedName("foo"); + assertEquals("foo", xmlJoinTable.getSpecifiedName()); + assertEquals("foo", manyToMany.getJoinTable().getName()); + + //set name to null in the context model + xmlJoinTable.setSpecifiedName(null); + assertNull(xmlJoinTable.getSpecifiedName()); + assertNull(manyToMany.getJoinTable()); + } + +// public void testUpdateDefaultNameFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.getTable().setSpecifiedName("FOO"); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +// } + + public void testUpdateSpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedSchema()); + assertNull(manyToMany.getJoinTable()); + + //set schema in the resource model, verify context model updated + manyToMany.setJoinTable(OrmFactory.eINSTANCE.createJoinTableImpl()); + manyToMany.getJoinTable().setSchema("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedSchema()); + assertEquals("FOO", manyToMany.getJoinTable().getSchema()); + + //set Schema to null in the resource model + manyToMany.getJoinTable().setSchema(null); + assertNull(xmlJoinTable.getSpecifiedSchema()); + assertNull(manyToMany.getJoinTable().getSchema()); + + manyToMany.getJoinTable().setSchema("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedSchema()); + assertEquals("FOO", manyToMany.getJoinTable().getSchema()); + + manyToMany.setJoinTable(null); + assertNull(xmlJoinTable.getSpecifiedSchema()); + assertNull(manyToMany.getJoinTable()); + } + +// public void testUpdateDefaultSchemaFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedSchema("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// } +// +// public void testUpdateDefaultSchemaNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// } +// +// public void testUpdateDefaultSchemaFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertNull(childXmlEntity.getTable().getDefaultSchema()); +// +// parentXmlEntity.getTable().setSpecifiedSchema("FOO"); +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultSchema()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertNull(childXmlEntity.getTable().getDefaultSchema()); +// } +// +// public void testUpdateDefaultSchemaFromPersistenceUnitDefaults() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("FOO"); +// assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.entityMappings().setSpecifiedSchema("BAR"); +// assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedSchema("JAVA_SCHEMA"); +// assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.getTable().setSpecifiedName("BLAH"); +// //xml entity now has a table element so default schema is not taken from java +// assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); +// +// +// xmlEntity.entityMappings().setSpecifiedSchema(null); +// assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); +// assertNull(xmlEntity.getTable().getDefaultSchema()); +// +// xmlEntity.getTable().setSpecifiedName(null); +// assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); +// } + + public void testModifySpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedSchema()); + assertNull(manyToMany.getJoinTable()); + + //set Schema in the context model, verify resource model modified + xmlJoinTable.setSpecifiedSchema("foo"); + assertEquals("foo", xmlJoinTable.getSpecifiedSchema()); + assertEquals("foo", manyToMany.getJoinTable().getSchema()); + + //set Schema to null in the context model + xmlJoinTable.setSpecifiedSchema(null); + assertNull(xmlJoinTable.getSpecifiedSchema()); + assertNull(manyToMany.getJoinTable()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedCatalog()); + assertNull(manyToMany.getJoinTable()); + + //set Catalog in the resource model, verify context model updated + manyToMany.setJoinTable(OrmFactory.eINSTANCE.createJoinTableImpl()); + manyToMany.getJoinTable().setCatalog("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedCatalog()); + assertEquals("FOO", manyToMany.getJoinTable().getCatalog()); + + //set Catalog to null in the resource model + manyToMany.getJoinTable().setCatalog(null); + assertNull(xmlJoinTable.getSpecifiedCatalog()); + assertNull(manyToMany.getJoinTable().getCatalog()); + + manyToMany.getJoinTable().setCatalog("FOO"); + assertEquals("FOO", xmlJoinTable.getSpecifiedCatalog()); + assertEquals("FOO", manyToMany.getJoinTable().getCatalog()); + + manyToMany.setJoinTable(null); + assertNull(xmlJoinTable.getSpecifiedCatalog()); + assertNull(manyToMany.getJoinTable()); + } + + public void testModifySpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + assertNull(xmlJoinTable.getSpecifiedCatalog()); + assertNull(manyToMany.getJoinTable()); + + //set Catalog in the context model, verify resource model modified + xmlJoinTable.setSpecifiedCatalog("foo"); + assertEquals("foo", xmlJoinTable.getSpecifiedCatalog()); + assertEquals("foo", manyToMany.getJoinTable().getCatalog()); + + //set Catalog to null in the context model + xmlJoinTable.setSpecifiedCatalog(null); + assertNull(xmlJoinTable.getSpecifiedCatalog()); + assertNull(manyToMany.getJoinTable()); + } + +// public void testUpdateDefaultCatalogFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedCatalog("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// } +// +// public void testUpdateDefaultCatalogNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// } +// +// public void testUpdateDefaultCatalogFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +// assertNull(childXmlEntity.getTable().getDefaultCatalog()); +// +// parentXmlEntity.getTable().setSpecifiedCatalog("FOO"); +// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultCatalog()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertNull(parentXmlEntity.getTable().getDefaultCatalog()); +// assertNull(childXmlEntity.getTable().getDefaultCatalog()); +// } +// +// public void testUpdateDefaultCatalogFromPersistenceUnitDefaults() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("FOO"); +// assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.entityMappings().setSpecifiedCatalog("BAR"); +// assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedCatalog("JAVA_CATALOG"); +// assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.getTable().setSpecifiedName("BLAH"); +// //xml entity now has a table element so default schema is not taken from java +// assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); +// +// +// xmlEntity.entityMappings().setSpecifiedCatalog(null); +// assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); +// assertNull(xmlEntity.getTable().getDefaultCatalog()); +// +// xmlEntity.getTable().setSpecifiedName(null); +// assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); +//} + +// +// public void testUpdateName() throws Exception { +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); +// assertEquals("Foo", xmlEntity.getName()); +// +// //set class in the resource model, verify context model updated +// entityResource.setClassName("com.Bar"); +// assertEquals("Bar", xmlEntity.getName()); +// +// entityResource.setName("Baz"); +// assertEquals("Baz", xmlEntity.getName()); +// +// //set class to null in the resource model +// entityResource.setClassName(null); +// assertEquals("Baz", xmlEntity.getName()); +// +// entityResource.setName(null); +// assertNull(xmlEntity.getName()); +// } + + + public void testAddSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn.setSpecifiedName("FOO"); + ormResource().save(null); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals("FOO", joinTableResource.getJoinColumns().get(0).getName()); + + XmlJoinColumn joinColumn2 = xmlJoinTable.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", joinTableResource.getJoinColumns().get(0).getName()); + assertEquals("FOO", joinTableResource.getJoinColumns().get(1).getName()); + + XmlJoinColumn joinColumn3 = xmlJoinTable.addSpecifiedJoinColumn(1); + ormResource().save(null); + joinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", joinTableResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getJoinColumns().get(2).getName()); + + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + xmlJoinTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlJoinTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlJoinTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals(3, joinTableResource.getJoinColumns().size()); + + xmlJoinTable.removeSpecifiedJoinColumn(0); + assertEquals(2, joinTableResource.getJoinColumns().size()); + assertEquals("BAR", joinTableResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getJoinColumns().get(1).getName()); + + xmlJoinTable.removeSpecifiedJoinColumn(0); + assertEquals(1, joinTableResource.getJoinColumns().size()); + assertEquals("BAZ", joinTableResource.getJoinColumns().get(0).getName()); + + xmlJoinTable.removeSpecifiedJoinColumn(0); + assertEquals(0, joinTableResource.getJoinColumns().size()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + xmlJoinTable.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlJoinTable.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlJoinTable.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals(3, joinTableResource.getJoinColumns().size()); + + + xmlJoinTable.moveSpecifiedJoinColumn(2, 0); + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", joinTableResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getJoinColumns().get(2).getName()); + + + xmlJoinTable.moveSpecifiedJoinColumn(0, 1); + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", joinTableResource.getJoinColumns().get(0).getName()); + assertEquals("BAR", joinTableResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getJoinColumns().get(2).getName()); + } + + public void testUpdateInverseJoinColumns() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + manyToMany.setJoinTable(OrmFactory.eINSTANCE.createJoinTableImpl()); + JoinTable joinTableResource = manyToMany.getJoinTable(); + + joinTableResource.getInverseJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + joinTableResource.getInverseJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + joinTableResource.getInverseJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + + joinTableResource.getInverseJoinColumns().get(0).setName("FOO"); + joinTableResource.getInverseJoinColumns().get(1).setName("BAR"); + joinTableResource.getInverseJoinColumns().get(2).setName("BAZ"); + + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getInverseJoinColumns().move(2, 0); + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getInverseJoinColumns().move(0, 1); + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getInverseJoinColumns().remove(1); + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getInverseJoinColumns().remove(1); + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getInverseJoinColumns().remove(0); + assertFalse(xmlJoinTable.specifiedInverseJoinColumns().hasNext()); + } + + public void testAddSpecifiedInverseJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + XmlJoinColumn joinColumn = xmlJoinTable.addSpecifiedInverseJoinColumn(0); + ormResource().save(null); + joinColumn.setSpecifiedName("FOO"); + ormResource().save(null); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals("FOO", joinTableResource.getInverseJoinColumns().get(0).getName()); + + XmlJoinColumn joinColumn2 = xmlJoinTable.addSpecifiedInverseJoinColumn(0); + ormResource().save(null); + joinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", joinTableResource.getInverseJoinColumns().get(0).getName()); + assertEquals("FOO", joinTableResource.getInverseJoinColumns().get(1).getName()); + + XmlJoinColumn joinColumn3 = xmlJoinTable.addSpecifiedInverseJoinColumn(1); + ormResource().save(null); + joinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", joinTableResource.getInverseJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getInverseJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getInverseJoinColumns().get(2).getName()); + + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedInverseJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + xmlJoinTable.addSpecifiedInverseJoinColumn(0).setSpecifiedName("FOO"); + xmlJoinTable.addSpecifiedInverseJoinColumn(1).setSpecifiedName("BAR"); + xmlJoinTable.addSpecifiedInverseJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals(3, joinTableResource.getInverseJoinColumns().size()); + + xmlJoinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(2, joinTableResource.getInverseJoinColumns().size()); + assertEquals("BAR", joinTableResource.getInverseJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getInverseJoinColumns().get(1).getName()); + + xmlJoinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(1, joinTableResource.getInverseJoinColumns().size()); + assertEquals("BAZ", joinTableResource.getInverseJoinColumns().get(0).getName()); + + xmlJoinTable.removeSpecifiedInverseJoinColumn(0); + assertEquals(0, joinTableResource.getInverseJoinColumns().size()); + } + + public void testMoveSpecifiedInverseJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + + xmlJoinTable.addSpecifiedInverseJoinColumn(0).setSpecifiedName("FOO"); + xmlJoinTable.addSpecifiedInverseJoinColumn(1).setSpecifiedName("BAR"); + xmlJoinTable.addSpecifiedInverseJoinColumn(2).setSpecifiedName("BAZ"); + + JoinTable joinTableResource = manyToMany.getJoinTable(); + assertEquals(3, joinTableResource.getInverseJoinColumns().size()); + + + xmlJoinTable.moveSpecifiedInverseJoinColumn(2, 0); + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", joinTableResource.getInverseJoinColumns().get(0).getName()); + assertEquals("BAZ", joinTableResource.getInverseJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getInverseJoinColumns().get(2).getName()); + + + xmlJoinTable.moveSpecifiedInverseJoinColumn(0, 1); + joinColumns = xmlJoinTable.specifiedInverseJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", joinTableResource.getInverseJoinColumns().get(0).getName()); + assertEquals("BAR", joinTableResource.getInverseJoinColumns().get(1).getName()); + assertEquals("FOO", joinTableResource.getInverseJoinColumns().get(2).getName()); + } + + public void testUpdateJoinColumns() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + XmlJoinTable xmlJoinTable = xmlManyToManyMapping.getJoinTable(); + manyToMany.setJoinTable(OrmFactory.eINSTANCE.createJoinTableImpl()); + JoinTable joinTableResource = manyToMany.getJoinTable(); + + joinTableResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + joinTableResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + joinTableResource.getJoinColumns().add(OrmFactory.eINSTANCE.createJoinColumnImpl()); + + joinTableResource.getJoinColumns().get(0).setName("FOO"); + joinTableResource.getJoinColumns().get(1).setName("BAR"); + joinTableResource.getJoinColumns().get(2).setName("BAZ"); + + ListIterator<XmlJoinColumn> joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getJoinColumns().move(2, 0); + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getJoinColumns().move(0, 1); + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getJoinColumns().remove(1); + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getJoinColumns().remove(1); + joinColumns = xmlJoinTable.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertFalse(joinColumns.hasNext()); + + joinTableResource.getJoinColumns().remove(0); + assertFalse(xmlJoinTable.specifiedJoinColumns().hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToManyMappingTests.java new file mode 100644 index 0000000000..5fd5d44bc5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToManyMappingTests.java @@ -0,0 +1,349 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.orm.XmlManyToManyMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.ManyToMany; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlManyToManyMappingTests extends ContextModelTestCase +{ + public XmlManyToManyMappingTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertEquals("manyToManyMapping", xmlManyToManyMapping.getName()); + assertEquals("manyToManyMapping", manyToMany.getName()); + + //set name in the resource model, verify context model updated + manyToMany.setName("newName"); + assertEquals("newName", xmlManyToManyMapping.getName()); + assertEquals("newName", manyToMany.getName()); + + //set name to null in the resource model + manyToMany.setName(null); + assertNull(xmlManyToManyMapping.getName()); + assertNull(manyToMany.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertEquals("manyToManyMapping", xmlManyToManyMapping.getName()); + assertEquals("manyToManyMapping", manyToMany.getName()); + + //set name in the context model, verify resource model updated + xmlManyToManyMapping.setName("newName"); + assertEquals("newName", xmlManyToManyMapping.getName()); + assertEquals("newName", manyToMany.getName()); + + //set name to null in the context model + xmlManyToManyMapping.setName(null); + assertNull(xmlManyToManyMapping.getName()); + assertNull(manyToMany.getName()); + } + + public void testUpdateTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + manyToMany.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToMany.getTargetEntity()); + + //set target entity to null in the resource model + manyToMany.setTargetEntity(null); + assertNull(xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + } + + public void testModifyTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + xmlManyToManyMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToMany.getTargetEntity()); + + //set target entity to null in the context model + xmlManyToManyMapping.setSpecifiedTargetEntity(null); + assertNull(xmlManyToManyMapping.getSpecifiedTargetEntity()); + assertNull(manyToMany.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToManyResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getSpecifiedFetch()); + assertNull(manyToManyResource.getFetch()); + + //set fetch in the resource model, verify context model updated + manyToManyResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER); + assertEquals(FetchType.EAGER, xmlManyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, manyToManyResource.getFetch()); + + manyToManyResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY); + assertEquals(FetchType.LAZY, xmlManyToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, manyToManyResource.getFetch()); + + //set fetch to null in the resource model + manyToManyResource.setFetch(null); + assertNull(xmlManyToManyMapping.getSpecifiedFetch()); + assertNull(manyToManyResource.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToManyResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getSpecifiedFetch()); + assertNull(manyToManyResource.getFetch()); + + //set fetch in the context model, verify resource model updated + xmlManyToManyMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, manyToManyResource.getFetch()); + assertEquals(FetchType.EAGER, xmlManyToManyMapping.getSpecifiedFetch()); + + xmlManyToManyMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, manyToManyResource.getFetch()); + assertEquals(FetchType.LAZY, xmlManyToManyMapping.getSpecifiedFetch()); + + //set fetch to null in the context model + xmlManyToManyMapping.setSpecifiedFetch(null); + assertNull(manyToManyResource.getFetch()); + assertNull(xmlManyToManyMapping.getSpecifiedFetch()); + } + + public void testUpdateMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + manyToMany.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlManyToManyMapping.getMappedBy()); + assertEquals("newMappedBy", manyToMany.getMappedBy()); + + //setmappedBy to null in the resource model + manyToMany.setMappedBy(null); + assertNull(xmlManyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + xmlManyToManyMapping.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlManyToManyMapping.getMappedBy()); + assertEquals("newMappedBy", manyToMany.getMappedBy()); + + //set mappedBy to null in the context model + xmlManyToManyMapping.setMappedBy(null); + assertNull(xmlManyToManyMapping.getMappedBy()); + assertNull(manyToMany.getMappedBy()); + } + + public void testUpdateMapKey() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getMapKey()); + assertNull(manyToMany.getMapKey()); + + //set mapKey in the resource model, verify context model does not change + manyToMany.setMapKey(OrmFactory.eINSTANCE.createMapKeyImpl()); + assertNull(xmlManyToManyMapping.getMapKey()); + assertNotNull(manyToMany.getMapKey()); + + //set mapKey name in the resource model, verify context model updated + manyToMany.getMapKey().setName("myMapKey"); + assertEquals("myMapKey", xmlManyToManyMapping.getMapKey()); + assertEquals("myMapKey", manyToMany.getMapKey().getName()); + + //set mapKey name to null in the resource model + manyToMany.getMapKey().setName(null); + assertNull(xmlManyToManyMapping.getMapKey()); + assertNull(manyToMany.getMapKey().getName()); + + manyToMany.getMapKey().setName("myMapKey"); + manyToMany.setMapKey(null); + assertNull(xmlManyToManyMapping.getMapKey()); + assertNull(manyToMany.getMapKey()); + } + + public void testModifyMapKey() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getMapKey()); + assertNull(manyToMany.getMapKey()); + + //set mapKey in the context model, verify resource model updated + xmlManyToManyMapping.setMapKey("myMapKey"); + assertEquals("myMapKey", xmlManyToManyMapping.getMapKey()); + assertEquals("myMapKey", manyToMany.getMapKey().getName()); + + //set mapKey to null in the context model + xmlManyToManyMapping.setMapKey(null); + assertNull(xmlManyToManyMapping.getMapKey()); + assertNull(manyToMany.getMapKey()); + } + + public void testUpdateOrderBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getOrderBy()); + assertNull(manyToMany.getOrderBy()); + + //set orderBy in the resource model, verify context model updated + manyToMany.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", xmlManyToManyMapping.getOrderBy()); + assertEquals("newOrderBy", manyToMany.getOrderBy()); + + //set orderBy to null in the resource model + manyToMany.setOrderBy(null); + assertNull(xmlManyToManyMapping.getOrderBy()); + assertNull(manyToMany.getOrderBy()); + } + + public void testModifyOrderBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + ManyToMany manyToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToManys().get(0); + + assertNull(xmlManyToManyMapping.getOrderBy()); + assertNull(manyToMany.getOrderBy()); + + //set mappedBy in the context model, verify resource model updated + xmlManyToManyMapping.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", xmlManyToManyMapping.getOrderBy()); + assertEquals("newOrderBy", manyToMany.getOrderBy()); + + //set mappedBy to null in the context model + xmlManyToManyMapping.setOrderBy(null); + assertNull(xmlManyToManyMapping.getOrderBy()); + assertNull(manyToMany.getOrderBy()); + } + + public void testIsNoOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + + assertTrue(xmlManyToManyMapping.isNoOrdering()); + + xmlManyToManyMapping.setOrderBy("foo"); + assertFalse(xmlManyToManyMapping.isNoOrdering()); + + xmlManyToManyMapping.setOrderBy(null); + assertTrue(xmlManyToManyMapping.isNoOrdering()); + } + + public void testSetNoOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + + assertTrue(xmlManyToManyMapping.isNoOrdering()); + + xmlManyToManyMapping.setOrderBy("foo"); + assertFalse(xmlManyToManyMapping.isNoOrdering()); + + xmlManyToManyMapping.setNoOrdering(); + assertTrue(xmlManyToManyMapping.isNoOrdering()); + assertNull(xmlManyToManyMapping.getOrderBy()); + } +//TODO +// public boolean isOrderByPk() { +// return "".equals(getOrderBy()); +// } +// +// public void setOrderByPk() { +// setOrderBy(""); +// } + + public void testIsCustomOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, "manyToManyMapping"); + XmlManyToManyMapping xmlManyToManyMapping = (XmlManyToManyMapping) xmlPersistentAttribute.getMapping(); + + assertFalse(xmlManyToManyMapping.isCustomOrdering()); + + xmlManyToManyMapping.setOrderBy("foo"); + assertTrue(xmlManyToManyMapping.isCustomOrdering()); + + xmlManyToManyMapping.setOrderBy(null); + assertFalse(xmlManyToManyMapping.isCustomOrdering()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToOneMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToOneMappingTests.java new file mode 100644 index 0000000000..bbb80b4be1 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlManyToOneMappingTests.java @@ -0,0 +1,571 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.orm.XmlCascade; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlManyToOneMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.ManyToOne; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlManyToOneMappingTests extends ContextModelTestCase +{ + public XmlManyToOneMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createManyToOneAnnotation() throws Exception{ + this.createAnnotationAndMembers("ManyToOne", + "Class targetEntity() default void.class;" + + "CascadeType[] cascade() default {};" + + "FetchType fetch() default EAGER;" + + "boolean optional() default true;"); + } + + private void createJoinColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("JoinColumn", + "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + } + + private IType createTestEntityManyToOneMapping() throws Exception { + createEntityAnnotation(); + createManyToOneAnnotation(); + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.MANY_TO_ONE, JPA.JOIN_COLUMN, JPA.FETCH_TYPE, JPA.CASCADE_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @ManyToOne(fetch=FetchType.LAZY, optional=false, targetEntity=Address.class, cascade={CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH})"); + sb.append(CR); + sb.append(" @JoinColumn(name=\"MY_COLUMN\", referencedColumnName=\"MY_REFERENCED_COLUMN\", unique=true, nullable=false, insertable=false, updatable=false, columnDefinition=\"COLUMN_DEFINITION\", table=\"MY_TABLE\")"); + sb.append(CR); + sb.append(" private Address address;").append(CR); + sb.append(CR); + sb.append(" @Id"); + } + }); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertEquals("manyToOneMapping", xmlManyToOneMapping.getName()); + assertEquals("manyToOneMapping", manyToOne.getName()); + + //set name in the resource model, verify context model updated + manyToOne.setName("newName"); + assertEquals("newName", xmlManyToOneMapping.getName()); + assertEquals("newName", manyToOne.getName()); + + //set name to null in the resource model + manyToOne.setName(null); + assertNull(xmlManyToOneMapping.getName()); + assertNull(manyToOne.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertEquals("manyToOneMapping", xmlManyToOneMapping.getName()); + assertEquals("manyToOneMapping", manyToOne.getName()); + + //set name in the context model, verify resource model updated + xmlManyToOneMapping.setName("newName"); + assertEquals("newName", xmlManyToOneMapping.getName()); + assertEquals("newName", manyToOne.getName()); + + //set name to null in the context model + xmlManyToOneMapping.setName(null); + assertNull(xmlManyToOneMapping.getName()); + assertNull(manyToOne.getName()); + } + + public void testUpdateTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + manyToOne.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToOne.getTargetEntity()); + + //set target entity to null in the resource model + manyToOne.setTargetEntity(null); + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + } + + public void testModifyTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + xmlManyToOneMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", manyToOne.getTargetEntity()); + + //set target entity to null in the context model + xmlManyToOneMapping.setSpecifiedTargetEntity(null); + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertNull(manyToOne.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + assertNull(manyToOneResource.getFetch()); + + //set fetch in the resource model, verify context model updated + manyToOneResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER); + assertEquals(FetchType.EAGER, xmlManyToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, manyToOneResource.getFetch()); + + manyToOneResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY); + assertEquals(FetchType.LAZY, xmlManyToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, manyToOneResource.getFetch()); + + //set fetch to null in the resource model + manyToOneResource.setFetch(null); + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + assertNull(manyToOneResource.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + assertNull(manyToOneResource.getFetch()); + + //set fetch in the context model, verify resource model updated + xmlManyToOneMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, manyToOneResource.getFetch()); + assertEquals(FetchType.EAGER, xmlManyToOneMapping.getSpecifiedFetch()); + + xmlManyToOneMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, manyToOneResource.getFetch()); + assertEquals(FetchType.LAZY, xmlManyToOneMapping.getSpecifiedFetch()); + + //set fetch to null in the context model + xmlManyToOneMapping.setSpecifiedFetch(null); + assertNull(manyToOneResource.getFetch()); + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + } + + public void testUpdateSpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOneResource.getOptional()); + + //set optional in the resource model, verify context model updated + manyToOneResource.setOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlManyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, manyToOneResource.getOptional()); + + manyToOneResource.setOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlManyToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, manyToOneResource.getOptional()); + + //set optional to null in the resource model + manyToOneResource.setOptional(null); + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOneResource.getOptional()); + } + + public void testModifySpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + assertNull(manyToOneResource.getOptional()); + + //set optional in the context model, verify resource model updated + xmlManyToOneMapping.setSpecifiedOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, manyToOneResource.getOptional()); + assertEquals(Boolean.TRUE, xmlManyToOneMapping.getSpecifiedOptional()); + + xmlManyToOneMapping.setSpecifiedOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, manyToOneResource.getOptional()); + assertEquals(Boolean.FALSE, xmlManyToOneMapping.getSpecifiedOptional()); + + //set optional to null in the context model + xmlManyToOneMapping.setSpecifiedOptional(null); + assertNull(manyToOneResource.getOptional()); + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + XmlJoinColumn joinColumn = xmlManyToOneMapping.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn.setSpecifiedName("FOO"); + ormResource().save(null); + + assertEquals("FOO", manyToOneResource.getJoinColumns().get(0).getName()); + + XmlJoinColumn joinColumn2 = xmlManyToOneMapping.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", manyToOneResource.getJoinColumns().get(0).getName()); + assertEquals("FOO", manyToOneResource.getJoinColumns().get(1).getName()); + + XmlJoinColumn joinColumn3 = xmlManyToOneMapping.addSpecifiedJoinColumn(1); + ormResource().save(null); + joinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", manyToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", manyToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", manyToOneResource.getJoinColumns().get(2).getName()); + + ListIterator<XmlJoinColumn> joinColumns = xmlManyToOneMapping.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = xmlManyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + xmlManyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlManyToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlManyToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, manyToOneResource.getJoinColumns().size()); + + xmlManyToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(2, manyToOneResource.getJoinColumns().size()); + assertEquals("BAR", manyToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", manyToOneResource.getJoinColumns().get(1).getName()); + + xmlManyToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(1, manyToOneResource.getJoinColumns().size()); + assertEquals("BAZ", manyToOneResource.getJoinColumns().get(0).getName()); + + xmlManyToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(0, manyToOneResource.getJoinColumns().size()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "manyToOneMapping"); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + ManyToOne manyToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getManyToOnes().get(0); + + xmlManyToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlManyToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlManyToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, manyToOneResource.getJoinColumns().size()); + + + xmlManyToOneMapping.moveSpecifiedJoinColumn(2, 0); + ListIterator<XmlJoinColumn> joinColumns = xmlManyToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", manyToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", manyToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", manyToOneResource.getJoinColumns().get(2).getName()); + + + xmlManyToOneMapping.moveSpecifiedJoinColumn(0, 1); + joinColumns = xmlManyToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", manyToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAR", manyToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", manyToOneResource.getJoinColumns().get(2).getName()); + } + + + public void testManyToOneMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityManyToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlManyToOneMapping.getName()); + + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertEquals(FetchType.EAGER, xmlManyToOneMapping.getFetch()); + assertEquals(Boolean.TRUE, xmlManyToOneMapping.getOptional()); + assertNull(xmlManyToOneMapping.getTargetEntity()); + + + assertFalse(xmlManyToOneMapping.specifiedJoinColumns().hasNext()); + //TODO default joinColumns + //assertTrue(xmlManyToOneMapping.defaultJoinColumns().hasNext()); + + + XmlCascade xmlCascade = xmlManyToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityManyToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlManyToOneMapping.getName()); + assertEquals(FetchType.LAZY, xmlManyToOneMapping.getSpecifiedFetch()); + assertEquals(Boolean.FALSE, xmlManyToOneMapping.getSpecifiedOptional()); + assertEquals("Address", xmlManyToOneMapping.getSpecifiedTargetEntity()); + + XmlJoinColumn xmlJoinColumn = xmlManyToOneMapping.specifiedJoinColumns().next(); + assertEquals("MY_COLUMN", xmlJoinColumn.getSpecifiedName()); + assertEquals("MY_REFERENCED_COLUMN", xmlJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUpdatable()); + assertEquals("COLUMN_DEFINITION", xmlJoinColumn.getColumnDefinition()); + assertEquals("MY_TABLE", xmlJoinColumn.getSpecifiedTable()); + + XmlCascade xmlCascade = xmlManyToOneMapping.getCascade(); + assertTrue(xmlCascade.isAll()); + assertTrue(xmlCascade.isMerge()); + assertTrue(xmlCascade.isPersist()); + assertTrue(xmlCascade.isRemove()); + assertTrue(xmlCascade.isRefresh()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityManyToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlManyToOneMapping.getName()); + assertEquals(FetchType.EAGER, xmlManyToOneMapping.getSpecifiedFetch()); + assertEquals(Boolean.TRUE, xmlManyToOneMapping.getSpecifiedOptional()); + //TODO hmm, is this correct? + assertEquals("test.Address", xmlManyToOneMapping.getSpecifiedTargetEntity()); + + XmlJoinColumn xmlJoinColumn = xmlManyToOneMapping.specifiedJoinColumns().next(); + //TODO java default columns name in JavaSingleRelationshipMapping.JoinColumnOwner + //assertEquals("address", xmlJoinColumn.getSpecifiedName()); + //assertEquals("address", xmlJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(xmlJoinColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlJoinColumn.getSpecifiedTable()); + + XmlCascade xmlCascade = xmlManyToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityManyToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, "address"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlManyToOneMapping xmlManyToOneMapping = (XmlManyToOneMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("address", xmlManyToOneMapping.getName()); + assertNull(xmlManyToOneMapping.getSpecifiedFetch()); + assertNull(xmlManyToOneMapping.getSpecifiedOptional()); + assertNull(xmlManyToOneMapping.getSpecifiedTargetEntity()); + assertEquals(FetchType.EAGER, xmlManyToOneMapping.getFetch()); + assertEquals(Boolean.TRUE, xmlManyToOneMapping.getOptional()); + //TODO default target entity in xml + //assertEquals("test.Address", xmlManyToOneMapping.getDefaultTargetEntity()); + + assertFalse(xmlManyToOneMapping.specifiedJoinColumns().hasNext()); + + //TODO default join columns for specified xmlManyToOne mapping +// XmlJoinColumn xmlJoinColumn = xmlManyToOneMapping.defaultJoinColumns().next(); +// assertNull(xmlJoinColumn.getSpecifiedName()); +// assertNull(xmlJoinColumn.getSpecifiedReferencedColumnName()); +// assertNull(xmlJoinColumn.getSpecifiedUnique()); +// assertNull(xmlJoinColumn.getSpecifiedNullable()); +// assertNull(xmlJoinColumn.getSpecifiedInsertable()); +// assertNull(xmlJoinColumn.getSpecifiedUpdatable()); +// assertNull(xmlJoinColumn.getColumnDefinition()); +// assertNull(xmlJoinColumn.getSpecifiedTable()); +// +// assertEquals("address", xmlJoinColumn.getDefaultName()); +// assertEquals("address", xmlJoinColumn.getDefaultReferencedColumnName()); +// assertEquals(Boolean.FALSE, xmlJoinColumn.getDefaultUnique()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultNullable()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultInsertable()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultUpdatable()); +// assertEquals(null, xmlJoinColumn.getColumnDefinition()); +// assertEquals(TYPE_NAME, xmlJoinColumn.getDefaultTable()); + + XmlCascade xmlCascade = xmlManyToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + + //3 things tested above + //1. virtual mapping metadata complete=false - defaults are taken from the java annotations + //2. virtual mapping metadata complete=true - defaults are taken from java defaults,annotations ignored + //3. specified mapping (metadata complete=true/false - defaults are taken from java annotations + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlMappedSuperclassTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlMappedSuperclassTests.java new file mode 100644 index 0000000000..66d5b69558 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlMappedSuperclassTests.java @@ -0,0 +1,356 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.orm.XmlEmbeddable; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlMappedSuperclass; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.Embeddable; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.MappedSuperclass; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlMappedSuperclassTests extends ContextModelTestCase +{ + public XmlMappedSuperclassTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals("model.Foo", mappedSuperclassResource.getClassName()); + + //set class in the resource model, verify context model updated + mappedSuperclassResource.setClassName("com.Bar"); + ormResource().save(null); + assertEquals("com.Bar", xmlMappedSuperclass.getClass_()); + assertEquals("com.Bar", mappedSuperclassResource.getClassName()); + + //set class to null in the resource model + mappedSuperclassResource.setClassName(null); + assertNull(xmlMappedSuperclass.getClass_()); + assertNull(mappedSuperclassResource.getClassName()); + } + + public void testModifyClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertEquals("model.Foo", xmlMappedSuperclass.getClass_()); + assertEquals("model.Foo", mappedSuperclassResource.getClassName()); + + //set class in the context model, verify resource model modified + xmlMappedSuperclass.setClass("com.Bar"); + assertEquals("com.Bar", xmlMappedSuperclass.getClass_()); + assertEquals("com.Bar", mappedSuperclassResource.getClassName()); + + //set class to null in the context model + xmlMappedSuperclass.setClass(null); + assertNull(xmlMappedSuperclass.getClass_()); + assertNull(mappedSuperclassResource.getClassName()); + } + //TODO add tests for setting the className when the package is set on entity-mappings + + public void testUpdateSpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertNull(xmlMappedSuperclass.getSpecifiedAccess()); + assertNull(mappedSuperclassResource.getAccess()); + + //set access in the resource model, verify context model updated + mappedSuperclassResource.setAccess(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD); + assertEquals(AccessType.FIELD, xmlMappedSuperclass.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.FIELD, mappedSuperclassResource.getAccess()); + + //set access to null in the resource model + mappedSuperclassResource.setAccess(null); + assertNull(xmlMappedSuperclass.getSpecifiedAccess()); + assertNull(mappedSuperclassResource.getAccess()); + } + + public void testModifySpecifiedAccess() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertNull(xmlMappedSuperclass.getSpecifiedAccess()); + assertNull(mappedSuperclassResource.getAccess()); + + //set access in the context model, verify resource model modified + xmlMappedSuperclass.setSpecifiedAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, xmlMappedSuperclass.getSpecifiedAccess()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, mappedSuperclassResource.getAccess()); + + //set access to null in the context model + xmlMappedSuperclass.setSpecifiedAccess(null); + assertNull(xmlMappedSuperclass.getSpecifiedAccess()); + assertNull(mappedSuperclassResource.getAccess()); + } + //TODO test default access from + //underlying java + //persistence-unit-defaults + //entity-mappings + //with xml-mapping-metadata-complete set + + public void testUpdateSpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + //set metadata-complete in the resource model, verify context model updated + mappedSuperclassResource.setMetadataComplete(true); + assertTrue(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertTrue(mappedSuperclassResource.getMetadataComplete()); + + //set access to false in the resource model + mappedSuperclassResource.setMetadataComplete(false); + assertFalse(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertFalse(mappedSuperclassResource.getMetadataComplete()); + + mappedSuperclassResource.setMetadataComplete(null); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + } + + public void testModifySpecifiedMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + //set access in the context model, verify resource model modified + xmlMappedSuperclass.setSpecifiedMetadataComplete(Boolean.TRUE); + assertTrue(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertTrue(mappedSuperclassResource.getMetadataComplete()); + + //set access to null in the context model + xmlMappedSuperclass.setSpecifiedMetadataComplete(Boolean.FALSE); + assertFalse(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertFalse(mappedSuperclassResource.getMetadataComplete()); + + xmlMappedSuperclass.setSpecifiedMetadataComplete(null); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + } + + public void testUpdateDefaultMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertFalse(xmlMappedSuperclass.isDefaultMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlMappedSuperclass.isDefaultMetadataComplete()); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertFalse(xmlMappedSuperclass.isDefaultMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + } + + public void testUpdateMetadataComplete() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertFalse(xmlMappedSuperclass.isMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + ormResource().getEntityMappings().setPersistenceUnitMetadata(OrmFactory.eINSTANCE.createPersistenceUnitMetadata()); + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + assertTrue(xmlMappedSuperclass.isMetadataComplete()); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + + ormResource().getEntityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertFalse(xmlMappedSuperclass.isMetadataComplete()); + assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + assertNull(mappedSuperclassResource.getMetadataComplete()); + } + + + public void testMakeMappedSuperclassEntity() throws Exception { + XmlPersistentType mappedSuperclassPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass mappedSuperclass = (XmlMappedSuperclass) mappedSuperclassPersistentType.getMapping(); + mappedSuperclass.setSpecifiedAccess(AccessType.PROPERTY); + mappedSuperclass.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + mappedSuperclassPersistentType.setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", entity.getClassName()); + assertEquals(Boolean.TRUE, entity.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, entity.getAccess()); + assertNull(entity.getDiscriminatorValue()); + assertNull(entity.getName()); + + XmlEntity xmlEntity = (XmlEntity) mappedSuperclassPersistentType.getMapping(); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals(Boolean.TRUE, xmlEntity.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEntity.getSpecifiedAccess()); + } + + //test with 2 MappedSuperclasses, make the first one an Entity so it has to move to the end of the list + public void testMakeMappedSuperclassEntity2() throws Exception { + XmlPersistentType mappedSuperclassPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo2"); + XmlMappedSuperclass mappedSuperclass = (XmlMappedSuperclass) mappedSuperclassPersistentType.getMapping(); + mappedSuperclass.setSpecifiedAccess(AccessType.PROPERTY); + mappedSuperclass.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + mappedSuperclassPersistentType.setMappingKey(IMappingKeys.ENTITY_TYPE_MAPPING_KEY); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("model.Foo", entity.getClassName()); + assertEquals(Boolean.TRUE, entity.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, entity.getAccess()); + assertNull(entity.getDiscriminatorValue()); + assertNull(entity.getName()); + + XmlEntity xmlEntity = (XmlEntity) mappedSuperclassPersistentType.getMapping(); + assertEquals("model.Foo", xmlEntity.getClass_()); + assertEquals(Boolean.TRUE, xmlEntity.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEntity.getSpecifiedAccess()); + + ListIterator<XmlPersistentType> persistentTypes = entityMappings().xmlPersistentTypes(); + assertEquals(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + assertEquals(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + } + + public void testMakeMappedSuperclassEmbeddable() throws Exception { + XmlPersistentType mappedSuperclassPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass mappedSuperclass = (XmlMappedSuperclass) mappedSuperclassPersistentType.getMapping(); + mappedSuperclass.setSpecifiedAccess(AccessType.PROPERTY); + mappedSuperclass.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + mappedSuperclassPersistentType.setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + ormResource().save(null); + + Embeddable embeddable = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", embeddable.getClassName()); + assertEquals(Boolean.TRUE, embeddable.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, embeddable.getAccess()); + + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) mappedSuperclassPersistentType.getMapping(); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals(Boolean.TRUE, xmlEmbeddable.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEmbeddable.getSpecifiedAccess()); + } + //test with 2 MappedSuperclasses, make the first one an Embeddable so it has to move to the end of the list + public void testMakeMappedSuperclassEmbeddable2() throws Exception { + XmlPersistentType mappedSuperclassPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo2"); + XmlMappedSuperclass mappedSuperclass = (XmlMappedSuperclass) mappedSuperclassPersistentType.getMapping(); + mappedSuperclass.setSpecifiedAccess(AccessType.PROPERTY); + mappedSuperclass.setSpecifiedMetadataComplete(Boolean.TRUE); + ormResource().save(null); + + mappedSuperclassPersistentType.setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + ormResource().save(null); + + Embeddable embeddable = ormResource().getEntityMappings().getEmbeddables().get(0); + assertEquals("model.Foo", embeddable.getClassName()); + assertEquals(Boolean.TRUE, embeddable.getMetadataComplete()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.AccessType.PROPERTY, embeddable.getAccess()); + + XmlEmbeddable xmlEmbeddable = (XmlEmbeddable) mappedSuperclassPersistentType.getMapping(); + assertEquals("model.Foo", xmlEmbeddable.getClass_()); + assertEquals(Boolean.TRUE, xmlEmbeddable.getSpecifiedMetadataComplete()); + assertEquals(AccessType.PROPERTY, xmlEmbeddable.getSpecifiedAccess()); + + ListIterator<XmlPersistentType> persistentTypes = entityMappings().xmlPersistentTypes(); + assertEquals(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + assertEquals(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, persistentTypes.next().mappingKey()); + } + + public void testUpdateIdClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + + assertNull(xmlMappedSuperclass.getIdClass()); + assertNull(mappedSuperclassResource.getIdClass()); + + mappedSuperclassResource.setIdClass(OrmFactory.eINSTANCE.createIdClass()); + + assertNull(xmlMappedSuperclass.getIdClass()); + assertNotNull(mappedSuperclassResource.getIdClass()); + + mappedSuperclassResource.getIdClass().setClassName("model.Foo"); + assertEquals("model.Foo", xmlMappedSuperclass.getIdClass()); + assertEquals("model.Foo", mappedSuperclassResource.getIdClass().getClassName()); + + //test setting @IdClass value to null, id-class tag is not removed + mappedSuperclassResource.getIdClass().setClassName(null); + assertNull(xmlMappedSuperclass.getIdClass()); + assertNotNull(mappedSuperclassResource.getIdClass()); + + //reset @IdClass value and then remove id-class tag + mappedSuperclassResource.setIdClass(OrmFactory.eINSTANCE.createIdClass()); + mappedSuperclassResource.getIdClass().setClassName("model.Foo"); + mappedSuperclassResource.setIdClass(null); + + assertNull(xmlMappedSuperclass.getIdClass()); + assertNull(mappedSuperclassResource.getIdClass()); + } + + public void testModifyIdClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo"); + XmlMappedSuperclass xmlMappedSuperclass = (XmlMappedSuperclass) xmlPersistentType.getMapping(); + MappedSuperclass mappedSuperclassResource = ormResource().getEntityMappings().getMappedSuperclasses().get(0); assertNull(xmlMappedSuperclass.getSpecifiedMetadataComplete()); + + assertNull(xmlMappedSuperclass.getIdClass()); + assertNull(mappedSuperclassResource.getIdClass()); + + xmlMappedSuperclass.setIdClass("model.Foo"); + assertEquals("model.Foo", mappedSuperclassResource.getIdClass().getClassName()); + assertEquals("model.Foo", xmlMappedSuperclass.getIdClass()); + + xmlMappedSuperclass.setIdClass(null); + assertNull(xmlMappedSuperclass.getIdClass()); + assertNull(mappedSuperclassResource.getIdClass()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedNativeQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedNativeQueryTests.java new file mode 100644 index 0000000000..7ada19bb4d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedNativeQueryTests.java @@ -0,0 +1,358 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedNativeQuery; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlQueryHint; +import org.eclipse.jpt.core.internal.resource.orm.NamedNativeQuery; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlNamedNativeQueryTests extends ContextModelTestCase +{ + public XmlNamedNativeQueryTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertNull(xmlNamedNativeQuery.getName()); + assertNull(namedNativeQueryResource.getName()); + + //set name in the resource model, verify context model updated + namedNativeQueryResource.setName("newName"); + assertEquals("newName", xmlNamedNativeQuery.getName()); + assertEquals("newName", namedNativeQueryResource.getName()); + + //set name to null in the resource model + namedNativeQueryResource.setName(null); + assertNull(xmlNamedNativeQuery.getName()); + assertNull(namedNativeQueryResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertNull(xmlNamedNativeQuery.getName()); + assertNull(namedNativeQueryResource.getName()); + + //set name in the context model, verify resource model updated + xmlNamedNativeQuery.setName("newName"); + assertEquals("newName", xmlNamedNativeQuery.getName()); + assertEquals("newName", namedNativeQueryResource.getName()); + + //set name to null in the context model + xmlNamedNativeQuery.setName(null); + assertNull(xmlNamedNativeQuery.getName()); + assertNull(namedNativeQueryResource.getName()); + } + + public void testUpdateQuery() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertNull(xmlNamedNativeQuery.getQuery()); + assertNull(namedNativeQueryResource.getQuery()); + + //set name in the resource model, verify context model updated + namedNativeQueryResource.setQuery("newName"); + assertEquals("newName", xmlNamedNativeQuery.getQuery()); + assertEquals("newName", namedNativeQueryResource.getQuery()); + + //set name to null in the resource model + namedNativeQueryResource.setQuery(null); + assertNull(xmlNamedNativeQuery.getQuery()); + assertNull(namedNativeQueryResource.getQuery()); + } + + public void testModifyQuery() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertNull(xmlNamedNativeQuery.getQuery()); + assertNull(namedNativeQueryResource.getQuery()); + + //set name in the context model, verify resource model updated + xmlNamedNativeQuery.setQuery("newName"); + assertEquals("newName", xmlNamedNativeQuery.getQuery()); + assertEquals("newName", namedNativeQueryResource.getQuery()); + + //set name to null in the context model + xmlNamedNativeQuery.setQuery(null); + assertNull(xmlNamedNativeQuery.getQuery()); + assertNull(namedNativeQueryResource.getQuery()); + } + + public void testAddHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + XmlQueryHint queryHint = xmlNamedNativeQuery.addHint(0); + queryHint.setName("FOO"); + + assertEquals("FOO", namedNativeQueryResource.getHints().get(0).getName()); + + XmlQueryHint queryHint2 = xmlNamedNativeQuery.addHint(0); + queryHint2.setName("BAR"); + + assertEquals("BAR", namedNativeQueryResource.getHints().get(0).getName()); + assertEquals("FOO", namedNativeQueryResource.getHints().get(1).getName()); + + XmlQueryHint queryHint3 = xmlNamedNativeQuery.addHint(1); + queryHint3.setName("BAZ"); + + assertEquals("BAR", namedNativeQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedNativeQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedNativeQueryResource.getHints().get(2).getName()); + + ListIterator<XmlQueryHint> queryHints = xmlNamedNativeQuery.hints(); + assertEquals(queryHint2, queryHints.next()); + assertEquals(queryHint3, queryHints.next()); + assertEquals(queryHint, queryHints.next()); + + queryHints = xmlNamedNativeQuery.hints(); + assertEquals("BAR", queryHints.next().getName()); + assertEquals("BAZ", queryHints.next().getName()); + assertEquals("FOO", queryHints.next().getName()); + } + + public void testRemoveHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + xmlNamedNativeQuery.addHint(0).setName("FOO"); + xmlNamedNativeQuery.addHint(1).setName("BAR"); + xmlNamedNativeQuery.addHint(2).setName("BAZ"); + + assertEquals(3, namedNativeQueryResource.getHints().size()); + + xmlNamedNativeQuery.removeHint(0); + assertEquals(2, namedNativeQueryResource.getHints().size()); + assertEquals("BAR", namedNativeQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedNativeQueryResource.getHints().get(1).getName()); + + xmlNamedNativeQuery.removeHint(0); + assertEquals(1, namedNativeQueryResource.getHints().size()); + assertEquals("BAZ", namedNativeQueryResource.getHints().get(0).getName()); + + xmlNamedNativeQuery.removeHint(0); + assertEquals(0, namedNativeQueryResource.getHints().size()); + } + + public void testMoveHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + xmlNamedNativeQuery.addHint(0).setName("FOO"); + xmlNamedNativeQuery.addHint(1).setName("BAR"); + xmlNamedNativeQuery.addHint(2).setName("BAZ"); + + assertEquals(3, namedNativeQueryResource.getHints().size()); + + + xmlNamedNativeQuery.moveHint(2, 0); + ListIterator<XmlQueryHint> hints = xmlNamedNativeQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAR", namedNativeQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedNativeQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedNativeQueryResource.getHints().get(2).getName()); + + + xmlNamedNativeQuery.moveHint(0, 1); + hints = xmlNamedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAZ", namedNativeQueryResource.getHints().get(0).getName()); + assertEquals("BAR", namedNativeQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedNativeQueryResource.getHints().get(2).getName()); + } + + public void testUpdateHints() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + namedNativeQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + namedNativeQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + namedNativeQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + + namedNativeQueryResource.getHints().get(0).setName("FOO"); + namedNativeQueryResource.getHints().get(1).setName("BAR"); + namedNativeQueryResource.getHints().get(2).setName("BAZ"); + + ListIterator<XmlQueryHint> hints = xmlNamedNativeQuery.hints(); + assertEquals("FOO", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedNativeQueryResource.getHints().move(2, 0); + hints = xmlNamedNativeQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedNativeQueryResource.getHints().move(0, 1); + hints = xmlNamedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedNativeQueryResource.getHints().remove(1); + hints = xmlNamedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedNativeQueryResource.getHints().remove(1); + hints = xmlNamedNativeQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedNativeQueryResource.getHints().remove(0); + assertFalse(xmlNamedNativeQuery.hints().hasNext()); + } + + + public void testUpdateResultSetMapping() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertEquals(null, namedNativeQueryResource.getResultSetMapping()); + assertEquals(null, xmlNamedNativeQuery.getResultSetMapping()); + + //set name in the resource model, verify context model updated + namedNativeQueryResource.setResultSetMapping("foo"); + assertEquals("foo", namedNativeQueryResource.getResultSetMapping()); + assertEquals("foo", xmlNamedNativeQuery.getResultSetMapping()); + + //set name to null in the resource model + namedNativeQueryResource.setResultSetMapping(null); + assertNull(namedNativeQueryResource.getResultSetMapping()); + assertNull(xmlNamedNativeQuery.getResultSetMapping()); + } + + public void testModifyResultSetMapping() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertEquals(null, namedNativeQueryResource.getResultSetMapping()); + assertEquals(null, xmlNamedNativeQuery.getResultSetMapping()); + + //set name in the context model, verify resource model updated + xmlNamedNativeQuery.setResultSetMapping("foo"); + assertEquals("foo", namedNativeQueryResource.getResultSetMapping()); + assertEquals("foo", xmlNamedNativeQuery.getResultSetMapping()); + + //set name to null in the context model + xmlNamedNativeQuery.setResultSetMapping(null); + assertNull(namedNativeQueryResource.getResultSetMapping()); + assertNull(xmlNamedNativeQuery.getResultSetMapping()); + } + + public void testUpdateResultClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertEquals(null, namedNativeQueryResource.getResultClass()); + assertEquals(null, xmlNamedNativeQuery.getResultClass()); + + //set name in the resource model, verify context model updated + namedNativeQueryResource.setResultClass("foo"); + assertEquals("foo", namedNativeQueryResource.getResultClass()); + assertEquals("foo", xmlNamedNativeQuery.getResultClass()); + + //set name to null in the resource model + namedNativeQueryResource.setResultClass(null); + assertNull(namedNativeQueryResource.getResultClass()); + assertNull(xmlNamedNativeQuery.getResultClass()); + } + + public void testModifyResultClass() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedNativeQuery xmlNamedNativeQuery = xmlEntity.addNamedNativeQuery(0); + + NamedNativeQuery namedNativeQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedNativeQueries().get(0); + + assertEquals(null, namedNativeQueryResource.getResultClass()); + assertEquals(null, xmlNamedNativeQuery.getResultClass()); + + //set name in the context model, verify resource model updated + xmlNamedNativeQuery.setResultClass("foo"); + assertEquals("foo", namedNativeQueryResource.getResultClass()); + assertEquals("foo", xmlNamedNativeQuery.getResultClass()); + + //set name to null in the context model + xmlNamedNativeQuery.setResultClass(null); + assertNull(namedNativeQueryResource.getResultClass()); + assertNull(xmlNamedNativeQuery.getResultClass()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedQueryTests.java new file mode 100644 index 0000000000..dbae9442c9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlNamedQueryTests.java @@ -0,0 +1,273 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlNamedQuery; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlQueryHint; +import org.eclipse.jpt.core.internal.resource.orm.NamedQuery; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlNamedQueryTests extends ContextModelTestCase +{ + public XmlNamedQueryTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + assertNull(xmlNamedQuery.getName()); + assertNull(namedQueryResource.getName()); + + //set name in the resource model, verify context model updated + namedQueryResource.setName("newName"); + assertEquals("newName", xmlNamedQuery.getName()); + assertEquals("newName", namedQueryResource.getName()); + + //set name to null in the resource model + namedQueryResource.setName(null); + assertNull(xmlNamedQuery.getName()); + assertNull(namedQueryResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + assertNull(xmlNamedQuery.getName()); + assertNull(namedQueryResource.getName()); + + //set name in the context model, verify resource model updated + xmlNamedQuery.setName("newName"); + assertEquals("newName", xmlNamedQuery.getName()); + assertEquals("newName", namedQueryResource.getName()); + + //set name to null in the context model + xmlNamedQuery.setName(null); + assertNull(xmlNamedQuery.getName()); + assertNull(namedQueryResource.getName()); + } + + public void testUpdateQuery() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + assertNull(xmlNamedQuery.getQuery()); + assertNull(namedQueryResource.getQuery()); + + //set name in the resource model, verify context model updated + namedQueryResource.setQuery("newName"); + assertEquals("newName", xmlNamedQuery.getQuery()); + assertEquals("newName", namedQueryResource.getQuery()); + + //set name to null in the resource model + namedQueryResource.setQuery(null); + assertNull(xmlNamedQuery.getQuery()); + assertNull(namedQueryResource.getQuery()); + } + + public void testModifyQuery() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + assertNull(xmlNamedQuery.getQuery()); + assertNull(namedQueryResource.getQuery()); + + //set name in the context model, verify resource model updated + xmlNamedQuery.setQuery("newName"); + assertEquals("newName", xmlNamedQuery.getQuery()); + assertEquals("newName", namedQueryResource.getQuery()); + + //set name to null in the context model + xmlNamedQuery.setQuery(null); + assertNull(xmlNamedQuery.getQuery()); + assertNull(namedQueryResource.getQuery()); + } + + public void testAddHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + XmlQueryHint queryHint = xmlNamedQuery.addHint(0); + queryHint.setName("FOO"); + + assertEquals("FOO", namedQueryResource.getHints().get(0).getName()); + + XmlQueryHint queryHint2 = xmlNamedQuery.addHint(0); + queryHint2.setName("BAR"); + + assertEquals("BAR", namedQueryResource.getHints().get(0).getName()); + assertEquals("FOO", namedQueryResource.getHints().get(1).getName()); + + XmlQueryHint queryHint3 = xmlNamedQuery.addHint(1); + queryHint3.setName("BAZ"); + + assertEquals("BAR", namedQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedQueryResource.getHints().get(2).getName()); + + ListIterator<XmlQueryHint> queryHints = xmlNamedQuery.hints(); + assertEquals(queryHint2, queryHints.next()); + assertEquals(queryHint3, queryHints.next()); + assertEquals(queryHint, queryHints.next()); + + queryHints = xmlNamedQuery.hints(); + assertEquals("BAR", queryHints.next().getName()); + assertEquals("BAZ", queryHints.next().getName()); + assertEquals("FOO", queryHints.next().getName()); + } + + public void testRemoveHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + xmlNamedQuery.addHint(0).setName("FOO"); + xmlNamedQuery.addHint(1).setName("BAR"); + xmlNamedQuery.addHint(2).setName("BAZ"); + + assertEquals(3, namedQueryResource.getHints().size()); + + xmlNamedQuery.removeHint(0); + assertEquals(2, namedQueryResource.getHints().size()); + assertEquals("BAR", namedQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedQueryResource.getHints().get(1).getName()); + + xmlNamedQuery.removeHint(0); + assertEquals(1, namedQueryResource.getHints().size()); + assertEquals("BAZ", namedQueryResource.getHints().get(0).getName()); + + xmlNamedQuery.removeHint(0); + assertEquals(0, namedQueryResource.getHints().size()); + } + + public void testMoveHint() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + xmlNamedQuery.addHint(0).setName("FOO"); + xmlNamedQuery.addHint(1).setName("BAR"); + xmlNamedQuery.addHint(2).setName("BAZ"); + + assertEquals(3, namedQueryResource.getHints().size()); + + + xmlNamedQuery.moveHint(2, 0); + ListIterator<XmlQueryHint> hints = xmlNamedQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAR", namedQueryResource.getHints().get(0).getName()); + assertEquals("BAZ", namedQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedQueryResource.getHints().get(2).getName()); + + + xmlNamedQuery.moveHint(0, 1); + hints = xmlNamedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + + assertEquals("BAZ", namedQueryResource.getHints().get(0).getName()); + assertEquals("BAR", namedQueryResource.getHints().get(1).getName()); + assertEquals("FOO", namedQueryResource.getHints().get(2).getName()); + } + + public void testUpdateHints() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlNamedQuery xmlNamedQuery = xmlEntity.addNamedQuery(0); + + NamedQuery namedQueryResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0); + + namedQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + namedQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + namedQueryResource.getHints().add(OrmFactory.eINSTANCE.createQueryHint()); + + namedQueryResource.getHints().get(0).setName("FOO"); + namedQueryResource.getHints().get(1).setName("BAR"); + namedQueryResource.getHints().get(2).setName("BAZ"); + + ListIterator<XmlQueryHint> hints = xmlNamedQuery.hints(); + assertEquals("FOO", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedQueryResource.getHints().move(2, 0); + hints = xmlNamedQuery.hints(); + assertEquals("BAR", hints.next().getName()); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedQueryResource.getHints().move(0, 1); + hints = xmlNamedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("BAR", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedQueryResource.getHints().remove(1); + hints = xmlNamedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertEquals("FOO", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedQueryResource.getHints().remove(1); + hints = xmlNamedQuery.hints(); + assertEquals("BAZ", hints.next().getName()); + assertFalse(hints.hasNext()); + + namedQueryResource.getHints().remove(0); + assertFalse(xmlNamedQuery.hints().hasNext()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToManyMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToManyMappingTests.java new file mode 100644 index 0000000000..af16fd8add --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToManyMappingTests.java @@ -0,0 +1,354 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.orm.XmlOneToManyMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.OneToMany; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlOneToManyMappingTests extends ContextModelTestCase +{ + public XmlOneToManyMappingTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertEquals("oneToManyMapping", xmlOneToManyMapping.getName()); + assertEquals("oneToManyMapping", oneToMany.getName()); + + //set name in the resource model, verify context model updated + oneToMany.setName("newName"); + assertEquals("newName", xmlOneToManyMapping.getName()); + assertEquals("newName", oneToMany.getName()); + + //set name to null in the resource model + oneToMany.setName(null); + assertNull(xmlOneToManyMapping.getName()); + assertNull(oneToMany.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertEquals("oneToManyMapping", xmlOneToManyMapping.getName()); + assertEquals("oneToManyMapping", oneToMany.getName()); + + //set name in the context model, verify resource model updated + xmlOneToManyMapping.setName("newName"); + assertEquals("newName", xmlOneToManyMapping.getName()); + assertEquals("newName", oneToMany.getName()); + + //set name to null in the context model + xmlOneToManyMapping.setName(null); + assertNull(xmlOneToManyMapping.getName()); + assertNull(oneToMany.getName()); + } + + public void testUpdateTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + ormResource().save(null); + + assertNull(xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + oneToMany.setTargetEntity("newTargetEntity"); + ormResource().save(null); + assertEquals("newTargetEntity", xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToMany.getTargetEntity()); + + //set target entity to null in the resource model + oneToMany.setTargetEntity(null); + ormResource().save(null); + assertNull(xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + } + + public void testModifyTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + xmlOneToManyMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToMany.getTargetEntity()); + + //set target entity to null in the context model + xmlOneToManyMapping.setSpecifiedTargetEntity(null); + assertNull(xmlOneToManyMapping.getSpecifiedTargetEntity()); + assertNull(oneToMany.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToManyResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getSpecifiedFetch()); + assertNull(oneToManyResource.getFetch()); + + //set fetch in the resource model, verify context model updated + oneToManyResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER); + assertEquals(FetchType.EAGER, xmlOneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, oneToManyResource.getFetch()); + + oneToManyResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY); + assertEquals(FetchType.LAZY, xmlOneToManyMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, oneToManyResource.getFetch()); + + //set fetch to null in the resource model + oneToManyResource.setFetch(null); + assertNull(xmlOneToManyMapping.getSpecifiedFetch()); + assertNull(oneToManyResource.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToManyResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getSpecifiedFetch()); + assertNull(oneToManyResource.getFetch()); + + //set fetch in the context model, verify resource model updated + xmlOneToManyMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, oneToManyResource.getFetch()); + assertEquals(FetchType.EAGER, xmlOneToManyMapping.getSpecifiedFetch()); + + xmlOneToManyMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, oneToManyResource.getFetch()); + assertEquals(FetchType.LAZY, xmlOneToManyMapping.getSpecifiedFetch()); + + //set fetch to null in the context model + xmlOneToManyMapping.setSpecifiedFetch(null); + assertNull(oneToManyResource.getFetch()); + assertNull(xmlOneToManyMapping.getSpecifiedFetch()); + } + + public void testUpdateMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + oneToMany.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlOneToManyMapping.getMappedBy()); + assertEquals("newMappedBy", oneToMany.getMappedBy()); + + //setmappedBy to null in the resource model + oneToMany.setMappedBy(null); + assertNull(xmlOneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + xmlOneToManyMapping.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlOneToManyMapping.getMappedBy()); + assertEquals("newMappedBy", oneToMany.getMappedBy()); + + //set mappedBy to null in the context model + xmlOneToManyMapping.setMappedBy(null); + assertNull(xmlOneToManyMapping.getMappedBy()); + assertNull(oneToMany.getMappedBy()); + } + + + public void testUpdateMapKey() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getMapKey()); + assertNull(oneToMany.getMapKey()); + + //set mapKey in the resource model, verify context model does not change + oneToMany.setMapKey(OrmFactory.eINSTANCE.createMapKeyImpl()); + assertNull(xmlOneToManyMapping.getMapKey()); + assertNotNull(oneToMany.getMapKey()); + + //set mapKey name in the resource model, verify context model updated + oneToMany.getMapKey().setName("myMapKey"); + assertEquals("myMapKey", xmlOneToManyMapping.getMapKey()); + assertEquals("myMapKey", oneToMany.getMapKey().getName()); + + //set mapKey name to null in the resource model + oneToMany.getMapKey().setName(null); + assertNull(xmlOneToManyMapping.getMapKey()); + assertNull(oneToMany.getMapKey().getName()); + + oneToMany.getMapKey().setName("myMapKey"); + oneToMany.setMapKey(null); + assertNull(xmlOneToManyMapping.getMapKey()); + assertNull(oneToMany.getMapKey()); + } + + public void testModifyMapKey() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getMapKey()); + assertNull(oneToMany.getMapKey()); + + //set mapKey in the context model, verify resource model updated + xmlOneToManyMapping.setMapKey("myMapKey"); + assertEquals("myMapKey", xmlOneToManyMapping.getMapKey()); + assertEquals("myMapKey", oneToMany.getMapKey().getName()); + + //set mapKey to null in the context model + xmlOneToManyMapping.setMapKey(null); + assertNull(xmlOneToManyMapping.getMapKey()); + assertNull(oneToMany.getMapKey()); + } + + public void testUpdateOrderBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getOrderBy()); + assertNull(oneToMany.getOrderBy()); + + //set orderBy in the resource model, verify context model updated + oneToMany.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", xmlOneToManyMapping.getOrderBy()); + assertEquals("newOrderBy", oneToMany.getOrderBy()); + + //set orderBy to null in the resource model + oneToMany.setOrderBy(null); + assertNull(xmlOneToManyMapping.getOrderBy()); + assertNull(oneToMany.getOrderBy()); + } + + public void testModifyOrderBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + OneToMany oneToMany = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToManys().get(0); + + assertNull(xmlOneToManyMapping.getOrderBy()); + assertNull(oneToMany.getOrderBy()); + + //set mappedBy in the context model, verify resource model updated + xmlOneToManyMapping.setOrderBy("newOrderBy"); + assertEquals("newOrderBy", xmlOneToManyMapping.getOrderBy()); + assertEquals("newOrderBy", oneToMany.getOrderBy()); + + //set mappedBy to null in the context model + xmlOneToManyMapping.setOrderBy(null); + assertNull(xmlOneToManyMapping.getOrderBy()); + assertNull(oneToMany.getOrderBy()); + } + + public void testIsNoOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + + assertTrue(xmlOneToManyMapping.isNoOrdering()); + + xmlOneToManyMapping.setOrderBy("foo"); + assertFalse(xmlOneToManyMapping.isNoOrdering()); + + xmlOneToManyMapping.setOrderBy(null); + assertTrue(xmlOneToManyMapping.isNoOrdering()); + } + + public void testSetNoOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + + assertTrue(xmlOneToManyMapping.isNoOrdering()); + + xmlOneToManyMapping.setOrderBy("foo"); + assertFalse(xmlOneToManyMapping.isNoOrdering()); + + xmlOneToManyMapping.setNoOrdering(); + assertTrue(xmlOneToManyMapping.isNoOrdering()); + assertNull(xmlOneToManyMapping.getOrderBy()); + } +//TODO +// public boolean isOrderByPk() { +// return "".equals(getOrderBy()); +// } +// +// public void setOrderByPk() { +// setOrderBy(""); +// } + + public void testIsCustomOrdering() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, "oneToManyMapping"); + XmlOneToManyMapping xmlOneToManyMapping = (XmlOneToManyMapping) xmlPersistentAttribute.getMapping(); + + assertFalse(xmlOneToManyMapping.isCustomOrdering()); + + xmlOneToManyMapping.setOrderBy("foo"); + assertTrue(xmlOneToManyMapping.isCustomOrdering()); + + xmlOneToManyMapping.setOrderBy(null); + assertFalse(xmlOneToManyMapping.isCustomOrdering()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToOneMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToOneMappingTests.java new file mode 100644 index 0000000000..8411098dcb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlOneToOneMappingTests.java @@ -0,0 +1,610 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.FetchType; +import org.eclipse.jpt.core.internal.context.orm.XmlCascade; +import org.eclipse.jpt.core.internal.context.orm.XmlJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlOneToOneMapping; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.OneToOne; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlOneToOneMappingTests extends ContextModelTestCase +{ + public XmlOneToOneMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createOneToOneAnnotation() throws Exception{ + this.createAnnotationAndMembers("OneToOne", + "Class targetEntity() default void.class;" + + "CascadeType[] cascade() default {};" + + "FetchType fetch() default EAGER;" + + "boolean optional() default true;" + + "String mappedBy() default \"\";"); + } + + private void createJoinColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("JoinColumn", + "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + } + + private IType createTestEntityOneToOneMapping() throws Exception { + createEntityAnnotation(); + createOneToOneAnnotation(); + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ONE_TO_ONE, JPA.JOIN_COLUMN, JPA.FETCH_TYPE, JPA.CASCADE_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" @OneToOne(fetch=FetchType.LAZY, optional=false, targetEntity=Address.class, cascade={CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH}, mappedBy=\"foo\")"); + sb.append(CR); + sb.append(" @JoinColumn(name=\"MY_COLUMN\", referencedColumnName=\"MY_REFERENCED_COLUMN\", unique=true, nullable=false, insertable=false, updatable=false, columnDefinition=\"COLUMN_DEFINITION\", table=\"MY_TABLE\")"); + sb.append(CR); + sb.append(" private Address address;").append(CR); + sb.append(CR); + sb.append(" @Id"); + } + }); + } + + private IType createTestTargetEntityAddress() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JPA.ID); + sb.append(";"); + sb.append(CR); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("Address").append(" "); + sb.append("{").append(CR); + sb.append(CR); + sb.append(" @Id").append(CR); + sb.append(" private int id;").append(CR); + sb.append(CR); + sb.append(" private String city;").append(CR); + sb.append(CR); + sb.append(" private String state;").append(CR); + sb.append(CR); + sb.append(" private int zip;").append(CR); + sb.append(CR); + sb.append("}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "Address.java", sourceWriter); + } + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertEquals("oneToOneMapping", xmlOneToOneMapping.getName()); + assertEquals("oneToOneMapping", oneToOne.getName()); + + //set name in the resource model, verify context model updated + oneToOne.setName("newName"); + assertEquals("newName", xmlOneToOneMapping.getName()); + assertEquals("newName", oneToOne.getName()); + + //set name to null in the resource model + oneToOne.setName(null); + assertNull(xmlOneToOneMapping.getName()); + assertNull(oneToOne.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertEquals("oneToOneMapping", xmlOneToOneMapping.getName()); + assertEquals("oneToOneMapping", oneToOne.getName()); + + //set name in the context model, verify resource model updated + xmlOneToOneMapping.setName("newName"); + assertEquals("newName", xmlOneToOneMapping.getName()); + assertEquals("newName", oneToOne.getName()); + + //set name to null in the context model + xmlOneToOneMapping.setName(null); + assertNull(xmlOneToOneMapping.getName()); + assertNull(oneToOne.getName()); + } + + public void testUpdateSpecifiedTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + + //set target entity in the resource model, verify context model updated + oneToOne.setTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToOne.getTargetEntity()); + + //set target entity to null in the resource model + oneToOne.setTargetEntity(null); + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + } + + public void testModifySpecifiedTargetEntity() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + + //set target entity in the context model, verify resource model updated + xmlOneToOneMapping.setSpecifiedTargetEntity("newTargetEntity"); + assertEquals("newTargetEntity", xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertEquals("newTargetEntity", oneToOne.getTargetEntity()); + + //set target entity to null in the context model + xmlOneToOneMapping.setSpecifiedTargetEntity(null); + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(oneToOne.getTargetEntity()); + } + + public void testUpdateSpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOneResource.getFetch()); + + //set fetch in the resource model, verify context model updated + oneToOneResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER); + assertEquals(FetchType.EAGER, xmlOneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, oneToOneResource.getFetch()); + + oneToOneResource.setFetch(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY); + assertEquals(FetchType.LAZY, xmlOneToOneMapping.getSpecifiedFetch()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, oneToOneResource.getFetch()); + + //set fetch to null in the resource model + oneToOneResource.setFetch(null); + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOneResource.getFetch()); + } + + public void testModifySpecifiedFetch() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + assertNull(oneToOneResource.getFetch()); + + //set fetch in the context model, verify resource model updated + xmlOneToOneMapping.setSpecifiedFetch(FetchType.EAGER); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.EAGER, oneToOneResource.getFetch()); + assertEquals(FetchType.EAGER, xmlOneToOneMapping.getSpecifiedFetch()); + + xmlOneToOneMapping.setSpecifiedFetch(FetchType.LAZY); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.FetchType.LAZY, oneToOneResource.getFetch()); + assertEquals(FetchType.LAZY, xmlOneToOneMapping.getSpecifiedFetch()); + + //set fetch to null in the context model + xmlOneToOneMapping.setSpecifiedFetch(null); + assertNull(oneToOneResource.getFetch()); + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + } + + public void testUpdateMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + + //set mappedBy in the resource model, verify context model updated + oneToOne.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlOneToOneMapping.getMappedBy()); + assertEquals("newMappedBy", oneToOne.getMappedBy()); + + //set mappedBy to null in the resource model + oneToOne.setMappedBy(null); + assertNull(xmlOneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + } + + public void testModifyMappedBy() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOne = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + + //set mappedBy in the context model, verify resource model updated + xmlOneToOneMapping.setMappedBy("newMappedBy"); + assertEquals("newMappedBy", xmlOneToOneMapping.getMappedBy()); + assertEquals("newMappedBy", oneToOne.getMappedBy()); + + //set mappedBy to null in the context model + xmlOneToOneMapping.setMappedBy(null); + assertNull(xmlOneToOneMapping.getMappedBy()); + assertNull(oneToOne.getMappedBy()); + } + + + public void testUpdateSpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOneResource.getOptional()); + + //set optional in the resource model, verify context model updated + oneToOneResource.setOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, xmlOneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.TRUE, oneToOneResource.getOptional()); + + oneToOneResource.setOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, xmlOneToOneMapping.getSpecifiedOptional()); + assertEquals(Boolean.FALSE, oneToOneResource.getOptional()); + + //set optional to null in the resource model + oneToOneResource.setOptional(null); + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOneResource.getOptional()); + } + + public void testModifySpecifiedOptional() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + assertNull(oneToOneResource.getOptional()); + + //set optional in the context model, verify resource model updated + xmlOneToOneMapping.setSpecifiedOptional(Boolean.TRUE); + assertEquals(Boolean.TRUE, oneToOneResource.getOptional()); + assertEquals(Boolean.TRUE, xmlOneToOneMapping.getSpecifiedOptional()); + + xmlOneToOneMapping.setSpecifiedOptional(Boolean.FALSE); + assertEquals(Boolean.FALSE, oneToOneResource.getOptional()); + assertEquals(Boolean.FALSE, xmlOneToOneMapping.getSpecifiedOptional()); + + //set optional to null in the context model + xmlOneToOneMapping.setSpecifiedOptional(null); + assertNull(oneToOneResource.getOptional()); + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + } + + public void testAddSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + XmlJoinColumn joinColumn = xmlOneToOneMapping.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn.setSpecifiedName("FOO"); + ormResource().save(null); + + assertEquals("FOO", oneToOneResource.getJoinColumns().get(0).getName()); + + XmlJoinColumn joinColumn2 = xmlOneToOneMapping.addSpecifiedJoinColumn(0); + ormResource().save(null); + joinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", oneToOneResource.getJoinColumns().get(0).getName()); + assertEquals("FOO", oneToOneResource.getJoinColumns().get(1).getName()); + + XmlJoinColumn joinColumn3 = xmlOneToOneMapping.addSpecifiedJoinColumn(1); + ormResource().save(null); + joinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", oneToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", oneToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", oneToOneResource.getJoinColumns().get(2).getName()); + + ListIterator<XmlJoinColumn> joinColumns = xmlOneToOneMapping.specifiedJoinColumns(); + assertEquals(joinColumn2, joinColumns.next()); + assertEquals(joinColumn3, joinColumns.next()); + assertEquals(joinColumn, joinColumns.next()); + + joinColumns = xmlOneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + } + + public void testRemoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + xmlOneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlOneToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlOneToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, oneToOneResource.getJoinColumns().size()); + + xmlOneToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(2, oneToOneResource.getJoinColumns().size()); + assertEquals("BAR", oneToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", oneToOneResource.getJoinColumns().get(1).getName()); + + xmlOneToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(1, oneToOneResource.getJoinColumns().size()); + assertEquals("BAZ", oneToOneResource.getJoinColumns().get(0).getName()); + + xmlOneToOneMapping.removeSpecifiedJoinColumn(0); + assertEquals(0, oneToOneResource.getJoinColumns().size()); + } + + public void testMoveSpecifiedJoinColumn() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "oneToOneMapping"); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + OneToOne oneToOneResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getOneToOnes().get(0); + + xmlOneToOneMapping.addSpecifiedJoinColumn(0).setSpecifiedName("FOO"); + xmlOneToOneMapping.addSpecifiedJoinColumn(1).setSpecifiedName("BAR"); + xmlOneToOneMapping.addSpecifiedJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, oneToOneResource.getJoinColumns().size()); + + + xmlOneToOneMapping.moveSpecifiedJoinColumn(2, 0); + ListIterator<XmlJoinColumn> joinColumns = xmlOneToOneMapping.specifiedJoinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAR", oneToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAZ", oneToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", oneToOneResource.getJoinColumns().get(2).getName()); + + + xmlOneToOneMapping.moveSpecifiedJoinColumn(0, 1); + joinColumns = xmlOneToOneMapping.specifiedJoinColumns(); + assertEquals("BAZ", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + + assertEquals("BAZ", oneToOneResource.getJoinColumns().get(0).getName()); + assertEquals("BAR", oneToOneResource.getJoinColumns().get(1).getName()); + assertEquals("FOO", oneToOneResource.getJoinColumns().get(2).getName()); + } + + public void testOneToOneMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityOneToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlOneToOneMapping.getName()); + + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertEquals(FetchType.EAGER, xmlOneToOneMapping.getFetch()); + assertEquals(Boolean.TRUE, xmlOneToOneMapping.getOptional()); + assertNull(xmlOneToOneMapping.getTargetEntity()); + + + assertFalse(xmlOneToOneMapping.specifiedJoinColumns().hasNext()); + //TODO default joinColumns + //assertTrue(xmlOneToOneMapping.defaultJoinColumns().hasNext()); + + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + + + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityOneToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlOneToOneMapping.getName()); + assertEquals(FetchType.LAZY, xmlOneToOneMapping.getSpecifiedFetch()); + assertEquals(Boolean.FALSE, xmlOneToOneMapping.getSpecifiedOptional()); + assertEquals("Address", xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertEquals("foo", xmlOneToOneMapping.getMappedBy()); + + XmlJoinColumn xmlJoinColumn = xmlOneToOneMapping.specifiedJoinColumns().next(); + assertEquals("MY_COLUMN", xmlJoinColumn.getSpecifiedName()); + assertEquals("MY_REFERENCED_COLUMN", xmlJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUpdatable()); + assertEquals("COLUMN_DEFINITION", xmlJoinColumn.getColumnDefinition()); + assertEquals("MY_TABLE", xmlJoinColumn.getSpecifiedTable()); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + assertTrue(xmlCascade.isAll()); + assertTrue(xmlCascade.isMerge()); + assertTrue(xmlCascade.isPersist()); + assertTrue(xmlCascade.isRemove()); + assertTrue(xmlCascade.isRefresh()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityOneToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(3, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + assertEquals("address", xmlOneToOneMapping.getName()); + assertEquals(FetchType.EAGER, xmlOneToOneMapping.getSpecifiedFetch()); + assertEquals(Boolean.TRUE, xmlOneToOneMapping.getSpecifiedOptional()); + //TODO hmm, is this correct? + assertEquals("test.Address", xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(xmlOneToOneMapping.getMappedBy()); + + XmlJoinColumn xmlJoinColumn = xmlOneToOneMapping.specifiedJoinColumns().next(); + //TODO java default columns name in JavaSingleRelationshipMapping.JoinColumnOwner + //assertEquals("address", xmlJoinColumn.getSpecifiedName()); + //assertEquals("address", xmlJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals(Boolean.FALSE, xmlJoinColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, xmlJoinColumn.getSpecifiedUpdatable()); + assertNull(xmlJoinColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlJoinColumn.getSpecifiedTable()); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityOneToOneMapping(); + createTestTargetEntityAddress(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".Address"); + + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, "address"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlOneToOneMapping xmlOneToOneMapping = (XmlOneToOneMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("address", xmlOneToOneMapping.getName()); + assertNull(xmlOneToOneMapping.getSpecifiedFetch()); + assertNull(xmlOneToOneMapping.getSpecifiedOptional()); + assertNull(xmlOneToOneMapping.getSpecifiedTargetEntity()); + assertNull(xmlOneToOneMapping.getMappedBy()); + assertEquals(FetchType.EAGER, xmlOneToOneMapping.getFetch()); + assertEquals(Boolean.TRUE, xmlOneToOneMapping.getOptional()); + //TODO default target entity in xml + //assertEquals("test.Address", xmlOneToOneMapping.getDefaultTargetEntity()); + + assertFalse(xmlOneToOneMapping.specifiedJoinColumns().hasNext()); + + //TODO default join columns for specified xmlOneToOne mapping +// XmlJoinColumn xmlJoinColumn = xmlOneToOneMapping.defaultJoinColumns().next(); +// assertNull(xmlJoinColumn.getSpecifiedName()); +// assertNull(xmlJoinColumn.getSpecifiedReferencedColumnName()); +// assertNull(xmlJoinColumn.getSpecifiedUnique()); +// assertNull(xmlJoinColumn.getSpecifiedNullable()); +// assertNull(xmlJoinColumn.getSpecifiedInsertable()); +// assertNull(xmlJoinColumn.getSpecifiedUpdatable()); +// assertNull(xmlJoinColumn.getColumnDefinition()); +// assertNull(xmlJoinColumn.getSpecifiedTable()); +// +// assertEquals("address", xmlJoinColumn.getDefaultName()); +// assertEquals("address", xmlJoinColumn.getDefaultReferencedColumnName()); +// assertEquals(Boolean.FALSE, xmlJoinColumn.getDefaultUnique()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultNullable()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultInsertable()); +// assertEquals(Boolean.TRUE, xmlJoinColumn.getDefaultUpdatable()); +// assertEquals(null, xmlJoinColumn.getColumnDefinition()); +// assertEquals(TYPE_NAME, xmlJoinColumn.getDefaultTable()); + + XmlCascade xmlCascade = xmlOneToOneMapping.getCascade(); + assertFalse(xmlCascade.isAll()); + assertFalse(xmlCascade.isMerge()); + assertFalse(xmlCascade.isPersist()); + assertFalse(xmlCascade.isRemove()); + assertFalse(xmlCascade.isRefresh()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPersistentTypeTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPersistentTypeTests.java new file mode 100644 index 0000000000..c8cc012285 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPersistentTypeTests.java @@ -0,0 +1,518 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.resource.orm.Basic; +import org.eclipse.jpt.core.internal.resource.orm.BasicImpl; +import org.eclipse.jpt.core.internal.resource.orm.Embedded; +import org.eclipse.jpt.core.internal.resource.orm.EmbeddedIdImpl; +import org.eclipse.jpt.core.internal.resource.orm.EmbeddedImpl; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.Id; +import org.eclipse.jpt.core.internal.resource.orm.IdImpl; +import org.eclipse.jpt.core.internal.resource.orm.ManyToManyImpl; +import org.eclipse.jpt.core.internal.resource.orm.ManyToOneImpl; +import org.eclipse.jpt.core.internal.resource.orm.OneToManyImpl; +import org.eclipse.jpt.core.internal.resource.orm.OneToOneImpl; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.orm.Transient; +import org.eclipse.jpt.core.internal.resource.orm.TransientImpl; +import org.eclipse.jpt.core.internal.resource.orm.Version; +import org.eclipse.jpt.core.internal.resource.orm.VersionImpl; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlPersistentTypeTests extends ContextModelTestCase +{ + public XmlPersistentTypeTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + +// public void testUpdateXmlTypeMapping() throws Exception { +// assertFalse(entityMappings().xmlPersistentTypes().hasNext()); +// assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); +// assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); +// assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); +// +// //add embeddable in the resource model, verify context model updated +// Embeddable embeddable = OrmFactory.eINSTANCE.createEmbeddable(); +// ormResource().getEntityMappings().getEmbeddables().add(embeddable); +// embeddable.setClassName("model.Foo"); +// assertTrue(entityMappings().xmlPersistentTypes().hasNext()); +// assertEquals("model.Foo", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); +// assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); +// assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); +// assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); +// assertEquals("model.Foo", ormResource().getEntityMappings().getEmbeddables().get(0).getClassName()); +// +// //add entity in the resource model, verify context model updated +// Entity entity = OrmFactory.eINSTANCE.createEntity(); +// ormResource().getEntityMappings().getEntities().add(entity); +// entity.setClassName("model.Foo2"); +// assertTrue(entityMappings().xmlPersistentTypes().hasNext()); +// assertEquals("model.Foo2", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); +// assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); +// assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); +// assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); +// assertEquals("model.Foo2", ormResource().getEntityMappings().getEntities().get(0).getClassName()); +// +// //add mapped-superclass in the resource model, verify context model updated +// MappedSuperclass mappedSuperclass = OrmFactory.eINSTANCE.createMappedSuperclass(); +// ormResource().getEntityMappings().getMappedSuperclasses().add(mappedSuperclass); +// mappedSuperclass.setClassName("model.Foo3"); +// assertTrue(entityMappings().xmlPersistentTypes().hasNext()); +// assertEquals("model.Foo3", entityMappings().xmlPersistentTypes().next().getMapping().getClass_()); +// assertFalse(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); +// assertFalse(ormResource().getEntityMappings().getEntities().isEmpty()); +// assertFalse(ormResource().getEntityMappings().getEmbeddables().isEmpty()); +// assertEquals("model.Foo3", ormResource().getEntityMappings().getMappedSuperclasses().get(0).getClassName()); +// } +// + + public void testMorphXmlTypeMapping() throws Exception { + assertFalse(entityMappings().xmlPersistentTypes().hasNext()); + assertTrue(ormResource().getEntityMappings().getMappedSuperclasses().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEntities().isEmpty()); + assertTrue(ormResource().getEntityMappings().getEmbeddables().isEmpty()); + + XmlPersistentType embeddablePersistentType = entityMappings().addXmlPersistentType(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + XmlPersistentType mappedSuperclassPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, "model.Foo3"); + ormResource().save(null); + + XmlPersistentType xmlPersistentType = entityMappings().xmlPersistentTypes().next(); + assertEquals(mappedSuperclassPersistentType, xmlPersistentType); + assertEquals(IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY, xmlPersistentType.getMapping().getKey()); + + xmlPersistentType.setMappingKey(IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY); + ormResource().save(null); + assertEquals(0, ormResource().getEntityMappings().getMappedSuperclasses().size()); + assertEquals(1, ormResource().getEntityMappings().getEntities().size()); + assertEquals(2, ormResource().getEntityMappings().getEmbeddables().size()); + + Iterator<XmlPersistentType> xmlPersistentTypes = entityMappings().xmlPersistentTypes(); + //the same XmlPersistentTypes should still be in the context model + assertEquals(xmlPersistentTypes.next(), entityPersistentType); + assertEquals(xmlPersistentTypes.next(), embeddablePersistentType); + assertEquals(xmlPersistentTypes.next(), mappedSuperclassPersistentType); + + assertEquals("model.Foo", ormResource().getEntityMappings().getEmbeddables().get(0).getClassName()); + assertEquals("model.Foo3", ormResource().getEntityMappings().getEmbeddables().get(1).getClassName()); + } + + public void testAddSpecifiedPersistentAttribute() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicAttribute"); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + Basic basic = entity.getAttributes().getBasics().get(0); + assertEquals("basicAttribute", basic.getName()); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedAttribute"); + ormResource().save(null); + + Embedded embedded = entity.getAttributes().getEmbeddeds().get(0); + assertEquals("embeddedAttribute", embedded.getName()); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "transientAttribute"); + ormResource().save(null); + + Transient transientResource = entity.getAttributes().getTransients().get(0); + assertEquals("transientAttribute", transientResource.getName()); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionAttribute"); + ormResource().save(null); + + Version version = entity.getAttributes().getVersions().get(0); + assertEquals("versionAttribute", version.getName()); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idAttribute"); + ormResource().save(null); + + Id id = entity.getAttributes().getIds().get(0); + assertEquals("idAttribute", id.getName()); + + + ListIterator<XmlPersistentAttribute> persistentAttributes = entityPersistentType.specifiedAttributes(); + assertEquals("idAttribute", persistentAttributes.next().getName()); + assertEquals("basicAttribute", persistentAttributes.next().getName()); + assertEquals("versionAttribute", persistentAttributes.next().getName()); + assertEquals("embeddedAttribute", persistentAttributes.next().getName()); + assertEquals("transientAttribute", persistentAttributes.next().getName()); + assertFalse(persistentAttributes.hasNext()); + } + + public void testRemoveSpecifiedPersistentAttribute() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicAttribute"); + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedAttribute"); + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionAttribute"); + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idAttribute"); + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "transientAttribute"); + ormResource().save(null); + + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + assertEquals("basicAttribute", entity.getAttributes().getBasics().get(0).getName()); + assertEquals("embeddedAttribute", entity.getAttributes().getEmbeddeds().get(0).getName()); + assertEquals("versionAttribute", entity.getAttributes().getVersions().get(0).getName()); + assertEquals("idAttribute", entity.getAttributes().getIds().get(0).getName()); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("basicAttribute")); + assertEquals("embeddedAttribute", entity.getAttributes().getEmbeddeds().get(0).getName()); + assertEquals("versionAttribute", entity.getAttributes().getVersions().get(0).getName()); + assertEquals("idAttribute", entity.getAttributes().getIds().get(0).getName()); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("embeddedAttribute")); + assertEquals("versionAttribute", entity.getAttributes().getVersions().get(0).getName()); + assertEquals("idAttribute", entity.getAttributes().getIds().get(0).getName()); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("versionAttribute")); + assertEquals("idAttribute", entity.getAttributes().getIds().get(0).getName()); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("idAttribute")); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("transientAttribute")); + assertNull(entity.getAttributes()); + } + + public void testRemoveId() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, "idAttribute"); + assertEquals("idAttribute", entity.getAttributes().getIds().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("idAttribute")); + assertNull(entity.getAttributes()); + } + + public void testRemoveBasic() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, "basicAttribute"); + assertEquals("basicAttribute", entity.getAttributes().getBasics().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("basicAttribute")); + assertNull(entity.getAttributes()); + } + + public void testRemoveVersion() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionAttribute"); + assertEquals("versionAttribute", entity.getAttributes().getVersions().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("versionAttribute")); + assertNull(entity.getAttributes()); + } + + public void testRemoveEmbedded() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, "embeddedAttribute"); + assertEquals("embeddedAttribute", entity.getAttributes().getEmbeddeds().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("embeddedAttribute")); + assertNull(entity.getAttributes()); + } + + public void testRemoveTransient() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entityPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "transientAttribute"); + assertEquals("transientAttribute", entity.getAttributes().getTransients().get(0).getName()); + + entityPersistentType.removeSpecifiedXmlPersistentAttribute(entityPersistentType.attributeNamed("transientAttribute")); + assertNull(entity.getAttributes()); + } + + public void testUpdateSpecifiedPersistentAttributes() throws Exception { + XmlPersistentType entityPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo2"); + Entity entity = ormResource().getEntityMappings().getEntities().get(0); + + entity.setAttributes(OrmFactory.eINSTANCE.createAttributes()); + BasicImpl basic = OrmFactory.eINSTANCE.createBasicImpl(); + entity.getAttributes().getBasics().add(basic); + basic.setName("basicAttribute"); + + XmlPersistentAttribute xmlPersistentAttribute = entityPersistentType.attributes().next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + + EmbeddedImpl embedded = OrmFactory.eINSTANCE.createEmbeddedImpl(); + entity.getAttributes().getEmbeddeds().add(embedded); + embedded.setName("embeddedAttribute"); + + ListIterator<XmlPersistentAttribute> attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + VersionImpl version = OrmFactory.eINSTANCE.createVersionImpl(); + entity.getAttributes().getVersions().add(version); + version.setName("versionAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + IdImpl id = OrmFactory.eINSTANCE.createIdImpl(); + entity.getAttributes().getIds().add(id); + id.setName("idAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + TransientImpl transientResource = OrmFactory.eINSTANCE.createTransientImpl(); + entity.getAttributes().getTransients().add(transientResource); + transientResource.setName("transientAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + ManyToOneImpl manyToOneResource = OrmFactory.eINSTANCE.createManyToOneImpl(); + entity.getAttributes().getManyToOnes().add(manyToOneResource); + manyToOneResource.setName("manyToOneAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + ManyToManyImpl manyToManyResource = OrmFactory.eINSTANCE.createManyToManyImpl(); + entity.getAttributes().getManyToManys().add(manyToManyResource); + manyToManyResource.setName("manyToManyAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + OneToManyImpl oneToManyResource = OrmFactory.eINSTANCE.createOneToManyImpl(); + entity.getAttributes().getOneToManys().add(oneToManyResource); + oneToManyResource.setName("oneToManyAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("oneToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + OneToOneImpl oneToOneResource = OrmFactory.eINSTANCE.createOneToOneImpl(); + entity.getAttributes().getOneToOnes().add(oneToOneResource); + oneToOneResource.setName("oneToOneAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("oneToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("oneToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + + EmbeddedIdImpl embeddedIdResource = OrmFactory.eINSTANCE.createEmbeddedIdImpl(); + entity.getAttributes().getEmbeddedIds().add(embeddedIdResource); + embeddedIdResource.setName("embeddedIdAttribute"); + + attributes = entityPersistentType.attributes(); + xmlPersistentAttribute = attributes.next(); + assertEquals("idAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedIdAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("basicAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("versionAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("oneToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ONE_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("oneToOneAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("manyToManyAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.MANY_TO_MANY_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("embeddedAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + xmlPersistentAttribute = attributes.next(); + assertEquals("transientAttribute", xmlPersistentAttribute.getName()); + assertEquals(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, xmlPersistentAttribute.getMapping().getKey()); + assertFalse(attributes.hasNext()); + + entity.getAttributes().getBasics().remove(0); + entity.getAttributes().getEmbeddeds().remove(0); + entity.getAttributes().getTransients().remove(0); + entity.getAttributes().getIds().remove(0); + entity.getAttributes().getVersions().remove(0); + entity.getAttributes().getManyToOnes().remove(0); + entity.getAttributes().getManyToManys().remove(0); + entity.getAttributes().getOneToManys().remove(0); + entity.getAttributes().getOneToOnes().remove(0); + entity.getAttributes().getEmbeddedIds().remove(0); + assertFalse(entityPersistentType.attributes().hasNext()); + assertNotNull(entity.getAttributes()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPrimaryKeyJoinColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPrimaryKeyJoinColumnTests.java new file mode 100644 index 0000000000..1a04472bb5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlPrimaryKeyJoinColumnTests.java @@ -0,0 +1,308 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlPrimaryKeyJoinColumnTests extends ContextModelTestCase +{ + public XmlPrimaryKeyJoinColumnTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertNull(primaryKeyJoinColumnResource.getName()); + + //set name in the resource model, verify context model updated + primaryKeyJoinColumnResource.setName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumnResource.getName()); + + //set name to null in the resource model + primaryKeyJoinColumnResource.setName(null); + ormResource().save(null); + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertNull(primaryKeyJoinColumnResource.getName()); + + primaryKeyJoinColumnResource.setName("FOO"); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertEquals("FOO", primaryKeyJoinColumnResource.getName()); + + entityResource.getPrimaryKeyJoinColumns().remove(0); + ormResource().save(null); + assertFalse(xmlEntity.primaryKeyJoinColumns().hasNext()); + assertTrue(entityResource.getPrimaryKeyJoinColumns().isEmpty()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertNull(primaryKeyJoinColumnResource.getName()); + + //set name in the context model, verify resource model modified + xmlPrimaryKeyJoinColumn.setSpecifiedName("foo"); + assertEquals("foo", xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertEquals("foo", primaryKeyJoinColumnResource.getName()); + + //set name to null in the context model + xmlPrimaryKeyJoinColumn.setSpecifiedName(null); + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedName()); + assertNull(entityResource.getPrimaryKeyJoinColumns().get(0).getName()); + } + + public void testUpdateSpecifiedReferencedColumnName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertNull(primaryKeyJoinColumnResource.getReferencedColumnName()); + + //set name in the resource model, verify context model updated + primaryKeyJoinColumnResource.setReferencedColumnName("FOO"); + ormResource().save(null); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals("FOO", primaryKeyJoinColumnResource.getReferencedColumnName()); + + //set name to null in the resource model + primaryKeyJoinColumnResource.setReferencedColumnName(null); + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertNull(primaryKeyJoinColumnResource.getReferencedColumnName()); + + primaryKeyJoinColumnResource.setReferencedColumnName("FOO"); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals("FOO", primaryKeyJoinColumnResource.getReferencedColumnName()); + + entityResource.getPrimaryKeyJoinColumns().remove(0); + assertFalse(xmlEntity.primaryKeyJoinColumns().hasNext()); + assertTrue(entityResource.getPrimaryKeyJoinColumns().isEmpty()); + } + + public void testModifySpecifiedReferencedColumnName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertNull(primaryKeyJoinColumnResource.getReferencedColumnName()); + + //set name in the context model, verify resource model modified + xmlPrimaryKeyJoinColumn.setSpecifiedReferencedColumnName("foo"); + assertEquals("foo", xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertEquals("foo", primaryKeyJoinColumnResource.getReferencedColumnName()); + + //set name to null in the context model + xmlPrimaryKeyJoinColumn.setSpecifiedReferencedColumnName(null); + assertNull(xmlPrimaryKeyJoinColumn.getSpecifiedReferencedColumnName()); + assertNull(entityResource.getPrimaryKeyJoinColumns().get(0).getReferencedColumnName()); + } + +// public void testUpdateDefaultNameFromJavaTable() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); +// xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.setSpecifiedMetadataComplete(null); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// +// xmlEntity.getTable().setSpecifiedName("Bar"); +// assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameNoJava() throws Exception { +// createTestEntity(); +// +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// assertEquals("Foo", xmlEntity.getTable().getDefaultName()); +// } +// +// public void testUpdateDefaultNameFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.getTable().setSpecifiedName("FOO"); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); +// assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); +// } + + + public void testUpdateSpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertNull(primaryKeyJoinColumnResource.getColumnDefinition()); + + //set name in the resource model, verify context model updated + primaryKeyJoinColumnResource.setColumnDefinition("FOO"); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertEquals("FOO", primaryKeyJoinColumnResource.getColumnDefinition()); + + //set name to null in the resource model + primaryKeyJoinColumnResource.setColumnDefinition(null); + assertNull(xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertNull(primaryKeyJoinColumnResource.getColumnDefinition()); + + primaryKeyJoinColumnResource.setColumnDefinition("FOO"); + assertEquals("FOO", xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertEquals("FOO", primaryKeyJoinColumnResource.getColumnDefinition()); + + entityResource.getPrimaryKeyJoinColumns().remove(0); + assertFalse(xmlEntity.primaryKeyJoinColumns().hasNext()); + assertTrue(entityResource.getPrimaryKeyJoinColumns().isEmpty()); + } + + public void testModifySpecifiedColumnDefinition() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlPrimaryKeyJoinColumn xmlPrimaryKeyJoinColumn = xmlEntity.addSpecifiedPrimaryKeyJoinColumn(0); + + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + PrimaryKeyJoinColumn primaryKeyJoinColumnResource = entityResource.getPrimaryKeyJoinColumns().get(0); + + assertNull(xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertNull(primaryKeyJoinColumnResource.getColumnDefinition()); + + //set name in the context model, verify resource model modified + xmlPrimaryKeyJoinColumn.setColumnDefinition("foo"); + assertEquals("foo", xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertEquals("foo", primaryKeyJoinColumnResource.getColumnDefinition()); + + //set name to null in the context model + xmlPrimaryKeyJoinColumn.setColumnDefinition(null); + assertNull(xmlPrimaryKeyJoinColumn.getColumnDefinition()); + assertNull(entityResource.getPrimaryKeyJoinColumns().get(0).getColumnDefinition()); + } + + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlQueryHintTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlQueryHintTests.java new file mode 100644 index 0000000000..ea6a41e786 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlQueryHintTests.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlQueryHint; +import org.eclipse.jpt.core.internal.resource.orm.QueryHint; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class XmlQueryHintTests extends ContextModelTestCase +{ + public XmlQueryHintTests(String name) { + super(name); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlQueryHint xmlQueryHint = xmlEntity.addNamedQuery(0).addHint(0); + + QueryHint queryHintResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0).getHints().get(0); + + assertNull(xmlQueryHint.getName()); + assertNull(queryHintResource.getName()); + + //set name in the resource model, verify context model updated + queryHintResource.setName("newName"); + assertEquals("newName", xmlQueryHint.getName()); + assertEquals("newName", queryHintResource.getName()); + + //set name to null in the resource model + queryHintResource.setName(null); + assertNull(xmlQueryHint.getName()); + assertNull(queryHintResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlQueryHint xmlQueryHint = xmlEntity.addNamedQuery(0).addHint(0); + + QueryHint queryHintResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0).getHints().get(0); + + assertNull(xmlQueryHint.getName()); + assertNull(queryHintResource.getName()); + + //set name in the context model, verify resource model updated + xmlQueryHint.setName("newName"); + assertEquals("newName", xmlQueryHint.getName()); + assertEquals("newName", queryHintResource.getName()); + + //set name to null in the context model + xmlQueryHint.setName(null); + assertNull(xmlQueryHint.getName()); + assertNull(queryHintResource.getName()); + } + + public void testUpdateValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlQueryHint xmlQueryHint = xmlEntity.addNamedQuery(0).addHint(0); + + QueryHint queryHintResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0).getHints().get(0); + + assertNull(xmlQueryHint.getValue()); + assertNull(queryHintResource.getValue()); + + //set name in the resource model, verify context model updated + queryHintResource.setValue("newName"); + assertEquals("newName", xmlQueryHint.getValue()); + assertEquals("newName", queryHintResource.getValue()); + + //set name to null in the resource model + queryHintResource.setValue(null); + assertNull(xmlQueryHint.getValue()); + assertNull(queryHintResource.getValue()); + } + + public void testModifyValue() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlQueryHint xmlQueryHint = xmlEntity.addNamedQuery(0).addHint(0); + + QueryHint queryHintResource = ormResource().getEntityMappings().getEntities().get(0).getNamedQueries().get(0).getHints().get(0); + + assertNull(xmlQueryHint.getValue()); + assertNull(queryHintResource.getValue()); + + //set name in the context model, verify resource model updated + xmlQueryHint.setValue("newName"); + assertEquals("newName", xmlQueryHint.getValue()); + assertEquals("newName", queryHintResource.getValue()); + + //set name to null in the context model + xmlQueryHint.setValue(null); + assertNull(xmlQueryHint.getValue()); + assertNull(queryHintResource.getValue()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSecondaryTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSecondaryTableTests.java new file mode 100644 index 0000000000..c716e9daf0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSecondaryTableTests.java @@ -0,0 +1,497 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.ISecondaryTable; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlPrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlSecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.orm.SecondaryTable; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlSecondaryTableTests extends ContextModelTestCase +{ + public XmlSecondaryTableTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set name in the resource model, verify context model updated + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + entityResource.getSecondaryTables().get(0).setName("FOO"); + XmlSecondaryTable secondaryTable = xmlEntity.specifiedSecondaryTables().next(); + assertEquals("FOO", secondaryTable.getSpecifiedName()); + assertEquals("FOO", entityResource.getSecondaryTables().get(0).getName()); + + //set name to null in the resource model + entityResource.getSecondaryTables().get(0).setName(null); + assertNull(secondaryTable.getSpecifiedName()); + assertNull(entityResource.getSecondaryTables().get(0).getName()); + + entityResource.getSecondaryTables().remove(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set name in the context model, verify resource model modified + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedName("foo"); + + assertEquals("foo", secondaryTable.getSpecifiedName()); + assertEquals("foo", entityResource.getSecondaryTables().get(0).getName()); + + //set name to null in the context model + secondaryTable.setSpecifiedName(null); + assertNull(secondaryTable.getSpecifiedName()); + assertNull(entityResource.getSecondaryTables().get(0).getName()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testUpdateDefaultNameFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + + xmlEntity.javaEntity().addSpecifiedSecondaryTable(0).setSpecifiedName("FOO"); + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedName("FOO"); + assertNull(secondaryTable.getDefaultName()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set schema in the resource model, verify context model updated + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + entityResource.getSecondaryTables().get(0).setSchema("FOO"); + XmlSecondaryTable secondaryTable = xmlEntity.specifiedSecondaryTables().next(); + assertEquals("FOO", secondaryTable.getSpecifiedSchema()); + assertEquals("FOO", entityResource.getSecondaryTables().get(0).getSchema()); + + //set schema to null in the resource model + entityResource.getSecondaryTables().get(0).setSchema(null); + assertNull(secondaryTable.getSpecifiedSchema()); + assertNull(entityResource.getSecondaryTables().get(0).getSchema()); + + entityResource.getSecondaryTables().remove(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testModifySpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set schema in the context model, verify resource model modified + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedSchema("foo"); + + assertEquals("foo", secondaryTable.getSpecifiedSchema()); + assertEquals("foo", entityResource.getSecondaryTables().get(0).getSchema()); + + //set schema to null in the context model + secondaryTable.setSpecifiedSchema(null); + assertNull(secondaryTable.getSpecifiedSchema()); + assertNull(entityResource.getSecondaryTables().get(0).getSchema()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testUpdateDefaultSchemaFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + ISecondaryTable javaSecondaryTable = xmlEntity.javaEntity().addSpecifiedSecondaryTable(0); + javaSecondaryTable.setSpecifiedName("FOO"); + javaSecondaryTable.setSpecifiedSchema("BAR"); + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedName("FOO"); + assertNull(secondaryTable.getDefaultSchema()); + } + +// public void testUpdateDefaultSchemaFromParent() throws Exception { +// createTestEntity(); +// createTestSubType(); +// +// XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(FULLY_QUALIFIED_TYPE_NAME, IMappingKeys.ENTITY_TYPE_MAPPING_KEY); +// XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(PACKAGE_NAME + ".AnnotationTestTypeChild", IMappingKeys.ENTITY_TYPE_MAPPING_KEY); +// XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); +// XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); +// +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertNull(childXmlEntity.getTable().getDefaultSchema()); +// +// parentXmlEntity.getTable().setSpecifiedSchema("FOO"); +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertEquals("FOO", childXmlEntity.getTable().getDefaultSchema()); +// +// parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); +// assertNull(parentXmlEntity.getTable().getDefaultSchema()); +// assertNull(childXmlEntity.getTable().getDefaultSchema()); +// } +// + public void testUpdateDefaultSchemaFromPersistenceUnitDefaults() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + xmlSecondaryTable.setSpecifiedName("FOO"); + assertNull(xmlSecondaryTable.getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("FOO"); + assertEquals("FOO", xmlSecondaryTable.getDefaultSchema()); + + xmlEntity.entityMappings().setSpecifiedSchema("BAR"); + assertEquals("BAR", xmlSecondaryTable.getDefaultSchema()); + + ISecondaryTable javaSecondaryTable = xmlEntity.javaEntity().addSpecifiedSecondaryTable(0); + javaSecondaryTable.setSpecifiedName("FOO"); + javaSecondaryTable.setSpecifiedSchema("JAVA_SCHEMA"); + assertEquals("BAR", xmlSecondaryTable.getDefaultSchema()); //schema is not defaulted from underlying java + + xmlEntity.entityMappings().setSpecifiedSchema(null); + assertEquals("FOO", xmlSecondaryTable.getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); + assertNull(xmlSecondaryTable.getDefaultSchema()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set catalog in the resource model, verify context model updated + entityResource.getSecondaryTables().add(OrmFactory.eINSTANCE.createSecondaryTable()); + entityResource.getSecondaryTables().get(0).setCatalog("FOO"); + XmlSecondaryTable secondaryTable = xmlEntity.specifiedSecondaryTables().next(); + assertEquals("FOO", secondaryTable.getSpecifiedCatalog()); + assertEquals("FOO", entityResource.getSecondaryTables().get(0).getCatalog()); + + //set catalog to null in the resource model + entityResource.getSecondaryTables().get(0).setCatalog(null); + assertNull(secondaryTable.getSpecifiedCatalog()); + assertNull(entityResource.getSecondaryTables().get(0).getCatalog()); + + entityResource.getSecondaryTables().remove(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testModifySpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + + //set catalog in the context model, verify resource model modified + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedCatalog("foo"); + + assertEquals("foo", secondaryTable.getSpecifiedCatalog()); + assertEquals("foo", entityResource.getSecondaryTables().get(0).getCatalog()); + + //set catalog to null in the context model + secondaryTable.setSpecifiedCatalog(null); + assertNull(secondaryTable.getSpecifiedCatalog()); + assertNull(entityResource.getSecondaryTables().get(0).getCatalog()); + + xmlEntity.removeSpecifiedSecondaryTable(0); + assertFalse(xmlEntity.specifiedSecondaryTables().hasNext()); + assertEquals(0, entityResource.getSecondaryTables().size()); + } + + public void testUpdateDefaultCatalogFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + ISecondaryTable javaSecondaryTable = xmlEntity.javaEntity().addSpecifiedSecondaryTable(0); + javaSecondaryTable.setSpecifiedName("FOO"); + javaSecondaryTable.setSpecifiedCatalog("BAR"); + XmlSecondaryTable secondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + secondaryTable.setSpecifiedName("FOO"); + assertNull(secondaryTable.getDefaultCatalog()); + } + + public void testUpdateDefaultCatalogFromPersistenceUnitDefaults() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + xmlSecondaryTable.setSpecifiedName("FOO"); + assertNull(xmlSecondaryTable.getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("FOO"); + assertEquals("FOO", xmlSecondaryTable.getDefaultCatalog()); + + xmlEntity.entityMappings().setSpecifiedCatalog("BAR"); + assertEquals("BAR", xmlSecondaryTable.getDefaultCatalog()); + + ISecondaryTable javaSecondaryTable = xmlEntity.javaEntity().addSpecifiedSecondaryTable(0); + javaSecondaryTable.setSpecifiedName("FOO"); + javaSecondaryTable.setSpecifiedCatalog("JAVA_CATALOG"); + assertEquals("BAR", xmlSecondaryTable.getDefaultCatalog()); //schema is not defaulted from underlying java + + xmlEntity.entityMappings().setSpecifiedCatalog(null); + assertEquals("FOO", xmlSecondaryTable.getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); + assertNull(xmlSecondaryTable.getDefaultCatalog()); + } + + public void testAddSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + SecondaryTable secondaryTableResource = entityResource.getSecondaryTables().get(0); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn = xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(0); + ormResource().save(null); + primaryKeyJoinColumn.setSpecifiedName("FOO"); + ormResource().save(null); + + assertEquals("FOO", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn2 = xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(0); + ormResource().save(null); + primaryKeyJoinColumn2.setSpecifiedName("BAR"); + ormResource().save(null); + + assertEquals("BAR", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("FOO", secondaryTableResource.getPrimaryKeyJoinColumns().get(1).getName()); + + XmlPrimaryKeyJoinColumn primaryKeyJoinColumn3 = xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(1); + ormResource().save(null); + primaryKeyJoinColumn3.setSpecifiedName("BAZ"); + ormResource().save(null); + + assertEquals("BAR", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", secondaryTableResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", secondaryTableResource.getPrimaryKeyJoinColumns().get(2).getName()); + + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals(primaryKeyJoinColumn2, primaryKeyJoinColumns.next()); + assertEquals(primaryKeyJoinColumn3, primaryKeyJoinColumns.next()); + assertEquals(primaryKeyJoinColumn, primaryKeyJoinColumns.next()); + + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + } + + public void testRemoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + SecondaryTable secondaryTableResource = entityResource.getSecondaryTables().get(0); + + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, secondaryTableResource.getPrimaryKeyJoinColumns().size()); + + xmlSecondaryTable.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(2, secondaryTableResource.getPrimaryKeyJoinColumns().size()); + assertEquals("BAR", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", secondaryTableResource.getPrimaryKeyJoinColumns().get(1).getName()); + + xmlSecondaryTable.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(1, secondaryTableResource.getPrimaryKeyJoinColumns().size()); + assertEquals("BAZ", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + + xmlSecondaryTable.removeSpecifiedPrimaryKeyJoinColumn(0); + assertEquals(0, secondaryTableResource.getPrimaryKeyJoinColumns().size()); + } + + public void testMoveSpecifiedPrimaryKeyJoinColumn() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + SecondaryTable secondaryTableResource = entityResource.getSecondaryTables().get(0); + + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(0).setSpecifiedName("FOO"); + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(1).setSpecifiedName("BAR"); + xmlSecondaryTable.addSpecifiedPrimaryKeyJoinColumn(2).setSpecifiedName("BAZ"); + + assertEquals(3, secondaryTableResource.getPrimaryKeyJoinColumns().size()); + + + xmlSecondaryTable.moveSpecifiedPrimaryKeyJoinColumn(2, 0); + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + + assertEquals("BAR", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAZ", secondaryTableResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", secondaryTableResource.getPrimaryKeyJoinColumns().get(2).getName()); + + + xmlSecondaryTable.moveSpecifiedPrimaryKeyJoinColumn(0, 1); + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + + assertEquals("BAZ", secondaryTableResource.getPrimaryKeyJoinColumns().get(0).getName()); + assertEquals("BAR", secondaryTableResource.getPrimaryKeyJoinColumns().get(1).getName()); + assertEquals("FOO", secondaryTableResource.getPrimaryKeyJoinColumns().get(2).getName()); + } + + public void testUpdatePrimaryKeyJoinColumns() throws Exception { + XmlPersistentType persistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) persistentType.getMapping(); + XmlSecondaryTable xmlSecondaryTable = xmlEntity.addSpecifiedSecondaryTable(0); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + SecondaryTable secondaryTableResource = entityResource.getSecondaryTables().get(0); + + secondaryTableResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + secondaryTableResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + secondaryTableResource.getPrimaryKeyJoinColumns().add(OrmFactory.eINSTANCE.createPrimaryKeyJoinColumn()); + + secondaryTableResource.getPrimaryKeyJoinColumns().get(0).setName("FOO"); + secondaryTableResource.getPrimaryKeyJoinColumns().get(1).setName("BAR"); + secondaryTableResource.getPrimaryKeyJoinColumns().get(2).setName("BAZ"); + + ListIterator<XmlPrimaryKeyJoinColumn> primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + secondaryTableResource.getPrimaryKeyJoinColumns().move(2, 0); + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + secondaryTableResource.getPrimaryKeyJoinColumns().move(0, 1); + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("BAR", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + secondaryTableResource.getPrimaryKeyJoinColumns().remove(1); + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertEquals("FOO", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + secondaryTableResource.getPrimaryKeyJoinColumns().remove(1); + primaryKeyJoinColumns = xmlSecondaryTable.specifiedPrimaryKeyJoinColumns(); + assertEquals("BAZ", primaryKeyJoinColumns.next().getName()); + assertFalse(primaryKeyJoinColumns.hasNext()); + + secondaryTableResource.getPrimaryKeyJoinColumns().remove(0); + assertFalse(xmlSecondaryTable.specifiedPrimaryKeyJoinColumns().hasNext()); + } + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSequenceGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSequenceGeneratorTests.java new file mode 100644 index 0000000000..d646674d24 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlSequenceGeneratorTests.java @@ -0,0 +1,223 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.ISequenceGenerator; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.SequenceGenerator; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlSequenceGeneratorTests extends ContextModelTestCase +{ + public XmlSequenceGeneratorTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set name in the resource model, verify context model updated + sequenceGeneratorResource.setName("FOO"); + assertEquals("FOO", sequenceGenerator.getName()); + assertEquals("FOO", sequenceGeneratorResource.getName()); + + //set name to null in the resource model + sequenceGeneratorResource.setName(null); + assertNull(sequenceGenerator.getName()); + assertNull(sequenceGeneratorResource.getName()); + } + + public void testModifySpecifiedName() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set name in the context model, verify resource model modified + sequenceGenerator.setName("FOO"); + assertEquals("FOO", sequenceGeneratorResource.getName()); + assertEquals("FOO", sequenceGenerator.getName()); + + //set name to null in the context model + sequenceGenerator.setName(null); + assertNull(sequenceGeneratorResource.getName()); + assertNull(sequenceGenerator.getName()); + } + + public void testUpdateSpecifiedSequenceName() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set name in the resource model, verify context model updated + sequenceGeneratorResource.setSequenceName("FOO"); + assertEquals("FOO", sequenceGenerator.getSpecifiedSequenceName()); + assertEquals("FOO", sequenceGeneratorResource.getSequenceName()); + + //set name to null in the resource model + sequenceGeneratorResource.setSequenceName(null); + assertNull(sequenceGenerator.getSpecifiedSequenceName()); + assertNull(sequenceGeneratorResource.getSequenceName()); + } + + public void testModifySpecifiedSequenceName() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set name in the context model, verify resource model modified + sequenceGenerator.setSpecifiedSequenceName("FOO"); + assertEquals("FOO", sequenceGeneratorResource.getSequenceName()); + assertEquals("FOO", sequenceGenerator.getSpecifiedSequenceName()); + + //set name to null in the context model + sequenceGenerator.setSpecifiedSequenceName(null); + assertNull(sequenceGeneratorResource.getSequenceName()); + assertNull(sequenceGenerator.getSpecifiedSequenceName()); + } + + public void testUpdateSpecifiedInitialValue() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set initial value in the resource model, verify context model updated + sequenceGeneratorResource.setInitialValue(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), sequenceGenerator.getSpecifiedInitialValue()); + assertEquals(Integer.valueOf(10), sequenceGeneratorResource.getInitialValue()); + + //set initial value to 1, which happens to be the default, in the resource model + sequenceGeneratorResource.setInitialValue(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), sequenceGenerator.getSpecifiedInitialValue()); + assertEquals(Integer.valueOf(1), sequenceGeneratorResource.getInitialValue()); + + //set initial value to null in the resource model + sequenceGeneratorResource.setInitialValue(null); + assertNull(sequenceGenerator.getSpecifiedInitialValue()); + assertNull(sequenceGeneratorResource.getInitialValue()); + } + + public void testModifySpecifiedInitialValue() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set initial value in the context model, verify resource model modified + sequenceGenerator.setSpecifiedInitialValue(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), sequenceGeneratorResource.getInitialValue()); + assertEquals(Integer.valueOf(10), sequenceGenerator.getSpecifiedInitialValue()); + + sequenceGenerator.setSpecifiedInitialValue(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), sequenceGeneratorResource.getInitialValue()); + assertEquals(Integer.valueOf(1), sequenceGenerator.getSpecifiedInitialValue()); + + //set initial value to null in the context model + sequenceGenerator.setSpecifiedInitialValue(null); + assertNull(sequenceGeneratorResource.getInitialValue()); + assertNull(sequenceGenerator.getSpecifiedInitialValue()); + } + + public void testUpdateSpecifiedAllocationSize() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set allocation size in the resource model, verify context model updated + sequenceGeneratorResource.setAllocationSize(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), sequenceGenerator.getSpecifiedAllocationSize()); + assertEquals(Integer.valueOf(10), sequenceGeneratorResource.getAllocationSize()); + + //set allocation size to 50, which happens to be the default, in the resource model + sequenceGeneratorResource.setAllocationSize(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), sequenceGenerator.getSpecifiedAllocationSize()); + assertEquals(Integer.valueOf(1), sequenceGeneratorResource.getAllocationSize()); + + //set allocation size to null in the resource model + sequenceGeneratorResource.setAllocationSize(null); + assertNull(sequenceGenerator.getSpecifiedAllocationSize()); + assertNull(sequenceGeneratorResource.getAllocationSize()); + } + + public void testModifySpecifiedAllocationSize() throws Exception { + ISequenceGenerator sequenceGenerator = entityMappings().addSequenceGenerator(0); + SequenceGenerator sequenceGeneratorResource = ormResource().getEntityMappings().getSequenceGenerators().get(0); + + //set allocation size in the context model, verify resource model modified + sequenceGenerator.setSpecifiedAllocationSize(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), sequenceGeneratorResource.getAllocationSize()); + assertEquals(Integer.valueOf(10), sequenceGenerator.getSpecifiedAllocationSize()); + + sequenceGenerator.setSpecifiedAllocationSize(Integer.valueOf(50)); + assertEquals(Integer.valueOf(50), sequenceGeneratorResource.getAllocationSize()); + assertEquals(Integer.valueOf(50), sequenceGenerator.getSpecifiedAllocationSize()); + + //set allocation size to null in the context model + sequenceGenerator.setSpecifiedAllocationSize(null); + assertNull(sequenceGeneratorResource.getAllocationSize()); + assertNull(sequenceGenerator.getSpecifiedAllocationSize()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableGeneratorTests.java new file mode 100644 index 0000000000..05c366c748 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableGeneratorTests.java @@ -0,0 +1,373 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.ITableGenerator; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.TableGenerator; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlTableGeneratorTests extends ContextModelTestCase +{ + public XmlTableGeneratorTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setName("FOO"); + assertEquals("FOO", tableGenerator.getName()); + assertEquals("FOO", tableGeneratorResource.getName()); + + //set name to null in the resource model + tableGeneratorResource.setName(null); + assertNull(tableGenerator.getName()); + assertNull(tableGeneratorResource.getName()); + } + + public void testModifySpecifiedName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setName("FOO"); + assertEquals("FOO", tableGeneratorResource.getName()); + assertEquals("FOO", tableGenerator.getName()); + + //set name to null in the context model + tableGenerator.setName(null); + assertNull(tableGeneratorResource.getName()); + assertNull(tableGenerator.getName()); + } + + public void testUpdateSpecifiedInitialValue() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set initial value in the resource model, verify context model updated + tableGeneratorResource.setInitialValue(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), tableGenerator.getSpecifiedInitialValue()); + assertEquals(Integer.valueOf(10), tableGeneratorResource.getInitialValue()); + + //set initial value to 1, which happens to be the default, in the resource model + tableGeneratorResource.setInitialValue(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), tableGenerator.getSpecifiedInitialValue()); + assertEquals(Integer.valueOf(1), tableGeneratorResource.getInitialValue()); + + //set initial value to null in the resource model + tableGeneratorResource.setInitialValue(null); + assertNull(tableGenerator.getSpecifiedInitialValue()); + assertNull(tableGeneratorResource.getInitialValue()); + } + + public void testModifySpecifiedInitialValue() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set initial value in the context model, verify resource model modified + tableGenerator.setSpecifiedInitialValue(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), tableGeneratorResource.getInitialValue()); + assertEquals(Integer.valueOf(10), tableGenerator.getSpecifiedInitialValue()); + + tableGenerator.setSpecifiedInitialValue(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), tableGeneratorResource.getInitialValue()); + assertEquals(Integer.valueOf(1), tableGenerator.getSpecifiedInitialValue()); + + //set initial value to null in the context model + tableGenerator.setSpecifiedInitialValue(null); + assertNull(tableGeneratorResource.getInitialValue()); + assertNull(tableGenerator.getSpecifiedInitialValue()); + } + + public void testUpdateSpecifiedAllocationSize() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set allocation size in the resource model, verify context model updated + tableGeneratorResource.setAllocationSize(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), tableGenerator.getSpecifiedAllocationSize()); + assertEquals(Integer.valueOf(10), tableGeneratorResource.getAllocationSize()); + + //set allocation size to 50, which happens to be the default, in the resource model + tableGeneratorResource.setAllocationSize(Integer.valueOf(1)); + assertEquals(Integer.valueOf(1), tableGenerator.getSpecifiedAllocationSize()); + assertEquals(Integer.valueOf(1), tableGeneratorResource.getAllocationSize()); + + //set allocation size to null in the resource model + tableGeneratorResource.setAllocationSize(null); + assertNull(tableGenerator.getSpecifiedAllocationSize()); + assertNull(tableGeneratorResource.getAllocationSize()); + } + + public void testModifySpecifiedAllocationSize() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set allocation size in the context model, verify resource model modified + tableGenerator.setSpecifiedAllocationSize(Integer.valueOf(10)); + assertEquals(Integer.valueOf(10), tableGeneratorResource.getAllocationSize()); + assertEquals(Integer.valueOf(10), tableGenerator.getSpecifiedAllocationSize()); + + tableGenerator.setSpecifiedAllocationSize(Integer.valueOf(50)); + assertEquals(Integer.valueOf(50), tableGeneratorResource.getAllocationSize()); + assertEquals(Integer.valueOf(50), tableGenerator.getSpecifiedAllocationSize()); + + //set allocation size to null in the context model + tableGenerator.setSpecifiedAllocationSize(null); + assertNull(tableGeneratorResource.getAllocationSize()); + assertNull(tableGenerator.getSpecifiedAllocationSize()); + } + + public void testUpdateSpecifiedTable() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setTable("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedTable()); + assertEquals("FOO", tableGeneratorResource.getTable()); + + //set name to null in the resource model + tableGeneratorResource.setTable(null); + assertNull(tableGenerator.getSpecifiedTable()); + assertNull(tableGeneratorResource.getTable()); + } + + public void testModifySpecifiedTable() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedTable("FOO"); + assertEquals("FOO", tableGeneratorResource.getTable()); + assertEquals("FOO", tableGenerator.getSpecifiedTable()); + + //set name to null in the context model + tableGenerator.setSpecifiedTable(null); + assertNull(tableGeneratorResource.getTable()); + assertNull(tableGenerator.getSpecifiedTable()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setSchema("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedSchema()); + assertEquals("FOO", tableGeneratorResource.getSchema()); + + //set name to null in the resource model + tableGeneratorResource.setSchema(null); + assertNull(tableGenerator.getSpecifiedSchema()); + assertNull(tableGeneratorResource.getSchema()); + } + + public void testModifySpecifiedSchema() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedSchema("FOO"); + assertEquals("FOO", tableGeneratorResource.getSchema()); + assertEquals("FOO", tableGenerator.getSpecifiedSchema()); + + //set name to null in the context model + tableGenerator.setSpecifiedSchema(null); + assertNull(tableGeneratorResource.getSchema()); + assertNull(tableGenerator.getSpecifiedSchema()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setCatalog("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedCatalog()); + assertEquals("FOO", tableGeneratorResource.getCatalog()); + + //set name to null in the resource model + tableGeneratorResource.setCatalog(null); + assertNull(tableGenerator.getSpecifiedCatalog()); + assertNull(tableGeneratorResource.getCatalog()); + } + + public void testModifySpecifiedCatalog() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedCatalog("FOO"); + assertEquals("FOO", tableGeneratorResource.getCatalog()); + assertEquals("FOO", tableGenerator.getSpecifiedCatalog()); + + //set name to null in the context model + tableGenerator.setSpecifiedCatalog(null); + assertNull(tableGeneratorResource.getCatalog()); + assertNull(tableGenerator.getSpecifiedCatalog()); + } + + public void testUpdateSpecifiedPkColumnName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setPkColumnName("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedPkColumnName()); + assertEquals("FOO", tableGeneratorResource.getPkColumnName()); + + //set name to null in the resource model + tableGeneratorResource.setPkColumnName(null); + assertNull(tableGenerator.getSpecifiedPkColumnName()); + assertNull(tableGeneratorResource.getPkColumnName()); + } + + public void testModifySpecifiedPkColumnName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedPkColumnName("FOO"); + assertEquals("FOO", tableGeneratorResource.getPkColumnName()); + assertEquals("FOO", tableGenerator.getSpecifiedPkColumnName()); + + //set name to null in the context model + tableGenerator.setSpecifiedPkColumnName(null); + assertNull(tableGeneratorResource.getPkColumnName()); + assertNull(tableGenerator.getSpecifiedPkColumnName()); + } + + public void testUpdateSpecifiedValueColumnName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setValueColumnName("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedValueColumnName()); + assertEquals("FOO", tableGeneratorResource.getValueColumnName()); + + //set name to null in the resource model + tableGeneratorResource.setValueColumnName(null); + assertNull(tableGenerator.getSpecifiedValueColumnName()); + assertNull(tableGeneratorResource.getValueColumnName()); + } + + public void testModifySpecifiedValueColumnName() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedValueColumnName("FOO"); + assertEquals("FOO", tableGeneratorResource.getValueColumnName()); + assertEquals("FOO", tableGenerator.getSpecifiedValueColumnName()); + + //set name to null in the context model + tableGenerator.setSpecifiedValueColumnName(null); + assertNull(tableGeneratorResource.getValueColumnName()); + assertNull(tableGenerator.getSpecifiedValueColumnName()); + } + + public void testUpdateSpecifiedPkColumnValue() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the resource model, verify context model updated + tableGeneratorResource.setPkColumnValue("FOO"); + assertEquals("FOO", tableGenerator.getSpecifiedPkColumnValue()); + assertEquals("FOO", tableGeneratorResource.getPkColumnValue()); + + //set name to null in the resource model + tableGeneratorResource.setPkColumnValue(null); + assertNull(tableGenerator.getSpecifiedPkColumnValue()); + assertNull(tableGeneratorResource.getPkColumnValue()); + } + + public void testModifySpecifiedPkColumnValue() throws Exception { + ITableGenerator tableGenerator = entityMappings().addTableGenerator(0); + TableGenerator tableGeneratorResource = ormResource().getEntityMappings().getTableGenerators().get(0); + + //set name in the context model, verify resource model modified + tableGenerator.setSpecifiedPkColumnValue("FOO"); + assertEquals("FOO", tableGeneratorResource.getPkColumnValue()); + assertEquals("FOO", tableGenerator.getSpecifiedPkColumnValue()); + + //set name to null in the context model + tableGenerator.setSpecifiedPkColumnValue(null); + assertNull(tableGeneratorResource.getPkColumnValue()); + assertNull(tableGenerator.getSpecifiedPkColumnValue()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableTests.java new file mode 100644 index 0000000000..b9d0d393a3 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTableTests.java @@ -0,0 +1,485 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.InheritanceType; +import org.eclipse.jpt.core.internal.context.orm.XmlEntity; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlTable; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Entity; +import org.eclipse.jpt.core.internal.resource.orm.OrmFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlTableTests extends ContextModelTestCase +{ + public XmlTableTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createIdAnnotation() throws Exception { + this.createAnnotationAndMembers("Id", ""); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + createIdAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestSubType() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JPA.ENTITY); + sb.append(";"); + sb.append(CR); + sb.append("@Entity"); + sb.append(CR); + sb.append("public class ").append("AnnotationTestTypeChild").append(" "); + sb.append("extends " + TYPE_NAME + " "); + sb.append("{}").append(CR); + } + }; + return this.javaProject.createType(PACKAGE_NAME, "AnnotationTestTypeChild.java", sourceWriter); + } + + public void testUpdateSpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlTable xmlTable = xmlEntity.getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedName()); + assertNull(entityResource.getTable()); + + //set name in the resource model, verify context model updated + entityResource.setTable(OrmFactory.eINSTANCE.createTable()); + entityResource.getTable().setName("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedName()); + assertEquals("FOO", entityResource.getTable().getName()); + + //set name to null in the resource model + entityResource.getTable().setName(null); + assertNull(xmlTable.getSpecifiedName()); + assertNull(entityResource.getTable().getName()); + + entityResource.getTable().setName("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedName()); + assertEquals("FOO", entityResource.getTable().getName()); + + entityResource.setTable(null); + assertNull(xmlTable.getSpecifiedName()); + assertNull(entityResource.getTable()); + } + + public void testModifySpecifiedName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlTable xmlTable = ((XmlEntity) xmlPersistentType.getMapping()).getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedName()); + assertNull(entityResource.getTable()); + + //set name in the context model, verify resource model modified + xmlTable.setSpecifiedName("foo"); + assertEquals("foo", xmlTable.getSpecifiedName()); + assertEquals("foo", entityResource.getTable().getName()); + + //set name to null in the context model + xmlTable.setSpecifiedName(null); + assertNull(xmlTable.getSpecifiedName()); + assertNull(entityResource.getTable()); + } + + public void testUpdateDefaultNameFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + xmlEntity.javaEntity().getTable().setSpecifiedName("Foo"); + assertEquals("Foo", xmlEntity.getTable().getDefaultName()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertEquals("Foo", xmlEntity.getTable().getDefaultName()); + + xmlEntity.getTable().setSpecifiedName("Bar"); + assertEquals(TYPE_NAME, xmlEntity.getTable().getDefaultName()); + } + + public void testUpdateDefaultNameNoJava() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertEquals("Foo", xmlEntity.getTable().getDefaultName()); + } + + public void testUpdateDefaultNameFromParent() throws Exception { + createTestEntity(); + createTestSubType(); + + XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); + XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); + + assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); + assertEquals(TYPE_NAME, childXmlEntity.getTable().getDefaultName()); + + parentXmlEntity.getTable().setSpecifiedName("FOO"); + assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); + assertEquals("FOO", childXmlEntity.getTable().getDefaultName()); + + parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + assertEquals(TYPE_NAME, parentXmlEntity.getTable().getDefaultName()); + assertEquals("AnnotationTestTypeChild", childXmlEntity.getTable().getDefaultName()); + } + + public void testUpdateSpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlTable xmlTable = xmlEntity.getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedSchema()); + assertNull(entityResource.getTable()); + + //set schema in the resource model, verify context model updated + entityResource.setTable(OrmFactory.eINSTANCE.createTable()); + entityResource.getTable().setSchema("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedSchema()); + assertEquals("FOO", entityResource.getTable().getSchema()); + + //set Schema to null in the resource model + entityResource.getTable().setSchema(null); + assertNull(xmlTable.getSpecifiedSchema()); + assertNull(entityResource.getTable().getSchema()); + + entityResource.getTable().setSchema("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedSchema()); + assertEquals("FOO", entityResource.getTable().getSchema()); + + entityResource.setTable(null); + assertNull(xmlTable.getSpecifiedSchema()); + assertNull(entityResource.getTable()); + } + + public void testUpdateDefaultSchemaFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.javaEntity().getTable().setSpecifiedSchema("Foo"); + assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertEquals("Foo", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.getTable().setSpecifiedName("Bar"); + assertNull(xmlEntity.getTable().getDefaultSchema()); + } + + public void testUpdateDefaultSchemaNoJava() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultSchema()); + } + + public void testUpdateDefaultSchemaFromParent() throws Exception { + createTestEntity(); + createTestSubType(); + + XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); + XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); + + assertNull(parentXmlEntity.getTable().getDefaultSchema()); + assertNull(childXmlEntity.getTable().getDefaultSchema()); + + parentXmlEntity.getTable().setSpecifiedSchema("FOO"); + assertNull(parentXmlEntity.getTable().getDefaultSchema()); + assertEquals("FOO", childXmlEntity.getTable().getDefaultSchema()); + + parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + assertNull(parentXmlEntity.getTable().getDefaultSchema()); + assertNull(childXmlEntity.getTable().getDefaultSchema()); + } + + public void testUpdateDefaultSchemaFromPersistenceUnitDefaults() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema("FOO"); + assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().setSpecifiedSchema("BAR"); + assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.javaEntity().getTable().setSpecifiedSchema("JAVA_SCHEMA"); + assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.getTable().setSpecifiedName("BLAH"); + //xml entity now has a table element so default schema is not taken from java + assertEquals("BAR", xmlEntity.getTable().getDefaultSchema()); + + + xmlEntity.entityMappings().setSpecifiedSchema(null); + assertEquals("FOO", xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setSchema(null); + assertNull(xmlEntity.getTable().getDefaultSchema()); + + xmlEntity.getTable().setSpecifiedName(null); + assertEquals("JAVA_SCHEMA", xmlEntity.getTable().getDefaultSchema()); + } + + public void testModifySpecifiedSchema() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlTable xmlTable = xmlEntity.getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedSchema()); + assertNull(entityResource.getTable()); + + //set Schema in the context model, verify resource model modified + xmlTable.setSpecifiedSchema("foo"); + assertEquals("foo", xmlTable.getSpecifiedSchema()); + assertEquals("foo", entityResource.getTable().getSchema()); + + //set Schema to null in the context model + xmlTable.setSpecifiedSchema(null); + assertNull(xmlTable.getSpecifiedSchema()); + assertNull(entityResource.getTable()); + } + + public void testUpdateSpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlTable xmlTable = xmlEntity.getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedCatalog()); + assertNull(entityResource.getTable()); + + //set Catalog in the resource model, verify context model updated + entityResource.setTable(OrmFactory.eINSTANCE.createTable()); + entityResource.getTable().setCatalog("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedCatalog()); + assertEquals("FOO", entityResource.getTable().getCatalog()); + + //set Catalog to null in the resource model + entityResource.getTable().setCatalog(null); + assertNull(xmlTable.getSpecifiedCatalog()); + assertNull(entityResource.getTable().getCatalog()); + + entityResource.getTable().setCatalog("FOO"); + assertEquals("FOO", xmlTable.getSpecifiedCatalog()); + assertEquals("FOO", entityResource.getTable().getCatalog()); + + entityResource.setTable(null); + assertNull(xmlTable.getSpecifiedCatalog()); + assertNull(entityResource.getTable()); + } + + public void testModifySpecifiedCatalog() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + XmlTable xmlTable = xmlEntity.getTable(); + Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); + assertNull(xmlTable.getSpecifiedCatalog()); + assertNull(entityResource.getTable()); + + //set Catalog in the context model, verify resource model modified + xmlTable.setSpecifiedCatalog("foo"); + assertEquals("foo", xmlTable.getSpecifiedCatalog()); + assertEquals("foo", entityResource.getTable().getCatalog()); + + //set Catalog to null in the context model + xmlTable.setSpecifiedCatalog(null); + assertNull(xmlTable.getSpecifiedCatalog()); + assertNull(entityResource.getTable()); + } + + public void testUpdateDefaultCatalogFromJavaTable() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.javaEntity().getTable().setSpecifiedCatalog("Foo"); + assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.setSpecifiedMetadataComplete(Boolean.TRUE); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(true); + xmlEntity.setSpecifiedMetadataComplete(Boolean.FALSE); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.setSpecifiedMetadataComplete(null); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().setXmlMappingMetadataComplete(false); + assertEquals("Foo", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.getTable().setSpecifiedName("Bar"); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + } + + public void testUpdateDefaultCatalogNoJava() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + } + + public void testUpdateDefaultCatalogFromParent() throws Exception { + createTestEntity(); + createTestSubType(); + + XmlPersistentType parentXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlPersistentType childXmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, PACKAGE_NAME + ".AnnotationTestTypeChild"); + XmlEntity parentXmlEntity = (XmlEntity) parentXmlPersistentType.getMapping(); + XmlEntity childXmlEntity = (XmlEntity) childXmlPersistentType.getMapping(); + + assertNull(parentXmlEntity.getTable().getDefaultCatalog()); + assertNull(childXmlEntity.getTable().getDefaultCatalog()); + + parentXmlEntity.getTable().setSpecifiedCatalog("FOO"); + assertNull(parentXmlEntity.getTable().getDefaultCatalog()); + assertEquals("FOO", childXmlEntity.getTable().getDefaultCatalog()); + + parentXmlEntity.setSpecifiedInheritanceStrategy(InheritanceType.JOINED); + assertNull(parentXmlEntity.getTable().getDefaultCatalog()); + assertNull(childXmlEntity.getTable().getDefaultCatalog()); + } + + public void testUpdateDefaultCatalogFromPersistenceUnitDefaults() throws Exception { + createTestEntity(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog("FOO"); + assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().setSpecifiedCatalog("BAR"); + assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.javaEntity().getTable().setSpecifiedCatalog("JAVA_CATALOG"); + assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.getTable().setSpecifiedName("BLAH"); + //xml entity now has a table element so default schema is not taken from java + assertEquals("BAR", xmlEntity.getTable().getDefaultCatalog()); + + + xmlEntity.entityMappings().setSpecifiedCatalog(null); + assertEquals("FOO", xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setCatalog(null); + assertNull(xmlEntity.getTable().getDefaultCatalog()); + + xmlEntity.getTable().setSpecifiedName(null); + assertEquals("JAVA_CATALOG", xmlEntity.getTable().getDefaultCatalog()); +} + +// +// public void testUpdateName() throws Exception { +// XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); +// XmlEntity xmlEntity = (XmlEntity) xmlPersistentType.getMapping(); +// Entity entityResource = ormResource().getEntityMappings().getEntities().get(0); +// assertEquals("Foo", xmlEntity.getName()); +// +// //set class in the resource model, verify context model updated +// entityResource.setClassName("com.Bar"); +// assertEquals("Bar", xmlEntity.getName()); +// +// entityResource.setName("Baz"); +// assertEquals("Baz", xmlEntity.getName()); +// +// //set class to null in the resource model +// entityResource.setClassName(null); +// assertEquals("Baz", xmlEntity.getName()); +// +// entityResource.setName(null); +// assertNull(xmlEntity.getName()); +// } + + + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTransientMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTransientMappingTests.java new file mode 100644 index 0000000000..118f676f10 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlTransientMappingTests.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlTransientMapping; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Transient; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlTransientMappingTests extends ContextModelTestCase +{ + public XmlTransientMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createTransientAnnotation() throws Exception{ + this.createAnnotationAndMembers("Transient", ""); + } + + + private IType createTestEntityTransientMapping() throws Exception { + createEntityAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TRANSIENT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Transient"); + } + }); + } + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "transientMapping"); + XmlTransientMapping xmlTransientnMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + Transient transientResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getTransients().get(0); + + assertEquals("transientMapping", xmlTransientnMapping.getName()); + assertEquals("transientMapping", transientResource.getName()); + + //set name in the resource model, verify context model updated + transientResource.setName("newName"); + assertEquals("newName", xmlTransientnMapping.getName()); + assertEquals("newName", transientResource.getName()); + + //set name to null in the resource model + transientResource.setName(null); + assertNull(xmlTransientnMapping.getName()); + assertNull(transientResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "transientMapping"); + XmlTransientMapping xmlTransientnMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + Transient transientResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getTransients().get(0); + + assertEquals("transientMapping", xmlTransientnMapping.getName()); + assertEquals("transientMapping", transientResource.getName()); + + //set name in the context model, verify resource model updated + xmlTransientnMapping.setName("newName"); + assertEquals("newName", xmlTransientnMapping.getName()); + assertEquals("newName", transientResource.getName()); + + //set name to null in the context model + xmlTransientnMapping.setName(null); + assertNull(xmlTransientnMapping.getName()); + assertNull(transientResource.getName()); + } + + + public void testTransientMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityTransientMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlTransientMapping xmlTransientMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlTransientMapping.getName()); + } + + //@Basic(fetch=FetchType.LAZY, optional=false) + //@Column(name="MY_COLUMN", unique=true, nullable=false, insertable=false, updatable=false, + // columnDefinition="COLUMN_DEFINITION", table="MY_TABLE", length=5, precision=6, scale=7)"); + //@Column( + //@Lob + //@Temporal(TemporalType.TIMESTAMP) + //@Enumerated(EnumType.STRING) + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityTransientMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlTransientMapping xmlTransientMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlTransientMapping.getName()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityTransientMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlTransientMapping xmlTransientMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlTransientMapping.getName()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityTransientMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY, "id"); + assertEquals(1, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlTransientMapping xmlTransientMapping = (XmlTransientMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("id", xmlTransientMapping.getName()); + } +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlVersionMappingTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlVersionMappingTests.java new file mode 100644 index 0000000000..1bc2d17ca0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/orm/XmlVersionMappingTests.java @@ -0,0 +1,335 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.orm; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IMappingKeys; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.TemporalType; +import org.eclipse.jpt.core.internal.context.orm.XmlColumn; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentAttribute; +import org.eclipse.jpt.core.internal.context.orm.XmlPersistentType; +import org.eclipse.jpt.core.internal.context.orm.XmlVersionMapping; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.orm.Version; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class XmlVersionMappingTests extends ContextModelTestCase +{ + public XmlVersionMappingTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } + + private void createEntityAnnotation() throws Exception { + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private void createVersionAnnotation() throws Exception{ + this.createAnnotationAndMembers("Version", ""); + } + + private void createColumnAnnotation() throws Exception{ + this.createAnnotationAndMembers("Column", + "String name() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";" + + "int length() default 255;" + + "int precision() default 0;" + + "int scale() default 0;"); + } + + private void createTemporalAnnotation() throws Exception{ + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + } + + private IType createTestEntityVersionMapping() throws Exception { + createEntityAnnotation(); + createVersionAnnotation(); + createColumnAnnotation(); + createTemporalAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.VERSION, JPA.COLUMN, JPA.TEMPORAL, JPA.TEMPORAL_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Version"); + sb.append(CR); + sb.append(" @Column(name=\"MY_COLUMN\", unique=true, nullable=false, insertable=false, updatable=false, columnDefinition=\"COLUMN_DEFINITION\", table=\"MY_TABLE\", length=5, precision=6, scale=7)"); + sb.append(CR); + sb.append(" @Temporal(TemporalType.TIMESTAMP)"); + } + }); + } + + public void testUpdateName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionMapping"); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + Version versionResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getVersions().get(0); + + assertEquals("versionMapping", xmlVersionMapping.getName()); + assertEquals("versionMapping", versionResource.getName()); + + //set name in the resource model, verify context model updated + versionResource.setName("newName"); + assertEquals("newName", xmlVersionMapping.getName()); + assertEquals("newName", versionResource.getName()); + + //set name to null in the resource model + versionResource.setName(null); + assertNull(xmlVersionMapping.getName()); + assertNull(versionResource.getName()); + } + + public void testModifyName() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionMapping"); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + Version versionResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getVersions().get(0); + + assertEquals("versionMapping", xmlVersionMapping.getName()); + assertEquals("versionMapping", versionResource.getName()); + + //set name in the context model, verify resource model updated + xmlVersionMapping.setName("newName"); + assertEquals("newName", xmlVersionMapping.getName()); + assertEquals("newName", versionResource.getName()); + + //set name to null in the context model + xmlVersionMapping.setName(null); + assertNull(xmlVersionMapping.getName()); + assertNull(versionResource.getName()); + } + + public void testUpdateTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionMapping"); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + Version versionResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getVersions().get(0); + ormResource().save(null); + + assertNull(xmlVersionMapping.getTemporal()); + assertNull(versionResource.getTemporal()); + + //set temporal in the resource model, verify context model updated + versionResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE); + ormResource().save(null); + assertEquals(TemporalType.DATE, xmlVersionMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, versionResource.getTemporal()); + + versionResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME); + ormResource().save(null); + assertEquals(TemporalType.TIME, xmlVersionMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, versionResource.getTemporal()); + + versionResource.setTemporal(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(TemporalType.TIMESTAMP, xmlVersionMapping.getTemporal()); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, versionResource.getTemporal()); + + //set temporal to null in the resource model + versionResource.setTemporal(null); + ormResource().save(null); + assertNull(xmlVersionMapping.getTemporal()); + assertNull(versionResource.getTemporal()); + } + + public void testModifyTemporal() throws Exception { + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, "model.Foo"); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "versionMapping"); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + Version versionResource = ormResource().getEntityMappings().getEntities().get(0).getAttributes().getVersions().get(0); + ormResource().save(null); + + assertNull(xmlVersionMapping.getTemporal()); + assertNull(versionResource.getTemporal()); + + //set temporal in the context model, verify resource model updated + xmlVersionMapping.setTemporal(TemporalType.DATE); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.DATE, versionResource.getTemporal()); + assertEquals(TemporalType.DATE, xmlVersionMapping.getTemporal()); + + xmlVersionMapping.setTemporal(TemporalType.TIME); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIME, versionResource.getTemporal()); + assertEquals(TemporalType.TIME, xmlVersionMapping.getTemporal()); + + xmlVersionMapping.setTemporal(TemporalType.TIMESTAMP); + ormResource().save(null); + assertEquals(org.eclipse.jpt.core.internal.resource.orm.TemporalType.TIMESTAMP, versionResource.getTemporal()); + assertEquals(TemporalType.TIMESTAMP, xmlVersionMapping.getTemporal()); + + //set temporal to null in the context model + xmlVersionMapping.setTemporal(null); + ormResource().save(null); + assertNull(versionResource.getTemporal()); + assertNull(xmlVersionMapping.getTemporal()); + } + + //TODO test morphing to other mapping types + //TODO test defaults + + public void testVersionMappingNoUnderylingJavaAttribute() throws Exception { + createTestEntityVersionMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "foo"); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("foo", xmlVersionMapping.getName()); + assertNull(xmlVersionMapping.getTemporal()); + + + XmlColumn xmlColumn = xmlVersionMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("foo", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + } + + //@Basic(fetch=FetchType.LAZY, optional=false) + //@Column(name="MY_COLUMN", unique=true, nullable=false, insertable=false, updatable=false, + // columnDefinition="COLUMN_DEFINITION", table="MY_TABLE", length=5, precision=6, scale=7)"); + //@Column( + //@Lob + //@Temporal(TemporalType.TIMESTAMP) + //@Enumerated(EnumType.STRING) + public void testVirtualMappingMetadataCompleteFalse() throws Exception { + createTestEntityVersionMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlVersionMapping.getName()); + assertEquals(TemporalType.TIMESTAMP, xmlVersionMapping.getTemporal()); + + XmlColumn xmlColumn = xmlVersionMapping.getColumn(); + assertEquals("MY_COLUMN", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUpdatable()); + assertEquals("COLUMN_DEFINITION", xmlColumn.getColumnDefinition()); + assertEquals("MY_TABLE", xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(5), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(6), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(7), xmlColumn.getSpecifiedScale()); + } + + public void testVirtualMappingMetadataCompleteTrue() throws Exception { + createTestEntityVersionMapping(); + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.getMapping().setSpecifiedMetadataComplete(true); + assertEquals(2, xmlPersistentType.virtualAttributesSize()); + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.virtualAttributes().next(); + + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + assertEquals("id", xmlVersionMapping.getName()); + assertNull(xmlVersionMapping.getTemporal()); + + XmlColumn xmlColumn = xmlVersionMapping.getColumn(); + assertEquals("id", xmlColumn.getSpecifiedName()); + assertEquals(Boolean.FALSE, xmlColumn.getSpecifiedUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getSpecifiedTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getSpecifiedLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getSpecifiedScale()); + } + + public void testSpecifiedMapping() throws Exception { + createTestEntityVersionMapping(); + + XmlPersistentType xmlPersistentType = entityMappings().addXmlPersistentType(IMappingKeys.ENTITY_TYPE_MAPPING_KEY, FULLY_QUALIFIED_TYPE_NAME); + xmlPersistentType.addSpecifiedPersistentAttribute(IMappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY, "id"); + assertEquals(1, xmlPersistentType.virtualAttributesSize()); + + XmlPersistentAttribute xmlPersistentAttribute = xmlPersistentType.specifiedAttributes().next(); + XmlVersionMapping xmlVersionMapping = (XmlVersionMapping) xmlPersistentAttribute.getMapping(); + + assertEquals("id", xmlVersionMapping.getName()); + assertNull(xmlVersionMapping.getTemporal()); + + XmlColumn xmlColumn = xmlVersionMapping.getColumn(); + assertNull(xmlColumn.getSpecifiedName()); + assertNull(xmlColumn.getSpecifiedUnique()); + assertNull(xmlColumn.getSpecifiedNullable()); + assertNull(xmlColumn.getSpecifiedInsertable()); + assertNull(xmlColumn.getSpecifiedUpdatable()); + assertNull(xmlColumn.getColumnDefinition()); + assertNull(xmlColumn.getSpecifiedTable()); + assertNull(xmlColumn.getSpecifiedLength()); + assertNull(xmlColumn.getSpecifiedPrecision()); + assertNull(xmlColumn.getSpecifiedScale()); + + assertEquals("id", xmlColumn.getDefaultName()); + assertEquals(Boolean.FALSE, xmlColumn.getDefaultUnique()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultNullable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultInsertable()); + assertEquals(Boolean.TRUE, xmlColumn.getDefaultUpdatable()); + assertEquals(null, xmlColumn.getColumnDefinition()); + assertEquals(TYPE_NAME, xmlColumn.getDefaultTable()); + assertEquals(Integer.valueOf(255), xmlColumn.getDefaultLength()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultPrecision()); + assertEquals(Integer.valueOf(0), xmlColumn.getDefaultScale()); + } + + +}
\ No newline at end of file diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/BaseJpaContentTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/BaseJpaContentTests.java new file mode 100644 index 0000000000..d83afa39a2 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/BaseJpaContentTests.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.context.base.IBaseJpaContent; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class BaseJpaContentTests extends ContextModelTestCase +{ + public BaseJpaContentTests(String name) { + super(name); + } + + public void testModifyAddPersistenceXml() throws Exception { + PersistenceResource pr = persistenceResource(); + deleteResource(pr); + + assertFalse(pr.exists()); + + IBaseJpaContent baseJpaContent = (IBaseJpaContent) getJavaProject().getJpaProject().contextModel(); + assertNull(baseJpaContent.getPersistenceXml()); + + baseJpaContent.addPersistenceXml(); + + assertNotNull(baseJpaContent.getPersistenceXml()); + + boolean exceptionThrown = false; + try { + baseJpaContent.addPersistenceXml(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue(exceptionThrown); + } + + public void testModifyRemovePersistenceXml() throws Exception { + IBaseJpaContent baseJpaContent = (IBaseJpaContent) getJavaProject().getJpaProject().contextModel(); + + assertNotNull(baseJpaContent.getPersistenceXml()); + + baseJpaContent.removePersistenceXml(); + waitForWorkspaceJobs(); + + assertNull(baseJpaContent.getPersistenceXml()); + + boolean exceptionThrown = false; + try { + baseJpaContent.removePersistenceXml(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue(exceptionThrown); + } + + @SuppressWarnings("unchecked") + public void testUpdateAddPersistenceXml() throws Exception { + PersistenceResource pr = persistenceResource(); + deleteResource(pr); + + assertFalse(pr.exists()); + + IBaseJpaContent baseJpaContent = (IBaseJpaContent) getJavaProject().getJpaProject().contextModel(); + assertNull(baseJpaContent.getPersistenceXml()); + + pr.getContents().add(PersistenceFactory.eINSTANCE.createXmlPersistence()); + pr.save(null); + assertNotNull(baseJpaContent.getPersistenceXml()); + } + + public void testUpdateRemovePersistenceXml() throws Exception { + PersistenceResource pr = persistenceResource(); + IBaseJpaContent baseJpaContent = (IBaseJpaContent) getJavaProject().getJpaProject().contextModel(); + + assertNotNull(baseJpaContent.getPersistenceXml()); + + deleteResource(pr); + + assertNull(baseJpaContent.getPersistenceXml()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/ClassRefTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/ClassRefTests.java new file mode 100644 index 0000000000..6583f7b0ce --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/ClassRefTests.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.context.base.IClassRef; +import org.eclipse.jpt.core.internal.context.base.IPersistenceUnit; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlJavaClassRef; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnit; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class ClassRefTests extends ContextModelTestCase +{ + public ClassRefTests(String name) { + super(name); + } + + public void testUpdateClassName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add class ref + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Bar"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + IClassRef classRef = persistenceUnit.classRefs().next(); + + // test that class names are initially equal + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set xml class name to different name, test equality + xmlClassRef.setJavaClass("com.bar.Foo"); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set class name to empty string, test equality + xmlClassRef.setJavaClass(""); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set class name to null, test equality + xmlClassRef.setJavaClass(null); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set name back to non-null, test equality + xmlClassRef.setJavaClass("com.foo.Bar"); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + } + + public void testModifyClassName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add class ref + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Bar"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + IClassRef classRef = persistenceUnit.classRefs().next(); + + // test that class names are initially equal + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set context class name to different name, test equality + classRef.setClassName("com.bar.Foo"); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set class name to empty string, test equality + classRef.setClassName(""); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set class name to null, test equality + classRef.setClassName(null); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + + // set name back to non-null, test equality + classRef.setClassName("com.foo.Bar"); + + assertEquals(classRef.getClassName(), xmlClassRef.getJavaClass()); + } + + + public void testGetPersistentType() throws Exception { + createTestType(); + + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass(FULLY_QUALIFIED_TYPE_NAME); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + + IClassRef classRef = classRef(); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, classRef.getJavaPersistentType().getName()); + + //test setting to a class that does not exist in the project + xmlClassRef.setJavaClass("com.foo.Bar"); + + classRef = classRef(); + assertNull(classRef.getJavaPersistentType()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/JptCorePersistenceContextModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/JptCorePersistenceContextModelTests.java new file mode 100644 index 0000000000..db12f873fd --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/JptCorePersistenceContextModelTests.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class JptCorePersistenceContextModelTests extends TestCase +{ + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptCorePersistenceContextModelTests.class.getName()); + suite.addTestSuite(BaseJpaContentTests.class); + suite.addTestSuite(PersistenceXmlTests.class); + suite.addTestSuite(PersistenceTests.class); + suite.addTestSuite(PersistenceUnitTests.class); + suite.addTestSuite(MappingFileRefTests.class); + suite.addTestSuite(ClassRefTests.class); + return suite; + } + + private JptCorePersistenceContextModelTests() { + throw new UnsupportedOperationException(); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/MappingFileRefTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/MappingFileRefTests.java new file mode 100644 index 0000000000..48d05e96dc --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/MappingFileRefTests.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.context.base.IMappingFileRef; +import org.eclipse.jpt.core.internal.context.base.IPersistenceUnit; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnit; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class MappingFileRefTests extends ContextModelTestCase +{ + public MappingFileRefTests(String name) { + super(name); + } + + protected IMappingFileRef mappingFileRef() { + return persistenceUnit().mappingFileRefs().next(); + } + + public void testUpdateFileName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add mapping file ref + XmlMappingFileRef xmlFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlFileRef.setFileName("foo.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlFileRef); + IMappingFileRef fileRef = persistenceUnit.specifiedMappingFileRefs().next(); + + // test that file names are initially equal + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set xml to different file name, test equality + xmlFileRef.setFileName("bar.xml"); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name to empty string, test equality + xmlFileRef.setFileName(""); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name to null, test equality + xmlFileRef.setFileName(null); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name back to non-null, test equality + xmlFileRef.setFileName("baz.xml"); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + } + + public void testModifyFileName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add mapping file ref + XmlMappingFileRef xmlFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlFileRef.setFileName("foo.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlFileRef); + IMappingFileRef fileRef = persistenceUnit.specifiedMappingFileRefs().next(); + + // test that file names are initially equal + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set context to different file name, test equality + fileRef.setFileName("bar.xml"); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name to empty string, test equality + fileRef.setFileName(""); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name to null, test equality + fileRef.setFileName(null); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + + // set file name back to non-null, test equality + fileRef.setFileName("baz.xml"); + + assertEquals(fileRef.getFileName(), xmlFileRef.getFileName()); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceTests.java new file mode 100644 index 0000000000..80be25e88f --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceTests.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.context.base.IPersistence; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceResource; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistence; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnit; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.CollectionTools; + +public class PersistenceTests extends ContextModelTestCase +{ + public PersistenceTests(String name) { + super(name); + } + + protected IPersistence persistence() { + return jpaContent().getPersistenceXml().getPersistence(); + } + + public void testUpdateAddPersistenceUnit() throws Exception { + PersistenceResource prm = persistenceResource(); + XmlPersistence xmlPersistence = prm.getPersistence(); + IPersistence persistence = jpaContent().getPersistenceXml().getPersistence(); + + // clear xml persistence units, test that it's clear in context + xmlPersistence.getPersistenceUnits().clear(); + + assertTrue(CollectionTools.list(persistence.persistenceUnits()).isEmpty()); + + // add xml persistence unit, test that it's added to context + XmlPersistenceUnit xmlPersistenceUnit = PersistenceFactory.eINSTANCE.createXmlPersistenceUnit(); + xmlPersistenceUnit.setName("test"); + xmlPersistence.getPersistenceUnits().add(xmlPersistenceUnit); + + assertTrue(CollectionTools.size(persistence.persistenceUnits()) == 1); + + // add another ... + xmlPersistenceUnit = PersistenceFactory.eINSTANCE.createXmlPersistenceUnit(); + xmlPersistenceUnit.setName("test2"); + xmlPersistence.getPersistenceUnits().add(xmlPersistenceUnit); + + assertTrue(CollectionTools.size(persistence.persistenceUnits()) == 2); + } + + public void testModifyAddPersistencUnit() { + XmlPersistence xmlPersistence = xmlPersistence(); + IPersistence persistence = persistence(); + + // clear xml persistence units, test that it's clear in context + xmlPersistence.getPersistenceUnits().clear(); + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 0); + + // add persistence unit, test that it's added to resource + persistence.addPersistenceUnit(); + + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 1); + + // add another ... + persistence.addPersistenceUnit(); + + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 2); + } + + public void testUpdateRemovePersistenceUnit() throws Exception { + PersistenceResource prm = persistenceResource(); + XmlPersistence xmlPersistence = prm.getPersistence(); + IPersistence persistence = jpaContent().getPersistenceXml().getPersistence(); + + // add a persistence unit and test that there are two existing xml and context persistence unit + XmlPersistenceUnit xmlPersistenceUnit = PersistenceFactory.eINSTANCE.createXmlPersistenceUnit(); + xmlPersistenceUnit.setName("test"); + xmlPersistence.getPersistenceUnits().add(xmlPersistenceUnit); + + assertTrue(xmlPersistence.getPersistenceUnits().size() == 2); + assertTrue(CollectionTools.size(persistence.persistenceUnits()) == 2); + + // remove persistence unit from xml, test that it's removed from context + xmlPersistenceUnit = xmlPersistence.getPersistenceUnits().get(0); + xmlPersistence.getPersistenceUnits().remove(xmlPersistenceUnit); + + assertTrue(CollectionTools.size(persistence.persistenceUnits()) == 1); + + // remove another one ... + xmlPersistenceUnit = xmlPersistence.getPersistenceUnits().get(0); + xmlPersistence.getPersistenceUnits().remove(xmlPersistenceUnit); + + assertTrue(CollectionTools.size(persistence.persistenceUnits()) == 0); + } + + public void testModifyRemovePersistenceUnit() { + XmlPersistence xmlPersistence = xmlPersistence(); + IPersistence persistence = persistence(); + + // add a persistence unit and test that there are two existing xml and context persistence unit + XmlPersistenceUnit xmlPersistenceUnit = PersistenceFactory.eINSTANCE.createXmlPersistenceUnit(); + xmlPersistenceUnit.setName("test"); + xmlPersistence.getPersistenceUnits().add(xmlPersistenceUnit); + + assertEquals(xmlPersistence.getPersistenceUnits().size(), 2); + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 2); + + // remove persistence unit, test that it's removed from resource + persistence.removePersistenceUnit(0); + + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 1); + + // remove another one ... + persistence.removePersistenceUnit(0); + + assertEquals(CollectionTools.size(persistence.persistenceUnits()), 0); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceUnitTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceUnitTests.java new file mode 100644 index 0000000000..60f592bee4 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceUnitTests.java @@ -0,0 +1,960 @@ +/******************************************************************************* + * 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.context.base.AccessType; +import org.eclipse.jpt.core.internal.context.base.IPersistenceUnit; +import org.eclipse.jpt.core.internal.context.base.IProperty; +import org.eclipse.jpt.core.internal.context.base.PersistenceUnitTransactionType; +import org.eclipse.jpt.core.internal.resource.orm.OrmResource; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.XmlJavaClassRef; +import org.eclipse.jpt.core.internal.resource.persistence.XmlMappingFileRef; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnit; +import org.eclipse.jpt.core.internal.resource.persistence.XmlPersistenceUnitTransactionType; +import org.eclipse.jpt.core.internal.resource.persistence.XmlProperties; +import org.eclipse.jpt.core.internal.resource.persistence.XmlProperty; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; +import org.eclipse.jpt.utility.internal.CollectionTools; + +public class PersistenceUnitTests extends ContextModelTestCase +{ + public PersistenceUnitTests(String name) { + super(name); + } + + public void testUpdateName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that names are initially equal + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to different name, test equality + xmlPersistenceUnit.setName("newName"); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to empty string, test equality + xmlPersistenceUnit.setName(""); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to null, test equality + xmlPersistenceUnit.setName(null); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name back to non-null, test equality + xmlPersistenceUnit.setName("newName"); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + } + + public void testModifyName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that names are initially equal + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to different name, test equality + persistenceUnit.setName("newName"); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to empty string, test equality + persistenceUnit.setName(""); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name to null, test equality + persistenceUnit.setName(null); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + + // set name back to non-null, test equality + persistenceUnit.setName("newName"); + + assertEquals(xmlPersistenceUnit.getName(), persistenceUnit.getName()); + } + + public void testUpdateTransactionType() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // 1 - initial value is default + assertNull(xmlPersistenceUnit.getTransactionType()); + assertEquals(persistenceUnit.getTransactionType(), PersistenceUnitTransactionType.DEFAULT); + + // 2 - set value, context changed + xmlPersistenceUnit.setTransactionType(XmlPersistenceUnitTransactionType.JTA); + + assertEquals(persistenceUnit.getTransactionType(), PersistenceUnitTransactionType.JTA); + + xmlPersistenceUnit.setTransactionType(XmlPersistenceUnitTransactionType.RESOURCE_LOCAL); + + assertEquals(persistenceUnit.getTransactionType(), PersistenceUnitTransactionType.RESOURCE_LOCAL); + + // 3 - unset value, context changed + xmlPersistenceUnit.unsetTransactionType(); + + assertEquals(persistenceUnit.getTransactionType(), PersistenceUnitTransactionType.DEFAULT); + } + + public void testModifyTransactionType() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // 1 - initial value is default + assertNull(xmlPersistenceUnit.getTransactionType()); + assertEquals(persistenceUnit.getTransactionType(), PersistenceUnitTransactionType.DEFAULT); + + // 2 - set context value, resource changed + persistenceUnit.setTransactionType(PersistenceUnitTransactionType.JTA); + + assertEquals(xmlPersistenceUnit.getTransactionType(), XmlPersistenceUnitTransactionType.JTA); + + persistenceUnit.setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL); + + assertEquals(xmlPersistenceUnit.getTransactionType(), XmlPersistenceUnitTransactionType.RESOURCE_LOCAL); + + // 3 - set context value to default, resource unset + persistenceUnit.setTransactionTypeToDefault(); + + assertTrue(persistenceUnit.isTransactionTypeDefault()); + assertFalse(xmlPersistenceUnit.isSetTransactionType()); + } + + public void testUpdateDescription() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that descriptions are initially equal + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to different description, test equality + xmlPersistenceUnit.setDescription("newDescription"); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to empty string, test equality + xmlPersistenceUnit.setDescription(""); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to null, test equality + xmlPersistenceUnit.setDescription(null); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description back to non-null, test equality + xmlPersistenceUnit.setDescription("newDescription"); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + } + + public void testModifyDescription() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that descriptions are initially equal + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to different description, test equality + persistenceUnit.setDescription("newDescription"); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to empty string, test equality + persistenceUnit.setDescription(""); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description to null, test equality + persistenceUnit.setDescription(null); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + + // set description back to non-null, test equality + persistenceUnit.setDescription("newDescription"); + + assertEquals(xmlPersistenceUnit.getDescription(), persistenceUnit.getDescription()); + } + + public void testUpdateProvider() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that providers are initially equal + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to different provider, test equality + xmlPersistenceUnit.setProvider("newProvider"); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to empty string, test equality + xmlPersistenceUnit.setProvider(""); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to null, test equality + xmlPersistenceUnit.setProvider(null); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider back to non-null, test equality + xmlPersistenceUnit.setProvider("newProvider"); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + } + + public void testModifyProvider() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that providers are initially equal + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to different provider, test equality + persistenceUnit.setProvider("newProvider"); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to empty string, test equality + persistenceUnit.setProvider(""); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider to null, test equality + persistenceUnit.setProvider(null); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + + // set provider back to non-null, test equality + persistenceUnit.setProvider("newProvider"); + + assertEquals(xmlPersistenceUnit.getProvider(), persistenceUnit.getProvider()); + } + + public void testUpdateJtaDataSource() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that jtaDataSources are initially equal + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to different jtaDataSource, test equality + xmlPersistenceUnit.setJtaDataSource("newJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to empty string, test equality + xmlPersistenceUnit.setJtaDataSource(""); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to null, test equality + xmlPersistenceUnit.setJtaDataSource(null); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource back to non-null, test equality + xmlPersistenceUnit.setJtaDataSource("newJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + } + + public void testModifyJtaDataSource() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that jtaDataSources are initially equal + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to different jtaDataSource, test equality + persistenceUnit.setJtaDataSource("newJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to empty string, test equality + persistenceUnit.setJtaDataSource(""); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource to null, test equality + persistenceUnit.setJtaDataSource(null); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + + // set jtaDataSource back to non-null, test equality + persistenceUnit.setJtaDataSource("newJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getJtaDataSource(), persistenceUnit.getJtaDataSource()); + } + + public void testUpdateNonJtaDataSource() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that nonJtaDataSources are initially equal + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to different nonJtaDataSource, test equality + xmlPersistenceUnit.setNonJtaDataSource("newNonJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to empty string, test equality + xmlPersistenceUnit.setNonJtaDataSource(""); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to null, test equality + xmlPersistenceUnit.setNonJtaDataSource(null); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource back to non-null, test equality + xmlPersistenceUnit.setNonJtaDataSource("newNonJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + } + + public void testModifyNonJtaDataSource() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that nonJtaDataSources are initially equal + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to different nonJtaDataSource, test equality + persistenceUnit.setNonJtaDataSource("newNonJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to empty string, test equality + persistenceUnit.setNonJtaDataSource(""); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource to null, test equality + persistenceUnit.setNonJtaDataSource(null); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + + // set nonJtaDataSource back to non-null, test equality + persistenceUnit.setNonJtaDataSource("newNonJtaDataSource"); + + assertEquals(xmlPersistenceUnit.getNonJtaDataSource(), persistenceUnit.getNonJtaDataSource()); + } + + public void testUpdateJarFileRefs1() { + // TODO + } + + public void testUpdateJarFileRefs2() { + // TODO + } + + public void testUpdateDefaultMappingFileRef1() throws Exception { + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that there is one initially + OrmResource ormResource = ormResource(); + assertTrue(ormResource.exists()); + assertNotNull(persistenceUnit.getDefaultMappingFileRef()); + + // remove orm.xml + deleteResource(ormResource); + + assertFalse(ormResource.exists()); + assertNull(persistenceUnit.getDefaultMappingFileRef()); + } + + public void testUpdateDefaultMappingFileRef2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test that there is one initially + OrmResource ormResource = ormResource(); + assertTrue(ormResource.exists()); + assertNotNull(persistenceUnit.getDefaultMappingFileRef()); + + // add specified orm.xml + XmlMappingFileRef xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("META-INF/orm.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 1); + + assertTrue(ormResource.exists()); + assertNull(persistenceUnit.getDefaultMappingFileRef()); + } + + public void testUpdateSpecifiedMappingFileRefs1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 0); + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 0); + + // add mapping file ref, test that it's added to context + XmlMappingFileRef xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 1); + + // add another ... + xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm2.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 2); + } + + public void testUpdateSpecifiedMappingFileRefs2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two mapping file refs and test that there are two existing in xml and context + XmlMappingFileRef xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm2.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 2); + + // remove mapping file ref from xml, test that it's removed from context + xmlMappingFileRef = xmlPersistenceUnit.getMappingFiles().get(0); + xmlPersistenceUnit.getMappingFiles().remove(xmlMappingFileRef); + + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 1); + + // remove another one ... + xmlMappingFileRef = xmlPersistenceUnit.getMappingFiles().get(0); + xmlPersistenceUnit.getMappingFiles().remove(xmlMappingFileRef); + + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 0); + } + + public void testModifySpecifiedMappingFileRefs1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 0); + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 0); + + // add mapping file ref, test that it's added to resource + persistenceUnit.addSpecifiedMappingFileRef(); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 1); + + // add another ... + persistenceUnit.addSpecifiedMappingFileRef(); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 2); + } + + public void testModifySpecifiedMappingFileRefs2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two mapping file refs and test that there are two existing in xml and context + XmlMappingFileRef xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + xmlMappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + xmlMappingFileRef.setFileName("orm2.xml"); + xmlPersistenceUnit.getMappingFiles().add(xmlMappingFileRef); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.specifiedMappingFileRefs()), 2); + + // remove mapping file ref from context, test that it's removed from xml + persistenceUnit.removeSpecifiedMappingFileRef(0); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 1); + + // remove another one ... + persistenceUnit.removeSpecifiedMappingFileRef(0); + + assertEquals(xmlPersistenceUnit.getMappingFiles().size(), 0); + } + + public void testUpdateClassRefs1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertEquals(xmlPersistenceUnit.getClasses().size(), 0); + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 0); + + // add mapping file ref, test that it's added to context + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Bar"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 1); + + // add another ... + xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Baz"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 2); + } + + public void testUpdateClassRefs2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two class refs and test that there are two existing in xml and context + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Bar"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Baz"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 2); + + // remove class ref from xml, test that it's removed from context + xmlClassRef = xmlPersistenceUnit.getClasses().get(0); + xmlPersistenceUnit.getClasses().remove(xmlClassRef); + + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 1); + + // remove another one ... + xmlClassRef = xmlPersistenceUnit.getClasses().get(0); + xmlPersistenceUnit.getClasses().remove(xmlClassRef); + + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 0); + } + + public void testModifyClassRefs1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertEquals(xmlPersistenceUnit.getClasses().size(), 0); + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 0); + + // add class ref, test that it's added to context + persistenceUnit.addClassRef(); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 1); + + // add another ... + persistenceUnit.addClassRef(); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 2); + } + + public void testModifyClassRefs2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two class refs and test that there are two existing in xml and context + XmlJavaClassRef xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Bar"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + xmlClassRef = PersistenceFactory.eINSTANCE.createXmlJavaClassRef(); + xmlClassRef.setJavaClass("com.foo.Baz"); + xmlPersistenceUnit.getClasses().add(xmlClassRef); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.classRefs()), 2); + + // remove class ref from context, test that it's removed from xml + persistenceUnit.removeClassRef(0); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 1); + + // remove another one ... + persistenceUnit.removeClassRef(0); + + assertEquals(xmlPersistenceUnit.getClasses().size(), 0); + } + + public void testUpdateExcludeUnlistedClasses() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // 1 - initial value is default + assertFalse(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertTrue(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + // 2 - set value, context changed + xmlPersistenceUnit.setExcludeUnlistedClasses(true); + + assertTrue(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertFalse(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + xmlPersistenceUnit.setExcludeUnlistedClasses(false); + + assertTrue(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertFalse(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + // 3 - unset value, context changed + xmlPersistenceUnit.unsetExcludeUnlistedClasses(); + + assertFalse(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertTrue(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + } + + public void testModifyExcludeUnlistedClasses() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // 1 - initial value is default + assertFalse(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertTrue(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + // 2 - set value, resource changed + persistenceUnit.setExcludeUnlistedClasses(true); + + assertTrue(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertFalse(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + persistenceUnit.setExcludeUnlistedClasses(false); + + assertTrue(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertFalse(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + + // 3 - set context to default, resource unset + persistenceUnit.setExcludeUnlistedClassesToDefault(); + + assertFalse(xmlPersistenceUnit.isSetExcludeUnlistedClasses()); + assertTrue(persistenceUnit.isExcludeUnlistedClassesDefault()); + assertEquals(persistenceUnit.getExcludeUnlistedClasses(), xmlPersistenceUnit.isExcludeUnlistedClasses()); + } + + public void testUpdateProperties1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertNull(xmlPersistenceUnit.getProperties()); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 0); + + // add "properties", test that there's no real change to context + XmlProperties xmlProperties = PersistenceFactory.eINSTANCE.createXmlProperties(); + xmlPersistenceUnit.setProperties(xmlProperties); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 0); + + // add property, test that it's added to context + XmlProperty xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("foo"); + xmlProperty.setValue("bar"); + xmlProperties.getProperties().add(xmlProperty); + + assertEquals(CollectionTools.size(persistenceUnit.properties()), 1); + + // add another ... + xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("FOO"); + xmlProperty.setValue("BAR"); + xmlProperties.getProperties().add(xmlProperty); + + assertEquals(CollectionTools.size(persistenceUnit.properties()), 2); + } + + public void testUpdateProperties2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two properties and test that there are two existing in xml and context + XmlProperties xmlProperties = PersistenceFactory.eINSTANCE.createXmlProperties(); + xmlPersistenceUnit.setProperties(xmlProperties); + XmlProperty xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("foo"); + xmlProperty.setValue("bar"); + xmlProperties.getProperties().add(xmlProperty); + xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("FOO"); + xmlProperty.setValue("BAR"); + xmlProperties.getProperties().add(xmlProperty); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 2); + + // remove property from xml, test that it's removed from context + xmlProperty = xmlProperties.getProperties().get(0); + xmlProperties.getProperties().remove(xmlProperty); + + assertEquals(CollectionTools.size(persistenceUnit.properties()), 1); + + // remove another one ... + xmlProperty = xmlProperties.getProperties().get(0); + xmlProperties.getProperties().remove(xmlProperty); + + assertEquals(CollectionTools.size(persistenceUnit.properties()), 0); + } + + public void testModifyProperties1() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // test there are none initially + assertNull(xmlPersistenceUnit.getProperties()); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 0); + + // add property, test that it's added to resource + persistenceUnit.addProperty(); + + assertNotNull(xmlPersistenceUnit.getProperties()); + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 1); + + // add another ... + persistenceUnit.addProperty(); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 2); + } + + public void testModifyProperties2() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two properties and test that there are two existing in xml and context + XmlProperties xmlProperties = PersistenceFactory.eINSTANCE.createXmlProperties(); + xmlPersistenceUnit.setProperties(xmlProperties); + XmlProperty xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("foo"); + xmlProperty.setValue("bar"); + xmlProperties.getProperties().add(xmlProperty); + xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperty.setName("FOO"); + xmlProperty.setValue("BAR"); + xmlProperties.getProperties().add(xmlProperty); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 2); + + // remove property from context, test that it's removed from resource + persistenceUnit.removeProperty("foo"); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 1); + + // remove another one. test that properties object is nulled + persistenceUnit.removeProperty("FOO", "BAR"); + + assertNull(xmlPersistenceUnit.getProperties()); + } + + public void testModifyProperties3() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two properties and test that there are two existing in xml and context + persistenceUnit.putProperty("foo", "bar", false); + persistenceUnit.putProperty("FOO", "BAR", false); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 2); + + // remove property from context, test that it's removed from resource + persistenceUnit.removeProperty("foo", "bar"); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 1); + + // remove another one, test that properties object is nulled + persistenceUnit.removeProperty("FOO"); + + assertNull(xmlPersistenceUnit.getProperties()); + } + + public void testModifyProperties4() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two properties and test that there are two existing in xml and context + persistenceUnit.putProperty("foo", "bar", false); + persistenceUnit.putProperty("FOO", "BAR", false); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 2); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 2); + + // modify a property, test its value + persistenceUnit.putProperty("foo", "", false); + assertEquals("", persistenceUnit.getProperty("foo").getValue()); + + persistenceUnit.putProperty("foo", "BAR", false); + assertEquals("BAR", persistenceUnit.getProperty("foo").getValue()); + + // remove property that doesn't from context, test that the resource is unchanged + persistenceUnit.removeProperty("notExist"); + assertEquals(2, xmlPersistenceUnit.getProperties().getProperties().size()); + + // remove property from context, test that it's removed from resource + persistenceUnit.removeProperty("FOO"); + assertNull(persistenceUnit.getProperty("FOO")); + assertEquals(1, xmlPersistenceUnit.getProperties().getProperties().size()); + + // remove by setting value to null, test that properties object is nulled + persistenceUnit.putProperty("notExist", null, false); + assertNull(persistenceUnit.getProperty("notExist")); + + persistenceUnit.putProperty("foo", null, false); + assertNull(persistenceUnit.getProperty("foo")); + assertNull(xmlPersistenceUnit.getProperties()); + } + + public void testModifyProperties5() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // testing duplicate keys, add four properties and test that there are four existing in xml and context + persistenceUnit.putProperty("FOO", "BAR", false); + persistenceUnit.putProperty("foo", "bar 3", true); + persistenceUnit.putProperty("foo", "bar 2", true); + persistenceUnit.putProperty("foo", "bar 1", true); + + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 4); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 4); + + // modify a property, test its value + persistenceUnit.replacePropertyValue("foo", "bar 2", "bar two"); + + IProperty property = persistenceUnit.getProperty("foo", "bar two"); + assertEquals("bar two", property.getValue()); + + // remove a property, test that there are four existing in xml and context + persistenceUnit.removeProperty("foo", "bar 1"); + assertEquals(xmlPersistenceUnit.getProperties().getProperties().size(), 3); + assertEquals(CollectionTools.size(persistenceUnit.properties()), 3); + } + + public void testAccessProperty() { + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add two properties and try to access it. + persistenceUnit.putProperty("foo", "bar", false); + persistenceUnit.putProperty("FOO", "BAR", false); + + IProperty property = persistenceUnit.getProperty("foo"); + assertNotNull(property); + assertEquals("bar", property.getValue()); + assertTrue(persistenceUnit.containsProperty("FOO")); + assertEquals("BAR", persistenceUnit.getProperty("FOO").getValue()); + assertNull(persistenceUnit.getProperty("notExist")); + } + + public void testUpdatePropertyName() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add property for testing + XmlProperties xmlProperties = PersistenceFactory.eINSTANCE.createXmlProperties(); + xmlPersistenceUnit.setProperties(xmlProperties); + XmlProperty xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperties.getProperties().add(xmlProperty); + IProperty property = persistenceUnit.properties().next(); + + // test that names are initially equal + assertEquals(xmlProperty.getName(), property.getName()); + + // set name to different name, test equality + xmlProperty.setName("newName"); + + assertEquals(xmlProperty.getName(), property.getName()); + + // set name to empty string, test equality + xmlProperty.setName(""); + + assertEquals(xmlProperty.getName(), property.getName()); + + // set name to null, test equality + xmlProperty.setName(null); + + assertEquals(xmlProperty.getName(), property.getName()); + + // set name back to non-null, test equality + xmlProperty.setName("newName"); + + assertEquals(xmlProperty.getName(), property.getName()); + } + + public void testUpdatePropertyValue() { + XmlPersistenceUnit xmlPersistenceUnit = xmlPersistenceUnit(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + // add property for testing + XmlProperties xmlProperties = PersistenceFactory.eINSTANCE.createXmlProperties(); + xmlPersistenceUnit.setProperties(xmlProperties); + XmlProperty xmlProperty = PersistenceFactory.eINSTANCE.createXmlProperty(); + xmlProperties.getProperties().add(xmlProperty); + IProperty property = persistenceUnit.properties().next(); + + // test that values are initially equal + assertEquals(xmlProperty.getValue(), property.getValue()); + + // set value to different value, test equality + xmlProperty.setValue("newValue"); + + assertEquals(xmlProperty.getValue(), property.getValue()); + + // set value to empty string, test equality + xmlProperty.setValue(""); + + assertEquals(xmlProperty.getValue(), property.getValue()); + + // set value to null, test equality + xmlProperty.setValue(null); + + assertEquals(xmlProperty.getValue(), property.getValue()); + + // set value back to non-null, test equality + xmlProperty.setValue("newValue"); + + assertEquals(xmlProperty.getValue(), property.getValue()); + } + + + public void testGetDefaultAccess() throws Exception { + createOrmXmlFile(); + IPersistenceUnit persistenceUnit = persistenceUnit(); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.PROPERTY); + assertEquals(AccessType.PROPERTY, persistenceUnit.getDefaultAccess()); + + entityMappings().getPersistenceUnitMetadata().getPersistenceUnitDefaults().setAccess(AccessType.FIELD); + assertEquals(AccessType.FIELD, persistenceUnit.getDefaultAccess()); + } + + protected void createOrmXmlFile() throws Exception { + XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef(); + mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH); + xmlPersistenceUnit().getMappingFiles().add(mappingFileRef); + persistenceResource().save(null); + } +//TODO +// String getDefaultSchema(); +// String getDefaultCatalog(); +// AccessType getDefaultAccess(); +// boolean getDefaultCascadePersist(); + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceXmlTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceXmlTests.java new file mode 100644 index 0000000000..e0a92c589d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/context/persistence/PersistenceXmlTests.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.context.persistence; + +import org.eclipse.jpt.core.internal.context.base.IPersistenceXml; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceFactory; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceResource; +import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase; + +public class PersistenceXmlTests extends ContextModelTestCase +{ + public PersistenceXmlTests(String name) { + super(name); + } + + protected IPersistenceXml persistenceXml() { + return jpaContent().getPersistenceXml(); + } + + public void testUpdateAddPersistence() throws Exception { + PersistenceResource prm = persistenceResource(); + prm.getContents().clear(); + prm.save(null); + + assertNull(persistenceXml().getPersistence()); + + prm.getContents().add(PersistenceFactory.eINSTANCE.createXmlPersistence()); + + assertNotNull(persistenceXml().getPersistence()); + + } + + public void testModifyAddPersistence() { + PersistenceResource prm = persistenceResource(); + prm.getContents().remove(prm.getPersistence()); + assertNull(prm.getPersistence()); + + IPersistenceXml persistenceXml = persistenceXml(); + + persistenceXml.addPersistence(); + + assertNotNull(persistenceXml.getPersistence()); + + boolean exceptionThrown = false; + try { + persistenceXml.addPersistence(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue(exceptionThrown); + } + + public void testUpdateRemovePersistence() throws Exception { + PersistenceResource prm = persistenceResource(); + + assertNotNull(persistenceXml().getPersistence()); + + prm.getContents().clear(); + + assertNull(persistenceXml().getPersistence()); + } + + public void testModifyRemovePersistence() { + IPersistenceXml persistenceXml = persistenceXml(); + + assertNotNull(persistenceXml.getPersistence()); + + persistenceXml.removePersistence(); + + assertNull(persistenceXml.getPersistence()); + + boolean exceptionThrown = false; + try { + persistenceXml.removePersistence(); + } + catch (IllegalStateException ise) { + exceptionThrown = true; + } + + assertTrue(exceptionThrown); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java index b4ad34ec36..edf77b4a38 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 Oracle. All rights reserved. + * Copyright (c) 2006, 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. @@ -12,7 +12,7 @@ package org.eclipse.jpt.core.tests.internal.jdtutility; import java.io.File; import java.util.Iterator; import java.util.List; - +import junit.framework.TestCase; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -37,8 +37,7 @@ import org.eclipse.jpt.utility.internal.CommandExecutor; import org.eclipse.jpt.utility.internal.CommandExecutorProvider; import org.eclipse.jpt.utility.internal.iterators.EmptyIterator; import org.eclipse.jpt.utility.internal.iterators.SingleElementIterator; - -import junit.framework.TestCase; +import org.eclipse.jpt.utility.tests.internal.TestTools; /** * Provide an easy(?) way to build an annotated source file. @@ -70,19 +69,27 @@ public abstract class AnnotationTestCase extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - ProjectUtility.deleteAllProjects(); - this.javaProject = this.buildJavaProject(PROJECT_NAME, false); // false = no auto-build + this.deleteAllProjects(); + this.javaProject = this.buildJavaProject(false); // false = no auto-build } - + + protected void deleteAllProjects() throws Exception { + ProjectUtility.deleteAllProjects(); + } + + protected TestJavaProject buildJavaProject(boolean autoBuild) throws Exception { + return this.buildJavaProject(PROJECT_NAME, autoBuild); + } + protected TestJavaProject buildJavaProject(String projectName, boolean autoBuild) throws Exception { - return new TestJavaProject(projectName, autoBuild); // false = no auto-build + return new TestJavaProject(projectName, autoBuild); } @Override protected void tearDown() throws Exception { // this.dumpSource(); // this.javaProject.dispose(); - this.javaProject = null; + TestTools.clear(this); super.tearDown(); } @@ -128,15 +135,24 @@ public abstract class AnnotationTestCase extends TestCase { return this.createTestType(null, idFieldAnnotation); } + protected IType createTestType(AnnotationWriter annotationWriter) throws CoreException { return this.javaProject.createType(PACKAGE_NAME, FILE_NAME, this.createSourceWriter(annotationWriter)); } + + protected IType createTestType(String packageName, String fileName, String typeName, AnnotationWriter annotationWriter) throws CoreException { + return this.javaProject.createType(packageName, fileName, this.createSourceWriter(annotationWriter, typeName)); + } protected SourceWriter createSourceWriter(AnnotationWriter annotationWriter) { return new AnnotatedSourceWriter(annotationWriter); } + + protected SourceWriter createSourceWriter(AnnotationWriter annotationWriter, String typeName) { + return new AnnotatedSourceWriter(annotationWriter, typeName); + } - protected void appendSourceTo(StringBuilder sb, AnnotationWriter annotationWriter) { + protected void appendSourceTo(StringBuilder sb, AnnotationWriter annotationWriter, String typeName) { sb.append(CR); for (Iterator<String> stream = annotationWriter.imports(); stream.hasNext(); ) { sb.append("import "); @@ -144,9 +160,12 @@ public abstract class AnnotationTestCase extends TestCase { sb.append(";"); sb.append(CR); } + sb.append(CR); annotationWriter.appendTypeAnnotationTo(sb); sb.append(CR); - sb.append("public class ").append(TYPE_NAME).append(" {").append(CR); + sb.append("public class ").append(typeName).append(" "); + annotationWriter.appendExtendsImplementsTo(sb); + sb.append("{").append(CR); sb.append(CR); sb.append(" "); annotationWriter.appendIdFieldAnnotationTo(sb); @@ -186,12 +205,20 @@ public abstract class AnnotationTestCase extends TestCase { sb.append(" this.testField = testField;").append(CR); sb.append(" }").append(CR); sb.append(CR); + annotationWriter.appendMemberTypeTo(sb); + sb.append(CR); sb.append("}").append(CR); + annotationWriter.appendTopLevelTypesTo(sb); + sb.append(CR); } // ********** queries ********** + protected TestJavaProject getJavaProject() { + return this.javaProject; + } + protected IType jdtType() throws JavaModelException { return this.javaProject.findType(FULLY_QUALIFIED_TYPE_NAME); } @@ -219,6 +246,10 @@ public abstract class AnnotationTestCase extends TestCase { protected MethodAttribute idGetMethod() throws JavaModelException { return this.methodNamed("getId"); } + + protected MethodAttribute idSetMethod() throws JavaModelException { + return this.method("setId", new String[] {"I"}); + } protected MethodAttribute nameGetMethod() throws JavaModelException { return this.methodNamed("getName"); @@ -296,6 +327,9 @@ public abstract class AnnotationTestCase extends TestCase { return null; } + /** + * minimize the scope of the suppressed warnings + */ @SuppressWarnings("unchecked") protected List<MemberValuePair> values(NormalAnnotation na) { return na.values(); @@ -409,23 +443,29 @@ public abstract class AnnotationTestCase extends TestCase { public interface AnnotationWriter { Iterator<String> imports(); void appendTypeAnnotationTo(StringBuilder sb); + void appendExtendsImplementsTo(StringBuilder sb); void appendIdFieldAnnotationTo(StringBuilder sb); void appendNameFieldAnnotationTo(StringBuilder sb); void appendGetIdMethodAnnotationTo(StringBuilder sb); void appendSetIdMethodAnnotationTo(StringBuilder sb); void appendGetNameMethodAnnotationTo(StringBuilder sb); void appendSetNameMethodAnnotationTo(StringBuilder sb); + void appendMemberTypeTo(StringBuilder sb); + void appendTopLevelTypesTo(StringBuilder sb); } public static class DefaultAnnotationWriter implements AnnotationWriter { public Iterator<String> imports() {return EmptyIterator.instance();} public void appendTypeAnnotationTo(StringBuilder sb) {/* do nothing */} + public void appendExtendsImplementsTo(StringBuilder sb) {/* do nothing */} public void appendIdFieldAnnotationTo(StringBuilder sb) {/* do nothing */} public void appendNameFieldAnnotationTo(StringBuilder sb) {/* do nothing */} public void appendGetIdMethodAnnotationTo(StringBuilder sb) {/* do nothing */} public void appendSetIdMethodAnnotationTo(StringBuilder sb) {/* do nothing */} public void appendGetNameMethodAnnotationTo(StringBuilder sb) {/* do nothing */} public void appendSetNameMethodAnnotationTo(StringBuilder sb) {/* do nothing */} + public void appendMemberTypeTo(StringBuilder sb) {/* do nothing */} + public void appendTopLevelTypesTo(StringBuilder sb) {/* do nothing */} } public static class AnnotationWriterWrapper implements AnnotationWriter { @@ -436,22 +476,30 @@ public abstract class AnnotationTestCase extends TestCase { } public Iterator<String> imports() {return aw.imports();} public void appendTypeAnnotationTo(StringBuilder sb) {aw.appendTypeAnnotationTo(sb);} + public void appendExtendsImplementsTo(StringBuilder sb) {aw.appendExtendsImplementsTo(sb);} public void appendIdFieldAnnotationTo(StringBuilder sb) {aw.appendIdFieldAnnotationTo(sb);} public void appendNameFieldAnnotationTo(StringBuilder sb) {aw.appendNameFieldAnnotationTo(sb);} public void appendGetIdMethodAnnotationTo(StringBuilder sb) {aw.appendGetIdMethodAnnotationTo(sb);} public void appendSetIdMethodAnnotationTo(StringBuilder sb) {aw.appendSetIdMethodAnnotationTo(sb);} public void appendGetNameMethodAnnotationTo(StringBuilder sb) {aw.appendGetNameMethodAnnotationTo(sb);} public void appendSetNameMethodAnnotationTo(StringBuilder sb) {aw.appendSetNameMethodAnnotationTo(sb);} + public void appendMemberTypeTo(StringBuilder sb) {aw.appendMemberTypeTo(sb);} + public void appendTopLevelTypesTo(StringBuilder sb) {aw.appendTopLevelTypesTo(sb);} } public class AnnotatedSourceWriter implements SourceWriter { private AnnotationWriter annotationWriter; + private String typeName; public AnnotatedSourceWriter(AnnotationWriter annotationWriter) { + this(annotationWriter, TYPE_NAME); + } + public AnnotatedSourceWriter(AnnotationWriter annotationWriter, String typeName) { super(); this.annotationWriter = annotationWriter; + this.typeName = typeName; } public void appendSourceTo(StringBuilder sb) { - AnnotationTestCase.this.appendSourceTo(sb, this.annotationWriter); + AnnotationTestCase.this.appendSourceTo(sb, this.annotationWriter, typeName); } } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java index b07c31488a..3c0fe0a112 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java @@ -15,7 +15,7 @@ import org.eclipse.jpt.core.internal.ITextRange; import org.eclipse.jpt.core.internal.jdtutility.ASTNodeTextRange; import org.eclipse.jpt.core.internal.jdtutility.AnnotationElementAdapter; import org.eclipse.jpt.core.internal.jdtutility.AnnotationStringArrayExpressionConverter; -import org.eclipse.jpt.core.internal.jdtutility.BooleanStringExpressionConverter; +import org.eclipse.jpt.core.internal.jdtutility.BooleanExpressionConverter; import org.eclipse.jpt.core.internal.jdtutility.CharacterStringExpressionConverter; import org.eclipse.jpt.core.internal.jdtutility.ConversionDeclarationAnnotationElementAdapter; import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter; @@ -26,7 +26,7 @@ import org.eclipse.jpt.core.internal.jdtutility.ExpressionConverter; import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationElementAdapter; import org.eclipse.jpt.core.internal.jdtutility.NestedDeclarationAnnotationAdapter; import org.eclipse.jpt.core.internal.jdtutility.NestedIndexedDeclarationAnnotationAdapter; -import org.eclipse.jpt.core.internal.jdtutility.NumberStringExpressionConverter; +import org.eclipse.jpt.core.internal.jdtutility.NumberIntegerExpressionConverter; import org.eclipse.jpt.core.internal.jdtutility.PrimitiveTypeStringExpressionConverter; import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter; import org.eclipse.jpt.core.internal.jdtutility.SimpleTypeStringExpressionConverter; @@ -59,9 +59,9 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { this.createAnnotationAndMembers("Foo", "int bar();"); this.createTestType("@annot.Foo(bar=48)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa, "bar", NumberStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("48", aea.getValue()); + DeclarationAnnotationElementAdapter<Integer> daea = new ConversionDeclarationAnnotationElementAdapter<Integer>(daa, "bar", NumberIntegerExpressionConverter.instance()); + AnnotationElementAdapter<Integer> aea = new MemberAnnotationElementAdapter<Integer>(this.idField(), daea); + assertEquals(Integer.valueOf(48), aea.getValue()); } public void testGetValue3() throws Exception { @@ -77,9 +77,9 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=false)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa, "bar", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa, "bar", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValue5() throws Exception { @@ -90,18 +90,18 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValue6() throws Exception { this.createAnnotationAndMembers("Foo", "boolean value();"); this.createTestType("@annot.Foo(false)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa, BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa, BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValueNull1() throws Exception { @@ -158,18 +158,18 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { this.createAnnotationAndMembers("Foo", "int bar();"); this.createTestType("@annot.Foo(bar=47 - 7 + 2 * 1 / 1)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("42", aea.getValue()); + DeclarationAnnotationElementAdapter<Integer> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); + AnnotationElementAdapter<Integer> aea = new MemberAnnotationElementAdapter<Integer>(this.idField(), daea); + assertEquals(Integer.valueOf(42), aea.getValue()); } public void testGetValueNumberShift() throws Exception { this.createAnnotationAndMembers("Foo", "int bar();"); this.createTestType("@annot.Foo(bar=2 << 2)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("8", aea.getValue()); + DeclarationAnnotationElementAdapter<Integer> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); + AnnotationElementAdapter<Integer> aea = new MemberAnnotationElementAdapter<Integer>(this.idField(), daea); + assertEquals(Integer.valueOf(8), aea.getValue()); } public void testGetValueNumberConstant() throws Exception { @@ -177,9 +177,9 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { // just a bit hacky: this.createTestType("private static final int FOO_BAR = 77; @annot.Foo(bar=FOO_BAR)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("77", aea.getValue()); + DeclarationAnnotationElementAdapter<Integer> daea = ConversionDeclarationAnnotationElementAdapter.forNumbers(daa, "bar"); + AnnotationElementAdapter<Integer> aea = new MemberAnnotationElementAdapter<Integer>(this.idField(), daea); + assertEquals(Integer.valueOf(77), aea.getValue()); } public void testGetValueCharacterConstant() throws Exception { @@ -205,36 +205,36 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=7 > 2)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("true", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.TRUE, aea.getValue()); } public void testGetValueBooleanOperator2() throws Exception { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=7 == 2)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValueBooleanOperator3() throws Exception { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=(7 != 2) && false)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValueBooleanOperator4() throws Exception { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=(7 != 2) ? false : true)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); } public void testGetValueInvalidValue1() throws Exception { @@ -259,8 +259,8 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { this.createAnnotationAndMembers("Foo", "boolean bar();"); this.createTestType("@annot.Foo(bar=bazzzz)"); DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo"); - DeclarationAnnotationElementAdapter<String> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = ConversionDeclarationAnnotationElementAdapter.forBooleans(daa, "bar"); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); assertNull(aea.getValue()); } @@ -303,8 +303,8 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); assertNull(aea.getValue()); } @@ -339,7 +339,7 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { ITextRange textRange = new ASTNodeTextRange(aea.astNode()); assertEquals(this.source().indexOf(value), textRange.getOffset()); assertEquals(value.length(), textRange.getLength()); - assertEquals(7, textRange.getLineNumber()); + assertEquals(8, textRange.getLineNumber()); } public void testASTNode2() throws Exception { @@ -353,9 +353,9 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); - assertEquals("false", aea.getValue()); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); + assertEquals(Boolean.FALSE, aea.getValue()); ITextRange textRange = new ASTNodeTextRange(aea.astNode()); assertEquals(value.length(), textRange.getLength()); } @@ -424,8 +424,8 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); aea.setValue(null); this.assertSourceDoesNotContain(annotation); @@ -443,8 +443,8 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar", false); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz", false); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); aea.setValue(null); this.assertSourceDoesNotContain(annotation); @@ -472,10 +472,10 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); - aea.setValue("true"); + aea.setValue(Boolean.TRUE); this.assertSourceDoesNotContain(annotation); this.assertSourceContains("@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=true)))"); } @@ -488,10 +488,10 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", BooleanStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Boolean> daea = new ConversionDeclarationAnnotationElementAdapter<Boolean>(daa3, "fred", BooleanExpressionConverter.instance()); + AnnotationElementAdapter<Boolean> aea = new MemberAnnotationElementAdapter<Boolean>(this.idField(), daea); - aea.setValue("true"); + aea.setValue(Boolean.TRUE); this.assertSourceContains("@Foo(@Bar(jimmy=@Baz(fred=true)))"); } @@ -560,11 +560,11 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase { DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo"); DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar"); DeclarationAnnotationAdapter daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "jimmy", 2, "annot.Baz"); - DeclarationAnnotationElementAdapter<String> daea = new ConversionDeclarationAnnotationElementAdapter<String>(daa3, "fred", NumberStringExpressionConverter.instance()); - AnnotationElementAdapter<String> aea = new MemberAnnotationElementAdapter<String>(this.idField(), daea); + DeclarationAnnotationElementAdapter<Integer> daea = new ConversionDeclarationAnnotationElementAdapter<Integer>(daa3, "fred", NumberIntegerExpressionConverter.instance()); + AnnotationElementAdapter<Integer> aea = new MemberAnnotationElementAdapter<Integer>(this.idField(), daea); - assertEquals("2", aea.getValue()); - aea.setValue("48"); + assertEquals(Integer.valueOf(2), aea.getValue()); + aea.setValue(Integer.valueOf(48)); this.assertSourceContains("@annot.Foo(@annot.Bar(jimmy={@annot.Baz(fred=0), @annot.Baz(fred=1), @annot.Baz(fred=48), @annot.Baz(fred=3)}))"); } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java index 47dde10180..60a7078554 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java @@ -41,10 +41,6 @@ public class TypeTests extends AnnotationTestCase { assertEquals(this.jdtType, this.testType.getJdtMember()); } - public void testIsAbstract() throws Exception { - assertFalse(this.testType.isAbstract()); - } - public void testTopLevelDeclaringType() throws Exception { assertEquals(this.testType, this.testType.topLevelDeclaringType()); } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaModelTests.java index 8b611e5559..640b33814d 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaModelTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaModelTests.java @@ -47,7 +47,7 @@ public class JpaModelTests extends TestCase { } private boolean debug() { - Boolean debug = (Boolean) ClassTools.getStaticFieldValue(JpaModelManager.class, "DEBUG"); + Boolean debug = (Boolean) ClassTools.staticFieldValue(JpaModelManager.class, "DEBUG"); return debug.booleanValue(); } @@ -120,103 +120,99 @@ public class JpaModelTests extends TestCase { assertNull(jpaProject); } -/* Commented out the following three tests as they are unreliable on linux running on the IBM JDK - * which describes the current build/test environment. - */ - -// public void testProjectCloseReopen() throws Exception { -// TestFacetedProject testProject = this.buildTestProject(); -// testProject.installFacet("jpt.jpa", "1.0"); -// -// testProject.getProject().close(null); -// assertFalse(testProject.getProject().isOpen()); -// IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); -// assertNull(jpaProject); -// -// testProject.getProject().open(null); -// jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); -// assertNotNull(jpaProject); -//// assertEquals(4, jpaProject.jpaFilesSize()); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); -// // persistence.xml and orm.xml are created in the background, so they probably -// // won't be there yet... -//// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); -//// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); -// } -// -// public void testProjectDeleteReimport() throws Exception { -// TestFacetedProject testProject = this.buildTestProject(); -// testProject.installFacet("jpt.jpa", "1.0"); -// IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); -// assertNotNull(jpaProject); -// assertEquals(1, JptCorePlugin.jpaModel().jpaProjectsSize()); -// -// testProject.getProject().delete(false, true, null); + public void testProjectCloseReopen() throws Exception { + TestFacetedProject testProject = this.buildTestProject(); + testProject.installFacet("jpt.jpa", "1.0"); + + testProject.getProject().close(null); + assertFalse(testProject.getProject().isOpen()); + IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); + assertNull(jpaProject); + + testProject.getProject().open(null); + jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); + assertNotNull(jpaProject); +// assertEquals(4, jpaProject.jpaFilesSize()); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); + // persistence.xml and orm.xml are created in the background, so they probably + // won't be there yet... +// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); +// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); + } + + public void testProjectDeleteReimport() throws Exception { + TestFacetedProject testProject = this.buildTestProject(); + testProject.installFacet("jpt.jpa", "1.0"); + IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); + assertNotNull(jpaProject); + assertEquals(1, JptCorePlugin.jpaModel().jpaProjectsSize()); + + testProject.getProject().delete(false, true, null); + jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); + assertNull(jpaProject); + assertEquals(0, JptCorePlugin.jpaModel().jpaProjectsSize()); + assertEquals(0, ResourcesPlugin.getWorkspace().getRoot().getProjects().length); + + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testProject.getProject().getName()); + project.create(null); + assertEquals(1, ResourcesPlugin.getWorkspace().getRoot().getProjects().length); + project.open(null); + + assertTrue(project.isOpen()); + assertTrue(JptCorePlugin.projectHasJpaFacet(project)); + jpaProject = JptCorePlugin.jpaProject(project); + assertNotNull(jpaProject); +// assertEquals(4, jpaProject.jpaFilesSize()); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); + // persistence.xml and orm.xml are created in the background, so they probably + // won't be there yet... +// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); +// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); + } + + public void testEditFacetSettingsFile() throws Exception { + TestFacetedProject testProject = this.buildTestProject(); + assertNull(JptCorePlugin.jpaProject(testProject.getProject())); + + // add the JPA facet by modifying the facet settings file directly + IFile facetSettingsFile = this.file(testProject, ".settings/org.eclipse.wst.common.project.facet.core.xml"); + InputStream inStream = new BufferedInputStream(facetSettingsFile.getContents()); + int fileSize = inStream.available(); + byte[] buf = new byte[fileSize]; + inStream.read(buf); + inStream.close(); + + String oldDocument = new String(buf); + String oldString = "<installed facet=\"jst.utility\" version=\"1.0\"/>"; + String newString = oldString + CR + " " + "<installed facet=\"jpt.jpa\" version=\"1.0\"/>"; + String newDocument = oldDocument.replaceAll(oldString, newString); + + facetSettingsFile.setContents(new ByteArrayInputStream(newDocument.getBytes()), false, false, null); + + assertEquals(1, JptCorePlugin.jpaModel().jpaProjectsSize()); + IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); + assertNotNull(jpaProject); + // persistence.xml and orm.xml do not get created in this situation (?) + assertEquals(2, jpaProject.jpaFilesSize()); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); + assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); + // assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); + // assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); + + // now remove the JPA facet + facetSettingsFile.setContents(new ByteArrayInputStream(oldDocument.getBytes()), false, false, null); +// TODO moved this stuff to the error console until we can figure out why it fails intermittently ~bjv +// assertEquals(0, JptCorePlugin.jpaModel().jpaProjectsSize()); // jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); // assertNull(jpaProject); -// assertEquals(0, JptCorePlugin.jpaModel().jpaProjectsSize()); -// assertEquals(0, ResourcesPlugin.getWorkspace().getRoot().getProjects().length); -// -// IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testProject.getProject().getName()); -// project.create(null); -// assertEquals(1, ResourcesPlugin.getWorkspace().getRoot().getProjects().length); -// project.open(null); -// -// assertTrue(project.isOpen()); -// assertTrue(JptCorePlugin.projectHasJpaFacet(project)); -// jpaProject = JptCorePlugin.jpaProject(project); -// assertNotNull(jpaProject); -//// assertEquals(4, jpaProject.jpaFilesSize()); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); -// // persistence.xml and orm.xml are created in the background, so they probably -// // won't be there yet... -//// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); -//// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); -// } - -// public void testEditFacetSettingsFile() throws Exception { -// TestFacetedProject testProject = this.buildTestProject(); -// assertNull(JptCorePlugin.jpaProject(testProject.getProject())); -// -// // add the JPA facet by modifying the facet settings file directly -// IFile facetSettingsFile = this.file(testProject, ".settings/org.eclipse.wst.common.project.facet.core.xml"); -// InputStream inStream = new BufferedInputStream(facetSettingsFile.getContents()); -// int fileSize = inStream.available(); -// byte[] buf = new byte[fileSize]; -// inStream.read(buf); -// inStream.close(); -// -// String oldDocument = new String(buf); -// String oldString = "<installed facet=\"jst.utility\" version=\"1.0\"/>"; -// String newString = oldString + CR + " " + "<installed facet=\"jpt.jpa\" version=\"1.0\"/>"; -// String newDocument = oldDocument.replaceAll(oldString, newString); -// -// facetSettingsFile.setContents(new ByteArrayInputStream(newDocument.getBytes()), false, false, null); -// -// assertEquals(1, JptCorePlugin.jpaModel().jpaProjectsSize()); -// IJpaProject jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); -// assertNotNull(jpaProject); -// // persistence.xml and orm.xml do not get created in this situation (?) -// assertEquals(2, jpaProject.jpaFilesSize()); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity.java"))); -// assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/test.pkg/TestEntity2.java"))); -// // assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/persistence.xml"))); -// // assertNotNull(jpaProject.jpaFile(this.file(testProject, "src/META-INF/orm.xml"))); -// -// // now remove the JPA facet -// facetSettingsFile.setContents(new ByteArrayInputStream(oldDocument.getBytes()), false, false, null); -//// TODO moved this stuff to the error console until we can figure out why it fails intermittently ~bjv -//// assertEquals(0, JptCorePlugin.jpaModel().jpaProjectsSize()); -//// jpaProject = JptCorePlugin.jpaProject(testProject.getProject()); -//// assertNull(jpaProject); -// int size = JptCorePlugin.jpaModel().jpaProjectsSize(); -// if (size != 0) { -// System.err.println("bogus size: " + size); -// System.err.println("bogus project: " + JptCorePlugin.jpaProject(testProject.getProject())); -// } -// } + int size = JptCorePlugin.jpaModel().jpaProjectsSize(); + if (size != 0) { + System.err.println("bogus size: " + size); + System.err.println("bogus project: " + JptCorePlugin.jpaProject(testProject.getProject())); + } + } /** * make sure the DEBUG constants are 'false' before checking in the code @@ -228,7 +224,7 @@ public class JpaModelTests extends TestCase { private void verifyDEBUG(Class<?> clazz) { assertFalse("Recompile with \"DEBUG = false\": " + clazz.getName(), - ((Boolean) ClassTools.getStaticFieldValue(clazz, "DEBUG")).booleanValue()); + ((Boolean) ClassTools.staticFieldValue(clazz, "DEBUG")).booleanValue()); } } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/BaseJpaPlatformTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/BaseJpaPlatformTests.java index 3266cb3693..07c4967a77 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/BaseJpaPlatformTests.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/BaseJpaPlatformTests.java @@ -1,30 +1,8 @@ -/******************************************************************************* - * Copyright (c) 2007 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.core.tests.internal.platform; -import java.io.IOException; import junit.framework.TestCase; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.jpt.core.internal.IJpaFile; -import org.eclipse.jpt.core.internal.content.orm.EntityMappingsInternal; -import org.eclipse.jpt.core.internal.content.orm.OrmFactory; -import org.eclipse.jpt.core.internal.content.orm.XmlEntityInternal; -import org.eclipse.jpt.core.internal.content.orm.XmlRootContentNode; -import org.eclipse.jpt.core.internal.content.persistence.JavaClassRef; -import org.eclipse.jpt.core.internal.content.persistence.Persistence; -import org.eclipse.jpt.core.internal.content.persistence.PersistenceFactory; -import org.eclipse.jpt.core.internal.content.persistence.PersistenceXmlRootContentNode; import org.eclipse.jpt.core.tests.internal.ProjectUtility; import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; -import org.eclipse.jpt.utility.internal.CollectionTools; public class BaseJpaPlatformTests extends TestCase { @@ -58,34 +36,33 @@ public class BaseJpaPlatformTests extends TestCase super.tearDown(); } - - public void testPersistentTypes() throws CoreException, IOException { - IFile persistenceXmlIFile = jpaProject.getProject().getFile(PERSISTENCE_XML_LOCATION); - IJpaFile persistenceXmlJpaFile = jpaProject.getJpaProject().jpaFile(persistenceXmlIFile); - PersistenceXmlRootContentNode persistenceRoot = (PersistenceXmlRootContentNode) persistenceXmlJpaFile.getContent(); - Persistence persistence = persistenceRoot.getPersistence(); - - IFile ormXmlIFile = jpaProject.getProject().getFile(ORM_XML_LOCATION); - IJpaFile ormXmlJpaFile = jpaProject.getJpaProject().jpaFile(ormXmlIFile); - XmlRootContentNode ormRoot = (XmlRootContentNode) ormXmlJpaFile.getContent(); - EntityMappingsInternal entityMappings = ormRoot.getEntityMappings(); - - // add xml persistent type - XmlEntityInternal xmlEntity = OrmFactory.eINSTANCE.createXmlEntityInternal(); - xmlEntity.setSpecifiedName("XmlEntity"); - entityMappings.getTypeMappings().add(xmlEntity); - entityMappings.eResource().save(null); - assertEquals(1, CollectionTools.size(jpaProject.getJpaProject().jpaPlatform().persistentTypes(PROJECT_NAME))); - - // add java persistent type - jpaProject.createType(PACKAGE_NAME, "JavaEntity.java", - "@Entity public class JavaEntity {}" - ); - JavaClassRef javaClassRef = PersistenceFactory.eINSTANCE.createJavaClassRef(); - javaClassRef.setJavaClass(PACKAGE_NAME + ".JavaEntity"); - persistence.getPersistenceUnits().get(0).getClasses().add(javaClassRef); - persistence.eResource().save(null); - - assertEquals(2, CollectionTools.size(jpaProject.getJpaProject().jpaPlatform().persistentTypes(PROJECT_NAME))); - } +// public void testPersistentTypes() throws CoreException, IOException { +// IFile persistenceXmlIFile = jpaProject.getProject().getFile(PERSISTENCE_XML_LOCATION); +// IJpaFile persistenceXmlJpaFile = jpaProject.getJpaProject().getJpaFile(persistenceXmlIFile); +// PersistenceXmlRootContentNode persistenceRoot = (PersistenceXmlRootContentNode) persistenceXmlJpaFile.getContent(); +// XmlPersistence persistence = persistenceRoot.getPersistence(); +// +// IFile ormXmlIFile = jpaProject.getProject().getFile(ORM_XML_LOCATION); +// IJpaFile ormXmlJpaFile = jpaProject.getJpaProject().getJpaFile(ormXmlIFile); +// XmlRootContentNode ormRoot = (XmlRootContentNode) ormXmlJpaFile.getContent(); +// EntityMappingsInternal entityMappings = ormRoot.getEntityMappings(); +// +// // add xml persistent type +// XmlEntityInternal xmlEntity = OrmFactory.eINSTANCE.createXmlEntityInternal(); +// xmlEntity.setSpecifiedName("XmlEntity"); +// entityMappings.getTypeMappings().add(xmlEntity); +// entityMappings.eResource().save(null); +// assertEquals(1, CollectionTools.size(jpaProject.getJpaProject().getPlatform().persistentTypes(PROJECT_NAME))); +// +// // add java persistent type +// jpaProject.createType(PACKAGE_NAME, "JavaEntity.java", +// "@Entity public class JavaEntity {}" +// ); +// XmlJavaClassRef javaClassRef = PersistenceFactory.eINSTANCE.createJavaClassRef(); +// javaClassRef.setJavaClass(PACKAGE_NAME + ".JavaEntity"); +// persistence.getPersistenceUnits().get(0).getClasses().add(javaClassRef); +// persistence.eResource().save(null); +// +// assertEquals(2, CollectionTools.size(jpaProject.getJpaProject().getPlatform().persistentTypes(PROJECT_NAME))); +// } } diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/JptCorePlatformTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/JptCorePlatformTests.java deleted file mode 100644 index 1052e87abb..0000000000 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/platform/JptCorePlatformTests.java +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006, 2007 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.core.tests.internal.platform; - -import junit.framework.Test; -import junit.framework.TestSuite; - -public class JptCorePlatformTests { - - public static Test suite() { - return suite(true); - } - - public static Test suite(boolean all) { - TestSuite suite = new TestSuite(JptCorePlatformTests.class.getPackage().getName()); - suite.addTestSuite(JpaPlatformExtensionTests.class); - if (all) { - suite.addTestSuite(BaseJpaPlatformTests.class); - suite.addTestSuite(JpaPlatformTests.class); - } - return suite; - } - - private JptCorePlatformTests() { - super(); - throw new UnsupportedOperationException(); - } -} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java index 30af1c30fa..f892e3a2f3 100644 --- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2007 Oracle. All rights reserved. + * Copyright (c) 2005, 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. @@ -12,6 +12,7 @@ package org.eclipse.jpt.core.tests.internal.projects; import org.eclipse.core.runtime.CoreException; import org.eclipse.jpt.core.internal.IJpaProject; import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.SynchronousJpaProjectUpdater; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; /** @@ -51,6 +52,7 @@ public class TestJpaProject extends TestJavaProject { this.installFacet("jpt.jpa", "1.0", jpaConfig); this.addJar(this.jarName()); this.jpaProject = JptCorePlugin.jpaProject(this.getProject()); + this.jpaProject.setUpdater(new SynchronousJpaProjectUpdater(this.jpaProject)); } protected String jarName() { diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/JptCoreResourceModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/JptCoreResourceModelTests.java new file mode 100644 index 0000000000..03d9784a6b --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/JptCoreResourceModelTests.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.eclipse.jpt.core.tests.internal.resource.java.JptJavaResourceTests; + +public class JptCoreResourceModelTests extends TestCase +{ + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptCoreResourceModelTests.class.getName()); + suite.addTest(JptJavaResourceTests.suite(all)); + suite.addTestSuite(OrmModelTests.class); + suite.addTestSuite(PersistenceModelTests.class); + return suite; + } + + private JptCoreResourceModelTests() { + super(); + throw new UnsupportedOperationException(); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/OrmModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/OrmModelTests.java new file mode 100644 index 0000000000..78746ac13c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/OrmModelTests.java @@ -0,0 +1,46 @@ +package org.eclipse.jpt.core.tests.internal.resource; + +import junit.framework.TestCase; +import org.eclipse.jpt.core.internal.resource.orm.OrmArtifactEdit; +import org.eclipse.jpt.core.internal.resource.orm.OrmResource; +import org.eclipse.jpt.core.tests.internal.ProjectUtility; +import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; + +public class OrmModelTests extends TestCase +{ + static final String BASE_PROJECT_NAME = OrmModelTests.class.getSimpleName(); + + TestJpaProject jpaProject; + + + public OrmModelTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + ProjectUtility.deleteAllProjects(); + jpaProject = TestJpaProject.buildJpaProject(BASE_PROJECT_NAME, false); // false = no auto-build + } + + @Override + protected void tearDown() throws Exception { + jpaProject = null; + super.tearDown(); + } + + public void testModelLoad() { + OrmArtifactEdit artifactEdit = OrmArtifactEdit.getArtifactEditForRead(jpaProject.getProject()); + assertNotNull(artifactEdit); + OrmResource resource = artifactEdit.getResource("META-INF/orm.xml"); + assertNotNull(resource); + } + + public void testModelLoad2() { + OrmArtifactEdit artifactEdit = OrmArtifactEdit.getArtifactEditForRead(jpaProject.getProject()); + assertNotNull(artifactEdit); + OrmResource resource = artifactEdit.getResource("META-INF/orm.xml"); + assertNotNull(resource); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/PersistenceModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/PersistenceModelTests.java new file mode 100644 index 0000000000..571ee4e0cc --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/PersistenceModelTests.java @@ -0,0 +1,46 @@ +package org.eclipse.jpt.core.tests.internal.resource; + +import junit.framework.TestCase; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceArtifactEdit; +import org.eclipse.jpt.core.internal.resource.persistence.PersistenceResource; +import org.eclipse.jpt.core.tests.internal.ProjectUtility; +import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; + +public class PersistenceModelTests extends TestCase +{ + static final String BASE_PROJECT_NAME = PersistenceModelTests.class.getSimpleName(); + + TestJpaProject jpaProject; + + + public PersistenceModelTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + ProjectUtility.deleteAllProjects(); + jpaProject = TestJpaProject.buildJpaProject(BASE_PROJECT_NAME, false); // false = no auto-build + } + + @Override + protected void tearDown() throws Exception { + jpaProject = null; + super.tearDown(); + } + + public void testModelLoad() { + PersistenceArtifactEdit artifactEdit = PersistenceArtifactEdit.getArtifactEditForRead(jpaProject.getProject()); + assertNotNull(artifactEdit); + PersistenceResource resource = artifactEdit.getResource("META-INF/persistence.xml"); + assertNotNull(resource); + } + + public void testModelLoad2() { + PersistenceArtifactEdit artifactEdit = PersistenceArtifactEdit.getArtifactEditForRead(jpaProject.getProject()); + assertNotNull(artifactEdit); + PersistenceResource resource = artifactEdit.getResource("META-INF/persistence.xml"); + assertNotNull(resource); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverrideTests.java new file mode 100644 index 0000000000..5413ea1b59 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverrideTests.java @@ -0,0 +1,283 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverride; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class AssociationOverrideTests extends JavaResourceModelTestCase { + + private static final String ASSOCIATION_OVERRIDE_NAME = "MY_ASSOCIATION_OVERRIDE"; + + public AssociationOverrideTests(String name) { + super(name); + } + + private void createAssociationOverrideAnnotation() throws Exception { + createJoinColumnAnnotation(); + this.createAnnotationAndMembers("AssociationOverride", + "String name(); " + + "JoinColumn[] joinColumns(); "); + } + + private void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + } + + private IType createTestAssociationOverrideOnField() throws Exception { + createAssociationOverrideAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ASSOCIATION_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\")"); + } + }); + } + + private IType createTestAssociationOverrideWithJoinColumns() throws Exception { + createAssociationOverrideAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ASSOCIATION_OVERRIDE, JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn})"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + assertNotNull(associationOverride); + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + assertNotNull(associationOverride); + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + + associationOverride.setName("Foo"); + assertEquals("Foo", associationOverride.getName()); + assertSourceContains("@AssociationOverride(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + + associationOverride.setName(null); + assertNull(associationOverride.getName()); + + assertSourceDoesNotContain("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\")"); + assertSourceContains("@AssociationOverride"); + } + + + public void testJoinColumns() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testJoinColumns2() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + + associationOverride.addJoinColumn(0); + associationOverride.addJoinColumn(1); + associationOverride.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testJoinColumns3() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + associationOverride.addJoinColumn(0).setName("FOO"); + associationOverride.addJoinColumn(1); + associationOverride.addJoinColumn(0).setName("BAR"); + + assertEquals("BAR", associationOverride.joinColumnAt(0).getName()); + assertEquals("FOO", associationOverride.joinColumnAt(1).getName()); + assertNull(associationOverride.joinColumnAt(2).getName()); + + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns = {@JoinColumn(name=\"BAR\"),@JoinColumn(name=\"FOO\"), @JoinColumn})"); + } + + public void testRemoveJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + associationOverride.addJoinColumn(0).setName("FOO"); + + Iterator<JoinColumn> joinColumns = associationOverride.joinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\"), @JoinColumn})"); + + associationOverride.removeJoinColumn(1); + joinColumns = associationOverride.joinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn})"); + + associationOverride.removeJoinColumn(0); + joinColumns = associationOverride.joinColumns(); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns=@JoinColumn)"); + + + associationOverride.setName(null); + associationOverride.removeJoinColumn(0); + assertSourceDoesNotContain("@AssociationOverride"); + } + + public void testMoveJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + JoinColumn joinColumn = associationOverride.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + associationOverride.addJoinColumn(0).setName("FOO"); + + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn})"); + + associationOverride.moveJoinColumn(2, 0); + assertEquals("BAR", associationOverride.joinColumnAt(0).getName()); + assertNull(associationOverride.joinColumnAt(1).getName()); + assertEquals("FOO", associationOverride.joinColumnAt(2).getName()); + assertEquals(3, associationOverride.joinColumnsSize()); + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn, @JoinColumn(name=\"FOO\")})"); + } + + public void testMoveJoinColumn2() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + JoinColumn joinColumn = associationOverride.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + associationOverride.addJoinColumn(0).setName("FOO"); + + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn})"); + + associationOverride.moveJoinColumn(0, 2); + assertNull(associationOverride.joinColumnAt(0).getName()); + assertEquals("FOO", associationOverride.joinColumnAt(1).getName()); + assertEquals("BAR", associationOverride.joinColumnAt(2).getName()); + assertEquals(3, associationOverride.joinColumnsSize()); + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn, @JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\")})"); + } + + public void testSetJoinColumnName() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDE); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + assertEquals(2, CollectionTools.size(iterator)); + + JoinColumn joinColumn = associationOverride.joinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"foo\"), @JoinColumn})"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverridesTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverridesTests.java new file mode 100644 index 0000000000..6a4c7c1f52 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AssociationOverridesTests.java @@ -0,0 +1,355 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverride; +import org.eclipse.jpt.core.internal.resource.java.AssociationOverrides; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class AssociationOverridesTests extends JavaResourceModelTestCase { + + private static final String ASSOCIATION_OVERRIDE_NAME = "MY_ASSOCIATION_OVERRIDE"; + + public AssociationOverridesTests(String name) { + super(name); + } + + private void createAssociationOverrideAnnotation() throws Exception { + createJoinColumnAnnotation(); + this.createAnnotationAndMembers("AssociationOverride", + "String name(); " + + "Column column(); "); + } + + private void createAssociationOverridesAnnotation() throws Exception { + createAssociationOverrideAnnotation(); + this.createAnnotationAndMembers("AssociationOverrides", + "AssociationOverride[] value();"); + } + + private void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + + } + + private IType createTestAssociationOverrideOnField() throws Exception { + createAssociationOverridesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\"))"); + } + }); + } + private IType createTestAssociationOverrideWithJoinColumns() throws Exception { + createAssociationOverridesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES, JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn}))"); + } + }); + } + + private IType createTestAssociationOverride() throws Exception { + createAssociationOverrideAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ASSOCIATION_OVERRIDE, JPA.JOIN_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@AssociationOverride(name=\"FOO\", joinColumns=@JoinColumn(name=\"FOO\", columnDefinition=\"BAR\", referencedColumnName=\"BAZ\"))"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverrides associationOverrides = (AssociationOverrides) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDES); + AssociationOverride associationOverride = associationOverrides.nestedAnnotations().next(); + + assertNotNull(associationOverride); + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverrides associationOverrides = (AssociationOverrides) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDES); + AssociationOverride associationOverride = associationOverrides.nestedAnnotations().next(); + + assertNotNull(associationOverride); + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + + associationOverride.setName("Foo"); + assertEquals("Foo", associationOverride.getName()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"Foo\"))"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AssociationOverrides associationOverrides = (AssociationOverrides) attributeResource.annotation(JPA.ASSOCIATION_OVERRIDES); + AssociationOverride associationOverride = associationOverrides.nestedAnnotations().next(); + assertEquals(ASSOCIATION_OVERRIDE_NAME, associationOverride.getName()); + + associationOverride.setName(null); + assertNull(associationOverride.getName()); + + assertSourceDoesNotContain("@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\")"); + assertSourceContains("@AssociationOverride"); + assertSourceContains("@AssociationOverrides"); + } + + public void testAddAssociationOverrideCopyExisting() throws Exception { + IType jdtType = createTestAssociationOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AssociationOverride associationOverride = (AssociationOverride) typeResource.addAnnotation(1, JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + associationOverride.setName("BAR"); + assertSourceContains("@AssociationOverrides({@AssociationOverride(name=\"FOO\", joinColumns = @JoinColumn(name=\"FOO\", columnDefinition = \"BAR\", referencedColumnName = \"BAZ\")),@AssociationOverride(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.ASSOCIATION_OVERRIDE)); + assertNotNull(typeResource.annotation(JPA.ASSOCIATION_OVERRIDES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES))); + } + + public void testAddAssociationOverrideToBeginningOfList() throws Exception { + IType jdtType = createTestAssociationOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AssociationOverride associationOverride = (AssociationOverride) typeResource.addAnnotation(1, JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + associationOverride.setName("BAR"); + assertSourceContains("@AssociationOverrides({@AssociationOverride(name=\"FOO\", joinColumns = @JoinColumn(name=\"FOO\", columnDefinition = \"BAR\", referencedColumnName = \"BAZ\")),@AssociationOverride(name=\"BAR\")})"); + + associationOverride = (AssociationOverride) typeResource.addAnnotation(0, JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + associationOverride.setName("BAZ"); + assertSourceContains("@AssociationOverrides({@AssociationOverride(name=\"BAZ\"),@AssociationOverride(name=\"FOO\", joinColumns = @JoinColumn(name=\"FOO\", columnDefinition = \"BAR\", referencedColumnName = \"BAZ\")), @AssociationOverride(name=\"BAR\")})"); + + Iterator<JavaResource> associationOverrides = typeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + assertEquals("BAZ", ((AssociationOverride) associationOverrides.next()).getName()); + assertEquals("FOO", ((AssociationOverride) associationOverrides.next()).getName()); + assertEquals("BAR", ((AssociationOverride) associationOverrides.next()).getName()); + + assertNull(typeResource.annotation(JPA.ASSOCIATION_OVERRIDE)); + assertNotNull(typeResource.annotation(JPA.ASSOCIATION_OVERRIDES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES))); + } + + public void testRemoveAssociationOverrideCopyExisting() throws Exception { + IType jdtType = createTestAssociationOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AssociationOverride associationOverride = (AssociationOverride) typeResource.addAnnotation(1, JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + associationOverride.setName("BAR"); + assertSourceContains("@AssociationOverrides({@AssociationOverride(name=\"FOO\", joinColumns = @JoinColumn(name=\"FOO\", columnDefinition = \"BAR\", referencedColumnName = \"BAZ\")),@AssociationOverride(name=\"BAR\")})"); + + typeResource.removeAnnotation(1, JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES); + assertSourceContains("@AssociationOverride(name=\"FOO\", joinColumns = @JoinColumn(name=\"FOO\", columnDefinition = \"BAR\", referencedColumnName = \"BAZ\"))"); + } + + public void testJoinColumns() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testJoinColumns2() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + associationOverride.addJoinColumn(0); + associationOverride.addJoinColumn(1); + associationOverride.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testJoinColumns3() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + associationOverride.addJoinColumn(0).setName("FOO"); + associationOverride.addJoinColumn(1); + associationOverride.addJoinColumn(0).setName("BAR"); + + + Iterator<JoinColumn> joinColumns = associationOverride.joinColumns(); + assertEquals("BAR", joinColumns.next().getName()); + assertEquals("FOO", joinColumns.next().getName()); + assertNull(joinColumns.next().getName()); + + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns = {@JoinColumn(name=\"BAR\"),@JoinColumn(name=\"FOO\"), @JoinColumn}))"); + } + + public void testRemoveJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + associationOverride.addJoinColumn(0).setName("FOO"); + + Iterator<JoinColumn> joinColumns = associationOverride.joinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertEquals("BAR", joinColumns.next().getName()); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\"), @JoinColumn}))"); + + associationOverride.removeJoinColumn(1); + joinColumns = associationOverride.joinColumns(); + assertEquals("FOO", joinColumns.next().getName()); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn}))"); + + associationOverride.removeJoinColumn(0); + joinColumns = associationOverride.joinColumns(); + assertNull(joinColumns.next().getName()); + assertEquals(false, joinColumns.hasNext()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns=@JoinColumn))"); + + + associationOverride.setName(null); + associationOverride.removeJoinColumn(0); + assertSourceDoesNotContain("@AssociationOverride"); + } + + public void testMoveJoinColumn() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + JoinColumn joinColumn = associationOverride.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + associationOverride.addJoinColumn(0).setName("FOO"); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn}))"); + + associationOverride.moveJoinColumn(2, 0); + assertEquals("BAR", associationOverride.joinColumnAt(0).getName()); + assertNull(associationOverride.joinColumnAt(1).getName()); + assertEquals("FOO", associationOverride.joinColumnAt(2).getName()); + assertEquals(3, associationOverride.joinColumnsSize()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn, @JoinColumn(name=\"FOO\")}))"); + } + + public void testMoveJoinColumn2() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + JoinColumn joinColumn = associationOverride.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + associationOverride.addJoinColumn(0).setName("FOO"); + + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn}))"); + associationOverride.moveJoinColumn(0, 2); + assertNull(associationOverride.joinColumnAt(0).getName()); + assertEquals("FOO", associationOverride.joinColumnAt(1).getName()); + assertEquals("BAR", associationOverride.joinColumnAt(2).getName()); + assertEquals(3, associationOverride.joinColumnsSize()); + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn, @JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\")}))"); + } + + public void testSetJoinColumnName() throws Exception { + IType testType = this.createTestAssociationOverrideWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AssociationOverride associationOverride = (AssociationOverride) attributeResource.annotations(JPA.ASSOCIATION_OVERRIDE, JPA.ASSOCIATION_OVERRIDES).next(); + + ListIterator<JoinColumn> iterator = associationOverride.joinColumns(); + assertEquals(2, CollectionTools.size(iterator)); + + JoinColumn joinColumn = associationOverride.joinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@AssociationOverrides(@AssociationOverride(name=\"" + ASSOCIATION_OVERRIDE_NAME + "\", joinColumns={@JoinColumn(name=\"foo\"), @JoinColumn}))"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverrideTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverrideTests.java new file mode 100644 index 0000000000..663f02bc33 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverrideTests.java @@ -0,0 +1,176 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class AttributeOverrideTests extends JavaResourceModelTestCase { + + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String ATTRIBUTE_OVERRIDE_NAME = "MY_ATTRIBUTE_OVERRIDE"; + + public AttributeOverrideTests(String name) { + super(name); + } + + private void createAttributeOverrideAnnotation() throws Exception { + this.createAnnotationAndMembers("AttributeOverride", + "String name(); " + + "Column column(); "); + } + + private void createColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("Column", + "String name() default \"\"; " + + "boolean unique() default false; " + + "boolean nullable() default true; " + + "boolean insertable() default true; " + + "boolean updatable() default true; " + + "String columnDefinition() default \"\"; " + + "String table() default \"\"; " + + "int length() default 255; " + + "int precision() default 0; " + + "int scale() default 0;"); + } + + private IType createTestAttributeOverrideOnField() throws Exception { + createAttributeOverrideAnnotation(); + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")"); + } + }); + } + + private IType createTestAttributeOverrideWithColumnOnField() throws Exception { + createAttributeOverrideAnnotation(); + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column = @Column(name=\"" + COLUMN_NAME + "\"))"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + + assertNotNull(attributeOverride); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + } + + public void testGetNullColumn() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column column = attributeOverride.getColumn(); + assertNotNull(attributeOverride); + assertNull(column); + } + + public void testSetName() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + + assertNotNull(attributeOverride); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + + attributeOverride.setName("Foo"); + assertEquals("Foo", attributeOverride.getName()); + assertSourceContains("@AttributeOverride(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + + attributeOverride.setName(null); + assertNull(attributeOverride.getName()); + + assertSourceDoesNotContain("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")"); + assertSourceContains("@AttributeOverride"); + } + + public void testColumnGetName() throws Exception { + IType testType = this.createTestAttributeOverrideWithColumnOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column column = attributeOverride.getColumn(); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testColumnSetName() throws Exception { + IType testType = this.createTestAttributeOverrideWithColumnOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column column = attributeOverride.getColumn(); + assertEquals(COLUMN_NAME, column.getName()); + + column.setName("Foo"); + + assertSourceContains("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column = @Column(name=\"Foo\"))"); + + column.setName(null); + //TODO should I have to update from java before column is set to null?? + attributeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertNull(attributeOverride.getColumn()); + assertSourceContains("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")"); + } + + public void testAddColumn() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column column = attributeOverride.getColumn(); + assertNull(column); + } + + public void testRemoveColumn() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE); + Column column = attributeOverride.getColumn(); + assertNull(column); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverridesTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverridesTests.java new file mode 100644 index 0000000000..f18d1e89be --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/AttributeOverridesTests.java @@ -0,0 +1,303 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverrides; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class AttributeOverridesTests extends JavaResourceModelTestCase { + + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String ATTRIBUTE_OVERRIDE_NAME = "MY_ATTRIBUTE_OVERRIDE"; + + public AttributeOverridesTests(String name) { + super(name); + } + + private void createAttributeOverrideAnnotation() throws Exception { + createColumnAnnotation(); + this.createAnnotationAndMembers("AttributeOverride", + "String name(); " + + "Column column(); "); + } + + private void createAttributeOverridesAnnotation() throws Exception { + createAttributeOverrideAnnotation(); + this.createAnnotationAndMembers("AttributeOverrides", + "AttributeOverride[] value();"); + } + + private void createColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("Column", + "String name() default \"\"; " + + "boolean unique() default false; " + + "boolean nullable() default true; " + + "boolean insertable() default true; " + + "boolean updatable() default true; " + + "String columnDefinition() default \"\"; " + + "String table() default \"\"; " + + "int length() default 255; " + + "int precision() default 0; " + + "int scale() default 0;"); + } + + private IType createTestAttributeOverrideOnField() throws Exception { + createAttributeOverridesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES, JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AttributeOverrides(@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\"))"); + } + }); + } + + private IType createTestAttributeOverrideWithColumnOnField() throws Exception { + createAttributeOverridesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES, JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@AttributeOverrides(@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column = @Column(name=\"" + COLUMN_NAME + "\")))"); + } + }); + } + + private IType createTestAttributeOverride() throws Exception { + createAttributeOverrideAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ATTRIBUTE_OVERRIDE, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@AttributeOverride(name=\"FOO\", column=@Column(name=\"FOO\", columnDefinition=\"BAR\", table=\"BAZ\", unique=false, nullable=false, insertable=false, updatable=false, length=1, precision=1, scale=1))"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); + AttributeOverride attributeOverride = attributeOverrides.nestedAnnotations().next(); + + assertNotNull(attributeOverride); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + } + + public void testGetNullColumn() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); + AttributeOverride attributeOverride = attributeOverrides.nestedAnnotations().next(); + Column column = attributeOverride.getColumn(); + assertNotNull(attributeOverride); + assertNull(column); + } + + public void testSetName() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); + AttributeOverride attributeOverride = attributeOverrides.nestedAnnotations().next(); + + assertNotNull(attributeOverride); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + + attributeOverride.setName("Foo"); + assertEquals("Foo", attributeOverride.getName()); + assertSourceContains("@AttributeOverrides(@AttributeOverride(name=\"Foo\"))"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestAttributeOverrideOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); + AttributeOverride attributeOverride = attributeOverrides.nestedAnnotations().next(); + assertEquals(ATTRIBUTE_OVERRIDE_NAME, attributeOverride.getName()); + + attributeOverride.setName(null); + assertNull(attributeOverride.getName()); + + assertSourceDoesNotContain("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")"); + assertSourceContains("@AttributeOverride"); + assertSourceContains("@AttributeOverrides"); + } + + public void testColumnGetName() throws Exception { + IType testType = this.createTestAttributeOverrideWithColumnOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); + AttributeOverride attributeOverride = attributeOverrides.nestedAnnotations().next(); + + Column column = attributeOverride.getColumn(); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testColumnSetName() throws Exception { + IType testType = this.createTestAttributeOverrideWithColumnOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES).next(); + + Column column = attributeOverride.getColumn(); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName("Foo"); + + assertSourceContains("@AttributeOverrides(@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\", column = @Column(name=\"Foo\")))"); + + column.setName(null); + //TODO should I have to update from java before column is set to null?? + attributeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertNull(attributeOverride.getColumn()); + assertSourceContains("@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")"); + } + + public void testAddAttributeOverrideCopyExisting() throws Exception { + IType jdtType = createTestAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AttributeOverride attributeOverride = (AttributeOverride) typeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\", column = @Column(name=\"FOO\", columnDefinition = \"BAR\", table = \"BAZ\", unique = false, nullable = false, insertable = false, updatable = false, length = 1, precision = 1, scale = 1)),@AttributeOverride(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(typeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + public void testAddAttributeOverrideToBeginningOfList() throws Exception { + IType jdtType = createTestAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AttributeOverride attributeOverride = (AttributeOverride) typeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\", column = @Column(name=\"FOO\", columnDefinition = \"BAR\", table = \"BAZ\", unique = false, nullable = false, insertable = false, updatable = false, length = 1, precision = 1, scale = 1)),@AttributeOverride(name=\"BAR\")})"); + + attributeOverride = (AttributeOverride) typeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAZ"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAZ\"),@AttributeOverride(name=\"FOO\", column = @Column(name=\"FOO\", columnDefinition = \"BAR\", table = \"BAZ\", unique = false, nullable = false, insertable = false, updatable = false, length = 1, precision = 1, scale = 1)), @AttributeOverride(name=\"BAR\")})"); + + Iterator<JavaResource> attributeOverrides = typeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + assertEquals("BAZ", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("FOO", ((AttributeOverride) attributeOverrides.next()).getName()); + assertEquals("BAR", ((AttributeOverride) attributeOverrides.next()).getName()); + + assertNull(typeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(typeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + public void testRemoveAttributeOverrideCopyExisting() throws Exception { + IType jdtType = createTestAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + AttributeOverride attributeOverride = (AttributeOverride) typeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\", column = @Column(name=\"FOO\", columnDefinition = \"BAR\", table = \"BAZ\", unique = false, nullable = false, insertable = false, updatable = false, length = 1, precision = 1, scale = 1)),@AttributeOverride(name=\"BAR\")})"); + + typeResource.removeAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + assertSourceContains("@AttributeOverride(name=\"FOO\", column = @Column(name=\"FOO\", columnDefinition = \"BAR\", table = \"BAZ\", unique = false, nullable = false, insertable = false, updatable = false, length = 1, precision = 1, scale = 1))"); + } + //not sure i want to test this api, how can we keep ContainerAnnotation.add, move, remove from being public? + //users should go throught AbstractJavapersistenceResource. this gets confusing because you would handle it differently + //for non top-level annotations +// public void testAdd() throws Exception { +// IType testType = this.createTestType(); +// JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); +// JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); +// AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); +// assertNull(attributeOverrides); +// +// attributeResource.addAnnotation(JPA.ATTRIBUTE_OVERRIDES); +// attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); +// assertNotNull(attributeOverrides); +// +// assertSourceContains("@AttributeOverrides"); +// +// AttributeOverride fooAttributeOverride = attributeOverrides.add(0); +// fooAttributeOverride.setName("Foo"); +// +// assertSourceContains("@AttributeOverrides(@AttributeOverride(name=\"Foo\"))"); +// +// AttributeOverride barAttributeOverride = attributeOverrides.add(0); +// barAttributeOverride.setName("Bar"); +// +// assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"Bar\"), @AttributeOverride(name=\"Foo\")})"); +// +// } +// +// public void testMove() throws Exception { +// IType testType = this.createTestAttributeOverrideWithColumnOnField(); +// JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); +// JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); +// AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); +// +// AttributeOverride fooAttributeOverride = attributeOverrides.add(1); +// fooAttributeOverride.setName("Foo"); +// +// assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\"), @AttributeOverride(name=\"Foo\")})"); +// +// attributeOverrides.move(0, 1); +// +// assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"Foo\"), @AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\")})"); +// } +// +// public void testRemove() throws Exception { +// IType testType = this.createTestAttributeOverrideWithColumnOnField(); +// JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); +// JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); +// AttributeOverrides attributeOverrides = (AttributeOverrides) attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES); +// +// AttributeOverride fooAttributeOverride = attributeOverrides.add(1); +// fooAttributeOverride.setName("Foo"); +// +// assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"" + ATTRIBUTE_OVERRIDE_NAME + "\"), @AttributeOverride(name=\"Foo\")})"); +// +// attributeOverrides.remove(0); +// +// assertSourceContains("@AttributeOverrides(@AttributeOverride(name=\"Foo\"))"); +// +// attributeOverrides.remove(0); +// +// assertSourceContains("@AttributeOverrides()"); +// +// } + + + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/BasicTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/BasicTests.java new file mode 100644 index 0000000000..35895d46ee --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/BasicTests.java @@ -0,0 +1,154 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.FetchType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class BasicTests extends JavaResourceModelTestCase { + + public BasicTests(String name) { + super(name); + } + + private IType createTestBasic() throws Exception { + this.createAnnotationAndMembers("Basic", "boolean optional() default true;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.BASIC); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic"); + } + }); + } + + private IType createTestBasicWithOptional() throws Exception { + this.createAnnotationAndMembers("Basic", "boolean optional() default true;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.BASIC); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic(optional=true)"); + } + }); + } + + private IType createTestBasicWithFetch() throws Exception { + this.createAnnotationAndMembers("Basic", "boolean optional() default true; FetchType fetch() default FetchType.EAGER;"); + this.createEnumAndMembers("FetchType", "EAGER, LAZY"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.BASIC, JPA.FETCH_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Basic(fetch=FetchType.EAGER)"); + } + }); + } + + public void testBasic() throws Exception { + IType testType = this.createTestBasic(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertNotNull(basic); + } + + public void testGetOptional() throws Exception { + IType testType = this.createTestBasicWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertTrue(basic.getOptional()); + } + + public void testSetOptional() throws Exception { + IType testType = this.createTestBasicWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertTrue(basic.getOptional()); + + basic.setOptional(false); + assertFalse(basic.getOptional()); + + assertSourceContains("@Basic(optional=false)"); + } + + public void testSetOptionalNull() throws Exception { + IType testType = this.createTestBasicWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertTrue(basic.getOptional()); + + basic.setOptional(null); + assertNull(basic.getOptional()); + + assertSourceContains("@Basic"); + assertSourceDoesNotContain("optional"); + } + + public void testGetFetch() throws Exception { + IType testType = this.createTestBasicWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertEquals(FetchType.EAGER, basic.getFetch()); + } + + public void testSetFetch() throws Exception { + IType testType = this.createTestBasicWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertEquals(FetchType.EAGER, basic.getFetch()); + + basic.setFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, basic.getFetch()); + + assertSourceContains("@Basic(fetch=LAZY)"); + } + + public void testSetFetchNull() throws Exception { + IType testType = this.createTestBasicWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Basic basic = (Basic) attributeResource.mappingAnnotation(JPA.BASIC); + assertEquals(FetchType.EAGER, basic.getFetch()); + + basic.setFetch(null); + assertNull(basic.getFetch()); + + assertSourceContains("@Basic"); + assertSourceDoesNotContain("fetch"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ColumnTests.java new file mode 100644 index 0000000000..388c46190d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ColumnTests.java @@ -0,0 +1,423 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class ColumnTests extends JavaResourceModelTestCase { + + 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"; + + public ColumnTests(String name) { + super(name); + } + + private void createColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("Column", + "String name() default \"\"; " + + "boolean unique() default false; " + + "boolean nullable() default true; " + + "boolean insertable() default true; " + + "boolean updatable() default true; " + + "String columnDefinition() default \"\"; " + + "String table() default \"\"; " + + "int length() default 255; " + + "int precision() default 0; " + + "int scale() default 0;"); + } + + private IType createTestColumn() throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column"); + } + }); + } + + private IType createTestColumnWithName() throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(name=\"" + COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestColumnWithTable() throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(table=\"" + COLUMN_TABLE + "\")"); + } + }); + } + + private IType createTestColumnWithColumnDefinition() throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(columnDefinition=\"" + COLUMN_COLUMN_DEFINITION + "\")"); + } + }); + } + + private IType createTestColumnWithBooleanElement(final String booleanElement) throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(" + booleanElement + "=true)"); + } + }); + } + + private IType createTestColumnWithIntElement(final String intElement) throws Exception { + createColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(" + intElement + "=5)"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + 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 { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@Column(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@Column"); + } + + public void testGetTable() throws Exception { + IType testType = this.createTestColumnWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + assertEquals(COLUMN_TABLE, column.getTable()); + } + + public void testSetTable() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getTable()); + + column.setTable("Foo"); + assertEquals("Foo", column.getTable()); + + assertSourceContains("@Column(table=\"Foo\")"); + + + column.setTable(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@Column(columnDefinition=\"Foo\")"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetUnique() throws Exception { + IType testType = this.createTestColumnWithBooleanElement("unique"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.TRUE, column.getUnique()); + } + + public void testSetUnique() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getUnique()); + + column.setUnique(Boolean.FALSE); + assertEquals(Boolean.FALSE, column.getUnique()); + + assertSourceContains("@Column(unique=false)"); + + column.setUnique(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetNullable() throws Exception { + IType testType = this.createTestColumnWithBooleanElement("nullable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.TRUE, column.getNullable()); + } + + public void testSetNullable() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getNullable()); + + column.setNullable(Boolean.FALSE); + assertEquals(Boolean.FALSE, column.getNullable()); + + assertSourceContains("@Column(nullable=false)"); + + column.setNullable(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetInsertable() throws Exception { + IType testType = this.createTestColumnWithBooleanElement("insertable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.TRUE, column.getInsertable()); + } + + public void testSetInsertable() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getInsertable()); + + column.setInsertable(Boolean.FALSE); + assertEquals(Boolean.FALSE, column.getInsertable()); + + assertSourceContains("@Column(insertable=false)"); + + column.setInsertable(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetUpdatable() throws Exception { + IType testType = this.createTestColumnWithBooleanElement("updatable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Boolean.TRUE, column.getUpdatable()); + } + + public void testSetUpdatable() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getUpdatable()); + + column.setUpdatable(Boolean.FALSE); + assertEquals(Boolean.FALSE, column.getUpdatable()); + + assertSourceContains("@Column(updatable=false)"); + + column.setUpdatable(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetLength() throws Exception { + IType testType = this.createTestColumnWithIntElement("length"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(5), column.getLength()); + } + + public void testSetLength() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getLength()); + + column.setLength(Integer.valueOf(5)); + assertEquals(Integer.valueOf(5), column.getLength()); + + assertSourceContains("@Column(length=5)"); + + column.setLength(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetPrecision() throws Exception { + IType testType = this.createTestColumnWithIntElement("precision"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(5), column.getPrecision()); + } + + public void testSetPrecision() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertEquals(null, column.getPrecision()); + + column.setPrecision(Integer.valueOf(5)); + assertEquals(Integer.valueOf(5), column.getPrecision()); + + assertSourceContains("@Column(precision=5)"); + + column.setPrecision(null); + assertSourceDoesNotContain("@Column"); + } + + public void testGetScale() throws Exception { + IType testType = this.createTestColumnWithIntElement("scale"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertEquals(Integer.valueOf(5), column.getScale()); + } + + public void testSetScale() throws Exception { + IType testType = this.createTestColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + + assertNotNull(column); + assertNull(column.getScale()); + + column.setScale(Integer.valueOf(5)); + assertEquals(Integer.valueOf(5), column.getScale()); + + assertSourceContains("@Column(scale=5)"); + + column.setScale(null); + assertSourceDoesNotContain("@Column"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorColumnTests.java new file mode 100644 index 0000000000..76a8d64d5d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorColumnTests.java @@ -0,0 +1,225 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorColumn; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class DiscriminatorColumnTests extends JavaResourceModelTestCase { + + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String COLUMN_COLUMN_DEFINITION = "COLUMN_DEFINITION"; + + public DiscriminatorColumnTests(String name) { + super(name); + } + + private void createDiscriminatorColumnAnnotation() throws Exception { + this.createEnumAndMembers("DiscriminatorType", "STRING, CHAR, INTEGER"); + this.createAnnotationAndMembers("DiscriminatorColumn", + "String name() default \"DTYPE\"; " + + "DiscriminatorType discriminatorType() default STRING; " + + "String columnDefinition() default \"\"; " + + "int length() default 31;"); + } + + private IType createTestDiscriminatorColumn() throws Exception { + createDiscriminatorColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorColumn"); + } + }); + } + + private IType createTestDiscriminatorColumnWithName() throws Exception { + createDiscriminatorColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorColumn(name=\"" + COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestDiscriminatorColumnWithColumnDefinition() throws Exception { + createDiscriminatorColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorColumn(columnDefinition=\"" + COLUMN_COLUMN_DEFINITION + "\")"); + } + }); + } + + private IType createTestDiscriminatorColumnWithDiscriminatorType() throws Exception { + createDiscriminatorColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_COLUMN, JPA.DISCRIMINATOR_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorColumn(discriminatorType=DiscriminatorType.CHAR)"); + } + }); + } + + private IType createTestColumnWithIntElement(final String intElement) throws Exception { + createDiscriminatorColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorColumn(" + intElement + "=5)"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestDiscriminatorColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestDiscriminatorColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + assertNotNull(column); + assertNull(column.getName()); + assertNull(column.getDiscriminatorType()); + assertNull(column.getColumnDefinition()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestDiscriminatorColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@DiscriminatorColumn(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestDiscriminatorColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@DiscriminatorColumn"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestDiscriminatorColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestDiscriminatorColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@DiscriminatorColumn(columnDefinition=\"Foo\")"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@DiscriminatorColumn"); + } + + public void testGetLength() throws Exception { + IType testType = this.createTestColumnWithIntElement("length"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertEquals(Integer.valueOf(5), column.getLength()); + } + + public void testSetLength() throws Exception { + IType testType = this.createTestDiscriminatorColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertNotNull(column); + assertNull(column.getLength()); + + column.setLength(Integer.valueOf(5)); + assertEquals(Integer.valueOf(5), column.getLength()); + + assertSourceContains("@DiscriminatorColumn(length=5)"); + + column.setLength(null); + assertSourceDoesNotContain("@DiscriminatorColumn"); + } + + public void testGetDiscriminatorType() throws Exception { + IType testType = this.createTestDiscriminatorColumnWithDiscriminatorType(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + assertEquals(DiscriminatorType.CHAR, column.getDiscriminatorType()); + } + + public void testSetDiscriminatorType() throws Exception { + IType testType = this.createTestDiscriminatorColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + DiscriminatorColumn column = (DiscriminatorColumn) typeResource.annotation(JPA.DISCRIMINATOR_COLUMN); + + assertNull(column.getDiscriminatorType()); + + column.setDiscriminatorType(DiscriminatorType.INTEGER); + assertEquals(DiscriminatorType.INTEGER, column.getDiscriminatorType()); + + assertSourceContains("@DiscriminatorColumn(discriminatorType=INTEGER)"); + + column.setDiscriminatorType(null); + assertSourceDoesNotContain("@DiscriminatorColumn"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorValueTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorValueTests.java new file mode 100644 index 0000000000..d10d7c377c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/DiscriminatorValueTests.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.DiscriminatorValue; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class DiscriminatorValueTests extends JavaResourceModelTestCase { + + public DiscriminatorValueTests(String name) { + super(name); + } + + private IType createTestDiscriminatorValue() throws Exception { + this.createAnnotationAndMembers("DiscriminatorValue", "String value() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_VALUE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorValue"); + } + }); + } + + private IType createTestDiscriminatorValueWithValue() throws Exception { + this.createAnnotationAndMembers("DiscriminatorValue", "String value() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.DISCRIMINATOR_VALUE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@DiscriminatorValue(value=\"discriminator\")"); + } + }); + } + + public void testDiscriminatorValue() throws Exception { + IType testType = this.createTestDiscriminatorValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + DiscriminatorValue discriminatorValue = (DiscriminatorValue) typeResource.annotation(JPA.DISCRIMINATOR_VALUE); + assertNotNull(discriminatorValue); + } + + public void testGetValue() throws Exception { + IType testType = this.createTestDiscriminatorValueWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + DiscriminatorValue discriminatorValue = (DiscriminatorValue) typeResource.annotation(JPA.DISCRIMINATOR_VALUE); + assertEquals("discriminator", discriminatorValue.getValue()); + } + + public void testSetValue() throws Exception { + IType testType = this.createTestDiscriminatorValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + DiscriminatorValue discriminatorValue = (DiscriminatorValue) typeResource.annotation(JPA.DISCRIMINATOR_VALUE); + + discriminatorValue.setValue("foo"); + + assertSourceContains("@DiscriminatorValue(\"foo\")"); + + discriminatorValue.setValue(null); + + assertSourceDoesNotContain("@DiscriminatorValue"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddableTests.java new file mode 100644 index 0000000000..41a3718e0f --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddableTests.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Embeddable; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class EmbeddableTests extends JavaResourceModelTestCase { + + public EmbeddableTests(String name) { + super(name); + } + + private IType createTestEmbeddable() throws Exception { + this.createAnnotationAndMembers("Embeddable", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Embeddable"); + } + }); + } + + private IType createTestEmbeddableAndEntity() throws Exception { + this.createAnnotationAndMembers("Embeddable", ""); + this.createAnnotationAndMembers("Entity", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDABLE, JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append("@Embeddable"); + } + }); + } + + public void testEmbeddable() throws Exception { + IType testType = this.createTestEmbeddable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaResource mappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Embeddable); + } + + public void testEmbeddableAndEntity() throws Exception { + IType testType = this.createTestEmbeddableAndEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaResource mappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Embeddable); + + Entity entity = (Entity) typeResource.mappingAnnotation(JPA.ENTITY); + assertNotNull(entity); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedIdTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedIdTests.java new file mode 100644 index 0000000000..ad46a125a9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedIdTests.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.EmbeddedId; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class EmbeddedIdTests extends JavaResourceModelTestCase { + + public EmbeddedIdTests(String name) { + super(name); + } + + private IType createTestEmbeddedId() throws Exception { + this.createAnnotationAndMembers("EmbeddedId", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED_ID); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@EmbeddedId"); + } + }); + } + + public void testEmbeddedId() throws Exception { + IType testType = this.createTestEmbeddedId(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JavaResource mappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof EmbeddedId); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedTests.java new file mode 100644 index 0000000000..cbac60ed83 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EmbeddedTests.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Embedded; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class EmbeddedTests extends JavaResourceModelTestCase { + + public EmbeddedTests(String name) { + super(name); + } + + private IType createTestEmbedded() throws Exception { + this.createAnnotationAndMembers("Embedded", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + } + }); + } + + public void testEmbedded() throws Exception { + IType testType = this.createTestEmbedded(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JavaResource mappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Embedded); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EntityTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EntityTests.java new file mode 100644 index 0000000000..0ed5ed4c03 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EntityTests.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.MappedSuperclass; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class EntityTests extends JavaResourceModelTestCase { + + private static final String ENTITY_NAME = "Foo"; + + public EntityTests(String name) { + super(name); + } + + private IType createTestEntity() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithName() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity(name=\"" + ENTITY_NAME + "\")"); + } + }); + } + + private IType createTestMappedSuperclassAndEntity() throws Exception { + this.createAnnotationAndMembers("MappedSuperclass", ""); + this.createAnnotationAndMembers("Entity", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS, JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + sb.append("@Entity"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestEntityWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Entity entity = (Entity) typeResource.mappingAnnotation(JPA.ENTITY); + assertTrue(entity != null); + assertEquals(ENTITY_NAME, entity.getName()); + } + + public void testGetNameNull() throws Exception { + IType testType = this.createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Entity entity = (Entity) typeResource.mappingAnnotation(JPA.ENTITY); + assertTrue(entity != null); + assertNull(entity.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Entity entity = (Entity) typeResource.mappingAnnotation(JPA.ENTITY); + assertNull(entity.getName()); + entity.setName("Foo"); + assertEquals("Foo", entity.getName()); + + assertSourceContains("@Entity(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestEntityWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Entity entity = (Entity) typeResource.mappingAnnotation(JPA.ENTITY); + assertEquals(ENTITY_NAME, entity.getName()); + + entity.setName(null); + assertNull(entity.getName()); + + assertSourceContains("@Entity"); + assertSourceDoesNotContain("@Entity(name=\"Foo\")"); + } + + public void testMappedSuperclassAndEntity() throws Exception { + IType testType = this.createTestMappedSuperclassAndEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaResource mappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Entity); + + MappedSuperclass mappedSuperclass = (MappedSuperclass) typeResource.mappingAnnotation(JPA.MAPPED_SUPERCLASS); + assertNotNull(mappedSuperclass); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EnumeratedTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EnumeratedTests.java new file mode 100644 index 0000000000..eb5a87fdd3 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/EnumeratedTests.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.EnumType; +import org.eclipse.jpt.core.internal.resource.java.Enumerated; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class EnumeratedTests extends JavaResourceModelTestCase { + + public EnumeratedTests(String name) { + super(name); + } + + private IType createTestEnumerated() throws Exception { + this.createAnnotationAndMembers("Enumerated", "EnumType value();"); + this.createEnumAndMembers("EnumType", "ORDINAL, STRING"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENUMERATED); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Enumerated"); + } + }); + } + + private IType createTestEnumeratedWithValue() throws Exception { + this.createAnnotationAndMembers("Enumerated", "EnumType value();"); + this.createEnumAndMembers("EnumType", "ORDINAL, STRING"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENUMERATED, JPA.ENUM_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Enumerated(EnumType.ORDINAL)"); + } + }); + } + + public void testEnumerated() throws Exception { + IType testType = this.createTestEnumerated(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Enumerated enumerated = (Enumerated) attributeResource.annotation(JPA.ENUMERATED); + assertNotNull(enumerated); + } + + public void testGetValue() throws Exception { + IType testType = this.createTestEnumeratedWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Enumerated enumerated = (Enumerated) attributeResource.annotation(JPA.ENUMERATED); + assertEquals(EnumType.ORDINAL, enumerated.getValue()); + } + + public void testSetValue() throws Exception { + IType testType = this.createTestEnumerated(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Enumerated enumerated = (Enumerated) attributeResource.annotation(JPA.ENUMERATED); + + enumerated.setValue(EnumType.STRING); + + assertSourceContains("@Enumerated(STRING)"); + + enumerated.setValue(null); + + assertSourceDoesNotContain("@Enumerated"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/GeneratedValueTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/GeneratedValueTests.java new file mode 100644 index 0000000000..cd06d1d1f0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/GeneratedValueTests.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.GeneratedValue; +import org.eclipse.jpt.core.internal.resource.java.GenerationType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class GeneratedValueTests extends JavaResourceModelTestCase { + + private static final String GENERATOR = "MY_GENERATOR"; + public GeneratedValueTests(String name) { + super(name); + } + + private void createGenerationTypeEnum() throws Exception { + this.createEnumAndMembers("GenerationType", "TABLE, SEQUENCE, IDENTITY, AUTO;"); + } + + private void createGeneratedValueAnnotation() throws Exception { + this.createAnnotationAndMembers("GeneratedValue", "GenerationType strategy() default AUTO; String generator() default \"\""); + createGenerationTypeEnum(); + } + + private IType createTestGeneratedValue() throws Exception { + createGeneratedValueAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.GENERATED_VALUE, JPA.GENERATION_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@GeneratedValue"); + } + }); + } + + private IType createTestGeneratedValueWithGenerator() throws Exception { + createGeneratedValueAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.GENERATED_VALUE, JPA.GENERATION_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@GeneratedValue(generator=\"" + GENERATOR + "\")"); + } + }); + } + + private IType createTestGeneratedValueWithStrategy() throws Exception { + createGeneratedValueAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.GENERATED_VALUE, JPA.GENERATION_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@GeneratedValue(strategy=GenerationType.SEQUENCE)"); + } + }); + } + + public void testGeneratedValue() throws Exception { + IType testType = this.createTestGeneratedValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertNotNull(generatedValue); + } + + public void testGetGenerator() throws Exception { + IType testType = this.createTestGeneratedValueWithGenerator(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertEquals(GENERATOR, generatedValue.getGenerator()); + } + + public void testSetGenerator() throws Exception { + IType testType = this.createTestGeneratedValueWithGenerator(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertEquals(GENERATOR, generatedValue.getGenerator()); + + generatedValue.setGenerator("foo"); + assertEquals("foo", generatedValue.getGenerator()); + + assertSourceContains("@GeneratedValue(generator=\"foo\")"); + + generatedValue.setGenerator(null); + assertNull(generatedValue.getGenerator()); + + assertSourceDoesNotContain("generator"); + assertSourceContains("@GeneratedValue"); + } + + public void testGetStrategy() throws Exception { + IType testType = this.createTestGeneratedValueWithStrategy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertEquals(GenerationType.SEQUENCE, generatedValue.getStrategy()); + } + + public void testSetStrategy() throws Exception { + IType testType = this.createTestGeneratedValueWithStrategy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + GeneratedValue generatedValue = (GeneratedValue) attributeResource.annotation(JPA.GENERATED_VALUE); + assertEquals(GenerationType.SEQUENCE, generatedValue.getStrategy()); + + generatedValue.setStrategy(GenerationType.TABLE); + assertEquals(GenerationType.TABLE, generatedValue.getStrategy()); + + assertSourceContains("@GeneratedValue(strategy=TABLE)"); + + generatedValue.setStrategy(null); + assertNull(generatedValue.getStrategy()); + assertSourceDoesNotContain("strategy"); + assertSourceContains("@GeneratedValue"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdClassTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdClassTests.java new file mode 100644 index 0000000000..8f5a43eca9 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdClassTests.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.IdClass; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class IdClassTests extends JavaResourceModelTestCase { + + private static final String ID_CLASS_VALUE = "MyClass"; + + public IdClassTests(String name) { + super(name); + } + + private void createIdClassAnnotation() throws Exception { + this.createAnnotationAndMembers("IdClass", "Class value();"); + } + + private IType createTestIdClass() throws Exception { + createIdClassAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ID_CLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@IdClass"); + } + }); + } + + private IType createTestIdClassWithValue() throws Exception { + createIdClassAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ID_CLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@IdClass(" + ID_CLASS_VALUE + ".class)"); + } + }); + } + + public void testIdClass() throws Exception { + IType testType = this.createTestIdClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + IdClass idClass = (IdClass) typeResource.annotation(JPA.ID_CLASS); + assertNotNull(idClass); + assertNull(idClass.getValue()); + } + + public void testGetValue() throws Exception { + IType testType = this.createTestIdClassWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + IdClass idClass = (IdClass) typeResource.annotation(JPA.ID_CLASS); + assertEquals(ID_CLASS_VALUE, idClass.getValue()); + } + + public void testSetValue() throws Exception { + IType testType = this.createTestIdClassWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + IdClass idClass = (IdClass) typeResource.annotation(JPA.ID_CLASS); + assertEquals(ID_CLASS_VALUE, idClass.getValue()); + + idClass.setValue("foo"); + assertEquals("foo", idClass.getValue()); + + assertSourceContains("@IdClass(foo.class)"); + + idClass.setValue(null); + + assertSourceDoesNotContain("@IdClass"); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + + idClass = (IdClass) typeResource.annotation(JPA.ID_CLASS); + assertNull(idClass); + } + + public void testGetFullyQualifiedClass() throws Exception { + IType testType = this.createTestIdClassWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + IdClass idClass = (IdClass) typeResource.annotation(JPA.ID_CLASS); + assertNotNull(idClass.getValue()); + assertNull(idClass.getFullyQualifiedClass()); + + + idClass.setValue(TYPE_NAME); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, idClass.getFullyQualifiedClass()); + + assertSourceContains("@IdClass(" + TYPE_NAME + ".class)"); + + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdTests.java new file mode 100644 index 0000000000..e48e4b4d76 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/IdTests.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class IdTests extends JavaResourceModelTestCase { + + public IdTests(String name) { + super(name); + } + + private IType createTestId() throws Exception { + this.createAnnotationAndMembers("Id", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ID); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + public void testId() throws Exception { + IType testType = this.createTestId(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JavaResource mappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Id); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/InheritanceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/InheritanceTests.java new file mode 100644 index 0000000000..fab5e57c33 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/InheritanceTests.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Inheritance; +import org.eclipse.jpt.core.internal.resource.java.InheritanceType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class InheritanceTests extends JavaResourceModelTestCase { + + public InheritanceTests(String name) { + super(name); + } + + private IType createTestInheritance() throws Exception { + this.createAnnotationAndMembers("Inheritance", "InheritanceType strategy() default SINGLE_TABLE;"); + this.createEnumAndMembers("InheritanceType", "SINGLE_TABLE, JOINED, TABLE_PER_CLASS"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.INHERITANCE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Inheritance"); + } + }); + } + + private IType createTestInheritanceWithStrategy() throws Exception { + this.createAnnotationAndMembers("Inheritance", "InheritanceType strategy() default SINGLE_TABLE;"); + this.createEnumAndMembers("InheritanceType", "SINGLE_TABLE, JOINED, TABLE_PER_CLASS"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.INHERITANCE, JPA.INHERITANCE_TYPE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Inheritance(strategy=InheritanceType.JOINED)"); + } + }); + } + + public void testInheritance() throws Exception { + IType testType = this.createTestInheritance(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Inheritance inheritance = (Inheritance) typeResource.annotation(JPA.INHERITANCE); + assertNotNull(inheritance); + } + + public void testGetStrategy() throws Exception { + IType testType = this.createTestInheritanceWithStrategy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Inheritance inheritance = (Inheritance) typeResource.annotation(JPA.INHERITANCE); + assertEquals(InheritanceType.JOINED, inheritance.getStrategy()); + } + + public void testSetStrategy() throws Exception { + IType testType = this.createTestInheritance(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Inheritance inheritance = (Inheritance) typeResource.annotation(JPA.INHERITANCE); + inheritance.setStrategy(InheritanceType.TABLE_PER_CLASS); + + assertSourceContains("@Inheritance(strategy=TABLE_PER_CLASS)"); + + inheritance.setStrategy(null); + + assertSourceDoesNotContain("@Inheritance"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JPTToolsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JPTToolsTests.java new file mode 100644 index 0000000000..e3b7470cac --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JPTToolsTests.java @@ -0,0 +1,527 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.IVariableBinding; +import org.eclipse.jpt.core.internal.jdtutility.FieldAttribute; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.jdtutility.JPTTools; +import org.eclipse.jpt.core.internal.jdtutility.MethodAttribute; +import org.eclipse.jpt.core.internal.jdtutility.Type; + +public class JPTToolsTests extends JavaResourceModelTestCase { + + public JPTToolsTests(String name) { + super(name); + } + + private IType createTestTypeFieldWithModifier(final String modifier) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" " + modifier + " String foo;").append(CR); + sb.append(CR); + } + }); + } + + protected FieldAttribute fooField() throws JavaModelException { + return this.fieldNamed("foo"); + } + + + private IType createTestTypeGetMethodWithModifier(final String modifier) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" " + modifier + " int getFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(int id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeInvalidMethodName() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public int foo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(int id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeConstructor() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public " + TYPE_NAME + "() {").append(CR); + sb.append(" super();").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeVoidMethodReturnType() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public void getFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(int id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + +// private IType createTestTypeInvalidMethodReturnType() throws Exception { +// return this.createTestType(new DefaultAnnotationWriter() { +// +// @Override +// public Iterator<String> imports() { +// return new ArrayIterator<String>("com.foo.Foo"); +// } +// @Override +// public void appendGetIdMethodAnnotationTo(StringBuffer sb) { +// sb.append(CR); +// sb.append(" public Foo getFoo() {").append(CR); +// sb.append(" return null;").append(CR); +// sb.append(" }").append(CR); +// sb.append(CR); +// sb.append(" public void setFoo(Foo id) {").append(CR); +// sb.append(" this.id = id;").append(CR); +// sb.append(" }").append(CR); +// sb.append(CR); +// } +// }); +// } + + private IType createTestTypeIsMethod() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public boolean isFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(boolean id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeIsMethodReturnInt() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public int isFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(int id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeIsAndGetMethodWithModifier(final String modifier) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" " + modifier + " boolean isFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" " + modifier + " boolean getFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" public void setFoo(boolean id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeSetMethodWithModifier(final String modifier) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" public int getFoo() {").append(CR); + sb.append(" return this.id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" " + modifier + " void setFoo(int id) {").append(CR); + sb.append(" this.id = id;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestTypeWithMemberTypes() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public void appendMemberTypeTo(StringBuilder sb) { + sb.append(" public static class FooStatic {}").append(CR); + sb.append(CR); + sb.append(" public class FooNotStatic {}").append(CR); + sb.append(CR); + sb.append(" public @interface MyAnnotation {}").append(CR); + sb.append(CR); + sb.append(" public enum MyEnum {}").append(CR); + } + }); + } + + protected MethodAttribute fooMethod() throws JavaModelException { + return this.methodNamed("getFoo"); + } + + //private String foo; - persistable + public void testFieldIsPersistable1() throws Exception { + IType testType = createTestTypeFieldWithModifier("private"); + FieldAttribute fieldAttribute = fooField(); + IVariableBinding variableBinding = fieldAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.fieldIsPersistable(variableBinding)); + } + + //private static String foo; - not persistable + public void testFieldIsPersistable2() throws Exception { + IType testType = createTestTypeFieldWithModifier("private static"); + FieldAttribute fieldAttribute = fooField(); + IVariableBinding variableBinding = fieldAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.fieldIsPersistable(variableBinding)); + } + + //private transient String foo; - not persistable + public void testFieldIsPersistable3() throws Exception { + IType testType = createTestTypeFieldWithModifier("private transient"); + FieldAttribute fieldAttribute = fooField(); + IVariableBinding variableBinding = fieldAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.fieldIsPersistable(variableBinding)); + } + + //private final String foo; - persistable + public void testFieldIsPersistable4() throws Exception { + IType testType = createTestTypeFieldWithModifier("private final"); + FieldAttribute fieldAttribute = fooField(); + IVariableBinding variableBinding = fieldAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.fieldIsPersistable(variableBinding)); + } + + //public String foo; - persistable + public void testFieldIsPersistable5() throws Exception { + IType testType = createTestTypeFieldWithModifier("public"); + FieldAttribute fieldAttribute = fooField(); + IVariableBinding variableBinding = fieldAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.fieldIsPersistable(variableBinding)); + } + + + //public int getFoo() {} - persistable + public void testMethodIsPersistablePropertyGetter1() throws Exception { + IType testType = createTestTypeGetMethodWithModifier("public"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + + } + + //protected int getFoo() {} - persistable + public void testMethodIsPersistablePropertyGetter2() throws Exception { + IType testType = createTestTypeGetMethodWithModifier("protected"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //int getFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter3() throws Exception { + IType testType = createTestTypeGetMethodWithModifier(""); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //private int getFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter4() throws Exception { + IType testType = createTestTypeGetMethodWithModifier("private"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public static int getFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter5() throws Exception { + IType testType = createTestTypeGetMethodWithModifier("public static"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public final int getFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter6() throws Exception { + IType testType = createTestTypeGetMethodWithModifier("public final"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public void setFoo(int foo) {} - persistable + public void testMethodIsPersistablePropertyGetter7() throws Exception { + IType testType = createTestTypeSetMethodWithModifier("public"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + + } + + //protected void setFoo(int foo) {} - persistable + public void testMethodIsPersistablePropertyGetter8() throws Exception { + IType testType = createTestTypeSetMethodWithModifier("protected"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //void setFoo(int foo) {} - not persistable + public void testMethodIsPersistablePropertyGetter9() throws Exception { + IType testType = createTestTypeSetMethodWithModifier(""); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //private void setFoo(int foo) {} - not persistable + public void testMethodIsPersistablePropertyGetter10() throws Exception { + IType testType = createTestTypeSetMethodWithModifier("private"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public static void setFoo(int foo) {} - not persistable + public void testMethodIsPersistablePropertyGetter11() throws Exception { + IType testType = createTestTypeSetMethodWithModifier("public static"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public final void setFoo(int foo) {} - not persistable + public void testMethodIsPersistablePropertyGetter12() throws Exception { + IType testType = createTestTypeSetMethodWithModifier("public final"); + MethodAttribute methodAttribute = fooMethod(); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + } + + //public boolean isFoo() {} - persistable + public void testMethodIsPersistablePropertyGetter13() throws Exception { + IType testType = createTestTypeIsMethod(); + MethodAttribute methodAttribute = this.methodNamed("isFoo"); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + + } + + //public int isFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter14() throws Exception { + IType testType = createTestTypeIsMethodReturnInt(); + MethodAttribute methodAttribute = this.methodNamed("isFoo"); + IMethodBinding methodBinding = methodAttribute.binding(JDTTools.buildASTRoot(testType)); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(methodBinding)); + + } + + //public int isFoo() {} - persistable + //public int getFoo() {} - not persistable + public void testMethodIsPersistablePropertyGetter15() throws Exception { + IType testType = createTestTypeIsAndGetMethodWithModifier("public"); + MethodAttribute isFooMethod = this.methodNamed("isFoo"); + MethodAttribute getFooMethod = this.methodNamed("getFoo"); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertTrue(JPTTools.methodIsPersistablePropertyGetter(isFooMethod.binding(astRoot))); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(getFooMethod.binding(astRoot))); + } + + //public int foo() {} - not persistable + public void testMethodIsPersistablePropertyGetter16() throws Exception { + IType testType = createTestTypeInvalidMethodName(); + MethodAttribute fooMethod = this.methodNamed("foo"); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(fooMethod.binding(astRoot))); + } + + //public void getFoo() {} - not persistable - void return type + public void testMethodIsPersistablePropertyGetter17() throws Exception { + IType testType = createTestTypeVoidMethodReturnType(); + MethodAttribute fooMethod = this.methodNamed("getFoo"); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(fooMethod.binding(astRoot))); + } + + //TODO + //**getFooMethod.binding(CompliationUnit) is returning null, not sure why and don't know how to test + //**this if it is returning null there instead of returning null for IMethodBinding.getReturnType() +// //public Foo getFoo() {} - not persistable - Foo does not resolve +// public void testMethodIsPersistablePropertyGetter18() throws Exception { +// IType testType = createTestTypeInvalidMethodReturnType(); +// MethodAttribute getFooMethod = this.methodNamed("getFoo"); +// +// CompilationUnit astRoot = JDTTools.buildASTRoot(testType); +// assertFalse(JPTTools.methodIsPersistablePropertyGetter(fooMethod.binding(astRoot))); +// } + + //method with parameters - not persistable + public void testMethodIsPersistablePropertyGetter19() throws Exception { + IType testType = createTestType(); + MethodAttribute setIdMethod = idSetMethod(); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(setIdMethod.binding(astRoot))); + } + + //constructor - not persistable + public void testMethodIsPersistablePropertyGetter20() throws Exception { + IType testType = createTestTypeConstructor(); + MethodAttribute constructor = methodNamed(TYPE_NAME); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(constructor.binding(astRoot))); + } + + //no corresponding set method - not persistable + public void testMethodIsPersistablePropertyGetter21() throws Exception { + IType testType = createTestType(); + MethodAttribute getNameMethod = nameGetMethod(); + + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.methodIsPersistablePropertyGetter(getNameMethod.binding(astRoot))); + } + + //public class AnnotationTestType + public void testTypeIsPersistable() throws Exception { + IType testType = createTestType(); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertTrue(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //public final class MyFinal + public void testTypeIsPersistable2() throws Exception { + IType testType = this.javaProject.createType("finals", "MyFinal.java", "public final class MyFinal { }"); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //public interface AnnotationTestType + public void testTypeIsPersistable3() throws Exception { + IType testType = this.javaProject.createType("interfaces", "MyInterface.java", "public interface MyInterface { }"); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //enum not persistable + public void testTypeIsPersistable4() throws Exception { + IType testType = this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ"); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //annotation not persistable + public void testTypeIsPersistable5() throws Exception { + IType testType = this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();"); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //public static member type is persistable + public void testTypeIsPersistable6() throws Exception { + IType testType = this.createTestTypeWithMemberTypes(); + IType staticType = testType.getType("FooStatic"); + Type type = buildType(staticType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertTrue(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + + //non-static member type is not persistable + public void testTypeIsPersistable7() throws Exception { + IType testType = this.createTestTypeWithMemberTypes(); + IType staticType = testType.getType("FooNotStatic"); + Type type = buildType(staticType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + assertFalse(JPTTools.typeIsPersistable(type.binding(astRoot))); + } + //TODO still need to test typeIsPersistable() returns false for local and anonymous classes + + + + public void testIsAbstract() throws Exception { + IType testType = createTestType(); + Type type = buildType(testType); + CompilationUnit astRoot = JDTTools.buildASTRoot(testType); + + assertFalse(JPTTools.typeIsAbstract(type.binding(astRoot))); + } + +} + diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentAttributeResourceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentAttributeResourceTests.java new file mode 100644 index 0000000000..0783fbaca6 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentAttributeResourceTests.java @@ -0,0 +1,825 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Collection; +import java.util.Iterator; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AttributeOverride; +import org.eclipse.jpt.core.internal.resource.java.Basic; +import org.eclipse.jpt.core.internal.resource.java.Column; +import org.eclipse.jpt.core.internal.resource.java.Id; +import org.eclipse.jpt.core.internal.resource.java.IdImpl; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.core.internal.resource.java.OneToOneImpl; +import org.eclipse.jpt.utility.internal.ClassTools; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + + +public class JavaPersistentAttributeResourceTests extends JavaResourceModelTestCase { + + public JavaPersistentAttributeResourceTests(String name) { + super(name); + } + + private IType createTestEntity() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestEntityWithNonResolvingField() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("private Foo foo;").append(CR); + sb.append(CR); + } + }); + } + private IType createTestEntityWithNonResolvingMethod() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("private Foo foo;").append(CR); + sb.append(CR); + sb.append(" @Id"); + sb.append(CR); + sb.append(" public Foo getFoo() {").append(CR); + sb.append(" return this.foo;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" "); + sb.append(CR); + sb.append(" public void setFoo(Foo foo) {").append(CR); + sb.append(" this.foo = foo;").append(CR); + sb.append(" }").append(CR); + sb.append(CR); + sb.append(" "); + } + }); + } + + private IType createTestEntityMultipleVariableDeclarationsPerLine() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Column", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append(CR); + sb.append(" "); + sb.append("@Id"); + sb.append(CR); + sb.append(" "); + sb.append("@Column(name=\"baz\")"); + sb.append(" private String foo, bar;").append(CR); + sb.append(CR); + } + }); + } + + private IType createTestEntityWithIdAndBasic() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Id", ""); + this.createAnnotationAndMembers("Basic", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.BASIC); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + sb.append(CR); + sb.append("@Basic"); + } + }); + } + + private IType createTestEntityAnnotatedField() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Column", "String name();"); + this.createAnnotationAndMembers("Id", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + sb.append(CR); + sb.append(" "); + sb.append("@Column"); + } + }); + } + + private IType createTestEntityWithColumn() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Column", "String name(); String table();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(name=\"FOO\", table=\"MY_TABLE\")"); + } + }); + } + + private IType createTestEntityWithIdColumnGeneratedValue() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Column", "String name(); String table();"); + this.createAnnotationAndMembers("GeneratedValue", ""); + this.createAnnotationAndMembers("Id", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.COLUMN, JPA.ID, JPA.GENERATED_VALUE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + sb.append(CR); + sb.append("@Column"); + sb.append(CR); + sb.append("@GeneratedValue"); + } + }); + } + + + private IType createTestEntityMultipleColumns() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + this.createAnnotationAndMembers("Column", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column(name=\"FOO\")"); + sb.append(CR); + sb.append("@Column(name=\"BAR\")"); + } + }); + } + + private IType createTestEmbeddedWithAttributeOverride() throws Exception { + this.createAnnotationAndMembers("Embedded", "String name();"); + this.createAnnotationAndMembers("AttributeOverride", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + sb.append(CR); + sb.append("@AttributeOverride(name=\"FOO\")"); + } + }); + } + private IType createTestEmbeddedWithAttributeOverrides() throws Exception { + this.createAnnotationAndMembers("Embedded", "String name();"); + this.createAnnotationAndMembers("AttributeOverride", "String name();"); + this.createAnnotationAndMembers("AttributeOverrides", "AttributeOverride[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + sb.append(CR); + sb.append("@AttributeOverrides(@AttributeOverride(name=\"FOO\"))"); + } + }); + } + private IType createTestEmbeddedWithAttributeOverridesEmpty() throws Exception { + this.createAnnotationAndMembers("Embedded", "String name();"); + this.createAnnotationAndMembers("AttributeOverrides", "AttributeOverride[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + sb.append(CR); + sb.append("@AttributeOverrides()"); + } + }); + } + + private IType createTestEmbeddedWith2AttributeOverrides() throws Exception { + this.createAnnotationAndMembers("Embedded", "String name();"); + this.createAnnotationAndMembers("AttributeOverride", "String name();"); + this.createAnnotationAndMembers("AttributeOverrides", "AttributeOverride[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + sb.append(CR); + sb.append("@AttributeOverrides({@AttributeOverride(name=\"FOO\"), @AttributeOverride(name=\"BAR\")})"); + } + }); + } + + private IType createTestEmbeddedWithAttributeOverrideAndAttributeOverrides() throws Exception { + this.createAnnotationAndMembers("Embedded", "String name();"); + this.createAnnotationAndMembers("AttributeOverride", "String name();"); + this.createAnnotationAndMembers("AttributeOverrides", "AttributeOverride[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.EMBEDDED, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Embedded"); + sb.append(CR); + sb.append("@AttributeOverride(name=\"FOO\")"); + sb.append(CR); + sb.append("@AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"BAZ\")})"); + } + }); + } + + public void testJavaAttributeAnnotations() throws Exception { + IType testType = this.createTestEntityWithColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertEquals(1, CollectionTools.size(attributeResource.annotations())); + } + + public void testJavaAttributeAnnotation() throws Exception { + IType testType = this.createTestEntityWithColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertNotNull(attributeResource.annotation(JPA.COLUMN)); + } + + public void testJavaAttributeAnnotationNull() throws Exception { + IType testType = this.createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertNull(attributeResource.annotation(JPA.TABLE)); + } + + //This will result in a compilation error, but we assume the first column found + public void testDuplicateAnnotations() throws Exception { + IType testType = this.createTestEntityMultipleColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + Column columnResource = (Column) attributeResource.annotation(JPA.COLUMN); + assertEquals("FOO", columnResource.getName()); + } + + public void testRemoveColumn() throws Exception { + IType testType = this.createTestEntityWithColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + attributeResource.removeAnnotation(JPA.COLUMN); + + assertSourceDoesNotContain("@Column"); + } + + public void testRemoveColumnName() throws Exception { + IType testType = this.createTestEntityWithColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Column columnResource = (Column) attributeResource.annotation(JPA.COLUMN); + columnResource.setTable(null); + assertSourceContains("@Column(name=\"FOO\")"); + + columnResource.setName(null); + assertSourceDoesNotContain("@Column"); + + //TODO should I be calling this in the test? where should the IElementChangedListener be set up? + typeResource.updateFromJava(JDTTools.buildASTRoot(jdtType())); + assertNull(typeResource.annotation(JPA.TABLE)); + } + + public void testMultipleAttributeMappings() throws Exception { + IType testType = this.createTestEntityWithIdAndBasic(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertEquals(2, CollectionTools.size(attributeResource.mappingAnnotations())); + assertEquals(0, CollectionTools.size(attributeResource.annotations())); + assertNotNull(attributeResource.mappingAnnotation(JPA.BASIC)); + assertNotNull(attributeResource.mappingAnnotation(JPA.ID)); + + JavaResource javaAttributeMappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(javaAttributeMappingAnnotation instanceof Basic); + assertSourceContains("@Basic"); + assertSourceContains("@Id"); + + attributeResource.setMappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(1, CollectionTools.size(attributeResource.mappingAnnotations())); + javaAttributeMappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(javaAttributeMappingAnnotation instanceof OneToMany); + assertSourceDoesNotContain("@Id"); + assertSourceContains("@OneToMany"); + assertSourceDoesNotContain("@Basic"); + } + + public void testSetJavaAttributeMappingAnnotation() throws Exception { + IType testType = createTestType(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertEquals(0, CollectionTools.size(attributeResource.mappingAnnotations())); + + attributeResource.setMappingAnnotation(JPA.ID); + assertTrue(attributeResource.mappingAnnotation() instanceof Id); + assertSourceContains("@Id"); + + attributeResource.setMappingAnnotation(JPA.ID); + //TODO need to test behavior here, throw an exception, remove the old @Id and replace it, + //thus clearing out any annotation elements?? + } + + public void testSetJavaAttributeMappingAnnotation2() throws Exception { + IType testType = createTestEntityWithColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertNull(attributeResource.mappingAnnotation()); + + attributeResource.setMappingAnnotation(JPA.ID); + assertTrue(attributeResource.mappingAnnotation() instanceof Id); + + assertSourceContains("@Id"); + assertSourceContains("@Column"); + } + + public void testSetJavaAttributeMappingAnnotation3() throws Exception { + IType testType = createTestEntityWithIdColumnGeneratedValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertTrue(attributeResource.mappingAnnotation() instanceof Id); + + attributeResource.setMappingAnnotation(JPA.BASIC); + assertTrue(attributeResource.mappingAnnotation() instanceof Basic); + + assertSourceDoesNotContain("@Id"); + assertSourceContains("@GeneratedValue"); //not supported by Basic + assertSourceContains("@Column"); //common between Id and Basic + } + + public void testSetJavaAttributeMappingAnnotationNull() throws Exception { + IType testType = createTestEntityWithIdColumnGeneratedValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertTrue(attributeResource.mappingAnnotation() instanceof Id); + + attributeResource.setMappingAnnotation(null); + assertNull(attributeResource.mappingAnnotation()); + + assertSourceDoesNotContain("@Id"); + assertSourceContains("@GeneratedValue"); //not supported by Basic + assertSourceContains("@Column"); //common between Id and Basic + } + + public void testAddJavaAttributeAnnotation() throws Exception { + IType testType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertSourceDoesNotContain("@Column"); + attributeResource.addAnnotation(JPA.COLUMN); + assertSourceContains("@Column"); + } + + public void testRemoveJavaAttributeAnnotation() throws Exception { + IType testType = createTestEntityAnnotatedField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertSourceContains("@Column"); + attributeResource.removeAnnotation(JPA.COLUMN); + assertSourceDoesNotContain("@Column"); + } + + + //update source code to change from @Id to @OneToOne and make sure @Column is not removed + public void testChangeAttributeMappingInSource() throws Exception { + IType jdtType = createTestEntityAnnotatedField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + idField().removeAnnotation(((IdImpl) attributeResource.mappingAnnotation()).getDeclarationAnnotationAdapter()); + + this.createAnnotationAndMembers("OneToOne", ""); + jdtType.getCompilationUnit().createImport("javax.persistence.OneToOne", null, new NullProgressMonitor()); + + idField().newMarkerAnnotation(OneToOneImpl.DECLARATION_ANNOTATION_ADAPTER); + + typeResource.updateFromJava(JDTTools.buildASTRoot(jdtType)); + + + assertNotNull(attributeResource.annotation(JPA.COLUMN)); + assertNull(attributeResource.mappingAnnotation(JPA.ID)); + assertNotNull(attributeResource.mappingAnnotation(JPA.ONE_TO_ONE)); + assertSourceContains("@Column"); + } + + public void testJavaAttributeAnnotationsNestable() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertEquals(1, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES).next(); + + assertEquals("FOO", attributeOverride.getName()); + } + + public void testJavaAttributeAnnotationsNoNestable() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertEquals(0, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + public void testJavaAttributeAnnotationsContainerNoNestable() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverridesEmpty(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertEquals(0, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + public void testJavaAttributeAnnotationsNestableAndContainer() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverrideAndAttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + + AttributeOverride attributeOverrideResource = (AttributeOverride) attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES).next(); + assertEquals("BAR", attributeOverrideResource.getName()); + } + + // -->> @AttributeOverride(name="FOO") + public void testAddJavaAttributeAnnotationNestableContainer() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("FOO"); + assertSourceContains("@AttributeOverride(name=\"FOO\")"); + } + + // @Embedded -->> @Embedded + // @AttributeOverride(name="FOO") @AttributeOverrides({@AttributeOverride(name="FOO"), @AttributeOverride(name="BAR")}) + public void testAddJavaAttributeAnnotationNestableContainer2() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"),@AttributeOverride(name=\"BAR\")})"); + + assertNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + // @Embedded + // @AttributeOverrides(@AttributeOverride(name="FOO")) + // || + // \/ + // @Embedded + // @AttributeOverrides({@AttributeOverride(name="FOO"), @AttributeOverride(name="BAR")}) + public void testAddJavaAttributeAnnotationNestableContainer3() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"),@AttributeOverride(name=\"BAR\")})"); + + assertNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + public void testAddJavaAttributeAnnotationNestableContainer5() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride.setName("BAR"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAR\"),@AttributeOverride(name=\"FOO\")})"); + + assertNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + } + + // @Embedded + // @SecondaryTable(name=\"FOO\") + // @AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"BAZ\")}) + // || + // \/ + // @Embedded + // @AttributeOverride(name=\"FOO\") + // @AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"BAZ\"), @AttributeOverride(name=\"BOO\")}) + public void testAddJavaAttributeAnnotationNestableContainer4() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverrideAndAttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + + AttributeOverride attributeOverride = (AttributeOverride) attributeResource.addAnnotation(2, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"BAZ\"),"); + assertSourceContains("@AttributeOverride})"); + attributeOverride.setName("BOO"); + + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDE)); + assertNotNull(attributeResource.annotation(JPA.ATTRIBUTE_OVERRIDES)); + assertEquals(3, CollectionTools.size(attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES))); + + Iterator<JavaResource> attributeOverrideAnnotations = attributeResource.annotations(JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + attributeOverride = (AttributeOverride) attributeOverrideAnnotations.next(); + assertEquals("BAR", attributeOverride.getName()); + attributeOverride = (AttributeOverride) attributeOverrideAnnotations.next(); + assertEquals("BAZ", attributeOverride.getName()); + attributeOverride = (AttributeOverride) attributeOverrideAnnotations.next(); + assertEquals("BOO", attributeOverride.getName()); + + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"BAZ\"),"); + assertSourceContains("@AttributeOverride(name=\"BOO\")})"); + } + + //@Entity + //@AttributeOverride(name="FOO") + public void testRemoveJavaAttributeAnnotationNestableContainer() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverride(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + + assertSourceDoesNotContain("@AttributeOverride"); + } + + + //@Entity + //@SecondaryTables(@SecondaryTable(name="FOO")) + public void testRemoveJavaAttributeAnnotationNestableContainer2() throws Exception { + IType jdtType = createTestEmbeddedWithAttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + + assertSourceDoesNotContain("@AttributeOverride"); + assertSourceDoesNotContain("@AttributeOverrides"); + } + + public void testRemoveJavaAttributeAnnotationIndex() throws Exception { + IType jdtType = createTestEmbeddedWith2AttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + attributeResource.removeAnnotation(0, JPA.ATTRIBUTE_OVERRIDE , JPA.ATTRIBUTE_OVERRIDES); + + assertSourceDoesNotContain("@AttributeOverride(name=\"FOO\""); + assertSourceContains("@AttributeOverride(name=\"BAR\""); + assertSourceDoesNotContain("@AttributeOverrides"); + } + + public void testRemoveJavaAttributeAnnotationIndex2() throws Exception { + IType jdtType = createTestEmbeddedWith2AttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride newAnnotation = (AttributeOverride)attributeResource.addAnnotation(2, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + newAnnotation.setName("BAZ"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"), @AttributeOverride(name=\"BAR\"),"); + assertSourceContains("@AttributeOverride(name=\"BAZ\")})"); + + attributeResource.removeAnnotation(1, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"), @AttributeOverride(name=\"BAZ\")})"); + } + + public void testMoveJavaTypeAnnotation() throws Exception { + IType jdtType = createTestEmbeddedWith2AttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride newAnnotation = (AttributeOverride)attributeResource.addAnnotation(2, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + newAnnotation.setName("BAZ"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"), @AttributeOverride(name=\"BAR\"),"); + assertSourceContains("@AttributeOverride(name=\"BAZ\")})"); + + + attributeResource.move(0, 2, JPA.ATTRIBUTE_OVERRIDES); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAZ\"), @AttributeOverride(name=\"FOO\"),"); + assertSourceContains("@AttributeOverride(name=\"BAR\")})"); + } + + public void testMoveJavaTypeAnnotation2() throws Exception { + IType jdtType = createTestEmbeddedWith2AttributeOverrides(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + AttributeOverride newAnnotation = (AttributeOverride) attributeResource.addAnnotation(2, JPA.ATTRIBUTE_OVERRIDE, JPA.ATTRIBUTE_OVERRIDES); + newAnnotation.setName("BAZ"); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"FOO\"), @AttributeOverride(name=\"BAR\"),"); + assertSourceContains("@AttributeOverride(name=\"BAZ\")})"); + + attributeResource.move(1, 0, JPA.ATTRIBUTE_OVERRIDES); + assertSourceContains("@AttributeOverrides({@AttributeOverride(name=\"BAR\"), @AttributeOverride(name=\"FOO\"),"); + assertSourceContains("@AttributeOverride(name=\"BAZ\")})"); + } + + //more detailed tests in JPTToolsTests + public void testIsPersistableField() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + assertTrue(attributeResource.isPersistable()); + } + + public void testIsPersistableField2() throws Exception { + IType jdtType = createTestEntityWithNonResolvingField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + Collection<JavaPersistentAttributeResource> attributes = (Collection<JavaPersistentAttributeResource>) ClassTools.fieldValue(typeResource, "attributes"); + JavaPersistentAttributeResource attributeResource = attributes.iterator().next(); + + assertEquals("foo", attributeResource.getName()); + assertTrue(attributeResource.isForField()); + assertFalse(attributeResource.isPersistable()); + + this.javaProject.createType("test", "Foo.java", "public class Foo {}"); + this.javaResourceModel.resolveTypes(); + + assertTrue(attributeResource.isPersistable()); + } + + public void testGetQualifiedTypeName() throws Exception { + IType jdtType = createTestEntityWithNonResolvingField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + Collection<JavaPersistentAttributeResource> attributes = (Collection<JavaPersistentAttributeResource>) ClassTools.fieldValue(typeResource, "attributes"); + JavaPersistentAttributeResource attributeResource = attributes.iterator().next(); + + assertEquals("foo", attributeResource.getName()); + assertNull(attributeResource.getQualifiedTypeName()); + + this.javaProject.createType("test", "Foo.java", "public class Foo {}"); + this.javaResourceModel.resolveTypes(); + + assertEquals("test.Foo", attributeResource.getQualifiedTypeName()); + } + + + //more detailed tests in JPTToolsTests + public void testIsPersistableMethod() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.properties().next(); + + assertTrue(attributeResource.isPersistable()); + } + + public void testIsPersistableMethod2() throws Exception { + IType jdtType = createTestEntityWithNonResolvingMethod(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + Collection<JavaPersistentAttributeResource> attributes = (Collection<JavaPersistentAttributeResource>) ClassTools.fieldValue(typeResource, "attributes"); + JavaPersistentAttributeResource attributeResource = (JavaPersistentAttributeResource) attributes.toArray()[3]; + + assertEquals("foo", attributeResource.getName()); + assertTrue(attributeResource.isForProperty()); + assertFalse(attributeResource.isPersistable()); + + this.javaProject.createType("test", "Foo.java", "public class Foo {}"); + this.javaResourceModel.resolveTypes(); + + assertTrue(attributeResource.isPersistable()); + } + + //this tests that we handle mutliple variable declarations in one line. + //The annotations should apply to all fields defined. This is not really a useful + //thing to do with JPA beyond the most basic things that use default column names + public void testMultipleVariableDeclarationsPerLine() throws Exception { + IType jdtType = createTestEntityMultipleVariableDeclarationsPerLine(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertEquals(4, CollectionTools.size(typeResource.fields())); + Iterator<JavaPersistentAttributeResource> fields = typeResource.fields(); + JavaPersistentAttributeResource attributeResource = fields.next(); + Column column = (Column) attributeResource.annotation(JPA.COLUMN); + assertEquals("baz", column.getName()); + + attributeResource = fields.next(); + column = (Column) attributeResource.annotation(JPA.COLUMN); + assertEquals("baz", column.getName()); + + } + //TODO add tests for JPTTools static methods +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentTypeResourceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentTypeResourceTests.java new file mode 100644 index 0000000000..5e964dc430 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaPersistentTypeResourceTests.java @@ -0,0 +1,977 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.List; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.AccessType; +import org.eclipse.jpt.core.internal.resource.java.Embeddable; +import org.eclipse.jpt.core.internal.resource.java.EmbeddableImpl; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.EntityImpl; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.MappedSuperclass; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.Table; +import org.eclipse.jpt.utility.internal.ClassTools; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JavaPersistentTypeResourceTests extends JavaResourceModelTestCase { + + public JavaPersistentTypeResourceTests(String name) { + super(name); + } + + private void createEntityAnnotation() throws Exception{ + this.createAnnotationAndMembers("Entity", "String name() default \"\";"); + } + + private IType createTestEntity() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + private IType createTestInvalidAnnotations() throws Exception { + this.createAnnotationAndMembers("Foo", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>("javax.persistence.Foo"); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Foo"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Foo"); + } + }); + } + + private IType createTestEntityWithMemberEmbeddable() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Embeddable", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendMemberTypeTo(StringBuilder sb) { + sb.append(" @Embeddable"); + sb.append(CR); + sb.append(" public static class Foo { }").append(CR); + + } + }); + } + + private IType createTestEntityDuplicates() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity(name=\"FirstEntity\")").append(CR); + sb.append("@Entity(name=\"SecondEntity\")"); + } + }); + } + + private IType createTestEntityWithEmbeddable() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Embeddable", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.EMBEDDABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@Embeddable"); + } + }); + } + + private IType createTestEntityAnnotatedField() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityAnnotatedMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + private IType createTestEntityAnnotatedFieldAndMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityAnnotatedNonPersistableMethod() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.ID); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendGetNameMethodAnnotationTo(StringBuilder sb) { + sb.append("@Id"); + } + }); + } + + private IType createTestEntityAnnotatedPersistableMethodNonPersistableField() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Id", ""); + this.createAnnotationAndMembers("Column", ""); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Column"); + sb.append(" private transient int notPersistable;").append(CR); + sb.append(CR); + + } + @Override + public void appendGetIdMethodAnnotationTo(StringBuilder sb) { + sb.append("@Column"); + } + }); + } + private IType createTestEntityNoPersistableFields() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendMemberTypeTo(StringBuilder sb) { + sb.delete(sb.indexOf("private int id;"), sb.indexOf("private int id;") + "private int id;".length()); + sb.delete(sb.indexOf("private String name;"), sb.indexOf("private String name;") + "private String name;".length()); + } + }); + } + +// private IType createTestEntityLarge(final int i) throws Exception { +// return this.createTestType(new DefaultAnnotationWriter() { +// @Override +// public Iterator<String> imports() { +// return new ArrayIterator<String>(JPA.ENTITY + i); +// } +// @Override +// public void appendTypeAnnotationTo(StringBuilder sb) { +// sb.append("@Entity" + i); +// sb.append(CR); +// } +// @Override +// public String typeName() { +// return TYPE_NAME + i; +// } +// }); +// } +// +// public void testLarge() throws Exception { +// for (int i = START; i <= END; i++) { +// this.createAnnotationAndMembers("Entity" +i, "String name();"); +// } +// +// List<IType> testTypes = new ArrayList<IType>(); +// for (int i = START; i <= END; i++) { +// testTypes.add(createTestEntityLarge(i)); +// } +// long start = System.currentTimeMillis(); +// List<JavaPersistentTypeResource> typeResources = new ArrayList<JavaPersistentTypeResource>(); +// for (int i = 0; i < END; i++) { +// typeResources.add(buildJavaTypeResource(testTypes.get(i))); +// } +// long end = System.currentTimeMillis(); +// +// System.out.println(end-start + "ms"); +// for (int i = 0; i < END; i++) { +// assertEquals(1, CollectionTools.size(typeResources.get(i).javaTypeMappingAnnotations())); +// assertNotNull(typeResources.get(i).javaTypeMappingAnnotation(JPA.ENTITY + (i+1))); +// } +// +// +//// assertEquals(0, CollectionTools.size(typeResource.javaTypeAnnotations())); +// } + + + + private IType createTestEntityWithTable() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Table", "String name(); String schema();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@Table(name=\"FOO\", schema=\"BAR\")"); + } + }); + } + + private IType createTestEntityWithTableAndIdClass() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Table", "String name(); String schema();"); + this.createAnnotationAndMembers("IdClass", "Class value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE, JPA.ID_CLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@Table"); + sb.append(CR); + sb.append("@IdClass"); + } + }); + } + + + private IType createTestEntityMultipleTables() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("Table", "String name(); String schema();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@Table(name=\"FOO\")"); + sb.append(CR); + sb.append("@Table(name=\"BAR\")"); + } + }); + } + + private IType createTestEntityWithSecondaryTable() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTable(name=\"FOO\")"); + } + }); + } + private IType createTestEntityWithEmptySecondaryTables() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name();"); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTables()"); + } + }); + } + + private IType createTestEntityWithSecondaryTables() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name();"); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTables(@SecondaryTable(name=\"FOO\"))"); + } + }); + } + + private IType createTestEntityWith2SecondaryTables() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name();"); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTables({@SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAR\")})"); + } + }); + } + + private IType createTestEntityWithSecondaryTableAndSecondaryTables() throws Exception { + createEntityAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name();"); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTable(name=\"FOO\")"); + sb.append(CR); + sb.append("@SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\")})"); + } + }); + } + + private IType createTestEntityWithMemberTypes() throws Exception { + createEntityAnnotation(); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + @Override + public void appendMemberTypeTo(StringBuilder sb) { + sb.append(" static class FooStatic {}").append(CR); + sb.append(CR); + sb.append(" class FooNotStatic {}").append(CR); + sb.append(CR); + sb.append(" @interface MyAnnotation {}").append(CR); + sb.append(CR); + sb.append(" enum MyEnum {}").append(CR); + } + }); + } + + public void testJavaTypeAnnotations() throws Exception { + IType testType = this.createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertEquals(1, CollectionTools.size(typeResource.annotations())); + } + + public void testJavaTypeAnnotation() throws Exception { + IType testType = this.createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertNotNull(typeResource.annotation(JPA.TABLE)); + } + + public void testJavaTypeAnnotationNull() throws Exception { + IType testType = this.createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertNull(typeResource.annotation(JPA.TABLE)); + } + + //This will result in a compilation error, but we assume the first table found + public void testDuplicateAnnotations() throws Exception { + IType testType = this.createTestEntityMultipleTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table tableResource = (Table) typeResource.annotation(JPA.TABLE); + assertEquals("FOO", tableResource.getName()); + } + + public void testRemoveTable() throws Exception { + IType testType = this.createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + typeResource.removeAnnotation(JPA.TABLE); + + assertSourceDoesNotContain("@Table"); + } + + public void testRemoveTableName() throws Exception { + IType testType = this.createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table tableResource = (Table) typeResource.annotation(JPA.TABLE); + tableResource.setSchema(null); + assertSourceContains("@Table(name=\"FOO\")"); + + tableResource.setName(null); + assertSourceDoesNotContain("@Table"); + + //TODO should I be calling this in the test? where should the IElementChangedListener be set up? + typeResource.updateFromJava(JDTTools.buildASTRoot(jdtType())); + assertNull(typeResource.annotation(JPA.TABLE)); + } + + public void testMultipleTypeMappings() throws Exception { + IType testType = this.createTestEntityWithEmbeddable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + assertEquals(2, CollectionTools.size(typeResource.mappingAnnotations())); + assertEquals(0, CollectionTools.size(typeResource.annotations())); + assertNotNull(typeResource.mappingAnnotation(JPA.EMBEDDABLE)); + assertNotNull(typeResource.mappingAnnotation(JPA.ENTITY)); + + JavaResource javaTypeMappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(javaTypeMappingAnnotation instanceof Embeddable); + assertSourceContains("@Entity"); + assertSourceContains("@Embeddable"); + + typeResource.setMappingAnnotation(JPA.MAPPED_SUPERCLASS); + assertEquals(1, CollectionTools.size(typeResource.mappingAnnotations())); + javaTypeMappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(javaTypeMappingAnnotation instanceof MappedSuperclass); + assertSourceDoesNotContain("@Entity"); + assertSourceContains("@MappedSuperclass"); + assertSourceDoesNotContain("@Embeddable"); + } + + public void testSetJavaTypeMappingAnnotation() throws Exception { + IType testType = createTestType(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertEquals(0, CollectionTools.size(typeResource.mappingAnnotations())); + + typeResource.setMappingAnnotation(JPA.ENTITY); + assertTrue(typeResource.mappingAnnotation() instanceof Entity); + assertSourceContains("@Entity"); + + typeResource.setMappingAnnotation(JPA.ENTITY); + //TODO need to test behavior here, throw an exception, remove the old @Entity and replace it, + //thus clearing out any annotation elements?? + } + + public void testSetJavaTypeMappingAnnotation2() throws Exception { + IType testType = createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertTrue(typeResource.mappingAnnotation() instanceof Entity); + + typeResource.setMappingAnnotation(JPA.EMBEDDABLE); + assertTrue(typeResource.mappingAnnotation() instanceof Embeddable); + + assertSourceDoesNotContain("@Entity"); + assertSourceContains("@Table"); + } + + public void testAddJavaTypeAnnotation() throws Exception { + IType testType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + assertSourceDoesNotContain("@Table"); + typeResource.addAnnotation(JPA.TABLE); + + assertSourceContains("@Table"); + } + + public void testRemoveJavaTypeAnnotation() throws Exception { + IType testType = createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertSourceContains("@Table"); + typeResource.removeAnnotation(JPA.TABLE); + assertSourceDoesNotContain("@Table"); + } + + + //update source code to change from @Entity to @Embeddable and make sure @Table is not removed + public void testChangeTypeMappingInSource() throws Exception { + IType jdtType = createTestEntityWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + testType().removeAnnotation(((EntityImpl) typeResource.mappingAnnotation()).getDeclarationAnnotationAdapter()); + + this.createAnnotationAndMembers("Embeddable", "String name();"); + jdtType.getCompilationUnit().createImport("javax.persistence.Embeddable", null, new NullProgressMonitor()); + + testType().newMarkerAnnotation(EmbeddableImpl.DECLARATION_ANNOTATION_ADAPTER); + + typeResource.updateFromJava(JDTTools.buildASTRoot(jdtType)); + + + assertNotNull(typeResource.annotation(JPA.TABLE)); + assertNull(typeResource.mappingAnnotation(JPA.ENTITY)); + assertNotNull(typeResource.mappingAnnotation(JPA.EMBEDDABLE)); + assertSourceContains("@Table"); + } + + public void testJavaTypeAnnotationsNestable() throws Exception { + IType jdtType = createTestEntityWithSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertEquals(1, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + assertEquals("FOO", secondaryTableResource.getName()); + } + + public void testJavaTypeAnnotationsNoNestable() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertEquals(0, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testJavaTypeAnnotationsContainerNoNestable() throws Exception { + IType jdtType = createTestEntityWithEmptySecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertEquals(0, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testJavaTypeAnnotationsNestableAndContainer() throws Exception { + IType jdtType = createTestEntityWithSecondaryTableAndSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + assertEquals("BAR", secondaryTableResource.getName()); + } + + // @Entity -->> @Entity + // @SecondaryTable(name="FOO") + public void testAddJavaTypeAnnotationNestableContainer() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("FOO"); + assertSourceContains("@SecondaryTable(name=\"FOO\")"); + } + + // @Entity -->> @Entity + // @SecondaryTable(name="FOO") @SecondaryTables({@SecondaryTable(name="FOO"), @SecondaryTable(name="BAR")}) + public void testAddJavaTypeAnnotationNestableContainer2() throws Exception { + IType jdtType = createTestEntityWithSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"),@SecondaryTable(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + // @Entity + // @SecondaryTables(@SecondaryTable(name="FOO")) + // || + // \/ + // @Entity + // @SecondaryTables({@SecondaryTable(name="FOO"), @SecondaryTable(name="BAR")}) + public void testAddJavaTypeAnnotationNestableContainer3() throws Exception { + IType jdtType = createTestEntityWithSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"),@SecondaryTable(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testAddJavaTypeAnnotationNestableContainer5() throws Exception { + IType jdtType = createTestEntityWithSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAR\"),@SecondaryTable(name=\"FOO\")})"); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testAddJavaTypeAnnotationNestableContainer6() throws Exception { + IType jdtType = createTestEntityWithSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAR\"),@SecondaryTable(name=\"FOO\")})"); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + + secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource.setName("BAZ"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAZ\"),@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"FOO\")})"); + + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + // @Entity + // @SecondaryTable(name=\"FOO\") + // @SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\")}) + // || + // \/ + // @Entity + // @SecondaryTable(name=\"FOO\") + // @SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\"), @SecondaryTable(name=\"BOO\")}) + public void testAddJavaTypeAnnotationNestableContainer4() throws Exception { + IType jdtType = createTestEntityWithSecondaryTableAndSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + + SecondaryTable secondaryTableResource = (SecondaryTable) typeResource.addAnnotation(2, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\"), @SecondaryTable})"); + secondaryTableResource.setName("BOO"); + + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + + Iterator<JavaResource> secondaryTableAnnotations = typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTableResource = (SecondaryTable) secondaryTableAnnotations.next(); + assertEquals("BAR", secondaryTableResource.getName()); + secondaryTableResource = (SecondaryTable) secondaryTableAnnotations.next(); + assertEquals("BAZ", secondaryTableResource.getName()); + secondaryTableResource = (SecondaryTable) secondaryTableAnnotations.next(); + assertEquals("BOO", secondaryTableResource.getName()); + + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\"), @SecondaryTable(name=\"BOO\")})"); + } + + //@Entity + //@SecondaryTable(name="FOO") + public void testRemoveJavaTypeAnnotationNestableContainer() throws Exception { + IType jdtType = createTestEntityWithSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + + //@Entity + //@SecondaryTables(@SecondaryTable(name="FOO")) + public void testRemoveJavaTypeAnnotationNestableContainer2() throws Exception { + IType jdtType = createTestEntityWithSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + assertSourceDoesNotContain("@SecondaryTable"); + assertSourceDoesNotContain("@SecondaryTables"); + } + + public void testRemoveJavaTypeAnnotationIndex() throws Exception { + IType jdtType = createTestEntityWith2SecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + typeResource.removeAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + + assertSourceDoesNotContain("@SecondaryTable(name=\"FOO\""); + assertSourceContains("@SecondaryTable(name=\"BAR\""); + assertSourceDoesNotContain("@SecondaryTables"); + } + + public void testRemoveJavaTypeAnnotationIndex2() throws Exception { + IType jdtType = createTestEntityWith2SecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable newAnnotation = (SecondaryTable)typeResource.addAnnotation(2, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + newAnnotation.setName("BAZ"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\")})"); + + typeResource.removeAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAZ\")})"); + } + + public void testMoveJavaTypeAnnotation() throws Exception { + IType jdtType = createTestEntityWith2SecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable newAnnotation = (SecondaryTable)typeResource.addAnnotation(2, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + newAnnotation.setName("BAZ"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\")})"); + + typeResource.move(0, 2, JPA.SECONDARY_TABLES); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAZ\"), @SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAR\")})"); + } + + public void testMoveJavaTypeAnnotation2() throws Exception { + IType jdtType = createTestEntityWith2SecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable newAnnotation = (SecondaryTable)typeResource.addAnnotation(2, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + newAnnotation.setName("BAZ"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\"), @SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\")})"); + + typeResource.move(2, 0, JPA.SECONDARY_TABLES); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAR\"), @SecondaryTable(name=\"BAZ\"), @SecondaryTable(name=\"FOO\")})"); + } + + public void testNestedTypes() throws Exception { + IType jdtType = createTestEntityWithMemberTypes(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals("only persistable types should be returned by nestedTypes()", 1, CollectionTools.size(persistentType.nestedTypes())); + + List<JavaPersistentTypeResource> nestedTypes = (List<JavaPersistentTypeResource>) ClassTools.fieldValue(persistentType, "nestedTypes"); + + assertEquals(4, CollectionTools.size(nestedTypes)); + + } + + public void testDuplicateEntityAnnotations() throws Exception { + IType jdtType = createTestEntityDuplicates(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + Entity javaTypeMappingAnnotation = (Entity) persistentType.mappingAnnotation(JPA.ENTITY); + assertEquals("FirstEntity", javaTypeMappingAnnotation.getName()); + + assertEquals(1, CollectionTools.size(persistentType.mappingAnnotations())); + + javaTypeMappingAnnotation = (Entity) persistentType.mappingAnnotation(); + assertEquals("FirstEntity", javaTypeMappingAnnotation.getName()); + + } + + + public void testAttributes() throws Exception { + + } + + public void testFields() throws Exception { + + } + + public void testProperties() throws Exception { + + } + + public void testGetAccessNoAttributesAnnotated() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertNull(persistentType.getAccess()); + } + + public void testGetAccessFieldsAnnotated() throws Exception { + IType jdtType = createTestEntityAnnotatedField(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals(AccessType.FIELD, persistentType.getAccess()); + } + + public void testGetAccessMethodsAnnotated() throws Exception { + IType jdtType = createTestEntityAnnotatedMethod(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals(AccessType.PROPERTY, persistentType.getAccess()); + } + + public void testGetAccessFieldsAndMethodsAnnotated() throws Exception { + IType jdtType = createTestEntityAnnotatedFieldAndMethod(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals(AccessType.FIELD, persistentType.getAccess()); + } + + public void testGetAccessNonPersistableMethodAnnotated() throws Exception { + IType jdtType = createTestEntityAnnotatedNonPersistableMethod(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertNull(persistentType.getAccess()); + } + + public void testGetAccessPersistableMethodAndNonPersistableFieldAnnotated() throws Exception { + IType jdtType = createTestEntityAnnotatedPersistableMethodNonPersistableField(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals(AccessType.PROPERTY, persistentType.getAccess()); + } + + public void testGetAccessNoPersistableFieldsAnnotated() throws Exception { + IType jdtType = createTestEntityNoPersistableFields(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals(AccessType.PROPERTY, persistentType.getAccess()); + } + + //TODO more tests here with superclasses other than Object. + //1. Test where the superclass does not resolve + //2. Test a superclass that does resolve + //3. What about a superclass that is a class file in a jar?? + //4. + public void testGetSuperclassQualifiedName() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertEquals("java.lang.Object", persistentType.getSuperClassQualifiedName()); + + } + + //more detailed tests in JPTToolsTests + public void testIsPersistable() throws Exception { + IType jdtType = createTestEntity(); + JavaPersistentTypeResource persistentType = buildJavaTypeResource(jdtType); + + assertTrue(persistentType.isPersistable()); + } + + public void testAnnotatedMemberType() throws Exception { + IType testType = this.createTestEntityWithMemberEmbeddable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertNotNull(typeResource.mappingAnnotation(JPA.ENTITY)); + assertNull(typeResource.mappingAnnotation(JPA.EMBEDDABLE)); + + JavaPersistentTypeResource nestedType = typeResource.nestedTypes().next(); + assertNull(nestedType.mappingAnnotation(JPA.ENTITY)); + assertNotNull(nestedType.mappingAnnotation(JPA.EMBEDDABLE)); + } + + public void testInvalidAnnotations() throws Exception { + IType testType = this.createTestInvalidAnnotations(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + assertEquals(0, CollectionTools.size(typeResource.mappingAnnotations())); + assertEquals(0, CollectionTools.size(typeResource.annotations())); + + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + assertEquals(0, CollectionTools.size(attributeResource.mappingAnnotations())); + assertEquals(0, CollectionTools.size(attributeResource.annotations())); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaResourceModelTestCase.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaResourceModelTestCase.java new file mode 100644 index 0000000000..c321e33a64 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JavaResourceModelTestCase.java @@ -0,0 +1,141 @@ +/******************************************************************************* + * 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.core.tests.internal.resource.java; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResourceProxy; +import org.eclipse.core.resources.IResourceProxyVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ElementChangedEvent; +import org.eclipse.jdt.core.IElementChangedListener; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jpt.core.internal.IContextModel; +import org.eclipse.jpt.core.internal.IJpaProject; +import org.eclipse.jpt.core.internal.JpaProject; +import org.eclipse.jpt.core.internal.JptCorePlugin; +import org.eclipse.jpt.core.internal.SimpleJpaProjectConfig; +import org.eclipse.jpt.core.internal.jdtutility.NullAnnotationEditFormatter; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResourceModel; +import org.eclipse.jpt.core.tests.internal.jdtutility.AnnotationTestCase; +import org.eclipse.jpt.utility.internal.StringTools; + +public class JavaResourceModelTestCase extends AnnotationTestCase +{ + private JavaElementChangeListener javaElementChangeListener; + protected JavaResourceModel javaResourceModel; + + + public JavaResourceModelTestCase(String name) { + super(name); + } + @Override + protected void setUp() throws Exception { + super.setUp(); + this.javaElementChangeListener = new JavaElementChangeListener(); + JavaCore.addElementChangedListener(this.javaElementChangeListener); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + JavaCore.removeElementChangedListener(this.javaElementChangeListener); + this.javaElementChangeListener = null; + } + + /** + * Forward the Java element change event back to the JPA model manager. + */ + private class JavaElementChangeListener implements IElementChangedListener { + JavaElementChangeListener() { + super(); + } + public void elementChanged(ElementChangedEvent event) { + if (JavaResourceModelTestCase.this.javaResourceModel != null) { + JavaResourceModelTestCase.this.javaResourceModel.handleJavaElementChangedEvent(event); + } + } + @Override + public String toString() { + return StringTools.buildToStringFor(this); + } + } + + protected IType createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception { + return this.javaProject.createType("javax.persistence", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }"); + } + + protected IType createEnumAndMembers(String enumName, String enumBody) throws Exception { + return this.javaProject.createType("javax.persistence", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }"); + } + + //build up a dummy JpaProject that does not have JpaFiles in it and does not update from java changes + protected IJpaProject buildJpaProject() throws CoreException { + return new TestJpaProject(this.buildJpaProjectConfig(this.javaProject.getProject())); + } + + protected class TestJpaProject extends JpaProject { + protected TestJpaProject(IJpaProject.Config config) throws CoreException { + super(config); + } + + @Override + protected IResourceProxyVisitor buildInitialResourceProxyVisitor() { + return new IResourceProxyVisitor() { + public boolean visit(IResourceProxy proxy) throws CoreException { + return false; // ignore all the files in the Eclipse project + } + }; + } + + @Override + protected IContextModel buildContextModel() { + return null; // no context model + } + + @Override + public void update() { + // ignore all updates, since there is no context model + } + + } + + private IJpaProject.Config buildJpaProjectConfig(IProject project) { + SimpleJpaProjectConfig config = new SimpleJpaProjectConfig(); + config.setProject(project); + config.setJpaPlatform(JptCorePlugin.jpaPlatform(project)); + config.setConnectionProfileName(JptCorePlugin.connectionProfileName(project)); + config.setDiscoverAnnotatedClasses(JptCorePlugin.discoverAnnotatedClasses(project)); + return config; + } + + protected JavaPersistentTypeResource buildJavaTypeResource(IType testType) + throws CoreException { + this.javaResourceModel = buildJavaResourceModel(testType); + this.javaResourceModel.resolveTypes(); + return this.javaResourceModel.resource().getPersistentType(); + } + + protected JavaResourceModel buildJavaResourceModel(IType testType) throws CoreException { + if (this.javaResourceModel != null) { + throw new IllegalStateException(); + } + IFile file = (IFile) testType.getResource(); + IJpaProject jpaProject = buildJpaProject(); + return new JavaResourceModel( + file, + jpaProject.jpaPlatform().annotationProvider(), + MODIFY_SHARED_DOCUMENT_COMMAND_EXECUTOR_PROVIDER, + NullAnnotationEditFormatter.instance()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnTests.java new file mode 100644 index 0000000000..08ff3bcaeb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnTests.java @@ -0,0 +1,369 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JoinColumnTests extends JavaResourceModelTestCase { + + 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 JoinColumnTests(String name) { + super(name); + } + + private void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", + "String name() default \"\"; " + + "String referencedColumnName() default \"\"; " + + "boolean unique() default false; " + + "boolean nullable() default true; " + + "boolean insertable() default true; " + + "boolean updatable() default true; " + + "String columnDefinition() default \"\"; " + + "String table() default \"\"; "); + } + + private IType createTestJoinColumn() throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn"); + } + }); + } + + private IType createTestJoinColumnWithName() throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn(name=\"" + COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestJoinColumnWithTable() throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn(table=\"" + COLUMN_TABLE + "\")"); + } + }); + } + + private IType createTestJoinColumnWithReferencedColumnName() throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn(referencedColumnName=\"" + COLUMN_REFERENCED_COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestJoinColumnWithColumnDefinition() throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn(columnDefinition=\"" + COLUMN_COLUMN_DEFINITION + "\")"); + } + }); + } + + private IType createTestJoinColumnWithBooleanElement(final String booleanElement) throws Exception { + createJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinColumn(" + booleanElement + "=true)"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + assertNotNull(column); + assertNull(column.getName()); + assertNull(column.getNullable()); + assertNull(column.getInsertable()); + assertNull(column.getUnique()); + assertNull(column.getUpdatable()); + assertNull(column.getTable()); + assertNull(column.getReferencedColumnName()); + assertNull(column.getColumnDefinition()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@JoinColumn(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetTable() throws Exception { + IType testType = this.createTestJoinColumnWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + assertEquals(COLUMN_TABLE, column.getTable()); + } + + public void testSetTable() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getTable()); + + column.setTable("Foo"); + assertEquals("Foo", column.getTable()); + + assertSourceContains("@JoinColumn(table=\"Foo\")"); + + + column.setTable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetReferencedColumnName() throws Exception { + IType testType = this.createTestJoinColumnWithReferencedColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + assertEquals(COLUMN_REFERENCED_COLUMN_NAME, column.getReferencedColumnName()); + } + + public void testSetReferencedColumnName() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getReferencedColumnName()); + + column.setReferencedColumnName("Foo"); + assertEquals("Foo", column.getReferencedColumnName()); + + assertSourceContains("@JoinColumn(referencedColumnName=\"Foo\")"); + + + column.setReferencedColumnName(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestJoinColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@JoinColumn(columnDefinition=\"Foo\")"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetUnique() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("unique"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertTrue(column.getUnique()); + } + + public void testSetUnique() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getUnique()); + + column.setUnique(false); + assertFalse(column.getUnique()); + + assertSourceContains("@JoinColumn(unique=false)"); + + column.setUnique(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetNullable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("nullable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertTrue(column.getNullable()); + } + + public void testSetNullable() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getNullable()); + + column.setNullable(false); + assertFalse(column.getNullable()); + + assertSourceContains("@JoinColumn(nullable=false)"); + + column.setNullable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetInsertable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("insertable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertTrue(column.getInsertable()); + } + + public void testSetInsertable() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getInsertable()); + + column.setInsertable(false); + assertFalse(column.getInsertable()); + + assertSourceContains("@JoinColumn(insertable=false)"); + + column.setInsertable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetUpdatable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("updatable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertTrue(column.getUpdatable()); + } + + public void testSetUpdatable() throws Exception { + IType testType = this.createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotation(JPA.JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getUpdatable()); + + column.setUpdatable(false); + assertFalse(column.getUpdatable()); + + assertSourceContains("@JoinColumn(updatable=false)"); + + column.setUpdatable(null); + assertSourceDoesNotContain("@JoinColumn"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnsTests.java new file mode 100644 index 0000000000..50771617a2 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinColumnsTests.java @@ -0,0 +1,444 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JoinColumnsTests extends JavaResourceModelTestCase { + + 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 void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", + "String name() default \"\"; " + + "String referencedColumnName() default \"\"; " + + "boolean unique() default false; " + + "boolean nullable() default true; " + + "boolean insertable() default true; " + + "boolean updatable() default true; " + + "String columnDefinition() default \"\"; " + + "String table() default \"\"; "); + } + + private void createJoinColumnsAnnotation() throws Exception { + createJoinColumnAnnotation(); + this.createAnnotationAndMembers("JoinColumns", + "JoinColumn[] value();"); + } + + private IType createTestJoinColumns() throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumnWithName() throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumnWithTable() throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumnWithReferencedColumnName() throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumnWithColumnDefinition() throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumnWithBooleanElement(final String booleanElement) throws Exception { + createJoinColumnsAnnotation(); + 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 IType createTestJoinColumn() throws Exception { + createJoinColumnsAnnotation(); + 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\")"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + 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 { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@JoinColumns(@JoinColumn(name=\"Foo\"))"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetTable() throws Exception { + IType testType = this.createTestJoinColumnWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + assertEquals(COLUMN_TABLE, column.getTable()); + } + + public void testSetTable() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getTable()); + + column.setTable("Foo"); + assertEquals("Foo", column.getTable()); + + assertSourceContains("@JoinColumns(@JoinColumn(table=\"Foo\"))"); + + + column.setTable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetReferencedColumnName() throws Exception { + IType testType = this.createTestJoinColumnWithReferencedColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + assertEquals(COLUMN_REFERENCED_COLUMN_NAME, column.getReferencedColumnName()); + } + + public void testSetReferencedColumnName() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getReferencedColumnName()); + + column.setReferencedColumnName("Foo"); + assertEquals("Foo", column.getReferencedColumnName()); + + assertSourceContains("@JoinColumns(@JoinColumn(referencedColumnName=\"Foo\"))"); + + + column.setReferencedColumnName(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestJoinColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@JoinColumns(@JoinColumn(columnDefinition=\"Foo\"))"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetUnique() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("unique"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertTrue(column.getUnique()); + } + + public void testSetUnique() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getUnique()); + + column.setUnique(false); + assertFalse(column.getUnique()); + + assertSourceContains("@JoinColumns(@JoinColumn(unique=false))"); + + column.setUnique(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetNullable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("nullable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertTrue(column.getNullable()); + } + + public void testSetNullable() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getNullable()); + + column.setNullable(false); + assertFalse(column.getNullable()); + + assertSourceContains("@JoinColumns(@JoinColumn(nullable=false))"); + + column.setNullable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetInsertable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("insertable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertTrue(column.getInsertable()); + } + + public void testSetInsertable() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getInsertable()); + + column.setInsertable(false); + assertFalse(column.getInsertable()); + + assertSourceContains("@JoinColumns(@JoinColumn(insertable=false))"); + + column.setInsertable(null); + assertSourceDoesNotContain("@JoinColumn"); + } + + public void testGetUpdatable() throws Exception { + IType testType = this.createTestJoinColumnWithBooleanElement("updatable"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertTrue(column.getUpdatable()); + } + + public void testSetUpdatable() throws Exception { + IType testType = this.createTestJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + JoinColumn column = (JoinColumn) attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getUpdatable()); + + column.setUpdatable(false); + assertFalse(column.getUpdatable()); + + assertSourceContains("@JoinColumns(@JoinColumn(updatable=false))"); + + column.setUpdatable(null); + assertSourceDoesNotContain("@JoinColumn"); + assertSourceDoesNotContain("@JoinColumns"); + } + + + public void testAddJoinColumnCopyExisting() throws Exception { + IType jdtType = createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinColumn joinColumn = (JoinColumn) attributeResource.addAnnotation(1, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@JoinColumns({@JoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", table = \"TABLE\", unique = false, nullable = false, insertable = false, updatable = false, referencedColumnName = \"REF_NAME\"),@JoinColumn(name=\"FOO\")})"); + + assertNull(attributeResource.annotation(JPA.JOIN_COLUMN)); + assertNotNull(attributeResource.annotation(JPA.JOIN_COLUMNS)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS))); + } + + public void testAddJoinColumnToBeginningOfList() throws Exception { + IType jdtType = createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinColumn joinColumn = (JoinColumn) attributeResource.addAnnotation(1, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@JoinColumns({@JoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", table = \"TABLE\", unique = false, nullable = false, insertable = false, updatable = false, referencedColumnName = \"REF_NAME\"),@JoinColumn(name=\"FOO\")})"); + + joinColumn = (JoinColumn) attributeResource.addAnnotation(0, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("BAZ"); + assertSourceContains("@JoinColumns({@JoinColumn(name=\"BAZ\"),@JoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", table = \"TABLE\", unique = false, nullable = false, insertable = false, updatable = false, referencedColumnName = \"REF_NAME\"), @JoinColumn(name=\"FOO\")})"); + + Iterator<JavaResource> joinColumns = attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + assertEquals("BAZ", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("BAR", ((JoinColumn) joinColumns.next()).getName()); + assertEquals("FOO", ((JoinColumn) joinColumns.next()).getName()); + + assertNull(attributeResource.annotation(JPA.JOIN_COLUMN)); + assertNotNull(attributeResource.annotation(JPA.JOIN_COLUMNS)); + assertEquals(3, CollectionTools.size(attributeResource.annotations(JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS))); + } + + + public void testRemoveJoinColumnCopyExisting() throws Exception { + IType jdtType = createTestJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinColumn joinColumn = (JoinColumn) attributeResource.addAnnotation(1, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@JoinColumns({@JoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", table = \"TABLE\", unique = false, nullable = false, insertable = false, updatable = false, referencedColumnName = \"REF_NAME\"),@JoinColumn(name=\"FOO\")})"); + + attributeResource.removeAnnotation(1, JPA.JOIN_COLUMN, JPA.JOIN_COLUMNS); + assertSourceContains("@JoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", table = \"TABLE\", unique = false, nullable = false, insertable = false, updatable = false, referencedColumnName = \"REF_NAME\")"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinTableTests.java new file mode 100644 index 0000000000..f7be017dad --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JoinTableTests.java @@ -0,0 +1,711 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JoinColumn; +import org.eclipse.jpt.core.internal.resource.java.JoinTable; +import org.eclipse.jpt.core.internal.resource.java.UniqueConstraint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JoinTableTests extends JavaResourceModelTestCase { + + private static final String TABLE_NAME = "MY_TABLE"; + private static final String SCHEMA_NAME = "MY_SCHEMA"; + private static final String CATALOG_NAME = "MY_CATALOG"; + + public JoinTableTests(String name) { + super(name); + } + + private void createJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("JoinColumn", "String name() default \"\";" + + "String referencedColumnName() default \"\";" + + "boolean unique() default false;" + + "boolean nullable() default true;" + + "boolean insertable() default true;" + + "boolean updatable() default true;" + + "String columnDefinition() default \"\";" + + "String table() default \"\";"); + + } + + private void createUniqueConstraintAnnotation() throws Exception { + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + } + + private void createJoinTableAnnotation() throws Exception { + createJoinColumnAnnotation(); + createUniqueConstraintAnnotation(); + this.createAnnotationAndMembers("JoinTable", "String name() default \"\"; String catalog() default \"\"; String schema() default \"\";JoinColumn[] joinColumns() default {}; JoinColumn[] inverseJoinColumns() default {}; UniqueConstraint[] uniqueConstraints() default {};"); + } + + private IType createTestJoinTable() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable"); + } + }); + } + + private IType createTestJoinTableWithName() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(name=\"" + TABLE_NAME + "\")"); + } + }); + } + + private IType createTestJoinTableWithSchema() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE); + } + + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(schema=\"" + SCHEMA_NAME + "\")"); + } + }); + } + + private IType createTestJoinTableWithCatalog() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(catalog=\"" + CATALOG_NAME + "\")"); + } + }); + } + + private IType createTestJoinTableWithUniqueConstraints() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + } + }); + } + + private IType createTestJoinTableWithJoinColumns() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE, JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(joinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn})"); + } + }); + } + + private IType createTestJoinTableWithInverseJoinColumns() throws Exception { + createJoinTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.JOIN_TABLE, JPA.JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@JoinTable(inverseJoinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn})"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestJoinTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertEquals(TABLE_NAME, table.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertNull(table.getName()); + assertNull(table.getCatalog()); + assertNull(table.getSchema()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertNull(table.getName()); + + table.setName("Foo"); + assertEquals("Foo", table.getName()); + + assertSourceContains("@JoinTable(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestJoinTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertEquals(TABLE_NAME, table.getName()); + + table.setName(null); + assertNull(table.getName()); + + assertSourceDoesNotContain("@JoinTable"); + } + + public void testGetCatalog() throws Exception { + IType testType = this.createTestJoinTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertEquals(CATALOG_NAME, table.getCatalog()); + } + + public void testSetCatalog() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertNull(table.getCatalog()); + + table.setCatalog("Foo"); + assertEquals("Foo", table.getCatalog()); + + assertSourceContains("@JoinTable(catalog=\"Foo\")"); + } + + public void testSetCatalogNull() throws Exception { + IType testType = this.createTestJoinTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertEquals(CATALOG_NAME, table.getCatalog()); + + table.setCatalog(null); + assertNull(table.getCatalog()); + + assertSourceDoesNotContain("@JoinTable"); + } + + public void testGetSchema() throws Exception { + IType testType = this.createTestJoinTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertEquals(SCHEMA_NAME, table.getSchema()); + } + + public void testSetSchema() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertNotNull(table); + assertNull(table.getSchema()); + + table.setSchema("Foo"); + assertEquals("Foo", table.getSchema()); + + assertSourceContains("@JoinTable(schema=\"Foo\")"); + } + + public void testSetSchemaNull() throws Exception { + IType testType = this.createTestJoinTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertEquals(SCHEMA_NAME, table.getSchema()); + + table.setSchema(null); + assertNull(table.getSchema()); + + assertSourceDoesNotContain("@JoinTable"); + } + + public void testUniqueConstraints() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints2() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + + table.addUniqueConstraint(0); + table.addUniqueConstraint(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints3() throws Exception { + IType testType = this.createTestJoinTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddUniqueConstraint() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(1); + table.addUniqueConstraint(0).addColumnName("BAR"); + + assertEquals("BAR", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", table.uniqueConstraintAt(1).columnNames().next()); + assertEquals(0, table.uniqueConstraintAt(2).columnNamesSize()); + + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint})"); + } + + public void testRemoveUniqueConstraint() throws Exception { + IType testType = this.createTestJoinTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertEquals("BAR", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", table.uniqueConstraintAt(1).columnNames().next()); + assertEquals("BAZ", table.uniqueConstraintAt(2).columnNames().next()); + assertEquals(3, table.uniqueConstraintsSize()); + + table.removeUniqueConstraint(1); + assertEquals("BAR", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals("BAZ", table.uniqueConstraintAt(1).columnNames().next()); + assertEquals(2, table.uniqueConstraintsSize()); + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + table.removeUniqueConstraint(0); + assertEquals("BAZ", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals(1, table.uniqueConstraintsSize()); + assertSourceContains("@JoinTable(uniqueConstraints=@UniqueConstraint(columnNames={\"BAZ\"}))"); + + table.removeUniqueConstraint(0); + assertEquals(0, table.uniqueConstraintsSize()); + assertSourceDoesNotContain("@JoinTable"); + } + + public void testMoveUniqueConstraint() throws Exception { + IType testType = this.createTestJoinTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + table.moveUniqueConstraint(2, 0); + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"})})"); + } + + public void testMoveUniqueConstraint2() throws Exception { + IType testType = this.createTestJoinTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + table.moveUniqueConstraint(0, 2); + assertSourceContains("@JoinTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"})})"); + } + + public void testJoinColumns() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.joinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testJoinColumns2() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + + table.addJoinColumn(0); + table.addJoinColumn(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<JoinColumn> iterator = table.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testJoinColumns3() throws Exception { + IType testType = this.createTestJoinTableWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.joinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddJoinColumn() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + table.addJoinColumn(0).setName("FOO"); + table.addJoinColumn(1); + table.addJoinColumn(0).setName("BAR"); + + assertEquals("BAR", table.joinColumnAt(0).getName()); + assertEquals("FOO", table.joinColumnAt(1).getName()); + assertNull(table.joinColumnAt(2).getName()); + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"BAR\"),@JoinColumn(name=\"FOO\"), @JoinColumn})"); + } + + public void testRemoveJoinColumn() throws Exception { + IType testType = this.createTestJoinTableWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + table.addJoinColumn(0).setName("FOO"); + + assertEquals("FOO", table.joinColumnAt(0).getName()); + assertEquals("BAR", table.joinColumnAt(1).getName()); + assertNull(table.joinColumnAt(2).getName()); + assertEquals(3, table.joinColumnsSize()); + + table.removeJoinColumn(1); + assertEquals("FOO", table.joinColumnAt(0).getName()); + assertNull(table.joinColumnAt(1).getName()); + assertEquals(2, table.joinColumnsSize()); + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn})"); + + table.removeJoinColumn(0); + assertNull(table.joinColumnAt(0).getName()); + assertEquals(1, table.joinColumnsSize()); + assertSourceContains("@JoinTable(joinColumns=@JoinColumn)"); + + + table.removeJoinColumn(0); + assertEquals(0, table.joinColumnsSize()); + assertSourceDoesNotContain("@JoinTable"); + } + + public void testMoveJoinColumn() throws Exception { + IType testType = this.createTestJoinTableWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + JoinColumn joinColumn = table.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + table.addJoinColumn(0).setName("FOO"); + + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn})"); + + table.moveJoinColumn(2, 0); + assertEquals("BAR", table.joinColumnAt(0).getName()); + assertNull(table.joinColumnAt(1).getName()); + assertEquals("FOO", table.joinColumnAt(2).getName()); + assertEquals(3, table.joinColumnsSize()); + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn, @JoinColumn(name=\"FOO\")})"); + } + + public void testMoveJoinColumn2() throws Exception { + IType testType = this.createTestJoinTableWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + JoinColumn joinColumn = table.joinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setUnique(Boolean.FALSE); + joinColumn.setNullable(Boolean.FALSE); + joinColumn.setInsertable(Boolean.FALSE); + joinColumn.setUpdatable(Boolean.FALSE); + joinColumn.setColumnDefinition("COLUMN_DEF"); + joinColumn.setTable("TABLE"); + + table.addJoinColumn(0).setName("FOO"); + + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\"), @JoinColumn})"); + + + table.moveJoinColumn(0, 2); + assertNull(table.joinColumnAt(0).getName()); + assertEquals("FOO", table.joinColumnAt(1).getName()); + assertEquals("BAR", table.joinColumnAt(2).getName()); + assertEquals(3, table.joinColumnsSize()); + assertSourceContains("@JoinTable(joinColumns={@JoinColumn, @JoinColumn(name=\"FOO\"), @JoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", unique = false, nullable = false, insertable = false, updatable = false, columnDefinition = \"COLUMN_DEF\", table = \"TABLE\")})"); + } + + public void testSetJoinColumnName() throws Exception { + IType testType = this.createTestJoinTableWithJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.joinColumns(); + assertEquals(2, CollectionTools.size(iterator)); + + JoinColumn joinColumn = table.joinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@JoinTable(joinColumns={@JoinColumn(name=\"foo\"), @JoinColumn})"); + } + + public void testInverseJoinColumns() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.inverseJoinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testInverseJoinColumns2() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + + table.addInverseJoinColumn(0); + table.addInverseJoinColumn(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<JoinColumn> iterator = table.inverseJoinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testInverseJoinColumns3() throws Exception { + IType testType = this.createTestJoinTableWithInverseJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.inverseJoinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddInverseJoinColumn() throws Exception { + IType testType = this.createTestJoinTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + table.addInverseJoinColumn(0).setName("FOO"); + table.addInverseJoinColumn(1); + table.addInverseJoinColumn(0).setName("BAR"); + + assertEquals("BAR", table.inverseJoinColumnAt(0).getName()); + assertEquals("FOO", table.inverseJoinColumnAt(1).getName()); + assertNull(table.inverseJoinColumnAt(2).getName()); + assertSourceContains("@JoinTable(inverseJoinColumns={@JoinColumn(name=\"BAR\"),@JoinColumn(name=\"FOO\"), @JoinColumn})"); + } + + public void testRemoveInverseJoinColumn() throws Exception { + IType testType = this.createTestJoinTableWithInverseJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + table.addInverseJoinColumn(2).setName("FOO"); + + Iterator<JoinColumn> inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertNull(inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + table.removeInverseJoinColumn(1); + assertSourceContains("@JoinTable(inverseJoinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn(name=\"FOO\")})"); + inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + table.removeInverseJoinColumn(0); + assertSourceContains("@JoinTable(inverseJoinColumns=@JoinColumn(name=\"FOO\"))"); + inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertFalse(inverseJoinColumns.hasNext()); + + table.removeInverseJoinColumn(0); + assertSourceDoesNotContain("@JoinTable"); + } + + public void testMoveInverseJoinColumn() throws Exception { + IType testType = this.createTestJoinTableWithInverseJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + table.addInverseJoinColumn(0).setName("FOO"); + + Iterator<JoinColumn> inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertNull(inverseJoinColumns.next().getName()); + + table.moveInverseJoinColumn(2, 0); + inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertNull(inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + + assertSourceContains("@JoinTable(inverseJoinColumns={@JoinColumn(name=\"BAR\"), @JoinColumn, @JoinColumn(name=\"FOO\")})"); + } + + public void testMoveInverseJoinColumn2() throws Exception { + IType testType = this.createTestJoinTableWithInverseJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + table.addInverseJoinColumn(1).setName("FOO"); + + Iterator<JoinColumn> inverseJoinColumns = table.inverseJoinColumns(); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + assertNull(inverseJoinColumns.next().getName()); + + table.moveInverseJoinColumn(0, 2); + inverseJoinColumns = table.inverseJoinColumns(); + assertNull(inverseJoinColumns.next().getName()); + assertEquals("BAR", inverseJoinColumns.next().getName()); + assertEquals("FOO", inverseJoinColumns.next().getName()); + + assertSourceContains("@JoinTable(inverseJoinColumns={@JoinColumn, @JoinColumn(name=\"BAR\"), @JoinColumn(name=\"FOO\")})"); + } + + public void testSetInverseJoinColumnName() throws Exception { + IType testType = this.createTestJoinTableWithInverseJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JoinTable table = (JoinTable) attributeResource.annotation(JPA.JOIN_TABLE); + + ListIterator<JoinColumn> iterator = table.inverseJoinColumns(); + assertEquals(2, CollectionTools.size(iterator)); + + JoinColumn joinColumn = table.inverseJoinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@JoinTable(inverseJoinColumns={@JoinColumn(name=\"foo\"), @JoinColumn})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JpaCompilationUnitResourceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JpaCompilationUnitResourceTests.java new file mode 100644 index 0000000000..6e1d3714a7 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JpaCompilationUnitResourceTests.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.IJpaFile; +import org.eclipse.jpt.core.internal.IJpaProject; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaResourceModel; +import org.eclipse.jpt.core.internal.resource.java.JpaCompilationUnitResource; +import org.eclipse.jpt.core.tests.internal.jdtutility.AnnotationTestCase; +import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject; +import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class JpaCompilationUnitResourceTests extends AnnotationTestCase { + + public JpaCompilationUnitResourceTests(String name) { + super(name); + } + + @Override + protected TestJavaProject buildJavaProject(String projectName, boolean autoBuild) throws Exception { + return new TestJpaProject(projectName, autoBuild); // false = no auto-build + } + + private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception { + this.javaProject.createType("javax.persistence", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }"); + } + + private ICompilationUnit createTestCompilationUnit() throws Exception { + IType type = createTestEntity(); + return type.getCompilationUnit(); + } + + private IType createTestEntity() throws Exception { + this.createAnnotationAndMembers("Entity", "String name();"); + + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + } + }); + } + + protected JpaCompilationUnitResource getJpaCompilationUnitResource(ICompilationUnit testCompilationUnit) throws CoreException { + IJpaProject jpaProject = ((TestJpaProject) this.javaProject).getJpaProject(); + IJpaFile jpaFile = jpaProject.jpaFile((IFile) testCompilationUnit.getResource()); + JavaResourceModel javaResourceModel = (JavaResourceModel) jpaFile.getResourceModel(); + return javaResourceModel.resource(); + } + + public void testGetPersistentType() throws Exception { + ICompilationUnit compilationUnit = this.createTestCompilationUnit(); + JpaCompilationUnitResource jpaCompilationUnit = getJpaCompilationUnitResource(compilationUnit); + + assertTrue(jpaCompilationUnit.getPersistentType().mappingAnnotation() instanceof Entity); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JptJavaResourceTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JptJavaResourceTests.java new file mode 100644 index 0000000000..fca651df98 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/JptJavaResourceTests.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class JptJavaResourceTests { + + public static Test suite() { + return suite(true); + } + + public static Test suite(boolean all) { + TestSuite suite = new TestSuite(JptJavaResourceTests.class.getName()); + suite.addTestSuite(JpaCompilationUnitResourceTests.class); + suite.addTestSuite(JavaPersistentTypeResourceTests.class); + suite.addTestSuite(JavaPersistentAttributeResourceTests.class); + suite.addTestSuite(JPTToolsTests.class); + + suite.addTestSuite(AssociationOverrideTests.class); + suite.addTestSuite(AssociationOverridesTests.class); + suite.addTestSuite(AttributeOverrideTests.class); + suite.addTestSuite(AttributeOverridesTests.class); + suite.addTestSuite(BasicTests.class); + suite.addTestSuite(ColumnTests.class); + suite.addTestSuite(DiscriminatorValueTests.class); + suite.addTestSuite(DiscriminatorColumnTests.class); + suite.addTestSuite(EmbeddableTests.class); + suite.addTestSuite(EmbeddedTests.class); + suite.addTestSuite(EmbeddedIdTests.class); + suite.addTestSuite(EntityTests.class); + suite.addTestSuite(EnumeratedTests.class); + suite.addTestSuite(GeneratedValueTests.class); + suite.addTestSuite(IdClassTests.class); + suite.addTestSuite(IdTests.class); + suite.addTestSuite(InheritanceTests.class); + suite.addTestSuite(JoinColumnTests.class); + suite.addTestSuite(JoinColumnsTests.class); + suite.addTestSuite(JoinTableTests.class); + suite.addTestSuite(LobTests.class); + suite.addTestSuite(ManyToManyTests.class); + suite.addTestSuite(ManyToOneTests.class); + suite.addTestSuite(MapKeyTests.class); + suite.addTestSuite(MappedSuperclassTests.class); + suite.addTestSuite(NamedNativeQueryTests.class); + suite.addTestSuite(NamedNativeQueriesTests.class); + suite.addTestSuite(NamedQueryTests.class); + suite.addTestSuite(NamedQueriesTests.class); + suite.addTestSuite(OneToManyTests.class); + suite.addTestSuite(OneToOneTests.class); + suite.addTestSuite(OrderByTests.class); + suite.addTestSuite(PrimaryKeyJoinColumnTests.class); + suite.addTestSuite(PrimaryKeyJoinColumnsTests.class); + suite.addTestSuite(QueryHintTests.class); + suite.addTestSuite(SecondaryTableTests.class); + suite.addTestSuite(SecondaryTablesTests.class); + suite.addTestSuite(SequenceGeneratorTests.class); + suite.addTestSuite(TableGeneratorTests.class); + suite.addTestSuite(TableTests.class); + suite.addTestSuite(TemporalTests.class); + suite.addTestSuite(TransientTests.class); + suite.addTestSuite(VersionTests.class); + + return suite; + } + + private JptJavaResourceTests() { + super(); + throw new UnsupportedOperationException(); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/LobTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/LobTests.java new file mode 100644 index 0000000000..5eb4fb07c1 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/LobTests.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.Lob; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class LobTests extends JavaResourceModelTestCase { + + public LobTests(String name) { + super(name); + } + + private IType createTestLob() throws Exception { + this.createAnnotationAndMembers("Lob", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.LOB); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Lob"); + } + }); + } + + public void testLob() throws Exception { + IType testType = this.createTestLob(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Lob lob = (Lob) attributeResource.annotation(JPA.LOB); + assertNotNull(lob); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToManyTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToManyTests.java new file mode 100644 index 0000000000..6424e4d000 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToManyTests.java @@ -0,0 +1,413 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.FetchType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToMany; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class ManyToManyTests extends JavaResourceModelTestCase { + + public ManyToManyTests(String name) { + super(name); + } + + private IType createTestManyToMany() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "FetchType fetch() default FetchType.LAZY; CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany"); + } + }); + } + + private IType createTestManyToManyWithFetch() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "FetchType fetch() default FetchType.LAZY;"); + this.createEnumAndMembers("FetchType", "EAGER, LAZY"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY, JPA.FETCH_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(fetch=FetchType.EAGER)"); + } + }); + } + + private IType createTestManyToManyWithTargetEntity() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "Class targetEntity() default void.class; "); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(targetEntity=AnnotationTestType.class)"); + } + }); + } + + private IType createTestManyToManyWithMappedBy() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "String mappedBy() default\"\";"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(mappedBy=\"foo\")"); + } + }); + } + + private IType createTestManyToManyWithCascade() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(cascade=CascadeType.ALL)"); + } + }); + } + + private IType createTestManyToManyWithMultipleCascade() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(cascade={CascadeType.MERGE, CascadeType.REMOVE})"); + } + }); + } + + private IType createTestManyToManyWithDuplicateCascade() throws Exception { + this.createAnnotationAndMembers("ManyToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToMany(cascade={CascadeType.MERGE, CascadeType.MERGE})"); + } + }); + } + + public void testManyToMany() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertNotNull(manyToMany); + } + + public void testGetFetch() throws Exception { + IType testType = this.createTestManyToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(FetchType.EAGER, manyToMany.getFetch()); + } + + public void testSetFetch() throws Exception { + IType testType = this.createTestManyToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(FetchType.EAGER, manyToMany.getFetch()); + + manyToMany.setFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, manyToMany.getFetch()); + + assertSourceContains("@ManyToMany(fetch=LAZY)"); + } + + public void testSetFetchNull() throws Exception { + IType testType = this.createTestManyToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(FetchType.EAGER, manyToMany.getFetch()); + + manyToMany.setFetch(null); + assertNull(manyToMany.getFetch()); + + assertSourceContains("@ManyToMany"); + assertSourceDoesNotContain("fetch"); + } + + public void testGetTargetEntity() throws Exception { + IType testType = this.createTestManyToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(TYPE_NAME, manyToMany.getTargetEntity()); + } + + public void testSetTargetEntity() throws Exception { + IType testType = this.createTestManyToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(TYPE_NAME, manyToMany.getTargetEntity()); + + manyToMany.setTargetEntity("Foo"); + + assertSourceContains("@ManyToMany(targetEntity=Foo.class)"); + } + + public void testSetTargetEntityNull() throws Exception { + IType testType = this.createTestManyToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(TYPE_NAME, manyToMany.getTargetEntity()); + + manyToMany.setTargetEntity(null); + + assertSourceContains("@ManyToMany"); + assertSourceDoesNotContain("targetEntity"); + } + + + public void testGetFullyQualifiedTargetEntity() throws Exception { + IType testType = this.createTestManyToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(FULLY_QUALIFIED_TYPE_NAME, manyToMany.getFullyQualifiedTargetEntity()); + + manyToMany.setTargetEntity("Foo"); + + assertSourceContains("@ManyToMany(targetEntity=Foo.class)"); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertEquals("Foo", manyToMany.getTargetEntity()); + + assertNull(manyToMany.getFullyQualifiedTargetEntity()); + } + + public void testGetMappedBy() throws Exception { + IType testType = this.createTestManyToManyWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals("foo", manyToMany.getMappedBy()); + } + + public void testGetMappedByNull() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals(null, manyToMany.getMappedBy()); + } + + public void testSetMappedBy() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertNull(manyToMany.getMappedBy()); + manyToMany.setMappedBy("bar"); + assertEquals("bar", manyToMany.getMappedBy()); + + assertSourceContains("@ManyToMany(mappedBy=\"bar\")"); + } + + public void testSetMappedByNull() throws Exception { + IType testType = this.createTestManyToManyWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertEquals("foo", manyToMany.getMappedBy()); + + manyToMany.setMappedBy(null); + assertNull(manyToMany.getMappedBy()); + + assertSourceContains("@ManyToMany"); + assertSourceDoesNotContain("mappedBy"); + } + + public void testSetCascadeAll() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertFalse(manyToMany.isCascadeAll()); + + manyToMany.setCascadeAll(true); + assertSourceContains("@ManyToMany(cascade=ALL)"); + + manyToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToMany.isCascadeAll()); + } + + public void testSetCascadeMerge() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertFalse(manyToMany.isCascadeMerge()); + + manyToMany.setCascadeMerge(true); + assertSourceContains("@ManyToMany(cascade=MERGE)"); + + manyToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToMany.isCascadeMerge()); + } + + public void testSetCascadePersist() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertFalse(manyToMany.isCascadePersist()); + + manyToMany.setCascadePersist(true); + assertSourceContains("@ManyToMany(cascade=PERSIST)"); + + manyToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToMany.isCascadePersist()); + } + + public void testSetCascadeRemove() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertFalse(manyToMany.isCascadeRemove()); + + manyToMany.setCascadeRemove(true); + assertSourceContains("@ManyToMany(cascade=REMOVE)"); + + manyToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToMany.isCascadeRemove()); + } + + public void testSetCascadeRefresh() throws Exception { + IType testType = this.createTestManyToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertFalse(manyToMany.isCascadeRefresh()); + + manyToMany.setCascadeRefresh(true); + assertSourceContains("@ManyToMany(cascade=REFRESH)"); + + manyToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToMany.isCascadeRefresh()); + } + + public void testCascadeMoreThanOnce() throws Exception { + IType testType = this.createTestManyToManyWithCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertTrue(manyToMany.isCascadeAll()); + + manyToMany.setCascadeAll(true); + assertTrue(manyToMany.isCascadeAll()); + //a second CascadeType.All should not have been added + assertSourceContains("@ManyToMany(cascade=CascadeType.ALL)"); + + manyToMany.setCascadeAll(false); + assertFalse(manyToMany.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + + //test setting cascadeAll to false again, should just do nothing + manyToMany.setCascadeAll(false); + assertFalse(manyToMany.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + } + + public void testDuplicateCascade() throws Exception { + IType testType = this.createTestManyToManyWithDuplicateCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertTrue(manyToMany.isCascadeMerge()); + + manyToMany.setCascadeMerge(false); //TODO should the resource model handle this and remove both MERGE + //settings instead of having to set it false twice? + assertTrue(manyToMany.isCascadeMerge()); + + manyToMany.setCascadeMerge(false); + assertFalse(manyToMany.isCascadeMerge()); + + assertSourceDoesNotContain("cascade"); + } + + public void testMultipleCascade() throws Exception { + IType testType = this.createTestManyToManyWithMultipleCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToMany manyToMany = (ManyToMany) attributeResource.mappingAnnotation(JPA.MANY_TO_MANY); + assertTrue(manyToMany.isCascadeMerge()); + assertTrue(manyToMany.isCascadeRemove()); + + manyToMany.setCascadeMerge(false); + assertSourceContains("@ManyToMany(cascade=REMOVE)"); + + manyToMany.setCascadeRemove(false); + assertSourceDoesNotContain("cascade"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToOneTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToOneTests.java new file mode 100644 index 0000000000..82a2624cbf --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/ManyToOneTests.java @@ -0,0 +1,405 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.FetchType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.ManyToOne; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class ManyToOneTests extends JavaResourceModelTestCase { + + public ManyToOneTests(String name) { + super(name); + } + + private IType createTestManyToOne() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "FetchType fetch() default FetchType.LAZY; CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne"); + } + }); + } + + private IType createTestManyToOneWithFetch() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "FetchType fetch() default FetchType.LAZY;"); + this.createEnumAndMembers("FetchType", "EAGER, LAZY"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE, JPA.FETCH_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(fetch=FetchType.EAGER)"); + } + }); + } + + private IType createTestManyToOneWithTargetEntity() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "Class targetEntity() default void.class;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(targetEntity=AnnotationTestType.class)"); + } + }); + } + + private IType createTestManyToOneWithOptional() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "boolean optional() default true;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(optional=true)"); + } + }); + } + + private IType createTestManyToOneWithCascade() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(cascade=CascadeType.ALL)"); + } + }); + } + + private IType createTestManyToOneWithMultipleCascade() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REMOVE})"); + } + }); + } + + private IType createTestManyToOneWithDuplicateCascade() throws Exception { + this.createAnnotationAndMembers("ManyToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MANY_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@ManyToOne(cascade={CascadeType.MERGE, CascadeType.MERGE})"); + } + }); + } + + public void testManyToOne() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertNotNull(manyToOne); + } + + public void testGetFetch() throws Exception { + IType testType = this.createTestManyToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(FetchType.EAGER, manyToOne.getFetch()); + } + + public void testSetFetch() throws Exception { + IType testType = this.createTestManyToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(FetchType.EAGER, manyToOne.getFetch()); + + manyToOne.setFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, manyToOne.getFetch()); + + assertSourceContains("@ManyToOne(fetch=LAZY)"); + } + + public void testSetFetchNull() throws Exception { + IType testType = this.createTestManyToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(FetchType.EAGER, manyToOne.getFetch()); + + manyToOne.setFetch(null); + assertNull(manyToOne.getFetch()); + + assertSourceContains("@ManyToOne"); + assertSourceDoesNotContain("fetch"); + } + + + public void testGetTargetEntity() throws Exception { + IType testType = this.createTestManyToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(TYPE_NAME, manyToOne.getTargetEntity()); + } + + public void testSetTargetEntity() throws Exception { + IType testType = this.createTestManyToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(TYPE_NAME, manyToOne.getTargetEntity()); + + manyToOne.setTargetEntity("Foo"); + + assertSourceContains("@ManyToOne(targetEntity=Foo.class)"); + } + + public void testSetTargetEntityNull() throws Exception { + IType testType = this.createTestManyToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(TYPE_NAME, manyToOne.getTargetEntity()); + + manyToOne.setTargetEntity(null); + + assertSourceContains("@ManyToOne"); + assertSourceDoesNotContain("targetEntity"); + } + + + public void testGetFullyQualifiedTargetEntity() throws Exception { + IType testType = this.createTestManyToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertEquals(FULLY_QUALIFIED_TYPE_NAME, manyToOne.getFullyQualifiedTargetEntity()); + + manyToOne.setTargetEntity("Foo"); + + assertSourceContains("@ManyToOne(targetEntity=Foo.class)"); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertEquals("Foo", manyToOne.getTargetEntity()); + + assertNull(manyToOne.getFullyQualifiedTargetEntity()); + } + + public void testGetOptional() throws Exception { + IType testType = this.createTestManyToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.getOptional()); + } + + public void testSetOptional() throws Exception { + IType testType = this.createTestManyToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.getOptional()); + + manyToOne.setOptional(false); + assertFalse(manyToOne.getOptional()); + + assertSourceContains("@ManyToOne(optional=false)"); + } + + public void testSetOptionalNull() throws Exception { + IType testType = this.createTestManyToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.getOptional()); + + manyToOne.setOptional(null); + assertNull(manyToOne.getOptional()); + + assertSourceContains("@ManyToOne"); + assertSourceDoesNotContain("optional"); + } + + public void testSetCascadeAll() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertFalse(manyToOne.isCascadeAll()); + + manyToOne.setCascadeAll(true); + assertSourceContains("@ManyToOne(cascade=ALL)"); + + manyToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToOne.isCascadeAll()); + } + + public void testSetCascadeMerge() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertFalse(manyToOne.isCascadeMerge()); + + manyToOne.setCascadeMerge(true); + assertSourceContains("@ManyToOne(cascade=MERGE)"); + + manyToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToOne.isCascadeMerge()); + } + + public void testSetCascadePersist() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertFalse(manyToOne.isCascadePersist()); + + manyToOne.setCascadePersist(true); + assertSourceContains("@ManyToOne(cascade=PERSIST)"); + + manyToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToOne.isCascadePersist()); + } + + public void testSetCascadeRemove() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertFalse(manyToOne.isCascadeRemove()); + + manyToOne.setCascadeRemove(true); + assertSourceContains("@ManyToOne(cascade=REMOVE)"); + + manyToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToOne.isCascadeRemove()); + } + + public void testSetCascadeRefresh() throws Exception { + IType testType = this.createTestManyToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertFalse(manyToOne.isCascadeRefresh()); + + manyToOne.setCascadeRefresh(true); + assertSourceContains("@ManyToOne(cascade=REFRESH)"); + + manyToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(manyToOne.isCascadeRefresh()); + } + + public void testCascadeMoreThanOnce() throws Exception { + IType testType = this.createTestManyToOneWithCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.isCascadeAll()); + + manyToOne.setCascadeAll(true); + assertTrue(manyToOne.isCascadeAll()); + //a second CascadeType.All should not have been added + assertSourceContains("@ManyToOne(cascade=CascadeType.ALL)"); + + manyToOne.setCascadeAll(false); + assertFalse(manyToOne.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + + //test setting cascadeAll to false again, should just do nothing + manyToOne.setCascadeAll(false); + assertFalse(manyToOne.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + } + + public void testDuplicateCascade() throws Exception { + IType testType = this.createTestManyToOneWithDuplicateCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.isCascadeMerge()); + + manyToOne.setCascadeMerge(false);//TODO should the resource model handle this and remove both MERGE + //settings instead of having to set it false twice? + assertTrue(manyToOne.isCascadeMerge()); + + manyToOne.setCascadeMerge(false); + assertFalse(manyToOne.isCascadeMerge()); + + assertSourceDoesNotContain("cascade"); + } + + public void testMultipleCascade() throws Exception { + IType testType = this.createTestManyToOneWithMultipleCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + ManyToOne manyToOne = (ManyToOne) attributeResource.mappingAnnotation(JPA.MANY_TO_ONE); + assertTrue(manyToOne.isCascadeMerge()); + assertTrue(manyToOne.isCascadeRemove()); + + manyToOne.setCascadeMerge(false); + assertSourceContains("@ManyToOne(cascade=REMOVE)"); + + manyToOne.setCascadeRemove(false); + assertSourceDoesNotContain("cascade"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MapKeyTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MapKeyTests.java new file mode 100644 index 0000000000..de0297605c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MapKeyTests.java @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.MapKey; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class MapKeyTests extends JavaResourceModelTestCase { + + public MapKeyTests(String name) { + super(name); + } + + private IType createTestMapKey() throws Exception { + this.createAnnotationAndMembers("MapKey", "String name() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAP_KEY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@MapKey"); + } + }); + } + + private IType createTestMapKeyWithName() throws Exception { + this.createAnnotationAndMembers("MapKey", "String name() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAP_KEY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@MapKey(name=\"key\")"); + } + }); + } + + public void testMapKey() throws Exception { + IType testType = this.createTestMapKey(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + MapKey mapKey = (MapKey) attributeResource.annotation(JPA.MAP_KEY); + assertNotNull(mapKey); + } + + public void testGetName() throws Exception { + IType testType = this.createTestMapKeyWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + MapKey mapKey = (MapKey) attributeResource.annotation(JPA.MAP_KEY); + assertEquals("key", mapKey.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestMapKey(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + MapKey mapKey = (MapKey) attributeResource.annotation(JPA.MAP_KEY); + + mapKey.setName("foo"); + + assertSourceContains("@MapKey(name=\"foo\")"); + + mapKey.setName(null); + + assertSourceContains("@MapKey"); + assertSourceDoesNotContain("@MapKey(name=\"foo\")"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MappedSuperclassTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MappedSuperclassTests.java new file mode 100644 index 0000000000..3540e6cb34 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/MappedSuperclassTests.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.Entity; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.MappedSuperclass; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class MappedSuperclassTests extends JavaResourceModelTestCase { + + public MappedSuperclassTests(String name) { + super(name); + } + + private IType createTestMappedSuperclass() throws Exception { + this.createAnnotationAndMembers("MappedSuperclass", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + } + }); + } + + private IType createTestMappedSuperclassAndEntity() throws Exception { + this.createAnnotationAndMembers("MappedSuperclass", ""); + this.createAnnotationAndMembers("Entity", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.MAPPED_SUPERCLASS, JPA.ENTITY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@MappedSuperclass"); + sb.append("@Entity"); + } + }); + } + + public void testMappedSuperclass() throws Exception { + IType testType = this.createTestMappedSuperclass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaResource mappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof MappedSuperclass); + } + + public void testMappedSuperclassAndEntity() throws Exception { + IType testType = this.createTestMappedSuperclassAndEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + JavaResource mappingAnnotation = typeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Entity); + + MappedSuperclass mappedSuperclass = (MappedSuperclass) typeResource.mappingAnnotation(JPA.MAPPED_SUPERCLASS); + assertNotNull(mappedSuperclass); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueriesTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueriesTests.java new file mode 100644 index 0000000000..81edea512c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueriesTests.java @@ -0,0 +1,452 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQueries; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class NamedNativeQueriesTests extends JavaResourceModelTestCase { + + private static final String QUERY_NAME = "myQuery"; + private static final String QUERY_QUERY = "SELECT name FROM Employee"; + private static final String QUERY_RESULT_CLASS = "Result"; + private static final String QUERY_RESULT_SET_MAPPING = "resultSetMapping"; + + public NamedNativeQueriesTests(String name) { + super(name); + } + + private void createNamedNativeQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedNativeQuery", "String name(); " + + "String query();" + + "QueryHint[] hints() default{};"); + } + + private void createNamedNativeQueriesAnnotation() throws Exception { + createNamedNativeQueryAnnotation(); + this.createAnnotationAndMembers("NamedNativeQueries", + "NamedNativeQuery[] value();"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", "String name(); " + + "String value();"); + } + + private IType createTestNamedNativeQueries() throws Exception { + createNamedNativeQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERIES, JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQueries(@NamedNativeQuery)"); + } + }); + } + + private IType createTestNamedNativeQueryWithName() throws Exception { + return createTestNamedNativeQueryWithStringElement("name", QUERY_NAME); + } + + private IType createTestNamedNativeQueryWithQuery() throws Exception { + return createTestNamedNativeQueryWithStringElement("query", QUERY_QUERY); + } + + private IType createTestNamedNativeQueryWithResultSetMapping() throws Exception { + return createTestNamedNativeQueryWithStringElement("resultSetMapping", QUERY_RESULT_SET_MAPPING); + } + + + private IType createTestNamedNativeQueryWithStringElement(final String elementName, final String value) throws Exception { + createNamedNativeQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERIES, JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQueries(@NamedNativeQuery(" + elementName + "=\"" + value + "\"))"); + } + }); + } + + private IType createTestNamedNativeQueryWithResultClass() throws Exception { + createNamedNativeQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERIES, JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQueries(@NamedNativeQuery(resultClass=" + QUERY_RESULT_CLASS + ".class))"); + } + }); + } + private IType createTestNamedNativeQueryWithQueryHints() throws Exception { + createNamedNativeQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERIES, JPA.NAMED_NATIVE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQueries(@NamedNativeQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint}))"); + } + }); + } + + private IType createTestNamedNativeQuery() throws Exception { + createNamedNativeQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQuery(name=\"foo\", query=\"bar\", hints=@QueryHint(name=\"BAR\", value=\"FOO\"), resultClass=Foo.class, resultSetMapping=\"mapping\")"); + } + }); + } + + public void testNamedNativeQuery() throws Exception { + IType testType = this.createTestNamedNativeQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertNotNull(namedQuery); + } + + public void testGetName() throws Exception { + IType testType = this.createTestNamedNativeQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestNamedNativeQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedNativeQuery(name=\"foo\")"); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetQuery() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + } + + public void testSetQuery() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + namedQuery.setQuery("foo"); + assertEquals("foo", namedQuery.getQuery()); + + assertSourceContains("@NamedNativeQuery(query=\"foo\")"); + + namedQuery.setQuery(null); + assertNull(namedQuery.getQuery()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetResultClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_RESULT_CLASS, namedQuery.getResultClass()); + } + + public void testSetResultClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_RESULT_CLASS, namedQuery.getResultClass()); + + namedQuery.setResultClass("foo"); + assertEquals("foo", namedQuery.getResultClass()); + + assertSourceContains("@NamedNativeQuery(resultClass=foo.class)"); + + namedQuery.setResultClass(null); + assertNull(namedQuery.getResultClass()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetFullyQualifiedClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertNotNull(namedQuery.getResultClass()); + assertNull(namedQuery.getFullyQualifiedResultClass()); + + namedQuery.setResultClass(TYPE_NAME); + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, namedQuery.getFullyQualifiedResultClass()); + assertSourceContains("@NamedNativeQuery(resultClass=" + TYPE_NAME + ".class)"); + } + + public void testGetResultSetMapping() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultSetMapping(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_RESULT_SET_MAPPING, namedQuery.getResultSetMapping()); + } + + public void testSetResultSetMapping() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultSetMapping(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_RESULT_SET_MAPPING, namedQuery.getResultSetMapping()); + + namedQuery.setResultSetMapping("foo"); + assertEquals("foo", namedQuery.getResultSetMapping()); + + assertSourceContains("@NamedNativeQuery(resultSetMapping=\"foo\")"); + + namedQuery.setResultSetMapping(null); + assertNull(namedQuery.getResultSetMapping()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testHints() throws Exception { + IType testType = this.createTestNamedNativeQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testHints2() throws Exception { + IType testType = this.createTestNamedNativeQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0); + namedQuery.addHint(1); + namedQuery.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testHints3() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddHint() throws Exception { + IType testType = this.createTestNamedNativeQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1); + namedQuery.addHint(0).setName("BAR"); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals("FOO", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertSourceContains("@NamedNativeQuery(hints={@QueryHint(name=\"BAR\"),@QueryHint(name=\"FOO\"), @QueryHint})"); + } + + public void testRemoveHint() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.removeHint(2); + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertEquals(2, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQueries(@NamedNativeQuery(hints={@QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")}))"); + + namedQuery.removeHint(0); + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals(1, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQueries(@NamedNativeQuery(hints=@QueryHint(name=\"BAR\", value=\"FOO\")))"); + + + namedQuery.removeHint(0); + assertEquals(0, namedQuery.hintsSize()); + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testMoveHint() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(2, 0); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertNull(namedQuery.hintAt(1).getName()); + assertEquals("BAZ", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQueries(@NamedNativeQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint, @QueryHint(name=\"BAZ\")}))"); + } + + public void testMoveHint2() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQueries namedQueries = (NamedNativeQueries) typeResource.annotation(JPA.NAMED_NATIVE_QUERIES); + NamedNativeQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(0, 2); + + assertNull(namedQuery.hintAt(0).getName()); + assertEquals("BAZ", namedQuery.hintAt(1).getName()); + assertEquals("BAR", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQueries(@NamedNativeQuery(hints={@QueryHint, @QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")}))"); + } + + public void testAddNamedNativeQueryCopyExisting() throws Exception { + IType jdtType = createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.addAnnotation(1, JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedNativeQueries({@NamedNativeQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"), resultClass = Foo.class, resultSetMapping = \"mapping\"),@NamedNativeQuery(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.NAMED_NATIVE_QUERY)); + assertNotNull(typeResource.annotation(JPA.NAMED_NATIVE_QUERIES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES))); + } + + public void testAddNamedNativeQueryToBeginningOfList() throws Exception { + IType jdtType = createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.addAnnotation(1, JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedNativeQueries({@NamedNativeQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"), resultClass = Foo.class, resultSetMapping = \"mapping\"),@NamedNativeQuery(name=\"BAR\")})"); + + namedQuery = (NamedNativeQuery) typeResource.addAnnotation(0, JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + namedQuery.setName("BAZ"); + assertSourceContains("@NamedNativeQueries({@NamedNativeQuery(name=\"BAZ\"),@NamedNativeQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"), resultClass = Foo.class, resultSetMapping = \"mapping\"), @NamedNativeQuery(name=\"BAR\")})"); + + Iterator<JavaResource> namedQueries = typeResource.annotations(JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + assertEquals("BAZ", ((NamedNativeQuery) namedQueries.next()).getName()); + assertEquals("foo", ((NamedNativeQuery) namedQueries.next()).getName()); + assertEquals("BAR", ((NamedNativeQuery) namedQueries.next()).getName()); + + assertNull(typeResource.annotation(JPA.NAMED_NATIVE_QUERY)); + assertNotNull(typeResource.annotation(JPA.NAMED_NATIVE_QUERIES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES))); + } + + public void testRemoveNamedNativeQueryCopyExisting() throws Exception { + IType jdtType = createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.addAnnotation(1, JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedNativeQueries({@NamedNativeQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"), resultClass = Foo.class, resultSetMapping = \"mapping\"),@NamedNativeQuery(name=\"BAR\")})"); + + typeResource.removeAnnotation(1, JPA.NAMED_NATIVE_QUERY, JPA.NAMED_NATIVE_QUERIES); + assertSourceContains("@NamedNativeQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"), resultClass = Foo.class, resultSetMapping = \"mapping\")"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueryTests.java new file mode 100644 index 0000000000..2385a10a72 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedNativeQueryTests.java @@ -0,0 +1,367 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedNativeQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class NamedNativeQueryTests extends JavaResourceModelTestCase { + + private static final String QUERY_NAME = "myQuery"; + private static final String QUERY_QUERY = "SELECT name FROM Employee"; + private static final String QUERY_RESULT_CLASS = "Result"; + private static final String QUERY_RESULT_SET_MAPPING = "resultSetMapping"; + + public NamedNativeQueryTests(String name) { + super(name); + } + + private void createNamedNativeQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedNativeQuery", "String name(); " + + "String query();" + + "QueryHint[] hints() default{};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", "String name(); " + + "String value();"); + } + + private IType createTestNamedNativeQuery() throws Exception { + createNamedNativeQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQuery"); + } + }); + } + + private IType createTestNamedNativeQueryWithName() throws Exception { + return createTestNamedNativeQueryWithStringElement("name", QUERY_NAME); + } + + private IType createTestNamedNativeQueryWithQuery() throws Exception { + return createTestNamedNativeQueryWithStringElement("query", QUERY_QUERY); + } + + private IType createTestNamedNativeQueryWithResultSetMapping() throws Exception { + return createTestNamedNativeQueryWithStringElement("resultSetMapping", QUERY_RESULT_SET_MAPPING); + } + + + private IType createTestNamedNativeQueryWithStringElement(final String elementName, final String value) throws Exception { + createNamedNativeQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQuery(" + elementName + "=\"" + value + "\")"); + } + }); + } + + private IType createTestNamedNativeQueryWithResultClass() throws Exception { + createNamedNativeQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQuery(resultClass=" + QUERY_RESULT_CLASS + ".class)"); + } + }); + } + private IType createTestNamedNativeQueryWithQueryHints() throws Exception { + createNamedNativeQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_NATIVE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedNativeQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint})"); + } + }); + } + + public void testNamedNativeQuery() throws Exception { + IType testType = this.createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertNotNull(namedQuery); + } + + public void testGetName() throws Exception { + IType testType = this.createTestNamedNativeQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestNamedNativeQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedNativeQuery(name=\"foo\")"); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetQuery() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + } + + public void testSetQuery() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + namedQuery.setQuery("foo"); + assertEquals("foo", namedQuery.getQuery()); + + assertSourceContains("@NamedNativeQuery(query=\"foo\")"); + + namedQuery.setQuery(null); + assertNull(namedQuery.getQuery()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetResultClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_RESULT_CLASS, namedQuery.getResultClass()); + } + + public void testSetResultClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_RESULT_CLASS, namedQuery.getResultClass()); + + namedQuery.setResultClass("foo"); + assertEquals("foo", namedQuery.getResultClass()); + + assertSourceContains("@NamedNativeQuery(resultClass=foo.class)"); + + namedQuery.setResultClass(null); + assertNull(namedQuery.getResultClass()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testGetFullyQualifiedClass() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultClass(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertNotNull(namedQuery.getResultClass()); + assertNull(namedQuery.getFullyQualifiedResultClass()); + + namedQuery.setResultClass(TYPE_NAME); + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, namedQuery.getFullyQualifiedResultClass()); + assertSourceContains("@NamedNativeQuery(resultClass=" + TYPE_NAME + ".class)"); + } + + public void testGetResultSetMapping() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultSetMapping(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_RESULT_SET_MAPPING, namedQuery.getResultSetMapping()); + } + + public void testSetResultSetMapping() throws Exception { + IType testType = this.createTestNamedNativeQueryWithResultSetMapping(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + assertEquals(QUERY_RESULT_SET_MAPPING, namedQuery.getResultSetMapping()); + + namedQuery.setResultSetMapping("foo"); + assertEquals("foo", namedQuery.getResultSetMapping()); + + assertSourceContains("@NamedNativeQuery(resultSetMapping=\"foo\")"); + + namedQuery.setResultSetMapping(null); + assertNull(namedQuery.getResultSetMapping()); + + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testHints() throws Exception { + IType testType = this.createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testHints2() throws Exception { + IType testType = this.createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + + namedQuery.addHint(0); + namedQuery.addHint(1); + namedQuery.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testHints3() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + + public void testAddHint() throws Exception { + IType testType = this.createTestNamedNativeQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1); + namedQuery.addHint(0).setName("BAR"); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals("FOO", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertSourceContains("@NamedNativeQuery(hints={@QueryHint(name=\"BAR\"),@QueryHint(name=\"FOO\"), @QueryHint})"); + } + + public void testRemoveHint() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.removeHint(2); + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertEquals(2, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQuery(hints={@QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")})"); + + namedQuery.removeHint(0); + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals(1, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQuery(hints=@QueryHint(name=\"BAR\", value=\"FOO\"))"); + + + namedQuery.removeHint(0); + assertEquals(0, namedQuery.hintsSize()); + assertSourceDoesNotContain("@NamedNativeQuery"); + } + + public void testMoveHint() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(2, 0); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertNull(namedQuery.hintAt(1).getName()); + assertEquals("BAZ", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint, @QueryHint(name=\"BAZ\")})"); + } + + public void testMoveHint2() throws Exception { + IType testType = this.createTestNamedNativeQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedNativeQuery namedQuery = (NamedNativeQuery) typeResource.annotation(JPA.NAMED_NATIVE_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(0, 2); + + assertNull(namedQuery.hintAt(0).getName()); + assertEquals("BAZ", namedQuery.hintAt(1).getName()); + assertEquals("BAR", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedNativeQuery(hints={@QueryHint, @QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueriesTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueriesTests.java new file mode 100644 index 0000000000..9884843a5d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueriesTests.java @@ -0,0 +1,361 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.NamedQueries; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class NamedQueriesTests extends JavaResourceModelTestCase { + + private static final String QUERY_NAME = "myQuery"; + private static final String QUERY_QUERY = "SELECT name FROM Employee"; + + public NamedQueriesTests(String name) { + super(name); + } + + private void createNamedQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedQuery", "String name(); " + + "String query();" + + "QueryHint[] hints() default{};"); + } + + private void createNamedQueriesAnnotation() throws Exception { + createNamedQueryAnnotation(); + this.createAnnotationAndMembers("NamedQueries", + "NamedQuery[] value();"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", "String name(); " + + "String value();"); + } + + private IType createTestNamedQueries() throws Exception { + createNamedQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERIES, JPA.NAMED_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQueries(@NamedQuery)"); + } + }); + } + + private IType createTestNamedQueryWithName() throws Exception { + return createTestNamedQueryWithStringElement("name", QUERY_NAME); + } + + private IType createTestNamedQueryWithQuery() throws Exception { + return createTestNamedQueryWithStringElement("query", QUERY_QUERY); + } + + private IType createTestNamedQueryWithStringElement(final String elementName, final String value) throws Exception { + createNamedQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERIES, JPA.NAMED_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQueries(@NamedQuery(" + elementName + "=\"" + value + "\"))"); + } + }); + } + + private IType createTestNamedQueryWithQueryHints() throws Exception { + createNamedQueriesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERIES, JPA.NAMED_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQueries(@NamedQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint}))"); + } + }); + } + + + private IType createTestNamedQuery() throws Exception { + createNamedQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQuery(name=\"foo\", query=\"bar\", hints=@QueryHint(name=\"BAR\", value=\"FOO\"))"); + } + }); + } + + public void testNamedQuery() throws Exception { + IType testType = this.createTestNamedQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertNotNull(namedQuery); + } + + public void testGetName() throws Exception { + IType testType = this.createTestNamedQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestNamedQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedQuery(name=\"foo\")"); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedQuery"); + } + + public void testGetQuery() throws Exception { + IType testType = this.createTestNamedQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + } + + public void testSetQuery() throws Exception { + IType testType = this.createTestNamedQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + namedQuery.setQuery("foo"); + assertEquals("foo", namedQuery.getQuery()); + + assertSourceContains("@NamedQuery(query=\"foo\")"); + + namedQuery.setQuery(null); + assertNull(namedQuery.getQuery()); + + assertSourceDoesNotContain("@NamedQuery"); + } + + public void testHints() throws Exception { + IType testType = this.createTestNamedQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testHints2() throws Exception { + IType testType = this.createTestNamedQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0); + namedQuery.addHint(1); + namedQuery.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testHints3() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddHint() throws Exception { + IType testType = this.createTestNamedQueries(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1); + namedQuery.addHint(0).setName("BAR"); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals("FOO", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + + assertSourceContains("@NamedQuery(hints={@QueryHint(name=\"BAR\"),@QueryHint(name=\"FOO\"), @QueryHint})"); + } + + public void testRemoveHint() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.removeHint(2); + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertEquals(2, namedQuery.hintsSize()); + assertSourceContains("@NamedQueries(@NamedQuery(hints={@QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")}))"); + + namedQuery.removeHint(0); + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals(1, namedQuery.hintsSize()); + assertSourceContains("@NamedQueries(@NamedQuery(hints=@QueryHint(name=\"BAR\", value=\"FOO\")))"); + + namedQuery.removeHint(0); + assertEquals(0, namedQuery.hintsSize()); + assertSourceDoesNotContain("@NamedQueries"); + } + + public void testMoveHint() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(2, 0); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertNull(namedQuery.hintAt(1).getName()); + assertEquals("BAZ", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedQueries(@NamedQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint, @QueryHint(name=\"BAZ\")}))"); + } + + public void testMoveHint2() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQueries namedQueries = (NamedQueries) typeResource.annotation(JPA.NAMED_QUERIES); + NamedQuery namedQuery = namedQueries.nestedAnnotations().next(); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(0, 2); + + assertNull(namedQuery.hintAt(0).getName()); + assertEquals("BAZ", namedQuery.hintAt(1).getName()); + assertEquals("BAR", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedQueries(@NamedQuery(hints={@QueryHint, @QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")}))"); + } + + public void testAddNamedQueryCopyExisting() throws Exception { + IType jdtType = createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedQuery namedQuery = (NamedQuery) typeResource.addAnnotation(1, JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedQueries({@NamedQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\")),@NamedQuery(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.NAMED_QUERY)); + assertNotNull(typeResource.annotation(JPA.NAMED_QUERIES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.NAMED_QUERY, JPA.NAMED_QUERIES))); + } + + public void testAddNamedQueryToBeginningOfList() throws Exception { + IType jdtType = createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedQuery namedQuery = (NamedQuery) typeResource.addAnnotation(1, JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedQueries({@NamedQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\")),@NamedQuery(name=\"BAR\")})"); + + + namedQuery = (NamedQuery) typeResource.addAnnotation(0, JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + namedQuery.setName("BAZ"); + assertSourceContains("@NamedQueries({@NamedQuery(name=\"BAZ\"),@NamedQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\")), @NamedQuery(name=\"BAR\")})"); + + Iterator<JavaResource> namedQueries = typeResource.annotations(JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + assertEquals("BAZ", ((NamedQuery) namedQueries.next()).getName()); + assertEquals("foo", ((NamedQuery) namedQueries.next()).getName()); + assertEquals("BAR", ((NamedQuery) namedQueries.next()).getName()); + + assertNull(typeResource.annotation(JPA.NAMED_QUERY)); + assertNotNull(typeResource.annotation(JPA.NAMED_QUERIES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.NAMED_QUERY, JPA.NAMED_QUERIES))); + } + + public void testRemoveNamedQueryCopyExisting() throws Exception { + IType jdtType = createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + NamedQuery namedQuery = (NamedQuery) typeResource.addAnnotation(1, JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + namedQuery.setName("BAR"); + assertSourceContains("@NamedQueries({@NamedQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\")),@NamedQuery(name=\"BAR\")})"); + + typeResource.removeAnnotation(1, JPA.NAMED_QUERY, JPA.NAMED_QUERIES); + assertSourceContains("@NamedQuery(name=\"foo\", query = \"bar\", hints = @QueryHint(name=\"BAR\", value = \"FOO\"))"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueryTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueryTests.java new file mode 100644 index 0000000000..5b9c9a0f03 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/NamedQueryTests.java @@ -0,0 +1,282 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class NamedQueryTests extends JavaResourceModelTestCase { + + private static final String QUERY_NAME = "myQuery"; + private static final String QUERY_QUERY = "SELECT name FROM Employee"; + + public NamedQueryTests(String name) { + super(name); + } + + private void createNamedQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedQuery", "String name(); " + + "String query();" + + "QueryHint[] hints() default{};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", "String name(); " + + "String value();"); + } + + private IType createTestNamedQuery() throws Exception { + createNamedQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQuery"); + } + }); + } + + private IType createTestNamedQueryWithName() throws Exception { + return createTestNamedQueryWithStringElement("name", QUERY_NAME); + } + + private IType createTestNamedQueryWithQuery() throws Exception { + return createTestNamedQueryWithStringElement("query", QUERY_QUERY); + } + + private IType createTestNamedQueryWithStringElement(final String elementName, final String value) throws Exception { + createNamedQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQuery(" + elementName + "=\"" + value + "\")"); + } + }); + } + + private IType createTestNamedQueryWithQueryHints() throws Exception { + createNamedQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint})"); + } + }); + } + + public void testNamedQuery() throws Exception { + IType testType = this.createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + assertNotNull(namedQuery); + } + + public void testGetName() throws Exception { + IType testType = this.createTestNamedQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestNamedQueryWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedQuery(name=\"foo\")"); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedQuery"); + } + + public void testGetQuery() throws Exception { + IType testType = this.createTestNamedQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + } + + public void testSetQuery() throws Exception { + IType testType = this.createTestNamedQueryWithQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + assertEquals(QUERY_QUERY, namedQuery.getQuery()); + + namedQuery.setQuery("foo"); + assertEquals("foo", namedQuery.getQuery()); + + assertSourceContains("@NamedQuery(query=\"foo\")"); + + namedQuery.setQuery(null); + assertNull(namedQuery.getQuery()); + + assertSourceDoesNotContain("@NamedQuery"); + } + + public void testHints() throws Exception { + IType testType = this.createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testHints2() throws Exception { + IType testType = this.createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + + namedQuery.addHint(0); + namedQuery.addHint(1); + namedQuery.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testHints3() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + + ListIterator<QueryHint> iterator = namedQuery.hints(); + + assertEquals(2, CollectionTools.size(iterator)); + + iterator = namedQuery.hints(); + assertEquals("BAR", iterator.next().getName()); + assertNull(iterator.next().getName()); + } + + public void testAddHint() throws Exception { + IType testType = this.createTestNamedQuery(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1); + QueryHint queryHint = namedQuery.addHint(0); + queryHint.setName("BAR"); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals("FOO", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertSourceContains("@NamedQuery(hints={@QueryHint(name=\"BAR\"),@QueryHint(name=\"FOO\"), @QueryHint})"); + } + + public void testRemoveHint() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.removeHint(2); + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertEquals(2, namedQuery.hintsSize()); + assertSourceContains("@NamedQuery(hints={@QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")})"); + + namedQuery.removeHint(0); + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals(1, namedQuery.hintsSize()); + assertSourceContains("@NamedQuery(hints=@QueryHint(name=\"BAR\", value=\"FOO\"))"); + + namedQuery.removeHint(0); + assertEquals(0, namedQuery.hintsSize()); + assertSourceDoesNotContain("@NamedQuery"); + } + + public void testMoveHint() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(2, 0); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertNull(namedQuery.hintAt(1).getName()); + assertEquals("BAZ", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedQuery(hints={@QueryHint(name=\"BAR\", value=\"FOO\"), @QueryHint, @QueryHint(name=\"BAZ\")})"); + } + + public void testMoveHint2() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + + namedQuery.moveHint(0, 2); + + assertNull(namedQuery.hintAt(0).getName()); + assertEquals("BAZ", namedQuery.hintAt(1).getName()); + assertEquals("BAR", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.hintsSize()); + assertSourceContains("@NamedQuery(hints={@QueryHint, @QueryHint(name=\"BAZ\"), @QueryHint(name=\"BAR\", value=\"FOO\")})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToManyTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToManyTests.java new file mode 100644 index 0000000000..59f53e5de8 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToManyTests.java @@ -0,0 +1,415 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.FetchType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.OneToMany; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class OneToManyTests extends JavaResourceModelTestCase { + + public OneToManyTests(String name) { + super(name); + } + + private IType createTestOneToMany() throws Exception { + this.createAnnotationAndMembers("OneToMany", "FetchType fetch() default FetchType.LAZY; CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany"); + } + }); + } + + private IType createTestOneToManyWithFetch() throws Exception { + this.createAnnotationAndMembers("OneToMany", "FetchType fetch() default FetchType.LAZY;"); + this.createEnumAndMembers("FetchType", "EAGER, LAZY"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY, JPA.FETCH_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(fetch=FetchType.EAGER)"); + } + }); + } + + private IType createTestOneToManyWithTargetEntity() throws Exception { + this.createAnnotationAndMembers("OneToMany", "Class targetEntity() default void.class;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(targetEntity=AnnotationTestType.class)"); + } + }); + } + + private IType createTestOneToManyWithMappedBy() throws Exception { + this.createAnnotationAndMembers("OneToMany", "String mappedBy() default\"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(mappedBy=\"foo\")"); + } + }); + } + + private IType createTestOneToManyWithCascade() throws Exception { + this.createAnnotationAndMembers("OneToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(cascade=CascadeType.ALL)"); + } + }); + } + + private IType createTestOneToManyWithMultipleCascade() throws Exception { + this.createAnnotationAndMembers("OneToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(cascade={CascadeType.MERGE, CascadeType.REMOVE})"); + } + }); + } + + private IType createTestOneToManyWithDuplicateCascade() throws Exception { + this.createAnnotationAndMembers("OneToMany", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_MANY, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToMany(cascade={CascadeType.MERGE, CascadeType.MERGE})"); + } + }); + } + + public void testOneToMany() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertNotNull(oneToMany); + } + + public void testGetFetch() throws Exception { + IType testType = this.createTestOneToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(FetchType.EAGER, oneToMany.getFetch()); + } + + public void testSetFetch() throws Exception { + IType testType = this.createTestOneToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(FetchType.EAGER, oneToMany.getFetch()); + + oneToMany.setFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToMany.getFetch()); + + assertSourceContains("@OneToMany(fetch=LAZY)"); + } + + public void testSetFetchNull() throws Exception { + IType testType = this.createTestOneToManyWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(FetchType.EAGER, oneToMany.getFetch()); + + oneToMany.setFetch(null); + assertNull(oneToMany.getFetch()); + + assertSourceContains("@OneToMany"); + assertSourceDoesNotContain("fetch"); + } + + + public void testGetTargetEntity() throws Exception { + IType testType = this.createTestOneToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(TYPE_NAME, oneToMany.getTargetEntity()); + } + + public void testSetTargetEntity() throws Exception { + IType testType = this.createTestOneToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(TYPE_NAME, oneToMany.getTargetEntity()); + + oneToMany.setTargetEntity("Foo"); + + assertSourceContains("@OneToMany(targetEntity=Foo.class)"); + } + + public void testSetTargetEntityNull() throws Exception { + IType testType = this.createTestOneToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(TYPE_NAME, oneToMany.getTargetEntity()); + + oneToMany.setTargetEntity(null); + + assertSourceContains("@OneToMany"); + assertSourceDoesNotContain("targetEntity"); + } + + + public void testGetFullyQualifiedTargetEntity() throws Exception { + IType testType = this.createTestOneToManyWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(FULLY_QUALIFIED_TYPE_NAME, oneToMany.getFullyQualifiedTargetEntity()); + + oneToMany.setTargetEntity("Foo"); + + assertSourceContains("@OneToMany(targetEntity=Foo.class)"); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertEquals("Foo", oneToMany.getTargetEntity()); + + assertNull(oneToMany.getFullyQualifiedTargetEntity()); + } + + public void testGetMappedBy() throws Exception { + IType testType = this.createTestOneToManyWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals("foo", oneToMany.getMappedBy()); + } + + + public void testGetMappedByNull() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals(null, oneToMany.getMappedBy()); + } + + public void testSetMappedBy() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertNull(oneToMany.getMappedBy()); + oneToMany.setMappedBy("bar"); + assertEquals("bar", oneToMany.getMappedBy()); + + assertSourceContains("@OneToMany(mappedBy=\"bar\")"); + } + + public void testSetMappedByNull() throws Exception { + IType testType = this.createTestOneToManyWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertEquals("foo", oneToMany.getMappedBy()); + + oneToMany.setMappedBy(null); + assertNull(oneToMany.getMappedBy()); + + assertSourceContains("@OneToMany"); + assertSourceDoesNotContain("mappedBy"); + } + + public void testSetCascadeAll() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertFalse(oneToMany.isCascadeAll()); + + oneToMany.setCascadeAll(true); + assertSourceContains("@OneToMany(cascade=ALL)"); + + oneToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToMany.isCascadeAll()); + } + + public void testSetCascadeMerge() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertFalse(oneToMany.isCascadeMerge()); + + oneToMany.setCascadeMerge(true); + assertSourceContains("@OneToMany(cascade=MERGE)"); + + oneToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToMany.isCascadeMerge()); + } + + public void testSetCascadePersist() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertFalse(oneToMany.isCascadePersist()); + + oneToMany.setCascadePersist(true); + assertSourceContains("@OneToMany(cascade=PERSIST)"); + + oneToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToMany.isCascadePersist()); + } + + public void testSetCascadeRemove() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertFalse(oneToMany.isCascadeRemove()); + + oneToMany.setCascadeRemove(true); + assertSourceContains("@OneToMany(cascade=REMOVE)"); + + oneToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToMany.isCascadeRemove()); + } + + public void testSetCascadeRefresh() throws Exception { + IType testType = this.createTestOneToMany(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertFalse(oneToMany.isCascadeRefresh()); + + oneToMany.setCascadeRefresh(true); + assertSourceContains("@OneToMany(cascade=REFRESH)"); + + oneToMany.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToMany.isCascadeRefresh()); + } + + public void testCascadeMoreThanOnce() throws Exception { + IType testType = this.createTestOneToManyWithCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertTrue(oneToMany.isCascadeAll()); + + oneToMany.setCascadeAll(true); + assertTrue(oneToMany.isCascadeAll()); + //a second CascadeType.All should not have been added + assertSourceContains("@OneToMany(cascade=CascadeType.ALL)"); + + oneToMany.setCascadeAll(false); + assertFalse(oneToMany.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + + //test setting cascadeAll to false again, should just do nothing + oneToMany.setCascadeAll(false); + assertFalse(oneToMany.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + } + + public void testDuplicateCascade() throws Exception { + IType testType = this.createTestOneToManyWithDuplicateCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertTrue(oneToMany.isCascadeMerge()); + + oneToMany.setCascadeMerge(false);//TODO should the resource model handle this and remove both MERGE + //settings instead of having to set it false twice? + assertTrue(oneToMany.isCascadeMerge()); + + oneToMany.setCascadeMerge(false); + assertFalse(oneToMany.isCascadeMerge()); + + assertSourceDoesNotContain("cascade"); + } + + public void testMultipleCascade() throws Exception { + IType testType = this.createTestOneToManyWithMultipleCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToMany oneToMany = (OneToMany) attributeResource.mappingAnnotation(JPA.ONE_TO_MANY); + assertTrue(oneToMany.isCascadeMerge()); + assertTrue(oneToMany.isCascadeRemove()); + + oneToMany.setCascadeMerge(false); + assertSourceContains("@OneToMany(cascade=REMOVE)"); + + oneToMany.setCascadeRemove(false); + assertSourceDoesNotContain("cascade"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToOneTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToOneTests.java new file mode 100644 index 0000000000..5f71bf21fb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OneToOneTests.java @@ -0,0 +1,466 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.FetchType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.OneToOne; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class OneToOneTests extends JavaResourceModelTestCase { + + public OneToOneTests(String name) { + super(name); + } + + private IType createTestOneToOne() throws Exception { + this.createAnnotationAndMembers("OneToOne", "FetchType fetch() default FetchType.LAZY; CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne"); + } + }); + } + + private IType createTestOneToOneWithFetch() throws Exception { + this.createAnnotationAndMembers("OneToOne", "FetchType fetch() default FetchType.LAZY;"); + this.createEnumAndMembers("FetchType", "EAGER, LAZY"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE, JPA.FETCH_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(fetch=FetchType.EAGER)"); + } + }); + } + + private IType createTestOneToOneWithTargetEntity() throws Exception { + this.createAnnotationAndMembers("OneToOne", "Class targetEntity() default void.class;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(targetEntity=AnnotationTestType.class)"); + } + }); + } + + private IType createTestOneToOneWithOptional() throws Exception { + this.createAnnotationAndMembers("OneToOne", "boolean optional() default true;"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(optional=true)"); + } + }); + } + + private IType createTestOneToOneWithMappedBy() throws Exception { + this.createAnnotationAndMembers("OneToOne", "String mappedBy() default\"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(mappedBy=\"foo\")"); + } + }); + } + + private IType createTestOneToOneWithCascade() throws Exception { + this.createAnnotationAndMembers("OneToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(cascade=CascadeType.ALL)"); + } + }); + } + + private IType createTestOneToOneWithMultipleCascade() throws Exception { + this.createAnnotationAndMembers("OneToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(cascade={CascadeType.MERGE, CascadeType.REMOVE})"); + } + }); + } + + private IType createTestOneToOneWithDuplicateCascade() throws Exception { + this.createAnnotationAndMembers("OneToOne", "CascadeType[] cascade() default = {};"); + this.createEnumAndMembers("CascadeType", "ALL, PERSIST, MERGE, REMOVE, REFRESH"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ONE_TO_ONE, JPA.CASCADE_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OneToOne(cascade={CascadeType.MERGE, CascadeType.MERGE})"); + } + }); + } + + public void testOneToOne() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertNotNull(oneToOne); + } + + public void testGetFetch() throws Exception { + IType testType = this.createTestOneToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(FetchType.EAGER, oneToOne.getFetch()); + } + + public void testSetFetch() throws Exception { + IType testType = this.createTestOneToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(FetchType.EAGER, oneToOne.getFetch()); + + oneToOne.setFetch(FetchType.LAZY); + assertEquals(FetchType.LAZY, oneToOne.getFetch()); + + assertSourceContains("@OneToOne(fetch=LAZY)"); + } + + public void testSetFetchNull() throws Exception { + IType testType = this.createTestOneToOneWithFetch(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(FetchType.EAGER, oneToOne.getFetch()); + + oneToOne.setFetch(null); + assertNull(oneToOne.getFetch()); + + assertSourceContains("@OneToOne"); + assertSourceDoesNotContain("fetch"); + } + + + public void testGetTargetEntity() throws Exception { + IType testType = this.createTestOneToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(TYPE_NAME, oneToOne.getTargetEntity()); + } + + public void testSetTargetEntity() throws Exception { + IType testType = this.createTestOneToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(TYPE_NAME, oneToOne.getTargetEntity()); + + oneToOne.setTargetEntity("Foo"); + + assertSourceContains("@OneToOne(targetEntity=Foo.class)"); + } + + public void testSetTargetEntityNull() throws Exception { + IType testType = this.createTestOneToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(TYPE_NAME, oneToOne.getTargetEntity()); + + oneToOne.setTargetEntity(null); + + assertSourceContains("@OneToOne"); + assertSourceDoesNotContain("targetEntity"); + } + + + public void testGetFullyQualifiedTargetEntity() throws Exception { + IType testType = this.createTestOneToOneWithTargetEntity(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(FULLY_QUALIFIED_TYPE_NAME, oneToOne.getFullyQualifiedTargetEntity()); + + oneToOne.setTargetEntity("Foo"); + + assertSourceContains("@OneToOne(targetEntity=Foo.class)"); + + typeResource.updateFromJava(JDTTools.buildASTRoot(testType)); + assertEquals("Foo", oneToOne.getTargetEntity()); + + assertNull(oneToOne.getFullyQualifiedTargetEntity()); + } + + public void testGetOptional() throws Exception { + IType testType = this.createTestOneToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.getOptional()); + } + + public void testSetOptional() throws Exception { + IType testType = this.createTestOneToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.getOptional()); + + oneToOne.setOptional(false); + assertFalse(oneToOne.getOptional()); + + assertSourceContains("@OneToOne(optional=false)"); + } + + public void testSetOptionalNull() throws Exception { + IType testType = this.createTestOneToOneWithOptional(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.getOptional()); + + oneToOne.setOptional(null); + assertNull(oneToOne.getOptional()); + + assertSourceContains("@OneToOne"); + assertSourceDoesNotContain("optional"); + } + + public void testGetMappedBy() throws Exception { + IType testType = this.createTestOneToOneWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals("foo", oneToOne.getMappedBy()); + } + + public void testGetMappedByNull() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals(null, oneToOne.getMappedBy()); + } + + public void testSetMappedBy() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertNull(oneToOne.getMappedBy()); + oneToOne.setMappedBy("bar"); + assertEquals("bar", oneToOne.getMappedBy()); + + assertSourceContains("@OneToOne(mappedBy=\"bar\")"); + } + + public void testSetMappedByNull() throws Exception { + IType testType = this.createTestOneToOneWithMappedBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertEquals("foo", oneToOne.getMappedBy()); + + oneToOne.setMappedBy(null); + assertNull(oneToOne.getMappedBy()); + + assertSourceContains("@OneToOne"); + assertSourceDoesNotContain("mappedBy"); + } + + public void testSetCascadeAll() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertFalse(oneToOne.isCascadeAll()); + + oneToOne.setCascadeAll(true); + assertSourceContains("@OneToOne(cascade=ALL)"); + + oneToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToOne.isCascadeAll()); + } + + public void testSetCascadeMerge() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertFalse(oneToOne.isCascadeMerge()); + + oneToOne.setCascadeMerge(true); + assertSourceContains("@OneToOne(cascade=MERGE)"); + + oneToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToOne.isCascadeMerge()); + } + + public void testSetCascadePersist() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertFalse(oneToOne.isCascadePersist()); + + oneToOne.setCascadePersist(true); + assertSourceContains("@OneToOne(cascade=PERSIST)"); + + oneToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToOne.isCascadePersist()); + } + + public void testSetCascadeRemove() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertFalse(oneToOne.isCascadeRemove()); + + oneToOne.setCascadeRemove(true); + assertSourceContains("@OneToOne(cascade=REMOVE)"); + + oneToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToOne.isCascadeRemove()); + } + + public void testSetCascadeRefresh() throws Exception { + IType testType = this.createTestOneToOne(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertFalse(oneToOne.isCascadeRefresh()); + + oneToOne.setCascadeRefresh(true); + assertSourceContains("@OneToOne(cascade=REFRESH)"); + + oneToOne.updateFromJava(JDTTools.buildASTRoot(testType)); + assertTrue(oneToOne.isCascadeRefresh()); + } + + public void testCascadeMoreThanOnce() throws Exception { + IType testType = this.createTestOneToOneWithCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.isCascadeAll()); + + oneToOne.setCascadeAll(true); + assertTrue(oneToOne.isCascadeAll()); + //a second CascadeType.All should not have been added + assertSourceContains("@OneToOne(cascade=CascadeType.ALL)"); + + oneToOne.setCascadeAll(false); + assertFalse(oneToOne.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + + //test setting cascadeAll to false again, should just do nothing + oneToOne.setCascadeAll(false); + assertFalse(oneToOne.isCascadeAll()); + + assertSourceDoesNotContain("cascade"); + } + + public void testDuplicateCascade() throws Exception { + IType testType = this.createTestOneToOneWithDuplicateCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.isCascadeMerge()); + + oneToOne.setCascadeMerge(false);//TODO should the resource model handle this and remove both MERGE + //settings instead of having to set it false twice? + assertTrue(oneToOne.isCascadeMerge()); + + oneToOne.setCascadeMerge(false); + assertFalse(oneToOne.isCascadeMerge()); + + assertSourceDoesNotContain("cascade"); + } + + public void testMultipleCascade() throws Exception { + IType testType = this.createTestOneToOneWithMultipleCascade(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OneToOne oneToOne = (OneToOne) attributeResource.mappingAnnotation(JPA.ONE_TO_ONE); + assertTrue(oneToOne.isCascadeMerge()); + assertTrue(oneToOne.isCascadeRemove()); + + oneToOne.setCascadeMerge(false); + assertSourceContains("@OneToOne(cascade=REMOVE)"); + + oneToOne.setCascadeRemove(false); + assertSourceDoesNotContain("cascade"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OrderByTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OrderByTests.java new file mode 100644 index 0000000000..bf9bd4f81a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/OrderByTests.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.OrderBy; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class OrderByTests extends JavaResourceModelTestCase { + + public OrderByTests(String name) { + super(name); + } + + private IType createTestOrderBy() throws Exception { + this.createAnnotationAndMembers("OrderBy", "String value() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ORDER_BY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OrderBy"); + } + }); + } + + private IType createTestOrderByWithValue() throws Exception { + this.createAnnotationAndMembers("OrderBy", "String value() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ORDER_BY); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@OrderBy(value=\"key\")"); + } + }); + } + + public void testOrderBy() throws Exception { + IType testType = this.createTestOrderBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OrderBy orderBy = (OrderBy) attributeResource.annotation(JPA.ORDER_BY); + assertNotNull(orderBy); + } + + public void testGetValue() throws Exception { + IType testType = this.createTestOrderByWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OrderBy orderBy = (OrderBy) attributeResource.annotation(JPA.ORDER_BY); + assertEquals("key", orderBy.getValue()); + } + + public void testSetValue() throws Exception { + IType testType = this.createTestOrderBy(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + OrderBy orderBy = (OrderBy) attributeResource.annotation(JPA.ORDER_BY); + + orderBy.setValue("foo"); + + assertSourceContains("@OrderBy(\"foo\")"); + + orderBy.setValue(null); + + assertSourceContains("@OrderBy"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnTests.java new file mode 100644 index 0000000000..db27db9e05 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnTests.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class PrimaryKeyJoinColumnTests extends JavaResourceModelTestCase { + + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String COLUMN_COLUMN_DEFINITION = "COLUMN_DEFINITION"; + private static final String COLUMN_REFERENCED_COLUMN_NAME = "MY_REF_COLUMN_NAME"; + + public PrimaryKeyJoinColumnTests(String name) { + super(name); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", + "String name() default \"\"; " + + "String referencedColumnName() default \"\"; " + + "String columnDefinition() default \"\"; "); + } + + private IType createTestPrimaryKeyJoinColumn() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumn"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithName() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumn(name=\"" + COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithReferencedColumnName() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumn(referencedColumnName=\"" + COLUMN_REFERENCED_COLUMN_NAME + "\")"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithColumnDefinition() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumn(columnDefinition=\"" + COLUMN_COLUMN_DEFINITION + "\")"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertNotNull(column); + assertNull(column.getReferencedColumnName()); + assertNull(column.getColumnDefinition()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@PrimaryKeyJoinColumn(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + + + + public void testGetReferencedColumnName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithReferencedColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertEquals(COLUMN_REFERENCED_COLUMN_NAME, column.getReferencedColumnName()); + } + + public void testSetReferencedColumnName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getReferencedColumnName()); + + column.setReferencedColumnName("Foo"); + assertEquals("Foo", column.getReferencedColumnName()); + + assertSourceContains("@PrimaryKeyJoinColumn(referencedColumnName=\"Foo\")"); + + + column.setReferencedColumnName(null); + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@PrimaryKeyJoinColumn(columnDefinition=\"Foo\")"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnsTests.java new file mode 100644 index 0000000000..ce04ec1bad --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/PrimaryKeyJoinColumnsTests.java @@ -0,0 +1,268 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class PrimaryKeyJoinColumnsTests extends JavaResourceModelTestCase { + + private static final String COLUMN_NAME = "MY_COLUMN"; + private static final String COLUMN_COLUMN_DEFINITION = "COLUMN_DEFINITION"; + private static final String COLUMN_REFERENCED_COLUMN_NAME = "MY_REF_COLUMN_NAME"; + + public PrimaryKeyJoinColumnsTests(String name) { + super(name); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", + "String name() default \"\"; " + + "String referencedColumnName() default \"\"; " + + "String columnDefinition() default \"\";"); + } + + private void createPrimaryKeyJoinColumnsAnnotation() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + this.createAnnotationAndMembers("PrimaryKeyJoinColumns", + "PrimaryKeyJoinColumn[] value();"); + } + + private IType createTestPrimaryKeyJoinColumns() throws Exception { + createPrimaryKeyJoinColumnsAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn)"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithName() throws Exception { + createPrimaryKeyJoinColumnsAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(name=\"" + COLUMN_NAME + "\"))"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithReferencedColumnName() throws Exception { + createPrimaryKeyJoinColumnsAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(referencedColumnName=\"" + COLUMN_REFERENCED_COLUMN_NAME + "\"))"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumnWithColumnDefinition() throws Exception { + createPrimaryKeyJoinColumnsAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(columnDefinition=\"" + COLUMN_COLUMN_DEFINITION + "\"))"); + } + }); + } + + private IType createTestPrimaryKeyJoinColumn() throws Exception { + createPrimaryKeyJoinColumnsAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\")"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + assertNotNull(column); + assertEquals(COLUMN_NAME, column.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + assertNotNull(column); + assertNull(column.getName()); + assertNull(column.getReferencedColumnName()); + assertNull(column.getColumnDefinition()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getName()); + + column.setName("Foo"); + assertEquals("Foo", column.getName()); + + assertSourceContains("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(name=\"Foo\"))"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + + assertEquals(COLUMN_NAME, column.getName()); + + column.setName(null); + assertNull(column.getName()); + + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + + public void testGetReferencedColumnName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithReferencedColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + assertEquals(COLUMN_REFERENCED_COLUMN_NAME, column.getReferencedColumnName()); + } + + public void testSetReferencedColumnName() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getReferencedColumnName()); + + column.setReferencedColumnName("Foo"); + assertEquals("Foo", column.getReferencedColumnName()); + + assertSourceContains("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(referencedColumnName=\"Foo\"))"); + + + column.setReferencedColumnName(null); + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + + public void testGetColumnDefinition() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumnWithColumnDefinition(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + assertEquals(COLUMN_COLUMN_DEFINITION, column.getColumnDefinition()); + } + + public void testSetColumnDefinition() throws Exception { + IType testType = this.createTestPrimaryKeyJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + PrimaryKeyJoinColumn column = (PrimaryKeyJoinColumn) attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS).next(); + + assertNotNull(column); + assertNull(column.getColumnDefinition()); + + column.setColumnDefinition("Foo"); + assertEquals("Foo", column.getColumnDefinition()); + + assertSourceContains("@PrimaryKeyJoinColumns(@PrimaryKeyJoinColumn(columnDefinition=\"Foo\"))"); + + + column.setColumnDefinition(null); + assertSourceDoesNotContain("@PrimaryKeyJoinColumn"); + } + + + public void testAddPrimaryKeyJoinColumnCopyExisting() throws Exception { + IType jdtType = createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + PrimaryKeyJoinColumn joinColumn = (PrimaryKeyJoinColumn) attributeResource.addAnnotation(1, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\"),@PrimaryKeyJoinColumn(name=\"FOO\")})"); + + assertNull(attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN)); + assertNotNull(attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMNS)); + assertEquals(2, CollectionTools.size(attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS))); + } + public void testAddPrimaryKeyJoinColumnToBeginningOfList() throws Exception { + IType jdtType = createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + PrimaryKeyJoinColumn joinColumn = (PrimaryKeyJoinColumn) attributeResource.addAnnotation(1, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\"),@PrimaryKeyJoinColumn(name=\"FOO\")})"); + + joinColumn = (PrimaryKeyJoinColumn) attributeResource.addAnnotation(0, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + joinColumn.setName("BAZ"); + assertSourceContains("@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name=\"BAZ\"),@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\"), @PrimaryKeyJoinColumn(name=\"FOO\")})"); + + Iterator<JavaResource> pkJoinColumns = attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + assertEquals("BAZ", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("BAR", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + assertEquals("FOO", ((PrimaryKeyJoinColumn) pkJoinColumns.next()).getName()); + + assertNull(attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMN)); + assertNotNull(attributeResource.annotation(JPA.PRIMARY_KEY_JOIN_COLUMNS)); + assertEquals(3, CollectionTools.size(attributeResource.annotations(JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS))); + } + + public void testRemovePrimaryKeyJoinColumnCopyExisting() throws Exception { + IType jdtType = createTestPrimaryKeyJoinColumn(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + PrimaryKeyJoinColumn joinColumn = (PrimaryKeyJoinColumn) attributeResource.addAnnotation(1, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + joinColumn.setName("FOO"); + assertSourceContains("@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\"),@PrimaryKeyJoinColumn(name=\"FOO\")})"); + + attributeResource.removeAnnotation(1, JPA.PRIMARY_KEY_JOIN_COLUMN, JPA.PRIMARY_KEY_JOIN_COLUMNS); + assertSourceContains("@PrimaryKeyJoinColumn(name=\"BAR\", columnDefinition = \"COLUMN_DEF\", referencedColumnName = \"REF_NAME\")"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/QueryHintTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/QueryHintTests.java new file mode 100644 index 0000000000..491a202468 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/QueryHintTests.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.NamedQuery; +import org.eclipse.jpt.core.internal.resource.java.QueryHint; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class QueryHintTests extends JavaResourceModelTestCase { + + private static final String QUERY_HINT_NAME = "myHint"; + private static final String QUERY_HINT_VALUE = "myValue"; + + public QueryHintTests(String name) { + super(name); + } + + private void createNamedQueryAnnotation() throws Exception { + createQueryHintAnnotation(); + this.createAnnotationAndMembers("NamedQuery", "String name(); " + + "String query();" + + "QueryHint[] hints() default{};"); + } + + private void createQueryHintAnnotation() throws Exception { + this.createAnnotationAndMembers("QueryHint", "String name(); " + + "String value();"); + } + + private IType createTestNamedQueryWithQueryHints() throws Exception { + createNamedQueryAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.NAMED_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedQuery(hints={@QueryHint(name=\"" + QUERY_HINT_NAME + "\", value=\"" + QUERY_HINT_VALUE + "\"), @QueryHint})"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + QueryHint queryHint = namedQuery.hints().next(); + assertEquals(QUERY_HINT_NAME, queryHint.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestNamedQueryWithQueryHints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + NamedQuery namedQuery = (NamedQuery) typeResource.annotation(JPA.NAMED_QUERY); + QueryHint queryHint = namedQuery.hints().next(); + assertEquals(QUERY_HINT_NAME, queryHint.getName()); + + queryHint.setName("foo"); + assertEquals("foo", queryHint.getName()); + + assertSourceContains("@QueryHint(name=\"foo\", value=\"" + QUERY_HINT_VALUE + "\")"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTableTests.java new file mode 100644 index 0000000000..fecb5c4b39 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTableTests.java @@ -0,0 +1,477 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.UniqueConstraint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class SecondaryTableTests extends JavaResourceModelTestCase { + + private static final String TABLE_NAME = "MY_TABLE"; + private static final String SCHEMA_NAME = "MY_SCHEMA"; + private static final String CATALOG_NAME = "MY_CATALOG"; + + public SecondaryTableTests(String name) { + super(name); + } + + private void createUniqueConstraintAnnotation() throws Exception { + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", "String name() default \"\"; String referencedColumnName() default \"\";String columnDefinition() default \"\";"); + } + + private void createSecondaryTableAnnotation() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + createUniqueConstraintAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name() default \"\"; " + + "String catalog() default \"\";" + + "String schema() default \"\";" + + "PrimaryKeyJoinColumn[] pkJoinColumns() default {};" + + "UniqueConstraint[] uniqueConstraints() default {};"); + } + + private IType createTestSecondaryTable() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable"); + } + }); + } + + private IType createTestSecondaryTableWithName() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable(name=\"" + TABLE_NAME + "\")"); + } + }); + } + + private IType createTestSecondaryTableWithSchema() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable(schema=\"" + SCHEMA_NAME + "\")"); + } + }); + } + private IType createTestSecondaryTableWithCatalog() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable(catalog=\"" + CATALOG_NAME + "\")"); + } + }); + } + + + private IType createTestSecondaryTableWithUniqueConstraints() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + } + }); + } + + private IType createTestSecondaryTableWithPkJoinColumns() throws Exception { + createSecondaryTableAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"), @PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\")})"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestSecondaryTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertEquals(TABLE_NAME, table.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertNull(table.getName()); + assertNull(table.getCatalog()); + assertNull(table.getSchema()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertNull(table.getName()); + + table.setName("Foo"); + assertEquals("Foo", table.getName()); + + assertSourceContains("@SecondaryTable(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestSecondaryTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertEquals(TABLE_NAME, table.getName()); + + table.setName(null); + assertNull(table.getName()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testGetCatalog() throws Exception { + IType testType = this.createTestSecondaryTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertEquals(CATALOG_NAME, table.getCatalog()); + } + + public void testSetCatalog() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertNull(table.getCatalog()); + + table.setCatalog("Foo"); + assertEquals("Foo", table.getCatalog()); + + assertSourceContains("@SecondaryTable(catalog=\"Foo\")"); + } + + public void testSetCatalogNull() throws Exception { + IType testType = this.createTestSecondaryTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertEquals(CATALOG_NAME, table.getCatalog()); + + table.setCatalog(null); + assertNull(table.getCatalog()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testGetSchema() throws Exception { + IType testType = this.createTestSecondaryTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertEquals(SCHEMA_NAME, table.getSchema()); + } + + public void testSetSchema() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNotNull(table); + assertNull(table.getSchema()); + + table.setSchema("Foo"); + assertEquals("Foo", table.getSchema()); + + assertSourceContains("@SecondaryTable(schema=\"Foo\")"); + } + + public void testSetSchemaNull() throws Exception { + IType testType = this.createTestSecondaryTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertEquals(SCHEMA_NAME, table.getSchema()); + + table.setSchema(null); + assertNull(table.getSchema()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + + public void testUniqueConstraints() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints2() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.addUniqueConstraint(0); + table.addUniqueConstraint(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints3() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(1); + table.addUniqueConstraint(0).addColumnName("BAR"); + + assertEquals("BAR", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", table.uniqueConstraintAt(1).columnNames().next()); + assertEquals(0, table.uniqueConstraintAt(2).columnNamesSize()); + + assertSourceContains("@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint})"); + } + + public void testRemoveUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.removeUniqueConstraint(1); + Iterator<UniqueConstraint> uniqueConstraints = table.uniqueConstraints(); + assertEquals("BAR", uniqueConstraints.next().columnNames().next()); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + assertSourceContains("@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + table.removeUniqueConstraint(0); + uniqueConstraints = table.uniqueConstraints(); + assertEquals("BAZ", uniqueConstraints.next().columnNames().next()); + assertFalse(uniqueConstraints.hasNext()); + assertSourceContains("@SecondaryTable(uniqueConstraints=@UniqueConstraint(columnNames={\"BAZ\"}))"); + + table.removeUniqueConstraint(0); + uniqueConstraints = table.uniqueConstraints(); + assertFalse(uniqueConstraints.hasNext()); + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testMoveUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable secondaryTable = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals("BAR", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals("BAZ", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertEquals(3, secondaryTable.uniqueConstraintsSize()); + + secondaryTable.moveUniqueConstraint(2, 0); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("BAZ", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertEquals(3, secondaryTable.uniqueConstraintsSize()); + assertSourceContains("@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"})})"); + } + + public void testMoveUniqueConstraint2() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable secondaryTable = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + assertEquals("BAR", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals("BAZ", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertEquals(3, secondaryTable.uniqueConstraintsSize()); + + secondaryTable.moveUniqueConstraint(0, 2); + assertEquals("BAZ", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertEquals(3, secondaryTable.uniqueConstraintsSize()); + assertSourceContains("@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"})})"); + } + + + public void testPkJoinColumns() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testPkJoinColumns2() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.addPkJoinColumn(0); + table.addPkJoinColumn(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testPkJoinColumns3() throws Exception { + IType testType = this.createTestSecondaryTableWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddPkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + + table.addPkJoinColumn(0).setName("FOO"); + table.addPkJoinColumn(0); + table.addPkJoinColumn(0).setName("BAR");//test adding a pkJoinColumn in front of 2 other joinColumns + + assertEquals("BAR", table.pkJoinColumnAt(0).getName()); + assertNull(table.pkJoinColumnAt(1).getName()); + assertEquals("FOO", table.pkJoinColumnAt(2).getName()); + + assertEquals(3, table.pkJoinColumnsSize()); + assertSourceContains("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"),@PrimaryKeyJoinColumn, @PrimaryKeyJoinColumn(name=\"FOO\")})"); + } + + public void testRemovePkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTableWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + table.removePkJoinColumn(1); + assertSourceContains("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"), @PrimaryKeyJoinColumn(name=\"BAZ\")})"); + + table.removePkJoinColumn(0); + assertSourceContains("@SecondaryTable(pkJoinColumns=@PrimaryKeyJoinColumn(name=\"BAZ\"))"); + + + table.removePkJoinColumn(0); + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testMovePkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTableWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setColumnDefinition("COLUMN_DEF"); + table.movePkJoinColumn(2, 0); + assertSourceContains("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\"), @PrimaryKeyJoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", columnDefinition = \"COLUMN_DEF\")})"); + } + + public void testMovePkJoinColumn2() throws Exception { + IType testType = this.createTestSecondaryTableWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setColumnDefinition("COLUMN_DEF"); + table.movePkJoinColumn(0, 2); + assertSourceContains("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAZ\"), @PrimaryKeyJoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", columnDefinition = \"COLUMN_DEF\"), @PrimaryKeyJoinColumn(name=\"FOO\")})"); + } + + public void testSetPkJoinColumnName() throws Exception { + IType testType = this.createTestSecondaryTableWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + assertEquals(3, CollectionTools.size(iterator)); + + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"foo\"), @PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\")})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTablesTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTablesTests.java new file mode 100644 index 0000000000..4e3440950a --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SecondaryTablesTests.java @@ -0,0 +1,550 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.PrimaryKeyJoinColumn; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTable; +import org.eclipse.jpt.core.internal.resource.java.SecondaryTables; +import org.eclipse.jpt.core.internal.resource.java.UniqueConstraint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class SecondaryTablesTests extends JavaResourceModelTestCase { + + private static final String TABLE_NAME = "MY_TABLE"; + private static final String SCHEMA_NAME = "MY_SCHEMA"; + private static final String CATALOG_NAME = "MY_CATALOG"; + + public SecondaryTablesTests(String name) { + super(name); + } + + + private void createUniqueConstraintAnnotation() throws Exception { + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + } + + private void createPrimaryKeyJoinColumnAnnotation() throws Exception { + this.createAnnotationAndMembers("PrimaryKeyJoinColumn", "String name() default \"\"; String referencedColumnName() default \"\";String columnDefinition() default \"\";"); + } + + private void createSecondaryTableAnnotation() throws Exception { + createPrimaryKeyJoinColumnAnnotation(); + createUniqueConstraintAnnotation(); + this.createAnnotationAndMembers("SecondaryTable", "String name() default \"\"; " + + "String catalog() default \"\"; " + + "String schema() default \"\";" + + "PrimaryKeyJoinColumn[] pkJoinColumns() default {};" + + "UniqueConstraint[] uniqueConstraints() default {};"); + + } + private void createSecondaryTablesAnnotation() throws Exception { + createSecondaryTableAnnotation(); + this.createAnnotationAndMembers("SecondaryTables", "SecondaryTable[] value()"); + } + + private IType createTestSecondaryTables() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable)"); + } + }); + } + + private IType createTestSecondaryTableWithName() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable(name=\"" + TABLE_NAME + "\"))"); + } + }); + } + + private IType createTestSecondaryTableWithSchema() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable(schema=\"" + SCHEMA_NAME + "\"))"); + } + }); + } + private IType createTestSecondaryTableWithCatalog() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable(catalog=\"" + CATALOG_NAME + "\"))"); + } + }); + } + + + private IType createTestSecondaryTableWithUniqueConstraints() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint}))"); + } + }); + } + + private IType createTestSecondaryTable() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.ENTITY, JPA.SECONDARY_TABLE, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Entity"); + sb.append(CR); + sb.append("@SecondaryTable(name=\"FOO\", catalog=\"BAR\", schema=\"BAZ\", uniqueConstraints=@UniqueConstraint(columnNames={\"BAR\"}))"); + } + }); + } + + private IType createTestSecondaryTablesWithPkJoinColumns() throws Exception { + createSecondaryTablesAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SECONDARY_TABLES, JPA.SECONDARY_TABLE, JPA.PRIMARY_KEY_JOIN_COLUMN); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"), @PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\")}))"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestSecondaryTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTable table = (SecondaryTable) typeResource.annotation(JPA.SECONDARY_TABLE); + assertNull(table); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + assertNotNull(secondaryTables); + + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertEquals(TABLE_NAME, secondaryTable.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertNull(secondaryTable.getName()); + assertNull(secondaryTable.getCatalog()); + assertNull(secondaryTable.getSchema()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + + assertNull(secondaryTable.getName()); + + secondaryTable.setName("Foo"); + assertEquals("Foo", secondaryTable.getName()); + + assertSourceContains("@SecondaryTables(@SecondaryTable(name=\"Foo\"))"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestSecondaryTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertEquals(TABLE_NAME, secondaryTable.getName()); + + secondaryTable.setName(null); + assertNull(secondaryTable.getName()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testGetCatalog() throws Exception { + IType testType = this.createTestSecondaryTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertEquals(CATALOG_NAME, secondaryTable.getCatalog()); + } + + public void testSetCatalog() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertNull(secondaryTable.getCatalog()); + + secondaryTable.setCatalog("Foo"); + assertEquals("Foo", secondaryTable.getCatalog()); + + assertSourceContains("@SecondaryTables(@SecondaryTable(catalog=\"Foo\"))"); + } + + public void testSetCatalogNull() throws Exception { + IType testType = this.createTestSecondaryTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertEquals(CATALOG_NAME, secondaryTable.getCatalog()); + + secondaryTable.setCatalog(null); + assertNull(secondaryTable.getCatalog()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testGetSchema() throws Exception { + IType testType = this.createTestSecondaryTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertNotNull(secondaryTable); + assertEquals(SCHEMA_NAME, secondaryTable.getSchema()); + } + + public void testSetSchema() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertNull(secondaryTable.getSchema()); + + secondaryTable.setSchema("Foo"); + assertEquals("Foo", secondaryTable.getSchema()); + + assertSourceContains("@SecondaryTables(@SecondaryTable(schema=\"Foo\"))"); + } + + public void testSetSchemaNull() throws Exception { + IType testType = this.createTestSecondaryTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + assertEquals(SCHEMA_NAME, secondaryTable.getSchema()); + + secondaryTable.setSchema(null); + assertNull(secondaryTable.getSchema()); + + assertSourceDoesNotContain("@SecondaryTable"); + } + + + public void testUniqueConstraints() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + + ListIterator<UniqueConstraint> iterator = secondaryTable.uniqueConstraints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints2() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + + secondaryTable.addUniqueConstraint(0); + secondaryTable.addUniqueConstraint(1); + secondaryTable.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<UniqueConstraint> iterator = secondaryTable.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints3() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + + ListIterator<UniqueConstraint> iterator = secondaryTable.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testAddUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + + secondaryTable.addUniqueConstraint(0).addColumnName("FOO"); + secondaryTable.addUniqueConstraint(1); + secondaryTable.addUniqueConstraint(0).addColumnName("BAR"); + + assertEquals("BAR", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals(0, secondaryTable.uniqueConstraintAt(2).columnNamesSize()); + + assertSourceContains("@SecondaryTables(@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint}))"); + } + + public void testRemoveUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + secondaryTable.addUniqueConstraint(0).addColumnName("FOO"); + + secondaryTable.removeUniqueConstraint(2); + assertSourceContains("@SecondaryTables(@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint(columnNames={\"BAR\"})})"); + + secondaryTable.removeUniqueConstraint(0); + assertSourceContains("@SecondaryTables(@SecondaryTable(uniqueConstraints=@UniqueConstraint(columnNames={\"BAR\"}))"); + + secondaryTable.removeUniqueConstraint(0); + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testMoveUniqueConstraint() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + secondaryTable.addUniqueConstraint(0).addColumnName("FOO"); + + assertEquals("FOO", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertFalse(secondaryTable.uniqueConstraintAt(2).columnNames().hasNext()); + + secondaryTable.moveUniqueConstraint(2, 0); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertFalse(secondaryTable.uniqueConstraintAt(1).columnNames().hasNext()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertSourceContains("@SecondaryTables(@SecondaryTable(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint, @UniqueConstraint(columnNames=\"FOO\")}))"); + } + + public void testMoveUniqueConstraint2() throws Exception { + IType testType = this.createTestSecondaryTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTables secondaryTables = (SecondaryTables) typeResource.annotation(JPA.SECONDARY_TABLES); + SecondaryTable secondaryTable = secondaryTables.nestedAnnotationAt(0); + secondaryTable.addUniqueConstraint(0).addColumnName("FOO"); + + assertEquals("FOO", secondaryTable.uniqueConstraintAt(0).columnNames().next()); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertFalse(secondaryTable.uniqueConstraintAt(2).columnNames().hasNext()); + + secondaryTable.moveUniqueConstraint(0, 2); + assertFalse(secondaryTable.uniqueConstraintAt(0).columnNames().hasNext()); + assertEquals("FOO", secondaryTable.uniqueConstraintAt(1).columnNames().next()); + assertEquals("BAR", secondaryTable.uniqueConstraintAt(2).columnNames().next()); + assertSourceContains("@SecondaryTables(@SecondaryTable(uniqueConstraints={@UniqueConstraint, @UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint(columnNames={\"BAR\"})}))"); + } + + + // @Entity -->> @Entity + // @SecondaryTable(name="FOO") @SecondaryTables({@SecondaryTable(name="FOO"), @SecondaryTable(name="BAR")}) + public void testAddSecondaryTableCopyExisting() throws Exception { + IType jdtType = createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTable = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\", catalog = \"BAR\", schema = \"BAZ\", uniqueConstraints = @UniqueConstraint(columnNames=\"BAR\")),@SecondaryTable(name=\"BAR\")})"); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(2, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testAddSecondaryTable() throws Exception { + IType jdtType = createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTable = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\", catalog = \"BAR\", schema = \"BAZ\", uniqueConstraints = @UniqueConstraint(columnNames=\"BAR\")),@SecondaryTable(name=\"BAR\")})"); + + secondaryTable = (SecondaryTable) typeResource.addAnnotation(0, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable.setName("BAZ"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"BAZ\"),@SecondaryTable(name=\"FOO\", catalog = \"BAR\", schema = \"BAZ\", uniqueConstraints = @UniqueConstraint(columnNames=\"BAR\")), @SecondaryTable(name=\"BAR\")})"); + + Iterator<JavaResource> secondaryTables = typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + assertEquals("BAZ", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("FOO", ((SecondaryTable) secondaryTables.next()).getName()); + assertEquals("BAR", ((SecondaryTable) secondaryTables.next()).getName()); + + assertNull(typeResource.annotation(JPA.SECONDARY_TABLE)); + assertNotNull(typeResource.annotation(JPA.SECONDARY_TABLES)); + assertEquals(3, CollectionTools.size(typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES))); + } + + public void testRemoveSecondaryTableCopyExisting() throws Exception { + IType jdtType = createTestSecondaryTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(jdtType); + + SecondaryTable secondaryTable = (SecondaryTable) typeResource.addAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + secondaryTable.setName("BAR"); + assertSourceContains("@SecondaryTables({@SecondaryTable(name=\"FOO\", catalog = \"BAR\", schema = \"BAZ\", uniqueConstraints = @UniqueConstraint(columnNames=\"BAR\")),@SecondaryTable(name=\"BAR\")})"); + + typeResource.removeAnnotation(1, JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES); + assertSourceContains("@SecondaryTable(name=\"FOO\", catalog = \"BAR\", schema = \"BAZ\", uniqueConstraints = @UniqueConstraint(columnNames=\"BAR\"))"); + } + + + public void testPkJoinColumns() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testPkJoinColumns2() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + table.addPkJoinColumn(0); + table.addPkJoinColumn(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testPkJoinColumns3() throws Exception { + IType testType = this.createTestSecondaryTablesWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddPkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTables(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + table.addPkJoinColumn(0).setName("FOO"); + table.addPkJoinColumn(1); + table.addPkJoinColumn(0).setName("BAR"); + + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"),@PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn}))"); + } + + public void testRemovePkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTablesWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + table.removePkJoinColumn(1); + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAR\"), @PrimaryKeyJoinColumn(name=\"BAZ\")}))"); + + table.removePkJoinColumn(0); + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns=@PrimaryKeyJoinColumn(name=\"BAZ\")))"); + + table.removePkJoinColumn(0); + assertSourceDoesNotContain("@SecondaryTable"); + } + + public void testMovePkJoinColumn() throws Exception { + IType testType = this.createTestSecondaryTablesWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setColumnDefinition("COLUMN_DEF"); + table.movePkJoinColumn(2, 0); + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\"), @PrimaryKeyJoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", columnDefinition = \"COLUMN_DEF\")}))"); + } + + public void testMovePkJoinColumn2() throws Exception { + IType testType = this.createTestSecondaryTablesWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumnAt(0); + joinColumn.setReferencedColumnName("REF_NAME"); + joinColumn.setColumnDefinition("COLUMN_DEF"); + table.movePkJoinColumn(0, 2); + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"BAZ\"), @PrimaryKeyJoinColumn(name=\"BAR\", referencedColumnName = \"REF_NAME\", columnDefinition = \"COLUMN_DEF\"), @PrimaryKeyJoinColumn(name=\"FOO\")}))"); + } + + public void testSetPkJoinColumnName() throws Exception { + IType testType = this.createTestSecondaryTablesWithPkJoinColumns(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + SecondaryTable table = (SecondaryTable) typeResource.annotations(JPA.SECONDARY_TABLE, JPA.SECONDARY_TABLES).next(); + + ListIterator<PrimaryKeyJoinColumn> iterator = table.pkJoinColumns(); + assertEquals(3, CollectionTools.size(iterator)); + + PrimaryKeyJoinColumn joinColumn = table.pkJoinColumns().next(); + + assertEquals("BAR", joinColumn.getName()); + + joinColumn.setName("foo"); + assertEquals("foo", joinColumn.getName()); + + assertSourceContains("@SecondaryTables(@SecondaryTable(pkJoinColumns={@PrimaryKeyJoinColumn(name=\"foo\"), @PrimaryKeyJoinColumn(name=\"FOO\"), @PrimaryKeyJoinColumn(name=\"BAZ\")}))"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SequenceGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SequenceGeneratorTests.java new file mode 100644 index 0000000000..1f48b1e14c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/SequenceGeneratorTests.java @@ -0,0 +1,242 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.SequenceGenerator; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class SequenceGeneratorTests extends JavaResourceModelTestCase { + + private static final String GENERATOR_NAME = "MY_GENERATOR"; + private static final String GENERATOR_SEQUENCE_NAME = "MY_SEQUENCE"; + private static final Integer GENERATOR_ALLOCATION_SIZE = Integer.valueOf(5); + private static final Integer GENERATOR_INITIAL_VALUE = Integer.valueOf(5); + + public SequenceGeneratorTests(String name) { + super(name); + } + + private void createSequenceGeneratorAnnotation() throws Exception { + this.createAnnotationAndMembers("SequenceGenerator", "String name(); " + + "String sequenceName() default \"\"" + + "int initialValue() default 1" + + "int allocationSize() default 50"); + } + + private IType createTestSequenceGeneratorOnField() throws Exception { + createSequenceGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SEQUENCE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@SequenceGenerator"); + } + }); + } + + private IType createTestSequenceGeneratorOnType() throws Exception { + createSequenceGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SEQUENCE_GENERATOR); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@SequenceGenerator"); + } + }); + } + + private IType createTestSequenceGeneratorWithName() throws Exception { + return createTestSequenceGeneratorWithStringElement("name", GENERATOR_NAME); + } + + private IType createTestSequenceGeneratorWithSequenceName() throws Exception { + return createTestSequenceGeneratorWithStringElement("sequenceName", GENERATOR_SEQUENCE_NAME); + } + + private IType createTestSequenceGeneratorWithStringElement(final String elementName, final String value) throws Exception { + createSequenceGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SEQUENCE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@SequenceGenerator(" + elementName + "=\"" + value + "\")"); + } + }); + } + + private IType createTestSequenceGeneratorWithAllocationSize() throws Exception { + return createTestSequenceGeneratorWithIntElement("allocationSize", GENERATOR_ALLOCATION_SIZE); + } + + private IType createTestSequenceGeneratorWithInitialValue() throws Exception { + return createTestSequenceGeneratorWithIntElement("initialValue", GENERATOR_INITIAL_VALUE); + } + + private IType createTestSequenceGeneratorWithIntElement(final String elementName, final int value) throws Exception { + createSequenceGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.SEQUENCE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@SequenceGenerator(" + elementName + "=" + value + ")"); + } + }); + } + + public void testSequenceGeneratorOnField() throws Exception { + IType testType = this.createTestSequenceGeneratorOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertNotNull(sequenceGenerator); + } + + public void testSequenceGeneratorOnType() throws Exception { + IType testType = this.createTestSequenceGeneratorOnType(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) typeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertNotNull(sequenceGenerator); + } + + public void testGetName() throws Exception { + IType testType = this.createTestSequenceGeneratorWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_NAME, sequenceGenerator.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestSequenceGeneratorWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_NAME, sequenceGenerator.getName()); + + sequenceGenerator.setName("foo"); + assertEquals("foo", sequenceGenerator.getName()); + + assertSourceContains("@SequenceGenerator(name=\"foo\")"); + + sequenceGenerator.setName(null); + assertNull(sequenceGenerator.getName()); + + assertSourceDoesNotContain("@SequenceGenerator"); + } + + public void testGetSequenceName() throws Exception { + IType testType = this.createTestSequenceGeneratorWithSequenceName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_SEQUENCE_NAME, sequenceGenerator.getSequenceName()); + } + + public void testSetSequenceName() throws Exception { + IType testType = this.createTestSequenceGeneratorWithSequenceName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_SEQUENCE_NAME, sequenceGenerator.getSequenceName()); + + sequenceGenerator.setSequenceName("foo"); + assertEquals("foo", sequenceGenerator.getSequenceName()); + + assertSourceContains("@SequenceGenerator(sequenceName=\"foo\")"); + + sequenceGenerator.setSequenceName(null); + assertNull(sequenceGenerator.getSequenceName()); + + assertSourceDoesNotContain("@SequenceGenerator"); + } + + public void testGetAllocationSize() throws Exception { + IType testType = this.createTestSequenceGeneratorWithAllocationSize(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_ALLOCATION_SIZE, sequenceGenerator.getAllocationSize()); + } + + public void testSetAllocationSize() throws Exception { + IType testType = this.createTestSequenceGeneratorWithAllocationSize(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_ALLOCATION_SIZE, sequenceGenerator.getAllocationSize()); + + sequenceGenerator.setAllocationSize(Integer.valueOf(500)); + assertEquals(Integer.valueOf(500), sequenceGenerator.getAllocationSize()); + + assertSourceContains("@SequenceGenerator(allocationSize=500)"); + + sequenceGenerator.setAllocationSize(null); + + assertSourceDoesNotContain("@SequenceGenerator"); + + sequenceGenerator.setAllocationSize(Integer.valueOf(0)); + assertSourceContains("@SequenceGenerator(allocationSize=0)"); + } + + public void testGetInitialValue() throws Exception { + IType testType = this.createTestSequenceGeneratorWithInitialValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_INITIAL_VALUE, sequenceGenerator.getInitialValue()); + } + + public void testSetInitialValue() throws Exception { + IType testType = this.createTestSequenceGeneratorWithInitialValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + SequenceGenerator sequenceGenerator = (SequenceGenerator) attributeResource.annotation(JPA.SEQUENCE_GENERATOR); + assertEquals(GENERATOR_INITIAL_VALUE, sequenceGenerator.getInitialValue()); + + sequenceGenerator.setInitialValue(Integer.valueOf(500)); + assertEquals(Integer.valueOf(500), sequenceGenerator.getInitialValue()); + + assertSourceContains("@SequenceGenerator(initialValue=500)"); + + sequenceGenerator.setInitialValue(null); + + assertSourceDoesNotContain("@SequenceGenerator"); + + sequenceGenerator.setInitialValue(Integer.valueOf(0)); + assertSourceContains("@SequenceGenerator(initialValue=0)"); + } +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableGeneratorTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableGeneratorTests.java new file mode 100644 index 0000000000..53d5c2e58c --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableGeneratorTests.java @@ -0,0 +1,532 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.TableGenerator; +import org.eclipse.jpt.core.internal.resource.java.UniqueConstraint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class TableGeneratorTests extends JavaResourceModelTestCase { + + private static final String GENERATOR_NAME = "MY_GENERATOR"; + private static final String GENERATOR_TABLE = "MY_TABLE"; + private static final String GENERATOR_CATALOG = "MY_CATALOG"; + private static final String GENERATOR_SCHEMA = "MY_SCHEMA"; + private static final String GENERATOR_PK_COLUMN_NAME = "MY_PK_COLUMN_NAME"; + private static final String GENERATOR_VALUE_COLUMN_NAME = "MY_VALUE_COLUMN_NAME"; + private static final String GENERATOR_PK_COLUMN_VALUE = "MY_PK_COLUMN_VALUE"; + private static final Integer GENERATOR_ALLOCATION_SIZE = Integer.valueOf(5); + private static final Integer GENERATOR_INITIAL_VALUE = Integer.valueOf(5); + + public TableGeneratorTests(String name) { + super(name); + } + + private void createTableGeneratorAnnotation() throws Exception { + this.createAnnotationAndMembers("TableGenerator", "String name(); " + + "String table() default \"\"" + + "String catalog() default \"\"" + + "String schema() default \"\"" + + "String pkColumnName() default \"\"" + + "String valueColumnName() default \"\"" + + "String pkColumnValue() default \"\"" + + "int initialValue() default 0" + + "int allocationSize() default 50" + + "UniqueConstraint[] uniqueConstraints() default{}"); + } + + private IType createTestTableGeneratorOnField() throws Exception { + createTableGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@TableGenerator"); + } + }); + } + + private IType createTestTableGeneratorOnType() throws Exception { + createTableGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE_GENERATOR); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@TableGenerator"); + } + }); + } + + private IType createTestTableGeneratorWithName() throws Exception { + return createTestTableGeneratorWithStringElement("name", GENERATOR_NAME); + } + + private IType createTestTableGeneratorWithTable() throws Exception { + return createTestTableGeneratorWithStringElement("table", GENERATOR_TABLE); + } + + private IType createTestTableGeneratorWithCatalog() throws Exception { + return createTestTableGeneratorWithStringElement("catalog", GENERATOR_CATALOG); + } + + private IType createTestTableGeneratorWithSchema() throws Exception { + return createTestTableGeneratorWithStringElement("schema", GENERATOR_SCHEMA); + } + private IType createTestTableGeneratorWithPkColumnName() throws Exception { + return createTestTableGeneratorWithStringElement("pkColumnName", GENERATOR_PK_COLUMN_NAME); + } + + private IType createTestTableGeneratorWithValueColumnName() throws Exception { + return createTestTableGeneratorWithStringElement("valueColumnName", GENERATOR_VALUE_COLUMN_NAME); + } + + private IType createTestTableGeneratorWithPkColumnValue() throws Exception { + return createTestTableGeneratorWithStringElement("pkColumnValue", GENERATOR_PK_COLUMN_VALUE); + } + + private IType createTestTableGeneratorWithStringElement(final String elementName, final String value) throws Exception { + createTableGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@TableGenerator(" + elementName + "=\"" + value + "\")"); + } + }); + } + + private IType createTestTableGeneratorWithAllocationSize() throws Exception { + return createTestTableGeneratorWithIntElement("allocationSize", GENERATOR_ALLOCATION_SIZE); + } + + private IType createTestTableGeneratorWithInitialValue() throws Exception { + return createTestTableGeneratorWithIntElement("initialValue", GENERATOR_INITIAL_VALUE); + } + + private IType createTestTableGeneratorWithIntElement(final String elementName, final int value) throws Exception { + createTableGeneratorAnnotation(); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE_GENERATOR); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@TableGenerator(" + elementName + "=" + value + ")"); + } + }); + } + + private IType createTestTableGeneratorWithUniqueConstraints() throws Exception { + createTableGeneratorAnnotation(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE_GENERATOR, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@TableGenerator(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + } + }); + } + + public void testTableGeneratorOnField() throws Exception { + IType testType = this.createTestTableGeneratorOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertNotNull(tableGenerator); + } + + public void testTableGeneratorOnType() throws Exception { + IType testType = this.createTestTableGeneratorOnType(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + TableGenerator tableGenerator = (TableGenerator) typeResource.annotation(JPA.TABLE_GENERATOR); + assertNotNull(tableGenerator); + } + + public void testGetName() throws Exception { + IType testType = this.createTestTableGeneratorWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_NAME, tableGenerator.getName()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestTableGeneratorWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_NAME, tableGenerator.getName()); + + tableGenerator.setName("foo"); + assertEquals("foo", tableGenerator.getName()); + + assertSourceContains("@TableGenerator(name=\"foo\")"); + + tableGenerator.setName(null); + assertNull(tableGenerator.getName()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetTable() throws Exception { + IType testType = this.createTestTableGeneratorWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_TABLE, tableGenerator.getTable()); + } + + public void testSetTable() throws Exception { + IType testType = this.createTestTableGeneratorWithTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_TABLE, tableGenerator.getTable()); + + tableGenerator.setTable("foo"); + assertEquals("foo", tableGenerator.getTable()); + + assertSourceContains("@TableGenerator(table=\"foo\")"); + + tableGenerator.setTable(null); + assertNull(tableGenerator.getTable()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetCatalog() throws Exception { + IType testType = this.createTestTableGeneratorWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_CATALOG, tableGenerator.getCatalog()); + } + + public void testSetCatalog() throws Exception { + IType testType = this.createTestTableGeneratorWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_CATALOG, tableGenerator.getCatalog()); + + tableGenerator.setCatalog("foo"); + assertEquals("foo", tableGenerator.getCatalog()); + + assertSourceContains("@TableGenerator(catalog=\"foo\")"); + + tableGenerator.setCatalog(null); + assertNull(tableGenerator.getCatalog()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetSchema() throws Exception { + IType testType = this.createTestTableGeneratorWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_SCHEMA, tableGenerator.getSchema()); + } + + public void testSetSchema() throws Exception { + IType testType = this.createTestTableGeneratorWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_SCHEMA, tableGenerator.getSchema()); + + tableGenerator.setSchema("foo"); + assertEquals("foo", tableGenerator.getSchema()); + + assertSourceContains("@TableGenerator(schema=\"foo\")"); + + tableGenerator.setSchema(null); + assertNull(tableGenerator.getSchema()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetPkColumnName() throws Exception { + IType testType = this.createTestTableGeneratorWithPkColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_PK_COLUMN_NAME, tableGenerator.getPkColumnName()); + } + + public void testSetPkColumnName() throws Exception { + IType testType = this.createTestTableGeneratorWithPkColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_PK_COLUMN_NAME, tableGenerator.getPkColumnName()); + + tableGenerator.setPkColumnName("foo"); + assertEquals("foo", tableGenerator.getPkColumnName()); + + assertSourceContains("@TableGenerator(pkColumnName=\"foo\")"); + + tableGenerator.setPkColumnName(null); + assertNull(tableGenerator.getPkColumnName()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetValueColumnName() throws Exception { + IType testType = this.createTestTableGeneratorWithValueColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_VALUE_COLUMN_NAME, tableGenerator.getValueColumnName()); + } + + public void testSetValueColumnName() throws Exception { + IType testType = this.createTestTableGeneratorWithValueColumnName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_VALUE_COLUMN_NAME, tableGenerator.getValueColumnName()); + + tableGenerator.setValueColumnName("foo"); + assertEquals("foo", tableGenerator.getValueColumnName()); + + assertSourceContains("@TableGenerator(valueColumnName=\"foo\")"); + + tableGenerator.setValueColumnName(null); + assertNull(tableGenerator.getValueColumnName()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetPkColumnValue() throws Exception { + IType testType = this.createTestTableGeneratorWithPkColumnValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_PK_COLUMN_VALUE, tableGenerator.getPkColumnValue()); + } + + public void testSetPkColumnValue() throws Exception { + IType testType = this.createTestTableGeneratorWithPkColumnValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_PK_COLUMN_VALUE, tableGenerator.getPkColumnValue()); + + tableGenerator.setPkColumnValue("foo"); + assertEquals("foo", tableGenerator.getPkColumnValue()); + + assertSourceContains("@TableGenerator(pkColumnValue=\"foo\")"); + + tableGenerator.setPkColumnValue(null); + assertNull(tableGenerator.getPkColumnValue()); + + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testGetAllocationSize() throws Exception { + IType testType = this.createTestTableGeneratorWithAllocationSize(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_ALLOCATION_SIZE, tableGenerator.getAllocationSize()); + } + + public void testSetAllocationSize() throws Exception { + IType testType = this.createTestTableGeneratorWithAllocationSize(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_ALLOCATION_SIZE, tableGenerator.getAllocationSize()); + + tableGenerator.setAllocationSize(Integer.valueOf(500)); + assertEquals(Integer.valueOf(500), tableGenerator.getAllocationSize()); + + assertSourceContains("@TableGenerator(allocationSize=500)"); + + tableGenerator.setAllocationSize(null); + + assertSourceDoesNotContain("@TableGenerator"); + + tableGenerator.setAllocationSize(Integer.valueOf(0)); + assertSourceContains("@TableGenerator(allocationSize=0)"); + } + + public void testGetInitialValue() throws Exception { + IType testType = this.createTestTableGeneratorWithInitialValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_INITIAL_VALUE, tableGenerator.getInitialValue()); + } + + public void testSetInitialValue() throws Exception { + IType testType = this.createTestTableGeneratorWithInitialValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + assertEquals(GENERATOR_INITIAL_VALUE, tableGenerator.getInitialValue()); + + tableGenerator.setInitialValue(Integer.valueOf(500)); + assertEquals(Integer.valueOf(500), tableGenerator.getInitialValue()); + + assertSourceContains("@TableGenerator(initialValue=500)"); + + tableGenerator.setInitialValue(null); + + assertSourceDoesNotContain("@TableGenerator"); + + tableGenerator.setInitialValue(Integer.valueOf(0)); + assertSourceContains("@TableGenerator(initialValue=0)"); + } + + public void testUniqueConstraints() throws Exception { + IType testType = this.createTestTableGeneratorOnField(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + ListIterator<UniqueConstraint> iterator = tableGenerator.uniqueConstraints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints2() throws Exception { + IType testType = this.createTestTableGeneratorOnField(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.addUniqueConstraint(0); + tableGenerator.addUniqueConstraint(1); + tableGenerator.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<UniqueConstraint> iterator = tableGenerator.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints3() throws Exception { + IType testType = this.createTestTableGeneratorWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + ListIterator<UniqueConstraint> iterator = tableGenerator.uniqueConstraints(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddUniqueConstraint() throws Exception { + IType testType = this.createTestTableGeneratorOnField(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.addUniqueConstraint(0).addColumnName("FOO"); + tableGenerator.addUniqueConstraint(1); + tableGenerator.addUniqueConstraint(0).addColumnName("BAR"); + + assertEquals("BAR", tableGenerator.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", tableGenerator.uniqueConstraintAt(1).columnNames().next()); + assertEquals(0, tableGenerator.uniqueConstraintAt(2).columnNamesSize()); + + assertEquals(3, tableGenerator.uniqueConstraintsSize()); + assertSourceContains("@TableGenerator(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint})"); + } + + public void testRemoveUniqueConstraint() throws Exception { + IType testType = this.createTestTableGeneratorWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.removeUniqueConstraint(1); + assertSourceContains("@TableGenerator(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + tableGenerator.removeUniqueConstraint(0); + assertSourceContains("@TableGenerator(uniqueConstraints=@UniqueConstraint(columnNames={\"BAZ\"}))"); + + tableGenerator.removeUniqueConstraint(0); + assertSourceDoesNotContain("@TableGenerator"); + } + + public void testMoveUniqueConstraint() throws Exception { + IType testType = this.createTestTableGeneratorWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.moveUniqueConstraint(2, 0); + assertSourceContains("@TableGenerator(uniqueConstraints={@UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"})})"); + } + + public void testMoveUniqueConstraint2() throws Exception { + IType testType = this.createTestTableGeneratorWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + TableGenerator tableGenerator = (TableGenerator) attributeResource.annotation(JPA.TABLE_GENERATOR); + + tableGenerator.moveUniqueConstraint(0, 2); + assertSourceContains("@TableGenerator(uniqueConstraints={@UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"})})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableTests.java new file mode 100644 index 0000000000..3e51271feb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TableTests.java @@ -0,0 +1,358 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.jdtutility.JDTTools; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.Table; +import org.eclipse.jpt.core.internal.resource.java.UniqueConstraint; +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class TableTests extends JavaResourceModelTestCase { + + private static final String TABLE_NAME = "MY_TABLE"; + private static final String SCHEMA_NAME = "MY_SCHEMA"; + private static final String CATALOG_NAME = "MY_CATALOG"; + + public TableTests(String name) { + super(name); + } + + private IType createTestTable() throws Exception { + this.createAnnotationAndMembers("Table", "String name() default \"\"; String catalog() default \"\"; String schema() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Table"); + } + }); + } + + private IType createTestTableWithName() throws Exception { + this.createAnnotationAndMembers("Table", "String name() default \"\"; String catalog() default \"\"; String schema() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Table(name=\"" + TABLE_NAME + "\")"); + } + }); + } + + private IType createTestTableWithSchema() throws Exception { + this.createAnnotationAndMembers("Table", "String name() default \"\"; String catalog() default \"\"; String schema() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Table(schema=\"" + SCHEMA_NAME + "\")"); + } + }); + } + private IType createTestTableWithCatalog() throws Exception { + this.createAnnotationAndMembers("Table", "String name() default \"\"; String catalog() default \"\"; String schema() default \"\";"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Table(catalog=\"" + CATALOG_NAME + "\")"); + } + }); + } + + private IType createTestTableWithUniqueConstraints() throws Exception { + this.createAnnotationAndMembers("Table", "UniqueConstraint[] uniqueConstraints() default{}"); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TABLE, JPA.UNIQUE_CONSTRAINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@Table(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + } + }); + } + + public void testGetName() throws Exception { + IType testType = this.createTestTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertEquals(TABLE_NAME, table.getName()); + } + + public void testGetNull() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertNull(table.getName()); + assertNull(table.getCatalog()); + assertNull(table.getSchema()); + } + + public void testSetName() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertNull(table.getName()); + + table.setName("Foo"); + assertEquals("Foo", table.getName()); + + assertSourceContains("@Table(name=\"Foo\")"); + } + + public void testSetNameNull() throws Exception { + IType testType = this.createTestTableWithName(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertEquals(TABLE_NAME, table.getName()); + + table.setName(null); + assertNull(table.getName()); + + assertSourceDoesNotContain("@Table"); + } + + public void testGetCatalog() throws Exception { + IType testType = this.createTestTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertEquals(CATALOG_NAME, table.getCatalog()); + } + + public void testSetCatalog() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertNull(table.getCatalog()); + + table.setCatalog("Foo"); + assertEquals("Foo", table.getCatalog()); + + assertSourceContains("@Table(catalog=\"Foo\")"); + } + + public void testSetCatalogNull() throws Exception { + IType testType = this.createTestTableWithCatalog(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertEquals(CATALOG_NAME, table.getCatalog()); + + table.setCatalog(null); + assertNull(table.getCatalog()); + + assertSourceDoesNotContain("@Table"); + } + + public void testGetSchema() throws Exception { + IType testType = this.createTestTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertEquals(SCHEMA_NAME, table.getSchema()); + } + + public void testSetSchema() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertNotNull(table); + assertNull(table.getSchema()); + + table.setSchema("Foo"); + assertEquals("Foo", table.getSchema()); + + assertSourceContains("@Table(schema=\"Foo\")"); + } + + public void testSetSchemaNull() throws Exception { + IType testType = this.createTestTableWithSchema(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + + Table table = (Table) typeResource.annotation(JPA.TABLE); + assertEquals(SCHEMA_NAME, table.getSchema()); + + table.setSchema(null); + assertNull(table.getSchema()); + + assertSourceDoesNotContain("@Table"); + } + + public void testUniqueConstraints() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(0, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints2() throws Exception { + IType testType = this.createTestTable(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.addUniqueConstraint(0); + table.addUniqueConstraint(1); + table.updateFromJava(JDTTools.buildASTRoot(testType)); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(2, CollectionTools.size(iterator)); + } + + public void testUniqueConstraints3() throws Exception { + IType testType = this.createTestTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + ListIterator<UniqueConstraint> iterator = table.uniqueConstraints(); + + assertEquals(3, CollectionTools.size(iterator)); + } + + public void testAddUniqueConstraint() throws Exception { + IType testType = this.createTestTable(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(1); + table.addUniqueConstraint(0).addColumnName("BAR"); + + assertEquals("BAR", table.uniqueConstraintAt(0).columnNames().next()); + assertEquals("FOO", table.uniqueConstraintAt(1).columnNames().next()); + assertEquals(0, table.uniqueConstraintAt(2).columnNamesSize()); + + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"FOO\"), @UniqueConstraint})"); + } + + public void testAddUniqueConstraint2() throws Exception { + IType testType = this.createTestTable(); + this.createAnnotationAndMembers("UniqueConstraint", "String[] columnNames();"); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(0); + table.addUniqueConstraint(1).addColumnName("BAR"); + table.uniqueConstraintAt(1).addColumnName("BAZ"); + + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint,@UniqueConstraint(columnNames={ \"BAR\", \"BAZ\" }), @UniqueConstraint(columnNames=\"FOO\")})"); + } + public void testRemoveUniqueConstraint() throws Exception { + IType testType = this.createTestTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.removeUniqueConstraint(1); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"BAZ\"})})"); + + table.removeUniqueConstraint(0); + assertSourceContains("@Table(uniqueConstraints=@UniqueConstraint(columnNames={\"BAZ\"}))"); + + table.removeUniqueConstraint(0); + assertSourceDoesNotContain("@Table"); + } + + public void testRemoveUniqueConstraint2() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(1).addColumnName("BAR"); + table.addUniqueConstraint(2).addColumnName("BAZ"); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames=\"FOO\"),@UniqueConstraint(columnNames=\"BAR\"), @UniqueConstraint(columnNames=\"BAZ\")})"); + + table.removeUniqueConstraint(0); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames=\"BAR\"),@UniqueConstraint(columnNames=\"BAZ\")})"); + + table.removeUniqueConstraint(0); + assertSourceContains("@Table(uniqueConstraints=@UniqueConstraint(columnNames=\"BAZ\"))"); + + table.removeUniqueConstraint(0); + assertSourceDoesNotContain("@Table"); + } + + public void testRemoveUniqueConstraint3() throws Exception { + IType testType = this.createTestTable(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + table.addUniqueConstraint(0).addColumnName("FOO"); + table.addUniqueConstraint(1).addColumnName("BAR"); + table.addUniqueConstraint(2).addColumnName("BAZ"); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames=\"FOO\"),@UniqueConstraint(columnNames=\"BAR\"), @UniqueConstraint(columnNames=\"BAZ\")})"); + + table.removeUniqueConstraint(2); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames=\"FOO\"),@UniqueConstraint(columnNames=\"BAR\")})"); + + table.removeUniqueConstraint(1); + assertSourceContains("@Table(uniqueConstraints=@UniqueConstraint(columnNames=\"FOO\"))"); + + table.removeUniqueConstraint(0); + assertSourceDoesNotContain("@Table"); + } + + public void testMoveUniqueConstraint() throws Exception { + IType testType = this.createTestTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.moveUniqueConstraint(2, 0); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames={\"FOO\"}), @UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"})})"); + } + + public void testMoveUniqueConstraint2() throws Exception { + IType testType = this.createTestTableWithUniqueConstraints(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + Table table = (Table) typeResource.annotation(JPA.TABLE); + + table.moveUniqueConstraint(0, 2); + assertSourceContains("@Table(uniqueConstraints={@UniqueConstraint(columnNames={\"BAZ\"}), @UniqueConstraint(columnNames={\"BAR\"}), @UniqueConstraint(columnNames={\"FOO\"})})"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TemporalTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TemporalTests.java new file mode 100644 index 0000000000..9ea3455421 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TemporalTests.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.Temporal; +import org.eclipse.jpt.core.internal.resource.java.TemporalType; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class TemporalTests extends JavaResourceModelTestCase { + + public TemporalTests(String name) { + super(name); + } + + private IType createTestTemporal() throws Exception { + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + this.createEnumAndMembers("TemporalType", "DATE, TIME, TIMESTAMP"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TEMPORAL); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Temporal"); + } + }); + } + + private IType createTestTemporalWithValue() throws Exception { + this.createAnnotationAndMembers("Temporal", "TemporalType value();"); + this.createEnumAndMembers("TemporalType", "DATE, TIME, TIMESTAMP"); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TEMPORAL, JPA.TEMPORAL_TYPE); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Temporal(TemporalType.DATE)"); + } + }); + } + + public void testTemporal() throws Exception { + IType testType = this.createTestTemporal(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Temporal temporal = (Temporal) attributeResource.annotation(JPA.TEMPORAL); + assertNotNull(temporal); + } + + public void testGetValue() throws Exception { + IType testType = this.createTestTemporalWithValue(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Temporal temporal = (Temporal) attributeResource.annotation(JPA.TEMPORAL); + assertEquals(TemporalType.DATE, temporal.getValue()); + } + + public void testSetValue() throws Exception { + IType testType = this.createTestTemporal(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + Temporal temporal = (Temporal) attributeResource.annotation(JPA.TEMPORAL); + + temporal.setValue(TemporalType.TIME); + + assertSourceContains("@Temporal(TIME)"); + + temporal.setValue(null); + + assertSourceDoesNotContain("@Temporal"); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TransientTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TransientTests.java new file mode 100644 index 0000000000..0e5472cb4d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/TransientTests.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.Transient; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class TransientTests extends JavaResourceModelTestCase { + + public TransientTests(String name) { + super(name); + } + + private IType createTestTransient() throws Exception { + this.createAnnotationAndMembers("Transient", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.TRANSIENT); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Transient"); + } + }); + } + + public void testTransient() throws Exception { + IType testType = this.createTestTransient(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JavaResource mappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Transient); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/VersionTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/VersionTests.java new file mode 100644 index 0000000000..c7ec8a9d29 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/resource/java/VersionTests.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2007 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.core.tests.internal.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.IType; +import org.eclipse.jpt.core.internal.resource.java.JPA; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentAttributeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource; +import org.eclipse.jpt.core.internal.resource.java.JavaResource; +import org.eclipse.jpt.core.internal.resource.java.Version; +import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; + +public class VersionTests extends JavaResourceModelTestCase { + + public VersionTests(String name) { + super(name); + } + + private IType createTestVersion() throws Exception { + this.createAnnotationAndMembers("Version", ""); + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator<String> imports() { + return new ArrayIterator<String>(JPA.VERSION); + } + @Override + public void appendIdFieldAnnotationTo(StringBuilder sb) { + sb.append("@Version"); + } + }); + } + + public void testVersion() throws Exception { + IType testType = this.createTestVersion(); + JavaPersistentTypeResource typeResource = buildJavaTypeResource(testType); + JavaPersistentAttributeResource attributeResource = typeResource.fields().next(); + + JavaResource mappingAnnotation = attributeResource.mappingAnnotation(); + assertTrue(mappingAnnotation instanceof Version); + } +} |