Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8916063a5f0ff5f42a53cf35063d0f1f48e2092c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 * Copyright (c) 2005, 2013 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.common.utility.tests.internal.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.jpt.common.utility.internal.iterator.TransformationIterator;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerTools;
import org.eclipse.jpt.common.utility.transformer.Transformer;
import junit.framework.TestCase;

@SuppressWarnings("nls")
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 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 testRemove() {
		Collection<String> c = this.buildCollection();
		for (Iterator<Integer> stream = this.buildTransformationIterator(c.iterator(), STRING_LENGTH_TRANSFORMER); 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(), STRING_LENGTH_TRANSFORMER);
	}

	private Iterator<Object> buildIteratorUpcast() {
		return this.buildTransformationIteratorUpcast(this.buildNestedIterator(), OBJECT_STRING_LENGTH_TRANSFORMER);
	}

	private Iterator<Integer> buildUnmodifiableIterator() {
		return this.buildTransformationIterator(this.buildUnmodifiableNestedIterator(), STRING_LENGTH_TRANSFORMER);
	}

	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 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());
	}

	public void testInvalidTransformationIterator() {
		// missing method override
		Iterator<Integer> iterator = new TransformationIterator<String, Integer>(this.buildCollection().iterator(), TransformerTools.disabledTransformer());
		boolean exCaught = false;
		try {
			Integer integer = iterator.next();
			fail("invalid integer: " + integer);
		} catch (UnsupportedOperationException ex) {
			exCaught = true;
		}
		assertTrue("NoSuchElementException not thrown", exCaught);
	}

	static final Transformer<String, Integer> STRING_LENGTH_TRANSFORMER = new StringLengthTransformer();
	/**
	 * transform each string into an integer with a value of the string's length
	 */
	static class StringLengthTransformer
		extends TransformerAdapter<String, Integer>
	{
		@Override
		public Integer transform(String s) {
			return new Integer(s.length());
		}
	}

	static final Transformer<Object, Integer> OBJECT_STRING_LENGTH_TRANSFORMER = new ObjectStringLengthTransformer();
	/**
	 * transform each string into an integer with a value of the string's length
	 */
	static class ObjectStringLengthTransformer
		extends TransformerAdapter<Object, Integer>
	{
		@Override
		public Integer transform(Object input) {
			return new Integer(((String) input).length());
		}
	}
}

Back to the top