Skip to main content
summaryrefslogtreecommitdiffstats
path: root/jpa/tests
diff options
context:
space:
mode:
authorpfullbright2007-04-25 20:43:59 +0000
committerpfullbright2007-04-25 20:43:59 +0000
commite0d72e09133326de640470b8ceb90238b5948984 (patch)
tree6327ea5f3d10873e56906e642427a955d637530f /jpa/tests
parentfb5519ce98bfc917d4f3eec2668a3c74f07f49eb (diff)
downloadwebtools.dali-e0d72e09133326de640470b8ceb90238b5948984.tar.gz
webtools.dali-e0d72e09133326de640470b8ceb90238b5948984.tar.xz
webtools.dali-e0d72e09133326de640470b8ceb90238b5948984.zip
renamed
Diffstat (limited to 'jpa/tests')
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/.classpath12
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/.cvsignore1
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/.project28
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF16
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/build.properties16
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/plugin.properties22
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JpaCoreTests.java36
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/ProjectUtility.java81
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JavaEntityTests.java42
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaCoreContentJavaMappingsTests.java28
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaJavaTestCase.java52
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java429
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/CombinationIndexedDeclarationAnnotationAdapterTests.java728
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JDTToolsTests.java164
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JpaCoreJdtUtilityTests.java34
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java542
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedDeclarationAnnotationAdapterTests.java763
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedIndexedDeclarationAnnotationAdapterTests.java2209
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/SimpleDeclarationAnnotationAdapterTests.java204
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java67
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaCoreModelTests.java27
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/ModelInitializationTests.java90
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestFacetedProject.java48
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJavaProject.java148
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java60
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestPlatformProject.java195
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/test.xml44
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/.classpath7
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/.cvsignore1
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/.project28
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/META-INF/MANIFEST.MF16
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/build.properties5
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/config/derby101.properties20
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/config/oracle10g.properties19
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/config/oracle9i.properties19
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/config/sqlserver2005.properties20
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/plugin.properties15
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/DbTestPlugin.java54
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JpaDbTests.java39
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/AllPlatformTests.java42
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java385
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Derby101Tests.java64
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java60
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle9iTests.java60
-rw-r--r--jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SQLServer2005Tests.java64
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/.classpath7
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/.cvsignore1
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/.project28
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/META-INF/MANIFEST.MF10
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/build.properties15
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/plugin.properties24
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/BitToolsTests.java59
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClassToolsTests.java548
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClasspathTests.java289
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CollectionToolsTests.java1972
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/FileToolsTests.java594
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/HashBagTests.java448
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/IndentingPrintWriterTests.java107
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JDBCTypeTests.java66
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JavaTypeTests.java250
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JpaUtilityTests.java49
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/NameToolsTests.java214
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/RangeTests.java74
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ReverseComparatorTests.java101
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/StringToolsTests.java698
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/TestTools.java156
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/XMLStringEncoderTests.java135
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayIteratorTests.java126
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayListIteratorTests.java150
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ChainIteratorTests.java119
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneIteratorTests.java252
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneListIteratorTests.java408
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeIteratorTests.java333
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeListIteratorTests.java403
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyEnumerationTests.java53
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyIteratorTests.java63
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyListIteratorTests.java127
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EnumerationIteratorTests.java119
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/FilteringIteratorTests.java285
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/GraphIteratorTests.java183
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/IteratorEnumerationTests.java98
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/JpaUtilityIteratorsTests.java53
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/PeekableIteratorTests.java140
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyIteratorTests.java118
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyListIteratorTests.java203
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementIteratorTests.java71
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementListIteratorTests.java111
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationIteratorTests.java216
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationListIteratorTests.java306
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TreeIteratorTests.java197
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/test.xml44
91 files changed, 17027 insertions, 0 deletions
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/.classpath b/jpa/tests/org.eclipse.jpt.core.tests/.classpath
new file mode 100644
index 0000000000..c8e5756d0f
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/.classpath
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
+ <accessrules>
+ <accessrule kind="accessible" pattern="org/eclipse/wst/**"/>
+ <accessrule kind="accessible" pattern="org/eclipse/jst/**"/>
+ </accessrules>
+ </classpathentry>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/.cvsignore b/jpa/tests/org.eclipse.jpt.core.tests/.cvsignore
new file mode 100644
index 0000000000..ba077a4031
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/.cvsignore
@@ -0,0 +1 @@
+bin
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/.project b/jpa/tests/org.eclipse.jpt.core.tests/.project
new file mode 100644
index 0000000000..08395a8e86
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.jpt.core.tests</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
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
new file mode 100644
index 0000000000..f03a1152e8
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/META-INF/MANIFEST.MF
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %pluginName
+Bundle-Vendor: %providerName
+Bundle-SymbolicName: org.eclipse.jpt.core.tests
+Bundle-Version: 1.0.0
+Bundle-Localization: plugin
+Require-Bundle: org.eclipse.core.resources,
+ org.eclipse.core.runtime,
+ org.eclipse.jdt.core,
+ org.eclipse.jpt.core,
+ org.eclipse.jpt.utility,
+ org.eclipse.wst.common.project.facet.core,
+ org.junit,
+ org.eclipse.emf.ecore
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/build.properties b/jpa/tests/org.eclipse.jpt.core.tests/build.properties
new file mode 100644
index 0000000000..6b79e8c08b
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/build.properties
@@ -0,0 +1,16 @@
+###############################################################################
+# 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
+###############################################################################
+javacSource = 1.5
+javacTarget = 1.5
+bin.includes = .,\
+ META-INF/,\
+ test.xml,\
+ plugin.properties
+source.. = src/
+output.. = bin/
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/plugin.properties b/jpa/tests/org.eclipse.jpt.core.tests/plugin.properties
new file mode 100644
index 0000000000..dd12574fb7
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/plugin.properties
@@ -0,0 +1,22 @@
+###############################################################################
+# Copyright (c) 2006 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
+###############################################################################
+# ====================================================================
+# To code developer:
+# Do NOT change the properties between this line and the
+# "%%% END OF TRANSLATED PROPERTIES %%%" line.
+# Make a new property name, append to the end of the file and change
+# the code to use the new property.
+# ====================================================================
+
+# ====================================================================
+# %%% END OF TRANSLATED PROPERTIES %%%
+# ====================================================================
+
+pluginName=Java Persistence API Core Tests
+providerName=Eclipse.org
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JpaCoreTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JpaCoreTests.java
new file mode 100644
index 0000000000..36d43bbcdb
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/JpaCoreTests.java
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * 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;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.eclipse.jpt.core.tests.internal.content.java.mappings.JpaCoreContentJavaMappingsTests;
+import org.eclipse.jpt.core.tests.internal.jdtutility.JpaCoreJdtUtilityTests;
+import org.eclipse.jpt.core.tests.internal.model.JpaCoreModelTests;
+
+/**
+ * Runs all JPA Core Tests
+ */
+public class JpaCoreTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(JpaCoreTests.class.getName());
+ suite.addTest(JpaCoreContentJavaMappingsTests.suite());
+ suite.addTest(JpaCoreModelTests.suite());
+ suite.addTest(JpaCoreJdtUtilityTests.suite());
+ return suite;
+ }
+
+ private JpaCoreTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/ProjectUtility.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/ProjectUtility.java
new file mode 100644
index 0000000000..364f0ad032
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/ProjectUtility.java
@@ -0,0 +1,81 @@
+package org.eclipse.jpt.core.tests.internal;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.AssertionFailedException;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+/**
+ * Copied from org.eclipse.wst.common.tests
+ */
+public class ProjectUtility {
+ public static IProject[] getAllProjects() {
+ IProject[] projects = new IProject[0];
+ try {
+ projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+ } catch (AssertionFailedException ex) {
+ // Catch Malformed tree exception that occurs from time to time...
+ }
+ return projects;
+ }
+ public static void deleteAllProjects() throws Exception {
+ //closing projects and tread work in here is a hack because of a BeanInfo bug holding
+ //onto jars loaded in another VM
+
+// for (int i = 0; i < projects.length; i++) {
+// if (projects[i].exists()) {
+// projects[i].close(null); // This should signal the extra VM to kill itself
+// }
+// }
+ // Thread.yield(); // give the VM a chance to die
+ IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
+
+ public void run(IProgressMonitor monitor) {
+ IProject[] projects = getAllProjects();
+ for (int i = 0; i < projects.length; i++) {
+ IProject project = projects[i];
+ boolean success = false;
+ Exception lastException = null;
+ // Don't make 2^12 is about 4 seconds which is the max we
+ // will wait for the VM to die
+ for (int j = 0; j < 13 && !success; j++) {
+ try {
+ if (project.exists()) {
+ project.delete(true, true, null);
+ ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
+ }
+ success = true;
+ } catch (Exception e) {
+ lastException = e;
+ if (project.exists()) {
+ try {
+ project.close(null);
+ project.open(null);
+ } catch (Exception e2) {
+ // do nothing
+ }
+ }
+ try {
+ Thread.sleep((int) Math.pow(2, j));
+ } catch (InterruptedException e1) {
+ // do nothing
+ } // if the VM isn't dead, try sleeping
+ }
+ }
+ if (!success && lastException != null) {
+ //Logger.getLogger().log("Problem while deleting: " + lastException.getMessage());
+// Assert.fail("Caught Exception=" +
+// lastException.getMessage() + " when deleting project=" + project.getName());
+ }
+ }
+ }
+ };
+ try {
+ ResourcesPlugin.getWorkspace().run(runnable, null);
+ } catch (CoreException ce) {
+ // do nothing
+ }
+ //verifyNoProjects();
+ }
+} \ No newline at end of file
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
new file mode 100644
index 0000000000..8b72295321
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JavaEntityTests.java
@@ -0,0 +1,42 @@
+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.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(StringBuffer sb) {
+ sb.append("@Entity");
+ }
+ });
+ this.synchPersistenceXml();
+ }
+
+ public void testGetName() throws Exception {
+ this.createTestEntity1();
+ IJavaTypeMapping typeMapping = this.javaPersistentTypeNamed(FULLY_QUALIFIED_TYPE_NAME).getMapping();
+ assertEquals(TYPE_NAME, typeMapping.getName());
+ }
+
+// TODO we can only execute 1 test for now...
+// 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/JpaCoreContentJavaMappingsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaCoreContentJavaMappingsTests.java
new file mode 100644
index 0000000000..33bf899579
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaCoreContentJavaMappingsTests.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * 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 JpaCoreContentJavaMappingsTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(JpaCoreContentJavaMappingsTests.class.getName());
+// suite.addTestSuite(JavaEntityTests.class);
+ return suite;
+ }
+
+ private JpaCoreContentJavaMappingsTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
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
new file mode 100644
index 0000000000..456e797a98
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/content/java/mappings/JpaJavaTestCase.java
@@ -0,0 +1,52 @@
+package org.eclipse.jpt.core.tests.internal.content.java.mappings;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jpt.core.internal.IJpaFile;
+import org.eclipse.jpt.core.internal.content.java.JavaPersistentType;
+import org.eclipse.jpt.core.internal.content.java.JpaCompilationUnit;
+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.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(projectName, autoBuild);
+ }
+
+ protected TestJpaProject jpaProject() {
+ return (TestJpaProject) this.javaProject;
+ }
+
+ protected JavaPersistentType javaPersistentTypeNamed(String typeName) {
+ for (IJpaFile jpaFile : this.jpaProject().getJpaProject().jpaFiles(JavaCore.JAVA_SOURCE_CONTENT_TYPE)) {
+ JpaCompilationUnit cu = (JpaCompilationUnit) jpaFile.getContent();
+ for (JavaPersistentType pt : cu.getTypes()) {
+ if (pt.fullyQualifiedTypeName().equals(typeName)) {
+ return pt;
+ }
+ }
+ }
+ throw new IllegalArgumentException("missing type: " + typeName);
+ }
+
+ 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/jdtutility/AnnotationTestCase.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java
new file mode 100644
index 0000000000..87d6169bac
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/AnnotationTestCase.java
@@ -0,0 +1,429 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import java.util.Iterator;
+import java.util.List;
+import junit.framework.TestCase;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.NumberLiteral;
+import org.eclipse.jdt.core.dom.SimpleName;
+import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+import org.eclipse.jpt.core.internal.jdtutility.FieldAttribute;
+import org.eclipse.jpt.core.internal.jdtutility.MethodAttribute;
+import org.eclipse.jpt.core.internal.jdtutility.Type;
+import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject;
+import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter;
+import org.eclipse.jpt.utility.internal.iterators.EmptyIterator;
+import org.eclipse.jpt.utility.internal.iterators.SingleElementIterator;
+
+/**
+ * Provide an easy(?) way to build an annotated source file.
+ * The type must be created by calling one of the #createType() methods
+ * before calling any of the various helper methods (i.e. the type is *not*
+ * created during #setUp()).
+ */
+public abstract class AnnotationTestCase extends TestCase {
+ protected TestJavaProject javaProject;
+
+ protected static final String CR = System.getProperty("line.separator");
+ protected static final String PROJECT_NAME = "AnnotationTestProject";
+ protected static final String PACKAGE_NAME = "test";
+ protected static final String TYPE_NAME = "AnnotationTestType";
+ protected static final String FULLY_QUALIFIED_TYPE_NAME = PACKAGE_NAME + "." + TYPE_NAME;
+ protected static final String FILE_NAME = TYPE_NAME + ".java";
+
+ protected static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+
+ // ********** TestCase behavior **********
+
+ protected AnnotationTestCase(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.javaProject = this.buildJavaProject(PROJECT_NAME, false); // false = no auto-build
+ }
+
+ protected TestJavaProject buildJavaProject(String projectName, boolean autoBuild) throws Exception {
+ return new TestJavaProject(projectName, autoBuild); // false = no auto-build
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+// this.dumpSource();
+ this.javaProject.dispose();
+ this.javaProject = null;
+ super.tearDown();
+ }
+
+ protected void dumpSource() throws Exception {
+ System.out.println("*** " + this.getName() + " ****");
+ System.out.println(this.source());
+ System.out.println();
+ }
+
+
+ // ********** type creation **********
+
+ /**
+ * create an un-annotated type
+ */
+ protected IType createTestType() throws CoreException {
+ return this.createTestType(new DefaultAnnotationWriter());
+ }
+
+ /**
+ * shortcut for simply adding an annotation to the 'id' field
+ */
+ protected IType createTestType(final String annotationImport, final String idFieldAnnotation) throws CoreException {
+ return this.createTestType(new DefaultAnnotationWriter() {
+ @Override
+ public Iterator<String> imports() {
+ return (annotationImport == null) ?
+ EmptyIterator.<String>instance()
+ :
+ new SingleElementIterator<String>(annotationImport);
+ }
+ @Override
+ public void appendIdFieldAnnotationTo(StringBuffer sb) {
+ sb.append(idFieldAnnotation);
+ }
+ });
+ }
+
+ /**
+ * shortcut for simply adding a fully-qualified annotation to the 'id' field
+ */
+ protected IType createTestType(final String idFieldAnnotation) throws CoreException {
+ return this.createTestType(null, idFieldAnnotation);
+ }
+
+ protected IType createTestType(AnnotationWriter annotationWriter) throws CoreException {
+ return this.javaProject.createType(PACKAGE_NAME, FILE_NAME, this.createSourceWriter(annotationWriter));
+ }
+
+ protected SourceWriter createSourceWriter(AnnotationWriter annotationWriter) {
+ return new AnnotatedSourceWriter(annotationWriter);
+ }
+
+ protected void appendSourceTo(StringBuffer sb, AnnotationWriter annotationWriter) {
+ sb.append(CR);
+ for (Iterator<String> stream = annotationWriter.imports(); stream.hasNext(); ) {
+ sb.append("import ");
+ sb.append(stream.next());
+ sb.append(";");
+ sb.append(CR);
+ }
+ annotationWriter.appendTypeAnnotationTo(sb);
+ sb.append(CR);
+ sb.append("public class ").append(TYPE_NAME).append(" {").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendIdFieldAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" private int id;").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendNameFieldAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" private String name;").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendGetIdMethodAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" public int getId() {").append(CR);
+ sb.append(" return this.id;").append(CR);
+ sb.append(" }").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendSetIdMethodAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" public void setId(int id) {").append(CR);
+ sb.append(" this.id = id;").append(CR);
+ sb.append(" }").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendGetNameMethodAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" public String getName() {").append(CR);
+ sb.append(" return this.name;").append(CR);
+ sb.append(" }").append(CR);
+ sb.append(CR);
+ sb.append(" ");
+ annotationWriter.appendSetNameMethodAnnotationTo(sb);
+ sb.append(CR);
+ sb.append(" public void setTestField(String testField) {").append(CR);
+ sb.append(" this.testField = testField;").append(CR);
+ sb.append(" }").append(CR);
+ sb.append(CR);
+ sb.append("}").append(CR);
+ }
+
+
+ // ********** queries **********
+
+ protected IType jdtType() throws JavaModelException {
+ return this.javaProject.findType(FULLY_QUALIFIED_TYPE_NAME);
+ }
+
+ protected Type testType() throws JavaModelException {
+ return new Type(this.jdtType());
+ }
+
+ protected FieldAttribute idField() throws JavaModelException {
+ return this.fieldNamed("id");
+ }
+
+ protected FieldAttribute nameField() throws JavaModelException {
+ return this.fieldNamed("name");
+ }
+
+ protected FieldAttribute fieldNamed(String fieldName) throws JavaModelException {
+ return new FieldAttribute(this.jdtType().getField(fieldName));
+ }
+
+ protected MethodAttribute idGetMethod() throws JavaModelException {
+ return this.methodNamed("getId");
+ }
+
+ protected MethodAttribute nameGetMethod() throws JavaModelException {
+ return this.methodNamed("getName");
+ }
+
+ protected MethodAttribute methodNamed(String methodName) throws JavaModelException {
+ return this.method(methodName, EMPTY_STRING_ARRAY);
+ }
+
+ protected MethodAttribute method(String methodName, String[] parameterTypeSignatures) throws JavaModelException {
+ return new MethodAttribute(this.jdtType().getMethod(methodName, parameterTypeSignatures));
+ }
+
+ protected String source() throws JavaModelException {
+ return this.jdtType().getOpenable().getBuffer().getContents();
+ }
+
+
+ // ********** test validation **********
+
+ protected void assertSourceContains(String s) throws JavaModelException {
+ String source = this.source();
+ boolean found = source.indexOf(s) > -1;
+ if ( ! found) {
+ String msg = "source does not contain the expected string: " + s + " (see System console)";
+ System.out.println("*** " + this.getName() + " ****");
+ System.out.println(msg);
+ System.out.println(source);
+ System.out.println();
+ fail(msg);
+ }
+ }
+
+ protected void assertSourceDoesNotContain(String s) throws JavaModelException {
+ String source = this.source();
+ int pos = source.indexOf(s);
+ if (pos != -1) {
+ String msg = "unexpected string in source (position: " + pos + "): " + s + " (see System console)";
+ System.out.println("*** " + this.getName() + " ****");
+ System.out.println(msg);
+ System.out.println(source);
+ System.out.println();
+ fail(msg);
+ }
+ }
+
+
+ // ********** manipulate annotations **********
+
+ /**
+ * Return the *first* member value pair for the specified annotation element
+ * with the specified name.
+ * Return null if the annotation has no such element.
+ */
+ protected MemberValuePair memberValuePair(NormalAnnotation annotation, String elementName) {
+ for (MemberValuePair pair : this.values(annotation)) {
+ if (pair.getName().getFullyQualifiedName().equals(elementName)) {
+ return pair;
+ }
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected List<MemberValuePair> values(NormalAnnotation na) {
+ return na.values();
+ }
+
+ /**
+ * check for null member value pair
+ */
+ protected Expression valueInternal(MemberValuePair pair) {
+ return (pair == null) ? null : pair.getValue();
+ }
+
+ /**
+ * Return the value of the *first* annotation element
+ * with the specified name.
+ * Return null if the annotation has no such element.
+ */
+ protected Expression annotationElementValue(NormalAnnotation annotation, String elementName) {
+ return this.valueInternal(this.memberValuePair(annotation, elementName));
+ }
+
+ /**
+ * Return the value of the *first* annotation element
+ * with the specified name.
+ * Return null if the annotation has no such element.
+ */
+ protected Expression annotationElementValue(SingleMemberAnnotation annotation, String elementName) {
+ return elementName.equals("value") ? annotation.getValue() : null;
+ }
+
+ /**
+ * Return the value of the *first* annotation element
+ * with the specified name.
+ * Return null if the annotation has no such element.
+ * (An element name of "value" will return the value of a single
+ * member annotation.)
+ */
+ protected Expression annotationElementValue(Annotation annotation, String elementName) {
+ if (annotation.isNormalAnnotation()) {
+ return this.annotationElementValue((NormalAnnotation) annotation, elementName);
+ }
+ if (annotation.isSingleMemberAnnotation()) {
+ return this.annotationElementValue((SingleMemberAnnotation) annotation, elementName);
+ }
+ return null;
+ }
+
+ /**
+ * Build a number literal and set its initial value to the specified literal.
+ */
+ protected NumberLiteral newNumberLiteral(AST ast, int value) {
+ return ast.newNumberLiteral(Integer.toString(value));
+ }
+
+ /**
+ * Build a string literal and set its initial value.
+ */
+ protected StringLiteral newStringLiteral(AST ast, String value) {
+ StringLiteral stringLiteral = ast.newStringLiteral();
+ stringLiteral.setLiteralValue(value);
+ return stringLiteral;
+ }
+
+ protected MemberValuePair newMemberValuePair(AST ast, SimpleName name, Expression value) {
+ MemberValuePair pair = ast.newMemberValuePair();
+ pair.setName(name);
+ pair.setValue(value);
+ return pair;
+ }
+
+ protected MemberValuePair newMemberValuePair(AST ast, String name, Expression value) {
+ return this.newMemberValuePair(ast, ast.newSimpleName(name), value);
+ }
+
+ protected MemberValuePair newMemberValuePair(AST ast, String name, String value) {
+ return this.newMemberValuePair(ast, name, this.newStringLiteral(ast, value));
+ }
+
+ protected MemberValuePair newMemberValuePair(AST ast, String name, int value) {
+ return this.newMemberValuePair(ast, name, this.newNumberLiteral(ast, value));
+ }
+
+ /**
+ * Add the specified member value pair to the specified annotation.
+ * Return the resulting annotation.
+ */
+ protected NormalAnnotation addMemberValuePair(NormalAnnotation annotation, MemberValuePair pair) {
+ this.values(annotation).add(pair);
+ return annotation;
+ }
+
+ /**
+ * Add the specified member value pair to the specified annotation.
+ * Return the resulting annotation.
+ */
+ protected NormalAnnotation addMemberValuePair(NormalAnnotation annotation, String name, int value) {
+ return this.addMemberValuePair(annotation, this.newMemberValuePair(annotation.getAST(), name, value));
+ }
+
+ /**
+ * Add the specified member value pair to the specified annotation.
+ * Return the resulting annotation.
+ */
+ protected NormalAnnotation addMemberValuePair(NormalAnnotation annotation, String name, String value) {
+ return this.addMemberValuePair(annotation, this.newMemberValuePair(annotation.getAST(), name, value));
+ }
+
+
+ // ********** member classes **********
+
+ public interface AnnotationWriter {
+ Iterator<String> imports();
+ void appendTypeAnnotationTo(StringBuffer sb);
+ void appendIdFieldAnnotationTo(StringBuffer sb);
+ void appendNameFieldAnnotationTo(StringBuffer sb);
+ void appendGetIdMethodAnnotationTo(StringBuffer sb);
+ void appendSetIdMethodAnnotationTo(StringBuffer sb);
+ void appendGetNameMethodAnnotationTo(StringBuffer sb);
+ void appendSetNameMethodAnnotationTo(StringBuffer sb);
+ }
+
+ public static class DefaultAnnotationWriter implements AnnotationWriter {
+ public Iterator<String> imports() {return EmptyIterator.instance();}
+ public void appendTypeAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendIdFieldAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendNameFieldAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendGetIdMethodAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendSetIdMethodAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendGetNameMethodAnnotationTo(StringBuffer sb) {/* do nothing */}
+ public void appendSetNameMethodAnnotationTo(StringBuffer sb) {/* do nothing */}
+ }
+
+ public static class AnnotationWriterWrapper implements AnnotationWriter {
+ private final AnnotationWriter aw;
+ public AnnotationWriterWrapper(AnnotationWriter aw) {
+ super();
+ this.aw = aw;
+ }
+ public Iterator<String> imports() {return aw.imports();}
+ public void appendTypeAnnotationTo(StringBuffer sb) {aw.appendTypeAnnotationTo(sb);}
+ public void appendIdFieldAnnotationTo(StringBuffer sb) {aw.appendIdFieldAnnotationTo(sb);}
+ public void appendNameFieldAnnotationTo(StringBuffer sb) {aw.appendNameFieldAnnotationTo(sb);}
+ public void appendGetIdMethodAnnotationTo(StringBuffer sb) {aw.appendGetIdMethodAnnotationTo(sb);}
+ public void appendSetIdMethodAnnotationTo(StringBuffer sb) {aw.appendSetIdMethodAnnotationTo(sb);}
+ public void appendGetNameMethodAnnotationTo(StringBuffer sb) {aw.appendGetNameMethodAnnotationTo(sb);}
+ public void appendSetNameMethodAnnotationTo(StringBuffer sb) {aw.appendSetNameMethodAnnotationTo(sb);}
+ }
+
+ public class AnnotatedSourceWriter implements SourceWriter {
+ private AnnotationWriter annotationWriter;
+ public AnnotatedSourceWriter(AnnotationWriter annotationWriter) {
+ super();
+ this.annotationWriter = annotationWriter;
+ }
+ public void appendSourceTo(StringBuffer sb) {
+ AnnotationTestCase.this.appendSourceTo(sb, this.annotationWriter);
+ }
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/CombinationIndexedDeclarationAnnotationAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/CombinationIndexedDeclarationAnnotationAdapterTests.java
new file mode 100644
index 0000000000..10a259ccf7
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/CombinationIndexedDeclarationAnnotationAdapterTests.java
@@ -0,0 +1,728 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+import org.eclipse.jpt.core.internal.jdtutility.AnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.CombinationIndexedDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.IndexedAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.IndexedDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.MemberIndexedAnnotationAdapter;
+
+public class CombinationIndexedDeclarationAnnotationAdapterTests extends AnnotationTestCase {
+
+ public CombinationIndexedDeclarationAnnotationAdapterTests(String name) {
+ super(name);
+ }
+
+ private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
+ }
+
+ public void testGetAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumn(name=\"ADDRESS_ID\")");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.JoinColumn", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isNormalAnnotation());
+ }
+
+ public void testGetAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumn(name=\"ADDRESS_ID\")");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns(@annot.JoinColumn(name=\"ADDRESS_ID\"))");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.JoinColumn", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isNormalAnnotation());
+ }
+
+ public void testGetAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns(@annot.JoinColumn(name=\"ADDRESS_ID\"))");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.JoinColumn", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isNormalAnnotation());
+ String value = ((StringLiteral) ((MemberValuePair) ((NormalAnnotation) annotation).values().get(0)).getValue()).getLiteralValue();
+ assertEquals("ADDRESS_ID2", value);
+ }
+
+ public void testGetAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.JoinColumn", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isNormalAnnotation());
+ String value = ((StringLiteral) ((MemberValuePair) ((NormalAnnotation) annotation).values().get(0)).getValue()).getLiteralValue();
+ assertEquals("ADDRESS_ID2", value);
+ }
+
+ public void testGetAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testRemoveAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumn(name=\"ADDRESS_ID\")");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumn");
+ }
+
+ public void testRemoveAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns(@annot.JoinColumn(name=\"ADDRESS_ID\"))");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumns");
+ this.assertSourceDoesNotContain("JoinColumn");
+ }
+
+ public void testRemoveAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumns");
+ this.assertSourceDoesNotContain("ADDRESS_ID2");
+ this.assertSourceContains("@JoinColumn(name=\"ADDRESS_ID1\")");
+ }
+
+ public void testRemoveAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumns");
+ this.assertSourceDoesNotContain("ADDRESS_ID2");
+ this.assertSourceContains("@JoinColumn(name=\"ADDRESS_ID1\")");
+ }
+
+ public void testRemoveAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "String comment(); JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(comment=\"test\",columns={@annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(comment=\"test\",columns=@annot.JoinColumn(name=\"ADDRESS_ID1\"))");
+ }
+
+ public void testRemoveAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns(null)");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.removeAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(null)");
+ }
+
+ public void testRemoveAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID0\")";
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("JoinColumns");
+ }
+
+ public void testRemoveAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({null, @annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ this.assertSourceContains("@annot.JoinColumn");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumn");
+ }
+
+ public void testRemoveAnnotation14() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\"), null})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID0\")";
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation15() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\"), @annot.JoinColumn(name=\"ADDRESS_ID3\")})");
+ String expected = "@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, null, @annot.JoinColumn(name=\"ADDRESS_ID3\")})";
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation16() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\"), @annot.JoinColumn(name=\"ADDRESS_ID3\")})");
+ String expected = "@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\")})";
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 3);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation17() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({null, null, @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ this.assertSourceContains("@annot.JoinColumn");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("JoinColumn");
+ }
+
+ public void testNewMarkerAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("JoinColumn");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("JoinColumn");
+ this.assertSourceDoesNotContain("JoinColumns");
+ }
+
+ public void testNewMarkerAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn");
+ this.assertSourceDoesNotContain("JoinColumns");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@JoinColumns(columns={@JoinColumn,@JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn, @annot.JoinColumn})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(columns={@annot.JoinColumn, @annot.JoinColumn," + CR + " @JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(77)");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("JoinColumn");
+ this.assertSourceDoesNotContain("JoinColumns");
+ this.assertSourceDoesNotContain("77");
+ }
+
+ public void testNewMarkerAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns=@annot.JoinColumn(77))");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(columns=@JoinColumn)");
+ this.assertSourceDoesNotContain("77");
+ }
+
+ public void testNewMarkerAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns=@annot.JoinColumn(77))");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(columns={@annot.JoinColumn(77),@JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(77)");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@JoinColumns(columns={@JoinColumn(77),@JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(77),@annot.JoinColumn(88)})");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.JoinColumns(columns={@annot.JoinColumn(77),@JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name(); String text(); int num();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(text=\"blah\",num=42)");
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@JoinColumns(columns={@JoinColumn(text=\"blah\", num=42),@JoinColumn})");
+ }
+
+ public void testNewMarkerAnnotation23() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name(); String text(); int num();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(text=\"blah\",num=42)");
+ String expected = "@JoinColumns(columns={@JoinColumn(text=\"blah\", num=42),null,@JoinColumn})";
+ this.assertSourceDoesNotContain(expected);
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation24() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name(); String text(); int num();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumn(text=\"blah\",num=42)");
+ String expected1 = "@JoinColumns({";
+ String expected2 = "@JoinColumn(text=\"blah\", num=42),null,";
+ String expected3 = "@JoinColumn})";
+ this.assertSourceDoesNotContain(expected1);
+ this.assertSourceDoesNotContain(expected2);
+ this.assertSourceDoesNotContain(expected3);
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected1);
+ this.assertSourceContains(expected2);
+ this.assertSourceContains(expected3);
+ }
+
+ public void testNewMarkerAnnotation25() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected1 = "@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), null, null,"; // the line gets split
+ String expected2 = "@JoinColumn})";
+ this.assertSourceDoesNotContain(expected1);
+ this.assertSourceDoesNotContain(expected2);
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 4);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected1);
+ this.assertSourceContains(expected2);
+ }
+
+ public void testNewMarkerAnnotation26() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected1 = "@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), null, null,"; // the line gets split
+ String expected2 = "@JoinColumn})";
+ this.assertSourceDoesNotContain(expected1);
+ this.assertSourceDoesNotContain(expected2);
+ DeclarationAnnotationAdapter daa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 4);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected1);
+ this.assertSourceContains(expected2);
+ }
+
+ public void testMoveAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(name=\"ADDRESS_ID0\")");
+ String expected = "@JoinColumns(columns={null,@JoinColumn(name=\"ADDRESS_ID0\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(1);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={null,@annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID1\")";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("JoinColumns");
+ }
+
+ public void testMoveAnnotation2a() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({null,@annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID1\")";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 1);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("JoinColumns");
+ }
+
+ public void testMoveAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), @annot.JoinColumn(name=\"ADDRESS_ID3\")})");
+ String expected = "@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID3\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 3);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), @annot.JoinColumn(name=\"ADDRESS_ID3\"), @annot.JoinColumn(name=\"ADDRESS_ID4\")})");
+ String expected = "@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID3\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), null, @annot.JoinColumn(name=\"ADDRESS_ID4\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 3);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ String expected = "@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 2);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ String expected = "@annot.JoinColumns(columns={null, @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), @annot.JoinColumn(name=\"ADDRESS_ID0\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ String expected = "@annot.JoinColumns(columns={null, @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 3);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), null, @annot.JoinColumn(name=\"ADDRESS_ID4\")})");
+ String expected = "@annot.JoinColumns(columns={null, @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\"), null, @annot.JoinColumn(name=\"ADDRESS_ID4\")})";
+ this.assertSourceDoesNotContain(expected);
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 3);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ String expected = "@annot.JoinColumns(columns={null, @annot.JoinColumn(name=\"ADDRESS_ID1\"), @annot.JoinColumn(name=\"ADDRESS_ID2\")})";
+ this.createTestType(expected); // the source should be unchanged
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 0);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID0\")";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 2);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(1);
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("@annot.JoinColumns");
+ }
+
+ public void testMoveAnnotation10a() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] value();");
+ this.createTestType("@annot.JoinColumns({@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID0\")";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "value", 2);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(1);
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("@annot.JoinColumns");
+ }
+
+ public void testMoveAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumn(name=\"ADDRESS_ID0\")");
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 1);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceDoesNotContain("JoinColumn");
+ }
+
+ public void testMoveAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), null, @annot.JoinColumn(name=\"ADDRESS_ID2\")})");
+ String expected = "@JoinColumn(name=\"ADDRESS_ID2\")";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 2);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ this.assertSourceDoesNotContain("@annot.JoinColumns");
+ }
+
+ public void testMoveAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("JoinColumn", "String name();");
+ this.createAnnotationAndMembers("JoinColumns", "JoinColumn[] columns();");
+ this.createTestType("@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID0\"), @annot.JoinColumn(name=\"ADDRESS_ID1\"), null, @annot.JoinColumn(name=\"ADDRESS_ID3\")})");
+ String expected = "@annot.JoinColumns(columns={@annot.JoinColumn(name=\"ADDRESS_ID3\"), @annot.JoinColumn(name=\"ADDRESS_ID1\")})";
+ IndexedDeclarationAnnotationAdapter cidaa = new CombinationIndexedDeclarationAnnotationAdapter(
+ "annot.JoinColumn", "annot.JoinColumns", "columns", 3);
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), cidaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JDTToolsTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JDTToolsTests.java
new file mode 100644
index 0000000000..079f2f59f6
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JDTToolsTests.java
@@ -0,0 +1,164 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.Signature;
+import org.eclipse.jpt.core.internal.jdtutility.ConversionDeclarationAnnotationElementAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationElementAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.FieldAttribute;
+import org.eclipse.jpt.core.internal.jdtutility.JDTTools;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
+
+public class JDTToolsTests extends AnnotationTestCase {
+
+ public JDTToolsTests(String name) {
+ super(name);
+ }
+
+ public void testResolveSignature1() throws Exception {
+ this.verifyResolveSignature("String", "java.lang.String");
+ }
+
+ public void testResolveSignature2() throws Exception {
+ this.verifyResolveSignature("List", null);
+ }
+
+ public void testResolveSignature3() throws Exception {
+ this.verifyResolveSignature("int", "int");
+ }
+
+ public void testResolveSignature4() throws Exception {
+ this.verifyResolveSignature("void", "void");
+ }
+
+ public void testResolveSignature5() throws Exception {
+ this.verifyResolveSignature("int[]", "int[]");
+ }
+
+ public void testResolveSignature6() throws Exception {
+ this.verifyResolveSignature("String[]", "java.lang.String[]");
+ }
+
+ public void testResolveSignature7() throws Exception {
+ this.verifyResolveSignature("java.util.List[][][]", "java.util.List[][][]");
+ }
+
+ public void testResolveSignature8a() throws Exception {
+ // inner class
+ this.verifyResolveSignature("java.util.Map.Entry", "java.util.Map.Entry");
+ }
+
+ public void testResolveSignature8b() throws Exception {
+ // inner class
+ this.createTestType("java.util.Map", "");
+ this.verifyResolveSignature2("Map.Entry", "java.util.Map.Entry");
+ }
+
+ public void testResolveSignature8c() throws Exception {
+ // inner class
+ this.createTestType("java.util.Map.Entry", "");
+ this.verifyResolveSignature2("Entry", "java.util.Map.Entry");
+ }
+
+ public void testResolveSignature9() throws Exception {
+ // inner class
+ this.verifyResolveSignature("Character.Subset", "java.lang.Character.Subset");
+ }
+
+ public void testResolveSignature10() throws Exception {
+ // generic type
+ this.verifyResolveSignature("java.util.List<java.lang.String>", "java.util.List"); // ????
+ }
+
+ public void testResolveSignature11() throws Exception {
+ // annotation
+ this.createTestType("java.lang.annotation.Target", "");
+ this.verifyResolveSignature2("Target", "java.lang.annotation.Target");
+ }
+
+ public void testResolveSignature12() throws Exception {
+ this.createTestType("java.lang.annotation.ElementType", "");
+ this.verifyResolveSignature2("ElementType", "java.lang.annotation.ElementType");
+ }
+
+ private void verifyResolveSignature(String unresolvedTypeName, String expected) throws Exception {
+ this.createTestType();
+ this.verifyResolveSignature2(unresolvedTypeName, expected);
+ }
+
+ private void verifyResolveSignature2(String unresolvedTypeName, String expected) throws Exception {
+ String signature = Signature.createTypeSignature(unresolvedTypeName, false);
+ String actual = JDTTools.resolveSignature(signature, this.jdtType());
+ assertEquals(expected, actual);
+ }
+
+
+ private void createEnumAndMembers(String enumName, String enumBody) throws Exception {
+ this.javaProject.createType("enums", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }");
+ }
+
+ private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
+ }
+
+ public void testResolveEnum1() throws Exception {
+ this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
+ this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");
+
+ this.createTestType("@annot.TestAnnotation(foo=enums.TestEnum.BAZ)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
+ FieldAttribute field = this.idField();
+
+ String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
+ assertEquals("enums.TestEnum.BAZ", actual);
+ }
+
+ public void testResolveEnum2() throws Exception {
+ this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
+ this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");
+
+ this.createTestType("static enums.TestEnum.BAZ", "@annot.TestAnnotation(foo=BAZ)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
+ FieldAttribute field = this.idField();
+
+ String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
+ assertEquals("enums.TestEnum.BAZ", actual);
+ }
+
+ public void testResolveEnum3() throws Exception {
+ this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
+ this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");
+
+ this.createTestType("static enums.TestEnum.*", "@annot.TestAnnotation(foo=BAZ)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
+ FieldAttribute field = this.idField();
+
+ String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
+ assertEquals("enums.TestEnum.BAZ", actual);
+ }
+
+ public void testResolveEnum4() throws Exception {
+ this.createEnumAndMembers("TestEnum", "FOO, BAR, BAZ");
+ this.createAnnotationAndMembers("TestAnnotation", "TestEnum foo();");
+
+ this.createTestType("enums.TestEnum", "@annot.TestAnnotation(foo=TestEnum.BAZ)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.TestAnnotation");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "foo");
+ FieldAttribute field = this.idField();
+
+ String actual = JDTTools.resolveEnum(this.jdtType().getCompilationUnit(), field.annotationElementExpression(daea));
+ assertEquals("enums.TestEnum.BAZ", actual);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JpaCoreJdtUtilityTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JpaCoreJdtUtilityTests.java
new file mode 100644
index 0000000000..eacc2ab232
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/JpaCoreJdtUtilityTests.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class JpaCoreJdtUtilityTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(JpaCoreJdtUtilityTests.class.getName());
+ suite.addTestSuite(MemberAnnotationElementAdapterTests.class);
+ suite.addTestSuite(CombinationIndexedDeclarationAnnotationAdapterTests.class);
+ suite.addTestSuite(JDTToolsTests.class);
+ suite.addTestSuite(NestedDeclarationAnnotationAdapterTests.class);
+ suite.addTestSuite(NestedIndexedDeclarationAnnotationAdapterTests.class);
+ suite.addTestSuite(SimpleDeclarationAnnotationAdapterTests.class);
+ suite.addTestSuite(TypeTests.class);
+ return suite;
+ }
+
+ private JpaCoreJdtUtilityTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
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
new file mode 100644
index 0000000000..90ca36671b
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java
@@ -0,0 +1,542 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+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.BooleanStringExpressionConverter;
+import org.eclipse.jpt.core.internal.jdtutility.CharacterStringExpressionConverter;
+import org.eclipse.jpt.core.internal.jdtutility.ConversionDeclarationAnnotationElementAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationElementAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.EnumDeclarationAnnotationElementAdapter;
+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.PrimitiveTypeStringExpressionConverter;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleTypeStringExpressionConverter;
+
+public class MemberAnnotationElementAdapterTests extends AnnotationTestCase {
+
+ public MemberAnnotationElementAdapterTests(String name) {
+ super(name);
+ }
+
+ private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
+ }
+
+ private void createEnum(String enumName, String enumBody) throws Exception {
+ this.javaProject.createType("enums", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }");
+ }
+
+ public void testGetValue1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ this.createTestType("@annot.Foo(bar=\"xxx\")");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("xxx", aea.getValue());
+ }
+
+ public void testGetValue2() throws Exception {
+ this.createAnnotationAndMembers("Foo", "int bar();");
+ this.createTestType("@annot.Foo(bar=48)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", NumberStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("48", aea.getValue());
+ }
+
+ public void testGetValue3() throws Exception {
+ this.createAnnotationAndMembers("Foo", "char bar();");
+ this.createTestType("@annot.Foo(bar='c')");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", CharacterStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("c", aea.getValue());
+ }
+
+ public void testGetValue4() throws Exception {
+ this.createAnnotationAndMembers("Foo", "boolean bar();");
+ this.createTestType("@annot.Foo(bar=false)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("false", aea.getValue());
+ }
+
+ public void testGetValue5() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType("@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=false)))");
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("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 daea = new ConversionDeclarationAnnotationElementAdapter(daa, BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("false", aea.getValue());
+ }
+
+ public void testGetValueNull1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ this.createTestType("@annot.Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testGetValueNull2() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testGetValueNull3() throws Exception {
+ this.createAnnotationAndMembers("Baz", "String fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType("@annot.Foo(@annot.Bar(jimmy=@annot.Baz))");
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testASTNode1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ String value = "\"xxx\"";
+ String element = "bar=" + value;
+ String annotation = "@annot.Foo(" + element + ")";
+ this.createTestType(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ ITextRange textRange = new ASTNodeTextRange(aea.astNode());
+ assertEquals(this.source().indexOf(value), textRange.getOffset());
+ assertEquals(value.length(), textRange.getLength());
+ assertEquals(7, textRange.getLineNumber());
+ }
+
+ public void testASTNode2() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ String value = "false";
+ String element = "fred=" + value;
+ String annotation = "@annot.Foo(@annot.Bar(jimmy=@annot.Baz(" + element + ")))";
+ this.createTestType(annotation);
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("false", aea.getValue());
+ ITextRange textRange = new ASTNodeTextRange(aea.astNode());
+ assertEquals(value.length(), textRange.getLength());
+ }
+
+ public void testASTNode3() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String value();");
+ String element = "\"xxx\"";
+ String annotation = "@annot.Foo(" + element + ")";
+ this.createTestType(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa);
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ ITextRange textRange = new ASTNodeTextRange(aea.astNode());
+ assertEquals(this.source().indexOf(element), textRange.getOffset());
+ assertEquals(element.length(), textRange.getLength());
+ }
+
+ public void testASTNode4() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String value();");
+ String annotation = "@annot.Foo";
+ this.createTestType(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa);
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ ITextRange textRange = new ASTNodeTextRange(aea.astNode());
+ assertEquals(this.source().indexOf(annotation), textRange.getOffset());
+ assertEquals(annotation.length(), textRange.getLength());
+ }
+
+ public void testSetValue1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ String annotation = "@annot.Foo(bar=\"xxx\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue(null);
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testSetValue2() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ String annotation = "@annot.Foo(bar=\"xxx\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", false);
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue(null);
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@Foo");
+ }
+
+ public void testSetValue3() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ String annotation = "@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=false)))";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue(null);
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceDoesNotContain("Foo");
+ this.assertSourceDoesNotContain("Bar");
+ }
+
+ public void testSetValue3a() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ String annotation = "@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=false)))";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ 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 daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue(null);
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@annot.Foo(@Bar)");
+ }
+
+ public void testSetValue4() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("xxx");
+ this.assertSourceContains("@Foo(bar=\"xxx\")");
+ }
+
+ public void testSetValue5() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ String annotation = "@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=false)))";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("true");
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@annot.Foo(@annot.Bar(jimmy=@annot.Baz(fred=true)))");
+ }
+
+ public void testSetValue6() throws Exception {
+ this.createAnnotationAndMembers("Baz", "boolean fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "jimmy", "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", BooleanStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("true");
+ this.assertSourceContains("@Foo(@Bar(jimmy=@Baz(fred=true)))");
+ }
+
+ public void testSetValue7() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ String annotation = "@annot.Foo(bar=\"xxx\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("yyy");
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@annot.Foo(bar=\"yyy\")");
+ }
+
+ public void testSetValue8() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar();");
+ String annotation = "@annot.Foo";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("xxx");
+ this.assertSourceContains("@Foo(bar=\"xxx\")");
+ }
+
+ public void testSetValue9() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String value(); String bar();");
+ String annotation = "@annot.Foo(\"zzz\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("xxx");
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@Foo(value=\"zzz\", bar=\"xxx\")");
+ }
+
+ public void testSetValue10() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String bar(); String baz();");
+ String annotation = "@annot.Foo(bar=\"xxx\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "baz");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("yyy");
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@annot.Foo(bar=\"xxx\", baz = \"yyy\")");
+ }
+
+ public void testSetValue11() throws Exception {
+ this.createAnnotationAndMembers("Baz", "int fred();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz[] jimmy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ String annotation = "@annot.Foo(@annot.Bar(jimmy={@annot.Baz(fred=0), @annot.Baz(fred=1), @annot.Baz(fred=2), @annot.Baz(fred=3)}))";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "value", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "jimmy", 2, "annot.Baz");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa3, "fred", NumberStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ assertEquals("2", aea.getValue());
+ aea.setValue("48");
+ this.assertSourceContains("@annot.Foo(@annot.Bar(jimmy={@annot.Baz(fred=0), @annot.Baz(fred=1), @annot.Baz(fred=48), @annot.Baz(fred=3)}))");
+ }
+
+ public void testSetValue12() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String value();");
+ String annotation = "@annot.Foo";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "value");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("xxx");
+ this.assertSourceContains("@Foo(\"xxx\")");
+ }
+
+ public void testSetValue13() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String value();");
+ String annotation = "@annot.Foo(\"zzz\")";
+ this.createTestType(annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "value");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+
+ aea.setValue("xxx");
+ this.assertSourceDoesNotContain(annotation);
+ this.assertSourceContains("@annot.Foo(\"xxx\")");
+ }
+
+ public void testSimpleTypeLiteral1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=java.lang.Object.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", SimpleTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("java.lang.Object", aea.getValue());
+ }
+
+ public void testSimpleTypeLiteral2() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", SimpleTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ aea.setValue("java.lang.Object");
+ this.assertSourceContains("@Foo(bar=java.lang.Object.class)");
+ }
+
+ public void testSimpleTypeLiteral3() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=int.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", SimpleTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testSimpleTypeLiteral4() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=java.util.Map.Entry.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", SimpleTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("java.util.Map.Entry", aea.getValue());
+ }
+
+ public void testPrimitiveTypeLiteral1() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=int.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", PrimitiveTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("int", aea.getValue());
+ }
+
+ public void testPrimitiveTypeLiteral2() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", PrimitiveTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ aea.setValue("int");
+ this.assertSourceContains("@Foo(bar=int.class)");
+ }
+
+ public void testPrimitiveTypeLiteral3() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=java.lang.Object.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", PrimitiveTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testPrimitiveTypeLiteral4() throws Exception {
+ this.createAnnotationAndMembers("Foo", "Class bar();");
+ this.createTestType("@annot.Foo(bar=void.class)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", PrimitiveTypeStringExpressionConverter.instance());
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("void", aea.getValue());
+ }
+
+ public void testGetValueEnum1() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ this.createTestType("@annot.Foo(bar=enums.TestEnum.XXX)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("enums.TestEnum.XXX", aea.getValue());
+ }
+
+ public void testGetValueEnum2() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ this.createTestType("static enums.TestEnum.XXX", "@annot.Foo(bar=XXX)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("enums.TestEnum.XXX", aea.getValue());
+ }
+
+ public void testGetValueEnum3() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ this.createTestType("@annot.Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testGetValueEnum4() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ this.createTestType("enums.TestEnum", "@annot.Foo(bar=TestEnum.XXX)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertEquals("enums.TestEnum.XXX", aea.getValue());
+ }
+
+ public void testSetValueEnum1() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ String annotation = "@annot.Foo(bar=XXX)";
+ this.createTestType("static enums.TestEnum.XXX", annotation);
+ this.assertSourceContains(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ aea.setValue(null);
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testSetValueEnum2() throws Exception {
+ this.createEnum("TestEnum", "XXX, YYY, ZZZ");
+ this.createAnnotationAndMembers("Foo", "enums.TestEnum bar();");
+ String annotation = "@Foo(bar=XXX)";
+ this.createTestType();
+ this.assertSourceDoesNotContain(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new EnumDeclarationAnnotationElementAdapter(daa, "bar");
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ aea.setValue("enums.TestEnum.XXX");
+ this.assertSourceContains("import static enums.TestEnum.XXX;");
+ this.assertSourceContains(annotation);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedDeclarationAnnotationAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedDeclarationAnnotationAdapterTests.java
new file mode 100644
index 0000000000..e82d42c7c4
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedDeclarationAnnotationAdapterTests.java
@@ -0,0 +1,763 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+import org.eclipse.jpt.core.internal.jdtutility.AnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.Member;
+import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.ModifiedDeclaration;
+import org.eclipse.jpt.core.internal.jdtutility.NestedDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
+
+public class NestedDeclarationAnnotationAdapterTests extends AnnotationTestCase {
+
+ public NestedDeclarationAnnotationAdapterTests(String name) {
+ super(name);
+ }
+
+ private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
+ }
+
+ public void testGetAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo(nestedAnnotation=@annot.Bar)");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Bar", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+ }
+
+ public void testGetAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Baz", "String value();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz yana();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo(nestedAnnotation=@annot.Bar(yana=@annot.Baz))");
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedDeclarationAnnotationAdapter(daa1, "nestedAnnotation", "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedDeclarationAnnotationAdapter(daa2, "yana", "annot.Baz");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa3);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Baz", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+ }
+
+ public void testGetAnnotationNull1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo()");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "String nestedAnnotation();");
+ this.createTestType("@annot.Foo(nestedAnnotation=\"annot.Bar\")");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Bar2", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar2 nestedAnnotation();");
+ this.createTestType("@annot.Foo(nestedAnnotation=@annot.Bar2)");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testRemoveAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ String na = "@annot.Foo(nestedAnnotation=@annot.Bar)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation1a() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ String na = "@annot.Foo(nestedAnnotation=@annot.Bar)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar", false);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains("Foo");
+ }
+
+ public void testRemoveAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "String nestedAnnotation();");
+ String na = "@annot.Foo(nestedAnnotation=\"annot.Bar\")";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Bar2", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar2 nestedAnnotation();");
+ String na = "@annot.Foo(nestedAnnotation=@annot.Bar2)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Baz", "String value();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz nestedAnnotation2();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation1();");
+ String na = "@annot.Foo(nestedAnnotation1=@annot.Bar(nestedAnnotation2=@annot.Baz))";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daaFoo = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daaBar = new NestedDeclarationAnnotationAdapter(daaFoo, "nestedAnnotation1", "annot.Bar");
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(daaBar, "nestedAnnotation2", "annot.Baz");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceDoesNotContain("Foo");
+ this.assertSourceDoesNotContain("Bar");
+ this.assertSourceDoesNotContain("Baz");
+ }
+
+ public void testRemoveAnnotation5a() throws Exception {
+ this.createAnnotationAndMembers("Baz", "String value();");
+ this.createAnnotationAndMembers("Bar", "annot.Baz nestedAnnotation2();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation1();");
+ String na = "@annot.Foo(nestedAnnotation1=@annot.Bar(nestedAnnotation2=@annot.Baz))";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daaFoo = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daaBar = new NestedDeclarationAnnotationAdapter(daaFoo, "nestedAnnotation1", "annot.Bar", false);
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(daaBar, "nestedAnnotation2", "annot.Baz", false);
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains("@annot.Foo(nestedAnnotation1=@Bar)");
+ }
+
+ public void testNewMarkerAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(nestedAnnotation=@Bar)");
+ }
+
+ public void testNewMarkerAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(@Bar)");
+ }
+
+ public void testNewMarkerAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotation=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation(); String value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotation=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "Object value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotation = @Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar value();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewSingleMemberAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation1(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(nestedAnnotation=@Bar(\"test string literal\"))");
+ }
+
+ void editNewSingleMemberAnnotation1(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) aa.getAnnotation();
+ assertNull(annotation);
+
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation2(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(@Bar(\"test string literal\"))");
+ }
+
+ void editNewSingleMemberAnnotation2(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotation=@Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation3(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation3(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation4(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation4(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation(); String value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotation=@Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation5(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation5(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "Object value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation6(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation6(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation(); String xxx();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotation = @Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation7(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation7(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value(); String xxx();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar(\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation8(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation8(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewNormalAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation1(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(nestedAnnotation=@Bar(yyy=\"test string literal\"))");
+ }
+
+ void editNewNormalAnnotation1(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation2(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(@Bar(yyy=\"test string literal\"))");
+ }
+
+ void editNewNormalAnnotation2(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotation=@Bar(yyy=\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation3(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation3(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar value();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar(yyy=\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation4(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation4(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar nestedAnnotation(); String value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotation=@Bar(yyy=\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation5(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation5(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "Object value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar(yyy=\"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation6(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation6(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar nestedAnnotation();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotation = @Bar(yyy = \"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation7(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation7(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotation", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+ public void testNewNormalAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String yyy();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar value();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar(yyy = \"test string literal\"))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation8(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation8(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "yyy", "test string literal");
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedIndexedDeclarationAnnotationAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedIndexedDeclarationAnnotationAdapterTests.java
new file mode 100644
index 0000000000..e361b43ff7
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/NestedIndexedDeclarationAnnotationAdapterTests.java
@@ -0,0 +1,2209 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.NumberLiteral;
+import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.eclipse.jpt.core.internal.jdtutility.AnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.IndexedAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.IndexedDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.Member;
+import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.MemberIndexedAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.ModifiedDeclaration;
+import org.eclipse.jpt.core.internal.jdtutility.NestedIndexedDeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
+
+public class NestedIndexedDeclarationAnnotationAdapterTests extends AnnotationTestCase {
+
+ public NestedIndexedDeclarationAnnotationAdapterTests(String name) {
+ super(name);
+ }
+
+ private void createAnnotation(String annotationName) throws Exception {
+ this.createAnnotationAndMembers(annotationName, "");
+ }
+
+ private void createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
+ }
+
+ public void testGetAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={@annot.Bar, @annot.Bar(\"two\")})");
+ // 0
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Bar", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+
+ // 1
+ daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 1, "annot.Bar");
+ aa = new MemberAnnotationAdapter(this.idField(), daa);
+ annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Bar", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isSingleMemberAnnotation());
+ }
+
+ public void testGetAnnotation2() throws Exception {
+ this.createAnnotation("Baz");
+ this.createAnnotationAndMembers("Bar", "annot.Baz[] yana();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(yana=@annot.Baz))");
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "yana", 0, "annot.Baz");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa3);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Baz", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+ }
+
+ public void testGetAnnotation3() throws Exception {
+ this.createAnnotation("Baz");
+ this.createAnnotationAndMembers("Bar", "annot.Baz[] yana();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(yana={@annot.Baz}))");
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "yana", 0, "annot.Baz");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa3);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Baz", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+
+ // name mismatch
+ daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "yana", 0, "annot.Xyz");
+ aa = new MemberAnnotationAdapter(this.idField(), daa3);
+ annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull1() throws Exception {
+ this.createAnnotation("Bar");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo()");
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull2() throws Exception {
+ this.createAnnotation("Bar");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull3() throws Exception {
+ this.createAnnotation("Bar");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=\"annot.Bar\")");
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull4() throws Exception {
+ this.createAnnotation("NotBar");
+ this.createAnnotation("Bar");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "String value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={@annot.Bar, @annot.Bar(\"two\")})");
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull6() throws Exception {
+ this.createAnnotation("Xyz");
+ this.createAnnotation("Baz");
+ this.createAnnotationAndMembers("Bar", "annot.Baz[] yana();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(yana={@annot.Baz}))");
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar");
+ DeclarationAnnotationAdapter daa3 = new NestedIndexedDeclarationAnnotationAdapter(daa2, "yana", 0, "annot.Xyz");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa3);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testRemoveAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), @annot.Bar(1)})";
+ String expected = "@annot.Foo(nestedAnnotations={null, @annot.Bar(1)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={\"annot.Bar1\", \"annot.Bar2\", \"annot.Bar3\"})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.NotBar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.NotBar(0), @annot.NotBar(1)})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations=@annot.Bar)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ assertNull(aa.getAnnotation());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation5a() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations=@annot.Bar)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 0, "annot.Bar", false);
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertTrue(aa1.getAnnotation().isMarkerAnnotation());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains("import annot.Foo;");
+ this.assertSourceContains("@Foo");
+ }
+
+ public void testRemoveAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations=\"annot.Bar\")";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.NotBar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations=@annot.NotBar)";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceContains(na);
+ }
+
+ public void testRemoveAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), @annot.Bar(1), @annot.Bar(2), @annot.Bar(3)})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertTrue(aa1.getAnnotation().isNormalAnnotation());
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "nestedAnnotations").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains("@annot.Foo(nestedAnnotations={@annot.Bar(0), @annot.Bar(1), null, @annot.Bar(3)})");
+ }
+
+ public void testRemoveAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), @annot.Bar(1), @annot.Bar(2), @annot.Bar(3)})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertTrue(aa1.getAnnotation().isSingleMemberAnnotation());
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains("@annot.Foo({@annot.Bar(0), @annot.Bar(1), null, @annot.Bar(3)})");
+ }
+
+ public void testRemoveAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), @annot.Bar(1)})";
+ String expected = "@annot.Foo({null, @annot.Bar(1)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 0, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), @annot.Bar(1)})";
+ String expected = "@annot.Foo(@annot.Bar(0))";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 1, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.SINGLE_MEMBER_ANNOTATION, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), null, @annot.Bar(2)})";
+ String expected = "@annot.Foo(@annot.Bar(0))";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.SINGLE_MEMBER_ANNOTATION, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({null, @annot.Bar(1)})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 1, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation14() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), null, @annot.Bar(2), null})";
+ String expected = "@annot.Foo(@annot.Bar(0))";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.SINGLE_MEMBER_ANNOTATION, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation15() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), null, @annot.Bar(2), @annot.Bar(3)})";
+ String expected = "@annot.Foo({@annot.Bar(0), null, null, @annot.Bar(3)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation16() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String na = "@annot.Foo({@annot.Bar(0), null, @annot.Bar(2), @annot.Bar(3)})";
+ String expected = "@annot.Foo({@annot.Bar(0), null, @annot.Bar(2)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "value", 3, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "value").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation17() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, @annot.Bar(2)})";
+ String expected = "@annot.Foo(nestedAnnotations=@annot.Bar(0))";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.SINGLE_MEMBER_ANNOTATION, this.annotationElementValue(aa1.getAnnotation(), "nestedAnnotations").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation18() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={null, @annot.Bar(1)})";
+ this.createTestType(na);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 1, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testRemoveAnnotation19() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, @annot.Bar(2), null})";
+ String expected = "@annot.Foo(nestedAnnotations=@annot.Bar(0))";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.SINGLE_MEMBER_ANNOTATION, this.annotationElementValue(aa1.getAnnotation(), "nestedAnnotations").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation20() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, @annot.Bar(2), @annot.Bar(3)})";
+ String expected = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, null, @annot.Bar(3)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "nestedAnnotations").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testRemoveAnnotation21() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ String na = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, @annot.Bar(2), @annot.Bar(3)})";
+ String expected = "@annot.Foo(nestedAnnotations={@annot.Bar(0), null, @annot.Bar(2)})";
+ this.createTestType(na);
+ this.assertSourceDoesNotContain(expected);
+ this.assertSourceContains(na);
+
+ DeclarationAnnotationAdapter daa1 = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa1 = new MemberAnnotationAdapter(this.idField(), daa1);
+ DeclarationAnnotationAdapter daa2 = new NestedIndexedDeclarationAnnotationAdapter(daa1, "nestedAnnotations", 3, "annot.Bar");
+ AnnotationAdapter aa2 = new MemberAnnotationAdapter(this.idField(), daa2);
+ Annotation annotation = aa2.getAnnotation();
+ assertNotNull(annotation);
+
+ aa2.removeAnnotation();
+ assertEquals(ASTNode.ARRAY_INITIALIZER, this.annotationElementValue(aa1.getAnnotation(), "nestedAnnotations").getNodeType());
+ this.assertSourceDoesNotContain(na);
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(nestedAnnotations=@Bar)");
+ }
+
+ public void testNewMarkerAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("Foo");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(@Bar)");
+ }
+
+ public void testNewMarkerAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotations=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String value(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotations=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotations = @Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar[] value();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ assertNull(aa.getAnnotation());
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(nestedAnnotations={null,null,null,null,null,@Bar})");
+ }
+
+ public void testNewMarkerAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String value(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(\"something\")");
+ this.assertSourceDoesNotContain("Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@Foo(value=\"something\", nestedAnnotations={null,null,null,null,null,@Bar})");
+ }
+
+ public void testNewMarkerAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ String expected = "@annot.Foo({@Bar, \"two\"})";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ this.assertSourceDoesNotContain("Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("Bar})"); // split line
+ }
+
+ public void testNewMarkerAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(7)");
+ String expected = "@annot.Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation14() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(7)");
+ this.assertSourceDoesNotContain("Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.Foo({7,null,null,null,null,@Bar})");
+ }
+
+ public void testNewMarkerAnnotation15() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.NotBar)");
+ String expected = "@annot.Foo(@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation16() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(55))");
+ String expected = "@annot.Foo({@annot.Bar(55),@Bar})";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 1, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation17() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ String expected = "@annot.Foo(nestedAnnotations={@Bar})";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation18() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ this.assertSourceDoesNotContain("Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("Bar})"); // split line
+ }
+
+ public void testNewMarkerAnnotation19() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=\"something\")");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation20() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation21() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("@annot.Foo(nestedAnnotations={@annot.NotBar,null,null,null,null,@Bar})");
+ }
+
+ public void testNewMarkerAnnotation22() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(88))");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar)";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation23() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(88))");
+ String expected = "@annot.Foo(nestedAnnotations={@annot.Bar(88),null,@Bar})";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 2, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation24() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(88))");
+ String expected = "@annot.Foo({@annot.Bar(88),null,@Bar})";
+ this.assertSourceDoesNotContain(expected);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 2, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected);
+ }
+
+ public void testNewMarkerAnnotation25() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={@annot.Bar(88), @annot.Bar(77)})");
+ String expected1 = "@annot.Foo(nestedAnnotations={@annot.Bar(88), @annot.Bar(77), null, null,"; // the line gets split
+ String expected2 = "@Bar})";
+ this.assertSourceDoesNotContain(expected1);
+ this.assertSourceDoesNotContain(expected2);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 4, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected1);
+ this.assertSourceContains(expected2);
+ }
+
+ public void testNewMarkerAnnotation26() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(88), @annot.Bar(77)})");
+ String expected1 = "@annot.Foo({@annot.Bar(88), @annot.Bar(77), null, null,"; // the line gets split
+ String expected2 = "@Bar})";
+ this.assertSourceDoesNotContain(expected1);
+ this.assertSourceDoesNotContain(expected2);
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 4, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ aa.newMarkerAnnotation();
+ this.assertSourceContains(expected1);
+ this.assertSourceContains(expected2);
+ }
+
+ public void testNewSingleMemberAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation1(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(nestedAnnotations=@Bar(88))");
+ }
+
+ void editNewSingleMemberAnnotation1(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation2(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(@Bar(88))");
+ }
+
+ void editNewSingleMemberAnnotation2(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotations=@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation3(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation3(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation4(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation4(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String value(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotations=@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation5(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation5(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation6(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation6(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotations = @Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation7(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation7(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String xxx(); annot.Bar[] value();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation8(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation8(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@Foo(nestedAnnotations={null,null,null,null,null,@Bar(MISSING)})"); // ???
+ }
+
+ public void testNewSingleMemberAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "String value(); annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(\"something\")");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@Foo(value=\"something\", nestedAnnotations={null,null,null,null,null,@Bar(MISSING)})"); // ???
+ }
+
+ public void testNewSingleMemberAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ String expected = "@annot.Foo({@Bar(88), \"two\"})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation11(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation11(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@Bar(MISSING)})"); // split line
+ }
+
+ public void testNewSingleMemberAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(7)");
+ String expected = "@annot.Foo(@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation13(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation13(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation14() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(7)");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@annot.Foo({7,null,null,null,null,@Bar(MISSING)})");
+ }
+
+ public void testNewSingleMemberAnnotation15() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(@annot.NotBar)");
+ String expected = "@annot.Foo(@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation15(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation15(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation16() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(55))");
+ String expected = "@annot.Foo({@annot.Bar(55),@Bar(88)})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation16(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation16(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 1, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation17() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ String expected = "@annot.Foo(nestedAnnotations={@Bar(88)})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation17(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation17(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation18() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@Bar(MISSING)})"); // ???
+ }
+
+ public void testNewSingleMemberAnnotation19() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=\"something\")");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation19(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation19(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation20() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation20(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation20(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewSingleMemberAnnotation21() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int value();");
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newSingleMemberAnnotation();
+ this.assertSourceContains("@annot.Foo(nestedAnnotations={@annot.NotBar,null,null,null,null,@Bar(MISSING)})");
+ }
+
+ public void testNewSingleMemberAnnotation22() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(77))");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation22(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewSingleMemberAnnotation22(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ SingleMemberAnnotation annotation = daa.newSingleMemberAnnotation(declaration);
+ NumberLiteral numberLiteral = annotation.getAST().newNumberLiteral();
+ numberLiteral.setToken("88");
+ annotation.setValue(numberLiteral);
+ }
+
+ public void testNewNormalAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation1(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(nestedAnnotations=@Bar(xxx=88))");
+ }
+
+ void editNewNormalAnnotation1(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation2(declaration);
+ }
+ });
+ this.assertSourceContains("@Foo(@Bar(xxx=88))");
+ }
+
+ void editNewNormalAnnotation2(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(nestedAnnotations=@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation3(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation3(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations();");
+ this.createTestType("@annot.Foo");
+ String expected = "@Foo(@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation4(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation4(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations(); String value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@Foo(value=\"something\", nestedAnnotations=@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation5(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation5(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ String expected = "@annot.Foo(@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation6(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation6(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations(); String xxx();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", nestedAnnotations = @Bar(xxx = 88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation7(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation7(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value(); String xxx();");
+ this.createTestType("@annot.Foo(xxx=\"something\")");
+ String expected = "@annot.Foo(xxx=\"something\", value = @Bar(xxx = 88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation8(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation8(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations(); String xxx();");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@Foo(nestedAnnotations={null,null,null,null,null,@Bar()})");
+ }
+
+ public void testNewNormalAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] nestedAnnotations(); String value();");
+ this.createTestType("@annot.Foo(\"something\")");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@Foo(value=\"something\", nestedAnnotations={null,null,null,null,null,@Bar()})");
+ }
+
+ public void testNewNormalAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ String expected = "@annot.Foo({@Bar(xxx=88), \"two\"})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation11(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation11(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo({\"one\", \"two\"})");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@Bar()})"); // split line
+ }
+
+ public void testNewNormalAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(7)");
+ String expected = "@annot.Foo(@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation13(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation13(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation14() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(7)");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@annot.Foo({7,null,null,null,null,@Bar()})");
+ }
+
+ public void testNewNormalAnnotation15() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int xxx();");
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] value();");
+ this.createTestType("@annot.Foo(@annot.NotBar)");
+ String expected = "@annot.Foo(@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation15(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation15(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation16() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(55))");
+ String expected = "@annot.Foo({@annot.Bar(55),@Bar(xxx=88)})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation16(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation16(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 1, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation17() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ String expected = "@annot.Foo(nestedAnnotations={@Bar(xxx=88)})";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation17(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation17(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation18() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations={\"something\"})");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@Bar()})"); // split line
+ }
+
+ public void testNewNormalAnnotation19() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=\"something\")");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation19(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation19(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation20() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int xxx();");
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation20(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation20(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testNewNormalAnnotation21() throws Exception {
+ this.createAnnotationAndMembers("NotBar", "int xxx();");
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.NotBar)");
+ this.assertSourceDoesNotContain("@Bar");
+
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 5, "annot.Bar");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newNormalAnnotation();
+ this.assertSourceContains("@annot.Foo(nestedAnnotations={@annot.NotBar,null,null,null,null,@Bar()})");
+ }
+
+ public void testNewNormalAnnotation22() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int xxx();");
+ this.createAnnotationAndMembers("Foo", "Object[] nestedAnnotations();");
+ this.createTestType("@annot.Foo(nestedAnnotations=@annot.Bar(77))");
+ String expected = "@annot.Foo(nestedAnnotations=@Bar(xxx=88))";
+ this.assertSourceDoesNotContain(expected);
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ NestedIndexedDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation22(declaration);
+ }
+ });
+ this.assertSourceContains(expected);
+ }
+
+ void editNewNormalAnnotation22(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "nestedAnnotations", 0, "annot.Bar");
+ NormalAnnotation annotation = daa.newNormalAnnotation(declaration);
+ this.addMemberValuePair(annotation, "xxx", 88);
+ }
+
+ public void testMoveAnnotation1() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(00))");
+ String expected = "@annot.Foo({null,@annot.Bar(00)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(1);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation2() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({null, @annot.Bar(11)})");
+ String expected = "@annot.Foo(@annot.Bar(11))";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 1, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation3() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22), @annot.Bar(33)})");
+ String expected = "@annot.Foo({@annot.Bar(33), @annot.Bar(11), @annot.Bar(22)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 3, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation4() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22), @annot.Bar(33), @annot.Bar(44)})");
+ String expected = "@annot.Foo({@annot.Bar(33), @annot.Bar(11), @annot.Bar(22), null, @annot.Bar(44)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 3, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation5() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22)})");
+ String expected = "@annot.Foo({@annot.Bar(00), @annot.Bar(11), null, @annot.Bar(22)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 2, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation6() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22)})");
+ String expected = "@annot.Foo({null, @annot.Bar(11), @annot.Bar(22), @annot.Bar(00)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation7() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22)})");
+ String expected = "@annot.Foo({null, @annot.Bar(11), @annot.Bar(22)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 3, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation8() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), @annot.Bar(22), null, @annot.Bar(44)})");
+ String expected = "@annot.Foo({null, @annot.Bar(11), @annot.Bar(22), null, @annot.Bar(44)})";
+ this.assertSourceDoesNotContain(expected);
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 3, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation9() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ String expected = "@annot.Foo({null, @annot.Bar(11), @annot.Bar(22)})";
+ this.createTestType(expected); // the source should be unchanged
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 0, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(3);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation10() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11)})");
+ String expected = "@annot.Foo(@annot.Bar(00))";
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 2, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+ iaa.moveAnnotation(1);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation11() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo(@annot.Bar(00))");
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 1, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceDoesNotContain("Foo");
+ }
+
+ public void testMoveAnnotation12() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), null, @annot.Bar(22)})");
+ String expected = "@annot.Foo(@annot.Bar(22))";
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 2, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+ public void testMoveAnnotation13() throws Exception {
+ this.createAnnotationAndMembers("Bar", "int value();");
+ this.createAnnotationAndMembers("Foo", "annot.Bar[] value();");
+ this.createTestType("@annot.Foo({@annot.Bar(00), @annot.Bar(11), null, @annot.Bar(33)})");
+ String expected = "@annot.Foo({@annot.Bar(33), @annot.Bar(11)})";
+
+ IndexedDeclarationAnnotationAdapter idaa = new NestedIndexedDeclarationAnnotationAdapter(
+ new SimpleDeclarationAnnotationAdapter("annot.Foo"), "value", 3, "annot.Bar");
+ IndexedAnnotationAdapter iaa = new MemberIndexedAnnotationAdapter(this.idField(), idaa);
+ Annotation annotation = iaa.getAnnotation();
+ assertNotNull(annotation);
+
+ iaa.moveAnnotation(0);
+ this.assertSourceContains(expected);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/SimpleDeclarationAnnotationAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/SimpleDeclarationAnnotationAdapterTests.java
new file mode 100644
index 0000000000..0e014b66b5
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/SimpleDeclarationAnnotationAdapterTests.java
@@ -0,0 +1,204 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.NumberLiteral;
+import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+import org.eclipse.jpt.core.internal.jdtutility.AnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.DeclarationAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.Member;
+import org.eclipse.jpt.core.internal.jdtutility.MemberAnnotationAdapter;
+import org.eclipse.jpt.core.internal.jdtutility.ModifiedDeclaration;
+import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
+
+public class SimpleDeclarationAnnotationAdapterTests extends AnnotationTestCase {
+
+ public SimpleDeclarationAnnotationAdapterTests(String name) {
+ super(name);
+ }
+
+ private void createAnnotation(String annotationName) throws Exception {
+ this.javaProject.createType("annot", annotationName + ".java", "public @interface " + annotationName + " {}");
+ }
+
+ public void testGetAnnotation1() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Foo", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+ }
+
+ public void testGetAnnotation2() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo(1) @annot.Foo(2)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("annot.Foo", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isSingleMemberAnnotation());
+ Expression value = ((SingleMemberAnnotation) annotation).getValue();
+ assertEquals(ASTNode.NUMBER_LITERAL, value.getNodeType());
+ assertEquals("1", ((NumberLiteral) value).getToken());
+ }
+
+ public void testGetAnnotation3() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("annot.Foo", "@Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+ assertEquals("Foo", annotation.getTypeName().getFullyQualifiedName());
+ assertTrue(annotation.isMarkerAnnotation());
+ }
+
+ public void testGetAnnotationNull1() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType();
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ }
+
+ public void testGetAnnotationNull2() throws Exception {
+ this.createAnnotation("Foo");
+ this.createAnnotation("Fop");
+ this.createTestType("@annot.Fop");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ this.assertSourceContains("@annot.Fop");
+ }
+
+ public void testGetAnnotationNull3() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo");
+ // un-qualified name
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+ this.assertSourceContains("@annot.Foo");
+ }
+
+ public void testRemoveAnnotation1() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo");
+ this.assertSourceContains("@annot.Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("@annot.Foo");
+ }
+
+ public void testRemoveAnnotation2() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo(1) @annot.Foo(2)");
+ this.assertSourceContains("@annot.Foo(1) @annot.Foo(2)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.removeAnnotation();
+ this.assertSourceDoesNotContain("@annot.Foo(1)");
+ this.assertSourceContains("@annot.Foo(2)");
+ }
+
+ public void testNewMarkerAnnotation1() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@annot.Foo");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("import annot.Foo;");
+ this.assertSourceContains("@Foo");
+ }
+
+ public void testNewMarkerAnnotation2() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType("@annot.Foo(88)");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ AnnotationAdapter aa = new MemberAnnotationAdapter(this.idField(), daa);
+ Annotation annotation = aa.getAnnotation();
+ assertNotNull(annotation);
+
+ aa.newMarkerAnnotation();
+ this.assertSourceContains("import annot.Foo;");
+ this.assertSourceContains("@Foo");
+ this.assertSourceDoesNotContain("@annot.Foo(88)");
+ }
+
+ public void testNewSingleMemberAnnotation() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ SimpleDeclarationAnnotationAdapterTests.this.editNewSingleMemberAnnotation(declaration);
+ }
+ });
+ this.assertSourceContains("import annot.Foo;");
+ this.assertSourceContains("@Foo(\"test string literal\")");
+ }
+
+ void editNewSingleMemberAnnotation(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ SingleMemberAnnotation annotation = (SingleMemberAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newSingleMemberAnnotation(declaration);
+ StringLiteral stringLiteral = annotation.getAST().newStringLiteral();
+ stringLiteral.setLiteralValue("test string literal");
+ annotation.setValue(stringLiteral);
+ }
+
+ public void testNewNormalAnnotation() throws Exception {
+ this.createAnnotation("Foo");
+ this.createTestType();
+ this.assertSourceDoesNotContain("@Foo");
+ this.idField().edit(new Member.Editor() {
+ public void edit(ModifiedDeclaration declaration) {
+ SimpleDeclarationAnnotationAdapterTests.this.editNewNormalAnnotation(declaration);
+ }
+ });
+ this.assertSourceContains("import annot.Foo;");
+ this.assertSourceContains("@Foo(bar=\"test string literal\")");
+ }
+
+ void editNewNormalAnnotation(ModifiedDeclaration declaration) {
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ NormalAnnotation annotation = (NormalAnnotation) daa.getAnnotation(declaration);
+ assertNull(annotation);
+ annotation = daa.newNormalAnnotation(declaration);
+ MemberValuePair mvp = this.newMemberValuePair(annotation.getAST(), "bar", "test string literal");
+ this.values(annotation).add(mvp);
+ }
+
+}
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
new file mode 100644
index 0000000000..360a9145dd
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/TypeTests.java
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * 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.jdtutility;
+
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jpt.core.internal.ITextRange;
+import org.eclipse.jpt.core.internal.jdtutility.Type;
+
+public class TypeTests extends AnnotationTestCase {
+
+ private IType jdtType;
+ private Type testType;
+
+
+ public TypeTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.jdtType = this.createTestType();
+ this.testType = new Type(this.jdtType);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ this.testType = null;
+ this.jdtType = null;
+ super.tearDown();
+ }
+
+ public void testJdtType() throws Exception {
+ assertEquals(this.jdtType, this.testType.jdtType());
+ }
+
+ public void testIsAbstract() throws Exception {
+ assertFalse(this.testType.isAbstract());
+ }
+
+ public void testTopLevelDeclaringType() throws Exception {
+ assertEquals(this.testType, this.testType.topLevelDeclaringType());
+ }
+
+ public void testGetDeclaringType() throws Exception {
+ assertNull(this.testType.getDeclaringType());
+ }
+
+ public void testGetName() throws Exception {
+ assertEquals(TYPE_NAME, this.testType.getName());
+ }
+
+ public void testTextRange() throws Exception {
+ String source = this.jdtType.getOpenable().getBuffer().getContents();
+ ITextRange textRange = this.testType.textRange();
+ String body = source.substring(textRange.getOffset());
+ assertTrue(body.startsWith("public class " + TYPE_NAME));
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaCoreModelTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaCoreModelTests.java
new file mode 100644
index 0000000000..be68ecc0e5
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/JpaCoreModelTests.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * 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.model;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class JpaCoreModelTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(JpaCoreModelTests.class.getPackage().getName());
+ suite.addTestSuite(ModelInitializationTests.class);
+ return suite;
+ }
+
+ private JpaCoreModelTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/ModelInitializationTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/ModelInitializationTests.java
new file mode 100644
index 0000000000..101703f0fc
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/model/ModelInitializationTests.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * 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.model;
+
+import junit.framework.TestCase;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jpt.core.internal.IJpaModel;
+import org.eclipse.jpt.core.internal.IJpaProject;
+import org.eclipse.jpt.core.internal.JpaCorePlugin;
+import org.eclipse.jpt.core.tests.internal.ProjectUtility;
+import org.eclipse.jpt.core.tests.internal.projects.TestFacetedProject;
+import org.eclipse.jpt.utility.internal.ClassTools;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+
+public class ModelInitializationTests extends TestCase {
+ private IJpaModel jpaModel;
+
+
+ public ModelInitializationTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ ProjectUtility.deleteAllProjects();
+ this.jpaModel = JpaCorePlugin.getJpaModel();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ ProjectUtility.deleteAllProjects();
+ this.jpaModel = null;
+ super.tearDown();
+ }
+
+ /**
+ * Builds a project with the java and utility facets installed, and with
+ * pre-existing entities added.
+ */
+ private TestFacetedProject buildTestProject() throws CoreException {
+ TestFacetedProject testProject = new TestFacetedProject(ClassTools.shortClassNameForObject(this), true);
+ testProject.installFacet("jst.java", "5.0");
+ testProject.installFacet("jst.utility", "1.0");
+ testProject.createFile(
+ new Path("src/test.pkg/TestEntity.java"),
+ "package test.pkg; @Entity public class TestEntity {}");
+ testProject.createFile(
+ new Path("src/test.pkg/TestEntity2.java"),
+ "package test.pkg; @Entity public class TestEntity2 {}");
+ return testProject;
+ }
+
+ public void testBasic() {
+ assertNotNull(this.jpaModel);
+ }
+
+ public void testFacetInstallation() throws CoreException {
+ TestFacetedProject testProject = buildTestProject();
+ assertNull(this.jpaModel.getJpaProject(testProject.getProject()));
+ testProject.installFacet("jpt.jpa", "1.0");
+ assertEquals(1, CollectionTools.size(this.jpaModel.jpaProjects()));
+ IJpaProject jpaProject = this.jpaModel.getJpaProject(testProject.getProject());
+ assertNotNull(jpaProject);
+ assertEquals(4, CollectionTools.size(jpaProject.jpaFiles()));
+ assertNotNull(jpaProject.getJpaFile(testProject.getProject().getFile(new Path("src/test.pkg/TestEntity.java"))));
+ assertNotNull(jpaProject.getJpaFile(testProject.getProject().getFile(new Path("src/test.pkg/TestEntity2.java"))));
+ }
+
+ public void testProjectOpening() throws CoreException {
+ TestFacetedProject testProject = buildTestProject();
+ testProject.installFacet("jpt.jpa", "1.0");
+ testProject.close();
+ assertTrue(! testProject.getProject().isOpen());
+ testProject.open();
+ IJpaProject jpaProject = this.jpaModel.getJpaProject(testProject.getProject());
+ assertNotNull(jpaProject);
+ assertNotNull(jpaProject.getJpaFile(testProject.getProject().getFile(new Path("src/test.pkg/TestEntity.java"))));
+ assertNotNull(jpaProject.getJpaFile(testProject.getProject().getFile(new Path("src/test.pkg/TestEntity2.java"))));
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestFacetedProject.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestFacetedProject.java
new file mode 100644
index 0000000000..6b638fa21b
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestFacetedProject.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.projects;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.wst.common.project.facet.core.IFacetedProject;
+import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
+import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
+
+public class TestFacetedProject extends TestPlatformProject {
+ private IFacetedProject facetedProject;
+
+
+ // ********** constructors/initialization **********
+
+ public TestFacetedProject(String projectName) throws CoreException {
+ this(projectName, true);
+ }
+
+ public TestFacetedProject(String projectName, boolean autoBuild) throws CoreException {
+ super(projectName, autoBuild);
+ this.facetedProject = this.createFacetedProject();
+ }
+
+ private IFacetedProject createFacetedProject() throws CoreException {
+ return ProjectFacetsManager.create(this.getProject(), true, null); // true = "convert if necessary"
+ }
+
+
+ // ********** public methods **********
+
+ public IFacetedProject getFacetedProject() {
+ return this.facetedProject;
+ }
+
+ public void installFacet(String facetName, String versionName) throws CoreException {
+ IProjectFacetVersion facetVersion = ProjectFacetsManager.getProjectFacet(facetName).getVersion(versionName);
+ this.facetedProject.installProjectFacet(facetVersion, null, null);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJavaProject.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJavaProject.java
new file mode 100644
index 0000000000..a19529b0e1
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJavaProject.java
@@ -0,0 +1,148 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.projects;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.search.IJavaSearchConstants;
+import org.eclipse.jdt.core.search.SearchEngine;
+import org.eclipse.jdt.core.search.SearchPattern;
+import org.eclipse.jdt.core.search.TypeNameRequestor;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+
+/**
+ * This builds and holds a "Java" project.
+ * Support for adding packages and types.
+ */
+public class TestJavaProject extends TestFacetedProject {
+
+ private IJavaProject javaProject;
+ private IPackageFragmentRoot sourceFolder;
+
+
+ // ********** constructors/initialization **********
+
+ public TestJavaProject() throws CoreException {
+ this("TestJavaProject");
+ }
+
+ public TestJavaProject(String projectName) throws CoreException {
+ this(projectName, false);
+ }
+
+ public TestJavaProject(String projectName, boolean autoBuild) throws CoreException {
+ super(projectName, autoBuild);
+ this.installFacet("jst.java", "5.0");
+ this.javaProject = JavaCore.create(this.getProject());
+ this.sourceFolder = this.javaProject.getPackageFragmentRoot(this.getProject().getFolder("src"));
+ }
+
+ protected void addJar(String jarPath) throws JavaModelException {
+ this.addClasspathEntry(JavaCore.newLibraryEntry(new Path(jarPath), null, null));
+ }
+
+ private void addClasspathEntry(IClasspathEntry entry) throws JavaModelException {
+ this.javaProject.setRawClasspath(CollectionTools.add(this.javaProject.getRawClasspath(), entry), null);
+ }
+
+
+ // ********** public methods **********
+
+ public IPackageFragment createPackage(String packageName) throws CoreException {
+ return this.sourceFolder.createPackageFragment(packageName, false, null); // false = "no force"
+ }
+
+ /**
+ * The source should NOT contain a package declaration;
+ * it will be added here.
+ */
+ public IType createType(String packageName, String compilationUnitName, String source) throws CoreException {
+ return this.createType(this.createPackage(packageName), compilationUnitName, new SimpleSourceWriter(source));
+ }
+
+ /**
+ * The source should NOT contain a package declaration;
+ * it will be added here.
+ */
+ public IType createType(String packageName, String compilationUnitName, SourceWriter sourceWriter) throws CoreException {
+ return this.createType(this.createPackage(packageName), compilationUnitName, sourceWriter);
+ }
+
+ /**
+ * The source should NOT contain a package declaration;
+ * it will be added here.
+ */
+ public IType createType(IPackageFragment packageFragment, String compilationUnitName, SourceWriter sourceWriter) throws CoreException {
+ StringBuffer sb = new StringBuffer(2000);
+ sb.append("package ").append(packageFragment.getElementName()).append(";").append(CR);
+ sb.append(CR);
+ sourceWriter.appendSourceTo(sb);
+ String source = sb.toString();
+ ICompilationUnit cu = packageFragment.createCompilationUnit(compilationUnitName, source, false, null); // false = "no force"
+ return cu.findPrimaryType();
+ }
+
+ public IType findType(String fullyQualifiedName) throws JavaModelException {
+ return this.javaProject.findType(fullyQualifiedName);
+ }
+
+ @Override
+ public void dispose() throws CoreException {
+ this.waitForIndexer();
+ this.sourceFolder = null;
+ this.javaProject = null;
+ super.dispose();
+ }
+
+
+ // ********** internal methods **********
+
+ private void waitForIndexer() throws JavaModelException {
+ new SearchEngine().searchAllTypeNames(
+ null,
+ SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
+ null,
+ SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
+ IJavaSearchConstants.CLASS,
+ SearchEngine.createJavaSearchScope(new IJavaElement[0]),
+ new TypeNameRequestor() {/* do nothing */},
+ IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
+ null
+ );
+ }
+
+
+ // ********** member classes **********
+
+ public interface SourceWriter {
+ void appendSourceTo(StringBuffer sb);
+ }
+
+ public class SimpleSourceWriter implements SourceWriter {
+ private final String source;
+ public SimpleSourceWriter(String source) {
+ super();
+ this.source = source;
+ }
+ public void appendSourceTo(StringBuffer sb) {
+ sb.append(source);
+ }
+ }
+
+}
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
new file mode 100644
index 0000000000..2c58c045dc
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestJpaProject.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.projects;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jpt.core.internal.IJpaProject;
+import org.eclipse.jpt.core.internal.JpaCorePlugin;
+
+public class TestJpaProject extends TestJavaProject {
+ private IJpaProject jpaProject;
+
+ public static final String JAR_NAME_SYSTEM_PROPERTY = "org.eclipse.jpt.jpa.jar";
+
+ // ********** constructors/initialization **********
+
+ public TestJpaProject() throws CoreException {
+ this("TestJpaProject");
+ }
+
+ public TestJpaProject(String projectName) throws CoreException {
+ this(projectName, false);
+ }
+
+ public TestJpaProject(String projectName, boolean autoBuild) throws CoreException {
+ super(projectName, autoBuild);
+ this.installFacet("jst.utility", "1.0");
+ this.installFacet("jpt.jpa", "1.0");
+ this.addJar(this.jarName());
+ this.jpaProject = JpaCorePlugin.getJpaProject(this.getProject());
+ }
+
+ protected String jarName() {
+ String jarName = System.getProperty(JAR_NAME_SYSTEM_PROPERTY);
+ if (jarName == null) {
+ throw new RuntimeException("missing Java system property: \"" + JAR_NAME_SYSTEM_PROPERTY + "\"");
+ }
+ return jarName;
+ }
+
+
+ // ********** public methods **********
+
+ public IJpaProject getJpaProject() {
+ return this.jpaProject;
+ }
+
+ @Override
+ public void dispose() throws CoreException {
+ this.jpaProject = null;
+ super.dispose();
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestPlatformProject.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestPlatformProject.java
new file mode 100644
index 0000000000..7fbca7957d
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/projects/TestPlatformProject.java
@@ -0,0 +1,195 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.projects;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IWorkspaceDescription;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+
+/**
+ * This builds and holds a "general" project.
+ * Support for adding natures, folders, and files.
+ */
+public class TestPlatformProject {
+
+ private final IProject project;
+
+ /** carriage return */
+ public static final String CR = System.getProperty("line.separator");
+
+
+ // ********** constructors/initialization **********
+
+ public TestPlatformProject() throws CoreException {
+ this(false);
+ }
+
+ public TestPlatformProject(boolean autoBuild) throws CoreException {
+ this("TestProject", autoBuild);
+ }
+
+ public TestPlatformProject(String projectName, boolean autoBuild) throws CoreException {
+ super();
+ this.setAutoBuild(autoBuild); // workspace-wide setting
+ this.project = this.createPlatformProject(projectName);
+ }
+
+ private void setAutoBuild(boolean autoBuild) throws CoreException {
+ IWorkspaceDescription description = ResourcesPlugin.getWorkspace().getDescription();
+ description.setAutoBuilding(autoBuild);
+ ResourcesPlugin.getWorkspace().setDescription(description);
+ }
+
+ private IProject createPlatformProject(String projectName) throws CoreException {
+ IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ p.create(null);
+ p.open(null);
+ return p;
+ }
+
+
+ // ********** public methods **********
+
+ public IProject getProject() {
+ return this.project;
+ }
+
+ public void addProjectNature(String natureID) throws CoreException {
+ IProjectDescription description = this.project.getDescription();
+ description.setNatureIds((String[]) CollectionTools.add(description.getNatureIds(), natureID));
+ this.project.setDescription(description, null);
+ }
+
+ public void removeProjectNature(String natureID) throws CoreException {
+ IProjectDescription description = this.project.getDescription();
+ description.setNatureIds((String[]) CollectionTools.removeAllOccurrences(description.getNatureIds(), natureID));
+ this.project.setDescription(description, null);
+ }
+
+ /**
+ * Create a folder with the specified name directly under the project.
+ */
+ public IFolder createFolder(String folderName) throws CoreException {
+ return this.createFolder(this.project, new Path(folderName));
+ }
+
+ /**
+ * Create a folder in the specified container with the specified name.
+ */
+ public IFolder createFolder(IContainer container, String folderName) throws CoreException {
+ return this.createFolder(container, new Path(folderName));
+ }
+
+ /**
+ * Create a folder in the specified container with the specified path.
+ */
+ public IFolder createFolder(IContainer container, IPath folderPath) throws CoreException {
+ IFolder folder = container.getFolder(folderPath);
+ if ( ! folder.exists()) {
+ folder.create(false, true, null); // false = "no force"; true = "local"
+ }
+ return folder;
+ }
+
+ /**
+ * Create a file with the specified name and content directly under the project.
+ */
+ public IFile createFile(String fileName, String content) throws CoreException {
+ return this.createFile(this.project, fileName, content);
+ }
+
+ /**
+ * Create a file in the specified container with the specified name and content.
+ */
+ public IFile createFile(IContainer container, String fileName, String content) throws CoreException {
+ return createFile(container, new Path(fileName), content);
+ }
+
+ /**
+ * Create a file in the project with the specified [relative] path
+ * and content.
+ */
+ public IFile createFile(IPath filePath, String content) throws CoreException {
+ return this.createFile(this.project, filePath, content);
+ }
+
+ /**
+ * Create a file in the specified container with the specified path and content.
+ */
+ public IFile createFile(IContainer container, IPath filePath, String content) throws CoreException {
+ return this.createFile(container, filePath, new ByteArrayInputStream(content.getBytes()));
+ }
+
+ /**
+ * Create a file in the specified container with the specified path and contents.
+ */
+ public IFile createFile(IContainer container, IPath filePath, InputStream content) throws CoreException {
+ int len = filePath.segmentCount() - 1;
+ for (int i = 0; i < len; i++) {
+ container = container.getFolder(new Path(filePath.segment(i)));
+ if ( ! container.exists()) {
+ ((IFolder) container).create(true, true, null); // true = "force"; true = "local"
+ }
+ }
+
+ IFile file = container.getFile(new Path(filePath.lastSegment()));
+ if (file.exists()) {
+ file.delete(true, null); // true = "force"
+ }
+ file.create(content, true, null); // true = "force"
+ return file;
+ }
+
+ public void open() throws CoreException {
+ this.project.open(null);
+ waitForJobs();
+ }
+
+ public void close() throws CoreException {
+ this.project.close(null);
+ waitForJobs();
+ }
+
+ public void dispose() throws CoreException {
+ this.project.delete(true, true, null); // true = "delete content"; true = "force"
+ for (int i = 1; this.project.exists(); i++) {
+ waitForJobs();
+ System.out.println("Project still exists: " + i);
+ }
+ }
+
+
+ // ********** static methods **********
+
+ /**
+ * Wait until all background tasks are complete.
+ */
+ public static void waitForJobs() {
+ while (Job.getJobManager().currentJob() != null) {
+ try {
+ Thread.sleep(100); // let other threads get something done
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/test.xml b/jpa/tests/org.eclipse.jpt.core.tests/test.xml
new file mode 100644
index 0000000000..865d9aad8f
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.core.tests/test.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+<project name="testsuite" default="run" basedir=".">
+ <!-- The property ${eclipse-home} should be passed into this script -->
+ <!-- Set a meaningful default value for when it is not. -->
+ <echo message="basedir ${basedir}" />
+ <echo message="eclipse place ${eclipse-home}" />
+ <!-- sets the properties plugin-name, and library-file -->
+ <property name="plugin-name" value="org.eclipse.jpt.core.tests"/>
+ <property name="library-file" value="${eclipse-home}/plugins/org.eclipse.test_3.1.0/library.xml"/>
+
+ <!-- This target holds all initialization code that needs to be done for -->
+ <!-- all tests that are to be run. Initialization for individual tests -->
+ <!-- should be done within the body of the suite target. -->
+ <target name="init">
+ <tstamp/>
+ <delete>
+ <fileset dir="${eclipse-home}" includes="org*.xml"/>
+ </delete>
+ </target>
+
+ <!-- This target defines the tests that need to be run. -->
+ <target name="suite">
+ <ant target="core-test" antfile="${library-file}" dir="${eclipse-home}">
+ <property name="plugin-name" value="${plugin-name}"/>
+ <property name="classname" value="org.eclipse.jpt.core.tests.internal.JpaCoreTests" />
+ <property name="plugin-path" value="${eclipse-home}/plugins/${plugin-name}"/>
+ </ant>
+ </target>
+
+ <!-- This target holds code to cleanup the testing environment after -->
+ <!-- after all of the tests have been run. You can use this target to -->
+ <!-- delete temporary files that have been created. -->
+ <target name="cleanup">
+ </target>
+
+ <!-- This target runs the test suite. Any actions that need to happen -->
+ <!-- after all the tests have been run should go here. -->
+ <target name="run" depends="init,suite,cleanup">
+ <ant target="collect" antfile="${library-file}" dir="${eclipse-home}">
+ <property name="includes" value="org*.xml"/>
+ <property name="output-file" value="${plugin-name}.xml"/>
+ </ant>
+ </target>
+</project> \ No newline at end of file
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/.classpath b/jpa/tests/org.eclipse.jpt.db.tests/.classpath
new file mode 100644
index 0000000000..751c8f2e50
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/.cvsignore b/jpa/tests/org.eclipse.jpt.db.tests/.cvsignore
new file mode 100644
index 0000000000..ba077a4031
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/.cvsignore
@@ -0,0 +1 @@
+bin
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/.project b/jpa/tests/org.eclipse.jpt.db.tests/.project
new file mode 100644
index 0000000000..f5b0127a1e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.jpt.db.tests</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/META-INF/MANIFEST.MF b/jpa/tests/org.eclipse.jpt.db.tests/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..2d889ce36a
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/META-INF/MANIFEST.MF
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %pluginName
+Bundle-Vendor: %providerName
+Bundle-SymbolicName: org.eclipse.jpt.db.tests
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.eclipse.jpt.db.tests.internal.DbTestPlugin
+Bundle-Localization: plugin
+Eclipse-LazyStart: false
+Require-Bundle: org.eclipse.core.runtime,
+ org.junit,
+ org.eclipse.datatools.connectivity,
+ org.eclipse.datatools.connectivity.db.generic,
+ org.eclipse.jpt.utility,
+ org.eclipse.jpt.db
+Export-Package: org.eclipse.jpt.db.tests.internal.platforms
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/build.properties b/jpa/tests/org.eclipse.jpt.db.tests/build.properties
new file mode 100644
index 0000000000..37d9849a17
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/build.properties
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = .,\
+ META-INF/,\
+ plugin.properties
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/config/derby101.properties b/jpa/tests/org.eclipse.jpt.db.tests/config/derby101.properties
new file mode 100644
index 0000000000..75c6b5a570
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/config/derby101.properties
@@ -0,0 +1,20 @@
+# Platform Property names & Default
+#
+# profileName - jpatest
+# profileDescription - JDBC Profile for JPA Testing
+# userName - jpatest
+# userPassword - (empty)
+# databasedriverJarList - (mandatory)
+# databaseName - testdb
+# databaseUrl - (mandatory)
+#
+profileName = derby101
+profileDescription = Derby Embedded JDBC Profile for Testing
+# set user name
+userName = jpatest
+userPassword =
+# set path to Derby driver here (example: databasedriverJarList = c:/derbydb/lib/derby.jar )
+databasedriverJarList =
+databaseName = testdb
+# set database URL here (example: databaseUrl = jdbc:derby:c:/derbydb/testdb;create=true )
+databaseUrl =
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/config/oracle10g.properties b/jpa/tests/org.eclipse.jpt.db.tests/config/oracle10g.properties
new file mode 100644
index 0000000000..7f3219321e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/config/oracle10g.properties
@@ -0,0 +1,19 @@
+# Platform Property names & Default
+#
+# profileName - jpatest
+# profileDescription - JDBC Profile for JPA Testing
+# userName - jpatest
+# userPassword - (empty)
+# databasedriverJarList - jar list separate by comma (mandatory)
+# databaseName - testdb
+# databaseUrl - (mandatory)
+#
+profileName = oracleXE10g
+profileDescription = XE 10g Release 2 (10.2) Thin JDBC Profile for Testing
+userName = scott
+userPassword = tiger
+# set path to Oracle driver here (example: databasedriverJarList = c:/oracle/jdbc/lib/ojdbc14.jar )
+databasedriverJarList =
+databaseName = testdb
+# set database URL here (example: databaseUrl = jdbc:oracle:thin:@localhost:1521:testdb )
+databaseUrl =
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/config/oracle9i.properties b/jpa/tests/org.eclipse.jpt.db.tests/config/oracle9i.properties
new file mode 100644
index 0000000000..93f7da5c05
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/config/oracle9i.properties
@@ -0,0 +1,19 @@
+# Platform Property names & Default
+#
+# profileName - jpatest
+# profileDescription - JDBC Profile for JPA Testing
+# userName - jpatest
+# userPassword - (empty)
+# databasedriverJarList - jar list separate by comma (mandatory)
+# databaseName - testdb
+# databaseUrl - (mandatory)
+#
+profileName = oracle9i
+profileDescription = Oracle Thin JDBC Profile for Testing
+userName = scott
+userPassword = tiger
+# set path to Oracle driver here (example: databasedriverJarList = c:/oracle/jdbc/lib/ojdbc14.jar )
+databasedriverJarList =
+databaseName = testdb
+# set database URL here (example: databaseUrl = jdbc:oracle:thin:@server:1521:testdb )
+databaseUrl =
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/config/sqlserver2005.properties b/jpa/tests/org.eclipse.jpt.db.tests/config/sqlserver2005.properties
new file mode 100644
index 0000000000..24312fcedb
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/config/sqlserver2005.properties
@@ -0,0 +1,20 @@
+# Platform Property names & Default
+#
+# profileName - jpatest
+# profileDescription - JDBC Profile for JPA Testing
+# userName - jpatest
+# userPassword - (empty)
+# databasedriverJarList - (mandatory)
+# databaseName - testdb
+# databaseUrl - (mandatory)
+#
+profileName = sqlserver2005
+profileDescription = MS SQL Server 2005 JDBC Profile for Testing
+# set user name
+userName =
+userPassword =
+# set path to Derby driver here (example: databasedriverJarList = c:/sqlserver/lib/sqljdbc.jar )
+databasedriverJarList =
+databaseName = testdb
+# set database URL here (example: databaseUrl = jdbc:sqlserver://localhost:1433;databaseName=testdb )
+databaseUrl =
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/plugin.properties b/jpa/tests/org.eclipse.jpt.db.tests/plugin.properties
new file mode 100644
index 0000000000..124f3b2de7
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/plugin.properties
@@ -0,0 +1,15 @@
+# ====================================================================
+# To code developer:
+# Do NOT change the properties between this line and the
+# "%%% END OF TRANSLATED PROPERTIES %%%" line.
+# Make a new property name, append to the end of the file and change
+# the code to use the new property.
+# ====================================================================
+
+# ====================================================================
+# %%% END OF TRANSLATED PROPERTIES %%%
+# ====================================================================
+
+pluginName = JPA DB Tests
+providerName = Eclipse.org
+
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/DbTestPlugin.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/DbTestPlugin.java
new file mode 100644
index 0000000000..39d0a48cb4
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/DbTestPlugin.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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.db.tests.internal;
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * DbTestPlugin
+ */
+public class DbTestPlugin extends Plugin {
+
+ // The shared instance
+ private static DbTestPlugin plugin;
+
+ public static final String BUNDLE_ID = "org.eclipse.jpt.db.tests"; //$NON-NLS-1$
+
+ /**
+ * Returns the shared instance
+ */
+ public static DbTestPlugin getDefault() {
+ return plugin;
+ }
+
+ /**
+ * The constructor
+ */
+ public DbTestPlugin() {
+ super();
+ plugin = this;
+ }
+
+ /**
+ * This method is called upon plug-in activation
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ }
+
+ /**
+ * This method is called when the plug-in is stopped
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JpaDbTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JpaDbTests.java
new file mode 100644
index 0000000000..dd1dcd4393
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JpaDbTests.java
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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.db.tests.internal;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+import org.eclipse.jpt.db.tests.internal.platforms.AllPlatformTests;
+import org.eclipse.jpt.utility.internal.ClassTools;
+
+/**
+ * JpaDbTests
+ */
+public class JpaDbTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", JpaDbTests.class.getName()});
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite( ClassTools.packageNameFor( JpaDbTests.class));
+
+ suite.addTest( AllPlatformTests.suite());
+
+ return suite;
+ }
+
+ private JpaDbTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+} \ No newline at end of file
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/AllPlatformTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/AllPlatformTests.java
new file mode 100644
index 0000000000..af9c4233cc
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/AllPlatformTests.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * 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.db.tests.internal.platforms;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+import org.eclipse.jpt.utility.internal.ClassTools;
+
+/**
+ * AllPlatformTests
+ */
+public class AllPlatformTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", AllPlatformTests.class.getName()});
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite( ClassTools.packageNameFor( AllPlatformTests.class));
+
+ // TODO - Uncomment the platform to test.
+// suite.addTest( Derby101Tests.suite());
+// suite.addTest( Oracle9iTests.suite());
+// suite.addTest( Oracle10gTests.suite());
+// suite.addTest( SQLServer2005Tests.suite());
+
+ return suite;
+ }
+
+ private AllPlatformTests() {
+ super();
+ }
+
+} \ No newline at end of file
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java
new file mode 100644
index 0000000000..baccb6965e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java
@@ -0,0 +1,385 @@
+/*******************************************************************************
+ * 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.db.tests.internal.platforms;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+import java.util.Properties;
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.datatools.connectivity.ConnectionProfileException;
+import org.eclipse.datatools.connectivity.IConnectionProfile;
+import org.eclipse.datatools.connectivity.ProfileManager;
+import org.eclipse.datatools.connectivity.db.generic.IDBDriverDefinitionConstants;
+import org.eclipse.datatools.connectivity.drivers.IDriverMgmtConstants;
+import org.eclipse.datatools.connectivity.drivers.IPropertySet;
+import org.eclipse.datatools.connectivity.drivers.PropertySetImpl;
+import org.eclipse.datatools.connectivity.drivers.XMLFileManager;
+import org.eclipse.datatools.connectivity.internal.ConnectivityPlugin;
+import org.eclipse.jpt.db.internal.Connection;
+import org.eclipse.jpt.db.internal.ConnectionProfile;
+import org.eclipse.jpt.db.internal.ConnectionProfileRepository;
+import org.eclipse.jpt.db.internal.DTPConnectionProfileWrapper;
+import org.eclipse.jpt.db.internal.Database;
+import org.eclipse.jpt.db.internal.Schema;
+import org.eclipse.jpt.db.internal.Table;
+import org.eclipse.jpt.db.tests.internal.DbTestPlugin;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.StringTools;
+
+/**
+ * Base class for all supported DTP platform.
+ */
+public abstract class DTPPlatformTests extends TestCase {
+
+ protected ConnectionProfileRepository connectionRepository;
+
+ private Properties platformProperties;
+ private String currentDbVendor;
+ private String currentDbVersion;
+
+ private static final String PLATFORM_CONFIG_DIRECTORY = "config"; //$NON-NLS-1$
+
+ private static final String PROFILE_NAME_PROPERTY = "profileName"; //$NON-NLS-1$
+ private static final String PROFILE_DESCRIPTION_PROPERTY = "profileDescription"; //$NON-NLS-1$
+ private static final String USER_NAME_PROPERTY = "userName"; //$NON-NLS-1$
+ private static final String USER_PASSWORD_PROPERTY = "userPassword"; //$NON-NLS-1$
+ private static final String DB_DRIVER_JAR_PROPERTY = "databasedriverJarList"; //$NON-NLS-1$
+ private static final String DB_NAME_PROPERTY = "databaseName"; //$NON-NLS-1$
+ private static final String DB_URL_PROPERTY = "databaseUrl"; //$NON-NLS-1$
+
+ private static final String PROFILE_NAME_DEFAULT = "jpatest"; //$NON-NLS-1$
+ private static final String PROFILE_DESCRIPTION_DEFAULT = "JDBC Profile for JPA Testing"; //$NON-NLS-1$
+ private static final String USER_NAME_DEFAULT = "userName"; //$NON-NLS-1$
+ private static final String USER_PASSWORD_DEFAULT = ""; //$NON-NLS-1$
+ private static final String DB_NAME_DEFAULT = "testdb"; //$NON-NLS-1$
+
+ private static final String MISSING_PROPERTY_MESSAGE = "Enter missing property in platform config file (config/database.properties)";
+
+ public DTPPlatformTests( String name) {
+ super( name);
+
+ this.connectionRepository = ConnectionProfileRepository.instance();
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.connectionRepository.initializeListeners();
+
+ if( this.platformIsNew()) {
+ this.loadPlatformProperties();
+
+ this.buildDriverDefinitionFile( IDriverMgmtConstants.DRIVER_FILE);
+ this.buildConnectionProfile( this.profileName());
+ this.currentDbVendor = this.databaseVendor();
+ this.currentDbVersion = this.databaseVersion();
+ }
+ this.verifyProfileNamed( this.profileName());
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ this.connectionRepository.disposeListeners();
+ }
+
+ // ********** tests **********
+
+ public void testConnection() throws Exception {
+
+ this.connect();
+
+ this.verifyDatabaseVersionNumber();
+ this.verifyConnection();
+ this.verifyDatabaseContent();
+
+ this.disconnect();
+ }
+
+ public void testGetProfiles() {
+ // Getting the profile directly from DTP ProfileManager
+ IConnectionProfile[] profiles = ProfileManager.getInstance().getProfiles();
+ Assert.assertNotNull( profiles);
+ Assert.assertTrue( profiles.length > 0);
+ }
+
+ public void testGetProfilesByProvider() {
+ // Get Profiles By ProviderID
+ IConnectionProfile[] profiles = ProfileManager.getInstance().getProfileByProviderID( this.getProfile().getProviderId());
+ Assert.assertNotNull( profiles);
+ Assert.assertTrue( profiles.length > 0);
+ }
+
+ public void testGetProfileByName() {
+ // Get Profile By Name
+ IConnectionProfile dtpProfile = ProfileManager.getInstance().getProfileByName( this.profileName());
+ Assert.assertNotNull( dtpProfile);
+ Assert.assertTrue( dtpProfile.getName().equals( this.profileName()));
+ }
+
+ public void testGetProfileByInstanceId() {
+ // Get Profile By InstanceID
+ IConnectionProfile dtpProfile = ProfileManager.getInstance().getProfileByInstanceID( this.getProfile().getInstanceId());
+ Assert.assertNotNull( dtpProfile);
+ Assert.assertTrue( dtpProfile.getName().equals( this.profileName()));
+ }
+
+ // ********** internal tests **********
+
+ private void verifyDatabaseVersionNumber() {
+ Database database = this.getProfile().getDatabase();
+ Assert.assertNotNull( database);
+
+ String actualVersionNumber = database.getVersion();
+ String expectedVersionNumber = this.databaseVersion();
+ String errorMessage = "Expected version number: " + expectedVersionNumber + " but the actual version number was: " + actualVersionNumber;
+ assertTrue( errorMessage, actualVersionNumber.indexOf( expectedVersionNumber) != -1);
+
+ String actualVendor = database.getVendor();
+ String expectedVendor = this.databaseVendor();
+ errorMessage = "Expected vendor: " + expectedVendor + " but the actual vendor was: " + actualVendor;
+ assertEquals( errorMessage, actualVendor, expectedVendor);
+ }
+
+ private void verifyConnection() {
+ Connection connection = this.getProfile().getConnection();
+ Assert.assertNotNull( connection);
+
+ String actualFactory = connection.getFactoryId();
+ String expectedFactory = DTPConnectionProfileWrapper.CONNECTION_TYPE;
+ String errorMessage = "Expected factory: " + expectedFactory + " but the actual factory was: " + actualFactory;
+ assertEquals( errorMessage, actualFactory, expectedFactory);
+ }
+
+ private void verifyDatabaseContent() {
+ Database database = this.getProfile().getDatabase();
+ Assert.assertTrue( database.schemataSize() > 0);
+
+ Schema schema = database.schemaNamed( this.getProfile().getUserName());
+ if( schema != null) {
+ Assert.assertTrue( schema.sequencesSize() >= 0);
+
+ Object[] tableNames = CollectionTools.array( schema.tableNames());
+ if( tableNames.length >= 1) {
+ Table table = schema.tableNamed(( String)tableNames[ 0]);
+ Assert.assertTrue( table.columnsSize() >= 0);
+ Assert.assertTrue( table.foreignKeyColumnsSize() >= 0);
+ Assert.assertTrue( table.foreignKeysSize() >= 0);
+ Assert.assertTrue( table.primaryKeyColumnsSize() >= 0);
+ }
+ }
+ }
+
+ private void verifyProfileNamed( String profileName) {
+
+ ConnectionProfile profile = this.getProfileNamed( profileName);
+ Assert.assertTrue( "ConnectionProfile not found", profileName.equals( profile.getName()));
+ }
+
+ // ***** Platform specific behavior *****
+
+ protected abstract String databaseVendor();
+ protected abstract String databaseVersion();
+
+ protected abstract String driverName();
+ protected abstract String driverDefinitionType();
+ protected abstract String driverDefinitionId();
+ protected abstract String driverClass();
+
+ protected abstract String getConfigName();
+
+ // ***** Behavior *****
+
+ protected void connect() {
+
+ this.getProfile().connect();
+ Assert.assertTrue( "Connect failed.", this.getProfile().isConnected());
+ }
+
+ protected void disconnect() {
+
+ this.getProfile().disconnect();
+ Assert.assertFalse( "Disconnect failed.", this.getProfile().isConnected());
+ }
+
+ // ********** queries **********
+
+ protected Schema getSchemaNamed( String schemaName) {
+
+ return this.getProfile().getDatabase().schemaNamed( schemaName);
+ }
+
+ protected Collection getTables() {
+
+ Schema schema = this.getSchemaNamed( this.getProfile().getUserName());
+ if( schema == null) {
+ return new ArrayList();
+ }
+ return CollectionTools.collection( schema.tables());
+ }
+
+ protected Table getTableNamed( String tableName) {
+
+ Schema schema = this.getSchemaNamed( this.getProfile().getUserName());
+ Assert.assertNotNull( schema);
+
+ return schema.tableNamed( tableName);
+ }
+
+ protected String providerId() {
+ return DTPConnectionProfileWrapper.CONNECTION_PROFILE_TYPE;
+ }
+
+ protected String passwordIsSaved() {
+ return "true";
+ }
+
+ protected String profileName() {
+ return this.platformProperties.getProperty( PROFILE_NAME_PROPERTY, PROFILE_NAME_DEFAULT);
+ }
+
+ protected String profileDescription() {
+ return this.platformProperties.getProperty( PROFILE_DESCRIPTION_PROPERTY, PROFILE_DESCRIPTION_DEFAULT);
+ }
+
+ protected String userName() {
+ return this.platformProperties.getProperty( USER_NAME_PROPERTY, USER_NAME_DEFAULT);
+ }
+
+ protected String userPassword() {
+ return this.platformProperties.getProperty( USER_PASSWORD_PROPERTY, USER_PASSWORD_DEFAULT);
+ }
+
+ protected String databaseName() {
+ return this.platformProperties.getProperty( DB_NAME_PROPERTY, DB_NAME_DEFAULT);
+ }
+
+ protected String databasedriverJarList() {
+ String dbDriverJarList = this.platformProperties.getProperty( DB_DRIVER_JAR_PROPERTY);
+ if ( StringTools.stringIsEmpty( dbDriverJarList)) {
+ throw new NoSuchElementException( MISSING_PROPERTY_MESSAGE);
+ }
+ return dbDriverJarList;
+ }
+
+ protected String databaseUrl() {
+ String dbUrl = this.platformProperties.getProperty( DB_URL_PROPERTY);
+ if ( StringTools.stringIsEmpty( dbUrl)) {
+ throw new NoSuchElementException( MISSING_PROPERTY_MESSAGE);
+ }
+ return dbUrl;
+ }
+
+ private ConnectionProfile getProfile() {
+
+ return this.getProfileNamed( this.profileName());
+ }
+
+ protected ConnectionProfile getProfileNamed( String profileName) {
+
+ return ConnectionProfileRepository.instance().profileNamed( profileName);
+ }
+
+ private String getTestPluginBundleId() {
+ return DbTestPlugin.BUNDLE_ID;
+ }
+
+ private IPath getDriverDefinitionLocation() {
+ return ConnectivityPlugin.getDefault().getStateLocation();
+ }
+
+ private String getConfigPath() {
+ return this.getConfigDir() + "/" + this.getConfigName();
+ }
+
+ private String getConfigDir() {
+ return PLATFORM_CONFIG_DIRECTORY;
+ }
+
+ private boolean platformIsNew() {
+ return( !this.databaseVendor().equals( this.currentDbVendor) || this.databaseVersion().equals( this.currentDbVersion));
+ }
+
+ private void loadPlatformProperties() throws IOException {
+
+ if( this.platformProperties == null) {
+ URL configUrl = Platform.getBundle( this.getTestPluginBundleId()).getEntry( this.getConfigPath());
+
+ this.platformProperties = new Properties();
+ this.platformProperties.load( configUrl.openStream());
+ }
+ }
+
+ private Properties buildDriverProperties() {
+ Properties driverProperties = new Properties();
+ driverProperties.setProperty( DTPConnectionProfileWrapper.DRIVER_DEFINITION_TYPE_PROP_ID, this.driverDefinitionType());
+ driverProperties.setProperty( DTPConnectionProfileWrapper.DRIVER_JAR_LIST_PROP_ID, this.databasedriverJarList());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.USERNAME_PROP_ID, this.userName());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.DRIVER_CLASS_PROP_ID, this.driverClass());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_NAME_PROP_ID, this.databaseName());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.PASSWORD_PROP_ID, this.userPassword());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.URL_PROP_ID, this.databaseUrl());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_VENDOR_PROP_ID, this.databaseVendor());
+ driverProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_VERSION_PROP_ID, this.databaseVersion());
+ return driverProperties;
+ }
+
+ private Properties buildBasicProperties() {
+ Properties basicProperties = new Properties();
+ basicProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_NAME_PROP_ID, this.databaseName());
+ basicProperties.setProperty( IDBDriverDefinitionConstants.USERNAME_PROP_ID, this.userName());
+ basicProperties.setProperty( IDBDriverDefinitionConstants.PASSWORD_PROP_ID, this.userPassword());
+ basicProperties.setProperty( DTPConnectionProfileWrapper.DRIVER_DEFINITION_PROP_ID, this.driverDefinitionId());
+
+ basicProperties.setProperty( IDBDriverDefinitionConstants.DRIVER_CLASS_PROP_ID, this.driverClass());
+ basicProperties.setProperty( IDBDriverDefinitionConstants.URL_PROP_ID, this.databaseUrl());
+ basicProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_VENDOR_PROP_ID, this.databaseVendor());
+ basicProperties.setProperty( IDBDriverDefinitionConstants.DATABASE_VERSION_PROP_ID, this.databaseVersion());
+
+ basicProperties.setProperty( DTPConnectionProfileWrapper.DATABASE_SAVE_PWD_PROP_ID, this.passwordIsSaved());
+ return basicProperties;
+ }
+
+ private void buildConnectionProfile( String profileName) throws ConnectionProfileException {
+
+ ProfileManager profileManager = ProfileManager.getInstance();
+ Assert.assertNotNull( profileManager);
+ IConnectionProfile profile = profileManager.getProfileByName( profileName);
+ if( profile == null) {
+ Properties basicProperties = buildBasicProperties();
+ ProfileManager.getInstance().createProfile( profileName, this.profileDescription(), this.providerId(), basicProperties);
+ }
+ }
+
+ private void buildDriverDefinitionFile( String driverFileName) throws CoreException {
+
+ XMLFileManager.setStorageLocation( this.getDriverDefinitionLocation());
+ XMLFileManager.setFileName( driverFileName);
+ IPropertySet[] propsets = new IPropertySet[ 1];
+ String driverName = this.driverName();
+ String driverId = this.driverDefinitionId();
+ PropertySetImpl propertySet = new PropertySetImpl( driverName, driverId);
+ propertySet.setProperties( driverId, this.buildDriverProperties());
+ propsets[ 0] = propertySet;
+
+ XMLFileManager.saveNamedPropertySet( propsets);
+
+ File driverDefinitioneFile = this.getDriverDefinitionLocation().append( driverFileName).toFile();
+ Assert.assertTrue( driverDefinitioneFile.exists());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Derby101Tests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Derby101Tests.java
new file mode 100644
index 0000000000..b3cec09137
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Derby101Tests.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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.db.tests.internal.platforms;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+
+/**
+ * Derby 10.1 Embedded Driver Test
+ */
+public class Derby101Tests extends DTPPlatformTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", Derby101Tests.class.getName()});
+ }
+
+ public static Test suite() {
+ return new TestSuite( Derby101Tests.class);
+ }
+
+ public Derby101Tests( String name) {
+ super( name);
+ }
+
+ protected String databaseVendor() {
+ return "Derby";
+ }
+
+ protected String databaseVersion() {
+ return "10.1";
+ }
+
+ protected String providerId() {
+ return "org.eclipse.datatools.connectivity.db.derby.embedded.connectionProfile";
+ }
+
+ protected String driverName() {
+ return "Derby Embedded JDBC Driver";
+ }
+
+ protected String driverDefinitionType() {
+ return "org.eclipse.datatools.connectivity.db.derby101.genericDriverTemplate";
+ }
+
+ protected String driverDefinitionId() {
+ return "DriverDefn.Derby Embedded JDBC Driver";
+ }
+
+ protected String driverClass() {
+ return "org.apache.derby.jdbc.EmbeddedDriver";
+ }
+
+ protected String getConfigName() {
+ return "derby101.properties";
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java
new file mode 100644
index 0000000000..2a1861cd64
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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.db.tests.internal.platforms;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+
+/**
+ * Oracle 10g Thin Driver Test
+ */
+public class Oracle10gTests extends DTPPlatformTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", Oracle10gTests.class.getName()});
+ }
+
+ public static Test suite() {
+ return new TestSuite( Oracle10gTests.class);
+ }
+
+ public Oracle10gTests( String name) {
+ super( name);
+ }
+
+ protected String databaseVendor() {
+ return "Oracle";
+ }
+
+ protected String databaseVersion() {
+ return "10";
+ }
+
+ protected String driverClass() {
+ return "oracle.jdbc.OracleDriver";
+ }
+
+ protected String driverDefinitionId() {
+ return "DriverDefn.Oracle Thin Driver";
+ }
+
+ protected String driverDefinitionType() {
+ return "org.eclipse.datatools.enablement.oracle.10.driverTemplate";
+ }
+
+ protected String driverName() {
+ return "Oracle 10g Thin Driver";
+ }
+
+ protected String getConfigName() {
+ return "oracle10g.properties";
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle9iTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle9iTests.java
new file mode 100644
index 0000000000..6e8d2e3239
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle9iTests.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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.db.tests.internal.platforms;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+
+/**
+ * Oracle 9i Thin Driver Test
+ */
+public class Oracle9iTests extends DTPPlatformTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", Oracle9iTests.class.getName()});
+ }
+
+ public static Test suite() {
+ return new TestSuite( Oracle9iTests.class);
+ }
+
+ public Oracle9iTests( String name) {
+ super( name);
+ }
+
+ protected String databaseVendor() {
+ return "Oracle";
+ }
+
+ protected String databaseVersion() {
+ return "9";
+ }
+
+ protected String driverClass() {
+ return "oracle.jdbc.OracleDriver";
+ }
+
+ protected String driverDefinitionId() {
+ return "DriverDefn.Oracle Thin Driver";
+ }
+
+ protected String driverDefinitionType() {
+ return "org.eclipse.datatools.enablement.oracle.9.driverTemplate";
+ }
+
+ protected String driverName() {
+ return "Oracle 9i Thin Driver";
+ }
+
+ protected String getConfigName() {
+ return "oracle9i.properties";
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SQLServer2005Tests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SQLServer2005Tests.java
new file mode 100644
index 0000000000..a0f5034f6d
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SQLServer2005Tests.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * 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.db.tests.internal.platforms;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.swingui.TestRunner;
+
+/**
+ * SQL Server 2005 Driver Test
+ */
+public class SQLServer2005Tests extends DTPPlatformTests {
+
+ public static void main( String[] args) {
+ TestRunner.main( new String[] { "-c", SQLServer2005Tests.class.getName()});
+ }
+
+ public static Test suite() {
+ return new TestSuite( SQLServer2005Tests.class);
+ }
+
+ public SQLServer2005Tests( String name) {
+ super( name);
+ }
+
+ protected String databaseVendor() {
+ return "SQLServer";
+ }
+
+ protected String databaseVersion() {
+ return "2005";
+ }
+
+ protected String providerId() {
+ return "org.eclipse.datatools.connectivity.db.generic.connectionProfile";
+ }
+
+ protected String driverName() {
+ return "Microsoft SQL Server 2005 JDBC Driver";
+ }
+
+ protected String driverDefinitionType() {
+ return "org.eclipse.datatools.enablement.msft.sqlserver.2005.driverTemplate";
+ }
+
+ protected String driverDefinitionId() {
+ return "DriverDefn.Microsoft SQL Server 2005 JDBC Driver";
+ }
+
+ protected String driverClass() {
+ return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
+ }
+
+ protected String getConfigName() {
+ return "sqlserver2005.properties";
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/.classpath b/jpa/tests/org.eclipse.jpt.utility.tests/.classpath
new file mode 100644
index 0000000000..751c8f2e50
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/.cvsignore b/jpa/tests/org.eclipse.jpt.utility.tests/.cvsignore
new file mode 100644
index 0000000000..ba077a4031
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/.cvsignore
@@ -0,0 +1 @@
+bin
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/.project b/jpa/tests/org.eclipse.jpt.utility.tests/.project
new file mode 100644
index 0000000000..aa5271e003
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.jpt.utility.tests</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/META-INF/MANIFEST.MF b/jpa/tests/org.eclipse.jpt.utility.tests/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..1bd552b95e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/META-INF/MANIFEST.MF
@@ -0,0 +1,10 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %pluginName
+Bundle-Vendor: %providerName
+Bundle-SymbolicName: org.eclipse.jpt.utility.tests
+Bundle-Version: 1.0.0
+Bundle-Localization: plugin
+Require-Bundle: org.eclipse.jpt.utility,
+ org.junit;bundle-version="[3.0.0,4.0.0)"
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/build.properties b/jpa/tests/org.eclipse.jpt.utility.tests/build.properties
new file mode 100644
index 0000000000..12ee406114
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/build.properties
@@ -0,0 +1,15 @@
+################################################################################
+# 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
+################################################################################
+source.. = src/
+output.. = bin/
+bin.includes = .,\
+ META-INF/,\
+ test.xml,\
+ plugin.properties
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/plugin.properties b/jpa/tests/org.eclipse.jpt.utility.tests/plugin.properties
new file mode 100644
index 0000000000..9995768587
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/plugin.properties
@@ -0,0 +1,24 @@
+################################################################################
+# 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
+################################################################################
+# ====================================================================
+# To code developer:
+# Do NOT change the properties between this line and the
+# "%%% END OF TRANSLATED PROPERTIES %%%" line.
+# Make a new property name, append to the end of the file and change
+# the code to use the new property.
+# ====================================================================
+
+# ====================================================================
+# %%% END OF TRANSLATED PROPERTIES %%%
+# ====================================================================
+
+pluginName = Java Persistence API Utility Tests
+providerName = Eclipse.org
+
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/BitToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/BitToolsTests.java
new file mode 100644
index 0000000000..ed3e6f2814
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/BitToolsTests.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * 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.utility.tests.internal;
+
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.BitTools;
+
+public class BitToolsTests extends TestCase {
+
+ public BitToolsTests(String name) {
+ super(name);
+ }
+
+ public void testAllFlagsAreSetIntInt() {
+ assertTrue(BitTools.allFlagsAreSet(0x0003, 0x0001));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, 0x0001));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, 0x0101));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, 0x0103));
+
+ assertFalse(BitTools.allFlagsAreSet(0x0303, 0x1103));
+ assertFalse(BitTools.allFlagsAreSet(0x0000, 0x1103));
+ }
+
+ public void testAllFlagsAreSetIntIntArray() {
+ assertTrue(BitTools.allFlagsAreSet(0x0003, new int[] { 0x0001 }));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, new int[] { 0x0001 }));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, new int[] { 0x0100, 0x0001 }));
+ assertTrue(BitTools.allFlagsAreSet(0x0303, new int[] { 0x0100, 0x0002, 0x0001 }));
+
+ assertFalse(BitTools.allFlagsAreSet(0x0303, new int[] { 0x1000, 0x0100, 0x0002, 0x0001 }));
+ assertFalse(BitTools.allFlagsAreSet(0x0000, new int[] { 0x1000, 0x0100, 0x0002, 0x0001 }));
+ }
+
+ public void testAnyFlagsAreSetIntInt() {
+ assertTrue(BitTools.anyFlagsAreSet(0x0003, 0x0001));
+ assertTrue(BitTools.anyFlagsAreSet(0xFFFF, 0x0001));
+ assertTrue(BitTools.anyFlagsAreSet(0x0003, 0xFFFF));
+
+ assertFalse(BitTools.anyFlagsAreSet(0x0303, 0x1010));
+ assertFalse(BitTools.anyFlagsAreSet(0x0000, 0xFFFF));
+ }
+
+ public void testAnyFlagsAreSetIntIntArray() {
+ assertTrue(BitTools.anyFlagsAreSet(0x0003, new int[] { 0x0001 }));
+ assertTrue(BitTools.anyFlagsAreSet(0xFFFF, new int[] { 0x0001 }));
+ assertTrue(BitTools.anyFlagsAreSet(0x0303, new int[] { 0xF000, 0x0F00, 0x00F0, 0x000F }));
+
+ assertFalse(BitTools.anyFlagsAreSet(0x0303, new int[] { 0x1000, 0x0010 }));
+ assertFalse(BitTools.anyFlagsAreSet(0x0000, new int[] { 0xF000, 0x0F00, 0x00F0, 0x000F }));
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClassToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClassToolsTests.java
new file mode 100644
index 0000000000..504b621daa
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClassToolsTests.java
@@ -0,0 +1,548 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Vector;
+import java.util.Map.Entry;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.ClassTools;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;
+
+public class ClassToolsTests extends TestCase {
+
+ private static String testStaticField;
+
+ public ClassToolsTests(String name) {
+ super(name);
+ }
+
+ /**
+ * Return the compiler-generated class name. The Eclipse compiler generates
+ * "local" classes with names in the form "com.foo.Outer$1$Local"; while the
+ * JDK compiler generates "com.foo.Outer$1Local". There might be other
+ * differences.... ~bjv
+ */
+ public static String compilerDependentClassNameFor(String className) {
+ int index = className.indexOf("$1$");
+ if (index == -1) {
+ return className;
+ }
+ try {
+ Class.forName(className);
+ } catch (ClassNotFoundException ex) {
+ return className.substring(0, index + 2) + className.substring(index + 3);
+ }
+ return className;
+ }
+
+ private static String munge(String className) {
+ return compilerDependentClassNameFor(className);
+ }
+
+ public void testAllFields() {
+ int fieldCount = 0;
+ fieldCount += java.util.Vector.class.getDeclaredFields().length;
+ fieldCount += java.util.AbstractList.class.getDeclaredFields().length;
+ fieldCount += java.util.AbstractCollection.class.getDeclaredFields().length;
+ fieldCount += java.lang.Object.class.getDeclaredFields().length;
+ Field[] fields = ClassTools.allFields(java.util.Vector.class);
+ assertEquals(fieldCount, fields.length);
+ assertTrue(CollectionTools.contains(this.names(fields), "modCount"));
+ assertTrue(CollectionTools.contains(this.names(fields), "serialVersionUID"));
+ assertTrue(CollectionTools.contains(this.names(fields), "capacityIncrement"));
+ assertTrue(CollectionTools.contains(this.names(fields), "elementCount"));
+ assertTrue(CollectionTools.contains(this.names(fields), "elementData"));
+ assertTrue(fields[0].isAccessible());
+ }
+
+ public void testAllMethods() {
+ int methodCount = 0;
+ methodCount += java.util.Vector.class.getDeclaredMethods().length;
+ methodCount += java.util.AbstractList.class.getDeclaredMethods().length;
+ methodCount += java.util.AbstractCollection.class.getDeclaredMethods().length;
+ methodCount += java.lang.Object.class.getDeclaredMethods().length;
+ Method[] methods = ClassTools.allMethods(java.util.Vector.class);
+ assertEquals(methodCount, methods.length);
+ assertTrue(CollectionTools.contains(this.names(methods), "wait"));
+ assertTrue(CollectionTools.contains(this.names(methods), "addElement"));
+ assertTrue(methods[0].isAccessible());
+ }
+
+ public void testNewInstanceClass() {
+ Vector<?> v = ClassTools.newInstance(java.util.Vector.class);
+ assertNotNull(v);
+ assertEquals(0, v.size());
+ }
+
+ public void testNewInstanceClassClassObject() {
+ int initialCapacity = 200;
+ Vector<?> v = ClassTools.newInstance(java.util.Vector.class, int.class, new Integer(initialCapacity));
+ assertNotNull(v);
+ assertEquals(0, v.size());
+ Object[] elementData = (Object[]) ClassTools.getFieldValue(v, "elementData");
+ assertEquals(initialCapacity, elementData.length);
+ }
+
+ public void testNewInstanceClassClassArrayObjectArray() {
+ int initialCapacity = 200;
+ Class<?>[] parmTypes = new Class[1];
+ parmTypes[0] = int.class;
+ Object[] parms = new Object[1];
+ parms[0] = new Integer(initialCapacity);
+ Vector<?> v = ClassTools.newInstance(java.util.Vector.class, parmTypes, parms);
+ assertNotNull(v);
+ assertEquals(0, v.size());
+ Object[] elementData = (Object[]) ClassTools.getFieldValue(v, "elementData");
+ assertEquals(initialCapacity, elementData.length);
+
+ parms[0] = new Integer(-1);
+ boolean exCaught = false;
+ try {
+ v = ClassTools.newInstance(java.util.Vector.class, parmTypes, parms);
+ } catch (RuntimeException ex) {
+ exCaught = true;
+ }
+ assertTrue("RuntimeException not thrown", exCaught);
+
+ parmTypes[0] = java.lang.String.class;
+ parms[0] = "foo";
+ exCaught = false;
+ try {
+ v = ClassTools.attemptNewInstance(java.util.Vector.class, parmTypes, parms);
+ } catch (NoSuchMethodException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchMethodException not thrown", exCaught);
+ }
+
+ public void testGetFieldValue() {
+ int initialCapacity = 200;
+ Vector<?> v = new Vector<Object>(initialCapacity);
+ Object[] elementData = (Object[]) ClassTools.getFieldValue(v, "elementData");
+ assertEquals(initialCapacity, elementData.length);
+
+ // test inherited field
+ Integer modCountInteger = (Integer) ClassTools.getFieldValue(v, "modCount");
+ int modCount = modCountInteger.intValue();
+ assertEquals(0, modCount);
+
+ boolean exCaught = false;
+ Object bogusFieldValue = null;
+ try {
+ bogusFieldValue = ClassTools.attemptToGetFieldValue(v, "bogusField");
+ } catch (NoSuchFieldException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchFieldException not thrown: " + bogusFieldValue, exCaught);
+ }
+
+ public void testExecuteMethodObjectString() {
+ Vector<String> v = new Vector<String>();
+ int size = ((Integer) ClassTools.executeMethod(v, "size")).intValue();
+ assertEquals(0, size);
+
+ v.addElement("foo");
+ size = ((Integer) ClassTools.executeMethod(v, "size")).intValue();
+ assertEquals(1, size);
+ }
+
+ public void testExecuteMethodObjectStringClassObject() {
+ Vector<String> v = new Vector<String>();
+ boolean booleanResult = ((Boolean) ClassTools.executeMethod(v, "add", Object.class, "foo")).booleanValue();
+ assertTrue(booleanResult);
+ assertTrue(v.contains("foo"));
+ Object voidResult = ClassTools.executeMethod(v, "addElement", Object.class, "bar");
+ assertNull(voidResult);
+ }
+
+ public void testExecuteMethodObjectStringClassArrayObjectArray() {
+ Vector<String> v = new Vector<String>();
+ Class<?>[] parmTypes = new Class[1];
+ parmTypes[0] = java.lang.Object.class;
+ Object[] parms = new Object[1];
+ parms[0] = "foo";
+ boolean booleanResult = ((Boolean) ClassTools.executeMethod(v, "add", parmTypes, parms)).booleanValue();
+ assertTrue(booleanResult);
+ assertTrue(v.contains("foo"));
+
+ boolean exCaught = false;
+ Object bogusMethodReturnValue = null;
+ try {
+ bogusMethodReturnValue = ClassTools.attemptToExecuteMethod(v, "bogusMethod", parmTypes, parms);
+ } catch (NoSuchMethodException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchMethodException not thrown: " + bogusMethodReturnValue, exCaught);
+ }
+
+ public void testExecuteStaticMethodClassString() {
+ Double randomObject = (Double) ClassTools.executeStaticMethod(java.lang.Math.class, "random");
+ assertNotNull(randomObject);
+ double random = randomObject.doubleValue();
+ assertTrue(random >= 0);
+ assertTrue(random < 1);
+ }
+
+ public void testExecuteStaticMethodClassStringClassObject() {
+ String s = (String) ClassTools.executeStaticMethod(java.lang.String.class, "valueOf", boolean.class, Boolean.TRUE);
+ assertNotNull(s);
+ assertEquals("true", s);
+ }
+
+ public void testExecuteStaticMethodClassStringClassArrayObjectArray() {
+ Class<?>[] parmTypes = new Class[1];
+ parmTypes[0] = boolean.class;
+ Object[] parms = new Object[1];
+ parms[0] = Boolean.TRUE;
+ String s = (String) ClassTools.executeStaticMethod(java.lang.String.class, "valueOf", parmTypes, parms);
+ assertNotNull(s);
+ assertEquals("true", s);
+
+ boolean exCaught = false;
+ Object bogusStaticMethodReturnValue = null;
+ try {
+ bogusStaticMethodReturnValue = ClassTools.attemptToExecuteStaticMethod(java.lang.String.class, "bogusStaticMethod", parmTypes, parms);
+ } catch (NoSuchMethodException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchMethodException not thrown: " + bogusStaticMethodReturnValue, exCaught);
+
+ // test non-static method
+ exCaught = false;
+ try {
+ bogusStaticMethodReturnValue = ClassTools.attemptToExecuteStaticMethod(java.lang.String.class, "toString");
+ } catch (NoSuchMethodException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchMethodException not thrown: " + bogusStaticMethodReturnValue, exCaught);
+ }
+
+ public void testSetFieldValue() {
+ Vector<String> v = new Vector<String>();
+ Object[] newElementData = new Object[5];
+ newElementData[0] = "foo";
+ ClassTools.setFieldValue(v, "elementData", newElementData);
+ ClassTools.setFieldValue(v, "elementCount", new Integer(1));
+ // test inherited field
+ ClassTools.setFieldValue(v, "modCount", new Integer(1));
+ assertTrue(v.contains("foo"));
+
+ boolean exCaught = false;
+ try {
+ ClassTools.attemptToSetFieldValue(v, "bogusField", "foo");
+ } catch (NoSuchFieldException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchFieldException not thrown", exCaught);
+ }
+
+ public void testSetStaticFieldValue() {
+ ClassTools.setStaticFieldValue(this.getClass(), "testStaticField", "new value");
+ assertEquals(testStaticField, "new value");
+
+ boolean exCaught = false;
+ try {
+ ClassTools.attemptToSetStaticFieldValue(this.getClass(), "bogusStaticField", "new value");
+ } catch (NoSuchFieldException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchFieldException not thrown", exCaught);
+ }
+
+ public void testShortName() {
+ assertEquals("Vector", ClassTools.shortClassNameForObject(new java.util.Vector<Object>()));
+ assertEquals("Vector", ClassTools.shortNameFor(java.util.Vector.class));
+ }
+
+ public void testNestedName() {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("foo", "bar");
+ Entry<String, String> entry = map.entrySet().iterator().next();
+ assertEquals("Entry", ClassTools.nestedClassNameForObject(entry));
+ assertEquals("Entry", ClassTools.nestedNameFor(java.util.Map.Entry.class));
+ }
+
+ public void testPackageName() {
+ assertEquals("java.util", ClassTools.packageNameFor(java.util.Vector.class));
+ assertEquals("java.util", ClassTools.packageNameFor(java.util.Map.Entry.class));
+ }
+
+ public void testArrayDepthFor() {
+ assertEquals(0, ClassTools.arrayDepthFor(java.util.Vector.class));
+ assertEquals(0, ClassTools.arrayDepthFor(int.class));
+ assertEquals(0, ClassTools.arrayDepthFor(void.class));
+ assertEquals(1, ClassTools.arrayDepthFor(java.util.Vector[].class));
+ assertEquals(1, ClassTools.arrayDepthFor(int[].class));
+ assertEquals(3, ClassTools.arrayDepthFor(java.util.Vector[][][].class));
+ assertEquals(3, ClassTools.arrayDepthFor(int[][][].class));
+ }
+
+ public void testArrayDepthForObject() {
+ assertEquals(0, ClassTools.arrayDepthForObject(new java.util.Vector<Object>()));
+ assertEquals(1, ClassTools.arrayDepthForObject(new java.util.Vector[0]));
+ assertEquals(1, ClassTools.arrayDepthForObject(new int[0]));
+ assertEquals(3, ClassTools.arrayDepthForObject(new java.util.Vector[0][0][0]));
+ assertEquals(3, ClassTools.arrayDepthForObject(new int[0][0][0]));
+ }
+
+ public void testArrayDepthForClassNamed() {
+ assertEquals(0, ClassTools.arrayDepthForClassNamed(java.util.Vector.class.getName()));
+ assertEquals(0, ClassTools.arrayDepthForClassNamed(int.class.getName()));
+ assertEquals(0, ClassTools.arrayDepthForClassNamed(void.class.getName()));
+ assertEquals(1, ClassTools.arrayDepthForClassNamed(java.util.Vector[].class.getName()));
+ assertEquals(1, ClassTools.arrayDepthForClassNamed(int[].class.getName()));
+ assertEquals(3, ClassTools.arrayDepthForClassNamed(java.util.Vector[][][].class.getName()));
+ assertEquals(3, ClassTools.arrayDepthForClassNamed(int[][][].class.getName()));
+ }
+
+ public void testElementTypeFor() {
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeFor(java.util.Vector.class));
+ assertEquals(int.class, ClassTools.elementTypeFor(int.class));
+ assertEquals(void.class, ClassTools.elementTypeFor(void.class));
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeFor(java.util.Vector[].class));
+ assertEquals(int.class, ClassTools.elementTypeFor(int[].class));
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeFor(java.util.Vector[][][].class));
+ assertEquals(int.class, ClassTools.elementTypeFor(int[][][].class));
+ }
+
+ public void testElementTypeForObject() {
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeForObject(new java.util.Vector<Object>()));
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeForObject(new java.util.Vector[0]));
+ assertEquals(int.class, ClassTools.elementTypeForObject(new int[0]));
+ assertEquals(java.util.Vector.class, ClassTools.elementTypeForObject(new java.util.Vector[0][0][0]));
+ assertEquals(int.class, ClassTools.elementTypeForObject(new int[0][0][0]));
+ }
+
+ public void testElementTypeNameFor() {
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameFor(java.util.Vector.class));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameFor(int.class));
+ assertEquals(void.class.getName(), ClassTools.elementTypeNameFor(void.class));
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameFor(java.util.Vector[].class));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameFor(int[].class));
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameFor(java.util.Vector[][][].class));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameFor(int[][][].class));
+ }
+
+ public void testElementTypeNameForClassNamed() {
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameForClassNamed(java.util.Vector.class.getName()));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameForClassNamed(int.class.getName()));
+ assertEquals(void.class.getName(), ClassTools.elementTypeNameForClassNamed(void.class.getName()));
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameForClassNamed(java.util.Vector[].class.getName()));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameForClassNamed(int[].class.getName()));
+ assertEquals(java.util.Vector.class.getName(), ClassTools.elementTypeNameForClassNamed(java.util.Vector[][][].class.getName()));
+ assertEquals(int.class.getName(), ClassTools.elementTypeNameForClassNamed(int[][][].class.getName()));
+ }
+
+ public void testClassCodes() {
+ assertEquals("byte", ClassTools.classNameForCode('B'));
+ assertEquals("char", ClassTools.classNameForCode('C'));
+ assertEquals("double", ClassTools.classNameForCode('D'));
+ assertEquals("float", ClassTools.classNameForCode('F'));
+ assertEquals("int", ClassTools.classNameForCode('I'));
+ assertEquals("long", ClassTools.classNameForCode('J'));
+ assertEquals("short", ClassTools.classNameForCode('S'));
+ assertEquals("boolean", ClassTools.classNameForCode('Z'));
+ assertEquals("void", ClassTools.classNameForCode('V'));
+
+ boolean exCaught = false;
+ try {
+ ClassTools.classNameForCode('X');
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalArgumentException not thrown", exCaught);
+ }
+
+ public void testClassNamedIsTopLevel() throws Exception {
+ assertTrue(ClassTools.classNamedIsTopLevel(java.util.Map.class.getName())); // top-level
+ assertFalse(ClassTools.classNamedIsTopLevel(java.util.Map.Entry.class.getName())); // member
+ assertFalse(ClassTools.classNamedIsTopLevel(Class.forName(munge(this.getClass().getName() + "$1$LocalClass")).getName())); // local
+ assertFalse(ClassTools.classNamedIsTopLevel(Class.forName("java.util.Vector$1").getName())); // anonymous
+
+ Object[] array = new java.util.Map[0]; // top-level
+ assertFalse(ClassTools.classNamedIsTopLevel(array.getClass().getName()));
+ array = new java.util.Map.Entry[0]; // member
+ assertFalse(ClassTools.classNamedIsTopLevel(array.getClass().getName()));
+ Class<?> localClass = Class.forName(munge(this.getClass().getName() + "$1$LocalClass")); // local
+ array = (Object[]) Array.newInstance(localClass, 0);
+ assertFalse(ClassTools.classNamedIsTopLevel(array.getClass().getName()));
+ Class<?> anonClass = Class.forName("java.util.Vector$1"); // local
+ array = (Object[]) Array.newInstance(anonClass, 0);
+ assertFalse(ClassTools.classNamedIsTopLevel(array.getClass().getName()));
+ }
+
+ public void testClassNamedIsMember() throws Exception {
+ assertFalse(ClassTools.classNamedIsMember(java.util.Map.class.getName())); // top-level
+ assertTrue(ClassTools.classNamedIsMember(java.util.Map.Entry.class.getName())); // member
+ assertFalse(ClassTools.classNamedIsMember(Class.forName(munge(this.getClass().getName() + "$1$LocalClass")).getName())); // local
+ assertFalse(ClassTools.classNamedIsMember(Class.forName("java.util.Vector$1").getName())); // anonymous
+
+ Object[] array = new java.util.Map[0]; // top-level
+ assertFalse(ClassTools.classNamedIsMember(array.getClass().getName()));
+ array = new java.util.Map.Entry[0]; // member
+ assertFalse(ClassTools.classNamedIsMember(array.getClass().getName()));
+ Class<?> localClass = Class.forName(munge(this.getClass().getName() + "$1$LocalClass")); // local
+ array = (Object[]) Array.newInstance(localClass, 0);
+ assertFalse(ClassTools.classNamedIsMember(array.getClass().getName()));
+ Class<?> anonClass = Class.forName("java.util.Vector$1"); // local
+ array = (Object[]) Array.newInstance(anonClass, 0);
+ assertFalse(ClassTools.classNamedIsMember(array.getClass().getName()));
+
+ // test a few edge cases
+ assertTrue(ClassTools.classNamedIsMember("java.util.Map$a1"));
+ assertTrue(ClassTools.classNamedIsMember("java.util.Map$a1$aaa$bbb"));
+ assertFalse(ClassTools.classNamedIsMember("java.util.Map$1a1$aaa"));
+ assertFalse(ClassTools.classNamedIsMember("java.util.Map$1a"));
+ assertTrue(ClassTools.classNamedIsMember("java.util.Map$a12345$b12345"));
+ assertFalse(ClassTools.classNamedIsMember("java.util.Map$12345a"));
+ assertFalse(ClassTools.classNamedIsMember("java.util.Map$333"));
+ assertFalse(ClassTools.classNamedIsMember("java.util.Map3$333"));
+ }
+
+ public void testClassNamedIsLocal() throws Exception {
+ class LocalClass {
+ void foo() {
+ System.getProperty("foo");
+ }
+ }
+ new LocalClass().foo();
+ assertFalse(ClassTools.classNamedIsLocal(java.util.Map.class.getName())); // top-level
+ assertFalse(ClassTools.classNamedIsLocal(java.util.Map.Entry.class.getName())); // member
+ assertTrue(ClassTools.classNamedIsLocal(Class.forName(munge(this.getClass().getName() + "$1$LocalClass")).getName())); // local
+ assertFalse(ClassTools.classNamedIsLocal(Class.forName("java.util.Vector$1").getName())); // anonymous
+
+ Object[] array = new java.util.Map[0]; // top-level
+ assertFalse(ClassTools.classNamedIsLocal(array.getClass().getName()));
+ array = new java.util.Map.Entry[0]; // member
+ assertFalse(ClassTools.classNamedIsLocal(array.getClass().getName()));
+ Class<?> localClass = Class.forName(munge(this.getClass().getName() + "$1$LocalClass")); // local
+ array = (Object[]) Array.newInstance(localClass, 0);
+ assertFalse(ClassTools.classNamedIsLocal(array.getClass().getName()));
+ Class<?> anonClass = Class.forName("java.util.Vector$1"); // local
+ array = (Object[]) Array.newInstance(anonClass, 0);
+ assertFalse(ClassTools.classNamedIsLocal(array.getClass().getName()));
+
+ // test a few edge cases
+ assertFalse(ClassTools.classNamedIsLocal("java.util.Map$a1"));
+ assertFalse(ClassTools.classNamedIsLocal("java.util.Map$a1$aaa$bbb"));
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$11$aaa"));
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$1$a")); // eclipse naming convention
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$2$abc")); // eclipse naming convention
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$2$abc1")); // eclipse naming convention
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$1a")); // jdk naming convention
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$2abc")); // jdk naming convention
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$2abc1")); // jdk naming convention
+ assertFalse(ClassTools.classNamedIsLocal("java.util.Map$a12345$b12345"));
+ assertTrue(ClassTools.classNamedIsLocal("java.util.Map$12345$a1234"));
+ assertFalse(ClassTools.classNamedIsLocal("java.util.Map$333"));
+ assertFalse(ClassTools.classNamedIsLocal("java.util.Map3$333"));
+ }
+
+ public void testClassNamedIsAnonymous() throws Exception {
+ assertFalse(ClassTools.classNamedIsAnonymous(java.util.Map.class.getName())); // top-level
+ assertFalse(ClassTools.classNamedIsAnonymous(java.util.Map.Entry.class.getName())); // member
+ assertFalse(ClassTools.classNamedIsAnonymous(Class.forName(munge(this.getClass().getName() + "$1$LocalClass")).getName())); // local
+ assertTrue(ClassTools.classNamedIsAnonymous(Class.forName("java.util.Vector$1").getName())); // anonymous
+
+ Object[] array = new java.util.Map[0]; // top-level
+ assertFalse(ClassTools.classNamedIsAnonymous(array.getClass().getName()));
+ array = new java.util.Map.Entry[0]; // member
+ assertFalse(ClassTools.classNamedIsAnonymous(array.getClass().getName()));
+ Class<?> localClass = Class.forName(munge(this.getClass().getName() + "$1$LocalClass")); // local
+ array = (Object[]) Array.newInstance(localClass, 0);
+ assertFalse(ClassTools.classNamedIsAnonymous(array.getClass().getName()));
+ Class<?> anonClass = Class.forName("java.util.Vector$1"); // local
+ array = (Object[]) Array.newInstance(anonClass, 0);
+ assertFalse(ClassTools.classNamedIsAnonymous(array.getClass().getName()));
+
+ // test a few edge cases
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$a1"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$a1$aaa$bbb"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$1a1$aaa"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$1$a"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$1a"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$a12345$b12345"));
+ assertFalse(ClassTools.classNamedIsAnonymous("java.util.Map$12345$a1234"));
+ assertTrue(ClassTools.classNamedIsAnonymous("java.util.Map$333"));
+ assertTrue(ClassTools.classNamedIsAnonymous("java.util.Map3$333"));
+ }
+
+ public void testCodeForClass() {
+ assertEquals('I', ClassTools.codeForClass(int.class));
+ assertEquals('B', ClassTools.codeForClass(byte.class));
+ }
+
+ public void testCodeForClassNamed() {
+ assertEquals('I', ClassTools.codeForClassNamed(int.class.getName()));
+ assertEquals('I', ClassTools.codeForClassNamed("int"));
+ assertEquals('B', ClassTools.codeForClassNamed(byte.class.getName()));
+ assertEquals('B', ClassTools.codeForClassNamed("byte"));
+ }
+
+ public void testClassForTypeDeclaration() throws Exception {
+ assertEquals(int.class, ClassTools.classForTypeDeclaration("int", 0));
+ assertEquals(int[].class, ClassTools.classForTypeDeclaration("int", 1));
+ assertEquals(int[][][].class, ClassTools.classForTypeDeclaration("int", 3));
+
+ assertEquals(Object.class, ClassTools.classForTypeDeclaration("java.lang.Object", 0));
+ assertEquals(Object[][][].class, ClassTools.classForTypeDeclaration("java.lang.Object", 3));
+
+ assertEquals(void.class, ClassTools.classForTypeDeclaration("void", 0));
+ try {
+ ClassTools.classForTypeDeclaration(void.class.getName(), 1);
+ fail("should not get here...");
+ } catch (ClassNotFoundException ex) {
+ // expected
+ }
+ }
+
+ public void testClassNameForTypeDeclaration() throws Exception {
+ assertEquals(int.class.getName(), ClassTools.classNameForTypeDeclaration("int", 0));
+ assertEquals(int[].class.getName(), ClassTools.classNameForTypeDeclaration("int", 1));
+ assertEquals(int[][][].class.getName(), ClassTools.classNameForTypeDeclaration("int", 3));
+
+ assertEquals(Object.class.getName(), ClassTools.classNameForTypeDeclaration("java.lang.Object", 0));
+ assertEquals(Object[][][].class.getName(), ClassTools.classNameForTypeDeclaration("java.lang.Object", 3));
+
+ assertEquals(void.class.getName(), ClassTools.classNameForTypeDeclaration("void", 0));
+ try {
+ ClassTools.classNameForTypeDeclaration(void.class.getName(), 1);
+ fail("should not get here...");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ private Iterator<String> names(Field[] fields) {
+ return new TransformationIterator<Field, String>(CollectionTools.iterator(fields)) {
+ @Override
+ protected String transform(Field field) {
+ return field.getName();
+ }
+ };
+ }
+
+ private Iterator<String> names(Method[] methods) {
+ return new TransformationIterator<Method, String>(CollectionTools.iterator(methods)) {
+ @Override
+ protected String transform(Method method) {
+ return method.getName();
+ }
+ };
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClasspathTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClasspathTests.java
new file mode 100644
index 0000000000..54beea0c81
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClasspathTests.java
@@ -0,0 +1,289 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.Classpath;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+
+public class ClasspathTests extends TestCase {
+ private static final String JAVA_HOME = System.getProperty("java.home");
+
+ public ClasspathTests(String name) {
+ super(name);
+ }
+
+ public void testCompressed() {
+ String path = "";
+
+ // no changes
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\i18n.jar;C:\\jdk\\i18n.jar;C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\jdk\\rt.jar;;;;C:\\jdk\\jaws.jar;C:\\jdk\\jaws.jar;C:\\jdk\\rt.jar;;;")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ // no changes
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\..\\jdk\\i18n.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ path = new Classpath(this.morph("C:\\jdk1\\jdk2\\jdk3\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk1\\jdk2\\jdk3\\..\\..\\..\\jdk1\\jdk2\\jdk3\\i18n.jar;C:\\jdk\\jaws.jar")).compressed().path();
+ assertEquals(this.morph("C:\\jdk1\\jdk2\\jdk3\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar"), path);
+
+ }
+
+ public void testConvertToClassName() {
+ String fileName = "java/lang/String.class";
+ File file = new File(fileName);
+ String className = Classpath.convertToClassName(file);
+ assertEquals(java.lang.String.class.getName(), className);
+ }
+
+ public void testConvertToClass() throws ClassNotFoundException {
+ String fileName = "java/lang/String.class";
+ File file = new File(fileName);
+ Class<?> javaClass = Classpath.convertToClass(file);
+ assertEquals(java.lang.String.class, javaClass);
+ }
+
+ public void testConvertToArchiveClassFileEntryName() {
+ String fileName = Classpath.convertToArchiveClassFileEntryName(java.lang.String.class);
+ assertEquals("java/lang/String.class", fileName);
+ }
+
+ public void testConvertToClassFileName() {
+ char sc = File.separatorChar;
+ String fileName = Classpath.convertToClassFileName(java.lang.String.class);
+ assertEquals("java" + sc + "lang" + sc + "String.class", fileName);
+ }
+
+ public void testConvertToJavaFileName() {
+ char sc = File.separatorChar;
+ String fileName = Classpath.convertToJavaFileName(java.lang.String.class.getName());
+ assertEquals("java" + sc + "lang" + sc + "String.java", fileName);
+ }
+
+ public void testConvertToURLs() {
+ URL[] entries = new Classpath(this.morph("C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).urls();
+ int i = 0;
+ assertEquals(this.morphURL("/C:/jdk/rt.jar"), entries[i++].getPath());
+ assertEquals(this.morphURL("/C:/jdk/i18n.jar"), entries[i++].getPath());
+ assertEquals(this.morphURL("/C:/jdk/jaws.jar"), entries[i++].getPath());
+ assertEquals(this.morphURL("/C:/foo/classes"), entries[i++].getPath());
+ assertEquals(this.morphURL("/C:/bar/bar.jar"), entries[i++].getPath());
+ assertEquals(i, entries.length);
+ }
+
+ public void testEntries() {
+ Classpath cp = new Classpath(this.morph("C:\\jdk\\rt.jar;;.;C:\\jdk\\i18n.jar;;;C:\\jdk\\jaws.jar;;C:\\foo\\classes;C:\\bar\\bar.jar;C:\\bar\\bar.jar;"));
+ Classpath.Entry[] entries = cp.getEntries();
+ int i = 0;
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entries[i++].fileName());
+ assertEquals(this.morph("."), entries[i++].fileName());
+ assertEquals(this.morph("C:\\jdk\\i18n.jar"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\jdk\\jaws.jar"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\foo\\classes"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\bar\\bar.jar"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\bar\\bar.jar"), entries[i++].fileName());
+ assertEquals(i, entries.length);
+
+ cp = cp.compressed();
+ entries = cp.getEntries();
+ i = 0;
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entries[i++].fileName());
+ assertEquals(this.morph("."), entries[i++].fileName());
+ assertEquals(this.morph("C:\\jdk\\i18n.jar"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\jdk\\jaws.jar"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\foo\\classes"), entries[i++].fileName());
+ assertEquals(this.morph("C:\\bar\\bar.jar"), entries[i++].fileName());
+ assertEquals(i, entries.length);
+ }
+
+ public void testEntryForFileNamed() {
+ Classpath.Entry entry = null;
+
+ // in the middle - qualified
+ entry = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\rt.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // in the middle - unqualified
+ entry = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;rt.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals("rt.jar", entry.fileName());
+
+ // at the beginning - qualified
+ entry = new Classpath(this.morph("C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // at the beginning - unqualified
+ entry = new Classpath(this.morph("rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals("rt.jar", entry.fileName());
+
+ // at the end - qualified
+ entry = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar;C:\\jdk\\rt.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // at the end - unqualified
+ entry = new Classpath(this.morph("C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar;rt.jar")).entryForFileNamed("rt.jar");
+ assertEquals("rt.jar", entry.fileName());
+
+ // alone - qualified
+ entry = new Classpath(this.morph("C:\\jdk\\rt.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // alone - unqualified
+ entry = new Classpath("rt.jar").entryForFileNamed("rt.jar");
+ assertEquals("rt.jar", entry.fileName());
+
+ // trick entry at the beginning
+ entry = new Classpath(this.morph("rt.jar.new;C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // trick entry in the middle
+ entry = new Classpath(this.morph("rt.jar.new;C:\\jdk\\rtrtrt.jar;C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // trick entry at the end
+ entry = new Classpath(this.morph("rt.jar.new;C:\\jdk\\rtrtrt.jar;C:\\jdk\\rt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar;C:\\jdk\\rtrtrt.jar")).entryForFileNamed("rt.jar");
+ assertEquals(this.morph("C:\\jdk\\rt.jar"), entry.fileName());
+
+ // missing
+ entry = new Classpath(this.morph("rt.jar.new;C:\\jdk\\rtrtrt.jar;C:\\jdk\\i18n.jar;C:\\jdk\\jaws.jar;C:\\foo\\classes;C:\\bar\\bar.jar;C:\\jdk\\rtrtrt.jar")).entryForFileNamed("rt.jar");
+ assertEquals("path entry should not be found", null, entry);
+
+ }
+
+ public void testLocationForClass() {
+ Class<?> javaClass = Classpath.class;
+ File entry = new File(Classpath.locationFor(javaClass));
+ if (entry.isFile() || entry.isDirectory()) {
+ assertTrue(entry.exists());
+ }
+ if (entry.isDirectory()) {
+ assertTrue(new File(entry, Classpath.convertToClassFileName(javaClass)).exists());
+ }
+ }
+
+ public void testRtJarName() throws IOException {
+ File rtFile = new File(Classpath.rtJarName());
+ assertTrue("rt.jar does not exist", rtFile.exists());
+
+ JarFile rtJarFile = new JarFile(rtFile);
+ JarEntry entry = rtJarFile.getJarEntry("java/lang/Object.class");
+ rtJarFile.close();
+ assertTrue("bogus rt.jar", entry != null);
+ }
+
+ public void testJREClassNames() {
+ assertTrue("Vector is missing from JRE class names", CollectionTools.contains(Classpath.bootClasspath().classNames(), java.util.Vector.class.getName()));
+ assertTrue("File is missing from JRE class names", CollectionTools.contains(Classpath.bootClasspath().classNames(), java.io.File.class.getName()));
+ }
+
+ public void testJavaExtensionDirectoryNames() {
+ char sep = File.separatorChar;
+ String stdExtDirName = JAVA_HOME + sep + "lib" + sep + "ext";
+ assertTrue("standard extension dir name missing: " + stdExtDirName, CollectionTools.contains(Classpath.javaExtensionDirectoryNames(), stdExtDirName));
+ }
+
+ public void testJavaExtensionDirectories() {
+ char sep = File.separatorChar;
+ File stdExtDir = new File(JAVA_HOME + sep + "lib" + sep + "ext");
+ assertTrue("standard extension dir missing: " + stdExtDir.getParent(), CollectionTools.contains(Classpath.javaExtensionDirectories(), stdExtDir));
+ }
+
+ //Commented out due to incompatibility with IBM JDK (used in Eclipse WTP Build)
+ //TODO Test should be revised to be compatible with IBM JDK
+ // public void testJavaExtensionClasspathEntries() {
+ // char sep = File.separatorChar;
+ // String jdk = System.getProperty("java.version");
+ // if (jdk.startsWith("1.4") || jdk.startsWith("1.5")) {
+ // Collection jarNames = new ArrayList();
+ // Classpath.Entry[] entries = Classpath.javaExtensionClasspath().getEntries();
+ // for (int i = 0; i < entries.length; i++) {
+ // jarNames.add(entries[i].fileName());
+ // }
+ // String stdExtJarName = JAVA_HOME + sep + "lib" + sep + "ext" + sep + "dnsns.jar";
+ // assertTrue("jdk 1.4.x standard extension jar missing: " + stdExtJarName, jarNames.contains(stdExtJarName));
+ // } else {
+ // fail("we need to update this test for the current jdk");
+ // }
+ // }
+
+ //Commented out due to incompatibility with IBM JDK (used in Eclipse WTP Build)
+ //TODO Test should be revised to be compatible with IBM JDK
+ // public void testJavaExtensionClassNames() {
+ // String jdk = System.getProperty("java.version");
+ // if (jdk.startsWith("1.4") || jdk.startsWith("1.5")) {
+ // String className = "sun.net.spi.nameservice.dns.DNSNameService";
+ // assertTrue("jdk 1.4.x standard extension class missing: " + className,
+ // CollectionTools.contains(Classpath.javaExtensionClasspath().classNames(), className));
+ // } else {
+ // fail("we need to update this test for the current jdk");
+ // }
+ // }
+
+ public void testJavaClasspathClassNames() {
+ String className = this.getClass().getName();
+ ClassLoader cl = this.getClass().getClassLoader();
+ // make sure we are running under the "normal" class loader;
+ // when the tests are executed as an ANT task, they are run under
+ // an ANT class loader and the "Java" classpath does not include this class
+ if (cl.getClass().getName().startsWith("sun.misc")) {
+ assertTrue("class missing: " + className, CollectionTools.contains(Classpath.javaClasspath().classNames(), className));
+ }
+ }
+
+ /**
+ * morph the specified path to a platform-independent path
+ */
+ private String morph(String path) {
+ String result = path;
+ result = result.replace('\\', File.separatorChar);
+ result = result.replace(';', File.pathSeparatorChar);
+ if (!CollectionTools.contains(File.listRoots(), new File("C:\\"))) {
+ result = result.replaceAll("C:", "");
+ }
+ return result;
+ }
+
+ /**
+ * morph the specified URL to a platform-independent path
+ */
+ private String morphURL(String url) {
+ String result = url;
+ if (!CollectionTools.contains(File.listRoots(), new File("C:\\"))) {
+ result = result.replaceAll("/C:", "");
+ }
+ return result;
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CollectionToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CollectionToolsTests.java
new file mode 100644
index 0000000000..027984029e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CollectionToolsTests.java
@@ -0,0 +1,1972 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.Vector;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.Bag;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.HashBag;
+import org.eclipse.jpt.utility.internal.Range;
+import org.eclipse.jpt.utility.internal.ReverseComparator;
+import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
+
+public class CollectionToolsTests extends TestCase {
+
+ public CollectionToolsTests(String name) {
+ super(name);
+ }
+
+ public void testAddObjectArrayObject1() {
+ Object[] a = CollectionTools.add(this.buildObjectArray1(), "twenty");
+ assertEquals(4, a.length);
+ assertTrue(CollectionTools.contains(a, "twenty"));
+ assertEquals("twenty", a[a.length-1]);
+ }
+
+ public void testAddObjectArrayObject2() {
+ String[] a = CollectionTools.add(this.buildStringArray1(), "twenty");
+ assertEquals(4, a.length);
+ assertTrue(CollectionTools.contains(a, "twenty"));
+ assertEquals("twenty", a[a.length-1]);
+ }
+
+ public void testAddObjectArrayIntObject1() {
+ Object[] a = new Object[] { "a", "b", "c", "d" };
+ a = CollectionTools.add(a, 2, "X");
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, "X"));
+ assertTrue(Arrays.equals(new Object[] { "a", "b", "X", "c", "d" }, a));
+ }
+
+ public void testAddObjectArrayIntObject2() {
+ String[] a = new String[] { "a", "b", "c", "d" };
+ a = CollectionTools.add(a, 2, "X");
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, "X"));
+ assertTrue(Arrays.equals(new String[] { "a", "b", "X", "c", "d" }, a));
+ }
+
+ public void testAddObjectArrayIntObjectException() {
+ Object[] a = new Object[] { "a", "b", "c", "d" };
+ boolean exCaught = false;
+ try {
+ a = CollectionTools.add(a, 33, "X");
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAddCharArrayChar() {
+ char[] a = CollectionTools.add(this.buildCharArray(), 'd');
+ assertEquals(4, a.length);
+ assertTrue(CollectionTools.contains(a, 'd'));
+ }
+
+ public void testAddCharArrayIntChar() {
+ char[] a = new char[] { 'a', 'b', 'c', 'd' };
+ a = CollectionTools.add(a, 2, 'X');
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, 'X'));
+ assertTrue(Arrays.equals(new char[] { 'a', 'b', 'X', 'c', 'd' }, a));
+ }
+
+ public void testAddIntArrayInt() {
+ int[] a = CollectionTools.add(this.buildIntArray(), 30);
+ assertEquals(4, a.length);
+ assertTrue(CollectionTools.contains(a, 30));
+ }
+
+ public void testAddIntArrayIntInt() {
+ int[] a = new int[] { 1, 2, 3, 4 };
+ a = CollectionTools.add(a, 2, 99);
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, 99));
+ assertTrue(Arrays.equals(new int[] { 1, 2, 99, 3, 4 }, a));
+ }
+
+ public void testAddAllCollectionIterator1() {
+ List<String> l1 = this.buildStringList1();
+ List<String> l2 = this.buildStringList2();
+ Set<String> s1 = this.buildStringSet1();
+ List<String> l3 = this.buildStringList1(); // same elements as s1
+
+ assertTrue(CollectionTools.addAll(l1, l2.iterator()));
+ assertEquals(6, l1.size());
+ assertTrue(l1.containsAll(l2));
+
+ assertFalse(CollectionTools.addAll(s1, l3.iterator()));
+ assertEquals(3, s1.size());
+ assertTrue(s1.containsAll(l3));
+ }
+
+ public void testAddAllCollectionIterator2() {
+ List<Object> l1 = this.buildObjectList1();
+ List<String> l2 = this.buildStringList2();
+ Set<Object> s1 = this.buildObjectSet1();
+ List<String> l3 = this.buildStringList1(); // same elements as s1
+
+ assertTrue(CollectionTools.addAll(l1, l2.iterator()));
+ assertEquals(6, l1.size());
+ assertTrue(l1.containsAll(l2));
+
+ assertFalse(CollectionTools.addAll(s1, l3.iterator()));
+ assertEquals(3, s1.size());
+ assertTrue(s1.containsAll(l3));
+ }
+
+ public void testAddAllCollectionObjectArray1() {
+ List<String> l = this.buildStringList1();
+ String[] a = this.buildStringArray1();
+ Set<String> s = this.buildStringSet1();
+
+ assertTrue(CollectionTools.addAll(l, a));
+ assertEquals(6, l.size());
+ assertTrue(l.containsAll(CollectionTools.collection(a)));
+
+ assertFalse(CollectionTools.addAll(s, a));
+ assertEquals(3, s.size());
+ assertTrue(s.containsAll(CollectionTools.collection(a)));
+ }
+
+ public void testAddAllCollectionObjectArray2() {
+ List<Object> l = this.buildObjectList1();
+ String[] a = this.buildStringArray1();
+ Set<Object> s = this.buildObjectSet1();
+
+ assertTrue(CollectionTools.addAll(l, a));
+ assertEquals(6, l.size());
+ assertTrue(l.containsAll(CollectionTools.collection(a)));
+
+ assertFalse(CollectionTools.addAll(s, a));
+ assertEquals(3, s.size());
+ assertTrue(s.containsAll(CollectionTools.collection(a)));
+ }
+
+ public void testAddAllObjectArrayCollection1() {
+ String[] a = this.buildStringArray1();
+ Collection<String> c = this.buildStringList2();
+ String[] newArray = CollectionTools.addAll(a, c);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, c));
+ }
+
+ public void testAddAllObjectArrayCollection2() {
+ Object[] a = this.buildObjectArray1();
+ Collection<String> c = this.buildStringList2();
+ Object[] newArray = CollectionTools.addAll(a, c);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, c));
+ }
+
+ public void testAddAllObjectArrayIterator1() {
+ String[] a = this.buildStringArray1();
+ Iterator<String> i = this.buildStringList1().iterator();
+ String[] newArray = CollectionTools.addAll(a, i);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, this.buildStringList1()));
+ }
+
+ public void testAddAllObjectArrayIterator2() {
+ String[] a = this.buildStringArray1();
+ Iterator<Object> i = this.buildObjectList1().iterator();
+ Object[] newArray = CollectionTools.addAll(a, i);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, this.buildObjectList1()));
+ }
+
+ public void testAddAllObjectArrayObjectArray1() {
+ Object[] a1 = this.buildObjectArray1();
+ Object[] a2 = this.buildObjectArray2();
+ Object[] newArray = CollectionTools.addAll(a1, a2);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, a1));
+ assertTrue(CollectionTools.containsAll(newArray, a2));
+ }
+
+ public void testAddAllObjectArrayObjectArray2() {
+ String[] a1 = this.buildStringArray1();
+ String[] a2 = this.buildStringArray2();
+ String[] newArray = CollectionTools.addAll(a1, a2);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, a1));
+ assertTrue(CollectionTools.containsAll(newArray, a2));
+ }
+
+ public void testAddAllObjectArrayObjectArray3() {
+ Object[] a1 = this.buildObjectArray1();
+ String[] a2 = this.buildStringArray2();
+ Object[] newArray = CollectionTools.addAll(a1, a2);
+
+ assertEquals(6, newArray.length);
+ assertTrue(CollectionTools.containsAll(newArray, a1));
+ assertTrue(CollectionTools.containsAll(newArray, a2));
+ }
+
+ public void testAddAllObjectArrayIntObjectArray1() {
+ Object[] a = new Object[] { "a", "b", "c", "d" };
+ a = CollectionTools.addAll(a, 2, new Object[] { "X", "X", "X" });
+ assertEquals(7, a.length);
+ assertTrue(CollectionTools.contains(a, "X"));
+ assertTrue(Arrays.equals(new Object[] { "a", "b", "X", "X", "X", "c", "d" }, a));
+ }
+
+ public void testAddAllObjectArrayIntObjectArray2() {
+ String[] a = new String[] { "a", "b", "c", "d" };
+ a = CollectionTools.addAll(a, 2, new String[] { "X", "X", "X" });
+ assertEquals(7, a.length);
+ assertTrue(CollectionTools.contains(a, "X"));
+ assertTrue(Arrays.equals(new String[] { "a", "b", "X", "X", "X", "c", "d" }, a));
+ }
+
+ public void testAddAllObjectArrayIntObjectArray3() {
+ Object[] a = new Object[] { "a", "b", "c", "d" };
+ a = CollectionTools.addAll(a, 2, new String[] { "X", "X", "X" });
+ assertEquals(7, a.length);
+ assertTrue(CollectionTools.contains(a, "X"));
+ assertTrue(Arrays.equals(new Object[] { "a", "b", "X", "X", "X", "c", "d" }, a));
+ }
+
+ public void testAddAllCharArrayCharArray() {
+ char[] a = CollectionTools.addAll(this.buildCharArray(), new char[] { 'd', 'e' });
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, 'd'));
+ assertTrue(CollectionTools.contains(a, 'e'));
+ }
+
+ public void testAddAllCharArrayIntCharArray() {
+ char[] a = new char[] { 'a', 'b', 'c', 'd' };
+ a = CollectionTools.addAll(a, 2, new char[] { 'X', 'X', 'X' });
+ assertEquals(7, a.length);
+ assertTrue(CollectionTools.contains(a, 'X'));
+ assertTrue(Arrays.equals(new char[] { 'a', 'b', 'X', 'X', 'X', 'c', 'd' }, a));
+ }
+
+ public void testAddAllIntArrayIntArray() {
+ int[] a = CollectionTools.addAll(this.buildIntArray(), new int[] { 30, 40 });
+ assertEquals(5, a.length);
+ assertTrue(CollectionTools.contains(a, 30));
+ assertTrue(CollectionTools.contains(a, 40));
+ }
+
+ public void testAddAllIntArrayIntIntArray() {
+ int[] a = new int[] { 1, 2, 3, 4 };
+ a = CollectionTools.addAll(a, 2, new int[] { 99, 99, 99 });
+ assertEquals(7, a.length);
+ assertTrue(CollectionTools.contains(a, 99));
+ assertTrue(Arrays.equals(new int[] { 1, 2, 99, 99, 99, 3, 4 }, a));
+ }
+
+ public void testArrayIterator() {
+ Object[] a = CollectionTools.array(this.buildStringList1().iterator());
+ assertEquals(3, a.length);
+ assertTrue(CollectionTools.containsAll(a, this.buildStringList1().iterator()));
+ }
+
+ public void testArrayIteratorObjectArray1() {
+ String[] a = CollectionTools.array(this.buildStringList1().iterator(), new String[0]);
+ assertEquals(3, a.length);
+ assertTrue(CollectionTools.containsAll(a, this.buildStringList1().iterator()));
+ }
+
+ public void testArrayIteratorObjectArray2() {
+ Object[] a = CollectionTools.array(this.buildStringList1().iterator(), new Object[0]);
+ assertEquals(3, a.length);
+ assertTrue(CollectionTools.containsAll(a, this.buildStringList1().iterator()));
+ }
+
+ public void testBagEnumeration1() {
+ Bag<String> b = CollectionTools.bag(this.buildStringVector1().elements());
+ assertEquals(3, b.size());
+ assertTrue(b.containsAll(this.buildStringVector1()));
+ }
+
+ public void testBagEnumeration2() {
+ Bag<Object> b = CollectionTools.<Object>bag(this.buildStringVector1().elements());
+ assertEquals(3, b.size());
+ assertTrue(b.containsAll(this.buildStringVector1()));
+ }
+
+ public void testBagIterator1() {
+ Bag<String> b = CollectionTools.bag(this.buildStringList1().iterator());
+ assertEquals(3, b.size());
+ assertTrue(b.containsAll(this.buildStringList1()));
+ }
+
+ public void testBagIterator2() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ Bag<Object> b = CollectionTools.<Object>bag(c.iterator());
+ assertEquals(4, b.size());
+ assertTrue(b.containsAll(c));
+ }
+
+ public void testBagObjectArray1() {
+ Bag<String> b = CollectionTools.bag(this.buildStringArray1());
+ assertEquals(3, b.size());
+ assertTrue(CollectionTools.containsAll(b, this.buildStringArray1()));
+ }
+
+ public void testBagObjectArray2() {
+ Bag<String> b = CollectionTools.bag("foo", "bar", "baz");
+ assertEquals(3, b.size());
+ assertTrue(CollectionTools.containsAll(b, new Object[]{"foo", "bar", "baz"}));
+ }
+
+ public void testCollectionEnumeration1() {
+ Collection<String> c = CollectionTools.collection(this.buildStringVector1().elements());
+ assertEquals(3, c.size());
+ assertTrue(c.containsAll(this.buildStringVector1()));
+ }
+
+ public void testCollectionEnumeration2() {
+ Collection<Object> c = CollectionTools.<Object>collection(this.buildStringVector1().elements());
+ assertEquals(3, c.size());
+ assertTrue(c.containsAll(this.buildStringVector1()));
+ }
+
+ public void testCollectionIterator1() {
+ Collection<String> c = CollectionTools.collection(this.buildStringList1().iterator());
+ assertEquals(3, c.size());
+ assertTrue(c.containsAll(this.buildStringList1()));
+ }
+
+ public void testCollectionIterator2() {
+ Collection<Object> c = CollectionTools.<Object>collection(this.buildStringList1().iterator());
+ assertEquals(3, c.size());
+ assertTrue(c.containsAll(this.buildStringList1()));
+ }
+
+ public void testCollectionObjectArray() {
+ Collection<String> c = CollectionTools.collection(this.buildStringArray1());
+ assertEquals(3, c.size());
+ assertTrue(CollectionTools.containsAll(c, this.buildStringArray1()));
+ }
+
+ public void testContainsEnumerationObject1() {
+ Vector<String> v = this.buildStringVector1();
+ assertTrue(CollectionTools.contains(v.elements(), "one"));
+ assertFalse(CollectionTools.contains(v.elements(), null));
+ v.add(null);
+ assertTrue(CollectionTools.contains(v.elements(), null));
+ }
+
+ public void testContainsEnumerationObject2() {
+ Vector<Object> c = new Vector<Object>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ String one = "one";
+ assertTrue(CollectionTools.contains(c.elements(), one));
+ assertFalse(CollectionTools.contains(c.elements(), null));
+ c.add(null);
+ assertTrue(CollectionTools.contains(c.elements(), null));
+ }
+
+ public void testContainsIteratorObject1() {
+ Collection<String> c = this.buildStringList1();
+ assertTrue(CollectionTools.contains(c.iterator(), "one"));
+ assertFalse(CollectionTools.contains(c.iterator(), null));
+ c.add(null);
+ assertTrue(CollectionTools.contains(c.iterator(), null));
+ }
+
+ public void testContainsIteratorObject2() {
+ Collection<Object> c = new HashBag<Object>();
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ String one = "one";
+ assertTrue(CollectionTools.contains(c.iterator(), one));
+ assertFalse(CollectionTools.contains(c.iterator(), null));
+ c.add(null);
+ assertTrue(CollectionTools.contains(c.iterator(), null));
+ }
+
+ public void testContainsObjectArrayObject() {
+ Object[] a = this.buildObjectArray1();
+ assertTrue(CollectionTools.contains(a, "one"));
+ assertFalse(CollectionTools.contains(a, null));
+ Object[] a2 = CollectionTools.add(a, null);
+ assertTrue(CollectionTools.contains(a2, null));
+ }
+
+ public void testContainsCharArrayChar() {
+ char[] a = this.buildCharArray();
+ assertTrue(CollectionTools.contains(a, 'a'));
+ assertFalse(CollectionTools.contains(a, 'z'));
+ char[] a2 = CollectionTools.add(a, 'z');
+ assertTrue(CollectionTools.contains(a2, 'z'));
+ }
+
+ public void testContainsIntArrayInt() {
+ int[] a = this.buildIntArray();
+ assertTrue(CollectionTools.contains(a, 10));
+ assertFalse(CollectionTools.contains(a, 55));
+ int[] a2 = CollectionTools.add(a, 55);
+ assertTrue(CollectionTools.contains(a2, 55));
+ }
+
+ public void testContainsAllCollectionIterator1() {
+ assertTrue(CollectionTools.containsAll(this.buildStringList1(), this.buildStringList1().iterator()));
+ }
+
+ public void testContainsAllCollectionIterator2() {
+ Collection<Object> c1 = new ArrayList<Object>();
+ c1.add("zero");
+ c1.add("one");
+ c1.add("two");
+ Collection<String> c2 = new ArrayList<String>();
+ c2.add("two");
+ c2.add("zero");
+ c2.add("one");
+ assertTrue(CollectionTools.containsAll(c1, c2.iterator()));
+ }
+
+ public void testContainsAllCollectionObjectArray1() {
+ assertTrue(CollectionTools.containsAll(this.buildStringList1(), this.buildObjectArray1()));
+ }
+
+ public void testContainsAllCollectionObjectArray2() {
+ Object[] a = new Object[] { "zero", "one", "two" };
+ assertTrue(CollectionTools.containsAll(this.buildStringList1(), a));
+ }
+
+ public void testContainsAllIteratorCollection1() {
+ assertTrue(CollectionTools.containsAll(this.buildStringList1().iterator(), this.buildStringList1()));
+ }
+
+ public void testContainsAllIteratorCollection2() {
+ Collection<Object> c1 = new ArrayList<Object>();
+ c1.add("zero");
+ c1.add("one");
+ c1.add("two");
+ Collection<String> c2 = new ArrayList<String>();
+ c2.add("zero");
+ c2.add("one");
+ c2.add("two");
+ assertTrue(CollectionTools.containsAll(c1.iterator(), c2));
+ }
+
+ public void testContainsAllIteratorIterator1() {
+ assertTrue(CollectionTools.containsAll(this.buildStringList1().iterator(), this.buildStringList1().iterator()));
+ }
+
+ public void testContainsAllIteratorIterator2() {
+ Collection<Object> c1 = new ArrayList<Object>();
+ c1.add("zero");
+ c1.add("one");
+ c1.add("two");
+ Collection<String> c2 = new ArrayList<String>();
+ c2.add("zero");
+ c2.add("one");
+ c2.add("two");
+ assertTrue(CollectionTools.containsAll(c1.iterator(), c2.iterator()));
+ }
+
+ public void testContainsAllIteratorObjectArray() {
+ assertTrue(CollectionTools.containsAll(this.buildStringList1().iterator(), this.buildObjectArray1()));
+ }
+
+ public void testContainsAllObjectArrayCollection() {
+ assertTrue(CollectionTools.containsAll(this.buildObjectArray1(), this.buildStringList1()));
+ }
+
+ public void testContainsAllObjectArrayIterator() {
+ assertTrue(CollectionTools.containsAll(this.buildObjectArray1(), this.buildStringList1().iterator()));
+ }
+
+ public void testContainsAllObjectArrayObjectArray() {
+ assertTrue(CollectionTools.containsAll(this.buildObjectArray1(), this.buildObjectArray1()));
+ }
+
+ public void testContainsAllCharArrayCharArray() {
+ assertTrue(CollectionTools.containsAll(this.buildCharArray(), this.buildCharArray()));
+ }
+
+ public void testContainsAllIntArrayIntArray() {
+ assertTrue(CollectionTools.containsAll(this.buildIntArray(), this.buildIntArray()));
+ }
+
+ public void testDiffEnd() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = new String("a");
+ String b_ = new String("b");
+ String c_ = new String("c");
+ String d_ = new String("d");
+ String e_ = new String("e");
+ assertTrue((a != a_) && a.equals(a_));
+ assertTrue((b != b_) && b.equals(b_));
+ assertTrue((c != c_) && c.equals(c_));
+ assertTrue((d != d_) && d.equals(d_));
+ assertTrue((e != e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(-1, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(-1, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(4, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(-1, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, a_, d_, e_ };
+ assertEquals(2, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(3, CollectionTools.diffEnd(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c_));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(-1, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(2, CollectionTools.diffEnd(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(-1, CollectionTools.diffEnd(array1, array2));
+ }
+
+ public void testDiffRange() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = a;
+ String b_ = b;
+ String c_ = c;
+ String d_ = d;
+ String e_ = e;
+ assertTrue((a == a_) && a.equals(a_));
+ assertTrue((b == b_) && b.equals(b_));
+ assertTrue((c == c_) && c.equals(c_));
+ assertTrue((d == d_) && d.equals(d_));
+ assertTrue((e == e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(5, -1), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(new Range(1, -1), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(new Range(0, 4), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(new Range(0, -1), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, a_, d_, e_ };
+ assertEquals(new Range(0, 2), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(new Range(0, 0), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(new Range(3, 3), CollectionTools.diffRange(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c_));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(new Range(5, -1), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(2, 2), CollectionTools.diffRange(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(new Range(5, -1), CollectionTools.diffRange(array1, array2));
+ }
+
+ public void testDiffStart() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = new String("a");
+ String b_ = new String("b");
+ String c_ = new String("c");
+ String d_ = new String("d");
+ String e_ = new String("e");
+ assertTrue((a != a_) && a.equals(a_));
+ assertTrue((b != b_) && b.equals(b_));
+ assertTrue((c != c_) && c.equals(c_));
+ assertTrue((d != d_) && d.equals(d_));
+ assertTrue((e != e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(5, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(1, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(4, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(0, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(0, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, e_, c_, d_ };
+ assertEquals(2, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(3, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.diffStart(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c__));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(5, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(2, CollectionTools.diffStart(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(5, CollectionTools.diffStart(array1, array2));
+ }
+
+ public void testEqualsListIteratorListIterator() {
+ List<String> list1 = new ArrayList<String>();
+ list1.add("1000");
+ list1.add("2000");
+ list1.add("3000");
+ list1.add("4000");
+
+ List<String> list2 = new ArrayList<String>();
+ for (int i = 0; i < list1.size(); i++) {
+ list2.add(String.valueOf((i + 1) * 1000));
+ }
+ assertFalse(CollectionTools.identical(list1.listIterator(), list2.listIterator()));
+ assertTrue(CollectionTools.equals(list1.listIterator(), list2.listIterator()));
+ }
+
+ public void testGetListIteratorInt() {
+ List<String> list = this.buildStringList1();
+ String o = CollectionTools.get(list.listIterator(), 1);
+ assertEquals("one", o);
+ list.add(null);
+ o = CollectionTools.get(list.listIterator(), list.size() - 1);
+ assertNull(o);
+
+ boolean exCaught = false;
+ try {
+ CollectionTools.get(list.listIterator(), list.size());
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testIdenticalObjectArrayObjectArray() {
+ Object[] a1 = new Object[4];
+ for (int i = 0; i < a1.length; i++) {
+ a1[i] = String.valueOf(i * 1000);
+ }
+
+ Object[] a2 = new Object[a1.length];
+ for (int i = 0; i < a2.length; i++) {
+ a2[i] = a1[i];
+ }
+
+ assertTrue(CollectionTools.identical(a1, a2));
+ a2[2] = "2000";
+ assertFalse(CollectionTools.identical(a1, a2));
+ assertTrue(Arrays.equals(a1, a2));
+ }
+
+ public void testIdenticalListIteratorListIterator() {
+ List<String> list1 = new ArrayList<String>();
+ list1.add("0");
+ list1.add("1");
+ list1.add("2");
+ list1.add("3");
+
+ List<String> list2 = new ArrayList<String>();
+ for (String s : list1) {
+ list2.add(s);
+ }
+ assertTrue(CollectionTools.identical(list1.listIterator(), list2.listIterator()));
+ assertTrue(CollectionTools.equals(list1.listIterator(), list2.listIterator()));
+ }
+
+ public void testIdentityDiffEnd() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = a;
+ String b_ = b;
+ String c_ = c;
+ String d_ = d;
+ String e_ = e;
+ assertTrue((a == a_) && a.equals(a_));
+ assertTrue((b == b_) && b.equals(b_));
+ assertTrue((c == c_) && c.equals(c_));
+ assertTrue((d == d_) && d.equals(d_));
+ assertTrue((e == e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(-1, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(-1, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(4, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(-1, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, a_, d_, e_ };
+ assertEquals(2, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(3, CollectionTools.identityDiffEnd(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c_));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(2, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(2, CollectionTools.identityDiffEnd(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(-1, CollectionTools.identityDiffEnd(array1, array2));
+ }
+
+ public void testIdentityDiffRange() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = a;
+ String b_ = b;
+ String c_ = c;
+ String d_ = d;
+ String e_ = e;
+ assertTrue((a == a_) && a.equals(a_));
+ assertTrue((b == b_) && b.equals(b_));
+ assertTrue((c == c_) && c.equals(c_));
+ assertTrue((d == d_) && d.equals(d_));
+ assertTrue((e == e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(5, -1), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(new Range(1, -1), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(0, 4), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(new Range(0, 4), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(new Range(0, -1), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { b_, c_, a_, d_, e_ };
+ assertEquals(new Range(0, 2), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(new Range(0, 0), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(new Range(3, 3), CollectionTools.identityDiffRange(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c_));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(new Range(2, 2), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(new Range(2, 2), CollectionTools.identityDiffRange(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(new Range(5, -1), CollectionTools.identityDiffRange(array1, array2));
+ }
+
+ public void testIdentityDiffStart() {
+ String a = "a";
+ String b = "b";
+ String c = "c";
+ String d = "d";
+ String e = "e";
+ String a_ = a;
+ String b_ = b;
+ String c_ = c;
+ String d_ = d;
+ String e_ = e;
+ assertTrue((a == a_) && a.equals(a_));
+ assertTrue((b == b_) && b.equals(b_));
+ assertTrue((c == c_) && c.equals(c_));
+ assertTrue((d == d_) && d.equals(d_));
+ assertTrue((e == e_) && e.equals(e_));
+ String[] array1;
+ String[] array2;
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(5, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a };
+ array2 = new String[] { a_ };
+ assertEquals(1, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(4, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(4, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[0];
+ assertEquals(0, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[0];
+ array2 = new String[0];
+ assertEquals(0, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, e_, c_, d_ };
+ assertEquals(2, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, c, e };
+ array2 = new String[] { a_, b_, c_, d_ };
+ assertEquals(3, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { b, c, d, e };
+ array2 = new String[] { a_, c_, d_, e_ };
+ assertEquals(0, CollectionTools.identityDiffStart(array1, array2));
+
+ String c__ = new String(c);
+ assertTrue((c != c__) && c.equals(c_));
+ array1 = new String[] { a, b, c, d, e };
+ array2 = new String[] { a_, b_, c__, d_, e_ };
+ assertEquals(2, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, c_, d_, e_ };
+ assertEquals(2, CollectionTools.identityDiffStart(array1, array2));
+
+ array1 = new String[] { a, b, null, d, e };
+ array2 = new String[] { a_, b_, null, d_, e_ };
+ assertEquals(5, CollectionTools.identityDiffStart(array1, array2));
+ }
+
+ public void testIndexOfListIteratorObject1() {
+ List<String> list = this.buildStringList1();
+ assertEquals(1, CollectionTools.indexOf(list.listIterator(), "one"));
+ list.add(null);
+ assertEquals(list.size() - 1, CollectionTools.indexOf(list.listIterator(), null));
+ }
+
+ public void testIndexOfListIteratorObject2() {
+ List<Object> list = new ArrayList<Object>();
+ list.add("0");
+ list.add("1");
+ list.add("2");
+ list.add("3");
+
+ String one = "1";
+ assertEquals(1, CollectionTools.indexOf(list.listIterator(), one));
+ list.add(null);
+ assertEquals(list.size() - 1, CollectionTools.indexOf(list.listIterator(), null));
+ }
+
+ public void testIndexOfObjectArrayObject() {
+ Object[] a = this.buildObjectArray1();
+ assertEquals(1, CollectionTools.indexOf(a, "one"));
+ a = CollectionTools.add(a, null);
+ assertEquals(a.length - 1, CollectionTools.indexOf(a, null));
+ }
+
+ public void testIndexOfCharArrayChar() {
+ char[] a = this.buildCharArray();
+ assertEquals(1, CollectionTools.indexOf(a, 'b'));
+ a = CollectionTools.add(a, 'd');
+ assertEquals(a.length - 1, CollectionTools.indexOf(a, 'd'));
+ }
+
+ public void testIndexOfIntArrayInt() {
+ int[] a = this.buildIntArray();
+ assertEquals(1, CollectionTools.indexOf(a, 10));
+ a = CollectionTools.add(a, 30);
+ assertEquals(a.length - 1, CollectionTools.indexOf(a, 30));
+ }
+
+ public void testInsertionIndexOfListComparable() {
+ List<String> list = Arrays.asList(new String[] { "A", "C", "D" });
+ assertEquals(1, CollectionTools.insertionIndexOf(list, "B"));
+
+ list = Arrays.asList(new String[] { "A", "B", "C", "D" });
+ assertEquals(2, CollectionTools.insertionIndexOf(list, "B"));
+
+ list = Arrays.asList(new String[] { "A", "B", "B", "B", "C", "D" });
+ assertEquals(4, CollectionTools.insertionIndexOf(list, "B"));
+
+ list = Arrays.asList(new String[] { "A", "B", "B", "B", "C", "D" });
+ assertEquals(6, CollectionTools.insertionIndexOf(list, "E"));
+
+ list = Arrays.asList(new String[] { "B", "B", "B", "C", "D" });
+ assertEquals(0, CollectionTools.insertionIndexOf(list, "A"));
+
+ list = Arrays.asList(new String[] { "A", "A", "B", "B", "C", "D" });
+ assertEquals(2, CollectionTools.insertionIndexOf(list, "A"));
+ }
+
+ public void testInsertionIndexOfListObjectComparator() {
+ Comparator<String> c = new ReverseComparator<String>();
+ List<String> list = Arrays.asList(new String[] { "D", "C", "A" });
+ assertEquals(2, CollectionTools.insertionIndexOf(list, "B", c));
+
+ list = Arrays.asList(new String[] { "D", "C", "B", "A" });
+ assertEquals(3, CollectionTools.insertionIndexOf(list, "B", c));
+
+ list = Arrays.asList(new String[] { "D", "C", "B", "B", "B", "A" });
+ assertEquals(5, CollectionTools.insertionIndexOf(list, "B", c));
+
+ list = Arrays.asList(new String[] { "D", "C", "B", "B", "B", "A" });
+ assertEquals(0, CollectionTools.insertionIndexOf(list, "E", c));
+
+ list = Arrays.asList(new String[] { "D", "C", "B", "B", "B" });
+ assertEquals(5, CollectionTools.insertionIndexOf(list, "A", c));
+
+ list = Arrays.asList(new String[] { "D", "C", "B", "B", "A", "A" });
+ assertEquals(6, CollectionTools.insertionIndexOf(list, "A", c));
+ }
+
+ public void testInsertionIndexOfObjectArrayComparable() {
+ String[] a = new String[] { "A", "C", "D" };
+ assertEquals(1, CollectionTools.insertionIndexOf(a, "B"));
+
+ a = new String[] { "A", "B", "C", "D" };
+ assertEquals(2, CollectionTools.insertionIndexOf(a, "B"));
+
+ a = new String[] { "A", "B", "B", "B", "C", "D" };
+ assertEquals(4, CollectionTools.insertionIndexOf(a, "B"));
+
+ a = new String[] { "A", "B", "B", "B", "C", "D" };
+ assertEquals(6, CollectionTools.insertionIndexOf(a, "E"));
+
+ a = new String[] { "B", "B", "B", "C", "D" };
+ assertEquals(0, CollectionTools.insertionIndexOf(a, "A"));
+
+ a = new String[] { "A", "A", "B", "B", "C", "D" };
+ assertEquals(2, CollectionTools.insertionIndexOf(a, "A"));
+ }
+
+ public void testInsertionIndexOfObjectArrayObjectComparator() {
+ Comparator<String> c = new ReverseComparator<String>();
+ String[] a = new String[] { "D", "C", "A" };
+ assertEquals(2, CollectionTools.insertionIndexOf(a, "B", c));
+
+ a = new String[] { "D", "C", "B", "A" };
+ assertEquals(3, CollectionTools.insertionIndexOf(a, "B", c));
+
+ a = new String[] { "D", "C", "B", "B", "B", "A" };
+ assertEquals(5, CollectionTools.insertionIndexOf(a, "B", c));
+
+ a = new String[] { "D", "C", "B", "B", "B", "A" };
+ assertEquals(0, CollectionTools.insertionIndexOf(a, "E", c));
+
+ a = new String[] { "D", "C", "B", "B", "B" };
+ assertEquals(5, CollectionTools.insertionIndexOf(a, "A", c));
+
+ a = new String[] { "D", "C", "B", "B", "A", "A" };
+ assertEquals(6, CollectionTools.insertionIndexOf(a, "A", c));
+ }
+
+ public void testIteratorObjectArray() {
+ String[] a = this.buildStringArray1();
+ int i = 0;
+ for (Iterator<String> stream = CollectionTools.iterator(a); stream.hasNext(); i++) {
+ assertEquals(a[i], stream.next());
+ }
+ }
+
+ public void testLastIndexOfListIteratorObject() {
+ List<String> list = this.buildStringList1();
+ assertEquals(1, CollectionTools.lastIndexOf(list.listIterator(), "one"));
+ list.add(null);
+ assertEquals(list.size() - 1, CollectionTools.lastIndexOf(list.listIterator(), null));
+ }
+
+ public void testLastIndexOfObjectArrayObject() {
+ Object[] a = this.buildObjectArray1();
+ assertEquals(1, CollectionTools.lastIndexOf(a, "one"));
+ a = CollectionTools.add(a, null);
+ assertEquals(a.length - 1, CollectionTools.lastIndexOf(a, null));
+ }
+
+ public void testLastIndexOfCharArrayChar() {
+ char[] a = this.buildCharArray();
+ assertEquals(1, CollectionTools.lastIndexOf(a, 'b'));
+ a = CollectionTools.add(a, 'd');
+ assertEquals(a.length - 1, CollectionTools.lastIndexOf(a, 'd'));
+ }
+
+ public void testLastIndexOfIntArrayInt() {
+ int[] a = this.buildIntArray();
+ assertEquals(1, CollectionTools.lastIndexOf(a, 10));
+ a = CollectionTools.add(a, 30);
+ assertEquals(a.length - 1, CollectionTools.lastIndexOf(a, 30));
+ }
+
+ public void testListIterator1() {
+ List<String> list = CollectionTools.list(this.buildStringList1().iterator());
+ assertEquals(this.buildStringList1(), list);
+ }
+
+ public void testListIterator2() {
+ List<String> list1 = new ArrayList<String>();
+ list1.add("0");
+ list1.add("1");
+ list1.add("2");
+ list1.add("3");
+
+ List<Object> list2 = CollectionTools.<Object>list(list1.iterator());
+ assertEquals(list1, list2);
+ }
+
+ public void testListObjectArray() {
+ List<String> list = CollectionTools.list(this.buildStringArray1());
+ assertEquals(this.buildStringList1(), list);
+ }
+
+ public void testListIteratorObjectArray() {
+ String[] a = this.buildStringArray1();
+ int i = 0;
+ for (ListIterator<String> stream = CollectionTools.listIterator(a); stream.hasNext(); i++) {
+ assertEquals(a[i], stream.next());
+ }
+ }
+
+ public void testListIteratorObjectArrayInt() {
+ String[] a = this.buildStringArray1();
+ int i = 1;
+ for (ListIterator<String> stream = CollectionTools.listIterator(a, 1); stream.hasNext(); i++) {
+ assertEquals(a[i], stream.next());
+ }
+ }
+
+ public void testMaxCharArray() {
+ assertEquals('c', CollectionTools.max(this.buildCharArray()));
+ }
+
+ public void testMaxIntArray() {
+ assertEquals(20, CollectionTools.max(this.buildIntArray()));
+ }
+
+ public void testMinCharArray() {
+ assertEquals('a', CollectionTools.min(this.buildCharArray()));
+ }
+
+ public void testMinIntArray() {
+ assertEquals(0, CollectionTools.min(this.buildIntArray()));
+ }
+
+ public void testRemoveAllObjectArrayObjectArray() {
+ String[] a1 = new String[] { "A", "A", "B", "B", "C", "C", "D", "D", "E", "E", "F", "F" };
+ String[] a2 = new String[] { "E", "B" };
+ String[] a3 = CollectionTools.removeAll(a1, a2);
+ assertTrue(Arrays.equals(new String[] { "A", "A", "C", "C", "D", "D", "F", "F" }, a3));
+ }
+
+ public void testRemoveAllCharArrayCharArray() {
+ char[] a1 = new char[] { 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F' };
+ char[] a2 = new char[] { 'E', 'B' };
+ assertTrue(Arrays.equals(new char[] { 'A', 'A', 'C', 'C', 'D', 'D', 'F', 'F' }, CollectionTools.removeAll(a1, a2)));
+ }
+
+ public void testRemoveAllIntArrayIntArray() {
+ int[] a1 = new int[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6 };
+ int[] a2 = new int[] { 5, 2 };
+ assertTrue(Arrays.equals(new int[] { 1, 1, 3, 3, 4, 4, 6, 6 }, CollectionTools.removeAll(a1, a2)));
+ }
+
+ public void testRemoveObjectArrayObject1() {
+ Object[] a = this.buildObjectArray1();
+ a = CollectionTools.add(a, "three");
+ a = CollectionTools.add(a, "four");
+ a = CollectionTools.add(a, "five");
+
+ assertEquals(6, a.length);
+ assertTrue(CollectionTools.contains(a, "three"));
+ a = CollectionTools.remove(a, "three");
+ assertEquals(5, a.length);
+ assertFalse(CollectionTools.contains(a, "three"));
+ assertTrue(CollectionTools.contains(a, "four"));
+ assertTrue(CollectionTools.contains(a, "five"));
+ }
+
+ public void testRemoveObjectArrayObject2() {
+ String[] a = this.buildStringArray1();
+ a = CollectionTools.add(a, "three");
+ a = CollectionTools.add(a, "four");
+ a = CollectionTools.add(a, "five");
+
+ assertEquals(6, a.length);
+ assertTrue(CollectionTools.contains(a, "three"));
+ a = CollectionTools.remove(a, "three");
+ assertEquals(5, a.length);
+ assertFalse(CollectionTools.contains(a, "three"));
+ assertTrue(CollectionTools.contains(a, "four"));
+ assertTrue(CollectionTools.contains(a, "five"));
+ }
+
+ public void testRemoveCharArrayChar() {
+ char[] a = this.buildCharArray();
+ a = CollectionTools.add(a, 'd');
+ a = CollectionTools.add(a, 'e');
+ a = CollectionTools.add(a, 'f');
+
+ assertEquals(6, a.length);
+ assertTrue(CollectionTools.contains(a, 'd'));
+ a = CollectionTools.remove(a, 'd');
+ assertEquals(5, a.length);
+ assertFalse(CollectionTools.contains(a, 'd'));
+ assertTrue(CollectionTools.contains(a, 'e'));
+ assertTrue(CollectionTools.contains(a, 'f'));
+ }
+
+ public void testRemoveIntArrayInt() {
+ int[] a = this.buildIntArray();
+ a = CollectionTools.add(a, 30);
+ a = CollectionTools.add(a, 40);
+ a = CollectionTools.add(a, 50);
+
+ assertEquals(6, a.length);
+ assertTrue(CollectionTools.contains(a, 30));
+ a = CollectionTools.remove(a, 30);
+ assertEquals(5, a.length);
+ assertFalse(CollectionTools.contains(a, 30));
+ assertTrue(CollectionTools.contains(a, 40));
+ assertTrue(CollectionTools.contains(a, 50));
+ }
+
+ public void testRemoveAllCollectionIterator1() {
+ Collection<String> c = this.buildStringList1();
+ assertTrue(CollectionTools.removeAll(c, this.buildStringList1().iterator()));
+ assertEquals(0, c.size());
+ assertFalse(c.contains("one"));
+ assertFalse(c.contains("two"));
+ assertFalse(c.contains("three"));
+
+ c = this.buildStringList1();
+ assertFalse(CollectionTools.removeAll(c, this.buildStringList2().iterator()));
+ assertEquals(this.buildStringList1().size(), c.size());
+ assertEquals(this.buildStringList1(), c);
+ }
+
+ public void testRemoveAllCollectionIterator2() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("a");
+ c.add("a");
+ c.add("b");
+ c.add("c");
+ c.add("d");
+ c.add("d");
+ String[] a = new String[] { "a", "d" };
+ assertTrue(CollectionTools.removeAll(c, new ArrayIterator<String>(a)));
+ assertEquals(2, c.size());
+ assertFalse(c.contains("a"));
+ assertTrue(c.contains("b"));
+ assertTrue(c.contains("c"));
+ assertFalse(c.contains("d"));
+
+ assertFalse(CollectionTools.removeAll(c, new ArrayIterator<String>(a)));
+ }
+
+ public void testRemoveAllCollectionIterator3() {
+ Collection<Object> c = new ArrayList<Object>();
+ c.add("a");
+ c.add("a");
+ c.add("b");
+ c.add("c");
+ c.add("d");
+ c.add("d");
+ String[] a = new String[] { "a", "d" };
+ assertTrue(CollectionTools.removeAll(c, new ArrayIterator<String>(a)));
+ assertEquals(2, c.size());
+ assertFalse(c.contains("a"));
+ assertTrue(c.contains("b"));
+ assertTrue(c.contains("c"));
+ assertFalse(c.contains("d"));
+
+ assertFalse(CollectionTools.removeAll(c, new ArrayIterator<String>(a)));
+ }
+
+ public void testRemoveAllCollectionObjectArray1() {
+ Collection<String> c = this.buildStringList1();
+ assertTrue(CollectionTools.removeAll(c, this.buildObjectArray1()));
+ assertEquals(0, c.size());
+ assertFalse(c.contains("one"));
+ assertFalse(c.contains("two"));
+ assertFalse(c.contains("three"));
+
+ c = this.buildStringList1();
+ assertFalse(CollectionTools.removeAll(c, this.buildObjectArray2()));
+ assertEquals(this.buildStringList1().size(), c.size());
+ assertEquals(this.buildStringList1(), c);
+ }
+
+ public void testRemoveAllCollectionObjectArray2() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("a");
+ c.add("a");
+ c.add("b");
+ c.add("c");
+ c.add("d");
+ c.add("d");
+ String[] a = new String[] { "a", "d" };
+ assertTrue(CollectionTools.removeAll(c, a));
+ assertEquals(2, c.size());
+ assertFalse(c.contains("a"));
+ assertTrue(c.contains("b"));
+ assertTrue(c.contains("c"));
+ assertFalse(c.contains("d"));
+
+ assertFalse(CollectionTools.removeAll(c, a));
+ }
+
+ public void testRemoveAllCollectionObjectArray3() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("a");
+ c.add("b");
+ c.add("c");
+ c.add("d");
+ c.add("a");
+ c.add("d");
+ c.add("d");
+ c.add("a");
+ c.add("c");
+ String[] a = new String[] { "a", "d" };
+ assertTrue(CollectionTools.removeAll(c, a));
+ assertEquals(3, c.size());
+ assertFalse(c.contains("a"));
+ assertTrue(c.contains("b"));
+ assertTrue(c.contains("c"));
+ assertFalse(c.contains("d"));
+
+ assertFalse(CollectionTools.removeAll(c, a));
+ }
+
+ public void testRemoveAllOccurrencesCollectionObject() {
+ Collection<String> c = this.buildStringList1();
+ assertEquals(3, c.size());
+ assertFalse(CollectionTools.removeAllOccurrences(c, "three"));
+ assertTrue(CollectionTools.removeAllOccurrences(c, "two"));
+ assertFalse(CollectionTools.removeAllOccurrences(c, "two"));
+ assertEquals(2, c.size());
+
+ c.add("five");
+ c.add("five");
+ c.add("five");
+ assertEquals(5, c.size());
+ assertTrue(CollectionTools.removeAllOccurrences(c, "five"));
+ assertFalse(CollectionTools.removeAllOccurrences(c, "five"));
+ assertEquals(2, c.size());
+
+ c.add(null);
+ c.add(null);
+ c.add(null);
+ assertEquals(5, c.size());
+ assertTrue(CollectionTools.removeAllOccurrences(c, null));
+ assertFalse(CollectionTools.removeAllOccurrences(c, null));
+ assertEquals(2, c.size());
+ }
+
+ public void testRemoveAllOccurrencesObjectArrayObject() {
+ String[] a = this.buildStringArray1();
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, "three");
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, "two");
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, "two");
+ assertEquals(2, a.length);
+
+ a = CollectionTools.add(a, "five");
+ a = CollectionTools.add(a, "five");
+ a = CollectionTools.add(a, "five");
+ assertEquals(5, a.length);
+ a = CollectionTools.removeAllOccurrences(a, "five");
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, "five");
+ assertEquals(2, a.length);
+
+ a = CollectionTools.add(a, null);
+ a = CollectionTools.add(a, null);
+ a = CollectionTools.add(a, null);
+ assertEquals(5, a.length);
+ a = CollectionTools.removeAllOccurrences(a, null);
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, null);
+ assertEquals(2, a.length);
+ }
+
+ public void testRemoveAllOccurrencesCharArrayChar() {
+ char[] a = this.buildCharArray();
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 'd');
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 'b');
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 'b');
+ assertEquals(2, a.length);
+
+ a = CollectionTools.add(a, 'g');
+ a = CollectionTools.add(a, 'g');
+ a = CollectionTools.add(a, 'g');
+ assertEquals(5, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 'g');
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 'g');
+ assertEquals(2, a.length);
+ }
+
+ public void testRemoveAllOccurrencesIntArrayInt() {
+ int[] a = this.buildIntArray();
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 55);
+ assertEquals(3, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 10);
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 10);
+ assertEquals(2, a.length);
+
+ a = CollectionTools.add(a, 77);
+ a = CollectionTools.add(a, 77);
+ a = CollectionTools.add(a, 77);
+ assertEquals(5, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 77);
+ assertEquals(2, a.length);
+ a = CollectionTools.removeAllOccurrences(a, 77);
+ assertEquals(2, a.length);
+ }
+
+ public void testRemoveElementAtIndexObjectArrayInt() {
+ String[] a = new String[] { "A", "B", "A", "C", "A", "D" };
+ a = CollectionTools.removeElementAtIndex(a, 3);
+ assertTrue(Arrays.equals(new String[] { "A", "B", "A", "A", "D" }, a));
+ }
+
+ public void testRemoveElementAtIndexCharArrayInt() {
+ char[] a = new char[] { 'A', 'B', 'A', 'C', 'A', 'D' };
+ a = CollectionTools.removeElementAtIndex(a, 3);
+ assertTrue(Arrays.equals(new char[] { 'A', 'B', 'A', 'A', 'D' }, a));
+ }
+
+ public void testRemoveElementAtIndexIntArrayInt() {
+ int[] a = new int[] { 8, 6, 7, 33, 2, 11 };
+ a = CollectionTools.removeElementAtIndex(a, 3);
+ assertTrue(Arrays.equals(new int[] { 8, 6, 7, 2, 11 }, a));
+ }
+
+ public void testReplaceAllObjectArray1() {
+ Object[] a = new Object[] { "A", "B", "A", "C", "A", "D" };
+ a = CollectionTools.replaceAll(a, "A", "Z");
+ assertTrue(Arrays.equals(new Object[] { "Z", "B", "Z", "C", "Z", "D" }, a));
+ }
+
+ public void testReplaceAllObjectArray2() {
+ String[] a = new String[] { "A", "B", "A", "C", "A", "D" };
+ a = CollectionTools.replaceAll(a, "A", "Z");
+ assertTrue(Arrays.equals(new String[] { "Z", "B", "Z", "C", "Z", "D" }, a));
+ }
+
+ public void testReplaceAllCharArray() {
+ char[] a = new char[] { 'A', 'B', 'A', 'C', 'A', 'D' };
+ a = CollectionTools.replaceAll(a, 'A', 'Z');
+ assertTrue(Arrays.equals(new char[] { 'Z', 'B', 'Z', 'C', 'Z', 'D' }, a));
+ }
+
+ public void testReplaceAllIntArray() {
+ int[] a = new int[] { 0, 1, 0, 7, 0, 99 };
+ a = CollectionTools.replaceAll(a, 0, 13);
+ assertTrue(Arrays.equals(new int[] { 13, 1, 13, 7, 13, 99 }, a));
+ }
+
+ public void testRetainAllCollectionIterator1() {
+ Collection<String> c = this.buildStringList1();
+ assertFalse(CollectionTools.retainAll(c, this.buildStringList1().iterator()));
+ assertEquals(this.buildStringList1().size(), c.size());
+ assertEquals(this.buildStringList1(), c);
+
+ assertTrue(CollectionTools.retainAll(c, this.buildStringList2().iterator()));
+ assertEquals(0, c.size());
+ assertFalse(c.contains("one"));
+ assertFalse(c.contains("two"));
+ assertFalse(c.contains("three"));
+ }
+
+ public void testRetainAllCollectionIterator2() {
+ Collection<Object> c1 = new ArrayList<Object>();
+ c1.add("zero");
+ c1.add("one");
+ c1.add("two");
+
+ Collection<String> c2 = new ArrayList<String>();
+ c2.add("zero");
+ c2.add("one");
+ c2.add("two");
+
+ assertFalse(CollectionTools.retainAll(c1, c2.iterator()));
+ assertEquals(c2.size(), c1.size());
+ assertEquals(c2, c1);
+
+ Collection<String> c3 = new ArrayList<String>();
+ c3.add("three");
+ c3.add("four");
+ c3.add("five");
+
+ assertTrue(CollectionTools.retainAll(c1, c3.iterator()));
+ assertEquals(0, c1.size());
+ assertFalse(c1.contains("one"));
+ assertFalse(c1.contains("two"));
+ assertFalse(c1.contains("three"));
+ }
+
+ public void testRetainAllCollectionObjectArray() {
+ Collection<String> c = this.buildStringList1();
+ assertFalse(CollectionTools.retainAll(c, this.buildObjectArray1()));
+ assertEquals(this.buildStringList1().size(), c.size());
+ assertEquals(this.buildStringList1(), c);
+
+ assertTrue(CollectionTools.retainAll(c, this.buildObjectArray2()));
+ assertEquals(0, c.size());
+ assertFalse(c.contains("one"));
+ assertFalse(c.contains("two"));
+ assertFalse(c.contains("three"));
+ }
+
+ public void testRetainAllObjectArrayObjectArray() {
+ String[] a1 = new String[] { "A", "A", "B", "B", "C", "C", "D", "D", "E", "E", "F", "F" };
+ Object[] a2 = new Object[] { "E", "B", new Integer(7) };
+ assertTrue(Arrays.equals(new String[] { "B", "B", "E", "E" }, CollectionTools.retainAll(a1, a2)));
+ }
+
+ public void testRetainAllCharArrayCharArray() {
+ char[] a1 = new char[] { 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F' };
+ char[] a2 = new char[] { 'E', 'B' };
+ assertTrue(Arrays.equals(new char[] { 'B', 'B', 'E', 'E' }, CollectionTools.retainAll(a1, a2)));
+ }
+
+ public void testRetainAllIntArrayIntArray() {
+ int[] a1 = new int[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6 };
+ int[] a2 = new int[] { 5, 2 };
+ assertTrue(Arrays.equals(new int[] { 2, 2, 5, 5 }, CollectionTools.retainAll(a1, a2)));
+ }
+
+ public void testReverseObjectArray1() {
+ Object[] a = this.buildObjectArray1();
+ a = CollectionTools.reverse(a);
+ assertEquals("two", a[0]);
+ assertEquals("one", a[1]);
+ assertEquals("zero", a[2]);
+ }
+
+ public void testReverseObjectArray2() {
+ String[] a = this.buildStringArray1();
+ a = CollectionTools.reverse(a);
+ assertEquals("two", a[0]);
+ assertEquals("one", a[1]);
+ assertEquals("zero", a[2]);
+ }
+
+ public void testReverseCharArray() {
+ char[] a = this.buildCharArray();
+ a = CollectionTools.reverse(a);
+ assertEquals('c', a[0]);
+ assertEquals('b', a[1]);
+ assertEquals('a', a[2]);
+ }
+
+ public void testReverseIntArray() {
+ int[] a = this.buildIntArray();
+ a = CollectionTools.reverse(a);
+ assertEquals(20, a[0]);
+ assertEquals(10, a[1]);
+ assertEquals(0, a[2]);
+ }
+
+ public void testReverseListIterator1() {
+ List<String> actual = CollectionTools.reverseList(this.buildStringList1().iterator());
+ List<String> expected = this.buildStringList1();
+ Collections.reverse(expected);
+ assertEquals(expected, actual);
+ }
+
+ public void testReverseListIterator2() {
+ List<Object> actual = CollectionTools.<Object>reverseList(this.buildStringList1().iterator());
+ List<Object> expected = this.buildObjectList1();
+ Collections.reverse(expected);
+ assertEquals(expected, actual);
+ }
+
+ public void testRotateObjectArray() {
+ String[] a = this.buildStringArray1();
+ a = CollectionTools.rotate(a);
+ assertEquals("two", a[0]);
+ assertEquals("zero", a[1]);
+ assertEquals("one", a[2]);
+ }
+
+ public void testRotateObjectArrayInt() {
+ String[] a = this.buildStringArray1();
+ a = CollectionTools.rotate(a, 2);
+ assertEquals("one", a[0]);
+ assertEquals("two", a[1]);
+ assertEquals("zero", a[2]);
+ }
+
+ public void testRotateCharArray() {
+ char[] a = this.buildCharArray();
+ a = CollectionTools.rotate(a);
+ assertEquals('c', a[0]);
+ assertEquals('a', a[1]);
+ assertEquals('b', a[2]);
+ }
+
+ public void testRotateCharArrayInt() {
+ char[] a = this.buildCharArray();
+ a = CollectionTools.rotate(a, 2);
+ assertEquals('b', a[0]);
+ assertEquals('c', a[1]);
+ assertEquals('a', a[2]);
+ }
+
+ public void testRotateIntArray() {
+ int[] a = this.buildIntArray();
+ a = CollectionTools.rotate(a);
+ assertEquals(20, a[0]);
+ assertEquals(0, a[1]);
+ assertEquals(10, a[2]);
+ }
+
+ public void testRotateIntArrayInt() {
+ int[] a = this.buildIntArray();
+ a = CollectionTools.rotate(a, 2);
+ assertEquals(10, a[0]);
+ assertEquals(20, a[1]);
+ assertEquals(0, a[2]);
+ }
+
+ public void testSetIterator1() {
+ assertEquals(this.buildStringSet1(), CollectionTools.set(this.buildStringSet1().iterator()));
+ }
+
+ public void testSetIterator2() {
+ Set<String> set1 = new HashSet<String>();
+ set1.add("0");
+ set1.add("1");
+ set1.add("2");
+ set1.add("3");
+
+ Set<Object> set2 = CollectionTools.<Object>set(set1.iterator());
+ assertEquals(set1, set2);
+ }
+
+ public void testSetObjectArray() {
+ assertEquals(this.buildStringSet1(), CollectionTools.set(this.buildStringSet1().toArray()));
+ }
+
+ public void testShuffleObjectArray() {
+ String[] array1 = this.buildStringArray1();
+ String[] array2 = CollectionTools.shuffle(this.buildStringArray1());
+ assertEquals(array1.length, array2.length);
+ assertTrue(CollectionTools.containsAll(array1, array2));
+ }
+
+ public void testShuffleCharArray() {
+ char[] array1 = this.buildCharArray();
+ char[] array2 = CollectionTools.shuffle(this.buildCharArray());
+ assertEquals(array1.length, array2.length);
+ assertTrue(CollectionTools.containsAll(array1, array2));
+ }
+
+ public void testShuffleIntArray() {
+ int[] array1 = this.buildIntArray();
+ int[] array2 = CollectionTools.shuffle(this.buildIntArray());
+ assertEquals(array1.length, array2.length);
+ assertTrue(CollectionTools.containsAll(array1, array2));
+ }
+
+ public void testSingletonIterator1() {
+ Iterator<String> stream = CollectionTools.singletonIterator("foo");
+ assertTrue(stream.hasNext());
+ assertEquals("foo", stream.next());
+ }
+
+ public void testSingletonIterator2() {
+ Iterator<Object> stream = CollectionTools.<Object>singletonIterator("foo");
+ assertTrue(stream.hasNext());
+ assertEquals("foo", stream.next());
+ }
+
+ public void testSingletonIterator3() {
+ Iterator<Object> stream = CollectionTools.singletonIterator((Object) "foo");
+ assertTrue(stream.hasNext());
+ assertEquals("foo", stream.next());
+ }
+
+ public void testSizeIterator() {
+ assertEquals(3, CollectionTools.size(this.buildObjectList1().iterator()));
+ }
+
+ public void testSortedSetIterator1() {
+ assertEquals(this.buildSortedStringSet1(), CollectionTools.set(this.buildSortedStringSet1().iterator()));
+ }
+
+ public void testSortedSetIterator2() {
+ SortedSet<String> ss1 = new TreeSet<String>();
+ ss1.add("0");
+ ss1.add("2");
+ ss1.add("3");
+ ss1.add("1");
+
+ SortedSet<String> set2 = CollectionTools.<String>sortedSet(ss1.iterator());
+ assertEquals(ss1, set2);
+ }
+
+ public void testSortedSetObjectArray() {
+ assertEquals(this.buildSortedStringSet1(), CollectionTools.set(this.buildSortedStringSet1().toArray()));
+ }
+
+ public void testSwapObjectArray() {
+ String[] a = this.buildStringArray1();
+ a = CollectionTools.swap(a, 1, 2);
+ assertEquals("zero", a[0]);
+ assertEquals("two", a[1]);
+ assertEquals("one", a[2]);
+ }
+
+ public void testSwapCharArray() {
+ char[] a = this.buildCharArray();
+ a = CollectionTools.swap(a, 1, 2);
+ assertEquals('a', a[0]);
+ assertEquals('c', a[1]);
+ assertEquals('b', a[2]);
+ }
+
+ public void testSwapIntArray() {
+ int[] a = this.buildIntArray();
+ a = CollectionTools.swap(a, 1, 2);
+ assertEquals(0, a[0]);
+ assertEquals(20, a[1]);
+ assertEquals(10, a[2]);
+ }
+
+ public void testRemoveDuplicateElementsList() {
+ List<String> list = this.buildStringVector1();
+ list.add("zero");
+ list.add("zero");
+ list.add("two");
+ list.add("zero");
+ list = CollectionTools.removeDuplicateElements(list);
+ int i = 0;
+ assertEquals("zero", list.get(i++));
+ assertEquals("one", list.get(i++));
+ assertEquals("two", list.get(i++));
+ assertEquals(i, list.size());
+ }
+
+ public void testRemoveDuplicateElementsObjectArray1() {
+ List<String> list = this.buildStringVector1();
+ list.add("zero");
+ list.add("zero");
+ list.add("two");
+ list.add("zero");
+ String[] array = CollectionTools.removeDuplicateElements(list.toArray(new String[list.size()]));
+ int i = 0;
+ assertEquals("zero", array[i++]);
+ assertEquals("one", array[i++]);
+ assertEquals("two", array[i++]);
+ assertEquals(i, array.length);
+ }
+
+ public void testRemoveDuplicateElementsObjectArray2() {
+ List<String> list = this.buildStringVector1();
+ list.add("zero");
+ list.add("zero");
+ list.add("two");
+ list.add("zero");
+ String[] array = CollectionTools.removeDuplicateElements(list.toArray(new String[list.size()]));
+ int i = 0;
+ assertEquals("zero", array[i++]);
+ assertEquals("one", array[i++]);
+ assertEquals("two", array[i++]);
+ assertEquals(i, array.length);
+ }
+
+ public void testVectorIterator1() {
+ Vector<String> v = CollectionTools.vector(this.buildStringList1().iterator());
+ assertEquals(3, v.size());
+ assertTrue(v.containsAll(this.buildStringList1()));
+ }
+
+ public void testVectorIterator2() {
+ Vector<Object> v = CollectionTools.<Object>vector(this.buildStringList1().iterator());
+ assertEquals(3, v.size());
+ assertTrue(v.containsAll(this.buildStringList1()));
+ }
+
+ public void testVectorObjectArray() {
+ Vector<String> v = CollectionTools.vector(this.buildStringArray1());
+ assertEquals(3, v.size());
+ assertTrue(v.containsAll(this.buildStringList1()));
+ }
+
+ private Object[] buildObjectArray1() {
+ return new Object[] { "zero", "one", "two" };
+ }
+
+ private String[] buildStringArray1() {
+ return new String[] { "zero", "one", "two" };
+ }
+
+ private char[] buildCharArray() {
+ return new char[] { 'a', 'b', 'c' };
+ }
+
+ private int[] buildIntArray() {
+ return new int[] { 0, 10, 20 };
+ }
+
+ private Object[] buildObjectArray2() {
+ return new Object[] { "three", "four", "five" };
+ }
+
+ private String[] buildStringArray2() {
+ return new String[] { "three", "four", "five" };
+ }
+
+ private Vector<String> buildStringVector1() {
+ Vector<String> v = new Vector<String>();
+ this.addToCollection1(v);
+ return v;
+ }
+
+ private List<String> buildStringList1() {
+ List<String> l = new ArrayList<String>();
+ this.addToCollection1(l);
+ return l;
+ }
+
+ private List<Object> buildObjectList1() {
+ List<Object> l = new ArrayList<Object>();
+ this.addToCollection1(l);
+ return l;
+ }
+
+ private void addToCollection1(Collection<? super String> c) {
+ c.add("zero");
+ c.add("one");
+ c.add("two");
+ }
+
+ private List<String> buildStringList2() {
+ List<String> l = new ArrayList<String>();
+ this.addToCollection2(l);
+ return l;
+ }
+
+ private void addToCollection2(Collection<? super String> c) {
+ c.add("three");
+ c.add("four");
+ c.add("five");
+ }
+
+ private Set<String> buildStringSet1() {
+ Set<String> s = new HashSet<String>();
+ this.addToCollection1(s);
+ return s;
+ }
+
+ private Set<Object> buildObjectSet1() {
+ Set<Object> s = new HashSet<Object>();
+ this.addToCollection1(s);
+ return s;
+ }
+
+ private SortedSet<String> buildSortedStringSet1() {
+ SortedSet<String> s = new TreeSet<String>();
+ this.addToCollection1(s);
+ return s;
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/FileToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/FileToolsTests.java
new file mode 100644
index 0000000000..adcccf0992
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/FileToolsTests.java
@@ -0,0 +1,594 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.ClassTools;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.FileTools;
+
+public class FileToolsTests extends TestCase {
+ private File tempDir;
+
+ public FileToolsTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.tempDir = this.buildTempDir();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ this.deleteDir(this.tempDir);
+ }
+
+ public void testFilesIn() {
+ Collection<File> files = CollectionTools.collection(FileTools.filesIn(this.tempDir.getPath()));
+ assertEquals("invalid file count", 3, files.size());
+ }
+
+ public void testDirectoriesIn() {
+ Collection<File> files = CollectionTools.collection(FileTools.directoriesIn(this.tempDir.getPath()));
+ assertEquals("invalid directory count", 2, files.size());
+ }
+
+ public void testFilesInTree() {
+ Collection<File> files = CollectionTools.collection(FileTools.filesInTree(this.tempDir.getPath()));
+ assertEquals("invalid file count", 9, files.size());
+ }
+
+ public void testDirectoriesInTree() {
+ Collection<File> files = CollectionTools.collection(FileTools.directoriesInTree(this.tempDir.getPath()));
+ assertEquals("invalid directory count", 3, files.size());
+ }
+
+ public void testDeleteDirectory() throws IOException {
+ // build another temporary directory just for this test
+ File dir = this.buildTempDir();
+ assertTrue("temporary directory not created", dir.exists());
+ FileTools.deleteDirectory(dir.getPath());
+ assertFalse("temporary directory not deleted", dir.exists());
+ }
+
+ public void testDeleteDirectoryContents() throws IOException {
+ // build another temporary directory just for this test
+ File dir = this.buildTempDir();
+ assertTrue("temporary directory not created", dir.exists());
+ FileTools.deleteDirectoryContents(dir.getPath());
+ assertTrue("temporary directory should not have been deleted", dir.exists());
+ assertTrue("temporary directory contents not deleted", dir.listFiles().length == 0);
+ dir.delete();
+ }
+
+ public void testCopyToFile() throws IOException {
+ File destFile = new File(this.tempDir, "destfile.txt");
+ this.copyToFile(destFile, "testCopyToFile");
+ }
+
+ public void testCopyToPreExistingFile() throws IOException {
+ File destFile = new File(this.tempDir, "destfile.txt");
+ Writer writer = new OutputStreamWriter(new FileOutputStream(destFile));
+ writer.write("this text should be replaced...");
+ writer.close();
+ this.copyToFile(destFile, "testCopyToPreExistingFile");
+ }
+
+ private void copyToFile(File destFile, String writeString) throws IOException {
+ File sourceFile = new File(this.tempDir, "sourcefile.txt");
+ char[] readBuffer = new char[writeString.length()];
+
+ Writer writer = new OutputStreamWriter(new FileOutputStream(sourceFile));
+ writer.write(writeString);
+ writer.close();
+
+ FileTools.copyToFile(sourceFile, destFile);
+
+ Reader reader = new InputStreamReader(new FileInputStream(destFile));
+ reader.read(readBuffer);
+ reader.close();
+ String readString = new String(readBuffer);
+ assertEquals(writeString, readString);
+ }
+
+ public void testCopyToDirectory() throws IOException {
+ File sourceFile = new File(this.tempDir, "sourcefile.txt");
+ String writeString = "testCopyToDirectory";
+
+ File destDir = new File(this.tempDir, "destdir");
+ destDir.mkdir();
+ File destFile = new File(destDir, "sourcefile.txt");
+ char[] readBuffer = new char[writeString.length()];
+
+ Writer writer = new OutputStreamWriter(new FileOutputStream(sourceFile));
+ writer.write(writeString);
+ writer.close();
+
+ FileTools.copyToDirectory(sourceFile, destDir);
+
+ Reader reader = new InputStreamReader(new FileInputStream(destFile));
+ reader.read(readBuffer);
+ reader.close();
+ String readString = new String(readBuffer);
+ assertEquals(writeString, readString);
+ }
+
+ public void testFilter() throws IOException {
+ String prefix = "XXXtestFileXXX";
+ File testFile1 = new File(this.tempDir, prefix + "1");
+ testFile1.createNewFile();
+ File testFile2 = new File(this.tempDir, prefix + "2");
+ testFile2.createNewFile();
+
+ FileFilter filter = this.buildFileFilter(prefix);
+ Iterator<File> filteredFilesIterator = FileTools.filter(FileTools.filesIn(this.tempDir), filter);
+ Collection<File> filteredFiles = CollectionTools.collection(filteredFilesIterator);
+ assertEquals(2, filteredFiles.size());
+ assertTrue(filteredFiles.contains(testFile1));
+ assertTrue(filteredFiles.contains(testFile2));
+ }
+
+ private FileFilter buildFileFilter(final String prefix) {
+ return new FileFilter() {
+ public boolean accept(File file) {
+ return file.getName().startsWith(prefix);
+ }
+ };
+ }
+
+ public void testStripExtension() {
+ assertEquals("foo", FileTools.stripExtension("foo.xml"));
+ assertEquals("foo.bar", FileTools.stripExtension("foo.bar.xml"));
+ assertEquals("foo", FileTools.stripExtension("foo"));
+ assertEquals("foo", FileTools.stripExtension("foo."));
+ }
+
+ public void testExtension() {
+ assertEquals(".xml", FileTools.extension("foo.xml"));
+ assertEquals(".xml", FileTools.extension("foo.bar.xml"));
+ assertEquals("", FileTools.extension("foo"));
+ assertEquals("", FileTools.extension("foo,xml"));
+ assertEquals(".", FileTools.extension("foo."));
+ }
+
+ public void testEmptyTemporaryDirectory() throws IOException {
+ File tempDir1 = FileTools.temporaryDirectory();
+ File testFile1 = new File(tempDir1, "junk");
+ testFile1.createNewFile();
+
+ File tempDir2 = FileTools.emptyTemporaryDirectory();
+ assertEquals(tempDir1, tempDir2);
+ assertTrue(tempDir2.isDirectory());
+ assertEquals(0, tempDir2.listFiles().length);
+ tempDir2.delete();
+ }
+
+ public void testCanonicalFileName() {
+ File file1 = new File("foo");
+ file1 = new File(file1, "bar");
+ file1 = new File(file1, "baz");
+ file1 = new File(file1, "..");
+ file1 = new File(file1, "..");
+ file1 = new File(file1, "bar");
+ file1 = new File(file1, "baz");
+ File file2 = new File(System.getProperty("user.dir"));
+ file2 = new File(file2, "foo");
+ file2 = new File(file2, "bar");
+ file2 = new File(file2, "baz");
+ File file3 = FileTools.canonicalFile(file1);
+ assertEquals(file2, file3);
+ }
+
+ private boolean isExecutingOnWindows() {
+ return this.isExecutingOn("Windows");
+ }
+
+ // private boolean isExecutingOnLinux() {
+ // return this.isExecutingOn("Linux");
+ // }
+ //
+ private boolean isExecutingOn(String osName) {
+ return System.getProperty("os.name").indexOf(osName) != -1;
+ }
+
+ public void testPathFiles() {
+ File[] expected;
+ File[] actual;
+
+ if (this.isExecutingOnWindows()) {
+ expected = new File[] { new File("C:/"), new File("C:/foo"), new File("C:/foo/bar"), new File("C:/foo/bar/baz.txt") };
+ actual = this.pathFiles(new File("C:/foo/bar/baz.txt"));
+ assertTrue(Arrays.equals(expected, actual));
+ }
+
+ expected = new File[] { new File("/"), new File("/foo"), new File("/foo/bar"), new File("/foo/bar/baz.txt") };
+ actual = this.pathFiles(new File("/foo/bar/baz.txt"));
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = new File[] { new File("foo"), new File("foo/bar"), new File("foo/bar/baz.txt") };
+ actual = this.pathFiles(new File("foo/bar/baz.txt"));
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = new File[] { new File(".."), new File("../foo"), new File("../foo/bar"), new File("../foo/bar/baz.txt") };
+ actual = this.pathFiles(new File("../foo/bar/baz.txt"));
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = new File[] { new File("."), new File("./foo"), new File("./foo/bar"), new File("./foo/bar/baz.txt") };
+ actual = this.pathFiles(new File("./foo/bar/baz.txt"));
+ assertTrue(Arrays.equals(expected, actual));
+ }
+
+ private File[] pathFiles(File file) {
+ return (File[]) ClassTools.executeStaticMethod(FileTools.class, "pathFiles", File.class, file);
+ }
+
+ public void testRelativeParentFile() {
+ assertEquals(new File(".."), this.relativeParentFile(1));
+ assertEquals(new File("../.."), this.relativeParentFile(2));
+ assertEquals(new File("../../.."), this.relativeParentFile(3));
+
+ boolean exCaught = false;
+ try {
+ File file = this.relativeParentFile(0);
+ fail("invalid return: " + file);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof InvocationTargetException) {
+ InvocationTargetException ite = (InvocationTargetException) ex.getCause();
+ if (ite.getTargetException() instanceof IllegalArgumentException) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ private File relativeParentFile(int len) {
+ return (File) ClassTools.executeStaticMethod(FileTools.class, "relativeParentFile", int.class, new Integer(len));
+ }
+
+ public void testConvertToRelativeFile() {
+ String prefix = this.isExecutingOnWindows() ? "C:" : "";
+ File file;
+ File dir;
+ File relativeFile;
+
+ if (this.isExecutingOnWindows()) {
+ // on Windows, a drive must be specified for a file to be absolute (i.e. not relative)
+ this.verifyUnchangedRelativeFile("/dir1/dir2/file.txt", "C:/dir1/dir2");
+ // different drives
+ this.verifyUnchangedRelativeFile("D:/dir1/dir2/file.txt", "C:/dir1/dir2");
+ }
+ this.verifyUnchangedRelativeFile("dir1/dir2/file.txt", prefix + "/dir1/dir2");
+ this.verifyUnchangedRelativeFile("./dir1/dir2/file.txt", prefix + "/dir1/dir2");
+ this.verifyUnchangedRelativeFile("../../dir1/dir2/file.txt", prefix + "/dir1/dir2");
+
+ file = new File(prefix + "/dir1/dir2");
+ dir = new File(prefix + "/dir1/dir2");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("."), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/../dir2/file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/dir3/dir4/dir5/file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("dir3/dir4/dir5/file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("../../../file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("../file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2/dirA/dirB/dirC/file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("../../../dirA/dirB/dirC/file.txt"), relativeFile);
+
+ file = new File(prefix + "/dir1/dir2");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("../../.."), relativeFile);
+
+ file = new File(prefix + "/My Documents/My Workspace/Project 1/lib/toplink.jar");
+ dir = new File(prefix + "/My Documents/My Workspace/Project 1");
+ relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(new File("lib/toplink.jar"), relativeFile);
+ }
+
+ private void verifyUnchangedRelativeFile(String fileName, String dirName) {
+ File file = new File(fileName);
+ File dir = new File(dirName);
+ File relativeFile = FileTools.convertToRelativeFile(file, dir);
+ assertEquals(file, relativeFile);
+ }
+
+ public void testConvertToAbsoluteFile() {
+ String prefix = this.isExecutingOnWindows() ? "C:" : "";
+ File file;
+ File dir;
+ File absoluteFile;
+
+ if (this.isExecutingOnWindows()) {
+ // on Windows, a drive must be specified for a file to be absolute (i.e. not relative)
+ this.verifyUnchangedAbsoluteFile("C:/dir1/dir2/file.txt", "C:/dir1/dir2");
+ // different drives
+ this.verifyUnchangedAbsoluteFile("D:/dir1/dir2/file.txt", "C:/dir1/dir2");
+ }
+ this.verifyUnchangedAbsoluteFile(prefix + "/dir1/dir2/file.txt", prefix + "/dir1/dir2");
+ this.verifyUnchangedAbsoluteFile(prefix + "/./dir1/dir2/file.txt", prefix + "/dir1/dir2");
+ this.verifyUnchangedAbsoluteFile(prefix + "/dir1/dir2/../../dir1/dir2/file.txt", prefix + "/dir1/dir2");
+
+ file = new File(".");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2"), absoluteFile);
+
+ file = new File("./file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/file.txt"), absoluteFile);
+
+ file = new File("file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/file.txt"), absoluteFile);
+
+ file = new File("../dir2/file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/file.txt"), absoluteFile);
+
+ file = new File("dir3/dir4/dir5/file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/dir3/dir4/dir5/file.txt"), absoluteFile);
+
+ file = new File("../../../file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/file.txt"), absoluteFile);
+
+ // too many ".." directories will resolve to the root;
+ // this is consistent with Windows and Linux command shells
+ file = new File("../../../../../../../../file.txt");
+ dir = new File(prefix + "/dir1/dir2");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/file.txt"), absoluteFile);
+
+ file = new File("../file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/file.txt"), absoluteFile);
+
+ file = new File("../../../dirA/dirB/dirC/file.txt");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2/dirA/dirB/dirC/file.txt"), absoluteFile);
+
+ file = new File("../../..");
+ dir = new File(prefix + "/dir1/dir2/dir3/dir4/dir5");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/dir1/dir2"), absoluteFile);
+
+ file = new File("lib/toplink.jar");
+ dir = new File(prefix + "/My Documents/My Workspace/Project 1");
+ absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(new File(prefix + "/My Documents/My Workspace/Project 1/lib/toplink.jar"), absoluteFile);
+ }
+
+ public void testFileNameIsReserved() {
+ boolean expected = this.isExecutingOnWindows();
+ assertEquals(expected, FileTools.fileNameIsReserved("CON"));
+ assertEquals(expected, FileTools.fileNameIsReserved("con"));
+ assertEquals(expected, FileTools.fileNameIsReserved("cON"));
+ assertEquals(expected, FileTools.fileNameIsReserved("AUX"));
+ assertEquals(expected, FileTools.fileNameIsReserved("COM3"));
+ assertEquals(expected, FileTools.fileNameIsReserved("LPT3"));
+ assertEquals(expected, FileTools.fileNameIsReserved("nUL"));
+ assertEquals(expected, FileTools.fileNameIsReserved("Prn"));
+ }
+
+ public void testFileHasAnyReservedComponents() {
+ boolean expected = this.isExecutingOnWindows();
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("C:/CON")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("/con/foo")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("c:/temp/cON")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("bar//baz//AUX")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("COM3//ttt")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("d:/LPT3/xxx")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("c:/my docs and stuff/tuesday/nUL")));
+ assertEquals(expected, FileTools.fileHasAnyReservedComponents(new File("Prn")));
+ }
+
+ public void testShortenFileNameFile() {
+ if (this.isExecutingOnWindows()) {
+ this.verifyShortenFileNameFileWin();
+ } else {
+ this.verifyShortenFileNameFileNonWin();
+ }
+ }
+
+ private void verifyShortenFileNameFileWin() {
+ File file = new File("C:\\Documents and Settings\\Administrator\\Desktop\\Project\\Text.txt");
+ String fileName = FileTools.shortenFileName(file);
+ assertEquals("C:\\Documents and Settings\\...\\Desktop\\Project\\Text.txt", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+
+ file = new File("C:/");
+ fileName = FileTools.shortenFileName(file);
+ assertEquals("C:\\", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+ }
+
+ private void verifyShortenFileNameFileNonWin() {
+ File file = new File("/home/administrator/documents and settings/desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(file);
+ assertEquals("/home/administrator/.../desktop/Project/Text.txt", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+
+ file = new File("/home");
+ fileName = FileTools.shortenFileName(file);
+ assertEquals("/home", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+ }
+
+ public void testShortenFileNameFileInt() {
+ if (this.isExecutingOnWindows()) {
+ this.verifyShortenFileNameFileIntWin();
+ } else {
+ this.verifyShortenFileNameFileIntNonWin();
+ }
+ }
+
+ private void verifyShortenFileNameFileIntWin() {
+ File file = new File("C:\\Documents and Settings\\Administrator\\Desktop\\Project\\Text.txt");
+ String fileName = FileTools.shortenFileName(file, 31);
+ assertEquals("C:\\...\\Desktop\\Project\\Text.txt", fileName);
+ assertEquals(31, fileName.length());
+
+ file = new File("C:/This is the file name.txt");
+ fileName = FileTools.shortenFileName(file, 10);
+ assertEquals("C:\\This is the file name.txt", fileName);
+ assertEquals(28, fileName.length());
+ }
+
+ private void verifyShortenFileNameFileIntNonWin() {
+ File file = new File("/home/administrator/documents and settings/desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(file, 31);
+ assertEquals("/home/.../desktop/Project/Text.txt", fileName);
+ assertEquals(34, fileName.length());
+
+ file = new File("/This is the file name.txt");
+ fileName = FileTools.shortenFileName(file, 10);
+ assertEquals("/This is the file name.txt", fileName);
+ assertEquals(26, fileName.length());
+ }
+
+ public void testShortenFileNameURL() throws Exception {
+ if (this.isExecutingOnWindows()) {
+ this.verifyShortenFileNameURLWin();
+ } else {
+ this.verifyShortenFileNameURLNonWin();
+ }
+ }
+
+ private void verifyShortenFileNameURLWin() throws Exception {
+ URL url = new URL("file", "", "C:/Documents and Settings/Administrator/Desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(url);
+ assertEquals("C:\\Documents and Settings\\...\\Desktop\\Project\\Text.txt", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+ }
+
+ private void verifyShortenFileNameURLNonWin() throws Exception {
+ URL url = new URL("file", "", "/home/administrator/documents and settings/desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(url);
+ assertEquals("/home/administrator/.../desktop/Project/Text.txt", fileName);
+ assertTrue(fileName.length() <= FileTools.MAXIMUM_SHORTENED_FILE_NAME_LENGTH);
+ }
+
+ public void testShortenFileNameURLInt() throws Exception {
+ if (this.isExecutingOnWindows()) {
+ this.verifyShortenFileNameURLIntWin();
+ } else {
+ this.verifyShortenFileNameURLIntNonWin();
+ }
+ }
+
+ private void verifyShortenFileNameURLIntWin() throws Exception {
+ URL url = new URL("file", "", "/C:/Documents and Settings/Administrator/Desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(url, 31);
+ assertEquals("C:\\...\\Desktop\\Project\\Text.txt", fileName);
+ assertEquals(31, fileName.length());
+ }
+
+ private void verifyShortenFileNameURLIntNonWin() throws Exception {
+ URL url = new URL("file", "", "/home/administrator/documents and settings/desktop/Project/Text.txt");
+ String fileName = FileTools.shortenFileName(url, 31);
+ assertEquals("/home/.../desktop/Project/Text.txt", fileName);
+ assertEquals(34, fileName.length());
+ }
+
+ private void verifyUnchangedAbsoluteFile(String fileName, String dirName) {
+ File file = new File(fileName);
+ File dir = new File(dirName);
+ File absoluteFile = FileTools.convertToAbsoluteFile(file, dir);
+ assertEquals(file, absoluteFile);
+ }
+
+ private File buildTempDir() throws IOException {
+ // build a new directory for each test, to prevent any cross-test effects
+ File dir = FileTools.newTemporaryDirectory(ClassTools.shortClassNameForObject(this) + "." + this.getName());
+
+ File file0a = new File(dir, "file0a");
+ file0a.createNewFile();
+ File file0b = new File(dir, "file0b");
+ file0b.createNewFile();
+ File file0c = new File(dir, "file0c");
+ file0c.createNewFile();
+
+ File subdir1 = new File(dir, "subdir1");
+ subdir1.mkdir();
+ File file1a = new File(subdir1, "file1a");
+ file1a.createNewFile();
+ File file1b = new File(subdir1, "file1b");
+ file1b.createNewFile();
+
+ File subdir2 = new File(dir, "subdir2");
+ subdir2.mkdir();
+ File file2a = new File(subdir2, "file2a");
+ file2a.createNewFile();
+ File file2b = new File(subdir2, "file2b");
+ file2b.createNewFile();
+
+ File subdir3 = new File(subdir2, "subdir3");
+ subdir3.mkdir();
+ File file3a = new File(subdir3, "file3a");
+ file3a.createNewFile();
+ File file3b = new File(subdir3, "file3b");
+ file3b.createNewFile();
+
+ return dir;
+ }
+
+ private void deleteDir(File dir) {
+ FileTools.deleteDirectory(dir);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/HashBagTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/HashBagTests.java
new file mode 100644
index 0000000000..51120c9eb2
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/HashBagTests.java
@@ -0,0 +1,448 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.ConcurrentModificationException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.Bag;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.HashBag;
+
+public class HashBagTests extends TestCase {
+ private HashBag<String> bag;
+
+ public HashBagTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.bag = this.buildBag();
+ }
+
+ private HashBag<String> buildBag() {
+ HashBag<String> b = new HashBag<String>();
+ b.add(null);
+ b.add(new String("one"));
+ b.add(new String("two"));
+ b.add(new String("two"));
+ b.add(new String("three"));
+ b.add(new String("three"));
+ b.add(new String("three"));
+ b.add(new String("four"));
+ b.add(new String("four"));
+ b.add(new String("four"));
+ b.add(new String("four"));
+ return b;
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+ private Collection<String> buildCollection() {
+ Collection<String> c = new ArrayList<String>();
+ c.add(new String("foo"));
+ c.add(new String("foo"));
+ c.add(new String("bar"));
+ c.add(new String("bar"));
+ c.add(new String("bar"));
+ return c;
+ }
+
+ public void testCtorCollection() {
+ Collection<String> c = this.buildCollection();
+ Bag<String> b = new HashBag<String>(c);
+ for (String s : c) {
+ assertTrue("missing element", b.contains(s));
+ }
+ }
+
+ public void testCtorIntFloat() {
+ boolean exCaught;
+
+ exCaught = false;
+ try {
+ this.bag = new HashBag<String>(-20, 0.66f);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalArgumentException not thrown", exCaught);
+
+ exCaught = false;
+ try {
+ this.bag = new HashBag<String>(20, -0.66f);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalArgumentException not thrown", exCaught);
+ }
+
+ public void testAdd() {
+ // the other adds took place in setUp
+ assertTrue("incorrect return value", this.bag.add("five"));
+
+ assertTrue("missing element", this.bag.contains("one"));
+ assertTrue("missing element", this.bag.contains("two"));
+ assertTrue("missing element", this.bag.contains("three"));
+ assertTrue("missing element", this.bag.contains("four"));
+ assertTrue("missing element", this.bag.contains("five"));
+ }
+
+ public void testAddAll() {
+ Collection<String> c = this.buildCollection();
+ assertTrue("incorrect return value", this.bag.addAll(c));
+ for (String s : c) {
+ assertTrue("missing element", this.bag.contains(s));
+ }
+ }
+
+ public void testClear() {
+ assertTrue("missing element", this.bag.contains("one"));
+ assertTrue("missing element", this.bag.contains("two"));
+ assertTrue("missing element", this.bag.contains("three"));
+ assertTrue("missing element", this.bag.contains("four"));
+ assertTrue("missing element", this.bag.contains(null));
+ assertEquals("invalid size", 11, this.bag.size());
+ this.bag.clear();
+ assertFalse("malingering element", this.bag.contains("one"));
+ assertFalse("malingering element", this.bag.contains("two"));
+ assertFalse("malingering element", this.bag.contains("three"));
+ assertFalse("malingering element", this.bag.contains("four"));
+ assertFalse("malingering element", this.bag.contains(null));
+ assertEquals("invalid size", 0, this.bag.size());
+ }
+
+ public void testClone() {
+ Bag<String> bag2 = this.bag.clone();
+ assertTrue("bad clone", this.bag != bag2);
+ assertEquals("bad clone", this.bag, bag2);
+ assertTrue("bad clone", this.bag.hashCode() == bag2.hashCode());
+ }
+
+ public void testContains() {
+ assertTrue("missing element", this.bag.contains(null));
+ assertTrue("missing element", this.bag.contains("one"));
+ assertTrue("missing element", this.bag.contains("two"));
+ assertTrue("missing element", this.bag.contains("three"));
+ assertTrue("missing element", this.bag.contains("four"));
+ assertTrue("missing element", this.bag.contains(new String("four")));
+ assertTrue("missing element", this.bag.contains("fo" + "ur"));
+ assertFalse("element found", this.bag.contains("five"));
+ }
+
+ public void testContainsAll() {
+ Collection<String> c = new ArrayList<String>();
+ c.add(null);
+ c.add(new String("one"));
+ c.add(new String("two"));
+ c.add(new String("three"));
+ c.add(new String("four"));
+ assertTrue("missing element(s)", this.bag.containsAll(c));
+ }
+
+ public void testCount() {
+ assertEquals("bad count", 0, this.bag.count("zero"));
+ assertEquals("bad count", 1, this.bag.count("one"));
+ assertEquals("bad count", 2, this.bag.count("two"));
+ assertEquals("bad count", 3, this.bag.count("three"));
+ assertEquals("bad count", 4, this.bag.count("four"));
+ assertEquals("bad count", 0, this.bag.count("five"));
+ }
+
+ public void testEquals() {
+ Bag<String> bag2 = this.buildBag();
+ assertEquals("bags are not equal", this.bag, bag2);
+ bag2.add("five");
+ assertFalse("bags are equal", this.bag.equals(bag2));
+ Collection<String> c = new ArrayList<String>(this.bag);
+ assertFalse("bags are not equal to collections", this.bag.equals(c));
+ }
+
+ public void testHashCode() {
+ Bag<String> bag2 = this.buildBag();
+ assertEquals("bad hash code", this.bag.hashCode(), bag2.hashCode());
+ }
+
+ public void testIsEmpty() {
+ assertFalse("bag is empty", this.bag.isEmpty());
+ this.bag.clear();
+ assertTrue("bag is not empty", this.bag.isEmpty());
+ this.bag.add("foo");
+ assertFalse("bag is empty", this.bag.isEmpty());
+ }
+
+ public void testEmptyIterator() {
+ this.bag.clear();
+ Iterator<String> iterator = this.bag.iterator();
+ assertFalse("iterator is not empty", iterator.hasNext());
+
+ boolean exCaught = false;
+ Object element = null;
+ try {
+ element = iterator.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + element, exCaught);
+
+ exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+
+ public void testIterator() {
+ int i = 0;
+ Iterator<String> iterator = this.bag.iterator();
+ assertTrue("iterator is empty", iterator.hasNext());
+ while (iterator.hasNext()) {
+ iterator.next();
+ i++;
+ }
+ assertEquals("invalid hasNext() loop", 11, i);
+ assertFalse("iterator should be empty now", iterator.hasNext());
+
+ boolean exCaught = false;
+ Object element = null;
+ try {
+ element = iterator.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + element, exCaught);
+
+ iterator.remove();
+ assertEquals("iterator did not remove element", 10, this.bag.size());
+
+ exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+
+ // start over
+ iterator = this.bag.iterator();
+ this.bag.add("five");
+ exCaught = false;
+ try {
+ iterator.next();
+ } catch (ConcurrentModificationException ex) {
+ exCaught = true;
+ }
+ assertTrue("ConcurrentModificationException not thrown", exCaught);
+ }
+
+ public void testUniqueIterator() {
+ int i = 0;
+ Iterator<String> iterator = this.bag.uniqueIterator();
+ assertTrue("iterator is empty", iterator.hasNext());
+ while (iterator.hasNext()) {
+ iterator.next();
+ i++;
+ }
+ assertEquals("invalid hasNext() loop", 5, i);
+ assertFalse("iterator should be empty now", iterator.hasNext());
+
+ boolean exCaught = false;
+ Object element = null;
+ try {
+ element = iterator.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + element, exCaught);
+
+ // start over
+ iterator = this.bag.uniqueIterator();
+ Object next = null;
+ while (iterator.hasNext() && !"four".equals(next)) {
+ next = iterator.next();
+ }
+ iterator.remove();
+ assertEquals("iterator did not remove all copies of element", 7, this.bag.size());
+
+ exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+
+ // start over
+ iterator = this.bag.uniqueIterator();
+ this.bag.add("five");
+ exCaught = false;
+ try {
+ iterator.next();
+ } catch (ConcurrentModificationException ex) {
+ exCaught = true;
+ }
+ assertTrue("ConcurrentModificationException not thrown", exCaught);
+ }
+
+ public void testHashingDistribution() throws Exception {
+ Bag<String> bigBag = new HashBag<String>();
+ for (int i = 0; i < 10000; i++) {
+ bigBag.add("object" + i);
+ }
+
+ java.lang.reflect.Field field = bigBag.getClass().getDeclaredField("table");
+ field.setAccessible(true);
+ Object[] table = (Object[]) field.get(bigBag);
+ int bucketCount = table.length;
+ int filledBucketCount = 0;
+ for (Object o : table) {
+ if (o != null) {
+ filledBucketCount++;
+ }
+ }
+ float loadFactor = ((float) filledBucketCount) / ((float) bucketCount);
+ assertTrue("WARNING - poor load factor: " + loadFactor, loadFactor > 0.20);
+ assertTrue("WARNING - poor load factor: " + loadFactor, loadFactor < 0.75);
+ }
+
+ public void testRemove() {
+ assertTrue("incorrect return value", this.bag.remove("one"));
+ assertFalse("element still present", this.bag.contains("one"));
+ assertFalse("incorrect return value", this.bag.remove("one"));
+
+ assertTrue("incorrect return value", this.bag.remove("two"));
+ assertTrue("incorrect return value", this.bag.remove("two"));
+ assertFalse("element still present", this.bag.contains("one"));
+ assertFalse("incorrect return value", this.bag.remove("one"));
+ }
+
+ public void testRemoveAll() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("one");
+ c.add("three");
+ assertTrue("incorrect return value", this.bag.removeAll(c));
+ assertFalse("element still present", this.bag.contains("one"));
+ assertFalse("element still present", this.bag.contains("three"));
+ assertFalse("incorrect return value", this.bag.remove("one"));
+ assertFalse("incorrect return value", this.bag.remove("three"));
+ assertFalse("incorrect return value", this.bag.removeAll(c));
+ }
+
+ public void testRetainAll() {
+ Collection<String> c = new ArrayList<String>();
+ c.add("one");
+ c.add("three");
+ assertTrue("incorrect return value", this.bag.retainAll(c));
+ assertTrue("element removed", this.bag.contains("one"));
+ assertTrue("element removed", this.bag.contains("three"));
+ assertFalse("element still present", this.bag.contains("two"));
+ assertFalse("element still present", this.bag.contains("four"));
+ assertFalse("incorrect return value", this.bag.remove("two"));
+ assertFalse("incorrect return value", this.bag.remove("four"));
+ assertFalse("incorrect return value", this.bag.retainAll(c));
+ }
+
+ public void testSize() {
+ assertTrue("incorrect size", this.bag.size() == 11);
+ this.bag.add("five");
+ this.bag.add("five");
+ this.bag.add("five");
+ this.bag.add("five");
+ this.bag.add("five");
+ assertEquals("incorrect size", 16, this.bag.size());
+ }
+
+ public void testSerialization() throws Exception {
+ Bag<String> bag2 = TestTools.serialize(this.bag);
+
+ assertTrue("same object?", this.bag != bag2);
+ assertEquals("incorrect size", 11, bag2.size());
+ assertEquals("unequal bag", this.bag, bag2);
+ // look for similar elements
+ assertTrue("missing element", bag2.contains(null));
+ assertTrue("missing element", bag2.contains("one"));
+ assertTrue("missing element", bag2.contains("two"));
+ assertTrue("missing element", bag2.contains("three"));
+ assertTrue("missing element", bag2.contains("four"));
+
+ int nullCount = 0, oneCount = 0, twoCount = 0, threeCount = 0, fourCount = 0;
+ for (String s : bag2) {
+ if (s == null) {
+ nullCount++;
+ } else if (s.equals("one")) {
+ oneCount++;
+ } else if (s.equals("two")) {
+ twoCount++;
+ } else if (s.equals("three")) {
+ threeCount++;
+ } else if (s.equals("four")) {
+ fourCount++;
+ }
+ }
+ assertEquals("bad element count", 1, nullCount);
+ assertEquals("bad element count", 1, oneCount);
+ assertEquals("bad element count", 2, twoCount);
+ assertEquals("bad element count", 3, threeCount);
+ assertEquals("bad element count", 4, fourCount);
+ }
+
+ public void testToArray() {
+ Object[] a = this.bag.toArray();
+ assertEquals("incorrect length", 11, a.length);
+ assertTrue("missing element", CollectionTools.contains(a, null));
+ assertTrue("missing element", CollectionTools.contains(a, "one"));
+ assertTrue("missing element", CollectionTools.contains(a, "two"));
+ assertTrue("missing element", CollectionTools.contains(a, "three"));
+ assertTrue("missing element", CollectionTools.contains(a, "four"));
+ }
+
+ public void testToArrayObjectArray() {
+ String[] a = new String[12];
+ a[11] = "not null";
+ String[] b = this.bag.toArray(a);
+ assertEquals("different array", a, b);
+ assertEquals("incorrect length", 12, a.length);
+ assertTrue("missing element", CollectionTools.contains(a, null));
+ assertTrue("missing element", CollectionTools.contains(a, "one"));
+ assertTrue("missing element", CollectionTools.contains(a, "two"));
+ assertTrue("missing element", CollectionTools.contains(a, "three"));
+ assertTrue("missing element", CollectionTools.contains(a, "four"));
+ assertTrue("missing null element", a[11] == null);
+ }
+
+ public void testToString() {
+ String s = this.bag.toString();
+ assertTrue("invalid string prefix", s.startsWith("["));
+ assertTrue("invalid string suffix", s.endsWith("]"));
+ int commaCount = 0;
+ for (int i = 0; i < s.length(); i++) {
+ if (s.charAt(i) == ',') {
+ commaCount++;
+ }
+ }
+ assertEquals("invalid number of commas", 10, commaCount);
+ assertTrue("missing element toString()", s.indexOf("one") != -1);
+ assertTrue("missing element toString()", s.indexOf("two") != -1);
+ assertTrue("missing element toString()", s.indexOf("three") != -1);
+ assertTrue("missing element toString()", s.indexOf("four") != -1);
+ assertTrue("missing element toString()", s.indexOf("null") != -1);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/IndentingPrintWriterTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/IndentingPrintWriterTests.java
new file mode 100644
index 0000000000..c96213249b
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/IndentingPrintWriterTests.java
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.io.StringWriter;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.IndentingPrintWriter;
+
+public class IndentingPrintWriterTests extends TestCase {
+ StringWriter sw1;
+ StringWriter sw2;
+ IndentingPrintWriter ipw1;
+ IndentingPrintWriter ipw2;
+
+ static final String CR = System.getProperty("line.separator");
+
+ public IndentingPrintWriterTests(String name) {
+ super(name);
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ this.sw1 = new StringWriter();
+ this.ipw1 = new IndentingPrintWriter(this.sw1);
+ this.sw2 = new StringWriter();
+ this.ipw2 = new IndentingPrintWriter(this.sw2, " "); // indent with 4 spaces instead of a tab
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+ public void testIndent() {
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ this.ipw1.indent();
+ assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
+ }
+
+ public void testUndent() {
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ this.ipw1.indent();
+ assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
+ this.ipw1.undent();
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ }
+
+ public void testIncrementIndentLevel() {
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ this.ipw1.incrementIndentLevel();
+ assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
+ }
+
+ public void testDecrementIndentLevel() {
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ this.ipw1.incrementIndentLevel();
+ assertEquals("wrong indent level", 1, this.ipw1.getIndentLevel());
+ this.ipw1.decrementIndentLevel();
+ assertEquals("wrong indent level", 0, this.ipw1.getIndentLevel());
+ }
+
+ public void testPrintTab() {
+ String expected = "foo0" + CR + "\tfoo1" + CR + "\tfoo1" + CR + "\t\tfoo2" + CR + "\tfoo1" + CR + "\tfoo1" + CR + "foo0" + CR;
+
+ this.ipw1.println("foo0");
+ this.ipw1.indent();
+ this.ipw1.println("foo1");
+ this.ipw1.println("foo1");
+ this.ipw1.indent();
+ this.ipw1.println("foo2");
+ this.ipw1.undent();
+ this.ipw1.println("foo1");
+ this.ipw1.println("foo1");
+ this.ipw1.undent();
+ this.ipw1.println("foo0");
+
+ assertEquals("bogus output", expected, this.sw1.toString());
+ }
+
+ public void testPrintSpaces() {
+ String expected = "foo0" + CR + " foo1" + CR + " foo1" + CR + " foo2" + CR + " foo1" + CR + " foo1" + CR + "foo0" + CR;
+
+ this.ipw2.println("foo0");
+ this.ipw2.indent();
+ this.ipw2.println("foo1");
+ this.ipw2.println("foo1");
+ this.ipw2.indent();
+ this.ipw2.println("foo2");
+ this.ipw2.undent();
+ this.ipw2.println("foo1");
+ this.ipw2.println("foo1");
+ this.ipw2.undent();
+ this.ipw2.println("foo0");
+
+ assertEquals("bogus output", expected, this.sw2.toString());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JDBCTypeTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JDBCTypeTests.java
new file mode 100644
index 0000000000..40f152dfa9
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JDBCTypeTests.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.sql.Types;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.JDBCType;
+
+public class JDBCTypeTests extends TestCase {
+
+ public JDBCTypeTests(String name) {
+ super(name);
+ }
+
+ public void testTypesSize() {
+ assertEquals(Types.class.getDeclaredFields().length, JDBCType.types().length);
+ }
+
+ public void testName() {
+ JDBCType jdbcType;
+ jdbcType = JDBCType.type(Types.VARCHAR);
+ assertEquals("VARCHAR", jdbcType.getName());
+
+ jdbcType = JDBCType.type(Types.INTEGER);
+ assertEquals("INTEGER", jdbcType.getName());
+ }
+
+ public void testCode() {
+ JDBCType jdbcType;
+ jdbcType = JDBCType.type(Types.VARCHAR);
+ assertEquals(Types.VARCHAR, jdbcType.getCode());
+
+ jdbcType = JDBCType.type(Types.INTEGER);
+ assertEquals(Types.INTEGER, jdbcType.getCode());
+ }
+
+ public void testInvalidTypeCode() throws Exception {
+ boolean exCaught = false;
+ try {
+ JDBCType jdbcType = JDBCType.type(55);
+ fail("invalid JDBCType: " + jdbcType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidTypeName() throws Exception {
+ boolean exCaught = false;
+ try {
+ JDBCType jdbcType = JDBCType.type("VARCHAR2");
+ fail("invalid JDBCType: " + jdbcType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JavaTypeTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JavaTypeTests.java
new file mode 100644
index 0000000000..1a0c0b826f
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JavaTypeTests.java
@@ -0,0 +1,250 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.JavaType;
+
+public class JavaTypeTests extends TestCase {
+
+ public JavaTypeTests(String name) {
+ super(name);
+ }
+
+ public void testInvalidElementTypeNull() throws Exception {
+ boolean exCaught = false;
+ try {
+ JavaType javaType = new JavaType(null, 0);
+ fail("invalid JavaType: " + javaType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidElementTypeEmpty() throws Exception {
+ boolean exCaught = false;
+ try {
+ JavaType javaType = new JavaType("", 0);
+ fail("invalid JavaType: " + javaType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidElementTypeArray() throws Exception {
+ boolean exCaught = false;
+ try {
+ JavaType javaType = new JavaType(java.lang.Object[].class.getName(), 0);
+ fail("invalid JavaType: " + javaType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidArrayDepthNegative() throws Exception {
+ boolean exCaught = false;
+ try {
+ JavaType javaType = new JavaType(java.lang.Object.class.getName(), -2);
+ fail("invalid JavaType: " + javaType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidVoidArray() throws Exception {
+ boolean exCaught = false;
+ try {
+ JavaType javaType = new JavaType(void.class.getName(), 2);
+ fail("invalid JavaType: " + javaType);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testElementTypeName() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertEquals("java.lang.Object", javaType.getElementTypeName());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertEquals("java.lang.Object", javaType.getElementTypeName());
+
+ javaType = new JavaType(int.class);
+ assertEquals("int", javaType.getElementTypeName());
+
+ javaType = new JavaType(int[].class);
+ assertEquals("int", javaType.getElementTypeName());
+
+ javaType = new JavaType(void.class);
+ assertEquals("void", javaType.getElementTypeName());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertEquals("java.util.Map$Entry", javaType.getElementTypeName());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertEquals("java.util.Map$Entry", javaType.getElementTypeName());
+ }
+
+ public void testArrayDepth() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertEquals(0, javaType.getArrayDepth());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertEquals(1, javaType.getArrayDepth());
+
+ javaType = new JavaType(int.class);
+ assertEquals(0, javaType.getArrayDepth());
+
+ javaType = new JavaType(int[].class);
+ assertEquals(1, javaType.getArrayDepth());
+
+ javaType = new JavaType(void.class);
+ assertEquals(0, javaType.getArrayDepth());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertEquals(0, javaType.getArrayDepth());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertEquals(2, javaType.getArrayDepth());
+ }
+
+ public void testIsArray() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertFalse(javaType.isArray());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertTrue(javaType.isArray());
+
+ javaType = new JavaType(int.class);
+ assertFalse(javaType.isArray());
+
+ javaType = new JavaType(int[].class);
+ assertTrue(javaType.isArray());
+
+ javaType = new JavaType(void.class);
+ assertFalse(javaType.isArray());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertFalse(javaType.isArray());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertTrue(javaType.isArray());
+ }
+
+ public void testJavaClass() throws Exception {
+ this.verifyJavaClass(java.lang.Object.class);
+ this.verifyJavaClass(java.lang.Object[].class);
+ this.verifyJavaClass(int.class);
+ this.verifyJavaClass(int[].class);
+ this.verifyJavaClass(void.class);
+ this.verifyJavaClass(java.util.Map.Entry.class);
+ this.verifyJavaClass(java.util.Map.Entry[][].class);
+ }
+
+ private void verifyJavaClass(Class<?> javaClass) throws Exception {
+ JavaType javaType = new JavaType(javaClass);
+ assertEquals(javaClass, javaType.javaClass());
+ }
+
+ public void testJavaClassName() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertEquals("java.lang.Object", javaType.javaClassName());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertEquals("[Ljava.lang.Object;", javaType.javaClassName());
+
+ javaType = new JavaType(int.class);
+ assertEquals("int", javaType.javaClassName());
+
+ javaType = new JavaType(int[].class);
+ assertEquals("[I", javaType.javaClassName());
+
+ javaType = new JavaType(void.class);
+ assertEquals("void", javaType.javaClassName());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertEquals("java.util.Map$Entry", javaType.javaClassName());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertEquals("[[Ljava.util.Map$Entry;", javaType.javaClassName());
+ }
+
+ public void testDescribes() throws Exception {
+ this.verifyDescribes(java.lang.Object.class);
+ this.verifyDescribes(java.lang.Object[].class);
+ this.verifyDescribes(int.class);
+ this.verifyDescribes(int[].class);
+ this.verifyDescribes(void.class);
+ this.verifyDescribes(java.util.Map.Entry.class);
+ this.verifyDescribes(java.util.Map.Entry[][].class);
+ }
+
+ private void verifyDescribes(Class<?> javaClass) throws Exception {
+ JavaType javaType = new JavaType(javaClass);
+ assertTrue(javaType.describes(javaClass));
+ }
+
+ public void testDeclaration() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertEquals("java.lang.Object", javaType.declaration());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertEquals("java.lang.Object[]", javaType.declaration());
+
+ javaType = new JavaType(int.class);
+ assertEquals("int", javaType.declaration());
+
+ javaType = new JavaType(int[].class);
+ assertEquals("int[]", javaType.declaration());
+
+ javaType = new JavaType(void.class);
+ assertEquals("void", javaType.declaration());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertEquals("java.util.Map.Entry", javaType.declaration());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertEquals("java.util.Map.Entry[][]", javaType.declaration());
+ }
+
+ public void testIsPrimitive() throws Exception {
+ JavaType javaType;
+ javaType = new JavaType(java.lang.Object.class);
+ assertFalse(javaType.isPrimitive());
+
+ javaType = new JavaType(java.lang.Object[].class);
+ assertFalse(javaType.isPrimitive());
+
+ javaType = new JavaType(int.class);
+ assertTrue(javaType.isPrimitive());
+
+ javaType = new JavaType(int[].class);
+ assertFalse(javaType.isPrimitive());
+
+ javaType = new JavaType(void.class);
+ assertTrue(javaType.isPrimitive());
+
+ javaType = new JavaType(java.util.Map.Entry.class);
+ assertFalse(javaType.isPrimitive());
+
+ javaType = new JavaType(java.util.Map.Entry[][].class);
+ assertFalse(javaType.isPrimitive());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JpaUtilityTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JpaUtilityTests.java
new file mode 100644
index 0000000000..f98710f25c
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JpaUtilityTests.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.eclipse.jpt.utility.internal.ClassTools;
+import org.eclipse.jpt.utility.tests.internal.iterators.JpaUtilityIteratorsTests;
+
+/**
+ * decentralize test creation code
+ */
+public class JpaUtilityTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(ClassTools.packageNameFor(JpaUtilityTests.class));
+
+ suite.addTest(JpaUtilityIteratorsTests.suite());
+
+ suite.addTestSuite(BitToolsTests.class);
+ suite.addTestSuite(ClasspathTests.class);
+ suite.addTestSuite(ClassToolsTests.class);
+ suite.addTestSuite(CollectionToolsTests.class);
+ suite.addTestSuite(FileToolsTests.class);
+ suite.addTestSuite(HashBagTests.class);
+ suite.addTestSuite(IndentingPrintWriterTests.class);
+ suite.addTestSuite(JavaTypeTests.class);
+ suite.addTestSuite(JDBCTypeTests.class);
+ suite.addTestSuite(NameToolsTests.class);
+ suite.addTestSuite(ReverseComparatorTests.class);
+ suite.addTestSuite(StringToolsTests.class);
+ suite.addTestSuite(XMLStringEncoderTests.class);
+
+ return suite;
+ }
+
+ private JpaUtilityTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/NameToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/NameToolsTests.java
new file mode 100644
index 0000000000..164fa49e03
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/NameToolsTests.java
@@ -0,0 +1,214 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.CollectionTools;
+import org.eclipse.jpt.utility.internal.NameTools;
+
+public class NameToolsTests extends TestCase {
+
+ public NameToolsTests(String name) {
+ super(name);
+ }
+
+ public void testStringAbsentIgnoreCase() {
+ List<String> colorCollection = this.buildColorCollection();
+ String returned = NameTools.uniqueNameForIgnoreCase("Taupe", colorCollection);
+ assertEquals("Taupe", returned);
+ }
+
+ public void testStringPresentCaseDiffers() {
+ List<String> colorCollection = this.buildColorCollection();
+ String returned = NameTools.uniqueNameFor("green", colorCollection);
+ assertEquals("green", returned);
+ }
+
+ public void testStringPresentIgnoreCase() {
+ List<String> colorCollection = this.buildColorCollection();
+ String returned = NameTools.uniqueNameForIgnoreCase("green", colorCollection);
+ assertEquals("green2", returned);
+ }
+
+ public void testStringPresentWithAppendices() {
+ List<String> colorCollection = this.buildColorCollection();
+ colorCollection.add("Red1");
+ colorCollection.add("red2");
+ String returned = NameTools.uniqueNameForIgnoreCase("red", colorCollection);
+ colorCollection.remove("Red1");
+ colorCollection.remove("red2");
+ assertEquals("red3", returned);
+ }
+
+ private List<String> buildColorCollection() {
+ List<String> colorCollection = new ArrayList<String>();
+ colorCollection.add("Red");
+ colorCollection.add("Orange");
+ colorCollection.add("Yellow");
+ colorCollection.add("Green");
+ colorCollection.add("Blue");
+ colorCollection.add("Indigo");
+ colorCollection.add("Violet");
+ return colorCollection;
+ }
+
+ public void testUniqueNameForCollection1() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("Oracle Corporation");
+ strings.add("Oracle2");
+ strings.add("oracle1");
+ strings.add("Oracl");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings));
+ assertEquals("Test", NameTools.uniqueNameFor("Test", strings));
+
+ assertEquals("Oracle3", NameTools.uniqueNameForIgnoreCase("Oracle", strings));
+ assertEquals("oracle3", NameTools.uniqueNameForIgnoreCase("oracle", strings));
+ assertEquals("Test", NameTools.uniqueNameForIgnoreCase("Test", strings));
+ }
+
+ public void testUniqueNameForCollection2() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("oracle");
+ strings.add("Oracle2");
+ strings.add("Oracle1");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings));
+ assertEquals("Test", NameTools.uniqueNameFor("Test", strings));
+
+ strings.add("Oracle Corporation");
+ assertEquals("Oracle3", NameTools.uniqueNameForIgnoreCase("Oracle", strings));
+ assertEquals("oracle3", NameTools.uniqueNameForIgnoreCase("oracle", strings));
+ assertEquals("Test", NameTools.uniqueNameForIgnoreCase("Test", strings));
+ }
+
+ public void testUniqueNameForCollection3() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("Oracle");
+ strings.add("Oracle2");
+ strings.add("Oracle1");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings));
+ }
+
+ public void testUniqueNameForIterator1() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("Oracle Corporation");
+ strings.add("Oracle2");
+ strings.add("oracle1");
+ strings.add("Oracl");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings.iterator()));
+ assertEquals("Test", NameTools.uniqueNameFor("Test", strings.iterator()));
+
+ assertEquals("Oracle3", NameTools.uniqueNameForIgnoreCase("Oracle", strings.iterator()));
+ assertEquals("oracle3", NameTools.uniqueNameForIgnoreCase("oracle", strings.iterator()));
+ assertEquals("Test", NameTools.uniqueNameForIgnoreCase("Test", strings.iterator()));
+ }
+
+ public void testUniqueNameForIterator2() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("oracle");
+ strings.add("Oracle2");
+ strings.add("Oracle1");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings.iterator()));
+ assertEquals("Test", NameTools.uniqueNameFor("Test", strings.iterator()));
+
+ strings.add("Oracle Corporation");
+ assertEquals("Oracle3", NameTools.uniqueNameForIgnoreCase("Oracle", strings.iterator()));
+ assertEquals("oracle3", NameTools.uniqueNameForIgnoreCase("oracle", strings.iterator()));
+ assertEquals("Test", NameTools.uniqueNameForIgnoreCase("Test", strings.iterator()));
+ }
+
+ public void testUniqueNameForIterator3() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("Oracle");
+ strings.add("Oracle2");
+ strings.add("Oracle1");
+
+ assertEquals("Oracle3", NameTools.uniqueNameFor("Oracle", strings.iterator()));
+ }
+
+ public void testUniqueJavaNameForCollection() {
+ Collection<String> strings = new ArrayList<String>();
+ strings.add("Oracle");
+ strings.add("Oracle");
+ strings.add("Oracle2");
+ strings.add("Oracle1");
+
+ assertEquals("private2", NameTools.uniqueJavaNameFor("private", strings.iterator()));
+ assertEquals("class2", NameTools.uniqueJavaNameFor("class", strings.iterator()));
+ }
+
+ public void testBuildQualifiedDatabaseObjectName() {
+ assertEquals("catalog.schema.name", NameTools.buildQualifiedDatabaseObjectName("catalog", "schema", "name"));
+ assertEquals("catalog..name", NameTools.buildQualifiedDatabaseObjectName("catalog", null, "name"));
+ assertEquals("schema.name", NameTools.buildQualifiedDatabaseObjectName(null, "schema", "name"));
+ assertEquals("name", NameTools.buildQualifiedDatabaseObjectName(null, null, "name"));
+ }
+
+ public void testJavaReservedWords() {
+ assertTrue(CollectionTools.contains(NameTools.javaReservedWords(), "class"));
+ assertFalse(CollectionTools.contains(NameTools.javaReservedWords(), "Class"));
+ assertTrue(CollectionTools.contains(NameTools.javaReservedWords(), "private"));
+ }
+
+ public void testconvertToJavaIdentifierString() {
+ assertEquals("foo", NameTools.convertToJavaIdentifier("foo"));
+ assertEquals("foo1", NameTools.convertToJavaIdentifier("foo1"));
+ assertEquals("Private", NameTools.convertToJavaIdentifier("private"));
+ assertEquals("_foo", NameTools.convertToJavaIdentifier("1foo"));
+ assertEquals("foo_", NameTools.convertToJavaIdentifier("foo%"));
+ assertEquals("foo__bar__", NameTools.convertToJavaIdentifier("foo bar "));
+ }
+
+ public void testconvertToJavaIdentifierStringChar() {
+ assertEquals("foo", NameTools.convertToJavaIdentifier("foo", '$'));
+ assertEquals("foo1", NameTools.convertToJavaIdentifier("foo1", '$'));
+ assertEquals("Private", NameTools.convertToJavaIdentifier("private", '$'));
+ assertEquals("$foo", NameTools.convertToJavaIdentifier("1foo", '$'));
+ assertEquals("foo$", NameTools.convertToJavaIdentifier("foo%", '$'));
+ assertEquals("foo$$bar$$", NameTools.convertToJavaIdentifier("foo bar ", '$'));
+
+ boolean exCaught = false;
+ try {
+ String s = NameTools.convertToJavaIdentifier("1foo", '7');
+ fail("invalid string: \"" + s + "\"");
+ } catch (IllegalArgumentException ex) {
+ if (ex.getMessage().indexOf('7') != -1) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+
+ exCaught = false;
+ try {
+ String s = NameTools.convertToJavaIdentifier("foo%", '^');
+ fail("invalid string: \"" + s + "\"");
+ } catch (IllegalArgumentException ex) {
+ if (ex.getMessage().indexOf('^') != -1) {
+ exCaught = true;
+ }
+ }
+ assertTrue(exCaught);
+
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/RangeTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/RangeTests.java
new file mode 100644
index 0000000000..d7f2cce428
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/RangeTests.java
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.Range;
+
+public class RangeTests extends TestCase {
+
+ public RangeTests(String name) {
+ super(name);
+ }
+
+ public void testIncludes() {
+ Range range = new Range(5, 17);
+ assertFalse(range.includes(-55));
+ assertFalse(range.includes(0));
+ assertFalse(range.includes(4));
+ assertTrue(range.includes(5));
+ assertTrue(range.includes(6));
+ assertTrue(range.includes(16));
+ assertTrue(range.includes(17));
+ assertFalse(range.includes(18));
+ assertFalse(range.includes(200));
+ }
+
+ public void testEquals() {
+ Range range1 = new Range(5, 17);
+ Range range2 = new Range(5, 17);
+ assertNotSame(range1, range2);
+ assertEquals(range1, range1);
+ assertEquals(range1, range2);
+ assertEquals(range2, range1);
+ assertEquals(range1.hashCode(), range2.hashCode());
+
+ range2 = new Range(17, 5);
+ assertFalse(range1.equals(range2));
+ assertFalse(range2.equals(range1));
+ // although they are unequal, they can have the same hash code
+ assertEquals(range1.hashCode(), range2.hashCode());
+
+ range2 = new Range(5, 15);
+ assertFalse(range1.equals(range2));
+ assertFalse(range2.equals(range1));
+ }
+
+ public void testClone() {
+ Range range1 = new Range(5, 17);
+ Range range2 = range1.clone();
+ assertNotSame(range1, range2);
+ assertEquals(range1, range1);
+ assertEquals(range1, range2);
+ assertEquals(range2, range1);
+ assertEquals(range1.hashCode(), range2.hashCode());
+ }
+
+ public void testSerialization() throws Exception {
+ Range range1 = new Range(5, 17);
+ Range range2 = TestTools.serialize(range1);
+ assertNotSame(range1, range2);
+ assertEquals(range1, range1);
+ assertEquals(range1, range2);
+ assertEquals(range2, range1);
+ assertEquals(range1.hashCode(), range2.hashCode());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ReverseComparatorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ReverseComparatorTests.java
new file mode 100644
index 0000000000..7ae00fdc2e
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ReverseComparatorTests.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.ReverseComparator;
+
+public class ReverseComparatorTests extends TestCase {
+ private Comparator<String> naturalReverseComparator;
+ private Comparator<String> customComparator;
+ private Comparator<String> customReverseComparator;
+
+ public ReverseComparatorTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.naturalReverseComparator = new ReverseComparator<String>();
+ this.customComparator = this.buildCustomComparator();
+ this.customReverseComparator = new ReverseComparator<String>(this.customComparator);
+ }
+
+ private Comparator<String> buildCustomComparator() {
+ return new Comparator<String>() {
+ public int compare(String s1, String s2) {
+ String lower1 = s1.toLowerCase();
+ String lower2 = s2.toLowerCase();
+ int result = lower1.compareTo(lower2);
+ if (result == 0) {
+ return s1.compareTo(s2); // use case to differentiate "equal" strings
+ }
+ return result;
+ }
+ };
+ }
+
+ private List<String> buildUnsortedList() {
+ List<String> result = new ArrayList<String>();
+ result.add("T");
+ result.add("Z");
+ result.add("Y");
+ result.add("M");
+ result.add("m");
+ result.add("a");
+ result.add("B");
+ result.add("b");
+ result.add("A");
+ return result;
+ }
+
+ private List<String> buildNaturallySortedList() {
+ List<String> result = new ArrayList<String>(this.buildUnsortedList());
+ Collections.sort(result);
+ return result;
+ }
+
+ private List<String> buildCustomSortedList() {
+ List<String> result = new ArrayList<String>(this.buildUnsortedList());
+ Collections.sort(result, this.customComparator);
+ return result;
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+ public void testNatural() {
+ List<String> list = this.buildUnsortedList();
+ Collections.sort(list, this.naturalReverseComparator);
+ this.verifyList(this.buildNaturallySortedList(), list);
+ }
+
+ public void testCustom() {
+ List<String> list = this.buildUnsortedList();
+ Collections.sort(list, this.customReverseComparator);
+ this.verifyList(this.buildCustomSortedList(), list);
+ }
+
+ private void verifyList(List<String> normal, List<String> reverse) {
+ int size = normal.size();
+ int max = size - 1;
+ for (int i = 0; i < size; i++) {
+ assertEquals(normal.get(i), reverse.get(max - i));
+ }
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/StringToolsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/StringToolsTests.java
new file mode 100644
index 0000000000..8e911e8e5a
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/StringToolsTests.java
@@ -0,0 +1,698 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.io.StringWriter;
+import java.io.Writer;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.StringTools;
+
+public class StringToolsTests extends TestCase {
+
+ public StringToolsTests(String name) {
+ super(name);
+ }
+
+ // ********** padding/truncating **********
+
+ public void testPad() {
+ assertEquals("fred", StringTools.pad("fred", 4));
+ assertEquals("fred ", StringTools.pad("fred", 6));
+ boolean exThrown = false;
+ try {
+ assertEquals("fr", StringTools.pad("fred", 2));
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testPadCharArray() {
+ assertEquals("fred", new String(StringTools.pad(new char[] { 'f', 'r', 'e', 'd' }, 4)));
+ assertEquals("fred ", new String(StringTools.pad(new char[] { 'f', 'r', 'e', 'd' }, 6)));
+ boolean exThrown = false;
+ try {
+ assertEquals("fr", new String(StringTools.pad(new char[] { 'f', 'r', 'e', 'd' }, 2)));
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testPadOnWriter() {
+ Writer writer;
+ writer = new StringWriter();
+ StringTools.padOn("fred", 4, writer);
+ assertEquals("fred", writer.toString());
+
+ writer = new StringWriter();
+ StringTools.padOn("fred", 6, writer);
+ assertEquals("fred ", writer.toString());
+
+ writer = new StringWriter();
+ boolean exThrown = false;
+ try {
+ StringTools.padOn("fred", 2, writer);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testPadOnStringBuffer() {
+ StringBuffer sb;
+ sb = new StringBuffer();
+ StringTools.padOn("fred", 4, sb);
+ assertEquals("fred", sb.toString());
+
+ sb = new StringBuffer();
+ StringTools.padOn("fred", 6, sb);
+ assertEquals("fred ", sb.toString());
+
+ sb = new StringBuffer();
+ boolean exThrown = false;
+ try {
+ StringTools.padOn("fred", 2, sb);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testPadOrTruncate() {
+ assertEquals("fred", StringTools.padOrTruncate("fred", 4));
+ assertEquals("fred ", StringTools.padOrTruncate("fred", 6));
+ assertEquals("fr", StringTools.padOrTruncate("fred", 2));
+ }
+
+ public void testPadOrTruncateCharArray() {
+ assertEquals("fred", new String(StringTools.padOrTruncate(new char[] { 'f', 'r', 'e', 'd' }, 4)));
+ assertEquals("fred ", new String(StringTools.padOrTruncate(new char[] { 'f', 'r', 'e', 'd' }, 6)));
+ assertEquals("fr", new String(StringTools.padOrTruncate(new char[] { 'f', 'r', 'e', 'd' }, 2)));
+ }
+
+ public void testPadOrTruncateOnWriter() {
+ this.verifyPadOrTruncateOnWriter("fred", "fred", 4);
+ this.verifyPadOrTruncateOnWriter("fred ", "fred", 6);
+ this.verifyPadOrTruncateOnWriter("fr", "fred", 2);
+ }
+
+ private void verifyPadOrTruncateOnWriter(String expected, String string, int length) {
+ Writer writer = new StringWriter();
+ StringTools.padOrTruncateOn(string, length, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testPadOrTruncateOnStringBuffer() {
+ this.verifyPadOrTruncateOnStringBuffer("fred", "fred", 4);
+ this.verifyPadOrTruncateOnStringBuffer("fred ", "fred", 6);
+ this.verifyPadOrTruncateOnStringBuffer("fr", "fred", 2);
+ }
+
+ private void verifyPadOrTruncateOnStringBuffer(String expected, String string, int length) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.padOrTruncateOn(string, length, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ public void testZeroPad() {
+ assertEquals("1234", StringTools.zeroPad("1234", 4));
+ assertEquals("001234", StringTools.zeroPad("1234", 6));
+ boolean exThrown = false;
+ try {
+ assertEquals("12", StringTools.zeroPad("1234", 2));
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testZeroPadCharArray() {
+ assertEquals("1234", new String(StringTools.zeroPad(new char[] { '1', '2', '3', '4' }, 4)));
+ assertEquals("001234", new String(StringTools.zeroPad(new char[] { '1', '2', '3', '4' }, 6)));
+ boolean exThrown = false;
+ try {
+ assertEquals("12", new String(StringTools.zeroPad(new char[] { '1', '2', '3', '4' }, 2)));
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testZeroPadOnWriter() {
+ Writer writer;
+ writer = new StringWriter();
+ StringTools.zeroPadOn("1234", 4, writer);
+ assertEquals("1234", writer.toString());
+
+ writer = new StringWriter();
+ StringTools.zeroPadOn("1234", 6, writer);
+ assertEquals("001234", writer.toString());
+
+ writer = new StringWriter();
+ boolean exThrown = false;
+ try {
+ StringTools.zeroPadOn("1234", 2, writer);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testZeroPadOnStringBuffer() {
+ StringBuffer sb;
+ sb = new StringBuffer();
+ StringTools.zeroPadOn("1234", 4, sb);
+ assertEquals("1234", sb.toString());
+
+ sb = new StringBuffer();
+ StringTools.zeroPadOn("1234", 6, sb);
+ assertEquals("001234", sb.toString());
+
+ sb = new StringBuffer();
+ boolean exThrown = false;
+ try {
+ StringTools.zeroPadOn("1234", 2, sb);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ exThrown = true;
+ }
+ assertTrue(exThrown);
+ }
+
+ public void testZeroPadOrTruncate() {
+ assertEquals("1234", StringTools.zeroPadOrTruncate("1234", 4));
+ assertEquals("001234", StringTools.zeroPadOrTruncate("1234", 6));
+ assertEquals("34", StringTools.zeroPadOrTruncate("1234", 2));
+ }
+
+ public void testZeroPadOrTruncateCharArray() {
+ assertEquals("1234", new String(StringTools.zeroPadOrTruncate(new char[] { '1', '2', '3', '4' }, 4)));
+ assertEquals("001234", new String(StringTools.zeroPadOrTruncate(new char[] { '1', '2', '3', '4' }, 6)));
+ assertEquals("34", new String(StringTools.zeroPadOrTruncate(new char[] { '1', '2', '3', '4' }, 2)));
+ }
+
+ public void testZeroPadOrTruncateOnWriter() {
+ this.verifyZeroPadOrTruncateOnWriter("1234", "1234", 4);
+ this.verifyZeroPadOrTruncateOnWriter("001234", "1234", 6);
+ this.verifyZeroPadOrTruncateOnWriter("34", "1234", 2);
+ }
+
+ private void verifyZeroPadOrTruncateOnWriter(String expected, String string, int length) {
+ Writer writer = new StringWriter();
+ StringTools.zeroPadOrTruncateOn(string, length, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testZeroPadOrTruncateOnStringBuffer() {
+ this.verifyZeroPadOrTruncateOnStringBuffer("1234", "1234", 4);
+ this.verifyZeroPadOrTruncateOnStringBuffer("001234", "1234", 6);
+ this.verifyZeroPadOrTruncateOnStringBuffer("34", "1234", 2);
+ }
+
+ private void verifyZeroPadOrTruncateOnStringBuffer(String expected, String string, int length) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.zeroPadOrTruncateOn(string, length, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ // ********** removing characters **********
+
+ public void testRemoveFirstOccurrence() {
+ this.verifyRemoveFirstOccurrence("Emplo&yee", '&', "Employee");
+ this.verifyRemoveFirstOccurrence("Emplo&yee&", '&', "Employee&");
+ this.verifyRemoveFirstOccurrence("Employee &Foo", '&', "Employee Foo");
+ this.verifyRemoveFirstOccurrence("Employee&", '&', "Employee");
+ this.verifyRemoveFirstOccurrence("&Employee", '&', "Employee");
+ }
+
+ private void verifyRemoveFirstOccurrence(String string, char charToRemove, String expectedString) {
+ assertEquals(expectedString, StringTools.removeFirstOccurrence(string, charToRemove));
+ }
+
+ public void testRemoveFirstOccurrenceCharArray() {
+ this.verifyRemoveFirstOccurrenceCharArray("Emplo&yee", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceCharArray("Emplo&yee&", '&', "Employee&");
+ this.verifyRemoveFirstOccurrenceCharArray("Employee &Foo", '&', "Employee Foo");
+ this.verifyRemoveFirstOccurrenceCharArray("Employee&", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceCharArray("&Employee", '&', "Employee");
+ }
+
+ private void verifyRemoveFirstOccurrenceCharArray(String string, char charToRemove, String expectedString) {
+ assertEquals(expectedString, new String(StringTools.removeFirstOccurrence(string.toCharArray(), charToRemove)));
+ }
+
+ public void testRemoveFirstOccurrenceOnWriter() {
+ this.verifyRemoveFirstOccurrenceOnWriter("Emplo&yee", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceOnWriter("Emplo&yee&", '&', "Employee&");
+ this.verifyRemoveFirstOccurrenceOnWriter("Employee &Foo", '&', "Employee Foo");
+ this.verifyRemoveFirstOccurrenceOnWriter("Employee&", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceOnWriter("&Employee", '&', "Employee");
+ }
+
+ private void verifyRemoveFirstOccurrenceOnWriter(String string, char charToRemove, String expectedString) {
+ Writer writer = new StringWriter();
+ StringTools.removeFirstOccurrenceOn(string, charToRemove, writer);
+ assertEquals(expectedString, writer.toString());
+ }
+
+ public void testRemoveFirstOccurrenceOnStringBuffer() {
+ this.verifyRemoveFirstOccurrenceOnStringBuffer("Emplo&yee", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceOnStringBuffer("Emplo&yee&", '&', "Employee&");
+ this.verifyRemoveFirstOccurrenceOnStringBuffer("Employee &Foo", '&', "Employee Foo");
+ this.verifyRemoveFirstOccurrenceOnStringBuffer("Employee&", '&', "Employee");
+ this.verifyRemoveFirstOccurrenceOnStringBuffer("&Employee", '&', "Employee");
+ }
+
+ private void verifyRemoveFirstOccurrenceOnStringBuffer(String string, char charToRemove, String expectedString) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.removeFirstOccurrenceOn(string, charToRemove, sb);
+ assertEquals(expectedString, sb.toString());
+ }
+
+ public void testRemoveAllOccurrences() {
+ this.verifyRemoveAllOccurrences("Employee Fred", ' ', "EmployeeFred");
+ this.verifyRemoveAllOccurrences(" Employee ", ' ', "Employee");
+ this.verifyRemoveAllOccurrences("Employee Foo", ' ', "EmployeeFoo");
+ this.verifyRemoveAllOccurrences(" Emp loyee Foo", ' ', "EmployeeFoo");
+ }
+
+ private void verifyRemoveAllOccurrences(String string, char charToRemove, String expectedString) {
+ assertEquals(expectedString, StringTools.removeAllOccurrences(string, charToRemove));
+ }
+
+ public void testRemoveAllOccurrencesCharArray() {
+ this.verifyRemoveAllOccurrencesCharArray("Employee Fred", ' ', "EmployeeFred");
+ this.verifyRemoveAllOccurrencesCharArray(" Employee ", ' ', "Employee");
+ this.verifyRemoveAllOccurrencesCharArray("Employee Foo", ' ', "EmployeeFoo");
+ this.verifyRemoveAllOccurrencesCharArray(" Emp loyee Foo", ' ', "EmployeeFoo");
+ }
+
+ private void verifyRemoveAllOccurrencesCharArray(String string, char charToRemove, String expectedString) {
+ assertEquals(expectedString, new String(StringTools.removeAllOccurrences(string.toCharArray(), charToRemove)));
+ }
+
+ public void testRemoveAllOccurrencesOnWriter() {
+ this.verifyRemoveAllOccurrencesOnWriter("Employee Fred", ' ', "EmployeeFred");
+ this.verifyRemoveAllOccurrencesOnWriter(" Employee ", ' ', "Employee");
+ this.verifyRemoveAllOccurrencesOnWriter("Employee Foo", ' ', "EmployeeFoo");
+ this.verifyRemoveAllOccurrencesOnWriter(" Emp loyee Foo", ' ', "EmployeeFoo");
+ }
+
+ private void verifyRemoveAllOccurrencesOnWriter(String string, char charToRemove, String expectedString) {
+ Writer writer = new StringWriter();
+ StringTools.removeAllOccurrencesOn(string, charToRemove, writer);
+ assertEquals(expectedString, writer.toString());
+ }
+
+ public void testRemoveAllOccurrencesOnStringBuffer() {
+ this.verifyRemoveAllOccurrencesOnStringBuffer("Employee Fred", ' ', "EmployeeFred");
+ this.verifyRemoveAllOccurrencesOnStringBuffer(" Employee ", ' ', "Employee");
+ this.verifyRemoveAllOccurrencesOnStringBuffer("Employee Foo", ' ', "EmployeeFoo");
+ this.verifyRemoveAllOccurrencesOnStringBuffer(" Emp loyee Foo", ' ', "EmployeeFoo");
+ }
+
+ private void verifyRemoveAllOccurrencesOnStringBuffer(String string, char charToRemove, String expectedString) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.removeAllOccurrencesOn(string, charToRemove, sb);
+ assertEquals(expectedString, sb.toString());
+ }
+
+ // ********** common prefix **********
+
+ public void testCommonPrefixLength() {
+ assertEquals(3, StringTools.commonPrefixLength("fooZZZ", "fooBBB"));
+ assertEquals(3, StringTools.commonPrefixLength("foo", "fooBBB"));
+ assertEquals(3, StringTools.commonPrefixLength("fooZZZ", "foo"));
+ assertEquals(3, StringTools.commonPrefixLength("foo", "foo"));
+ }
+
+ // ********** capitalization **********
+
+ public void testCapitalizeCharArray() {
+ this.verifyCapitalizeCharArray("Oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeCharArray("Oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeCharArray(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyCapitalizeCharArray("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyCapitalizeCharArray("", new char[0]);
+ this.verifyCapitalizeCharArray("A", new char[] { 'a' });
+ this.verifyCapitalizeCharArray("\u00C9cole", new char[] { '\u00E9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyCapitalizeCharArray(String expected, char[] string) {
+ assertEquals(expected, new String(StringTools.capitalize(string)));
+ }
+
+ public void testCapitalizeString() {
+ this.verifyCapitalizeString("Oracle", "Oracle");
+ this.verifyCapitalizeString("Oracle", "oracle");
+ this.verifyCapitalizeString(" ", " ");
+ this.verifyCapitalizeString("ORACLE", "ORACLE");
+ this.verifyCapitalizeString("", "");
+ this.verifyCapitalizeString("A", "a");
+ this.verifyCapitalizeString("\u00C9cole", "\u00E9cole"); // �cole->�COLE
+ }
+
+ private void verifyCapitalizeString(String expected, String string) {
+ assertEquals(expected, StringTools.capitalize(string));
+ }
+
+ public void testCapitalizeOnCharArrayStringBuffer() {
+ this.verifyCapitalizeOnCharArrayStringBuffer("Oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeOnCharArrayStringBuffer("Oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeOnCharArrayStringBuffer(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyCapitalizeOnCharArrayStringBuffer("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyCapitalizeOnCharArrayStringBuffer("", new char[0]);
+ this.verifyCapitalizeOnCharArrayStringBuffer("A", new char[] { 'a' });
+ this.verifyCapitalizeOnCharArrayStringBuffer("\u00C9cole", new char[] { '\u00E9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyCapitalizeOnCharArrayStringBuffer(String expected, char[] string) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.capitalizeOn(string, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ public void testCapitalizeOnStringStringBuffer() {
+ this.verifyCapitalizeOnStringStringBuffer("Oracle", "Oracle");
+ this.verifyCapitalizeOnStringStringBuffer("Oracle", "oracle");
+ this.verifyCapitalizeOnStringStringBuffer(" ", " ");
+ this.verifyCapitalizeOnStringStringBuffer("ORACLE", "ORACLE");
+ this.verifyCapitalizeOnStringStringBuffer("", "");
+ this.verifyCapitalizeOnStringStringBuffer("A", "a");
+ this.verifyCapitalizeOnStringStringBuffer("\u00C9cole", "\u00E9cole"); // �cole->�COLE
+ }
+
+ private void verifyCapitalizeOnStringStringBuffer(String expected, String string) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.capitalizeOn(string, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ public void testCapitalizeOnCharArrayWriter() {
+ this.verifyCapitalizeOnCharArrayWriter("Oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeOnCharArrayWriter("Oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyCapitalizeOnCharArrayWriter(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyCapitalizeOnCharArrayWriter("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyCapitalizeOnCharArrayWriter("", new char[0]);
+ this.verifyCapitalizeOnCharArrayWriter("A", new char[] { 'a' });
+ this.verifyCapitalizeOnCharArrayWriter("\u00C9cole", new char[] { '\u00E9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyCapitalizeOnCharArrayWriter(String expected, char[] string) {
+ Writer writer = new StringWriter();
+ StringTools.capitalizeOn(string, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testCapitalizeOnStringWriter() {
+ this.verifyCapitalizeOnStringWriter("Oracle", "Oracle");
+ this.verifyCapitalizeOnStringWriter("Oracle", "oracle");
+ this.verifyCapitalizeOnStringWriter(" ", " ");
+ this.verifyCapitalizeOnStringWriter("ORACLE", "ORACLE");
+ this.verifyCapitalizeOnStringWriter("", "");
+ this.verifyCapitalizeOnStringWriter("A", "a");
+ this.verifyCapitalizeOnStringWriter("\u00C9cole", "\u00E9cole"); // �cole->�COLE
+ }
+
+ private void verifyCapitalizeOnStringWriter(String expected, String string) {
+ Writer writer = new StringWriter();
+ StringTools.capitalizeOn(string, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testUnapitalizeCharArray() {
+ this.verifyUncapitalizeCharArray("oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeCharArray("oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeCharArray(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyUncapitalizeCharArray("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyUncapitalizeCharArray("", new char[0]);
+ this.verifyUncapitalizeCharArray("a", new char[] { 'A' });
+ this.verifyUncapitalizeCharArray("\u00E9cole", new char[] { '\u00C9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyUncapitalizeCharArray(String expected, char[] string) {
+ assertEquals(expected, new String(StringTools.uncapitalize(string)));
+ }
+
+ public void testUncapitalizeString() {
+ this.verifyUncapitalizeString("oracle", "Oracle");
+ this.verifyUncapitalizeString("oracle", "oracle");
+ this.verifyUncapitalizeString(" ", " ");
+ this.verifyUncapitalizeString("ORACLE", "ORACLE");
+ this.verifyUncapitalizeString("", "");
+ this.verifyUncapitalizeString("a", "A");
+ this.verifyUncapitalizeString("\u00E9cole", "\u00C9cole"); // �cole->�COLE
+ }
+
+ private void verifyUncapitalizeString(String expected, String string) {
+ assertEquals(expected, StringTools.uncapitalize(string));
+ }
+
+ public void testUncapitalizeOnCharArrayStringBuffer() {
+ this.verifyUncapitalizeOnCharArrayStringBuffer("oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeOnCharArrayStringBuffer("oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeOnCharArrayStringBuffer(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyUncapitalizeOnCharArrayStringBuffer("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyUncapitalizeOnCharArrayStringBuffer("", new char[0]);
+ this.verifyUncapitalizeOnCharArrayStringBuffer("a", new char[] { 'A' });
+ this.verifyUncapitalizeOnCharArrayStringBuffer("\u00E9cole", new char[] { '\u00C9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyUncapitalizeOnCharArrayStringBuffer(String expected, char[] string) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.uncapitalizeOn(string, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ public void testUncapitalizeOnStringStringBuffer() {
+ this.verifyUncapitalizeOnStringStringBuffer("oracle", "Oracle");
+ this.verifyUncapitalizeOnStringStringBuffer("oracle", "oracle");
+ this.verifyUncapitalizeOnStringStringBuffer(" ", " ");
+ this.verifyUncapitalizeOnStringStringBuffer("ORACLE", "ORACLE");
+ this.verifyUncapitalizeOnStringStringBuffer("", "");
+ this.verifyUncapitalizeOnStringStringBuffer("a", "A");
+ this.verifyUncapitalizeOnStringStringBuffer("\u00E9cole", "\u00C9cole"); // �cole->�COLE
+ }
+
+ private void verifyUncapitalizeOnStringStringBuffer(String expected, String string) {
+ StringBuffer sb = new StringBuffer();
+ StringTools.uncapitalizeOn(string, sb);
+ assertEquals(expected, sb.toString());
+ }
+
+ public void testUncapitalizeOnCharArrayWriter() {
+ this.verifyUncapitalizeOnCharArrayWriter("oracle", new char[] { 'O', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeOnCharArrayWriter("oracle", new char[] { 'o', 'r', 'a', 'c', 'l', 'e' });
+ this.verifyUncapitalizeOnCharArrayWriter(" ", new char[] { ' ', ' ', ' ' });
+ this.verifyUncapitalizeOnCharArrayWriter("ORACLE", new char[] { 'O', 'R', 'A', 'C', 'L', 'E' });
+ this.verifyUncapitalizeOnCharArrayWriter("", new char[0]);
+ this.verifyUncapitalizeOnCharArrayWriter("a", new char[] { 'A' });
+ this.verifyUncapitalizeOnCharArrayWriter("\u00E9cole", new char[] { '\u00C9', 'c', 'o', 'l', 'e' });
+ }
+
+ private void verifyUncapitalizeOnCharArrayWriter(String expected, char[] string) {
+ Writer writer = new StringWriter();
+ StringTools.uncapitalizeOn(string, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testUncapitalizeOnStringWriter() {
+ this.verifyUncapitalizeOnStringWriter("oracle", "Oracle");
+ this.verifyUncapitalizeOnStringWriter("oracle", "oracle");
+ this.verifyUncapitalizeOnStringWriter(" ", " ");
+ this.verifyUncapitalizeOnStringWriter("ORACLE", "ORACLE");
+ this.verifyUncapitalizeOnStringWriter("", "");
+ this.verifyUncapitalizeOnStringWriter("a", "A");
+ this.verifyUncapitalizeOnStringWriter("\u00E9cole", "\u00C9cole"); // �cole->�COLE
+ }
+
+ private void verifyUncapitalizeOnStringWriter(String expected, String string) {
+ Writer writer = new StringWriter();
+ StringTools.uncapitalizeOn(string, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ // ********** queries **********
+
+ public void testStringIsEmpty() {
+ assertTrue(StringTools.stringIsEmpty((String) null));
+ assertTrue(StringTools.stringIsEmpty(""));
+ assertTrue(StringTools.stringIsEmpty(" "));
+ assertTrue(StringTools.stringIsEmpty(" \t\t "));
+ assertTrue(StringTools.stringIsEmpty(" \t\t " + StringTools.CR));
+ }
+
+ public void testStringsAreEqualIgnoreCase() {
+ assertTrue(StringTools.stringsAreEqualIgnoreCase((String) null, (String) null));
+ assertFalse(StringTools.stringsAreEqualIgnoreCase(null, "asdf"));
+ assertFalse(StringTools.stringsAreEqualIgnoreCase("asdf", null));
+ assertTrue(StringTools.stringsAreEqualIgnoreCase("asdf", "asdf"));
+ assertTrue(StringTools.stringsAreEqualIgnoreCase("asdf", "ASDF"));
+ }
+
+ // ********** conversions **********
+
+ public void testConvertCamelCaseToAllCaps() {
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("test"));
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("TEST"));
+ assertEquals("TEST_TEST", StringTools.convertCamelCaseToAllCaps("testTest"));
+ assertEquals("TEST_TEST", StringTools.convertCamelCaseToAllCaps("TestTest"));
+ assertEquals("TEST_TEST_TEST", StringTools.convertCamelCaseToAllCaps("testTESTTest"));
+ assertEquals("TEST_TEST_TEST", StringTools.convertCamelCaseToAllCaps("TestTESTTest"));
+ assertEquals("TEST_TEST_TEST_T", StringTools.convertCamelCaseToAllCaps("TestTESTTestT"));
+ }
+
+ public void testConvertCamelCaseToAllCapsOnWriter() {
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST", "test");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST", "TEST");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST_TEST", "testTest");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST_TEST", "TestTest");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST_TEST_TEST", "testTESTTest");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST_TEST_TEST", "TestTESTTest");
+ this.verifyConvertCamelCaseToAllCapsOnWriter("TEST_TEST_TEST_T", "TestTESTTestT");
+ }
+
+ private void verifyConvertCamelCaseToAllCapsOnWriter(String expected, String string) {
+ Writer writer = new StringWriter();
+ StringTools.convertCamelCaseToAllCapsOn(string, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testConvertCamelCaseToAllCapsMaxLength() {
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("test", 44));
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("test", 4));
+ assertEquals("TES", StringTools.convertCamelCaseToAllCaps("test", 3));
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("TEST", 5));
+ assertEquals("TE", StringTools.convertCamelCaseToAllCaps("TEST", 2));
+ assertEquals("TEST_TEST", StringTools.convertCamelCaseToAllCaps("testTest", 9));
+ assertEquals("TEST_TES", StringTools.convertCamelCaseToAllCaps("testTest", 8));
+ assertEquals("TEST_T", StringTools.convertCamelCaseToAllCaps("testTest", 6));
+ assertEquals("TEST_", StringTools.convertCamelCaseToAllCaps("testTest", 5));
+ assertEquals("TEST", StringTools.convertCamelCaseToAllCaps("testTest", 4));
+ assertEquals("TEST_TEST", StringTools.convertCamelCaseToAllCaps("TestTest", 9));
+ assertEquals("TEST_TEST", StringTools.convertCamelCaseToAllCaps("TestTest", 1100));
+ assertEquals("TEST_TEST_", StringTools.convertCamelCaseToAllCaps("testTESTTest", 10));
+ assertEquals("TEST_TEST_TEST", StringTools.convertCamelCaseToAllCaps("TestTESTTest", 14));
+ assertEquals("TEST_TEST_TEST_T", StringTools.convertCamelCaseToAllCaps("TestTESTTestT", 16));
+ assertEquals("TEST_TEST_TEST_", StringTools.convertCamelCaseToAllCaps("TestTESTTestT", 15));
+ }
+
+ public void testConvertCamelCaseToAllCapsMaxLengthOnWriter() {
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST", "test", 44);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST", "test", 4);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TES", "test", 3);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST", "TEST", 5);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TE", "TEST", 2);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST", "testTest", 9);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TES", "testTest", 8);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_T", "testTest", 6);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_", "testTest", 5);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST", "testTest", 4);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST", "TestTest", 9);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST", "TestTest", 1100);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST_", "testTESTTest", 10);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST_TEST", "TestTESTTest", 14);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST_TEST_T", "TestTESTTestT", 16);
+ this.verifyConvertCamelCaseToAllCapsMaxLengthOnWriter("TEST_TEST_TEST_", "TestTESTTestT", 15);
+ }
+
+ private void verifyConvertCamelCaseToAllCapsMaxLengthOnWriter(String expected, String string, int max) {
+ Writer writer = new StringWriter();
+ StringTools.convertCamelCaseToAllCapsOn(string, max, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+ public void testConvertUnderscoresToCamelCase() {
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("TEST", false));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("TEST_", false));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("TEST____", false));
+ assertEquals("Test", StringTools.convertUnderscoresToCamelCase("TEST", true));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("TeST", false));
+ assertEquals("testTest", StringTools.convertUnderscoresToCamelCase("TEST_TEST", false));
+ assertEquals("testTest", StringTools.convertUnderscoresToCamelCase("TEST___TEST", false));
+ assertEquals("TestTest", StringTools.convertUnderscoresToCamelCase("TEST_TEST", true));
+ assertEquals("testTestTest", StringTools.convertUnderscoresToCamelCase("TEST_TEST_TEST", false));
+ assertEquals("TestTestTest", StringTools.convertUnderscoresToCamelCase("TEST_TEST_TEST", true));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("TEST_TEST_TEST_T", false));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("_TEST_TEST_TEST_T", false));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("__TEST_TEST_TEST_T", false));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("TEST_TEST_TEST_T", true));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("_TEST_TEST_TEST_T", true));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("__TEST_TEST_TEST_T", true));
+ }
+
+ public void testConvertUnderscoresToCamelCaseLowercase() {
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("test", false));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("test_", false));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("test____", false));
+ assertEquals("Test", StringTools.convertUnderscoresToCamelCase("test", true));
+ assertEquals("test", StringTools.convertUnderscoresToCamelCase("test", false));
+ assertEquals("testTest", StringTools.convertUnderscoresToCamelCase("test_test", false));
+ assertEquals("testTest", StringTools.convertUnderscoresToCamelCase("test___test", false));
+ assertEquals("TestTest", StringTools.convertUnderscoresToCamelCase("test_test", true));
+ assertEquals("testTestTest", StringTools.convertUnderscoresToCamelCase("test_test_test", false));
+ assertEquals("TestTestTest", StringTools.convertUnderscoresToCamelCase("test_test_test", true));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("test_test_test_t", false));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("_test_test_test_t", false));
+ assertEquals("testTestTestT", StringTools.convertUnderscoresToCamelCase("__test_test_test_t", false));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("test_test_test_t", true));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("_test_test_test_t", true));
+ assertEquals("TestTestTestT", StringTools.convertUnderscoresToCamelCase("__test_test_test_t", true));
+ }
+
+ public void testConvertUnderscoresToCamelCaseOnWriter() {
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "TEST", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "TEST_", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "TEST____", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("Test", "TEST", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "TeST", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTest", "TEST_TEST", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTest", "TEST___TEST", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTest", "TEST_TEST", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTest", "TEST_TEST_TEST", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTest", "TEST_TEST_TEST", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "TEST_TEST_TEST_T", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "_TEST_TEST_TEST_T", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "__TEST_TEST_TEST_T", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "TEST_TEST_TEST_T", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "_TEST_TEST_TEST_T", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "__TEST_TEST_TEST_T", true);
+ }
+
+ public void testConvertUnderscoresToCamelCaseOnWriterLowercase() {
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "test", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "test_", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "test____", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("Test", "test", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("test", "test", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTest", "test_test", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTest", "test___test", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTest", "test_test", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTest", "test_test_test", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTest", "test_test_test", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "test_test_test_t", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "_test_test_test_t", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("testTestTestT", "__test_test_test_t", false);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "test_test_test_t", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "_test_test_test_t", true);
+ this.verifyConvertUnderscoresToCamelCaseOnWriter("TestTestTestT", "__test_test_test_t", true);
+ }
+
+ private void verifyConvertUnderscoresToCamelCaseOnWriter(String expected, String string, boolean capitalizeFirstLetter) {
+ Writer writer = new StringWriter();
+ StringTools.convertUnderscoresToCamelCaseOn(string, capitalizeFirstLetter, writer);
+ assertEquals(expected, writer.toString());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/TestTools.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/TestTools.java
new file mode 100644
index 0000000000..1eed587df1
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/TestTools.java
@@ -0,0 +1,156 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import junit.framework.TestCase;
+import junit.framework.TestFailure;
+import junit.framework.TestResult;
+
+/**
+ * various tools that can be used by test cases
+ */
+public final class TestTools {
+
+ /**
+ * test an object's implementation of Serializable by serializing the
+ * specified object to a byte array; then de-serializing the byte array and
+ * returning the resultant object
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T serialize(T o) throws IOException, ClassNotFoundException {
+ ByteArrayOutputStream baOutStream = new ByteArrayOutputStream(2000);
+ ObjectOutputStream outStream = new ObjectOutputStream(baOutStream);
+ outStream.writeObject(o);
+ outStream.close();
+
+ ByteArrayInputStream baInStream = new ByteArrayInputStream(baOutStream.toByteArray());
+ ObjectInputStream inStream = new ObjectInputStream(baInStream);
+ T o2 = (T) inStream.readObject();
+ inStream.close();
+
+ return o2;
+ }
+
+ /**
+ * some tests require access to the Web (e.g. any tests that parse an XML
+ * document that specifies a DTD or Schema that is loaded from the Web); use
+ * this method to configure the JDK proxy settings
+ */
+ public static void setUpOracleProxy() {
+ System.setProperty("http.proxyHost", "www-proxy.us.oracle.com");
+ System.setProperty("http.proxyPort", "80");
+ }
+
+ /**
+ * some tests require access to the Web (e.g. any tests that parse an XML
+ * document that specifies a DTD or Schema that is loaded from the Web); use
+ * this method to configure the JDK proxy settings via the command-line
+ */
+ public static String[] buildOracleProxyCommandLineOptions() {
+ return new String[] { "-Dhttp.proxyHost=www-proxy.us.oracle.com", "-Dhttp.proxyPort=80" };
+ }
+
+ /**
+ * redirect std out and std err to the specified stream
+ */
+ public static void redirectSystemStreamsTo(OutputStream outputStream) {
+ redirectSystemStreamsTo(new PrintStream(outputStream));
+ }
+
+ /**
+ * redirect std out and std err to the specified stream
+ */
+ public static void redirectSystemStreamsTo(PrintStream printStream) {
+ System.setOut(printStream);
+ System.setErr(printStream);
+ }
+
+ /**
+ * execute the specified test and dump the results to the console
+ */
+ public static String execute(TestCase testCase) {
+ long start = System.currentTimeMillis();
+ TestResult result = testCase.run();
+ long end = System.currentTimeMillis();
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ writer.print(testCase.getName());
+ writer.print(": ");
+ if (result.wasSuccessful()) {
+ writer.println("OK");
+ } else {
+ TestFailure failure = null;
+ if (result.failures().hasMoreElements()) {
+ failure = (TestFailure) result.failures().nextElement();
+ } else {
+ failure = (TestFailure) result.errors().nextElement();
+ }
+ failure.thrownException().printStackTrace(writer);
+ }
+ writer.print("elapsed time: ");
+ long elapsed = end - start;
+ writer.print(elapsed / 1000L);
+ writer.println(" sec.");
+ return stringWriter.toString();
+ }
+
+ private static final Class<TestCase> TestCase_class = TestCase.class;
+
+ /**
+ * Clear out all the instance variable of the specified test case, allowing
+ * the various test fixtures to be garbage-collected. Typically this is
+ * called in the #tearDown() method.
+ */
+ public static void clear(TestCase testCase) throws IllegalAccessException {
+ for (Class<?> tempClass = testCase.getClass(); tempClass != TestCase_class; tempClass = tempClass.getSuperclass()) {
+ Field[] fields = tempClass.getDeclaredFields();
+ for (int i = fields.length; i-- > 0;) {
+ Field field = fields[i];
+ // leave primitives alone - they don't get garbage-collected, and we can't set them to null...
+ if (field.getType().isPrimitive()) {
+ continue;
+ }
+ // leave static fields alone (?)
+ if (Modifier.isStatic(field.getModifiers())) {
+ continue;
+ }
+ field.setAccessible(true);
+ field.set(testCase, null);
+ }
+ }
+ }
+
+ /**
+ * Workaround for a JUnit bug: JUnit does not configure the testing Thread
+ * with a context class loader. This should probably happen in
+ * TestRunner.doRunTest(Test), just before starting the thread.
+ */
+ public static void setUpJUnitThreadContextClassLoader() {
+ Thread.currentThread().setContextClassLoader(TestTools.class.getClassLoader());
+ }
+
+ private TestTools() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/XMLStringEncoderTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/XMLStringEncoderTests.java
new file mode 100644
index 0000000000..3b104b9b89
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/XMLStringEncoderTests.java
@@ -0,0 +1,135 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal;
+
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.FileTools;
+import org.eclipse.jpt.utility.internal.XMLStringEncoder;
+
+public class XMLStringEncoderTests extends TestCase {
+
+ public XMLStringEncoderTests(String name) {
+ super(name);
+ }
+
+ public void testEncodeNoCharacterSequences() {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+
+ String s = "foo";
+ assertEquals(s, encoder.encode(s));
+
+ s = "123foo123";
+ assertEquals(s, encoder.encode(s));
+ }
+
+ public void testEncodeCharacterSequences() {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+
+ String s = "?foo?";
+ String expected = "&#x3f;foo&#x3f;";
+ assertEquals(expected, encoder.encode(s));
+
+ s = "?foo&123";
+ expected = "&#x3f;foo&#x26;123";
+ assertEquals(expected, encoder.encode(s));
+ }
+
+ public void testDenormalizeValidFileName() {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+
+ String s = "foo";
+ assertEquals(s, encoder.decode(s));
+
+ s = "123foo123";
+ assertEquals(s, encoder.decode(s));
+ }
+
+ public void testDenormalizeInvalidFileName() {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+
+ String s = "&#x3f;foo&#x3f;";
+ String expected = "?foo?";
+ assertEquals(expected, encoder.decode(s));
+
+ s = "&#x3f;foo&#x26;123";
+ expected = "?foo&123";
+ assertEquals(expected, encoder.decode(s));
+ }
+
+ public void testRoundTripNoCharacterSequences() {
+ this.verifyRoundTrip("foo");
+ this.verifyRoundTrip("123foo456");
+ }
+
+ public void testRoundTripCharacterSequences() {
+ this.verifyRoundTrip("?foo?");
+ this.verifyRoundTrip("?foo&123&&&&&&>>>>");
+ }
+
+ private void verifyRoundTrip(String s) {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+ String actual = encoder.encode(s);
+ assertEquals(s, encoder.decode(actual));
+ }
+
+ public void testInvalidCharacterSequence1() {
+ this.verifyIllegalStateException("foo&");
+ }
+
+ public void testInvalidCharacterSequence2() {
+ this.verifyIllegalStateException("foo&#");
+ }
+
+ public void testInvalidCharacterSequence3() {
+ this.verifyIllegalStateException("foo&#x");
+ }
+
+ public void testInvalidCharacterSequence4() {
+ this.verifyIllegalStateException("foo&#x3");
+ }
+
+ public void testInvalidCharacterSequence5() {
+ this.verifyIllegalStateException("foo&#x;");
+ }
+
+ public void testInvalidCharacterSequence6() {
+ this.verifyIllegalStateException("foo&A");
+ }
+
+ public void testInvalidCharacterSequence7() {
+ this.verifyIllegalStateException("foo&#A");
+ }
+
+ private void verifyIllegalStateException(String s) {
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+ boolean exCaught = false;
+ try {
+ s = encoder.decode(s);
+ fail(s);
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInvalidCharacterSequence8() {
+ String s = "foo&#xZZZZ;";
+ XMLStringEncoder encoder = new XMLStringEncoder(FileTools.INVALID_FILENAME_CHARACTERS);
+ boolean exCaught = false;
+ try {
+ s = encoder.decode(s);
+ fail(s);
+ } catch (NumberFormatException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayIteratorTests.java
new file mode 100644
index 0000000000..d7cf76ad44
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayIteratorTests.java
@@ -0,0 +1,126 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
+
+public class ArrayIteratorTests extends TestCase {
+
+ public ArrayIteratorTests(String name) {
+ super(name);
+ }
+
+ public void testHasNext() {
+ int i = 0;
+ for (Iterator<String> stream = this.buildIterator(); stream.hasNext();) {
+ stream.next();
+ i++;
+ }
+ assertEquals(this.buildArray().length, i);
+ }
+
+ public void testNext() {
+ int i = 0;
+ for (Iterator<String> stream = this.buildIterator(); stream.hasNext();) {
+ assertEquals("bogus element", ++i, Integer.parseInt(stream.next()));
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ Iterator<String> stream = this.buildIterator();
+ String string = null;
+ while (stream.hasNext()) {
+ string = stream.next();
+ }
+ try {
+ string = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ public void testUnsupportedOperationException() {
+ boolean exCaught = false;
+ for (Iterator<String> stream = this.buildIterator(); stream.hasNext();) {
+ if (stream.next().equals("3")) {
+ try {
+ stream.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ public void testIllegalArgumentException() {
+ this.triggerIllegalArgumentException(-1, 1);
+ this.triggerIllegalArgumentException(8, 1);
+ this.triggerIllegalArgumentException(0, -1);
+ this.triggerIllegalArgumentException(0, 9);
+ }
+
+ public void testGenerics() {
+ Integer[] integers = new Integer[3];
+ integers[0] = 0;
+ integers[1] = 1;
+ integers[2] = 2;
+ int i = 0;
+ for (Iterator<Number> stream = new ArrayIterator<Number>(integers); stream.hasNext();) {
+ assertEquals(i++, stream.next().intValue());
+ }
+ assertEquals(integers.length, i);
+ }
+
+ public void testVarargs() {
+ int i = 0;
+ for (Iterator<Number> stream = new ArrayIterator<Number>(0, 1, 2); stream.hasNext();) {
+ assertEquals(i++, stream.next().intValue());
+ }
+ assertEquals(3, i);
+ }
+
+ public void triggerIllegalArgumentException(int start, int length) {
+ boolean exCaught = false;
+ Iterator<String> stream = null;
+ try {
+ stream = this.buildIterator(start, length);
+ } catch (IllegalArgumentException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalArgumentException not thrown: " + stream, exCaught);
+ }
+
+ Iterator<String> buildIterator() {
+ return this.buildIterator(this.buildArray());
+ }
+
+ Iterator<String> buildIterator(String[] array) {
+ return new ArrayIterator<String>(array);
+ }
+
+ Iterator<String> buildIterator(int start, int length) {
+ return this.buildIterator(this.buildArray(), start, length);
+ }
+
+ Iterator<String> buildIterator(String[] array, int start, int length) {
+ return new ArrayIterator<String>(array, start, length);
+ }
+
+ String[] buildArray() {
+ return new String[] { "1", "2", "3", "4", "5", "6", "7", "8" };
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayListIteratorTests.java
new file mode 100644
index 0000000000..9d82950b94
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayListIteratorTests.java
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.Iterator;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import org.eclipse.jpt.utility.internal.iterators.ArrayListIterator;
+
+public class ArrayListIteratorTests extends ArrayIteratorTests {
+
+ public ArrayListIteratorTests(String name) {
+ super(name);
+ }
+
+ public void testHasPrevious() {
+ ListIterator<String> stream = this.buildListIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ int i = 0;
+ while (stream.hasPrevious()) {
+ stream.previous();
+ i++;
+ }
+ assertEquals(this.buildArray().length, i);
+ }
+
+ public void testPrevious() {
+ ListIterator<String> stream = this.buildListIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ int i = this.buildArray().length;
+ while (stream.hasPrevious()) {
+ assertEquals("bogus element", i--, Integer.parseInt(stream.previous()));
+ }
+ }
+
+ public void testNextIndex() {
+ int i = 0;
+ ListIterator<String> stream = this.buildListIterator();
+ while (stream.hasNext()) {
+ assertEquals(i, stream.nextIndex());
+ stream.next();
+ i++;
+ }
+ assertEquals(i, stream.nextIndex());
+ }
+
+ public void testPreviousIndex() {
+ int i = 0;
+ ListIterator<String> stream = this.buildListIterator();
+ while (stream.hasNext()) {
+ assertEquals(i - 1, stream.previousIndex());
+ stream.next();
+ i++;
+ }
+ assertEquals(i - 1, stream.previousIndex());
+ }
+
+ @Override
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ ListIterator<String> stream = this.buildListIterator();
+ String string = null;
+ try {
+ string = stream.previous();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ public void testUnsupportedOperationExceptionAdd() {
+ boolean exCaught = false;
+ for (ListIterator<String> stream = this.buildListIterator(); stream.hasNext();) {
+ if (stream.next().equals("3")) {
+ try {
+ stream.add("3.5");
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ public void testUnsupportedOperationExceptionSet() {
+ boolean exCaught = false;
+ for (ListIterator<String> stream = this.buildListIterator(); stream.hasNext();) {
+ if (stream.next().equals("3")) {
+ try {
+ stream.set("three");
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ @Override
+ public void testGenerics() {
+ Integer[] integers = new Integer[3];
+ integers[0] = 0;
+ integers[1] = 1;
+ integers[2] = 2;
+ int i = 0;
+ for (Iterator<Number> stream = new ArrayListIterator<Number>(integers); stream.hasNext();) {
+ assertEquals(i++, stream.next().intValue());
+ }
+ assertEquals(integers.length, i);
+ }
+
+ @Override
+ public void testVarargs() {
+ int i = 0;
+ for (Iterator<Number> stream = new ArrayListIterator<Number>(0, 1, 2); stream.hasNext();) {
+ assertEquals(i++, stream.next().intValue());
+ }
+ assertEquals(3, i);
+ }
+
+ private ListIterator<String> buildListIterator() {
+ return this.buildListIterator(this.buildArray());
+ }
+
+ private ListIterator<String> buildListIterator(String[] array) {
+ return new ArrayListIterator<String>(array);
+ }
+
+ @Override
+ Iterator<String> buildIterator(String[] array) {
+ return new ArrayListIterator<String>(array);
+ }
+
+ @Override
+ Iterator<String> buildIterator(String[] array, int start, int length) {
+ return new ArrayListIterator<String>(array, start, length);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ChainIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ChainIteratorTests.java
new file mode 100644
index 0000000000..59cd3073b9
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ChainIteratorTests.java
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.AbstractCollection;
+import java.util.AbstractList;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Vector;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.ChainIterator;
+
+public class ChainIteratorTests extends TestCase {
+ private final static Class<?>[] VECTOR_HIERARCHY = { Vector.class, AbstractList.class, AbstractCollection.class, Object.class };
+
+ public ChainIteratorTests(String name) {
+ super(name);
+ }
+
+ public void testHasNext() {
+ int i = 0;
+ for (Iterator<Class<?>> stream = this.buildIterator(); stream.hasNext();) {
+ stream.next();
+ i++;
+ }
+ assertEquals(VECTOR_HIERARCHY.length, i);
+ }
+
+ public void testInnerHasNext() {
+ int i = 0;
+ for (Iterator<Class<?>> stream = this.buildInnerIterator(); stream.hasNext();) {
+ stream.next();
+ i++;
+ }
+ assertEquals(VECTOR_HIERARCHY.length, i);
+ }
+
+ public void testNext() {
+ int i = 0;
+ for (Iterator<Class<?>> stream = this.buildIterator(); stream.hasNext(); i++) {
+ assertEquals("bogus link", VECTOR_HIERARCHY[i], stream.next());
+ }
+ }
+
+ public void testInnerNext() {
+ int i = 0;
+ for (Iterator<Class<?>> stream = this.buildInnerIterator(); stream.hasNext(); i++) {
+ assertEquals("bogus link", VECTOR_HIERARCHY[i], stream.next());
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ Iterator<Class<?>> stream = this.buildIterator();
+ Class<?> javaClass = null;
+ while (stream.hasNext()) {
+ javaClass = stream.next();
+ }
+ try {
+ javaClass = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + javaClass, exCaught);
+ }
+
+ public void testUnsupportedOperationException() {
+ boolean exCaught = false;
+ for (Iterator<Class<?>> stream = this.buildIterator(); stream.hasNext();) {
+ if (stream.next() == AbstractCollection.class) {
+ try {
+ stream.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ private Iterator<Class<?>> buildIterator() {
+ return this.buildChainIterator(Vector.class, this.buildLinker());
+ }
+
+ private Iterator<Class<?>> buildInnerIterator() {
+ return this.buildInnerChainIterator(Vector.class);
+ }
+
+ private Iterator<Class<?>> buildChainIterator(Class<?> startLink, ChainIterator.Linker<Class<?>> linker) {
+ return new ChainIterator<Class<?>>(startLink, linker);
+ }
+
+ private ChainIterator.Linker<Class<?>> buildLinker() {
+ // chain up the class's hierarchy
+ return new ChainIterator.Linker<Class<?>>() {
+ public Class<?> nextLink(Class<?> currentLink) {
+ return currentLink.getSuperclass();
+ }
+ };
+ }
+
+ private Iterator<Class<?>> buildInnerChainIterator(Class<?> startLink) {
+ // chain up the class's hierarchy
+ return new ChainIterator<Class<?>>(startLink) {
+ @Override
+ protected Class<?> nextLink(Class<?> currentLink) {
+ return currentLink.getSuperclass();
+ }
+ };
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneIteratorTests.java
new file mode 100644
index 0000000000..e3afdb585f
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneIteratorTests.java
@@ -0,0 +1,252 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
+import org.eclipse.jpt.utility.tests.internal.TestTools;
+
+public class CloneIteratorTests extends TestCase {
+ Collection<String> originalCollection;
+
+ private boolean concurrentProblem;
+ private Collection<String> concurrentCollection;
+
+ public CloneIteratorTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.originalCollection = this.buildCollection();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+ public void testHasNext() {
+ int originalSize = this.originalCollection.size();
+ int i = 0;
+ for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
+ stream.next();
+ // should allow concurrent modification
+ this.originalCollection.add("foo");
+ i++;
+ }
+ assertTrue(originalSize != this.originalCollection.size());
+ assertEquals(originalSize, i);
+ }
+
+ public void testNext() {
+ Iterator<String> nestedIterator = this.originalCollection.iterator();
+ for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
+ assertEquals("bogus element", nestedIterator.next(), stream.next());
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ Iterator<String> stream = this.buildCloneIterator();
+ String string = null;
+ while (stream.hasNext()) {
+ string = stream.next();
+ }
+ try {
+ string = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ public void testRemoveDefault() {
+ boolean exCaught = false;
+ for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
+ if (stream.next().equals("three")) {
+ try {
+ stream.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ public void testRemoveEliminator() {
+ CloneIterator.Mutator<String> eliminator = new CloneIterator.Mutator<String>() {
+ public void remove(String current) {
+ CloneIteratorTests.this.originalCollection.remove(current);
+ }
+ };
+ this.verifyRemove(new CloneIterator<String>(this.originalCollection, eliminator));
+ }
+
+ public void testRemoveSubclass() {
+ this.verifyRemove(new CloneIterator<String>(this.originalCollection) {
+ @Override
+ protected void remove(String current) {
+ CloneIteratorTests.this.originalCollection.remove(current);
+ }
+ });
+ }
+
+ /**
+ * Test concurrent access: First build a clone iterator in a separate thread
+ * that hangs momentarily during its construction; then modify the shared
+ * collection in this thread. This would cause a
+ * ConcurrentModificationException in the other thread if the clone iterator
+ * were not synchronized on the original collection.
+ */
+ public void testConcurrentAccess() throws Exception {
+ SlowCollection<String> slow = new SlowCollection<String>();
+ this.populateCollection(slow);
+ // using the unsynchronized collection will cause the test to fail
+ // this.originalCollection = slow;
+ this.originalCollection = Collections.synchronizedCollection(slow);
+
+ this.concurrentProblem = false;
+ this.concurrentCollection = new ArrayList<String>();
+ Thread thread = new Thread(this.buildRunnable());
+ thread.start();
+ while (!slow.hasStartedClone()) {
+ // wait for the other thread to start the clone...
+ Thread.yield();
+ }
+ // ...then sneak in an extra element
+ this.originalCollection.add("seventeen");
+ while (thread.isAlive()) {
+ // wait for the other thread to finish
+ Thread.yield();
+ }
+ assertFalse(this.concurrentProblem);
+ Collection<String> expected = new ArrayList<String>();
+ this.populateCollection(expected);
+ assertEquals(expected, this.concurrentCollection);
+ }
+
+ private Runnable buildRunnable() {
+ return new Runnable() {
+ public void run() {
+ CloneIteratorTests.this.loopWithCloneIterator();
+ }
+ };
+ }
+
+ /**
+ * use a clone iterator to loop over the "slow" collection and copy its
+ * contents to the concurrent collection
+ */
+ void loopWithCloneIterator() {
+ try {
+ for (Iterator<String> stream = this.buildCloneIterator(); stream.hasNext();) {
+ this.concurrentCollection.add(stream.next());
+ }
+ } catch (Throwable t) {
+ this.concurrentProblem = true;
+ }
+ }
+
+ private void verifyRemove(Iterator<String> iterator) {
+ Object removed = "three";
+ assertTrue(this.originalCollection.contains(removed));
+ // try to remove before calling #next()
+ boolean exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ while (iterator.hasNext()) {
+ if (iterator.next().equals(removed)) {
+ iterator.remove();
+ // try to remove twice
+ exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+ }
+ assertFalse(this.originalCollection.contains(removed));
+ }
+
+ private Iterator<String> buildCloneIterator() {
+ return this.buildCloneIterator(this.originalCollection);
+ }
+
+ private Iterator<String> buildCloneIterator(Collection<String> c) {
+ return new CloneIterator<String>(c);
+ }
+
+ private Collection<String> buildCollection() {
+ Collection<String> c = this.buildEmptyCollection();
+ this.populateCollection(c);
+ return c;
+ }
+
+ protected Collection<String> buildEmptyCollection() {
+ return new ArrayList<String>();
+ }
+
+ private void populateCollection(Collection<String> c) {
+ c.add("one");
+ c.add("two");
+ c.add("three");
+ c.add("four");
+ c.add("five");
+ c.add("six");
+ c.add("seven");
+ c.add("eight");
+ }
+
+ // ********** custom collection **********
+ static class SlowCollection<E> extends ArrayList<E> {
+ private static final long serialVersionUID = 1L;
+ private boolean hasStartedClone = false;
+
+ public SlowCollection() {
+ super();
+ }
+
+ @Override
+ public Object[] toArray() {
+ this.setHasStartedClone(true);
+ // take a little snooze before returning the array
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ return super.toArray();
+ }
+
+ synchronized void setHasStartedClone(boolean hasStartedClone) {
+ this.hasStartedClone = hasStartedClone;
+ }
+
+ synchronized boolean hasStartedClone() {
+ return this.hasStartedClone;
+ }
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneListIteratorTests.java
new file mode 100644
index 0000000000..c6045a9f12
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneListIteratorTests.java
@@ -0,0 +1,408 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.CloneListIterator;
+import org.eclipse.jpt.utility.tests.internal.TestTools;
+
+public class CloneListIteratorTests extends TestCase {
+ List<String> originalList;
+
+ private boolean concurrentProblem;
+ private List<String> concurrentList;
+
+ public CloneListIteratorTests(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.originalList = this.buildList();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+ public void testHasNext() {
+ int originalSize = this.originalList.size();
+ int i = 0;
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ stream.next();
+ // should allow concurrent modification
+ this.originalList.add("foo");
+ i++;
+ }
+ assertTrue(originalSize != this.originalList.size());
+ assertEquals(originalSize, i);
+ }
+
+ public void testNext() {
+ ListIterator<String> nestedListIterator = this.buildNestedListIterator();
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ assertEquals("bogus element", nestedListIterator.next(), stream.next());
+ }
+ }
+
+ public void testIndex() {
+ ListIterator<String> cloneListIterator = this.buildCloneListIterator();
+ ListIterator<String> nestedListIterator = this.buildNestedListIterator();
+ for (int i = 0; i < 7; i++) {
+ nestedListIterator.next();
+ cloneListIterator.next();
+ assertEquals("bogus index", nestedListIterator.nextIndex(), cloneListIterator.nextIndex());
+ assertEquals("bogus index", nestedListIterator.previousIndex(), cloneListIterator.previousIndex());
+ }
+
+ for (int i = 0; i < 3; i++) {
+ nestedListIterator.previous();
+ cloneListIterator.previous();
+ assertEquals("bogus index", nestedListIterator.nextIndex(), cloneListIterator.nextIndex());
+ assertEquals("bogus index", nestedListIterator.previousIndex(), cloneListIterator.previousIndex());
+ }
+
+ while (nestedListIterator.hasNext()) {
+ nestedListIterator.next();
+ cloneListIterator.next();
+ assertEquals("bogus index", nestedListIterator.nextIndex(), cloneListIterator.nextIndex());
+ assertEquals("bogus index", nestedListIterator.previousIndex(), cloneListIterator.previousIndex());
+ }
+ }
+
+ public void testHasPrevious() {
+ int originalSize = this.originalList.size();
+ int i = 0;
+ ListIterator<String> stream = this.buildCloneListIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ this.originalList.add("foo");
+ i++;
+ }
+ assertTrue(originalSize != this.originalList.size());
+ originalSize = this.originalList.size();
+ while (stream.hasPrevious()) {
+ stream.previous();
+ // should allow concurrent modification
+ this.originalList.add("bar");
+ i--;
+ }
+ assertTrue(originalSize != this.originalList.size());
+ assertEquals(0, i);
+ }
+
+ public void testPrevious() {
+ ListIterator<String> nestedListIterator = this.buildNestedListIterator();
+ ListIterator<String> stream = this.buildCloneListIterator();
+ while (stream.hasNext()) {
+ nestedListIterator.next();
+ stream.next();
+ }
+ while (stream.hasPrevious()) {
+ assertEquals("bogus element", nestedListIterator.previous(), stream.previous());
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ ListIterator<String> stream = this.buildCloneListIterator();
+ String string = null;
+ while (stream.hasNext()) {
+ string = stream.next();
+ }
+ try {
+ string = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+
+ exCaught = false;
+ while (stream.hasPrevious()) {
+ string = stream.previous();
+ }
+ try {
+ string = stream.previous();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ public void testModifyDefault() {
+ boolean exCaught = false;
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ if (stream.next().equals("three")) {
+ try {
+ stream.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+
+ exCaught = false;
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ if (stream.next().equals("three")) {
+ try {
+ stream.add("three and a half");
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+
+ exCaught = false;
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ if (stream.next().equals("three")) {
+ try {
+ stream.set("another three");
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ public void testModifyMutatorNext() {
+ this.verifyModifyNext(new CloneListIterator<String>(this.originalList, this.buildMutator()));
+ }
+
+ public void testModifyMutatorPrevious() {
+ this.verifyModifyPrevious(new CloneListIterator<String>(this.originalList, this.buildMutator()));
+ }
+
+ private CloneListIterator.Mutator<String> buildMutator() {
+ return new CloneListIterator.Mutator<String>() {
+ public void add(int index, String o) {
+ CloneListIteratorTests.this.originalList.add(index, o);
+ }
+
+ public void remove(int index) {
+ CloneListIteratorTests.this.originalList.remove(index);
+ }
+
+ public void set(int index, String o) {
+ CloneListIteratorTests.this.originalList.set(index, o);
+ }
+ };
+ }
+
+ public void testModifySubclassNext() {
+ this.verifyModifyNext(this.buildSubclass());
+ }
+
+ public void testModifySubclassPrevious() {
+ this.verifyModifyPrevious(this.buildSubclass());
+ }
+
+ private ListIterator<String> buildSubclass() {
+ return new CloneListIterator<String>(this.originalList) {
+ @Override
+ protected void add(int currentIndex, String o) {
+ CloneListIteratorTests.this.originalList.add(currentIndex, o);
+ }
+
+ @Override
+ protected void remove(int currentIndex) {
+ CloneListIteratorTests.this.originalList.remove(currentIndex);
+ }
+
+ @Override
+ protected void set(int currentIndex, String o) {
+ CloneListIteratorTests.this.originalList.set(currentIndex, o);
+ }
+ };
+ }
+
+ private void verifyModifyNext(ListIterator<String> iterator) {
+ String removed = "three";
+ String addedAfter = "five";
+ String added = "five and a half";
+ String replaced = "seven";
+ String replacement = "another seven";
+ assertTrue(this.originalList.contains(removed));
+ assertTrue(this.originalList.contains(addedAfter));
+ assertTrue(this.originalList.contains(replaced));
+ // try to remove before calling #next()
+ boolean exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ while (iterator.hasNext()) {
+ String next = iterator.next();
+ if (next.equals(addedAfter)) {
+ iterator.add(added);
+ }
+ if (next.equals(removed)) {
+ iterator.remove();
+ // try to remove twice
+ exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+ if (next.equals(replaced)) {
+ iterator.set(replacement);
+ }
+ }
+ assertTrue(this.originalList.contains(added));
+ assertFalse(this.originalList.contains(removed));
+ assertFalse(this.originalList.contains(replaced));
+ assertTrue(this.originalList.contains(replacement));
+ }
+
+ private void verifyModifyPrevious(ListIterator<String> iterator) {
+ String removed = "three";
+ String addedBefore = "five";
+ String added = "four and a half";
+ String replaced = "seven";
+ String replacement = "another seven";
+ assertTrue(this.originalList.contains(removed));
+ assertTrue(this.originalList.contains(addedBefore));
+ assertTrue(this.originalList.contains(replaced));
+ while (iterator.hasNext()) {
+ iterator.next();
+ }
+ while (iterator.hasPrevious()) {
+ Object previous = iterator.previous();
+ if (previous.equals(addedBefore)) {
+ iterator.add(added);
+ }
+ if (previous.equals(removed)) {
+ iterator.remove();
+ // try to remove twice
+ boolean exCaught = false;
+ try {
+ iterator.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+ if (previous.equals(replaced)) {
+ iterator.set(replacement);
+ }
+ }
+ assertTrue(this.originalList.contains(added));
+ assertFalse(this.originalList.contains(removed));
+ assertFalse(this.originalList.contains(replaced));
+ assertTrue(this.originalList.contains(replacement));
+ }
+
+ private ListIterator<String> buildCloneListIterator() {
+ return this.buildCloneListIterator(this.originalList);
+ }
+
+ private ListIterator<String> buildCloneListIterator(List<String> list) {
+ return new CloneListIterator<String>(list);
+ }
+
+ private ListIterator<String> buildNestedListIterator() {
+ return this.originalList.listIterator();
+ }
+
+ private List<String> buildList() {
+ List<String> list = this.buildEmptyList();
+ this.populateList(list);
+ return list;
+ }
+
+ private void populateList(List<String> list) {
+ list.add("zero");
+ list.add("one");
+ list.add("two");
+ list.add("three");
+ list.add("four");
+ list.add("five");
+ list.add("six");
+ list.add("seven");
+ list.add("eight");
+ list.add("nine");
+ }
+
+ protected List<String> buildEmptyList() {
+ return new ArrayList<String>();
+ }
+
+ /**
+ * Test concurrent access: First build a clone iterator in a separate thread
+ * that hangs momentarily during its construction; then modify the shared
+ * collection in this thread. This would cause a
+ * ConcurrentModificationException in the other thread if the clone iterator
+ * were not synchronized on the original collection.
+ */
+ public void testConcurrentAccess() throws Exception {
+ CloneIteratorTests.SlowCollection<String> slow = new CloneIteratorTests.SlowCollection<String>();
+ this.populateList(slow);
+ // using the unsynchronized list will cause the test to fail
+ // this.originalList = slow;
+ this.originalList = Collections.synchronizedList(slow);
+
+ this.concurrentProblem = false;
+ this.concurrentList = new ArrayList<String>();
+ Thread thread = new Thread(this.buildRunnable());
+ thread.start();
+ while (!slow.hasStartedClone()) {
+ // wait for the other thread to start the clone...
+ Thread.yield();
+ }
+ // ...then sneak in an extra element
+ this.originalList.add("seventeen");
+ while (thread.isAlive()) {
+ // wait for the other thread to finish
+ Thread.yield();
+ }
+ assertFalse(this.concurrentProblem);
+ List<String> expected = new ArrayList<String>();
+ this.populateList(expected);
+ assertEquals(expected, this.concurrentList);
+ }
+
+ private Runnable buildRunnable() {
+ return new Runnable() {
+ public void run() {
+ CloneListIteratorTests.this.loopWithCloneListIterator();
+ }
+ };
+ }
+
+ /**
+ * use a clone iterator to loop over the "slow" collection and copy its
+ * contents to the concurrent collection
+ */
+ void loopWithCloneListIterator() {
+ try {
+ for (ListIterator<String> stream = this.buildCloneListIterator(); stream.hasNext();) {
+ this.concurrentList.add(stream.next());
+ }
+ } catch (Throwable t) {
+ this.concurrentProblem = true;
+ }
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeIteratorTests.java
new file mode 100644
index 0000000000..69239c1195
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeIteratorTests.java
@@ -0,0 +1,333 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
+
+public class CompositeIteratorTests extends TestCase {
+
+ public CompositeIteratorTests(String name) {
+ super(name);
+ }
+
+ public void testHasAnother() {
+ this.verifyHasAnother(this.buildCompositeIterator());
+ }
+
+ public void testHasAnother2() {
+ this.verifyHasAnother(this.buildCompositeIterator2());
+ }
+
+ public void testHasAnother3() {
+ this.verifyHasAnother(this.buildCompositeIterator3());
+ }
+
+ void verifyHasAnother(Iterator<String> stream) {
+ this.verifyHasAnother(8, stream);
+ }
+
+ void verifyHasAnother(int expected, Iterator<String> stream) {
+ int i = 0;
+ while (stream.hasNext()) {
+ stream.next();
+ i++;
+ }
+ assertEquals(expected, i);
+ }
+
+ public void testAnother() {
+ this.verifyAnother(this.buildCompositeIterator());
+ }
+
+ public void testAnother2() {
+ this.verifyAnother(this.buildCompositeIterator2());
+ }
+
+ public void testAnother3() {
+ this.verifyAnother(this.buildCompositeIterator3());
+ }
+
+ void verifyAnother(Iterator<String> stream) {
+ this.verifyAnother(1, stream);
+ }
+
+ void verifyAnother(int start, Iterator<String> stream) {
+ int index = start;
+ while (stream.hasNext()) {
+ assertEquals("bogus element", String.valueOf(index++), stream.next().substring(0, 1));
+ }
+ }
+
+ public void testRemove() {
+ List<String> list1 = this.buildList1();
+ Object lastElement1 = list1.get(list1.size() - 1);
+ List<String> list2 = this.buildList2();
+ List<String> list3 = this.buildList3();
+
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(list1.listIterator());
+ list.add(list2.listIterator());
+ list.add(list3.listIterator());
+
+ Iterator<String> stream = this.buildCompositeIterator(list.listIterator());
+ while (stream.hasNext()) {
+ Object next = stream.next();
+ if (next.equals("333")) {
+ stream.remove();
+ }
+ // test special case - where we are between iterators
+ if (next.equals(lastElement1)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasNext();
+ // now try to remove from the previous iterator
+ stream.remove();
+ }
+ }
+ stream.remove();
+
+ assertEquals("nothing removed from collection 1", this.buildList1().size() - 2, list1.size());
+ assertFalse("element still in collection 1", list1.contains("333"));
+ assertFalse("last element still in collection 1", list1.contains(lastElement1));
+ assertTrue("wrong element removed from collection 1", list1.contains("22"));
+
+ assertEquals("nothing removed from collection 3", this.buildList3().size() - 1, list3.size());
+ assertFalse("element still in collection 3", list3.contains("88888888"));
+ assertTrue("wrong element removed from collection 3", list3.contains("666666"));
+ }
+
+ public void testSingleElement() {
+ String item = "0";
+ this.verifyHasAnother(9, this.buildCompositeIterator(item, this.buildCompositeIterator()));
+ this.verifyAnother(0, this.buildCompositeIterator(item, this.buildCompositeIterator()));
+ }
+
+ public void testNoSuchElementException() {
+ this.verifyNoSuchElementException(this.buildCompositeIterator());
+ }
+
+ void verifyNoSuchElementException(Iterator<String> stream) {
+ boolean exCaught = false;
+ String string = null;
+ while (stream.hasNext()) {
+ string = stream.next();
+ }
+ try {
+ string = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ public void testUnsupportedOperationException() {
+ this.verifyUnsupportedOperationException(this.buildUnmodifiableCompositeIterator());
+ }
+
+ void verifyUnsupportedOperationException(Iterator<String> stream) {
+ boolean exCaught = false;
+ while (stream.hasNext()) {
+ Object string = stream.next();
+ if (string.equals("333")) {
+ try {
+ stream.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ public void testIllegalStateException() {
+ this.verifyIllegalStateException(this.buildCompositeIterator());
+ }
+
+ void verifyIllegalStateException(Iterator<String> stream) {
+ boolean exCaught = false;
+ try {
+ stream.remove();
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+
+ public void testEmptyHasAnother1() {
+ this.verifyEmptyHasAnother(this.buildEmptyCompositeIterator1());
+ }
+
+ void verifyEmptyHasAnother(Iterator<String> stream) {
+ int i = 0;
+ while (stream.hasNext()) {
+ stream.next();
+ i++;
+ }
+ assertEquals(0, i);
+ }
+
+ public void testEmptyNoSuchElementException1() {
+ this.verifyNoSuchElementException(this.buildEmptyCompositeIterator1());
+ }
+
+ public void testEmptyIllegalStateException1() {
+ this.verifyIllegalStateException(this.buildEmptyCompositeIterator1());
+ }
+
+ public void testEmptyHasAnother2() {
+ this.verifyEmptyHasAnother(this.buildEmptyCompositeIterator2());
+ }
+
+ public void testEmptyNoSuchElementException2() {
+ this.verifyNoSuchElementException(this.buildEmptyCompositeIterator2());
+ }
+
+ public void testEmptyIllegalStateException2() {
+ this.verifyIllegalStateException(this.buildEmptyCompositeIterator2());
+ }
+
+ Iterator<String> buildCompositeIterator() {
+ return this.buildCompositeIterator(this.buildIterators());
+ }
+
+ Iterator<String> buildEmptyCompositeIterator1() {
+ return this.buildCompositeIterator(this.buildEmptyIterators1());
+ }
+
+ Iterator<String> buildEmptyCompositeIterator2() {
+ return this.buildCompositeIterator(this.buildEmptyIterators2());
+ }
+
+ Iterator<String> buildUnmodifiableCompositeIterator() {
+ return this.buildCompositeIterator(this.buildUnmodifiableIterators());
+ }
+
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator(Iterator iterators) {
+ return new CompositeIterator<String>(iterators);
+ }
+
+ // use vararg constructor
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator2() {
+ return new CompositeIterator<String>(this.buildIterator1(), this.buildIterator2(), this.buildIterator3());
+ }
+
+ // use vararg constructor
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator3() {
+ return new CompositeIterator<String>(new Iterator[] { this.buildIterator1(), this.buildIterator2(), this.buildIterator3() });
+ }
+
+ Iterator<String> buildCompositeIterator(String string, Iterator<String> iterator) {
+ return new CompositeIterator<String>(string, iterator);
+ }
+
+ ListIterator<Iterator<String>> buildIterators() {
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(this.buildIterator1());
+ list.add(this.buildIterator2());
+ list.add(this.buildIterator3());
+ return list.listIterator();
+ }
+
+ ListIterator<Iterator<String>> buildEmptyIterators1() {
+ return this.buildEmptyIteratorIterator();
+ }
+
+ ListIterator<Iterator<String>> buildEmptyIterators2() {
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(this.buildEmptyStringIterator());
+ list.add(this.buildEmptyStringIterator());
+ list.add(this.buildEmptyStringIterator());
+ return list.listIterator();
+ }
+
+ ListIterator<Iterator<String>> buildUnmodifiableIterators() {
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(this.buildUnmodifiableIterator1());
+ list.add(this.buildUnmodifiableIterator2());
+ list.add(this.buildUnmodifiableIterator3());
+ return list.listIterator();
+ }
+
+ ListIterator<String> buildIterator1() {
+ return this.buildList1().listIterator();
+ }
+
+ ListIterator<String> buildIterator2() {
+ return this.buildList2().listIterator();
+ }
+
+ ListIterator<String> buildIterator3() {
+ return this.buildList3().listIterator();
+ }
+
+ ListIterator<String> buildUnmodifiableIterator1() {
+ return this.buildUnmodifiableList1().listIterator();
+ }
+
+ ListIterator<String> buildUnmodifiableIterator2() {
+ return this.buildUnmodifiableList2().listIterator();
+ }
+
+ ListIterator<String> buildUnmodifiableIterator3() {
+ return this.buildUnmodifiableList3().listIterator();
+ }
+
+ ListIterator<Iterator<String>> buildEmptyIteratorIterator() {
+ return (new ArrayList<Iterator<String>>()).listIterator();
+ }
+
+ ListIterator<String> buildEmptyStringIterator() {
+ return (new ArrayList<String>()).listIterator();
+ }
+
+ List<String> buildList1() {
+ List<String> list = new ArrayList<String>();
+ list.add("1");
+ list.add("22");
+ list.add("333");
+ list.add("4444");
+ return list;
+ }
+
+ List<String> buildList2() {
+ return new ArrayList<String>();
+ }
+
+ List<String> buildList3() {
+ List<String> list = new ArrayList<String>();
+ list.add("55555");
+ list.add("666666");
+ list.add("7777777");
+ list.add("88888888");
+ return list;
+ }
+
+ List<String> buildUnmodifiableList1() {
+ return Collections.unmodifiableList(this.buildList1());
+ }
+
+ List<String> buildUnmodifiableList2() {
+ return Collections.unmodifiableList(this.buildList2());
+ }
+
+ List<String> buildUnmodifiableList3() {
+ return Collections.unmodifiableList(this.buildList3());
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeListIteratorTests.java
new file mode 100644
index 0000000000..6486c33ebe
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeListIteratorTests.java
@@ -0,0 +1,403 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
+import org.eclipse.jpt.utility.internal.iterators.CompositeListIterator;
+
+public class CompositeListIteratorTests extends CompositeIteratorTests {
+
+ public CompositeListIteratorTests(String name) {
+ super(name);
+ }
+
+ @Override
+ void verifyHasAnother(Iterator<String> stream) {
+ super.verifyHasAnother(stream);
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ int i = 0;
+ while (stream2.hasPrevious()) {
+ stream2.previous();
+ i++;
+ }
+ assertEquals(8, i);
+ }
+
+ @Override
+ void verifyAnother(Iterator<String> stream) {
+ super.verifyAnother(stream);
+ int i = 8;
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ while (stream2.hasPrevious()) {
+ assertEquals("bogus element", String.valueOf(i--), stream2.previous().substring(0, 1));
+ }
+ }
+
+ @Override
+ public void testRemove() {
+ super.testRemove();
+ List<String> list1 = this.buildList1();
+ List<String> list2 = this.buildList2();
+ List<String> list3 = this.buildList3();
+ Object firstElement3 = list3.get(0);
+
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(list1.listIterator());
+ list.add(list2.listIterator());
+ list.add(list3.listIterator());
+
+ ListIterator<String> stream = (ListIterator<String>) this.buildCompositeIterator(list.listIterator());
+ // position to end of stream
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ while (stream.hasPrevious()) {
+ Object previous = stream.previous();
+ if (previous.equals("333")) {
+ stream.remove();
+ }
+ // test special case - where we are between iterators
+ if (previous.equals(firstElement3)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasPrevious();
+ // now try to remove from the previous iterator
+ stream.remove();
+ }
+ }
+ stream.remove();
+
+ assertEquals("nothing removed from collection 1", this.buildList1().size() - 2, list1.size());
+ assertFalse("element still in collection 1", list1.contains("1"));
+ assertFalse("element still in collection 1", list1.contains("333"));
+
+ assertEquals("nothing removed from collection 3", this.buildList3().size() - 1, list3.size());
+ assertFalse("first element still in collection 3", list3.contains(firstElement3));
+ assertTrue("wrong element removed from collection 3", list3.contains("666666"));
+ }
+
+ public void testAdd() {
+ List<String> list1 = this.buildList1();
+ Object lastElement1 = list1.get(list1.size() - 1);
+ List<String> list2 = this.buildList2();
+ List<String> list3 = this.buildList3();
+ Object firstElement3 = list3.get(0);
+
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(list1.listIterator());
+ list.add(list2.listIterator());
+ list.add(list3.listIterator());
+
+ ListIterator<String> stream = (ListIterator<String>) this.buildCompositeIterator(list.listIterator());
+ while (stream.hasNext()) {
+ Object next = stream.next();
+ if (next.equals("333")) {
+ stream.add("3.5");
+ }
+ // test special case - where we are between iterators
+ if (next.equals(lastElement1)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasNext();
+ // now try to add to the iterator
+ stream.add("something in 3");
+ }
+ }
+ stream.add("finale");
+ boolean checkForFinale = true;
+ while (stream.hasPrevious()) {
+ Object previous = stream.previous();
+ if (checkForFinale) {
+ checkForFinale = false;
+ assertEquals("added element dropped", "finale", previous);
+ }
+ if (previous.equals("333")) {
+ stream.add("2.5");
+ }
+ // test special case - where we are between iterators
+ if (previous.equals(firstElement3)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasPrevious();
+ // now try to remove from the previous iterator
+ stream.add("old start of 3");
+ }
+ }
+ stream.add("prelude");
+ assertEquals("added element dropped", "prelude", stream.previous());
+
+ assertEquals("elements not added to collection 1", this.buildList1().size() + 3, list1.size());
+ assertEquals("element not added to collection 1", "prelude", list1.get(0));
+ assertEquals("element not added to collection 1", "2.5", list1.get(3));
+ assertEquals("element not added to collection 1", "3.5", list1.get(5));
+
+ assertEquals("elements not added to collection 3", this.buildList3().size() + 3, list3.size());
+ assertEquals("element not added to collection 3", "something in 3", list3.get(0));
+ assertEquals("element not added to collection 3", "old start of 3", list3.get(1));
+ assertEquals("element not added to collection 3", "finale", list3.get(list3.size() - 1));
+
+ // add to the front
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ stream.add("blah");
+ assertFalse("added element should be placed BEFORE the \"cursor\"", stream.next().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ stream.add("blah");
+ assertTrue("added element should be placed BEFORE the \"cursor\"", stream.previous().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ while (stream.hasPrevious()) {
+ stream.previous();
+ }
+ stream.add("blah");
+ assertFalse("added element should be placed BEFORE the \"cursor\"", stream.next().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ while (stream.hasPrevious()) {
+ stream.previous();
+ }
+ stream.add("blah");
+ assertTrue("added element should be placed BEFORE the \"cursor\"", stream.previous().equals("blah"));
+
+ // add to the middle
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ stream.next();
+ stream.add("blah");
+ assertFalse("added element should be placed BEFORE the \"cursor\"", stream.next().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ stream.next();
+ stream.add("blah");
+ assertTrue("added element should be placed BEFORE the \"cursor\"", stream.previous().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ stream.previous();
+ stream.add("blah");
+ assertFalse("added element should be placed BEFORE the \"cursor\"", stream.next().equals("blah"));
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ stream.previous();
+ stream.add("blah");
+ assertTrue("added element should be placed BEFORE the \"cursor\"", stream.previous().equals("blah"));
+
+ // add to the end
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ stream.add("blah");
+ assertFalse("added element should be placed BEFORE the \"cursor\"", stream.hasNext());
+
+ stream = (ListIterator<String>) this.buildCompositeIterator();
+ while (stream.hasNext()) {
+ stream.next();
+ }
+ stream.add("blah");
+ assertTrue("added element should be placed BEFORE the \"cursor\"", stream.previous().equals("blah"));
+ }
+
+ public void testSet() {
+ List<String> list1 = this.buildList1();
+ Object lastElement1 = list1.get(list1.size() - 1);
+ List<String> list2 = this.buildList2();
+ List<String> list3 = this.buildList3();
+ Object firstElement3 = list3.get(0);
+
+ List<Iterator<String>> list = new ArrayList<Iterator<String>>();
+ list.add(list1.listIterator());
+ list.add(list2.listIterator());
+ list.add(list3.listIterator());
+
+ ListIterator<String> stream = (ListIterator<String>) this.buildCompositeIterator(list.listIterator());
+ // position to end of stream
+ while (stream.hasNext()) {
+ Object next = stream.next();
+ if (next.equals("333")) {
+ stream.set("333a");
+ }
+ // test special case - where we are between iterators
+ if (next.equals(lastElement1)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasNext();
+ // now try to remove from the previous iterator
+ stream.set("end of 1");
+ }
+ }
+ while (stream.hasPrevious()) {
+ Object previous = stream.previous();
+ if (previous.equals("22")) {
+ stream.set("22a");
+ }
+ // test special case - where we are between iterators
+ if (previous.equals(firstElement3)) {
+ // this will trigger the next iterator to be loaded
+ stream.hasPrevious();
+ // now try to remove from the previous iterator
+ stream.set("start of 3");
+ }
+ }
+
+ assertEquals("element(s) added to collection 1", this.buildList1().size(), list1.size());
+ assertEquals("element not set in collection 1", "22a", list1.get(1));
+ assertFalse("element not set in collection 1", list1.contains("22"));
+ assertEquals("element not set in collection 1", "333a", list1.get(2));
+ assertFalse("element not set in collection 1", list1.contains("333"));
+ assertEquals("element not set in collection 1", "end of 1", list1.get(list1.size() - 1));
+ assertFalse("element not set in collection 1", list1.contains(lastElement1));
+
+ assertEquals("element(s) added to collection 3", this.buildList3().size(), list3.size());
+ assertEquals("element not set in collection 3", "start of 3", list3.get(0));
+ assertFalse("element not set in collection 3", list3.contains(firstElement3));
+ }
+
+ public void testNextIndexPreviousIndex() {
+ int i = 0;
+ ListIterator<String> stream = (ListIterator<String>) this.buildCompositeIterator();
+ assertEquals(i, stream.nextIndex());
+ assertEquals(i - 1, stream.previousIndex());
+ while (stream.hasNext()) {
+ Object next = stream.next();
+ i++;
+ if (next.equals("333")) {
+ stream.remove();
+ i--;
+ }
+ if (next.equals("7777777")) {
+ stream.add("7.5");
+ i++;
+ }
+ assertEquals(i, stream.nextIndex());
+ assertEquals(i - 1, stream.previousIndex());
+ }
+ assertEquals("index is corrupt", 8, i);
+
+ assertEquals(i, stream.nextIndex());
+ assertEquals(i - 1, stream.previousIndex());
+ while (stream.hasPrevious()) {
+ Object previous = stream.previous();
+ i--;
+ if (previous.equals("666666")) {
+ stream.remove();
+ // removing a previous element, does not change the cursor
+ }
+ if (previous.equals("22")) {
+ stream.add("1.5");
+ i++;
+ }
+ assertEquals(i, stream.nextIndex());
+ assertEquals(i - 1, stream.previousIndex());
+ }
+ assertEquals("index is corrupt", 0, i);
+ }
+
+ public void testPreviousIndex() {
+ // TODO
+ }
+
+ @Override
+ void verifyNoSuchElementException(Iterator<String> stream) {
+ super.verifyNoSuchElementException(stream);
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ boolean exCaught = false;
+ String string = null;
+ while (stream2.hasPrevious()) {
+ string = stream2.previous();
+ }
+ try {
+ string = stream2.previous();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + string, exCaught);
+ }
+
+ @Override
+ void verifyUnsupportedOperationException(Iterator<String> stream) {
+ super.verifyUnsupportedOperationException(stream);
+ boolean exCaught = false;
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ while (stream2.hasPrevious()) {
+ Object string = stream2.previous();
+ if (string.equals("333")) {
+ try {
+ stream2.remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+ @Override
+ void verifyIllegalStateException(Iterator<String> stream) {
+ super.verifyIllegalStateException(stream);
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ boolean exCaught = false;
+ try {
+ stream2.set("junk");
+ } catch (IllegalStateException ex) {
+ exCaught = true;
+ }
+ assertTrue("IllegalStateException not thrown", exCaught);
+ }
+
+ @Override
+ void verifyEmptyHasAnother(Iterator<String> stream) {
+ super.verifyEmptyHasAnother(stream);
+ ListIterator<String> stream2 = (ListIterator<String>) stream;
+ int i = 0;
+ while (stream2.hasPrevious()) {
+ stream2.previous();
+ i++;
+ }
+ assertEquals(0, i);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator(Iterator iterators) {
+ return new CompositeListIterator<String>((ListIterator<ListIterator<String>>) iterators);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator2() {
+ return new CompositeListIterator<String>(this.buildIterator1(), this.buildIterator2(), this.buildIterator3());
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ Iterator<String> buildCompositeIterator3() {
+ return new CompositeListIterator<String>(new ListIterator[] { this.buildIterator1(), this.buildIterator2(), this.buildIterator3() });
+ }
+
+ Iterator<String> buildCompositeIterator(String string, ListIterator<String> iterator) {
+ return this.buildCompositeListIterator(string, iterator);
+ }
+
+ ListIterator<String> buildCompositeListIterator(String string, ListIterator<String> iterator) {
+ return new CompositeListIterator<String>(string, iterator);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyEnumerationTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyEnumerationTests.java
new file mode 100644
index 0000000000..98697897b7
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyEnumerationTests.java
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.Enumeration;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.EmptyEnumeration;
+
+public class EmptyEnumerationTests extends TestCase {
+
+ public EmptyEnumerationTests(String name) {
+ super(name);
+ }
+
+ public void testHasMoreElements() {
+ int i = 0;
+ for (Enumeration<Object> stream = EmptyEnumeration.instance(); stream.hasMoreElements();) {
+ stream.nextElement();
+ i++;
+ }
+ assertEquals(0, i);
+ }
+
+ public void testNextElement() {
+ for (Enumeration<Object> stream = EmptyEnumeration.instance(); stream.hasMoreElements();) {
+ fail("bogus element: " + stream.nextElement());
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ Enumeration<Object> stream = EmptyEnumeration.instance();
+ Object element = null;
+ while (stream.hasMoreElements()) {
+ element = stream.nextElement();
+ }
+ try {
+ element = stream.nextElement();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + element, exCaught);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyIteratorTests.java
new file mode 100644
index 0000000000..6985ad4ffa
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyIteratorTests.java
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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.utility.tests.internal.iterators;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.iterators.EmptyIterator;
+
+public class EmptyIteratorTests extends TestCase {
+
+ public EmptyIteratorTests(String name) {
+ super(name);
+ }
+
+ public void testHasNext() {
+ int i = 0;
+ for (Iterator<Object> stream = EmptyIterator.instance(); stream.hasNext();) {
+ stream.next();
+ i++;
+ }
+ assertEquals(0, i);
+ }
+
+ public void testNext() {
+ for (Iterator<String> stream = EmptyIterator.instance(); stream.hasNext();) {
+ fail("bogus element: " + stream.next());
+ }
+ }
+
+ public void testNoSuchElementException() {
+ boolean exCaught = false;
+ Iterator<Number> stream = EmptyIterator.instance();
+ Object element = null;
+ while (stream.hasNext()) {
+ element = stream.next();
+ }
+ try {
+ element = stream.next();
+ } catch (NoSuchElementException ex) {
+ exCaught = true;
+ }
+ assertTrue("NoSuchElementException not thrown: " + element, exCaught);
+ }
+
+ public void testUnsupportedOperationException() {
+ boolean exCaught = false;
+ try {
+ EmptyIterator.instance().remove();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ assertTrue("UnsupportedOperationException not thrown", exCaught);
+ }
+
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyListIteratorTests.java
new file mode 100644
index 0000000000..35c8efc011
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyListIteratorTests.java
@@ -0,0 +1,127 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 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