diff options
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal')
39 files changed, 0 insertions, 10036 deletions
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 deleted file mode 100644 index ed3e6f2814..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/BitToolsTests.java +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006, 2007 Oracle. All rights reserved. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0, which accompanies this distribution - * and is available at http://www.eclipse.org/legal/epl-v10.html. - * - * Contributors: - * Oracle - initial API and implementation - ******************************************************************************/ -package org.eclipse.jpt.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 deleted file mode 100644 index 504b621daa..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClassToolsTests.java +++ /dev/null @@ -1,548 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 54beea0c81..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ClasspathTests.java +++ /dev/null @@ -1,289 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 027984029e..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/CollectionToolsTests.java +++ /dev/null @@ -1,1972 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index adcccf0992..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/FileToolsTests.java +++ /dev/null @@ -1,594 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 51120c9eb2..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/HashBagTests.java +++ /dev/null @@ -1,448 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index c96213249b..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/IndentingPrintWriterTests.java +++ /dev/null @@ -1,107 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 40f152dfa9..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JDBCTypeTests.java +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 1a0c0b826f..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JavaTypeTests.java +++ /dev/null @@ -1,250 +0,0 @@ -/******************************************************************************* - * 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/JptUtilityTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JptUtilityTests.java deleted file mode 100644 index 82143f3bb4..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/JptUtilityTests.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * 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.JptUtilityIteratorsTests; - -/** - * decentralize test creation code - */ -public class JptUtilityTests { - - public static Test suite() { - TestSuite suite = new TestSuite(ClassTools.packageNameFor(JptUtilityTests.class)); - - suite.addTest(JptUtilityIteratorsTests.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 JptUtilityTests() { - 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 deleted file mode 100644 index 164fa49e03..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/NameToolsTests.java +++ /dev/null @@ -1,214 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index d7f2cce428..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/RangeTests.java +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 7ae00fdc2e..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/ReverseComparatorTests.java +++ /dev/null @@ -1,101 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 454cb81651..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/StringToolsTests.java +++ /dev/null @@ -1,833 +0,0 @@ -/******************************************************************************* - * 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()); - } - - // ********** wrapping ********** - - public void testWrap() { - this.verifyWrap("Employee", "123", "123Employee123"); - this.verifyWrap("123", "123", "123123123"); - this.verifyWrap("", "123", "123123"); - } - - private void verifyWrap(String string, String wrap, String expectedString) { - assertEquals(expectedString, StringTools.wrap(string, wrap)); - } - - public void testWrapOnWriter() { - this.verifyWrapOnWriter("Employee", "123", "123Employee123"); - this.verifyWrapOnWriter("123", "123", "123123123"); - this.verifyWrapOnWriter("", "123", "123123"); - } - - private void verifyWrapOnWriter(String string, String wrap, String expectedString) { - Writer writer = new StringWriter(); - StringTools.wrapOn(string, wrap, writer); - assertEquals(expectedString, writer.toString()); - } - - public void testWrapOnStringBuffer() { - this.verifyWrapOnStringBuffer("Employee", "123", "123Employee123"); - this.verifyWrapOnStringBuffer("123", "123", "123123123"); - this.verifyWrapOnStringBuffer("", "123", "123123"); - } - - private void verifyWrapOnStringBuffer(String string, String wrap, String expectedString) { - StringBuffer sb = new StringBuffer(); - StringTools.wrapOn(string, wrap, sb); - assertEquals(expectedString, sb.toString()); - } - - public void testQuote() { - this.verifyQuote("Employee", "\"Employee\""); - this.verifyQuote("123", "\"123\""); - this.verifyQuote("", "\"\""); - } - - private void verifyQuote(String string, String expectedString) { - assertEquals(expectedString, StringTools.quote(string)); - } - - public void testQuoteOnWriter() { - this.verifyQuoteOnWriter("Employee", "\"Employee\""); - this.verifyQuoteOnWriter("123", "\"123\""); - this.verifyQuoteOnWriter("", "\"\""); - } - - private void verifyQuoteOnWriter(String string, String expectedString) { - Writer writer = new StringWriter(); - StringTools.quoteOn(string, writer); - assertEquals(expectedString, writer.toString()); - } - - public void testQuoteOnStringBuffer() { - this.verifyQuoteOnStringBuffer("Employee", "\"Employee\""); - this.verifyQuoteOnStringBuffer("123", "\"123\""); - this.verifyQuoteOnStringBuffer("", "\"\""); - } - - private void verifyQuoteOnStringBuffer(String string, String expectedString) { - StringBuffer sb = new StringBuffer(); - StringTools.quoteOn(string, sb); - assertEquals(expectedString, 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")); - } - - public void testCommonPrefixLengthMax() { - assertEquals(2, StringTools.commonPrefixLength("fooZZZ", "fooBBB", 2)); - assertEquals(2, StringTools.commonPrefixLength("foo", "fooBBB", 2)); - assertEquals(2, StringTools.commonPrefixLength("fooZZZ", "foo", 2)); - assertEquals(2, StringTools.commonPrefixLength("foo", "foo", 2)); - } - - // ********** 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 testStringIsEmptyString() { - 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 testStringIsEmptyCharArray() { - assertTrue(StringTools.stringIsEmpty((char[]) null)); - this.verifyStringIsEmptyCharArray(""); - this.verifyStringIsEmptyCharArray(" \t\t "); - this.verifyStringIsEmptyCharArray(" "); - this.verifyStringIsEmptyCharArray(" \t\t " + StringTools.CR); - } - - private void verifyStringIsEmptyCharArray(String string) { - assertTrue(StringTools.stringIsEmpty(string.toCharArray())); - } - - public void testStringsAreEqualIgnoreCaseStringString() { - 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")); - } - - public void testStringsAreEqualIgnoreCaseCharArrayCharArray() { - assertTrue(StringTools.stringsAreEqualIgnoreCase((char[]) null, (char[]) null)); - assertFalse(StringTools.stringsAreEqualIgnoreCase((char[]) null, "asdf".toCharArray())); - assertFalse(StringTools.stringsAreEqualIgnoreCase("asdf".toCharArray(), (char[]) null)); - assertTrue(StringTools.stringsAreEqualIgnoreCase("asdf".toCharArray(), "asdf".toCharArray())); - assertTrue(StringTools.stringsAreEqualIgnoreCase("asdf".toCharArray(), "ASDF".toCharArray())); - } - - public void testStringStartsWithIgnoreCaseStringString() { - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf", "as")); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf", "aS")); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf", "")); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf", "A")); - - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf", "bsdf")); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf", "g")); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf", "asdg")); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf", "asdfg")); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf", "asdfgggggg")); - } - - public void testStringStartsWithIgnoreCaseCharArrayCharArray() { - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "as".toCharArray())); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "aS".toCharArray())); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "".toCharArray())); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "A".toCharArray())); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "ASDF".toCharArray())); - assertTrue(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "asdf".toCharArray())); - - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "bsdf".toCharArray())); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "g".toCharArray())); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "asdg".toCharArray())); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "asdfg".toCharArray())); - assertFalse(StringTools.stringStartsWithIgnoreCase("asdf".toCharArray(), "asdfgggggg".toCharArray())); - } - - public void testCharactersAreEqualIgnoreCase() { - assertTrue(StringTools.charactersAreEqualIgnoreCase('a', 'a')); - assertTrue(StringTools.charactersAreEqualIgnoreCase('a', 'A')); - assertTrue(StringTools.charactersAreEqualIgnoreCase('A', 'a')); - assertTrue(StringTools.charactersAreEqualIgnoreCase('A', 'A')); - - assertFalse(StringTools.charactersAreEqualIgnoreCase('a', 'b')); - assertFalse(StringTools.charactersAreEqualIgnoreCase('A', 'b')); - } - - // ********** 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 deleted file mode 100644 index 04a3d84786..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/TestTools.java +++ /dev/null @@ -1,161 +0,0 @@ -/******************************************************************************* - * 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.ObjectInput; -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 - */ - 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 = readObject(inStream); - inStream.close(); - - return o2; - } - - @SuppressWarnings("unchecked") - private static <T> T readObject(ObjectInput objectInput) throws IOException, ClassNotFoundException { - return (T) objectInput.readObject(); - } - - /** - * 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 deleted file mode 100644 index 3b104b9b89..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/XMLStringEncoderTests.java +++ /dev/null @@ -1,135 +0,0 @@ -/******************************************************************************* - * 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 = "?foo?"; - assertEquals(expected, encoder.encode(s)); - - s = "?foo&123"; - expected = "?foo&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 = "?foo?"; - String expected = "?foo?"; - assertEquals(expected, encoder.decode(s)); - - s = "?foo&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"); - } - - 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 deleted file mode 100644 index d7cf76ad44..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayIteratorTests.java +++ /dev/null @@ -1,126 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 9d82950b94..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ArrayListIteratorTests.java +++ /dev/null @@ -1,150 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 59cd3073b9..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ChainIteratorTests.java +++ /dev/null @@ -1,119 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index e3afdb585f..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneIteratorTests.java +++ /dev/null @@ -1,252 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index c6045a9f12..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CloneListIteratorTests.java +++ /dev/null @@ -1,408 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index d7173cefa8..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeIteratorTests.java +++ /dev/null @@ -1,334 +0,0 @@ -/******************************************************************************* - * 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()); - } - - // leave unchecked so we can override in subclass - @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 deleted file mode 100644 index f2de1d1426..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/CompositeListIteratorTests.java +++ /dev/null @@ -1,404 +0,0 @@ -/******************************************************************************* - * 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); - } - - // unchecked so we can override the unchecked method in superclass - @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 deleted file mode 100644 index 98697897b7..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyEnumerationTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 6985ad4ffa..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyIteratorTests.java +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* - * 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 deleted file mode 100644 index 35c8efc011..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EmptyListIteratorTests.java +++ /dev/null @@ -1,127 +0,0 @@ -/******************************************************************************* - * 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.ListIterator; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.EmptyListIterator; - -public class EmptyListIteratorTests extends TestCase { - - public EmptyListIteratorTests(String name) { - super(name); - } - - public void testHasNext() { - int i = 0; - for (ListIterator<Object> stream = EmptyListIterator.instance(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(0, i); - } - - public void testNext() { - for (ListIterator<Object> stream = EmptyListIterator.instance(); stream.hasNext();) { - fail("bogus element: " + stream.next()); - } - } - - public void testNextIndex() { - ListIterator<Object> stream = EmptyListIterator.instance(); - assertEquals(0, stream.nextIndex()); - } - - public void testHasPrevious() { - ListIterator<Object> stream = EmptyListIterator.instance(); - int i = 0; - while (stream.hasPrevious()) { - stream.previous(); - i++; - } - assertEquals(0, i); - - while (stream.hasNext()) { - stream.next(); - } - i = 0; - while (stream.hasPrevious()) { - stream.previous(); - i++; - } - assertEquals(0, i); - } - - public void testPrevious() { - ListIterator<Object> stream = EmptyListIterator.instance(); - while (stream.hasPrevious()) { - fail("bogus element: " + stream.previous()); - } - while (stream.hasNext()) { - stream.next(); - } - while (stream.hasPrevious()) { - fail("bogus element: " + stream.previous()); - } - } - - public void testPreviousIndex() { - ListIterator<Object> stream = EmptyListIterator.instance(); - assertEquals(-1, stream.previousIndex()); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - ListIterator<Object> stream = EmptyListIterator.instance(); - Object element = null; - while (stream.hasNext()) { - element = stream.next(); - } - try { - element = stream.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown (next): " + element, exCaught); - while (stream.hasPrevious()) { - element = stream.previous(); - } - try { - element = stream.previous(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown (previous): " + element, exCaught); - } - - public void testUnsupportedOperationException() { - boolean exCaught = false; - try { - EmptyListIterator.instance().remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - assertTrue("UnsupportedOperationException not thrown (remove)", exCaught); - try { - EmptyListIterator.instance().set(new Object()); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - assertTrue("UnsupportedOperationException not thrown (set)", exCaught); - try { - EmptyListIterator.instance().add(new Object()); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - assertTrue("UnsupportedOperationException not thrown (add)", exCaught); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EnumerationIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EnumerationIteratorTests.java deleted file mode 100644 index 528f101110..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/EnumerationIteratorTests.java +++ /dev/null @@ -1,119 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.NoSuchElementException; -import java.util.Vector; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.EnumerationIterator; - -public class EnumerationIteratorTests extends TestCase { - - public EnumerationIteratorTests(String name) { - super(name); - } - - public void testHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testHasNextUpcast() { - int i = 0; - for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testNext() { - Enumeration<String> enumeration = this.buildEnumeration(); - for (Iterator<String> stream = this.buildIterator(); stream.hasNext();) { - assertEquals("bogus element", enumeration.nextElement(), stream.next()); - } - } - - public void testNextUpcast() { - Enumeration<String> enumeration = this.buildEnumeration(); - for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) { - assertEquals("bogus element", enumeration.nextElement(), 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("three")) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - private Iterator<String> buildIterator() { - return this.buildIterator(this.buildEnumeration()); - } - - private Iterator<String> buildIterator(Enumeration<String> enumeration) { - return new EnumerationIterator<String>(enumeration); - } - - private Enumeration<String> buildEnumeration() { - return this.buildVector().elements(); - } - - private Vector<String> buildVector() { - Vector<String> v = new Vector<String>(); - v.addElement("one"); - v.addElement("two"); - v.addElement("three"); - v.addElement("four"); - v.addElement("five"); - v.addElement("six"); - v.addElement("seven"); - v.addElement("eight"); - return v; - } - - private Iterator<Object> buildIteratorUpcast() { - return this.buildIteratorUpcast(this.buildEnumeration()); - } - - private Iterator<Object> buildIteratorUpcast(Enumeration<String> enumeration) { - return new EnumerationIterator<Object>(enumeration); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/FilteringIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/FilteringIteratorTests.java deleted file mode 100644 index c268245304..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/FilteringIteratorTests.java +++ /dev/null @@ -1,285 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.List; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.CollectionTools; -import org.eclipse.jpt.utility.internal.Filter; -import org.eclipse.jpt.utility.internal.SimpleFilter; -import org.eclipse.jpt.utility.internal.iterators.FilteringIterator; - -public class FilteringIteratorTests extends TestCase { - - private static final String PREFIX = "prefix"; - - public FilteringIteratorTests(String name) { - super(name); - } - - public void testUnsupportedOperationException() { - boolean exCaught = false; - for (Iterator<String> stream = this.buildAcceptIterator(); stream.hasNext();) { - String string = stream.next(); - if (string.equals(PREFIX + "3")) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Iterator<String> stream = this.buildAcceptIterator(); - 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 testAcceptHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildAcceptIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testAcceptNext() { - for (Iterator<String> stream = this.buildAcceptIterator(); stream.hasNext();) { - assertTrue("bogus accept", stream.next().startsWith(PREFIX)); - } - } - - public void testInnerHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildInnerIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testInnerNext() { - for (Iterator<String> stream = this.buildInnerIterator(); stream.hasNext();) { - assertTrue("bogus accept", stream.next().startsWith(PREFIX)); - } - } - - public void testRejectHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildRejectIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(2, i); - } - - public void testRejectNext() { - for (Iterator<String> stream = this.buildRejectIterator(); stream.hasNext();) { - assertFalse("bogus reject", stream.next().startsWith(PREFIX)); - } - } - - public void testBothHasNext() { - // if both accept() and reject() are overridden, accept() is used - int i = 0; - for (Iterator<String> stream = this.buildBothIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testLoadNext() { - // loadNext() used to cause a NPE when executing during the - // constructor because the "outer" class is not bound until completion - // of the constructor - for (Iterator<String> stream = this.buildInnerIterator2(); stream.hasNext();) { - assertTrue("bogus accept", stream.next().startsWith(PREFIX)); - } - } - - // test a filtered iterator with a nested iterator of a sub-type (Number vs. Integer) - public void testGenerics1() { - List<Integer> integers = new ArrayList<Integer>(); - integers.add(new Integer(0)); - integers.add(new Integer(1)); - integers.add(new Integer(2)); - integers.add(new Integer(3)); - Iterator<Number> stream = new FilteringIterator<Number>(integers.iterator()) { - @Override - protected boolean accept(Object o) { - return ((Number) o).intValue() > 1; - } - }; - assertEquals(2, CollectionTools.size(stream)); - } - - // test a filtered iterator with a nested iterator of a super-type (Integer vs. Number) - // i.e. trust that Filter will only return the appropriate sub-type objects - public void testGenerics2() { - List<Number> integers = new ArrayList<Number>(); - integers.add(new Integer(0)); - integers.add(new Long(1)); - integers.add(new Integer(2)); - integers.add(new Float(3)); - Iterator<Integer> stream = new FilteringIterator<Integer>(integers.iterator()) { - @Override - protected boolean accept(Object o) { - return o instanceof Integer; - } - }; - assertEquals(2, CollectionTools.size(stream)); - } - - public void testFilterHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildFilterIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testFilterNext() { - for (Iterator<String> stream = this.buildFilterIterator(); stream.hasNext();) { - assertTrue("bogus accept", stream.next().startsWith(PREFIX)); - } - } - - private Iterator<String> buildFilteredIterator(Iterator<String> nestedIterator, Filter<String> filter) { - return new FilteringIterator<String>(nestedIterator, filter); - } - - private Iterator<String> buildInnerFilteredIterator(Iterator<String> nestedIterator) { - return new FilteringIterator<String>(nestedIterator) { - @Override - protected boolean accept(Object o) { - return ((String) o).startsWith(PREFIX); - } - }; - } - - String getPrefix() { - return PREFIX; - } - - // this inner iterator will call the "outer" object - private Iterator<String> buildInnerFilteredIterator2(Iterator<String> nestedIterator) { - return new FilteringIterator<String>(nestedIterator) { - @Override - protected boolean accept(Object o) { - return ((String) o).startsWith(FilteringIteratorTests.this.getPrefix()); - } - }; - } - - private Iterator<String> buildNestedIterator() { - Collection<String> c = new ArrayList<String>(); - c.add(PREFIX + "1"); - c.add(PREFIX + "2"); - c.add(PREFIX + "3"); - c.add("4"); - c.add(PREFIX + "5"); - c.add(PREFIX + "6"); - c.add(PREFIX + "7"); - c.add("8"); - return c.iterator(); - } - - private Iterator<String> buildAcceptIterator() { - return this.buildFilteredIterator(this.buildNestedIterator(), this.buildAcceptFilter(PREFIX)); - } - - private Iterator<String> buildInnerIterator() { - return this.buildInnerFilteredIterator(this.buildNestedIterator()); - } - - // this inner iterator will call the "outer" object - private Iterator<String> buildInnerIterator2() { - return this.buildInnerFilteredIterator2(this.buildNestedIterator()); - } - - private Iterator<String> buildFilterIterator() { - return this.buildFilteredIterator(this.buildNestedIterator(), this.buildFilterFilter(PREFIX)); - } - - private Filter<String> buildAcceptFilter(String prefix) { - return new SimpleFilter<String, String>(prefix) { - private static final long serialVersionUID = 1L; - - @Override - public boolean accept(String s) { - return s.startsWith(this.criterion); - } - }; - } - - private Iterator<String> buildRejectIterator() { - return this.buildFilteredIterator(this.buildNestedIterator(), this.buildRejectFilter(PREFIX)); - } - - private Filter<String> buildRejectFilter(String prefix) { - return new SimpleFilter<String, String>(prefix) { - private static final long serialVersionUID = 1L; - - @Override - public boolean reject(String s) { - return s.startsWith(this.criterion); - } - }; - } - - // use anonymous inner Filter - private Filter<String> buildFilterFilter(final String prefix) { - return new Filter<String>() { - public boolean accept(String s) { - return s.startsWith(prefix); - } - }; - } - - private Iterator<String> buildBothIterator() { - return this.buildFilteredIterator(this.buildNestedIterator(), this.buildBothFilter(PREFIX)); - } - - private Filter<String> buildBothFilter(String prefix) { - return new SimpleFilter<String, String>(prefix) { - private static final long serialVersionUID = 1L; - - @Override - public boolean reject(String s) { - return s.startsWith(this.criterion); - } - - @Override - public boolean accept(String s) { - return s.startsWith(this.criterion); - } - }; - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/GraphIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/GraphIteratorTests.java deleted file mode 100644 index d7f884c04e..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/GraphIteratorTests.java +++ /dev/null @@ -1,183 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.CollectionTools; -import org.eclipse.jpt.utility.internal.iterators.GraphIterator; -import org.eclipse.jpt.utility.tests.internal.TestTools; - -public class GraphIteratorTests extends TestCase { - /** this will be populated with all the nodes created for the test */ - Collection<GraphNode> nodes = new ArrayList<GraphNode>(); - - public GraphIteratorTests(String name) { - super(name); - } - - @Override - protected void tearDown() throws Exception { - TestTools.clear(this); - super.tearDown(); - } - - public void testHasNext1() { - this.verifyHasNext(this.buildGraphIterator1()); - } - - public void testHasNext2() { - this.verifyHasNext(this.buildGraphIterator2()); - } - - private void verifyHasNext(Iterator<GraphNode> iterator) { - int i = 0; - while (iterator.hasNext()) { - iterator.next(); - i++; - } - assertEquals(this.nodes.size(), i); - } - - public void testNext1() { - this.verifyNext(this.buildGraphIterator1()); - } - - public void testNext2() { - this.verifyNext(this.buildGraphIterator2()); - } - - private void verifyNext(Iterator<GraphNode> iterator) { - while (iterator.hasNext()) { - assertTrue("bogus element", this.nodes.contains(iterator.next())); - } - } - - public void testNoSuchElementException1() { - this.verifyNoSuchElementException(this.buildGraphIterator1()); - } - - public void testNoSuchElementException2() { - this.verifyNoSuchElementException(this.buildGraphIterator2()); - } - - private void verifyNoSuchElementException(Iterator<GraphNode> iterator) { - boolean exCaught = false; - while (iterator.hasNext()) { - iterator.next(); - } - try { - iterator.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown", exCaught); - } - - public void testSize1() { - this.verifySize(this.buildGraphIterator1()); - } - - public void testSize2() { - this.verifySize(this.buildGraphIterator2()); - } - - private void verifySize(Iterator<GraphNode> iterator) { - int iteratorSize = CollectionTools.size(iterator); - int actualSize = this.nodes.size(); - assertTrue("Too few items in iterator.", iteratorSize >= actualSize); - assertTrue("Too many items in iterator.", iteratorSize <= actualSize); - } - - /** - * build a graph iterator with an explicit misterRogers - */ - private Iterator<GraphNode> buildGraphIterator1() { - return new GraphIterator<GraphNode>(this.buildGraphRoot(), this.buildMisterRogers()); - } - - private GraphIterator.MisterRogers<GraphNode> buildMisterRogers() { - return new GraphIterator.MisterRogers<GraphNode>() { - public Iterator<GraphNode> neighbors(GraphNode next) { - return next.neighbors(); - } - }; - } - - /** - * build a graph iterator with an override - */ - private Iterator<GraphNode> buildGraphIterator2() { - return new GraphIterator<GraphNode>(this.buildGraphRoot()) { - @Override - public Iterator<GraphNode> neighbors(GraphNode next) { - return next.neighbors(); - } - }; - } - - private GraphNode buildGraphRoot() { - GraphNode ncNode = new GraphNode("North Carolina"); - GraphNode vaNode = new GraphNode("Virginia"); - GraphNode scNode = new GraphNode("South Carolina"); - GraphNode gaNode = new GraphNode("Georgia"); - GraphNode flNode = new GraphNode("Florida"); - GraphNode alNode = new GraphNode("Alabama"); - GraphNode msNode = new GraphNode("Mississippi"); - GraphNode tnNode = new GraphNode("Tennessee"); - - ncNode.setNeighbors(new GraphNode[] { vaNode, scNode, gaNode, tnNode }); - vaNode.setNeighbors(new GraphNode[] { ncNode, tnNode }); - scNode.setNeighbors(new GraphNode[] { ncNode, gaNode }); - gaNode.setNeighbors(new GraphNode[] { ncNode, scNode, flNode, alNode, tnNode }); - flNode.setNeighbors(new GraphNode[] { gaNode }); - alNode.setNeighbors(new GraphNode[] { gaNode, msNode, tnNode }); - msNode.setNeighbors(new GraphNode[] { alNode, tnNode }); - tnNode.setNeighbors(new GraphNode[] { vaNode, ncNode, gaNode, alNode, msNode }); - - return ncNode; - } - - private class GraphNode { - private String name; - - private Collection<GraphNode> neighbors = new ArrayList<GraphNode>(); - - public GraphNode(String name) { - super(); - GraphIteratorTests.this.nodes.add(this); // log node - this.name = name; - } - - public String getName() { - return this.name; - } - - void setNeighbors(GraphNode[] neighbors) { - this.neighbors = CollectionTools.list(neighbors); - } - - public Iterator<GraphNode> neighbors() { - return this.neighbors.iterator(); - } - - public int neighborsSize() { - return this.neighbors.size(); - } - - @Override - public String toString() { - return "GraphNode(" + this.name + ")"; - } - } -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/IteratorEnumerationTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/IteratorEnumerationTests.java deleted file mode 100644 index 42d8eb459e..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/IteratorEnumerationTests.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.NoSuchElementException; -import java.util.Vector; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.IteratorEnumeration; - -public class IteratorEnumerationTests extends TestCase { - - public IteratorEnumerationTests(String name) { - super(name); - } - - public void testHasMoreElements() { - int i = 0; - for (Enumeration<String> stream = this.buildEnumeration(); stream.hasMoreElements();) { - stream.nextElement(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testHasMoreElementsUpcast() { - int i = 0; - for (Enumeration<Object> stream = this.buildEnumerationUpcast(); stream.hasMoreElements();) { - stream.nextElement(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testNextElement() { - Iterator<String> iterator = this.buildIterator(); - for (Enumeration<String> stream = this.buildEnumeration(); stream.hasMoreElements();) { - assertEquals("bogus element", iterator.next(), stream.nextElement()); - } - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Enumeration<String> stream = this.buildEnumeration(); - String string = null; - while (stream.hasMoreElements()) { - string = stream.nextElement(); - } - try { - string = stream.nextElement(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown: " + string, exCaught); - } - - private Enumeration<String> buildEnumeration() { - return this.buildEnumeration(this.buildIterator()); - } - - private Enumeration<Object> buildEnumerationUpcast() { - return this.buildEnumerationUpcast(this.buildIterator()); - } - - private Enumeration<String> buildEnumeration(Iterator<String> iterator) { - return new IteratorEnumeration<String>(iterator); - } - - private Enumeration<Object> buildEnumerationUpcast(Iterator<String> iterator) { - return new IteratorEnumeration<Object>(iterator); - } - - private Iterator<String> buildIterator() { - return this.buildVector().iterator(); - } - - private Vector<String> buildVector() { - Vector<String> v = new Vector<String>(); - v.addElement("one"); - v.addElement("two"); - v.addElement("three"); - v.addElement("four"); - v.addElement("five"); - v.addElement("six"); - v.addElement("seven"); - v.addElement("eight"); - return v; - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/JptUtilityIteratorsTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/JptUtilityIteratorsTests.java deleted file mode 100644 index 76e6717b34..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/JptUtilityIteratorsTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * 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 junit.framework.Test; -import junit.framework.TestSuite; - -/** - * decentralize test creation code - */ -public class JptUtilityIteratorsTests { - - public static Test suite() { - TestSuite suite = new TestSuite(JptUtilityIteratorsTests.class.getPackage().getName()); - - suite.addTestSuite(ArrayIteratorTests.class); - suite.addTestSuite(ArrayListIteratorTests.class); - suite.addTestSuite(ChainIteratorTests.class); - suite.addTestSuite(CloneIteratorTests.class); - suite.addTestSuite(CloneListIteratorTests.class); - suite.addTestSuite(CompositeIteratorTests.class); - suite.addTestSuite(CompositeListIteratorTests.class); - suite.addTestSuite(EmptyEnumerationTests.class); - suite.addTestSuite(EmptyIteratorTests.class); - suite.addTestSuite(EmptyListIteratorTests.class); - suite.addTestSuite(EnumerationIteratorTests.class); - suite.addTestSuite(FilteringIteratorTests.class); - suite.addTestSuite(GraphIteratorTests.class); - suite.addTestSuite(IteratorEnumerationTests.class); - suite.addTestSuite(PeekableIteratorTests.class); - suite.addTestSuite(ReadOnlyIteratorTests.class); - suite.addTestSuite(ReadOnlyListIteratorTests.class); - suite.addTestSuite(SingleElementIteratorTests.class); - suite.addTestSuite(SingleElementListIteratorTests.class); - suite.addTestSuite(TransformationIteratorTests.class); - suite.addTestSuite(TransformationListIteratorTests.class); - suite.addTestSuite(TreeIteratorTests.class); - - return suite; - } - - private JptUtilityIteratorsTests() { - super(); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/PeekableIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/PeekableIteratorTests.java deleted file mode 100644 index 1a3b47b78f..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/PeekableIteratorTests.java +++ /dev/null @@ -1,140 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.PeekableIterator; - -public class PeekableIteratorTests extends TestCase { - - public PeekableIteratorTests(String name) { - super(name); - } - - public void testUnsupportedOperationException() { - boolean exCaught = false; - for (Iterator<String> stream = this.buildPeekableIterator(); stream.hasNext();) { - String string = stream.next(); - if (string.equals("three")) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Iterator<String> stream = this.buildPeekableIterator(); - 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 testHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildPeekableIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testHasNextUpcast() { - int i = 0; - for (Iterator<Object> stream = this.buildPeekableIteratorUpcast(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(6, i); - } - - public void testNext() { - Iterator<String> stream = this.buildPeekableIterator(); - assertEquals("zero", stream.next()); - assertEquals("one", stream.next()); - assertEquals("two", stream.next()); - assertEquals("three", stream.next()); - assertEquals("four", stream.next()); - assertEquals("five", stream.next()); - } - - public void testNextUpcast() { - Iterator<Object> stream = this.buildPeekableIteratorUpcast(); - assertEquals("zero", stream.next()); - assertEquals("one", stream.next()); - assertEquals("two", stream.next()); - assertEquals("three", stream.next()); - assertEquals("four", stream.next()); - assertEquals("five", stream.next()); - } - - public void testPeek() { - Object next = null; - for (PeekableIterator<String> stream = this.buildPeekableIterator(); stream.hasNext();) { - Object peek = stream.peek(); - assertTrue("peek and next are prematurely identical", peek != next); - next = stream.next(); - assertTrue("peek and next are not identical", peek == next); - } - } - - public void testPeekUpcast() { - Object next = null; - for (PeekableIterator<Object> stream = this.buildPeekableIteratorUpcast(); stream.hasNext();) { - Object peek = stream.peek(); - assertTrue("peek and next are prematurely identical", peek != next); - next = stream.next(); - assertTrue("peek and next are not identical", peek == next); - } - } - - private PeekableIterator<String> buildPeekableIterator() { - return this.buildPeekableIterator(this.buildNestedIterator()); - } - - private PeekableIterator<Object> buildPeekableIteratorUpcast() { - return this.buildPeekableIteratorUpcast(this.buildNestedIterator()); - } - - private PeekableIterator<String> buildPeekableIterator(Iterator<String> nestedIterator) { - return new PeekableIterator<String>(nestedIterator); - } - - private PeekableIterator<Object> buildPeekableIteratorUpcast(Iterator<String> nestedIterator) { - return new PeekableIterator<Object>(nestedIterator); - } - - private Iterator<String> buildNestedIterator() { - Collection<String> c = new ArrayList<String>(); - c.add("zero"); - c.add("one"); - c.add("two"); - c.add("three"); - c.add("four"); - c.add("five"); - return c.iterator(); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyIteratorTests.java deleted file mode 100644 index 4defa2e414..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyIteratorTests.java +++ /dev/null @@ -1,118 +0,0 @@ -/******************************************************************************* - * 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 java.util.Vector; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.ReadOnlyIterator; - -public class ReadOnlyIteratorTests extends TestCase { - - public ReadOnlyIteratorTests(String name) { - super(name); - } - - public void testHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildReadOnlyIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testHasNextUpcast() { - int i = 0; - for (Iterator<Object> stream = this.buildReadOnlyIteratorUpcast(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(this.buildVector().size(), i); - } - - public void testNext() { - Iterator<String> nestedIterator = this.buildNestedIterator(); - for (Iterator<String> stream = this.buildReadOnlyIterator(); stream.hasNext();) { - assertEquals("bogus element", nestedIterator.next(), stream.next()); - } - } - - public void testNextUpcast() { - Iterator<String> nestedIterator = this.buildNestedIterator(); - for (Iterator<Object> stream = this.buildReadOnlyIteratorUpcast(); stream.hasNext();) { - assertEquals("bogus element", nestedIterator.next(), stream.next()); - } - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Iterator<String> stream = this.buildReadOnlyIterator(); - 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 testRemove() { - boolean exCaught = false; - for (Iterator<String> stream = this.buildReadOnlyIterator(); stream.hasNext();) { - if (stream.next().equals("three")) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - private Iterator<String> buildReadOnlyIterator() { - return this.buildReadOnlyIterator(this.buildNestedIterator()); - } - - private Iterator<Object> buildReadOnlyIteratorUpcast() { - return this.buildReadOnlyIteratorUpcast(this.buildNestedIterator()); - } - - private Iterator<String> buildReadOnlyIterator(Iterator<String> nestedIterator) { - return new ReadOnlyIterator<String>(nestedIterator); - } - - private Iterator<Object> buildReadOnlyIteratorUpcast(Iterator<String> nestedIterator) { - return new ReadOnlyIterator<Object>(nestedIterator); - } - - private Iterator<String> buildNestedIterator() { - return this.buildVector().iterator(); - } - - private Vector<String> buildVector() { - Vector<String> v = new Vector<String>(); - v.addElement("one"); - v.addElement("two"); - v.addElement("three"); - v.addElement("four"); - v.addElement("five"); - v.addElement("six"); - v.addElement("seven"); - v.addElement("eight"); - return v; - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyListIteratorTests.java deleted file mode 100644 index 4db72f6754..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/ReadOnlyListIteratorTests.java +++ /dev/null @@ -1,203 +0,0 @@ -/******************************************************************************* - * 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.List; -import java.util.ListIterator; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.ReadOnlyListIterator; - -public class ReadOnlyListIteratorTests extends TestCase { - - public ReadOnlyListIteratorTests(String name) { - super(name); - } - - public void testHasNextAndHasPrevious() { - int i = 0; - ListIterator<String> stream = this.buildReadOnlyListIterator(); - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(this.buildList().size(), i); - - while (stream.hasPrevious()) { - stream.previous(); - i--; - } - assertEquals(0, i); - } - - public void testHasNextAndHasPreviousUpcast() { - int i = 0; - ListIterator<Object> stream = this.buildReadOnlyListIteratorUpcast(); - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(this.buildList().size(), i); - - while (stream.hasPrevious()) { - stream.previous(); - i--; - } - assertEquals(0, i); - } - - public void testNextAndPrevious() { - ListIterator<String> nestedListIterator = this.buildNestedListIterator(); - ListIterator<String> stream = this.buildReadOnlyListIterator(); - while (stream.hasNext()) { - assertEquals("bogus element", nestedListIterator.next(), stream.next()); - } - while (stream.hasPrevious()) { - assertEquals("bogus element", nestedListIterator.previous(), stream.previous()); - } - } - - public void testNextAndPreviousUpcast() { - ListIterator<String> nestedListIterator = this.buildNestedListIterator(); - ListIterator<Object> stream = this.buildReadOnlyListIteratorUpcast(); - while (stream.hasNext()) { - assertEquals("bogus element", nestedListIterator.next(), stream.next()); - } - while (stream.hasPrevious()) { - assertEquals("bogus element", nestedListIterator.previous(), stream.previous()); - } - } - - public void testNextIndexAndPreviousIndex() { - ListIterator<String> nestedListIterator = this.buildNestedListIterator(); - ListIterator<String> stream = this.buildReadOnlyListIterator(); - while (stream.hasNext()) { - assertEquals("bogus index", nestedListIterator.nextIndex(), stream.nextIndex()); - nestedListIterator.next(); - stream.next(); - } - assertEquals("bogus index", this.buildList().size(), stream.nextIndex()); - while (stream.hasPrevious()) { - assertEquals("bogus element", nestedListIterator.previousIndex(), stream.previousIndex()); - nestedListIterator.previous(); - stream.previous(); - } - assertEquals("bogus index", -1, stream.previousIndex()); - } - - public void testNextIndexAndPreviousIndexUpcast() { - ListIterator<String> nestedListIterator = this.buildNestedListIterator(); - ListIterator<Object> stream = this.buildReadOnlyListIteratorUpcast(); - while (stream.hasNext()) { - assertEquals("bogus index", nestedListIterator.nextIndex(), stream.nextIndex()); - nestedListIterator.next(); - stream.next(); - } - assertEquals("bogus index", this.buildList().size(), stream.nextIndex()); - while (stream.hasPrevious()) { - assertEquals("bogus element", nestedListIterator.previousIndex(), stream.previousIndex()); - nestedListIterator.previous(); - stream.previous(); - } - assertEquals("bogus index", -1, stream.previousIndex()); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - ListIterator<String> stream = this.buildReadOnlyListIterator(); - 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 testRemove() { - boolean exCaught = false; - for (ListIterator<String> stream = this.buildReadOnlyListIterator(); stream.hasNext();) { - if (stream.next().equals("three")) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testSet() { - boolean exCaught = false; - for (ListIterator<String> stream = this.buildReadOnlyListIterator(); stream.hasNext();) { - if (stream.next().equals("three")) { - try { - stream.set("bogus"); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testAdd() { - boolean exCaught = false; - for (ListIterator<String> stream = this.buildReadOnlyListIterator(); stream.hasNext();) { - if (stream.next().equals("three")) { - try { - stream.add("bogus"); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - private ListIterator<String> buildReadOnlyListIterator() { - return this.buildReadOnlyListIterator(this.buildNestedListIterator()); - } - - private ListIterator<Object> buildReadOnlyListIteratorUpcast() { - return this.buildReadOnlyListIteratorUpcast(this.buildNestedListIterator()); - } - - private ListIterator<String> buildReadOnlyListIterator(ListIterator<String> nestedListIterator) { - return new ReadOnlyListIterator<String>(nestedListIterator); - } - - private ListIterator<Object> buildReadOnlyListIteratorUpcast(ListIterator<String> nestedListIterator) { - return new ReadOnlyListIterator<Object>(nestedListIterator); - } - - private ListIterator<String> buildNestedListIterator() { - return this.buildList().listIterator(); - } - - private List<String> buildList() { - List<String> l = new ArrayList<String>(); - l.add("one"); - l.add("two"); - l.add("three"); - l.add("four"); - l.add("five"); - l.add("six"); - l.add("seven"); - l.add("eight"); - return l; - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementIteratorTests.java deleted file mode 100644 index e84657c93d..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementIteratorTests.java +++ /dev/null @@ -1,71 +0,0 @@ -/******************************************************************************* - * 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.SingleElementIterator; - -public class SingleElementIteratorTests extends TestCase { - - public SingleElementIteratorTests(String name) { - super(name); - } - - public void testHasNext() { - int i = 0; - for (Iterator<String> stream = this.buildSingleElementIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(1, i); - } - - public void testNext() { - for (Iterator<String> stream = this.buildSingleElementIterator(); stream.hasNext();) { - assertEquals("bogus element", this.singleElement(), stream.next()); - } - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Iterator<String> stream = this.buildSingleElementIterator(); - String string = stream.next(); - try { - string = stream.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown: " + string, exCaught); - } - - public void testRemove() { - boolean exCaught = false; - for (Iterator<String> stream = this.buildSingleElementIterator(); stream.hasNext();) { - if (stream.next().equals(this.singleElement())) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - protected Iterator<String> buildSingleElementIterator() { - return new SingleElementIterator<String>(this.singleElement()); - } - - protected String singleElement() { - return "single element"; - } -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementListIteratorTests.java deleted file mode 100644 index e3e4197c7d..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/SingleElementListIteratorTests.java +++ /dev/null @@ -1,111 +0,0 @@ -/******************************************************************************* - * 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 org.eclipse.jpt.utility.internal.iterators.SingleElementListIterator; - -public class SingleElementListIteratorTests extends SingleElementIteratorTests { - - public SingleElementListIteratorTests(String name) { - super(name); - } - - public void testNextIndex() { - ListIterator<String> stream = this.buildSingleElementListIterator(); - while (stream.hasNext()) { - assertEquals("bogus index", 0, stream.nextIndex()); - stream.next(); - } - assertEquals("bogus index", 1, stream.nextIndex()); - } - - public void testHasPrevious() { - int i = 0; - ListIterator<String> stream = this.buildSingleElementListIterator(); - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(1, i); - - while (stream.hasPrevious()) { - stream.previous(); - i++; - } - assertEquals(2, i); - } - - public void testPrevious() { - ListIterator<String> stream = this.buildSingleElementListIterator(); - - while (stream.hasNext()) { - assertEquals("bogus element", this.singleElement(), stream.next()); - } - - while (stream.hasPrevious()) { - assertEquals("bogus element", this.singleElement(), stream.previous()); - } - } - - public void testPreviousIndex() { - ListIterator<String> stream = this.buildSingleElementListIterator(); - - while (stream.hasNext()) { - assertEquals("bogus index", 0, stream.nextIndex()); - stream.next(); - } - - while (stream.hasPrevious()) { - assertEquals("bogus index", 0, stream.previousIndex()); - stream.previous(); - } - - assertEquals("bogus index", -1, stream.previousIndex()); - } - - public void testAdd() { - boolean exCaught = false; - ListIterator<String> stream = this.buildSingleElementListIterator(); - - try { - stream.add("foo"); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testSet() { - boolean exCaught = false; - for (ListIterator<String> stream = this.buildSingleElementListIterator(); stream.hasNext();) { - if (stream.next().equals(this.singleElement())) { - try { - stream.set("foo"); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - @Override - protected Iterator<String> buildSingleElementIterator() { - return new SingleElementListIterator<String>(this.singleElement()); - } - - protected ListIterator<String> buildSingleElementListIterator() { - return (ListIterator<String>) this.buildSingleElementIterator(); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationIteratorTests.java deleted file mode 100644 index 6ec0d082b1..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationIteratorTests.java +++ /dev/null @@ -1,216 +0,0 @@ -/******************************************************************************* - * 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.Transformer; -import org.eclipse.jpt.utility.internal.iterators.TransformationIterator; - -public class TransformationIteratorTests extends TestCase { - - public TransformationIteratorTests(String name) { - super(name); - } - - public void testHasNext() { - int i = 0; - for (Iterator<Integer> stream = this.buildIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(8, i); - } - - public void testHasNextUpcast() { - int i = 0; - for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(8, i); - } - - public void testInnerHasNext() { - int i = 0; - for (Iterator<Integer> stream = this.buildInnerIterator(); stream.hasNext();) { - stream.next(); - i++; - } - assertEquals(8, i); - } - - public void testNext() { - int i = 0; - for (Iterator<Integer> stream = this.buildIterator(); stream.hasNext();) { - assertEquals("bogus transformation", ++i, stream.next().intValue()); - } - } - - public void testNextUpcast() { - int i = 0; - for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) { - assertEquals("bogus transformation", ++i, ((Integer) stream.next()).intValue()); - } - } - - public void testInnerNext() { - int i = 0; - for (Iterator<Integer> stream = this.buildInnerIterator(); stream.hasNext();) { - assertEquals("bogus transformation", ++i, stream.next().intValue()); - } - } - - public void testRemove() { - Collection<String> c = this.buildCollection(); - for (Iterator<Integer> stream = this.buildInnerTransformationIterator(c.iterator()); stream.hasNext();) { - if (stream.next().intValue() == 3) { - stream.remove(); - } - } - assertEquals("nothing removed", this.buildCollection().size() - 1, c.size()); - assertFalse("element still in collection", c.contains("333")); - assertTrue("wrong element removed", c.contains("22")); - } - - public void testInnerRemove() { - Collection<String> c = this.buildCollection(); - for (Iterator<Integer> stream = this.buildTransformationIterator(c.iterator(), this.buildTransformer()); stream.hasNext();) { - if (stream.next().intValue() == 3) { - stream.remove(); - } - } - assertEquals("nothing removed", this.buildCollection().size() - 1, c.size()); - assertFalse("element still in collection", c.contains("333")); - assertTrue("wrong element removed", c.contains("22")); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - Iterator<Integer> stream = this.buildIterator(); - Integer integer = null; - while (stream.hasNext()) { - integer = stream.next(); - } - try { - integer = stream.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown: " + integer, exCaught); - } - - public void testUnsupportedOperationException() { - boolean exCaught = false; - for (Iterator<Integer> stream = this.buildUnmodifiableIterator(); stream.hasNext();) { - int i = stream.next().intValue(); - if (i == 3) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testIllegalStateException() { - boolean exCaught = false; - try { - this.buildIterator().remove(); - } catch (IllegalStateException ex) { - exCaught = true; - } - assertTrue("IllegalStateException not thrown", exCaught); - } - - private Iterator<Integer> buildIterator() { - return this.buildTransformationIterator(this.buildNestedIterator(), this.buildTransformer()); - } - - private Iterator<Object> buildIteratorUpcast() { - return this.buildTransformationIteratorUpcast(this.buildNestedIterator(), this.buildTransformerUpcast()); - } - - private Iterator<Integer> buildInnerIterator() { - return this.buildInnerTransformationIterator(this.buildNestedIterator()); - } - - private Iterator<Integer> buildUnmodifiableIterator() { - return this.buildTransformationIterator(this.buildUnmodifiableNestedIterator(), this.buildTransformer()); - } - - private Iterator<Integer> buildTransformationIterator(Iterator<String> nestedIterator, Transformer<String, Integer> transformer) { - return new TransformationIterator<String, Integer>(nestedIterator, transformer); - } - - private Iterator<Object> buildTransformationIteratorUpcast(Iterator<String> nestedIterator, Transformer<Object, Integer> transformer) { - return new TransformationIterator<Object, Object>(nestedIterator, transformer); - } - - private Transformer<String, Integer> buildTransformer() { - // transform each string into an integer with a value of the string's length - return new Transformer<String, Integer>() { - public Integer transform(String next) { - return new Integer(next.length()); - } - }; - } - - private Transformer<Object, Integer> buildTransformerUpcast() { - // transform each string into an integer with a value of the string's length - return new Transformer<Object, Integer>() { - public Integer transform(Object next) { - return new Integer(((String) next).length()); - } - }; - } - - private Iterator<Integer> buildInnerTransformationIterator(Iterator<String> nestedIterator) { - // transform each string into an integer with a value of the string's length - return new TransformationIterator<String, Integer>(nestedIterator) { - @Override - protected Integer transform(String next) { - return new Integer(next.length()); - } - }; - } - - private Iterator<String> buildNestedIterator() { - return this.buildCollection().iterator(); - } - - private Iterator<String> buildUnmodifiableNestedIterator() { - return this.buildUnmodifiableCollection().iterator(); - } - - private Collection<String> buildCollection() { - Collection<String> c = new ArrayList<String>(); - c.add("1"); - c.add("22"); - c.add("333"); - c.add("4444"); - c.add("55555"); - c.add("666666"); - c.add("7777777"); - c.add("88888888"); - return c; - } - - private Collection<String> buildUnmodifiableCollection() { - return Collections.unmodifiableCollection(this.buildCollection()); - } - -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationListIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationListIteratorTests.java deleted file mode 100644 index fb507d5af4..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TransformationListIteratorTests.java +++ /dev/null @@ -1,306 +0,0 @@ -/******************************************************************************* - * 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.Transformer; -import org.eclipse.jpt.utility.internal.iterators.TransformationListIterator; - -public class TransformationListIteratorTests extends TestCase { - public TransformationListIteratorTests(String name) { - super(name); - } - - public void testHasNextAndHasPrevious() { - int i = 0; - ListIterator<Integer> stream = this.buildIterator(); - - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(8, i); - - while (stream.hasPrevious()) { - stream.previous(); - i--; - } - assertEquals(0, i); - } - - public void testHasNextAndHasPreviousUpcast() { - int i = 0; - ListIterator<Object> stream = this.buildIteratorUpcast(); - - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(8, i); - - while (stream.hasPrevious()) { - stream.previous(); - i--; - } - assertEquals(0, i); - } - - public void testInnerHasNextAndHasPrevious() { - int i = 0; - ListIterator<Integer> stream = this.buildInnerIterator(); - - while (stream.hasNext()) { - stream.next(); - i++; - } - assertEquals(8, i); - - while (stream.hasPrevious()) { - stream.previous(); - i--; - } - assertEquals(0, i); - } - - public void testNextAndPrevious() { - int i = 0; - ListIterator<Integer> stream = this.buildIterator(); - - while (stream.hasNext()) { - assertEquals(++i, stream.next().intValue()); - } - - ++i; - - while (stream.hasPrevious()) { - assertEquals(--i, stream.previous().intValue()); - } - } - - public void testInnerNextAndPrevious() { - int i = 0; - ListIterator<Integer> stream = this.buildInnerIterator(); - - while (stream.hasNext()) { - assertEquals(++i, stream.next().intValue()); - } - - ++i; - - while (stream.hasPrevious()) { - assertEquals(--i, stream.previous().intValue()); - } - } - - public void testNextIndexAndPreviousIndex() { - int i = -1; - ListIterator<Integer> stream = this.buildIterator(); - - while (stream.hasNext()) { - assertEquals(++i, stream.nextIndex()); - stream.next(); - } - - ++i; - - while (stream.hasPrevious()) { - assertEquals(--i, stream.previousIndex()); - stream.previous(); - } - } - - public void testInnerNextIndexAndPreviousIndex() { - int i = -1; - ListIterator<Integer> stream = this.buildInnerIterator(); - - while (stream.hasNext()) { - assertEquals(++i, stream.nextIndex()); - stream.next(); - } - - ++i; - - while (stream.hasPrevious()) { - assertEquals(--i, stream.previousIndex()); - stream.previous(); - } - } - - public void testRemove() { - List<String> l = this.buildList(); - for (ListIterator<Integer> stream = this.buildInnerTransformationListIterator(l.listIterator()); stream.hasNext();) { - if (stream.next().intValue() == 3) { - stream.remove(); - } - } - assertEquals("nothing removed", this.buildList().size() - 1, l.size()); - assertFalse("element still in list", l.contains("333")); - assertTrue("wrong element removed", l.contains("22")); - } - - public void testInnerRemove() { - List<String> l = this.buildList(); - for (ListIterator<Integer> stream = this.buildTransformationListIterator(l.listIterator(), this.buildTransformer()); stream.hasNext();) { - if (stream.next().intValue() == 3) { - stream.remove(); - } - } - assertEquals("nothing removed", this.buildList().size() - 1, l.size()); - assertFalse("element still in list", l.contains("333")); - assertTrue("wrong element removed", l.contains("22")); - } - - public void testUnsupportedOperationExceptionOnAdd() { - ListIterator<Integer> stream = this.buildIterator(); - boolean exCaught = false; - try { - stream.add(new Integer(0)); - fail("exception not thrown"); - } catch (UnsupportedOperationException e) { - exCaught = true; - } - assertTrue(exCaught); - } - - public void testUnsupportedOperationExceptionOnSet() { - ListIterator<Integer> stream = this.buildIterator(); - boolean exCaught = false; - try { - stream.set(new Integer(0)); - fail("exception not thrown"); - } catch (UnsupportedOperationException e) { - exCaught = true; - } - assertTrue(exCaught); - } - - public void testNoSuchElementException() { - boolean exCaught = false; - ListIterator<Integer> stream = this.buildIterator(); - Integer integer = null; - while (stream.hasNext()) { - integer = stream.next(); - } - try { - integer = stream.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown: " + integer, exCaught); - } - - public void testUnsupportedOperationException() { - boolean exCaught = false; - for (Iterator<Integer> stream = this.buildUnmodifiableIterator(); stream.hasNext();) { - int i = stream.next().intValue(); - if (i == 3) { - try { - stream.remove(); - } catch (UnsupportedOperationException ex) { - exCaught = true; - } - } - } - assertTrue("UnsupportedOperationException not thrown", exCaught); - } - - public void testIllegalStateException() { - boolean exCaught = false; - try { - this.buildIterator().remove(); - } catch (IllegalStateException ex) { - exCaught = true; - } - assertTrue("IllegalStateException not thrown", exCaught); - } - - private ListIterator<Integer> buildIterator() { - return this.buildTransformationListIterator(this.buildNestedIterator(), this.buildTransformer()); - } - - private ListIterator<Object> buildIteratorUpcast() { - return this.buildTransformationListIteratorUpcast(this.buildNestedIterator(), this.buildTransformerUpcast()); - } - - private ListIterator<Integer> buildInnerIterator() { - return this.buildInnerTransformationListIterator(this.buildNestedIterator()); - } - - private ListIterator<Integer> buildUnmodifiableIterator() { - return this.buildTransformationListIterator(this.buildUnmodifiableNestedIterator(), this.buildTransformer()); - } - - private ListIterator<Integer> buildTransformationListIterator(ListIterator<String> nestedIterator, Transformer<String, Integer> transformer) { - return new TransformationListIterator<String, Integer>(nestedIterator, transformer); - } - - private ListIterator<Object> buildTransformationListIteratorUpcast(ListIterator<String> nestedIterator, Transformer<Object, Integer> transformer) { - return new TransformationListIterator<Object, Object>(nestedIterator, transformer); - } - - private Transformer<String, Integer> buildTransformer() { - // transform each string into an integer with a value of the string's length - return new Transformer<String, Integer>() { - public Integer transform(String next) { - return new Integer(next.length()); - } - }; - } - - private Transformer<Object, Integer> buildTransformerUpcast() { - // transform each string into an integer with a value of the string's length - return new Transformer<Object, Integer>() { - public Integer transform(Object next) { - return new Integer(((String) next).length()); - } - }; - } - - private ListIterator<Integer> buildInnerTransformationListIterator(ListIterator<String> nestedIterator) { - // transform each string into an integer with a value of the string's length - return new TransformationListIterator<String, Integer>(nestedIterator) { - @Override - protected Integer transform(String next) { - return new Integer(next.length()); - } - }; - } - - private ListIterator<String> buildNestedIterator() { - return this.buildList().listIterator(); - } - - private ListIterator<String> buildUnmodifiableNestedIterator() { - return this.buildUnmodifiableList().listIterator(); - } - - private List<String> buildList() { - List<String> l = new ArrayList<String>(); - l.add("1"); - l.add("22"); - l.add("333"); - l.add("4444"); - l.add("55555"); - l.add("666666"); - l.add("7777777"); - l.add("88888888"); - return l; - } - - private List<String> buildUnmodifiableList() { - return Collections.unmodifiableList(this.buildList()); - } -} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TreeIteratorTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TreeIteratorTests.java deleted file mode 100644 index 655d0835c0..0000000000 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterators/TreeIteratorTests.java +++ /dev/null @@ -1,197 +0,0 @@ -/******************************************************************************* - * 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.Iterator; -import java.util.NoSuchElementException; -import junit.framework.TestCase; -import org.eclipse.jpt.utility.internal.iterators.TreeIterator; -import org.eclipse.jpt.utility.tests.internal.TestTools; - -public class TreeIteratorTests extends TestCase { - /** this will be populated with all the nodes created for the test */ - Collection<TreeNode> nodes = new ArrayList<TreeNode>(); - - public TreeIteratorTests(String name) { - super(name); - } - - @Override - protected void tearDown() throws Exception { - TestTools.clear(this); - super.tearDown(); - } - - public void testHasNext1() { - this.verifyHasNext(this.buildTreeIterator1()); - } - - public void testHasNext2() { - this.verifyHasNext(this.buildTreeIterator2()); - } - - private void verifyHasNext(Iterator<TreeNode> iterator) { - int i = 0; - while (iterator.hasNext()) { - iterator.next(); - i++; - } - assertEquals(this.nodes.size(), i); - } - - public void testNext1() { - this.verifyNext(this.buildTreeIterator1()); - } - - public void testNext2() { - this.verifyNext(this.buildTreeIterator2()); - } - - private void verifyNext(Iterator<TreeNode> iterator) { - while (iterator.hasNext()) { - assertTrue("bogus element", this.nodes.contains(iterator.next())); - } - } - - public void testNoSuchElementException1() { - this.verifyNoSuchElementException(this.buildTreeIterator1()); - } - - public void testNoSuchElementException2() { - this.verifyNoSuchElementException(this.buildTreeIterator2()); - } - - private void verifyNoSuchElementException(Iterator<TreeNode> iterator) { - boolean exCaught = false; - while (iterator.hasNext()) { - iterator.next(); - } - try { - iterator.next(); - } catch (NoSuchElementException ex) { - exCaught = true; - } - assertTrue("NoSuchElementException not thrown", exCaught); - } - - public void testRemove1() { - this.verifyRemove(this.buildTreeIterator1()); - } - - public void testRemove2() { - this.verifyRemove(this.buildTreeIterator2()); - } - - private void verifyRemove(Iterator<TreeNode> iterator) { - String parentName = "child 2"; - String childName = "grandchild 2A"; - int startSize = this.childrenSize(parentName); - while (iterator.hasNext()) { - TreeNode node = iterator.next(); - if (node.getName().equals(childName)) { - iterator.remove(); - } - } - int endSize = this.childrenSize(parentName); - assertEquals(startSize - 1, endSize); - } - - private int childrenSize(String nodeName) { - for (Iterator<TreeNode> stream = this.nodes.iterator(); stream.hasNext();) { - TreeNode node = stream.next(); - if (node.getName().equals(nodeName)) { - return node.childrenSize(); - } - } - throw new IllegalArgumentException(nodeName); - } - - /** - * build a tree iterator with an explicit midwife - */ - private Iterator<TreeNode> buildTreeIterator1() { - return new TreeIterator<TreeNode>(this.buildTree(), this.buildMidwife()); - } - - private TreeIterator.Midwife<TreeNode> buildMidwife() { - return new TreeIterator.Midwife<TreeNode>() { - public Iterator<TreeNode> children(TreeNode next) { - return next.children(); - } - }; - } - - /** - * build a tree iterator with an override - */ - private Iterator<TreeNode> buildTreeIterator2() { - return new TreeIterator<TreeNode>(this.buildTree()) { - @Override - public Iterator<TreeNode> children(TreeNode next) { - return next.children(); - } - }; - } - - private TreeNode buildTree() { - TreeNode root = new TreeNode("root"); - TreeNode child1 = new TreeNode(root, "child 1"); - new TreeNode(child1, "grandchild 1A"); - TreeNode child2 = new TreeNode(root, "child 2"); - new TreeNode(child2, "grandchild 2A"); - TreeNode grandchild2B = new TreeNode(child2, "grandchild 2B"); - new TreeNode(grandchild2B, "great-grandchild 2B1"); - new TreeNode(grandchild2B, "great-grandchild 2B2"); - TreeNode grandchild2C = new TreeNode(child2, "grandchild 2C"); - new TreeNode(grandchild2C, "great-grandchild 2C1"); - new TreeNode(root, "child 3"); - return root; - } - - private class TreeNode { - private String name; - private Collection<TreeNode> children = new ArrayList<TreeNode>(); - - public TreeNode(String name) { - super(); - TreeIteratorTests.this.nodes.add(this); // log node - this.name = name; - } - - public TreeNode(TreeNode parent, String name) { - this(name); - parent.addChild(this); - } - - public String getName() { - return this.name; - } - - private void addChild(TreeNode child) { - this.children.add(child); - } - - public Iterator<TreeNode> children() { - return this.children.iterator(); - } - - public int childrenSize() { - return this.children.size(); - } - - @Override - public String toString() { - return "TreeNode(" + this.name + ")"; - } - } - -} |