/eclipse-wtp-jpa-3.4.0/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/iterators/TransformationIteratorTests.java

# · Java · 230 lines · 188 code · 29 blank · 13 comment · 16 complexity · 848e773c2f413d8766f7b986ee7cbd0f MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2005, 2009 Oracle. All rights reserved.
  3. * This program and the accompanying materials are made available under the
  4. * terms of the Eclipse Public License v1.0, which accompanies this distribution
  5. * and is available at http://www.eclipse.org/legal/epl-v10.html.
  6. *
  7. * Contributors:
  8. * Oracle - initial API and implementation
  9. ******************************************************************************/
  10. package org.eclipse.jpt.common.utility.tests.internal.iterators;
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.Collections;
  14. import java.util.Iterator;
  15. import java.util.NoSuchElementException;
  16. import junit.framework.TestCase;
  17. import org.eclipse.jpt.common.utility.internal.Transformer;
  18. import org.eclipse.jpt.common.utility.internal.iterators.TransformationIterator;
  19. @SuppressWarnings("nls")
  20. public class TransformationIteratorTests extends TestCase {
  21. public TransformationIteratorTests(String name) {
  22. super(name);
  23. }
  24. public void testHasNext() {
  25. int i = 0;
  26. for (Iterator<Integer> stream = this.buildIterator(); stream.hasNext();) {
  27. stream.next();
  28. i++;
  29. }
  30. assertEquals(8, i);
  31. }
  32. public void testHasNextUpcast() {
  33. int i = 0;
  34. for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) {
  35. stream.next();
  36. i++;
  37. }
  38. assertEquals(8, i);
  39. }
  40. public void testInnerHasNext() {
  41. int i = 0;
  42. for (Iterator<Integer> stream = this.buildInnerIterator(); stream.hasNext();) {
  43. stream.next();
  44. i++;
  45. }
  46. assertEquals(8, i);
  47. }
  48. public void testNext() {
  49. int i = 0;
  50. for (Iterator<Integer> stream = this.buildIterator(); stream.hasNext();) {
  51. assertEquals("bogus transformation", ++i, stream.next().intValue());
  52. }
  53. }
  54. public void testNextUpcast() {
  55. int i = 0;
  56. for (Iterator<Object> stream = this.buildIteratorUpcast(); stream.hasNext();) {
  57. assertEquals("bogus transformation", ++i, ((Integer) stream.next()).intValue());
  58. }
  59. }
  60. public void testInnerNext() {
  61. int i = 0;
  62. for (Iterator<Integer> stream = this.buildInnerIterator(); stream.hasNext();) {
  63. assertEquals("bogus transformation", ++i, stream.next().intValue());
  64. }
  65. }
  66. public void testRemove() {
  67. Collection<String> c = this.buildCollection();
  68. for (Iterator<Integer> stream = this.buildInnerTransformationIterator(c.iterator()); stream.hasNext();) {
  69. if (stream.next().intValue() == 3) {
  70. stream.remove();
  71. }
  72. }
  73. assertEquals("nothing removed", this.buildCollection().size() - 1, c.size());
  74. assertFalse("element still in collection", c.contains("333"));
  75. assertTrue("wrong element removed", c.contains("22"));
  76. }
  77. public void testInnerRemove() {
  78. Collection<String> c = this.buildCollection();
  79. for (Iterator<Integer> stream = this.buildTransformationIterator(c.iterator(), this.buildTransformer()); stream.hasNext();) {
  80. if (stream.next().intValue() == 3) {
  81. stream.remove();
  82. }
  83. }
  84. assertEquals("nothing removed", this.buildCollection().size() - 1, c.size());
  85. assertFalse("element still in collection", c.contains("333"));
  86. assertTrue("wrong element removed", c.contains("22"));
  87. }
  88. public void testNoSuchElementException() {
  89. boolean exCaught = false;
  90. Iterator<Integer> stream = this.buildIterator();
  91. Integer integer = null;
  92. while (stream.hasNext()) {
  93. integer = stream.next();
  94. }
  95. try {
  96. integer = stream.next();
  97. } catch (NoSuchElementException ex) {
  98. exCaught = true;
  99. }
  100. assertTrue("NoSuchElementException not thrown: " + integer, exCaught);
  101. }
  102. public void testUnsupportedOperationException() {
  103. boolean exCaught = false;
  104. for (Iterator<Integer> stream = this.buildUnmodifiableIterator(); stream.hasNext();) {
  105. int i = stream.next().intValue();
  106. if (i == 3) {
  107. try {
  108. stream.remove();
  109. } catch (UnsupportedOperationException ex) {
  110. exCaught = true;
  111. }
  112. }
  113. }
  114. assertTrue("UnsupportedOperationException not thrown", exCaught);
  115. }
  116. public void testIllegalStateException() {
  117. boolean exCaught = false;
  118. try {
  119. this.buildIterator().remove();
  120. } catch (IllegalStateException ex) {
  121. exCaught = true;
  122. }
  123. assertTrue("IllegalStateException not thrown", exCaught);
  124. }
  125. private Iterator<Integer> buildIterator() {
  126. return this.buildTransformationIterator(this.buildNestedIterator(), this.buildTransformer());
  127. }
  128. private Iterator<Object> buildIteratorUpcast() {
  129. return this.buildTransformationIteratorUpcast(this.buildNestedIterator(), this.buildTransformerUpcast());
  130. }
  131. private Iterator<Integer> buildInnerIterator() {
  132. return this.buildInnerTransformationIterator(this.buildNestedIterator());
  133. }
  134. private Iterator<Integer> buildUnmodifiableIterator() {
  135. return this.buildTransformationIterator(this.buildUnmodifiableNestedIterator(), this.buildTransformer());
  136. }
  137. private Iterator<Integer> buildTransformationIterator(Iterator<String> nestedIterator, Transformer<String, Integer> transformer) {
  138. return new TransformationIterator<String, Integer>(nestedIterator, transformer);
  139. }
  140. private Iterator<Object> buildTransformationIteratorUpcast(Iterator<String> nestedIterator, Transformer<Object, Integer> transformer) {
  141. return new TransformationIterator<Object, Object>(nestedIterator, transformer);
  142. }
  143. private Transformer<String, Integer> buildTransformer() {
  144. // transform each string into an integer with a value of the string's length
  145. return new Transformer<String, Integer>() {
  146. public Integer transform(String next) {
  147. return new Integer(next.length());
  148. }
  149. };
  150. }
  151. private Transformer<Object, Integer> buildTransformerUpcast() {
  152. // transform each string into an integer with a value of the string's length
  153. return new Transformer<Object, Integer>() {
  154. public Integer transform(Object next) {
  155. return new Integer(((String) next).length());
  156. }
  157. };
  158. }
  159. private Iterator<Integer> buildInnerTransformationIterator(Iterator<String> nestedIterator) {
  160. // transform each string into an integer with a value of the string's length
  161. return new TransformationIterator<String, Integer>(nestedIterator) {
  162. @Override
  163. protected Integer transform(String next) {
  164. return new Integer(next.length());
  165. }
  166. };
  167. }
  168. private Iterator<String> buildNestedIterator() {
  169. return this.buildCollection().iterator();
  170. }
  171. private Iterator<String> buildUnmodifiableNestedIterator() {
  172. return this.buildUnmodifiableCollection().iterator();
  173. }
  174. private Collection<String> buildCollection() {
  175. Collection<String> c = new ArrayList<String>();
  176. c.add("1");
  177. c.add("22");
  178. c.add("333");
  179. c.add("4444");
  180. c.add("55555");
  181. c.add("666666");
  182. c.add("7777777");
  183. c.add("88888888");
  184. return c;
  185. }
  186. private Collection<String> buildUnmodifiableCollection() {
  187. return Collections.unmodifiableCollection(this.buildCollection());
  188. }
  189. public void testInvalidTransformationIterator() {
  190. // missing method override
  191. Iterator<Integer> iterator = new TransformationIterator<String, Integer>(this.buildCollection().iterator());
  192. boolean exCaught = false;
  193. try {
  194. Integer integer = iterator.next();
  195. fail("invalid integer: " + integer);
  196. } catch (UnsupportedOperationException ex) {
  197. exCaught = true;
  198. }
  199. assertTrue("NoSuchElementException not thrown", exCaught);
  200. }
  201. }