Skip to main content
summaryrefslogtreecommitdiffstats
blob: d541d687db29bb8ea1562a469f57cf80bafc65fc (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.node;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.HashBag;
import org.eclipse.jpt.utility.internal.Range;
import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
import org.eclipse.jpt.utility.internal.node.AbstractNode;
import org.eclipse.jpt.utility.internal.node.Node;
import org.eclipse.jpt.utility.internal.node.Problem;
import org.eclipse.jpt.utility.tests.internal.TestTools;

import junit.framework.TestCase;

public class AbstractNodeTests extends TestCase {
	private TestWorkbenchModel root;

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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.root = this.buildRoot();
	}

	private TestWorkbenchModel buildRoot() {
		TestWorkbenchModel r = new RootTestWorkbenchModel("root");
			TestWorkbenchModel node1 = r.addTestChildNamed("node 1");
				TestWorkbenchModel node1_1 = node1.addTestChildNamed("node 1.1");
					node1_1.addTestChildNamed("node 1.1.1");
					node1_1.addTestChildNamed("node 1.1.2");
					node1_1.addTestChildNamed("node 1.1.3");
				node1.addTestChildNamed("node 1.2");
			TestWorkbenchModel node2 = r.addTestChildNamed("node 2");
				node2.addTestChildNamed("node 2.1");
				node2.addTestChildNamed("node 2.2");
			r.addTestChildNamed("node 3");
			r.addTestChildNamed("node 4");

		// mark the entire tree clean
		r.markEntireBranchClean();
		return r;
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testTestWorkbenchModel() {
		// make sure our test class works OK...
		assertNull(this.root.testChildNamed(""));
		assertNotNull(this.root.testChildNamed("node 1"));
		assertTrue(this.root.testChildNamed("node 1").isClean());
		assertTrue(this.root.testChildNamed("node 1").isCleanBranch());
		assertNotNull(this.root.testChildNamed("node 2"));
		assertTrue(this.root.testChildNamed("node 2").isClean());
		assertTrue(this.root.testChildNamed("node 2").isCleanBranch());
		assertNull(this.root.testChildNamed("node 2.1"));

		assertNull(this.root.testDescendantNamed(""));
		assertNotNull(this.root.testDescendantNamed("node 1"));
		assertNotNull(this.root.testDescendantNamed("node 2"));
		assertNotNull(this.root.testDescendantNamed("node 2.1"));
		assertTrue(this.root.testDescendantNamed("node 2.1").isClean());
		assertTrue(this.root.testDescendantNamed("node 2.1").isCleanBranch());
		assertNotNull(this.root.testDescendantNamed("node 1.1.3"));
		assertTrue(this.root.testDescendantNamed("node 1.1.3").isClean());
		assertTrue(this.root.testDescendantNamed("node 1.1.3").isCleanBranch());
	}

	public void testParentAndChildren() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		assertEquals("node 1.1.3", node.getName());
		assertEquals(0, CollectionTools.size(node.children()));

		node = (TestWorkbenchModel) node.getParent();
		assertEquals("node 1.1", node.getName());
		assertEquals(3, CollectionTools.size(node.children()));

		node = (TestWorkbenchModel) node.getParent();
		assertEquals("node 1", node.getName());
		assertEquals(2, CollectionTools.size(node.children()));

		node = (TestWorkbenchModel) node.getParent();
		assertEquals("root", node.getName());
		assertEquals(4, CollectionTools.size(node.children()));

		node = (TestWorkbenchModel) node.getParent();
		assertNull(node);
	}

	public void testDirty() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setSize(42);
		assertTrue(node.isDirty());

		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		assertTrue(parent.isClean());
		assertTrue(this.root.isClean());
	}

	public void testDirtyUnchangedAttribute() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setSize(42);
		assertTrue(node.isDirty());

		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		assertTrue(parent.isClean());
		assertTrue(this.root.isClean());

		this.root.markEntireBranchClean();
		// set size to same number - should stay clean
		node.setSize(42);
		assertTrue(node.isClean());
		assertTrue(parent.isClean());
		assertTrue(this.root.isClean());
	}

	public void testDirtyBranch() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setSize(42);
		assertTrue(node.isDirtyBranch());

		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		assertTrue(parent.isDirtyBranch());
		assertTrue(this.root.isDirtyBranch());

		parent.setSize(77);
		assertTrue(parent.isDirty());
		assertTrue(parent.isDirtyBranch());

		node.markEntireBranchClean();
		assertTrue(parent.isDirty());
		assertTrue(parent.isDirtyBranch());
	}

	public void testDirtyBranchCleanChildDirtyParent() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setSize(42);

		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		parent.setSize(77);
		assertTrue(parent.isDirty());
		assertTrue(parent.isDirtyBranch());

		// now, clean the child, but leave the parent dirty
		node.markEntireBranchClean();
		assertTrue(parent.isDirty());
		assertTrue(parent.isDirtyBranch());
	}

	public void testDirtyBranchCleanChildDirtyChild() {
		TestWorkbenchModel node1 = this.root.testDescendantNamed("node 1.1.1");
		node1.setSize(41);
		TestWorkbenchModel node2 = this.root.testDescendantNamed("node 1.1.2");
		node2.setSize(42);

		TestWorkbenchModel parent = (TestWorkbenchModel) node1.getParent();
		assertTrue(parent.isClean());
		assertTrue(parent.isDirtyBranch());

		// now, clean the first child, but leave the second child dirty
		node1.markEntireBranchClean();
		assertTrue(parent.isClean());
		assertTrue(parent.isDirtyBranch());
	}

	public void testDirtyBranchForced() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();

		assertTrue(node.isClean());
		assertTrue(node.isCleanBranch());
		assertTrue(parent.isClean());
		assertTrue(parent.isCleanBranch());
		assertTrue(this.root.isClean());
		assertTrue(this.root.isCleanBranch());

		this.root.markEntireBranchDirty();

		assertTrue(node.isDirty());
		assertTrue(node.isDirtyBranch());
		assertTrue(parent.isDirty());
		assertTrue(parent.isDirtyBranch());
		assertTrue(this.root.isDirty());
		assertTrue(this.root.isDirtyBranch());
	}

	public void testDirtyTransientAttribute() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setName("BOGUS");
		assertTrue(node.isDirty());
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		assertTrue(parent.isClean());
		assertTrue(parent.isDirtyBranch());
		assertTrue(this.root.isClean());
		assertTrue(this.root.isDirtyBranch());

		this.root.markEntireBranchClean();

		this.root.validateBranch();

		assertTrue(this.root.problemsSize() == 0);
		assertTrue(node.branchProblems().hasNext());
		assertTrue(parent.problemsSize() == 0);
		assertTrue(parent.branchProblems().hasNext());
		assertTrue(node.problemsSize() > 0);

		// since problems are transient, everything should still be clean
		assertTrue(node.isClean());
		assertTrue(node.isCleanBranch());
		assertTrue(parent.isClean());
		assertTrue(parent.isCleanBranch());
		assertTrue(this.root.isClean());
		assertTrue(this.root.isCleanBranch());
	}

	public void testProblems() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setName("BOGUS");
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();

		this.root.validateBranch();

		assertEquals(0, this.root.problemsSize());
		assertTrue(node.branchProblems().hasNext());
		assertEquals(0, parent.problemsSize());
		assertTrue(parent.branchProblems().hasNext());
		assertEquals(1, node.problemsSize());
		Problem problem1 = node.problems().next();

		// now create another problem that should remove the old problem
		node.setName("STILL BOGUS");
		this.root.validateBranch();

		assertEquals(0, this.root.problemsSize());
		assertTrue(node.branchProblems().hasNext());
		assertEquals(0, parent.problemsSize());
		assertTrue(parent.branchProblems().hasNext());
		assertEquals(1, node.problemsSize());
		Problem problem2 = node.problems().next();
		assertFalse(problem1 == problem2);
		problem1 = problem2;

		// now create another problem that should replace the old problem
		node.setName("STILL BOGUS");
		this.root.validateBranch();

		assertEquals(0, this.root.problemsSize());
		assertTrue(node.branchProblems().hasNext());
		assertEquals(0, parent.problemsSize());
		assertTrue(parent.branchProblems().hasNext());
		assertEquals(1, node.problemsSize());
		problem2 = node.problems().next();
		// the same problem should be there
		assertTrue(problem1.equals(problem2));
	}

	public void testBranchProblems() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setName("BOGUS");
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		parent.setName("BOGUS TOO");
		this.root.setName("BOGUS TOO TOO");

		this.root.validateBranch();

		assertEquals(1, this.root.problemsSize());
		assertEquals(3, this.root.branchProblemsSize());
		assertEquals(1, parent.problemsSize());
		assertEquals(2, parent.branchProblemsSize());
		assertEquals(1, node.problemsSize());
		assertEquals(1, node.branchProblemsSize());

		node.setName("okie-dokie");

		this.root.validateBranch();

		assertEquals(1, this.root.problemsSize());
		assertEquals(2, this.root.branchProblemsSize());
		assertEquals(1, parent.problemsSize());
		assertEquals(1, parent.branchProblemsSize());
		assertEquals(0, node.problemsSize());
		assertEquals(0, node.branchProblemsSize());
	}

	public void testClearAllBranchProblems() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setName("BOGUS");
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		parent.setName("BOGUS TOO");
		this.root.setName("BOGUS TOO TOO");

		this.root.validateBranch();

		assertEquals(1, this.root.problemsSize());
		assertEquals(3, this.root.branchProblemsSize());
		assertEquals(1, parent.problemsSize());
		assertEquals(2, parent.branchProblemsSize());
		assertEquals(1, node.problemsSize());
		assertEquals(1, node.branchProblemsSize());

		parent.clearAllBranchProblems();

		assertEquals(1, this.root.problemsSize());
		assertEquals(1, this.root.branchProblemsSize());
		assertEquals(0, parent.problemsSize());
		assertEquals(0, parent.branchProblemsSize());
		assertEquals(0, node.problemsSize());
		assertEquals(0, CollectionTools.size(node.branchProblems()));
	}

	public void testRemovedBranchProblems() {
		TestWorkbenchModel node = this.root.testDescendantNamed("node 1.1.3");
		node.setName("BOGUS");
		TestWorkbenchModel parent = (TestWorkbenchModel) node.getParent();
		parent.setName("BOGUS TOO");
		this.root.setName("BOGUS TOO TOO");

		this.root.validateBranch();

		assertEquals(1, this.root.problemsSize());
		assertEquals(3, CollectionTools.size(this.root.branchProblems()));
		assertEquals(1, parent.problemsSize());
		assertEquals(2, parent.branchProblemsSize());
		assertEquals(1, node.problemsSize());
		assertEquals(1, CollectionTools.size(node.branchProblems()));

		// completely remove a node that has problems -
		// the entire tree should recalculate its "branch" problems
		parent.removeTestChild(node);

		this.root.validateBranch();

		assertEquals(1, this.root.problemsSize());
		assertEquals(2, CollectionTools.size(this.root.branchProblems()));
		assertEquals(1, parent.problemsSize());
		assertEquals(1, parent.branchProblemsSize());
	}

	public void testSort() {
		List<TestWorkbenchModel> nodes = this.buildSortedNodes();
		assertTrue(new Range(0, 1).includes(this.indexOf(nodes, "aaa")));
		assertTrue(new Range(0, 1).includes(this.indexOf(nodes, "AAA")));
		assertTrue(new Range(2, 3).includes(this.indexOf(nodes, "bbb")));
		assertTrue(new Range(2, 3).includes(this.indexOf(nodes, "BBB")));
		assertTrue(new Range(4, 6).includes(this.indexOf(nodes, "ccc")));
		assertTrue(new Range(4, 6).includes(this.indexOf(nodes, "CCC")));
		assertTrue(new Range(4, 6).includes(this.indexOf(nodes, "���")));
	}

	private int indexOf(List<TestWorkbenchModel> nodes, String nodeName) {
		for (int i = nodes.size(); i-- > 0; ) {
			if (nodes.get(i).getName().equals(nodeName)) {
				return i;
			}
		}
		throw new IllegalArgumentException();
	}

	private List<TestWorkbenchModel> buildSortedNodes() {
		List<TestWorkbenchModel> result = new ArrayList<TestWorkbenchModel>();
		result.add(new RootTestWorkbenchModel("AAA"));
		result.add(new RootTestWorkbenchModel("BBB"));
		result.add(new RootTestWorkbenchModel("CCC"));
		result.add(new RootTestWorkbenchModel("���"));
		result.add(new RootTestWorkbenchModel("ccc"));
		result.add(new RootTestWorkbenchModel("bbb"));
		result.add(new RootTestWorkbenchModel("aaa"));
		return CollectionTools.sort(result);
	}


	// ********** inner classes **********

	private class TestWorkbenchModel extends AbstractNode {
		private String name;
			public static final String NAME_PROPERTY = "name";
		private int size;
			public static final String SIZE_PROPERTY = "size";
		private Collection<TestWorkbenchModel> testChildren;
			public static final String TEST_CHILDREN_COLLECTION = "children";
	
		// ********** construction/initialization **********
		public TestWorkbenchModel(TestWorkbenchModel parent, String name) {
			super(parent);
			if (name == null) {
				throw new NullPointerException();
			}
			this.name = name;
		}
		@Override
		protected void initialize() {
			super.initialize();
			this.size = 0;
			this.testChildren = new HashBag<TestWorkbenchModel>();
		}
		
		@Override
		protected void checkParent(Node parent) {
			// do nothing
		}
	
	
		// ********** accessors **********
		public String getName() {
			return this.name;
		}
		public void setName(String name) {
			Object old = this.name;
			this.name = name;
			this.firePropertyChanged(NAME_PROPERTY, old, name);
		}
	
		public int getSize() {
			return this.size;
		}
		public void setSize(int size) {
			int old = this.size;
			this.size = size;
			this.firePropertyChanged(SIZE_PROPERTY, old, size);
		}
	
		public Iterator<TestWorkbenchModel> testChildren() {
			return new CloneIterator<TestWorkbenchModel>(this.testChildren) {
				@Override
				protected void remove(TestWorkbenchModel current) {
					TestWorkbenchModel.this.removeTestChild(current);
				}
			};
		}
		public int testChildrenSize() {
			return this.testChildren.size();
		}
		private TestWorkbenchModel addTestChild(TestWorkbenchModel testChild) {
			this.addItemToCollection(testChild, this.testChildren, TEST_CHILDREN_COLLECTION);
			return testChild;
		}
		public TestWorkbenchModel addTestChildNamed(String childName) {
			if (this.testChildNamed(childName) != null) {
				throw new IllegalArgumentException(childName);
			}
			return this.addTestChild(new TestWorkbenchModel(this, childName));
		}
		public void removeTestChild(TestWorkbenchModel testChild) {
			this.removeItemFromCollection(testChild, this.testChildren, TEST_CHILDREN_COLLECTION);
		}
	
		// ********** queries **********
		public String displayString() {
			return this.name;
		}
		public TestWorkbenchModel testChildNamed(String childName) {
			for (TestWorkbenchModel testChild : this.testChildren) {
				if (testChild.getName().equals(childName)) {
					return testChild;
				}
			}
			return null;
		}
		public TestWorkbenchModel testDescendantNamed(String descendantName) {
			for (TestWorkbenchModel testDescendant : this.testChildren) {
				if (testDescendant.getName().equals(descendantName)) {
					return testDescendant;
				}
				// recurse...
				testDescendant = testDescendant.testDescendantNamed(descendantName);
				if (testDescendant != null) {
					return testDescendant;
				}
			}
			return null;
		}
	
		// ********** behavior **********
		@Override
		protected void addChildrenTo(List<Node> children) {
			super.addChildrenTo(children);
			children.addAll(this.testChildren);
		}
		@Override
		protected void addProblemsTo(List<Problem> currentProblems) {
			super.addProblemsTo(currentProblems);
			// names must be all lowercase...
			for (int i = this.name.length(); i-- > 0; ) {
				char c = this.name.charAt(i);
				if (Character.isLetter(c) && ! Character.isLowerCase(c)) {
					currentProblems.add(this.buildProblem("NAME_MUST_BE_LOWERCASE", this.name));
					return;
				}
			}
		}
		@Override
		public void toString(StringBuilder sb) {
			sb.append(this.name);
		}
	}


	private class RootTestWorkbenchModel extends TestWorkbenchModel {
		public RootTestWorkbenchModel(String name) {
			super(null, name);
		}
		@Override
		public Validator getValidator() {
			return Node.NULL_VALIDATOR;
		}
	}

}

Back to the top