diff options
author | bvosburgh | 2009-12-19 05:59:41 +0000 |
---|---|---|
committer | bvosburgh | 2009-12-19 05:59:41 +0000 |
commit | 40d0f8e56c2e5dad270f3cb5821fc0d0af1f9509 (patch) | |
tree | 9ba9e5a35129e2928279307ba7cf4f1489cf7810 /jpa/tests/org.eclipse.jpt.utility.tests | |
parent | 8465584c62d5e81bf9a255e38a0e028055a44f5d (diff) | |
download | webtools.dali-40d0f8e56c2e5dad270f3cb5821fc0d0af1f9509.tar.gz webtools.dali-40d0f8e56c2e5dad270f3cb5821fc0d0af1f9509.tar.xz webtools.dali-40d0f8e56c2e5dad270f3cb5821fc0d0af1f9509.zip |
ListIterable work
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.utility.tests')
25 files changed, 1528 insertions, 115 deletions
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayIterableTests.java index d10cf764e6..0cd9451f3c 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayIterableTests.java @@ -27,6 +27,13 @@ public class ArrayIterableTests extends TestCase { } } + public void testSubIterator() { + int i = 3; + for (String string : this.buildIterable(2)) { + assertEquals(i++, Integer.parseInt(string)); + } + } + public void testIllegalArgumentException() { this.triggerIllegalArgumentException(-1, 1); this.triggerIllegalArgumentException(8, 1); @@ -46,11 +53,15 @@ public class ArrayIterableTests extends TestCase { } private Iterable<String> buildIterable() { - return this.buildIterable(this.buildArray()); + return this.buildIterable(0); + } + + private Iterable<String> buildIterable(int start) { + return this.buildIterable(this.buildArray(), start); } - private Iterable<String> buildIterable(String[] array) { - return new ArrayIterable<String>(array); + private Iterable<String> buildIterable(String[] array, int start) { + return new ArrayIterable<String>(array, start); } private Iterable<String> buildIterable(int start, int length) { diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayListIterableTests.java new file mode 100644 index 0000000000..e810e83daa --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ArrayListIterableTests.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.ListIterator; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.ArrayListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListIterable; + +@SuppressWarnings("nls") +public class ArrayListIterableTests extends TestCase { + + public ArrayListIterableTests(String name) { + super(name); + } + + public void testIterator() { + int i = 1; + ListIterable<String> iterable = this.buildIterable(); + for (String string : iterable) { + assertEquals(i++, Integer.parseInt(string)); + } + ListIterator<String> stream = iterable.iterator(); + while (stream.hasNext()) { + stream.next(); + } + while (stream.hasPrevious()) { + assertEquals(--i, Integer.parseInt(stream.previous())); + } + } + + public void testSubIterator() { + int i = 3; + for (String string : this.buildIterable(2)) { + assertEquals(i++, Integer.parseInt(string)); + } + } + + public void testIllegalArgumentException() { + this.triggerIllegalArgumentException(-1, 1); + this.triggerIllegalArgumentException(8, 1); + this.triggerIllegalArgumentException(0, -1); + this.triggerIllegalArgumentException(0, 9); + } + + private void triggerIllegalArgumentException(int start, int length) { + boolean exCaught = false; + try { + Iterable<String> iterable = this.buildIterable(start, length); + fail("bogus iterable: " + iterable); + } catch (IllegalArgumentException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + private ListIterable<String> buildIterable() { + return this.buildIterable(0); + } + + private ListIterable<String> buildIterable(int start) { + return this.buildIterable(this.buildArray(), start); + } + + private ListIterable<String> buildIterable(String[] array, int start) { + return (start == 0) ? + new ArrayListIterable<String>(array) : + new ArrayListIterable<String>(array, start); + } + + private ListIterable<String> buildIterable(int start, int length) { + return this.buildIterable(this.buildArray(), start, length); + } + + private ListIterable<String> buildIterable(String[] array, int start, int length) { + return new ArrayListIterable<String>(array, start, length); + } + + private 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/iterables/ChainIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ChainIterableTests.java index 4b057e8026..67255da19b 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ChainIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ChainIterableTests.java @@ -11,12 +11,15 @@ package org.eclipse.jpt.utility.tests.internal.iterables; import java.util.AbstractCollection; import java.util.AbstractList; +import java.util.Iterator; import java.util.Vector; import junit.framework.TestCase; import org.eclipse.jpt.utility.internal.iterables.ChainIterable; +import org.eclipse.jpt.utility.internal.iterators.ChainIterator; +@SuppressWarnings("nls") public class ChainIterableTests extends TestCase { private final static Class<?>[] VECTOR_HIERARCHY = { Vector.class, AbstractList.class, AbstractCollection.class, Object.class }; @@ -32,6 +35,30 @@ public class ChainIterableTests extends TestCase { } } + public void testLinker() { + int i = 0; + for (Class<?> clazz : new ChainIterable<Class<?>>(Vector.class, this.buildLinker())) { + assertEquals(VECTOR_HIERARCHY[i++], clazz); + } + } + + public void testException() { + Iterable<Class<?>> iterable = new ChainIterable<Class<?>>(Vector.class); + Iterator<Class<?>> iterator = iterable.iterator(); + boolean exCaught = false; + try { + Class<?> clazz = iterator.next(); + fail("bogus class: " + clazz); + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testToString() { + assertNotNull(this.buildIterable().toString()); + } + private Iterable<Class<?>> buildIterable() { return this.buildChainIterable(Vector.class); } @@ -46,4 +73,12 @@ public class ChainIterableTests extends TestCase { }; } + private ChainIterator.Linker<Class<?>> buildLinker() { + return new ChainIterator.Linker<Class<?>>() { + public Class<?> nextLink(Class<?> currentLink) { + return currentLink.getSuperclass(); + } + }; + } + } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CloneIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CloneIterableTests.java new file mode 100644 index 0000000000..f8eff94e23 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CloneIterableTests.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterators.CloneIterator; +import org.eclipse.jpt.utility.internal.iterators.CloneListIterator; + +@SuppressWarnings("nls") +public abstract class CloneIterableTests extends TestCase { + Iterable<String> iterable; + + public CloneIterableTests(String name) { + super(name); + } + + public void testIterator() { + List<String> c = new ArrayList<String>(); + c.add("0"); + c.add("1"); + c.add("2"); + c.add("3"); + assertEquals(4, c.size()); + this.iterable = this.buildIterable(c); + int i = 0; + for (String s : this.iterable) { + assertEquals(String.valueOf(i++), s); + c.remove("3"); + } + assertEquals(4, i); + assertEquals(3, c.size()); + } + + public void testRemove() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildRemovingIterable(collection); + + Object removed = "three"; + assertTrue(CollectionTools.contains(this.iterable, removed)); + for (Iterator<String> iterator = this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals(removed)) { + iterator.remove(); + } + } + assertFalse(collection.contains(removed)); + } + + public void testRemover() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterableWithRemover(collection); + + Object removed = "three"; + assertTrue(CollectionTools.contains(this.iterable, removed)); + for (Iterator<String> iterator = this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals(removed)) { + iterator.remove(); + } + } + assertFalse(collection.contains(removed)); + } + + public void testMissingRemover() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(this.iterable.toString()); + + Object removed = "three"; + assertTrue(CollectionTools.contains(this.iterable, removed)); + boolean exCaught = false; + for (Iterator<String> iterator = this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals(removed)) { + try { + iterator.remove(); + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + public void testToString() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(iterable.toString()); + } + + abstract Iterable<String> buildIterable(List<String> c); + + abstract Iterable<String> buildRemovingIterable(List<String> c); + + abstract Iterable<String> buildIterableWithRemover(List<String> c); + + CloneIterator.Remover<String> buildRemover(final Collection<String> c) { + return new CloneIterator.Remover<String>() { + public void remove(String current) { + c.remove(current); + } + }; + } + + CloneListIterator.Mutator<String> buildMutator(final List<String> list) { + return new CloneListIterator.Mutator<String>() { + public void add(int index, String string) { + list.add(index, string); + } + public void set(int index, String string) { + list.set(index, string); + } + public void remove(int index) { + list.remove(index); + } + }; + } + + List<String> buildCollection() { + List<String> c = new ArrayList<String>(); + c.add("one"); + c.add("two"); + c.add("three"); + c.add("four"); + c.add("five"); + c.add("six"); + c.add("seven"); + c.add("eight"); + return c; + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CompositeListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CompositeListIterableTests.java new file mode 100644 index 0000000000..aca4d95f72 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/CompositeListIterableTests.java @@ -0,0 +1,117 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.CompositeListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListListIterable; + +@SuppressWarnings("nls") +public class CompositeListIterableTests extends TestCase { + + public CompositeListIterableTests(String name) { + super(name); + } + + public void testIterator() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + + @SuppressWarnings("unchecked") + Iterable<String> composite = new CompositeListIterable<String>(c1, c2); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testExtraElement1() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + + Iterable<String> composite = new CompositeListIterable<String>(c1, "4"); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testExtraElement2() { + List<String> c1 = new ArrayList<String>(); + c1.add("1"); + c1.add("2"); + c1.add("3"); + + Iterable<String> composite = new CompositeListIterable<String>("0", c1); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testCollectionOfIterables() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + + List<ListIterable<String>> collection = new ArrayList<ListIterable<String>>(); + collection.add(new ListListIterable<String>(c1)); + collection.add(new ListListIterable<String>(c2)); + Iterable<String> composite = new CompositeListIterable<String>(collection); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testToString() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + + @SuppressWarnings("unchecked") + Iterable<String> composite = new CompositeListIterable<String>(c1, c2); + assertNotNull(composite.toString()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyIterableTests.java index d1eac7dac9..ae269e9c42 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyIterableTests.java @@ -12,6 +12,7 @@ package org.eclipse.jpt.utility.tests.internal.iterables; import junit.framework.TestCase; import org.eclipse.jpt.utility.internal.iterables.EmptyIterable; +import org.eclipse.jpt.utility.tests.internal.TestTools; @SuppressWarnings("nls") public class EmptyIterableTests extends TestCase { @@ -26,4 +27,13 @@ public class EmptyIterableTests extends TestCase { } } + public void testToString() { + assertNotNull(EmptyIterable.instance().toString()); + } + + public void testSerialization() throws Exception { + Iterable<String> iterable = EmptyIterable.instance(); + assertSame(iterable, TestTools.serialize(iterable)); + } + } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyListIterableTests.java new file mode 100644 index 0000000000..b037ace764 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/EmptyListIterableTests.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.EmptyListIterable; +import org.eclipse.jpt.utility.tests.internal.TestTools; + +@SuppressWarnings("nls") +public class EmptyListIterableTests extends TestCase { + + public EmptyListIterableTests(String name) { + super(name); + } + + public void testIterator() { + for (String s : EmptyListIterable.<String>instance()) { + fail("bogus element: " + s); + } + } + + public void testToString() { + assertNotNull(EmptyListIterable.instance().toString()); + } + + public void testSerialization() throws Exception { + Iterable<String> iterable = EmptyListIterable.instance(); + assertSame(iterable, TestTools.serialize(iterable)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/FilteringIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/FilteringIterableTests.java index 1cf2f87ba5..7d870c0a85 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/FilteringIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/FilteringIterableTests.java @@ -11,9 +11,11 @@ package org.eclipse.jpt.utility.tests.internal.iterables; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import junit.framework.TestCase; +import org.eclipse.jpt.utility.Filter; import org.eclipse.jpt.utility.internal.iterables.FilteringIterable; @SuppressWarnings("nls") @@ -33,6 +35,32 @@ public class FilteringIterableTests extends TestCase { assertEquals(6, i); } + public void testFilter() { + Filter<String> filter = this.buildFilter(); + int i = 0; + for (String s : new FilteringIterable<String, String>(this.buildNestedIterable(), filter)) { + assertTrue(s.contains(PREFIX)); + i++; + } + assertEquals(6, i); + } + + public void testToString() { + assertNotNull(this.buildIterable().toString()); + } + + public void testMissingFilter() { + boolean exCaught = false; + Iterable<String> iterable = new FilteringIterable<String, String>(this.buildNestedIterable()); + try { + Iterator<String> iterator = iterable.iterator(); + fail("bogus iterator: " + iterator); + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + private Iterable<String> buildIterable() { return this.buildFilteringIterable(this.buildNestedIterable()); } @@ -46,6 +74,14 @@ public class FilteringIterableTests extends TestCase { }; } + private Filter<String> buildFilter() { + return new Filter<String>() { + public boolean accept(String s) { + return s.startsWith(PREFIX); + } + }; + } + private Iterable<String> buildNestedIterable() { Collection<String> c = new ArrayList<String>(); c.add(PREFIX + "1"); diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GenericIterableWrapperTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GenericIterableWrapperTests.java index bb06c97a86..c936cf45db 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GenericIterableWrapperTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GenericIterableWrapperTests.java @@ -10,6 +10,7 @@ package org.eclipse.jpt.utility.tests.internal.iterables; import java.util.ArrayList; +import java.util.Collections; import junit.framework.TestCase; @@ -41,4 +42,9 @@ public class GenericIterableWrapperTests extends TestCase { assertEquals("foobarbaz", concat); } + public void testToString() { + Iterable<Object> iterable = new GenericIterableWrapper<Object>(Collections.emptyList()); + assertNotNull(iterable.toString()); + } + } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GraphIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GraphIterableTests.java index 69f4e12903..e1b74caf29 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GraphIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/GraphIterableTests.java @@ -17,6 +17,7 @@ import junit.framework.TestCase; import org.eclipse.jpt.utility.internal.CollectionTools; import org.eclipse.jpt.utility.internal.iterables.GraphIterable; +import org.eclipse.jpt.utility.internal.iterators.GraphIterator; import org.eclipse.jpt.utility.tests.internal.TestTools; @SuppressWarnings("nls") @@ -34,13 +35,13 @@ public class GraphIterableTests extends TestCase { super.tearDown(); } - public void testNeighbors() { - for (GraphNode gn : this.buildGraphIterable()) { + public void testNeighbors1() { + for (GraphNode gn : this.buildGraphIterable1()) { assertTrue(this.nodes.contains(gn)); } } - private Iterable<GraphNode> buildGraphIterable() { + private Iterable<GraphNode> buildGraphIterable1() { return new GraphIterable<GraphNode>(this.buildGraphRoot()) { @Override public Iterator<GraphNode> neighbors(GraphNode next) { @@ -49,6 +50,66 @@ public class GraphIterableTests extends TestCase { }; } + public void testNeighbors2() { + for (GraphNode gn : this.buildGraphIterable2()) { + assertTrue(this.nodes.contains(gn)); + } + } + + private Iterable<GraphNode> buildGraphIterable2() { + return new GraphIterable<GraphNode>(this.buildGraphRoot(), this.buildMisterRogers()); + } + + public void testNeighbors3() { + for (GraphNode gn : this.buildGraphIterable3()) { + assertTrue(this.nodes.contains(gn)); + } + } + + private Iterable<GraphNode> buildGraphIterable3() { + return new GraphIterable<GraphNode>(new GraphNode[] { this.buildGraphRoot() }) { + @Override + public Iterator<GraphNode> neighbors(GraphNode next) { + return next.neighbors(); + } + }; + } + + public void testNeighbors4() { + for (GraphNode gn : this.buildGraphIterable4()) { + assertTrue(this.nodes.contains(gn)); + } + } + + private Iterable<GraphNode> buildGraphIterable4() { + return new GraphIterable<GraphNode>(new GraphNode[] { this.buildGraphRoot() }, this.buildMisterRogers()); + } + + public void testToString() { + assertNotNull(this.buildGraphIterable1().toString()); + } + + public void testMissingMisterRogers() { + boolean exCaught = false; + try { + for (GraphNode gn : new GraphIterable<GraphNode>(this.buildGraphRoot())) { + assertTrue(this.nodes.contains(gn)); + } + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + private GraphIterator.MisterRogers<GraphNode> buildMisterRogers() { + return new GraphIterator.MisterRogers<GraphNode>() { + public Iterator<GraphNode> neighbors(GraphNode next) { + return next.neighbors(); + } + }; + } + private GraphNode buildGraphRoot() { GraphNode ncNode = new GraphNode("North Carolina"); GraphNode vaNode = new GraphNode("Virginia"); diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/JptUtilityIterablesTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/JptUtilityIterablesTests.java index 2d1523db9d..f3f88f4627 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/JptUtilityIterablesTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/JptUtilityIterablesTests.java @@ -21,17 +21,29 @@ public class JptUtilityIterablesTests { TestSuite suite = new TestSuite(JptUtilityIterablesTests.class.getPackage().getName()); suite.addTestSuite(ArrayIterableTests.class); + suite.addTestSuite(ArrayListIterableTests.class); suite.addTestSuite(ChainIterableTests.class); suite.addTestSuite(CompositeIterableTests.class); + suite.addTestSuite(CompositeListIterableTests.class); suite.addTestSuite(EmptyIterableTests.class); + suite.addTestSuite(EmptyListIterableTests.class); suite.addTestSuite(FilteringIterableTests.class); suite.addTestSuite(GenericIterableWrapperTests.class); suite.addTestSuite(GraphIterableTests.class); suite.addTestSuite(LiveCloneIterableTests.class); + suite.addTestSuite(LiveCloneListIterableTests.class); + suite.addTestSuite(PeekableIterableTests.class); + suite.addTestSuite(QueueIterableTests.class); + suite.addTestSuite(ReadOnlyCompositeListIterableTests.class); suite.addTestSuite(ReadOnlyIterableTests.class); + suite.addTestSuite(ReadOnlyListIterableTests.class); suite.addTestSuite(SingleElementIterableTests.class); + suite.addTestSuite(SingleElementListIterableTests.class); suite.addTestSuite(SnapshotCloneIterableTests.class); + suite.addTestSuite(SnapshotCloneListIterableTests.class); + suite.addTestSuite(StackIterableTests.class); suite.addTestSuite(TransformationIterableTests.class); + suite.addTestSuite(TransformationListIterableTests.class); suite.addTestSuite(TreeIterableTests.class); return suite; diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneIterableTests.java index 7cb5970e59..466d4213c4 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneIterableTests.java @@ -9,79 +9,63 @@ ******************************************************************************/ package org.eclipse.jpt.utility.tests.internal.iterables; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -import junit.framework.TestCase; +import java.util.List; import org.eclipse.jpt.utility.internal.CollectionTools; import org.eclipse.jpt.utility.internal.iterables.LiveCloneIterable; @SuppressWarnings("nls") -public class LiveCloneIterableTests extends TestCase { +public class LiveCloneIterableTests extends CloneIterableTests { public LiveCloneIterableTests(String name) { super(name); } + @Override public void testIterator() { - Collection<String> c = new ArrayList<String>(); - c.add("0"); - c.add("1"); - c.add("2"); - c.add("3"); - assertEquals(4, c.size()); - Iterable<String> iterable = new LiveCloneIterable<String>(c); - int i = 0; - for (String s : iterable) { - assertEquals(String.valueOf(i++), s); - c.remove("3"); - } - assertEquals(4, i); - assertEquals(3, c.size()); - + super.testIterator(); // iterable should now return only 3 strings (since it's "live") - i = 0; - for (String s : iterable) { + int i = 0; + for (String s : this.iterable) { assertEquals(String.valueOf(i++), s); } assertEquals(3, i); } + @Override public void testRemove() { - final Collection<String> collection = this.buildCollection(); - Iterable<String> iterable = new LiveCloneIterable<String>(collection) { - @Override - protected void remove(String current) { - collection.remove(current); - } - }; + super.testRemove(); + // "live" clone iterable will no longer contain the element removed from the + // original collection + assertFalse(CollectionTools.contains(this.iterable, "three")); + } - Object removed = "three"; - assertTrue(CollectionTools.contains(iterable, removed)); - for (Iterator<String> iterator = iterable.iterator(); iterator.hasNext(); ) { - if (iterator.next().equals(removed)) { - iterator.remove(); - } - } - assertFalse(collection.contains(removed)); + @Override + public void testRemover() { + super.testRemover(); // "live" clone iterable will no longer contain the element removed from the // original collection - assertFalse(CollectionTools.contains(iterable, "three")); + assertFalse(CollectionTools.contains(this.iterable, "three")); + } + + @Override + Iterable<String> buildIterable(List<String> c) { + return new LiveCloneIterable<String>(c); + } + + @Override + Iterable<String> buildRemovingIterable(final List<String> c) { + return new LiveCloneIterable<String>(c) { + @Override + protected void remove(String current) { + c.remove(current); + } + }; } - private Collection<String> buildCollection() { - Collection<String> c = new ArrayList<String>(); - c.add("one"); - c.add("two"); - c.add("three"); - c.add("four"); - c.add("five"); - c.add("six"); - c.add("seven"); - c.add("eight"); - return c; + @Override + Iterable<String> buildIterableWithRemover(List<String> c) { + return new LiveCloneIterable<String>(c, this.buildRemover(c)); } } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneListIterableTests.java new file mode 100644 index 0000000000..47ba2ed357 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/LiveCloneListIterableTests.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.List; +import java.util.ListIterator; + +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterables.LiveCloneListIterable; + +@SuppressWarnings("nls") +public class LiveCloneListIterableTests extends LiveCloneIterableTests { + + public LiveCloneListIterableTests(String name) { + super(name); + } + + public void testAdd() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildRemovingIterable(collection); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("two")) { + iterator.add(added); + } + } + assertTrue(collection.contains(added)); + // "live" clone iterable will contain the element added to the + // original collection + assertTrue(CollectionTools.contains(this.iterable, added)); + } + + public void testMissingMutatorAdd() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(this.iterable.toString()); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + boolean exCaught = false; + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("three")) { + try { + iterator.add(added); + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + public void testSet() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildRemovingIterable(collection); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("two")) { + iterator.set(added); + } + } + assertTrue(collection.contains(added)); + assertFalse(collection.contains("two")); + // "live" clone iterable will contain the element added to the + // original collection + assertTrue(CollectionTools.contains(this.iterable, added)); + assertFalse(CollectionTools.contains(this.iterable, "two")); + } + + public void testMissingMutatorSet() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(this.iterable.toString()); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + boolean exCaught = false; + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("three")) { + try { + iterator.set(added); + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + @Override + Iterable<String> buildIterable(List<String> c) { + return new LiveCloneListIterable<String>(c); + } + + @Override + Iterable<String> buildRemovingIterable(final List<String> c) { + return new LiveCloneListIterable<String>(c) { + @Override + protected void add(int index, String element) { + c.add(index, element); + } + @Override + protected void remove(int index) { + c.remove(index); + } + @Override + protected void set(int index, String element) { + c.set(index, element); + } + }; + } + + @Override + Iterable<String> buildIterableWithRemover(List<String> c) { + return new LiveCloneListIterable<String>(c, this.buildMutator(c)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/PeekableIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/PeekableIterableTests.java new file mode 100644 index 0000000000..118184f8d0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/PeekableIterableTests.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.ArrayIterable; +import org.eclipse.jpt.utility.internal.iterables.PeekableIterable; +import org.eclipse.jpt.utility.internal.iterators.PeekableIterator; + +@SuppressWarnings("nls") +public class PeekableIterableTests extends TestCase { + + public PeekableIterableTests(String name) { + super(name); + } + + public void testIterator() { + PeekableIterable<String> iterable = this.buildIterable(); + PeekableIterator<String> iterator = iterable.iterator(); + assertEquals("one", iterator.peek()); + } + + public void testToString() { + assertNotNull(this.buildIterable().toString()); + } + + private PeekableIterable<String> buildIterable() { + return new PeekableIterable<String>(this.buildNestedIterable()); + } + + private Iterable<String> buildNestedIterable() { + return new ArrayIterable<String>(this.buildArray()); + } + + private String[] buildArray() { + return new String[] {"one", "two", "three"}; + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/QueueIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/QueueIterableTests.java new file mode 100644 index 0000000000..42fbb19df5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/QueueIterableTests.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.Queue; +import org.eclipse.jpt.utility.internal.SimpleQueue; +import org.eclipse.jpt.utility.internal.iterables.QueueIterable; + +@SuppressWarnings("nls") +public class QueueIterableTests extends TestCase { + + public QueueIterableTests(String name) { + super(name); + } + + public void testIterator() { + Iterable<String> iterable = this.buildIterable(); + for (String s : iterable) { + assertNotNull(s); + } + } + + public void testToString() { + assertNotNull(this.buildIterable().toString()); + } + + private Iterable<String> buildIterable() { + return new QueueIterable<String>(this.buildQueue()); + } + + private Queue<String> buildQueue() { + Queue<String> q = new SimpleQueue<String>(); + q.enqueue("foo"); + q.enqueue("bar"); + q.enqueue("baz"); + return q; + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyCompositeListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyCompositeListIterableTests.java new file mode 100644 index 0000000000..5e18ab93d0 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyCompositeListIterableTests.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.ReadOnlyCompositeListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListListIterable; + +@SuppressWarnings("nls") +public class ReadOnlyCompositeListIterableTests extends TestCase { + + public ReadOnlyCompositeListIterableTests(String name) { + super(name); + } + + public void testIterator() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + ListIterable<String> li1 = new ListListIterable<String>(c1); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + ListIterable<String> li2 = new ListListIterable<String>(c2); + + @SuppressWarnings("unchecked") + Iterable<String> composite = new ReadOnlyCompositeListIterable<String>(li1, li2); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testExtraElement1() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + ListIterable<String> li1 = new ListListIterable<String>(c1); + + Iterable<String> composite = new ReadOnlyCompositeListIterable<String>(li1, "4"); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testExtraElement2() { + List<String> c1 = new ArrayList<String>(); + c1.add("1"); + c1.add("2"); + c1.add("3"); + ListIterable<String> li1 = new ListListIterable<String>(c1); + + Iterable<String> composite = new ReadOnlyCompositeListIterable<String>("0", li1); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testCollectionOfIterables() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + ListIterable<String> li1 = new ListListIterable<String>(c1); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + ListIterable<String> li2 = new ListListIterable<String>(c2); + + List<ListIterable<String>> collection = new ArrayList<ListIterable<String>>(); + collection.add(li1); + collection.add(li2); + ListIterable<ListIterable<String>> li = new ListListIterable<ListIterable<String>>(collection); + + Iterable<String> composite = new ReadOnlyCompositeListIterable<String>(li); + int i = 0; + for (String s : composite) { + assertEquals(String.valueOf(i++), s); + } + } + + public void testToString() { + List<String> c1 = new ArrayList<String>(); + c1.add("0"); + c1.add("1"); + c1.add("2"); + c1.add("3"); + ListIterable<String> li1 = new ListListIterable<String>(c1); + + List<String> c2 = new ArrayList<String>(); + c2.add("4"); + c2.add("5"); + c2.add("6"); + c2.add("7"); + ListIterable<String> li2 = new ListListIterable<String>(c2); + + @SuppressWarnings("unchecked") + Iterable<String> composite = new ReadOnlyCompositeListIterable<String>(li1, li2); + assertNotNull(composite.toString()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyIterableTests.java index aafebc185d..8bf2b79fc6 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyIterableTests.java @@ -44,6 +44,10 @@ public class ReadOnlyIterableTests extends TestCase { assertTrue(exCaught); } + public void testToString() { + assertNotNull(this.buildReadOnlyIterable().toString()); + } + private Iterable<String> buildReadOnlyIterable() { return new ReadOnlyIterable<String>(this.buildVector()); } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyListIterableTests.java new file mode 100644 index 0000000000..20bb6e7262 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/ReadOnlyListIterableTests.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.Iterator; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.ListIterable; +import org.eclipse.jpt.utility.internal.iterables.ListListIterable; +import org.eclipse.jpt.utility.internal.iterables.ReadOnlyListIterable; + +@SuppressWarnings("nls") +public class ReadOnlyListIterableTests extends TestCase { + + public ReadOnlyListIterableTests(String name) { + super(name); + } + + public void testIterator() { + Iterator<String> nestedIterator = this.buildVector().iterator(); + for (String s : this.buildReadOnlyListIterable()) { + assertEquals(nestedIterator.next(), s); + } + } + + public void testRemove() { + boolean exCaught = false; + for (Iterator<String> stream = this.buildReadOnlyListIterable().iterator(); stream.hasNext();) { + if (stream.next().equals("three")) { + try { + stream.remove(); + } catch (UnsupportedOperationException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + public void testToString() { + assertNotNull(this.buildReadOnlyListIterable().toString()); + } + + private Iterable<String> buildReadOnlyListIterable() { + return new ReadOnlyListIterable<String>(this.buildNestedListIterable()); + } + + private ListIterable<String> buildNestedListIterable() { + return new ListListIterable<String>(this.buildVector()); + } + + 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/iterables/SingleElementListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SingleElementListIterableTests.java new file mode 100644 index 0000000000..b3e19f9260 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SingleElementListIterableTests.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.iterables.SingleElementListIterable; + +@SuppressWarnings("nls") +public class SingleElementListIterableTests extends TestCase { + + public SingleElementListIterableTests(String name) { + super(name); + } + + public void testIterator() { + for (String s : this.buildSingleElementListIterable()) { + assertEquals(this.singleElement(), s); + } + } + + public void testNoSuchElementException() { + boolean exCaught = false; + Iterator<String> stream = this.buildSingleElementListIterable().iterator(); + String string = stream.next(); + try { + string = stream.next(); + fail("bogus element: " + string); + } catch (NoSuchElementException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testRemove() { + boolean exCaught = false; + for (Iterator<String> stream = this.buildSingleElementListIterable().iterator(); stream.hasNext(); ) { + if (stream.next().equals(this.singleElement())) { + try { + stream.remove(); + } catch (UnsupportedOperationException ex) { + exCaught = true; + } + } + } + assertTrue("UnsupportedOperationException not thrown", exCaught); + } + + public void testToString() { + assertNotNull(this.buildSingleElementListIterable().toString()); + } + + protected Iterable<String> buildSingleElementListIterable() { + return new SingleElementListIterable<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/iterables/SnapshotCloneIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneIterableTests.java index e90d996b4c..fd187a8a81 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneIterableTests.java @@ -9,79 +9,63 @@ ******************************************************************************/ package org.eclipse.jpt.utility.tests.internal.iterables; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -import junit.framework.TestCase; +import java.util.List; import org.eclipse.jpt.utility.internal.CollectionTools; import org.eclipse.jpt.utility.internal.iterables.SnapshotCloneIterable; @SuppressWarnings("nls") -public class SnapshotCloneIterableTests extends TestCase { +public class SnapshotCloneIterableTests extends CloneIterableTests { public SnapshotCloneIterableTests(String name) { super(name); } + @Override public void testIterator() { - Collection<String> c = new ArrayList<String>(); - c.add("0"); - c.add("1"); - c.add("2"); - c.add("3"); - assertEquals(4, c.size()); - Iterable<String> iterable = new SnapshotCloneIterable<String>(c); + super.testIterator(); + // "snapshot" iterable should still return 4 strings (since the original collection was cloned) int i = 0; - for (String s : iterable) { - assertEquals(String.valueOf(i++), s); - c.remove("3"); - } - assertEquals(4, i); - assertEquals(3, c.size()); - - // iterable should still return only 4 strings (since the original collection was cloned) - i = 0; - for (String s : iterable) { + for (String s : this.iterable) { assertEquals(String.valueOf(i++), s); } assertEquals(4, i); } + @Override public void testRemove() { - final Collection<String> collection = this.buildCollection(); - Iterable<String> iterable = new SnapshotCloneIterable<String>(collection) { - @Override - protected void remove(String current) { - collection.remove(current); - } - }; + super.testRemove(); + // "snapshot" clone iterable will still contain the element removed from the + // original collection + assertTrue(CollectionTools.contains(this.iterable, "three")); + } - Object removed = "three"; - assertTrue(CollectionTools.contains(iterable, removed)); - for (Iterator<String> iterator = iterable.iterator(); iterator.hasNext(); ) { - if (iterator.next().equals(removed)) { - iterator.remove(); - } - } - assertFalse(collection.contains(removed)); - // "static" clone iterable will still contain the element removed from the + @Override + public void testRemover() { + super.testRemover(); + // "snapshot" clone iterable will still contain the element removed from the // original collection - assertTrue(CollectionTools.contains(iterable, "three")); + assertTrue(CollectionTools.contains(this.iterable, "three")); + } + + @Override + Iterable<String> buildIterable(List<String> c) { + return new SnapshotCloneIterable<String>(c); + } + + @Override + Iterable<String> buildRemovingIterable(final List<String> c) { + return new SnapshotCloneIterable<String>(c) { + @Override + protected void remove(String current) { + c.remove(current); + } + }; } - private Collection<String> buildCollection() { - Collection<String> c = new ArrayList<String>(); - c.add("one"); - c.add("two"); - c.add("three"); - c.add("four"); - c.add("five"); - c.add("six"); - c.add("seven"); - c.add("eight"); - return c; + @Override + Iterable<String> buildIterableWithRemover(List<String> c) { + return new SnapshotCloneIterable<String>(c, this.buildRemover(c)); } } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneListIterableTests.java new file mode 100644 index 0000000000..426670f050 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/SnapshotCloneListIterableTests.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.List; +import java.util.ListIterator; + +import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.iterables.SnapshotCloneListIterable; + +@SuppressWarnings("nls") +public class SnapshotCloneListIterableTests extends SnapshotCloneIterableTests { + + public SnapshotCloneListIterableTests(String name) { + super(name); + } + + public void testAdd() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildRemovingIterable(collection); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("two")) { + iterator.add(added); + } + } + assertTrue(collection.contains(added)); + // "snapshot" clone iterable not will contain the element added to the + // original collection + assertFalse(CollectionTools.contains(this.iterable, added)); + } + + public void testMissingMutatorAdd() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(this.iterable.toString()); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + boolean exCaught = false; + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("three")) { + try { + iterator.add(added); + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + public void testSet() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildRemovingIterable(collection); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + assertTrue(CollectionTools.contains(this.iterable, "two")); + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("two")) { + iterator.set(added); + } + } + assertTrue(collection.contains(added)); + assertFalse(collection.contains("two")); + // "snapshot" clone iterable will not be changed + assertFalse(CollectionTools.contains(this.iterable, added)); + assertTrue(CollectionTools.contains(this.iterable, "two")); + } + + public void testMissingMutatorSet() { + final List<String> collection = this.buildCollection(); + this.iterable = this.buildIterable(collection); + assertNotNull(this.iterable.toString()); + + String added = "xxxx"; + assertFalse(CollectionTools.contains(this.iterable, added)); + boolean exCaught = false; + for (ListIterator<String> iterator = (ListIterator<String>) this.iterable.iterator(); iterator.hasNext(); ) { + if (iterator.next().equals("three")) { + try { + iterator.set(added); + fail(); + } catch (RuntimeException ex) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } + + @Override + Iterable<String> buildIterable(List<String> c) { + return new SnapshotCloneListIterable<String>(c); + } + + @Override + Iterable<String> buildRemovingIterable(final List<String> c) { + return new SnapshotCloneListIterable<String>(c) { + @Override + protected void add(int index, String element) { + c.add(index, element); + } + @Override + protected void remove(int index) { + c.remove(index); + } + @Override + protected void set(int index, String element) { + c.set(index, element); + } + }; + } + + @Override + Iterable<String> buildIterableWithRemover(List<String> c) { + return new SnapshotCloneListIterable<String>(c, this.buildMutator(c)); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/StackIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/StackIterableTests.java new file mode 100644 index 0000000000..750895042d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/StackIterableTests.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.SimpleStack; +import org.eclipse.jpt.utility.internal.Stack; +import org.eclipse.jpt.utility.internal.iterables.StackIterable; + +@SuppressWarnings("nls") +public class StackIterableTests extends TestCase { + + public StackIterableTests(String name) { + super(name); + } + + public void testIterator() { + Iterator<String> iterator = this.buildIterable().iterator(); + assertEquals("three", iterator.next()); + assertEquals("two", iterator.next()); + assertEquals("one", iterator.next()); + } + + public void testToString() { + assertNotNull(this.buildIterable().toString()); + } + + private Iterable<String> buildIterable() { + return new StackIterable<String>(this.buildStack()); + } + + private Stack<String> buildStack() { + Stack<String> stack = new SimpleStack<String>(); + stack.push("one"); + stack.push("two"); + stack.push("three"); + return stack; + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationIterableTests.java index 0b64c36c47..011942d978 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationIterableTests.java @@ -14,6 +14,7 @@ import java.util.Collection; import junit.framework.TestCase; +import org.eclipse.jpt.utility.internal.Transformer; import org.eclipse.jpt.utility.internal.iterables.TransformationIterable; @SuppressWarnings("nls") @@ -23,18 +24,18 @@ public class TransformationIterableTests extends TestCase { super(name); } - public void testTransform() { + public void testTransform1() { int i = 1; - for (Integer integer : this.buildIterable()) { + for (Integer integer : this.buildIterable1()) { assertEquals(i++, integer.intValue()); } } - private Iterable<Integer> buildIterable() { - return this.buildTransformationIterable(this.buildNestedIterable()); + private Iterable<Integer> buildIterable1() { + return this.buildTransformationIterable1(this.buildNestedIterable()); } - private Iterable<Integer> buildTransformationIterable(Iterable<String> nestedIterable) { + private Iterable<Integer> buildTransformationIterable1(Iterable<String> nestedIterable) { // transform each string into an integer with a value of the string's length return new TransformationIterable<String, Integer>(nestedIterable) { @Override @@ -44,6 +45,31 @@ public class TransformationIterableTests extends TestCase { }; } + public void testTransform2() { + int i = 1; + for (Integer integer : this.buildIterable2()) { + assertEquals(i++, integer.intValue()); + } + } + + private Iterable<Integer> buildIterable2() { + return this.buildTransformationIterable2(this.buildNestedIterable()); + } + + private Iterable<Integer> buildTransformationIterable2(Iterable<String> nestedIterable) { + // transform each string into an integer with a value of the string's length + return new TransformationIterable<String, Integer>(nestedIterable, this.buildTransformer()); + } + + 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 Iterable<String> buildNestedIterable() { Collection<String> c = new ArrayList<String>(); c.add("1"); @@ -57,4 +83,22 @@ public class TransformationIterableTests extends TestCase { return c; } + public void testToString() { + assertNotNull(this.buildIterable1().toString()); + } + + public void testMissingTransformer() { + Iterable<Integer> iterable = new TransformationIterable<String, Integer>(this.buildNestedIterable()); + boolean exCaught = false; + try { + int i = 1; + for (Integer integer : iterable) { + assertEquals(i++, integer.intValue()); + } + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationListIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationListIterableTests.java new file mode 100644 index 0000000000..684bcc8ec5 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TransformationListIterableTests.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2009 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.iterables; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.Transformer; +import org.eclipse.jpt.utility.internal.iterables.TransformationListIterable; + +@SuppressWarnings("nls") +public class TransformationListIterableTests extends TestCase { + + public TransformationListIterableTests(String name) { + super(name); + } + + public void testTransform1() { + int i = 1; + for (Integer integer : this.buildIterable1()) { + assertEquals(i++, integer.intValue()); + } + } + + private Iterable<Integer> buildIterable1() { + return this.buildTransformationListIterable1(this.buildNestedList()); + } + + private Iterable<Integer> buildTransformationListIterable1(List<String> nestedList) { + // transform each string into an integer with a value of the string's length + return new TransformationListIterable<String, Integer>(nestedList) { + @Override + protected Integer transform(String next) { + return new Integer(next.length()); + } + }; + } + + public void testTransform2() { + int i = 1; + for (Integer integer : this.buildIterable2()) { + assertEquals(i++, integer.intValue()); + } + } + + private Iterable<Integer> buildIterable2() { + return this.buildTransformationListIterable2(this.buildNestedList()); + } + + private Iterable<Integer> buildTransformationListIterable2(List<String> nestedList) { + // transform each string into an integer with a value of the string's length + return new TransformationListIterable<String, Integer>(nestedList, this.buildTransformer()); + } + + 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 List<String> buildNestedList() { + List<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; + } + + public void testToString() { + assertNotNull(this.buildIterable1().toString()); + } + + public void testMissingTransformer() { + Iterable<Integer> iterable = new TransformationListIterable<String, Integer>(this.buildNestedList()); + boolean exCaught = false; + try { + int i = 1; + for (Integer integer : iterable) { + assertEquals(i++, integer.intValue()); + } + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TreeIterableTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TreeIterableTests.java index 0188db252f..e01c440e24 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TreeIterableTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/iterables/TreeIterableTests.java @@ -16,6 +16,7 @@ import java.util.Iterator; import junit.framework.TestCase; import org.eclipse.jpt.utility.internal.iterables.TreeIterable; +import org.eclipse.jpt.utility.internal.iterators.TreeIterator; import org.eclipse.jpt.utility.tests.internal.TestTools; @SuppressWarnings("nls") @@ -33,13 +34,47 @@ public class TreeIterableTests extends TestCase { super.tearDown(); } - public void testIterator() { - for (TreeNode tn : this.buildTreeIterable()) { + public void testIterator1() { + for (TreeNode tn : this.buildTreeIterable1()) { assertTrue(this.nodes.contains(tn)); } } - private Iterable<TreeNode> buildTreeIterable() { + public void testIterator2() { + for (TreeNode tn : this.buildTreeIterable2()) { + assertTrue(this.nodes.contains(tn)); + } + } + + public void testMidwife1() { + for (TreeNode tn : new TreeIterable<TreeNode>(this.buildTree(), this.buildMidwife())) { + assertTrue(this.nodes.contains(tn)); + } + } + + public void testMidwife2() { + for (TreeNode tn : new TreeIterable<TreeNode>(new TreeNode[] { this.buildTree() }, this.buildMidwife())) { + assertTrue(this.nodes.contains(tn)); + } + } + + public void testToString() { + assertNotNull(this.buildTreeIterable1().toString()); + } + + public void testMissingMidwife() { + boolean exCaught = false; + try { + for (TreeNode tn : new TreeIterable<TreeNode>(this.buildTree())) { + assertTrue(this.nodes.contains(tn)); + } + } catch (RuntimeException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + private Iterable<TreeNode> buildTreeIterable1() { return new TreeIterable<TreeNode>(this.buildTree()) { @Override public Iterator<TreeNode> children(TreeNode next) { @@ -48,6 +83,23 @@ public class TreeIterableTests extends TestCase { }; } + private Iterable<TreeNode> buildTreeIterable2() { + return new TreeIterable<TreeNode>(new TreeNode[] { this.buildTree() }) { + @Override + public Iterator<TreeNode> children(TreeNode next) { + return next.children(); + } + }; + } + + private TreeIterator.Midwife<TreeNode> buildMidwife() { + return new TreeIterator.Midwife<TreeNode>() { + public Iterator<TreeNode> children(TreeNode next) { + return next.children(); + } + }; + } + private TreeNode buildTree() { TreeNode root = new TreeNode("root"); TreeNode child1 = new TreeNode(root, "child 1"); |