Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3b8c5b1123495a2fb8b975e5fac9603a664a97db (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
/*******************************************************************************
 *  Copyright (c) 2008, 2017 IBM Corporation and others.
 *  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:
 *     IBM Corporation - initial API and implementation
 *     EclipseSource - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.core;

import java.util.*;
import org.eclipse.equinox.p2.query.*;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

/**
 * Tests for {@link Collector}.
 */
public class CollectorTest extends AbstractProvisioningTest {
	public void testAccept() {
		Collector collector = new Collector();
		String value = "value";
		collector.accept(value);
		Object[] result = collector.toArray(Object.class);
		assertEquals("1.0", 1, result.length);
		assertEquals("1.1", value, result[0]);

		//adding a second copy of the same object is rejected
		collector.accept(new String(value));
		result = collector.toArray(Object.class);
		assertEquals("1.0", 1, result.length);
		assertEquals("1.1", value, result[0]);
	}

	public void testIsEmpty() {
		Collector collector = new Collector();
		assertEquals("1.0", true, collector.isEmpty());
		collector.accept("value");
		assertEquals("1.0", false, collector.isEmpty());
	}

	/**
	 * This tests the query method on the collector.
	 */
	public void testCompositeCollectors() {
		String[] s = new String[] {"A", "B", "C", "D", "E", "F", "G", "1", "2", "3", "4", "5", "6", "7"};
		List list = Arrays.asList(s);
		IQuery numeric = new MatchQuery() {

			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).compareTo("0") > 0 && ((String) candidate).compareTo("8") < 0) {
					return true;
				}
				return false;
			}
		};

		IQuery fourOrFiveOrABC = new MatchQuery() {
			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).equals("4") || ((String) candidate).equals("5") || ((String) candidate).equals("A") || ((String) candidate).equals("B") || ((String) candidate).equals("C")) {
					return true;
				}
				return false;
			}
		};
		IQueryResult queryResult = numeric.perform(list.iterator());
		assertEquals("1.0", 7, queryResultSize(queryResult));

		queryResult = queryResult.query(fourOrFiveOrABC, null);
		assertEquals("2.0", 2, queryResultSize(queryResult));
		assertContains("2.1", queryResult, "4");
		assertContains("2.2", queryResult, "5");
	}

	public void testSameCollector() {
		String[] s = new String[] {"A", "B", "C", "D", "E", "F", "G", "1", "2", "3", "4", "5", "6", "7"};
		List list = Arrays.asList(s);
		IQuery numeric = new MatchQuery() {

			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).compareTo("0") > 0 && ((String) candidate).compareTo("8") < 0) {
					return true;
				}
				return false;
			}
		};

		IQuery fourOrFiveOrABC = new MatchQuery() {
			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).equals("4") || ((String) candidate).equals("5") || ((String) candidate).equals("A") || ((String) candidate).equals("B") || ((String) candidate).equals("C")) {
					return true;
				}
				return false;
			}
		};
		Collector collector = new Collector();
		collector.addAll(numeric.perform(list.iterator()));
		assertEquals("1.0", 7, collector.toUnmodifiableSet().size());

		collector.addAll(collector.query(fourOrFiveOrABC, null));
		Collection collection = collector.toUnmodifiableSet();
		assertEquals("2.0", 7, collection.size());
	}

	/**
	 * This tests the query method on the collector.
	 */
	public void testEmptyCompositeCollectors() {
		String[] s = new String[] {"A", "B", "C", "D", "E", "F", "G", "1", "2", "3", "4", "5", "6", "7"};
		List list = Arrays.asList(s);
		IQuery eightOrNine = new MatchQuery() {

			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).compareTo("8") > 0 && ((String) candidate).compareTo("9") < 0) {
					return true;
				}
				return false;
			}
		};

		IQuery fourOrFiveOrABC = new MatchQuery() {
			@Override
			public boolean isMatch(Object candidate) {
				if (((String) candidate).equals("4") || ((String) candidate).equals("5") || ((String) candidate).equals("A") || ((String) candidate).equals("B") || ((String) candidate).equals("C")) {
					return true;
				}
				return false;
			}
		};
		IQueryResult queryResult = eightOrNine.perform(list.iterator());
		assertTrue("1.0", queryResult.isEmpty());

		queryResult = queryResult.query(fourOrFiveOrABC, null);
		assertTrue("2.0", queryResult.isEmpty());
	}

	public void testToCollection() {
		Collector collector = new Collector();
		Collection result = collector.toUnmodifiableSet();
		assertEquals("1.0", 0, result.size());
		//collection should be immutable
		try {
			result.add("value");
			fail("1.1");
		} catch (RuntimeException e) {
			//expected
		}

		String value = "value";
		collector.accept(value);
		result = collector.toUnmodifiableSet();
		assertEquals("2.0", 1, result.size());
		assertEquals("2.1", value, result.iterator().next());
		//collection should be immutable
		try {
			result.clear();
			fail("2.2");
		} catch (RuntimeException e) {
			//expected
		}

	}
}

Back to the top