Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2cfbb371fb3ab27e1f9468040ddf3d3c192ba8ea (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.builder;

import static org.junit.Assert.assertArrayEquals;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.core.builder.ReferenceCollection;

import junit.framework.Test;

public class ReferenceCollectionTest extends BuilderTests {

	public ReferenceCollectionTest(String name) {
		super(name);
	}

	public static Test suite() {
		return buildTestSuite(ReferenceCollectionTest.class);
	}

	private static class TestableReferenceCollection extends ReferenceCollection {
		/*
		    Make the package visible fields reflectively available for testing
			char[][][] qualifiedNameReferences;
			char[][] simpleNameReferences;
			char[][] rootReferences;
		 */
		protected TestableReferenceCollection(char[][][] qualifiedNameReferences, char[][] simpleNameReferences,
				char[][] rootReferences) {
			super(qualifiedNameReferences, simpleNameReferences, rootReferences);
		}

		char[][][] getQualifiedNameReferences() {
			try {
				Field fld = ReferenceCollection.class.getDeclaredField("qualifiedNameReferences");
				fld.setAccessible(true);
				return (char[][][]) fld.get(this);
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}

		char[][] getSimpleNameReferences() {
			try {
				Field fld = ReferenceCollection.class.getDeclaredField("simpleNameReferences");
				fld.setAccessible(true);
				return (char[][]) fld.get(this);
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}

		char[][] getRootReferences() {
			try {
				Field fld = ReferenceCollection.class.getDeclaredField("rootReferences");
				fld.setAccessible(true);
				return (char[][]) fld.get(this);
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
	}

	// Disabled due bug 545491 comment 15
	public void XtestInternQualifiedNamesSorts_01() {
		char[][][] qualifiedNames = new char[][][] {
			CharOperation.splitOn('.', "java.lang.RuntimeException".toCharArray()),
			CharOperation.splitOn('.', "a.a.a".toCharArray()),
			CharOperation.splitOn('.', "b.b.b".toCharArray()),
			CharOperation.splitOn('.', "a.a".toCharArray()),
			CharOperation.splitOn('.', "a.b".toCharArray()),
			CharOperation.splitOn('.', "com".toCharArray()),
			CharOperation.splitOn('.', "a".toCharArray()),
			CharOperation.splitOn('.', "b".toCharArray())
		};
		char[][][] expectation = qualifiedNames.clone();
		Collections.shuffle(Arrays.asList(qualifiedNames));
		char[][][] internQualifiedNames = ReferenceCollection.internQualifiedNames(qualifiedNames, true);
		assertArrayEquals("Should be sorted, longest first, alphanumeric later",
				toStringArray(expectation),
				toStringArray(internQualifiedNames));
	}

	// Disabled due bug 545491 comment 15
	public void XtestInternQualifiedNamesSorts_02() {
		char[][][] qualifiedNames = new char[][][] {
			CharOperation.splitOn('.', "java.lang.RuntimeException".toCharArray()),
			CharOperation.splitOn('.', "a.a.a".toCharArray()),
			CharOperation.splitOn('.', "b.b.b".toCharArray()),
			CharOperation.splitOn('.', "a.a".toCharArray()),
			CharOperation.splitOn('.', "a.b".toCharArray()),
			CharOperation.splitOn('.', "com".toCharArray()),
			CharOperation.splitOn('.', "a".toCharArray()),
			CharOperation.splitOn('.', "b".toCharArray())
		};
		char[][][] expectation = new char[][][] {
			CharOperation.splitOn('.', "a.a.a".toCharArray()),
			CharOperation.splitOn('.', "b.b.b".toCharArray()),
			CharOperation.splitOn('.', "a.a".toCharArray()),
			CharOperation.splitOn('.', "a.b".toCharArray()),
			CharOperation.splitOn('.', "a".toCharArray()),
			CharOperation.splitOn('.', "b".toCharArray())
		};
		Collections.shuffle(Arrays.asList(qualifiedNames));
		char[][][] internQualifiedNames = ReferenceCollection.internQualifiedNames(qualifiedNames, false);
		assertArrayEquals("Should be sorted, longest first, alphanumeric later",
				toStringArray(expectation),
				toStringArray(internQualifiedNames));
	}

	// Disabled due bug 545491 comment 15
	public void XtestInternSimpleNamesSorts_01() {
		char[][] simpleNames = new char[][] {
			"Throwable".toCharArray(),
			"aaa".toCharArray(),
			"bbb".toCharArray(),
			"ccc".toCharArray(),
			"aa".toCharArray(),
			"a".toCharArray()
		};
		char[][] expectation = simpleNames.clone();
		Collections.shuffle(Arrays.asList(simpleNames));
		char[][] internSimpleNames = ReferenceCollection.internSimpleNames(simpleNames, false);
		assertArrayEquals("Should be sorted, longest first, alphanumeric later",
				toStringArray(expectation),
				toStringArray(internSimpleNames));
	}

	// Disabled due bug 545491 comment 15
	public void XtestInternSimpleNamesSorts_02() {
		char[][] simpleNames = new char[][] {
			"aaa".toCharArray(),
			"bbb".toCharArray(),
			"Throwable".toCharArray(),
			"ccc".toCharArray(),
			"java".toCharArray(),
			"aa".toCharArray(),
			"a".toCharArray()
		};
		char[][] expectation = new char[][] {
			"aaa".toCharArray(),
			"bbb".toCharArray(),
			"ccc".toCharArray(),
			"aa".toCharArray(),
			"a".toCharArray()
		};
		Collections.shuffle(Arrays.asList(simpleNames));
		char[][] internSimpleNames = ReferenceCollection.internSimpleNames(simpleNames, true);
		assertArrayEquals("Should be sorted, longest first, alphanumeric later",
				toStringArray(expectation),
				toStringArray(internSimpleNames));
	}

	public void testIncludesWithBinarySearch() {
		char[][] simpleNames = ReferenceCollection.internSimpleNames(new char[][] {
			"a".toCharArray(),
			"b".toCharArray(),
			"c".toCharArray(),
			"d".toCharArray(),
			"e".toCharArray(),
			"f".toCharArray(),
			"g".toCharArray(),
			"h".toCharArray(),
			"i".toCharArray(),
			"j".toCharArray(),
			"k".toCharArray(),
			"l".toCharArray(),
			"m".toCharArray(),
			"n".toCharArray(),
			"o".toCharArray(),
			"p".toCharArray(),
			"q".toCharArray(),
			"r".toCharArray(),
			"s".toCharArray(),
			"t".toCharArray(),
			"u".toCharArray(),
			"v".toCharArray(),
			"w".toCharArray(),
			"x".toCharArray(),
			"y".toCharArray(),
			"z".toCharArray()
		}, false);
		ReferenceCollection collection = new TestableReferenceCollection(null, simpleNames, null);
		for(char[] simpleName: simpleNames) {
			assertTrue("Should include " + simpleName[0], collection.includes(simpleName));
			assertFalse("Should not include " + CharOperation.toUpperCase(simpleName)[0], collection.includes(CharOperation.toUpperCase(simpleName)));
		}
	}

	public void testIncludesWithLinearSearch() {
		char[][] simpleNames = ReferenceCollection.internSimpleNames(new char[][] {
			"a".toCharArray(),
			"b".toCharArray(),
			"c".toCharArray(),
			"d".toCharArray(),
			"e".toCharArray(),
			"f".toCharArray(),
			"g".toCharArray(),
			"h".toCharArray()
		}, false);
		ReferenceCollection collection = new TestableReferenceCollection(null, simpleNames, null);
		for(char[] simpleName: simpleNames) {
			assertTrue("Should include " + simpleName[0], collection.includes(simpleName));
			assertFalse("Should not include " + CharOperation.toUpperCase(simpleName)[0], collection.includes(CharOperation.toUpperCase(simpleName)));
		}
	}

	public void testIncludes01() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] { "a.a", "b.a", "b.b" };
		refColl.addDependencies(array);

		TestableReferenceCollection other = new TestableReferenceCollection(null, null, null);
		String[] array2 = new String[] { "a.a", "B.A", "b.b" };
		other.addDependencies(array2);

		assertTrue(refColl.includes(other.getQualifiedNameReferences(), other.getSimpleNameReferences(), other.getRootReferences()));
		assertTrue(other.includes(refColl.getQualifiedNameReferences(), refColl.getSimpleNameReferences(), refColl.getRootReferences()));
	}

	public void testIncludes02() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] { "a.x", "b.y" };
		refColl.addDependencies(array);

		TestableReferenceCollection other = new TestableReferenceCollection(null, null, null);
		String[] array2 = new String[] { "a.y", "b.x" };
		other.addDependencies(array2);

		assertTrue(refColl.includes(other.getQualifiedNameReferences(), other.getSimpleNameReferences(), other.getRootReferences()));
		assertTrue(other.includes(refColl.getQualifiedNameReferences(), refColl.getSimpleNameReferences(), refColl.getRootReferences()));
	}

	public void testIncludes03() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] { "a.d.y", "c.x" };
		refColl.addDependencies(array);

		TestableReferenceCollection other = new TestableReferenceCollection(null, null, null);
		String[] array2 = new String[] { "c.y" };
		other.addDependencies(array2);

		assertTrue(refColl.includes(other.getQualifiedNameReferences(), other.getSimpleNameReferences(), other.getRootReferences()));
		assertTrue(other.includes(refColl.getQualifiedNameReferences(), refColl.getSimpleNameReferences(), refColl.getRootReferences()));
	}

	public void testIncludes04() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] { "a.d.y" };
		refColl.addDependencies(array);

		TestableReferenceCollection other = new TestableReferenceCollection(null, null, null);
		String[] array2 = new String[] { "a.d" };
		other.addDependencies(array2);

		assertTrue(refColl.includes(other.getQualifiedNameReferences(), other.getSimpleNameReferences(), other.getRootReferences()));
		assertTrue(other.includes(refColl.getQualifiedNameReferences(), refColl.getSimpleNameReferences(), refColl.getRootReferences()));
	}

	public void testIncludesNot() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] {"b.a"};
		refColl.addDependencies(array);

		TestableReferenceCollection other = new TestableReferenceCollection(null, null, null);
		String[] array2 = new String[] {"B.A"};
		other.addDependencies(array2);

		assertFalse(refColl.includes(other.getQualifiedNameReferences(), other.getSimpleNameReferences(), other.getRootReferences()));
		assertFalse(other.includes(refColl.getQualifiedNameReferences(), refColl.getSimpleNameReferences(), refColl.getRootReferences()));
	}

	// Disabled due bug 545491 comment 15
	public void XtestAddDependencies() {
		TestableReferenceCollection refColl = new TestableReferenceCollection(null, null, null);

		String[] array = new String[] {"a.b.c.D"};
		refColl.addDependencies(array);

		char[][][] qualifiedNameReferences = refColl.getQualifiedNameReferences();
		String [] strings = toStringArray(qualifiedNameReferences);
		assertArrayEquals(new String[] {
			"a.b.c.D",
			"a.b.c",
			"a.b",
			"a"
		}, strings);

		char[][] simpleNameReferences = refColl.getSimpleNameReferences();
		assertArrayEquals(new String[] {
			"D",
			"a",
			"b",
			"c"
		}, CharOperation.toStrings(simpleNameReferences));

		char[][] rootReferences = refColl.getRootReferences();
		assertArrayEquals(new String[] {
			"a"
		}, CharOperation.toStrings(rootReferences));
	}

	private static String[] toStringArray(char[][][] qualifiedNameReferences) {
		return Arrays.stream(qualifiedNameReferences).map(a -> CharOperation.toString(a)).toArray(String[]::new);
	}

	private static String[] toStringArray(char[][] qualifiedNameReferences) {
		return Arrays.stream(qualifiedNameReferences).map(a -> CharOperation.charToString(a)).toArray(String[]::new);
	}

	public void testRegression01() {
		char[][][] qualifiedNames = new char[][][] {
			CharOperation.splitOn('.', "p1.IX".toCharArray()),
			CharOperation.splitOn('.', "p2.X".toCharArray())
		};
		char[][] simpleNames = new char[][] {
			"I__X".toCharArray(), "IX".toCharArray(), "p1".toCharArray(), "p2".toCharArray(), "X".toCharArray()
		};
		char[][] rootNames = new char[][] {
			"java".toCharArray(), "IX".toCharArray(), "p1".toCharArray(), "p2".toCharArray()
		};
		ReferenceCollection collection = new TestableReferenceCollection(qualifiedNames, simpleNames, rootNames);
		qualifiedNames = ReferenceCollection.internQualifiedNames(new char[][][] {
			CharOperation.splitOn('.', "p2".toCharArray())
		});
		simpleNames = ReferenceCollection.internSimpleNames(new char[][] {
			"X".toCharArray()
		}, true);
		rootNames = ReferenceCollection.internSimpleNames(new char[][] {
			"p2".toCharArray()
		}, false);

		assertTrue("Should include", collection.includes(qualifiedNames, simpleNames, rootNames));
	}

	public void testRegression02() {
		char[][][] qualifiedNames = new char[][][] {};
		char[][] simpleNames = new char[][] {
			"GeneratedAnnotation".toCharArray(), "Test".toCharArray()
		};
		char[][] rootNames = new char[][] {
			"java".toCharArray(), "GeneratedAnnotation".toCharArray()
		};
		ReferenceCollection collection = new TestableReferenceCollection(qualifiedNames, simpleNames, rootNames);

		qualifiedNames = null;
		simpleNames = ReferenceCollection.internSimpleNames(new char[][] {
			"GeneratedAnnotation".toCharArray()
		}, true);
		rootNames = ReferenceCollection.internSimpleNames(new char[][] {
			"GeneratedAnnotation".toCharArray()
		}, false);

		assertTrue("Should include", collection.includes(qualifiedNames, simpleNames, rootNames));
	}
}

Back to the top