Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a5f7e394364c9f2885416851f8f5233cc344c782 (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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*******************************************************************************
 * Copyright (c) 2000, 2015 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
 *     Alexander Kurtakov <akurtako@redhat.com> - Bug 459343
 *******************************************************************************/
package org.eclipse.core.tests.resources;

import java.io.*;
import java.net.URI;
import java.util.*;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.internal.resources.*;
import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;

/**
 * Tests path variables.
 */
public class IPathVariableTest extends ResourceTest {

	IPathVariableManager manager = null;
	IProject project = null;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		project = getWorkspace().getRoot().getProject("MyProject");
		try {
			project.create(getMonitor());
			project.open(getMonitor());
		} catch (CoreException e) {
			fail("1.3", e);
		}
		assertTrue("1.4", project.exists());
		manager = project.getPathVariableManager();
	}

	class PathVariableChangeVerifier implements IPathVariableChangeListener {
		class VerificationFailedException extends Exception {
			/**
			 * All serializable objects should have a stable serialVersionUID
			 */
			private static final long serialVersionUID = 1L;

			VerificationFailedException(String message) {
				super(message);
			}
		}

		class Event {
			int type;
			String name;
			IPath value;

			Event(int type, String name, IPath value) {
				this.type = type;
				this.name = name;
				this.value = value;
			}

			@Override
			public boolean equals(Object obj) {
				if (obj == null || !(obj instanceof Event))
					return false;
				Event that = (Event) obj;
				if (this.type != that.type || !this.name.equals(that.name))
					return false;
				return this.value == null ? that.value == null : this.value.equals(that.value);
			}

			@Override
			public String toString() {
				StringBuffer buffer = new StringBuffer();
				buffer.append("Event(");
				buffer.append("type: ");
				buffer.append(stringForType(type));
				buffer.append(" name: ");
				buffer.append(name);
				buffer.append(" value: ");
				buffer.append(value);
				buffer.append(")");
				return buffer.toString();
			}

			String stringForType(int typeValue) {
				switch (typeValue) {
					case IPathVariableChangeEvent.VARIABLE_CREATED :
						return "CREATED";
					case IPathVariableChangeEvent.VARIABLE_CHANGED :
						return "CHANGED";
					case IPathVariableChangeEvent.VARIABLE_DELETED :
						return "DELETED";
					default :
						return "UNKNOWN";
				}
			}
		}

		List<Event> expected = new ArrayList<Event>();
		List<Event> actual = new ArrayList<Event>();

		void addExpectedEvent(int type, String name, IPath value) {
			expected.add(new Event(type, name, value));
		}

		void verify() throws VerificationFailedException {
			String message;
			if (expected.size() != actual.size()) {
				message = "Expected size: " + expected.size() + " does not equal actual size: " + actual.size() + "\n";
				message += dump();
				throw new VerificationFailedException(message);
			}
			for (Event event : expected) {
				if (!actual.contains(event)) {
					message = "Expected and actual results differ.\n";
					message += dump();
					throw new VerificationFailedException(message);
				}
			}
		}

		void reset() {
			expected = new ArrayList<Event>();
			actual = new ArrayList<Event>();
		}

		@Override
		public void pathVariableChanged(IPathVariableChangeEvent event) {
			actual.add(new Event(event.getType(), event.getVariableName(), event.getValue()));
		}

		String dump() {
			StringBuffer buffer = new StringBuffer();
			buffer.append("Expected:\n");
			for (Event event : expected)
				buffer.append("\t" + event + "\n");
			buffer.append("Actual:\n");
			for (Event event : actual)
				buffer.append("\t" + event + "\n");
			return buffer.toString();
		}
	}

	/**
	 * Default constructor for this class. 
	 */
	public IPathVariableTest() {
		super();
	}

	/**
	 * Constructor for the class. 
	 */
	public IPathVariableTest(String name) {
		super(name);
	}

	/**
	 * Return the tests to run.
	 *  
	 * @see org.eclipse.core.tests.harness.ResourceTest#suite()
	 */
	public static Test suite() {
		return new TestSuite(IPathVariableTest.class);
	}

	/**
	 * Test IPathVariableManager#getPathVariableNames
	 */
	public void testGetPathVariableNames() {
		String[] names = null;

		// add one
		try {
			manager.setValue("one", getRandomLocation());
		} catch (CoreException e) {
			fail("1.0", e);
		}
		names = manager.getPathVariableNames();
		List<String> list = Arrays.asList(names);
		assertTrue("1.2", list.contains("one"));

		// add another
		try {
			manager.setValue("two", Path.ROOT);
		} catch (CoreException e) {
			fail("2.0", e);
		}
		names = manager.getPathVariableNames();
		list = Arrays.asList(names);
		assertTrue("2.2", list.contains("one"));
		assertTrue("2.3", list.contains("two"));

		// remove one
		try {
			manager.setValue("one", (IPath) null);
		} catch (CoreException e) {
			fail("3.0", e);
		}
		names = manager.getPathVariableNames();
		list = Arrays.asList(names);
		assertTrue("3.2", list.contains("two"));
		assertTrue("3.3", !list.contains("one"));

		// remove the last one	
		try {
			manager.setValue("two", (IPath) null);
		} catch (CoreException e) {
			fail("4.0", e);
		}
		names = manager.getPathVariableNames();
		list = Arrays.asList(names);
		assertTrue("4.2", !list.contains("two"));
		assertTrue("4.3", !list.contains("one"));
	}

	/**
	 * Test IPathVariableManager#getValue and IPathVariableManager#setValue
	 */
	public void testGetSetValue() {
		boolean WINDOWS = java.io.File.separatorChar == '\\';
		IPath pathOne = WINDOWS ? new Path("C:\\testGetSetValue") : new Path("/testGetSetValue");
		IPath pathTwo = new Path("/blort/backup");
		//add device if necessary
		pathTwo = new Path(pathTwo.toFile().getAbsolutePath());
		IPath pathOneEdit = WINDOWS ? new Path("D:/foobar") : new Path("/foobar");

		// nothing to begin with	
		assertNull("0.0", manager.getValue("one"));

		// add a value to the table
		try {
			manager.setValue("one", pathOne);
		} catch (CoreException e) {
			fail("1.0", e);
		}
		IPath value = manager.getValue("one");
		assertNotNull("1.1", value);
		assertEquals("1.2", pathOne, value);

		// add another value
		try {
			manager.setValue("two", pathTwo);
		} catch (CoreException e) {
			fail("2.0", e);
		}
		value = manager.getValue("two");
		assertNotNull("2.1", value);
		assertEquals("2.2", pathTwo, value);

		// edit the first value
		try {
			manager.setValue("one", pathOneEdit);
		} catch (CoreException e) {
			fail("3.0", e);
		}
		value = manager.getValue("one");
		assertNotNull("3.1", value);
		assertTrue("3.2", pathOneEdit.equals(value));

		// setting with value == null will remove
		try {
			manager.setValue("one", (IPath) null);
		} catch (CoreException e) {
			fail("4.0", e);
		}
		assertNull("4.1", manager.getValue("one"));

		// set values with bogus names 
		try {
			manager.setValue("ECLIPSE$HOME", Path.ROOT);
			fail("5.0 Accepted invalid variable name in setValue()");
		} catch (CoreException ce) {
			// success
		}

		// set value with relative path
		try {
			manager.setValue("one", new Path("foo/bar"));
		} catch (CoreException ce) {
			fail("5.0 Did not Accepted invalid variable value in setValue()");
		}

		// set invalid value (with invalid segment)
		if (WINDOWS) {
			String invalidPathString = "C:/a/\\::/b";
			IPath invalidPath = Path.fromPortableString(invalidPathString);
			assertTrue("6.0", invalidPath.isAbsolute());
			assertTrue("6.1", !Path.EMPTY.isValidPath(invalidPathString));
			assertTrue("6.2", manager.validateValue(invalidPath).isOK());
			try {
				manager.setValue("one", invalidPath);
			} catch (CoreException ce) {
				fail("6.3 Fail to accept invalid variable value in setValue()");
			}
		}

	}

	/**
	 * Test IPathVariableManager#isDefined
	 */
	public void testIsDefined() {
		assertTrue("0.0", !manager.isDefined("one"));
		try {
			manager.setValue("one", Path.ROOT);
		} catch (CoreException e) {
			fail("1.0", e);
		}
		assertTrue("1.1", manager.isDefined("one"));
		try {
			manager.setValue("one", (IPath) null);
		} catch (CoreException e) {
			fail("2.0", e);
		}
		assertTrue("2.1", !manager.isDefined("one"));
	}

	/**
	 * Test IPathVariableManager#resolvePath
	 */
	public void testResolvePathWithMacro() {
		final boolean WINDOWS = java.io.File.separatorChar == '\\';
		IPath pathOne = WINDOWS ? new Path("c:/testGetSetValue/foo") : new Path("/testGetSetValue/foo");
		IPath pathTwo = WINDOWS ? new Path("c:/tmp/backup") : new Path("/tmp/backup");
		// add device if neccessary
		pathTwo = new Path(pathTwo.toFile().getAbsolutePath());

		try {
			manager.setValue("one", pathOne);
		} catch (CoreException e) {
			fail("0.1", e);
		}
		try {
			manager.setValue("two", pathTwo);
		} catch (CoreException e) {
			fail("0.2", e);
		}

		try {
			manager.setValue("three", Path.fromOSString("${two}/extra"));
		} catch (CoreException e) {
			fail("0.3", e);
		}

		IPath path = new Path("three/bar");
		IPath expected = new Path("/tmp/backup/extra/bar").setDevice(WINDOWS ? "c:" : null);
		IPath actual = manager.resolvePath(path);
		assertEquals("1.0", expected, actual);
	}

	/**
	 */
	public void testProjectLoc() {
		IPath path = new Path("${PROJECT_LOC}/bar");
		IPath projectLocation = project.getLocation();

		IPath expected = projectLocation.append("bar");
		IPath actual = manager.resolvePath(path);
		assertEquals("1.0", expected, actual);
	}

	/**
	 */
	public void testEclipseHome() {
		IPath path = new Path("${ECLIPSE_HOME}/bar");
		IPath expected = new Path(Platform.getInstallLocation().getURL().getPath()).append("bar");
		IPath actual = manager.resolvePath(path);
		assertEquals("1.0", expected, actual);
	}

	/**
	 */
	public void testWorkspaceLocation() {
		IPath path = new Path("${WORKSPACE_LOC}/bar");
		IPath expected = project.getWorkspace().getRoot().getLocation().append("bar");
		IPath actual = manager.resolvePath(path);
		assertEquals("1.0", expected, actual);
	}

	/**
	 * Test IgetVariableRelativePathLocation(project, IPath)
	 */

	public void testGetVariableRelativePathLocation() {
		IPath path = project.getWorkspace().getRoot().getLocation().append("bar");
		IPath actual;
		IPath expected;
		/* Does not work on the test machine because ECLIPSE_HOME and WORKSPACE is the same location
		actual = getVariableRelativePathLocation(project, path);
		expected = new Path("WORKSPACE_LOC/bar");
		assertEquals("1.0", expected, actual);
		 */
		path = new Path(Platform.getInstallLocation().getURL().getPath()).append("bar");
		expected = new Path("ECLIPSE_HOME/bar");
		actual = getVariableRelativePathLocation(project, path);
		assertEquals("2.0", expected, actual);

		path = project.getLocation().append("bar");
		expected = new Path("PROJECT_LOC/bar");
		actual = getVariableRelativePathLocation(project, path);
		assertEquals("3.0", expected, actual);

		actual = getVariableRelativePathLocation(project, new Path("/nonExistentPath/foo"));
		assertEquals("4.0", null, actual);
	}

	private IPath getVariableRelativePathLocation(IProject project, IPath location) {
		URI variableRelativePathLocation = project.getPathVariableManager().getVariableRelativePathLocation(URIUtil.toURI(location));
		if (variableRelativePathLocation != null)
			return URIUtil.toPath(variableRelativePathLocation);
		return null;
	}

	/**
	 * Test IPathVariableManager#resolvePath
	 */
	public void testResolvePath() {
		final boolean WINDOWS = java.io.File.separatorChar == '\\';
		IPath pathOne = WINDOWS ? new Path("C:/testGetSetValue/foo") : new Path("/testGetSetValue/foo");
		IPath pathTwo = new Path("/blort/backup");
		//add device if necessary
		pathTwo = new Path(pathTwo.toFile().getAbsolutePath());

		try {
			// for WINDOWS - the device id for windows will be changed to upper case 
			// in the variable stored in the manager
			manager.setValue("one", pathOne);
		} catch (CoreException e) {
			fail("0.1", e);
		}
		try {
			manager.setValue("two", pathTwo);
		} catch (CoreException e) {
			fail("0.2", e);
		}

		// one substitution
		IPath path = new Path("one/bar");
		IPath expected = new Path("/testGetSetValue/foo/bar").setDevice(WINDOWS ? "C:" : null);
		IPath actual = manager.resolvePath(path);
		assertEquals("1.0", expected, actual);

		// another substitution
		path = new Path("two/myworld");
		expected = new Path("/blort/backup/myworld");
		expected = new Path(expected.toFile().getAbsolutePath());
		actual = manager.resolvePath(path);
		assertEquals("2.0", expected, actual);

		// variable not defined
		path = new Path("three/nothere");
		expected = path;
		actual = manager.resolvePath(path);
		assertEquals("3.0", expected, actual);

		// device
		path = new Path("/one").setDevice(WINDOWS ? "C:" : null);
		expected = path;
		actual = manager.resolvePath(path);
		assertEquals("4.0", expected, actual);

		// device2
		if (WINDOWS) {
			path = new Path("C:two");
			expected = path;
			actual = manager.resolvePath(path);
			assertEquals("5.0", expected, actual);
		}

		// absolute
		path = new Path("/one");
		expected = path;
		actual = manager.resolvePath(path);
		assertEquals("6.0", expected, actual);

		// just resolving, check if the variable stored in the manager is canonicalized
		if (WINDOWS) {
			path = new Path("one");
			expected = FileUtil.canonicalPath(pathOne);
			actual = manager.resolvePath(path);
			// the path stored in the manager is canonicalized, so the device id of actual will be upper case
			assertEquals("7.0", expected, actual);
		}

		// null
		path = null;
		assertNull("7.0", manager.resolvePath(path));

	}

	private IPath convertToRelative(IPathVariableManager manager, IPath path, boolean force, String variableHint) throws CoreException {
		return URIUtil.toPath(manager.convertToRelative(URIUtil.toURI(path), force, variableHint));
	}

	/**
	 * Test IPathVariableManager#convertToRelative()
	 */
	public void testConvertToRelative() {
		final boolean WINDOWS = java.io.File.separatorChar == '\\';
		IPath pathOne = WINDOWS ? new Path("c:/foo/bar") : new Path("/foo/bar");
		IPath pathTwo = WINDOWS ? new Path("c:/foo/other") : new Path("/foo/other");
		IPath pathThree = WINDOWS ? new Path("c:/random/other/subpath") : new Path("/random/other/subpath");
		IPath file = WINDOWS ? new Path("c:/foo/other/file.txt") : new Path("/foo/other/file.txt");

		try {
			manager.setValue("ONE", pathOne);
			manager.setValue("THREE", pathThree);
		} catch (CoreException e) {
			fail("0.1", e);
		}

		IPath actual = null;
		try {
			actual = convertToRelative(manager, file, false, "ONE");
		} catch (CoreException e) {
			fail("0.2", e);
		}
		IPath expected = file;
		assertEquals("1.0", expected, actual);

		try {
			manager.setValue("TWO", pathTwo);
		} catch (CoreException e) {
			fail("1.1", e);
		}

		try {
			actual = convertToRelative(manager, file, false, "ONE");
		} catch (CoreException e) {
			fail("1.2", e);
		}
		expected = file;
		assertEquals("2.0", expected, actual);

		try {
			actual = convertToRelative(manager, file, false, "TWO");
		} catch (CoreException e) {
			fail("2.1", e);
		}
		expected = new Path("TWO/file.txt");
		assertEquals("3.0", expected, actual);

		// force the path to be relative to "ONE"
		try {
			actual = convertToRelative(manager, file, true, "ONE");
		} catch (CoreException e) {
			fail("3.1", e);
		}
		expected = new Path("PARENT-1-ONE/other/file.txt");
		assertEquals("4.0", expected, actual);

		// the second time should be re-using "FOO"
		try {
			actual = convertToRelative(manager, file, true, "ONE");
		} catch (CoreException e) {
			fail("4.3", e);
		}
		expected = new Path("PARENT-1-ONE/other/file.txt");
		assertEquals("5.0", expected, actual);

		try {
			actual = convertToRelative(manager, file, true, "TWO");
		} catch (CoreException e) {
			fail("5.4", e);
		}
		expected = new Path("TWO/file.txt");
		assertEquals("6.0", expected, actual);

		try {
			actual = convertToRelative(manager, file, true, "TWO");
		} catch (CoreException e) {
			fail("6.1", e);
		}
		expected = new Path("TWO/file.txt");
		assertEquals("7.0", expected, actual);

		try {
			actual = convertToRelative(manager, file, false, null);
		} catch (CoreException e) {
			fail("7.1", e);
		}
		expected = new Path("TWO/file.txt");
		assertEquals("8.0", expected, actual);

		try {
			manager.setValue("TWO", (IPath) null);
		} catch (CoreException e) {
			fail("8.1", e);
		}

		// now without any direct reference
		try {
			actual = convertToRelative(manager, file, false, null);
		} catch (CoreException e) {
			fail("8.2", e);
		}
		expected = file;
		assertEquals("9.0", expected, actual);

		try {
			actual = convertToRelative(manager, file, true, null);
		} catch (CoreException e) {
			fail("9.1", e);
		}
		expected = new Path("PARENT-1-ONE/other/file.txt");
		assertEquals("10.0", expected, actual);
	}

	/**
	 * Test IPathVariableManager#testValidateName
	 */
	public void testValidateName() {

		// valid names 	
		assertTrue("0.0", manager.validateName("ECLIPSEHOME").isOK());
		assertTrue("0.1", manager.validateName("ECLIPSE_HOME").isOK());
		assertTrue("0.2", manager.validateName("ECLIPSE_HOME_1").isOK());
		assertTrue("0.3", manager.validateName("_").isOK());

		// invalid names
		assertTrue("1.0", !manager.validateName("1FOO").isOK());
		assertTrue("1.1", !manager.validateName("FOO%BAR").isOK());
		assertTrue("1.2", !manager.validateName("FOO$BAR").isOK());
		assertTrue("1.3", !manager.validateName(" FOO").isOK());
		assertTrue("1.4", !manager.validateName("FOO ").isOK());

	}

	/**
	 * Regression test for Bug 304195
	 */
	public void testEmptyURIResolution() {
		IPath path = new Path(new String());
		URI uri = URIUtil.toURI(path);
		try {
			manager.resolveURI(uri);
		} catch (Throwable e) {
			fail("1.2", e);
		}
		try {
			getWorkspace().getPathVariableManager().resolveURI(uri);
		} catch (Throwable e) {
			fail("1.2", e);
		}
	}

	/**
	 * Test IPathVariableManager#addChangeListener and IPathVariableManager#removeChangeListener
	 */
	public void testListeners() {
		PathVariableChangeVerifier listener = new PathVariableChangeVerifier();
		manager.addChangeListener(listener);
		IPath pathOne = new Path("/blort/foobar");
		//add device if necessary
		pathOne = new Path(pathOne.toFile().getAbsolutePath());
		IPath pathOneEdit = pathOne.append("myworld");

		try {

			// add a variable
			try {
				manager.setValue("one", pathOne);
			} catch (CoreException e) {
				fail("1.0", e);
			}
			listener.addExpectedEvent(IPathVariableChangeEvent.VARIABLE_CREATED, "one", pathOne);
			try {
				listener.verify();
			} catch (PathVariableChangeVerifier.VerificationFailedException e) {
				fail("1.1", e);
			}

			// change a variable
			listener.reset();
			try {
				manager.setValue("one", pathOneEdit);
			} catch (CoreException e) {
				fail("2.0", e);
			}
			listener.addExpectedEvent(IPathVariableChangeEvent.VARIABLE_CHANGED, "one", pathOneEdit);
			try {
				listener.verify();
			} catch (PathVariableChangeVerifier.VerificationFailedException e) {
				fail("2.1", e);
			}

			// remove a variable
			listener.reset();
			try {
				manager.setValue("one", (IPath) null);
			} catch (CoreException e) {
				fail("3.0", e);
			}
			listener.addExpectedEvent(IPathVariableChangeEvent.VARIABLE_DELETED, "one", null);
			try {
				listener.verify();
			} catch (PathVariableChangeVerifier.VerificationFailedException e) {
				fail("3.1", e);
			}

		} finally {
			manager.removeChangeListener(listener);
		}
	}

	boolean contains(Object[] array, Object obj) {
		for (int i = 0; i < array.length; i++)
			if (array[i].equals(obj))
				return true;
		return false;
	}

	/**
	 * Ensure there are no path variables in the workspace.
	 */
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		String[] names = manager.getPathVariableNames();
		for (int i = 0; i < names.length; i++) {
			manager.setValue(names[i], (IPath) null);
		}
	}

	@Override
	protected void cleanup() throws CoreException {
		project.delete(true, getMonitor());
		super.cleanup();
	}

	/**
	 * Regression for Bug 308975 - Can't recover from 'invalid' path variable
	 */
	public void testLinkExistInProjectDescriptionButNotInWorkspace() {
		String dorProjectContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + //
				"<projectDescription>" + //
				"<name>ExistingProject</name>" + //
				"<comment></comment>" + //
				"<projects>" + //
				"</projects>" + //
				"<buildSpec>" + //
				"</buildSpec>" + //
				"<natures>" + //
				"</natures>" + //
				"<variableList>" + //
				"<variable>" + //
				"		<name>PROJ_UP</name>" + //
				"		<value>$%7BPARENT-1-PROJECT_LOC%7D</value>" + //
				"	</variable>" + //
				"	</variableList>" + //
				"</projectDescription>";

		IProject existingProject = getWorkspace().getRoot().getProject("ExistingProject");

		ensureExistsInWorkspace(new IResource[] {existingProject}, true);

		try {
			existingProject.close(getMonitor());
			ProjectInfo info = (ProjectInfo) ((Project) existingProject).getResourceInfo(false, false);
			info.clear(ICoreConstants.M_USED);
			String dotProjectPath = existingProject.getLocation().append(".project").toOSString();
			FileWriter fstream = new FileWriter(dotProjectPath);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(dorProjectContent);
			out.close();
			existingProject.open(getMonitor());
		} catch (CoreException e) {
			fail("1.99", e);
		} catch (IOException e) {
			fail("1.99", e);
		}

		IPathVariableManager pathVariableManager = existingProject.getPathVariableManager();
		String[] varNames = pathVariableManager.getPathVariableNames();

		for (int i = 0; i < varNames.length; i++) {
			try {
				pathVariableManager.getURIValue(varNames[i]);
			} catch (Exception e) {
				fail("3.99", e);
			}
		}
	}

	/**
	 * Regression test for Bug 328045 where a class cast exception is thrown when
	 * attempting to get the location of a resource that would live under an 
	 * existing IFile.
	 */
	public void testDiscoverLocationOfInvalidFile() {
		IPath filep = new Path("someFile");
		IPath invalidChild = filep.append("invalidChild");

		// Create filep
		IFile f = project.getFile(filep);
		ensureExistsInWorkspace(f, true);

		// Get a reference to a child
		IFile invalidFile = project.getFile(invalidChild);

		// Don't care about the result, just care there's no exception.
		invalidFile.getLocationURI();
	}
}

Back to the top