Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e82124702c815ad3f451db94618b8a3b791630f5 (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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*****************************************************************************
 * Copyright (c) 2014, 2021 Christian W. Damus, CEA LIST, and others.
 *
 * All rights reserved. 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:
 *   Christian W. Damus - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.emf.resource.index;

import static org.eclipse.papyrus.junit.matchers.MoreMatchers.greaterThan;
import static org.eclipse.papyrus.junit.matchers.MoreMatchers.statusWithException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.emf.Activator;
import org.eclipse.papyrus.infra.emf.internal.resource.index.IIndexManagerListener;
import org.eclipse.papyrus.infra.emf.internal.resource.index.IndexManager;
import org.eclipse.papyrus.infra.emf.internal.resource.index.InternalModelIndex;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.LogTracker;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;

import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.io.Files;

/**
 * Test suite for the {@link WorkspaceModelIndex} class.
 */
@FixMethodOrder(MethodSorters.JVM)
public class WorkspaceModelIndexTest extends AbstractPapyrusTest {

	static final boolean DEBUG = Platform.inDebugMode();

	private final Sync sync = new Sync();
	private final CrossReferenceIndexer index = new CrossReferenceIndexer(sync);

	private IndexManager manager;
	private WorkspaceModelIndex<CrossReferenceIndex> fixture;

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();

	@Rule
	public final TestName testName = new TestName();

	private IProject referencingProject;
	private IFile referencingFile;
	private URI referencingURI;
	private IProject referencedProject;
	private IFile referencedFile;
	private URI referencedURI;

	public WorkspaceModelIndexTest() {
		super();
	}

	@Test
	public void indexBuilt() throws Exception {
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		assertIndex(index);
	}

	@Test
	public void indexRebuiltOnChanges() throws Exception {
		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		final String newFileName = "the_referencing_model.uml";

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.MANAGER);

		RenameJob rename = new RenameJob(referencingFile, newFileName);

		// Update the identity of the file
		referencingFile = referencingProject.getFile(new Path(newFileName));
		referencingURI = uri(referencingFile);

		// Rename it
		rename.go();

		sync.syncFromTest(); // Wait for indexing to start

		long requestIndex = System.currentTimeMillis();

		// Check the index
		index = fixture.getIndex().get();

		long gotIndex = System.currentTimeMillis();

		assertThat("Didn't have to wait for the index to be rebuilt", (gotIndex - requestIndex), greaterThan(1000L));

		assertIndex(index);
	}

	@Test
	public void indexRebuiltOnDeletion() throws Exception {
		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.MANAGER);

		DeleteJob delete = new DeleteJob(referencingFile);

		// Delete it
		delete.go();

		sync.syncFromTest(); // Wait for indexing to start

		long requestIndex = System.currentTimeMillis();

		// Check the index
		index = fixture.getIndex().get();

		long gotIndex = System.currentTimeMillis();

		assertThat("Didn't have to wait for the index to be rebuilt", (gotIndex - requestIndex), greaterThan(1000L));

		assertIndex(index, true, Collections.<URI> emptySet(), Collections.<URI> emptySet(), false, null, null);
	}

	@Test
	public void indexRebuiltOnProjectClose() throws Exception {
		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.UNINDEX);

		referencingFile.getProject().close(null);

		sync.syncFromTest(); // Wait for unindexing to start

		// Check the index
		index = fixture.getIndex().get();

		assertIndex(index, true, Collections.<URI> emptySet(), Collections.<URI> emptySet(), false, null, null);

		referencingFile.getProject().open(null);

		index = fixture.getIndex().get();
		assertIndex(index);
	}

	@Test
	public void indexRebuiltOnProjectDelete() throws Exception {
		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.UNINDEX);

		referencingFile.getProject().delete(true, true, null);

		sync.syncFromTest(); // Wait for unindexing to start

		// Check the index
		index = fixture.getIndex().get();

		assertIndex(index, true, Collections.<URI> emptySet(), Collections.<URI> emptySet(), false, null, null);
	}

	@Test
	public void indexNotifiesOnReindex() throws Exception {
		// Initial build
		fixture.getIndex().get();

		class TestListener extends WorkspaceModelIndexAdapter {
			volatile int started;
			volatile int ended;

			@Override
			protected void indexAboutToCalculateOrRecalculate(WorkspaceModelIndexEvent event) {
				if (referencingProject.equals(event.getProject())) {
					started++;
				}
			}

			@Override
			protected void indexCalculatedOrRecalculated(WorkspaceModelIndexEvent event) {
				if (referencingProject.equals(event.getProject())) {
					ended++;
				}
			}
		}

		TestListener listener = attach(new TestListener());

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.UNINDEX);

		referencingFile.delete(true, null);

		sync.syncFromTest(); // Wait for unindexing to start

		// Access the index again to synchronize
		fixture.getIndex().get();

		assertThat(listener.started, is(1));
		assertThat(listener.ended, is(1));
	}

	@Test
	public void indexRecoversFromCancel() throws Exception {
		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		// Interlock with the indexing for timing purposes
		Future<Job> controlJob = sync.init(() -> getJobWrangler(manager), SyncMode.INDEX, IndexMode.MANAGER, IndexMode.INDEX);

		final String newFileName = "the_referencing_model.uml";

		// Rename the file
		referencingFile.move(referencingFile.getFullPath().removeLastSegments(1).append(newFileName), true, null);

		// Update the identity of the file
		referencingFile = referencingProject.getFile(new Path(newFileName));
		referencingURI = uri(referencingFile);

		// Let the index manager start
		sync.syncFromTest(IndexMode.MANAGER);

		// Cancel the index control job
		JobWaiter controlJobWaiter = waitForStart(controlJob.get(30L, TimeUnit.SECONDS));

		// Let the indexing start
		sync.syncFromTest(IndexMode.INDEX);

		long cancellingAt = System.currentTimeMillis();

		controlJob.get().cancel();

		// Let the indexing finish
		sync.syncFromTest(IndexMode.INDEX);

		controlJobWaiter.waitForJob();

		long restartedAt = System.currentTimeMillis();

		assertThat("Didn't have to wait for the index to recover",
				(restartedAt - cancellingAt), greaterThan(1000L));

		// Check the index
		index = fixture.getIndex().get(30L, TimeUnit.SECONDS);

		assertIndex(index);
	}

	/**
	 * Verify that the index will refresh workspace resources as necessary in order to
	 * properly determine the content-types of files, skip missing resources, etc.
	 *
	 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=473154
	 */
	@Test
	public void indexRefreshesIfNecessary_bug473154() throws Exception {

		final LogTracker tracker = houseKeeper.cleanUpLater(new LogTracker());
		tracker.start("org.eclipse.core.filesystem");

		final File nativeFile = new File(referencingFile.getLocationURI());
		final Charset charset = Charset.forName(referencedFile.getCharset());
		final String contents = Files.asCharSource(nativeFile, charset).read();

		// De-synchronize the file (by deletion, as in the bugzilla scenario)
		new FileManipulationJob("Sneakily delete file", referencingFile) {

			@Override
			protected void manipulateFile(IFile file, IProgressMonitor monitor) throws CoreException {
				// Sneakily delete the file (don't tell the workspace)
				File nativeFile = new File(file.getLocationURI());
				nativeFile.delete();
			}
		}.go();

		// Initial build
		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		tracker.assertNone(statusWithException(instanceOf(FileNotFoundException.class)));

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.INDEX);

		// Put the file back and synchronize it
		new FileManipulationJob("Restore file", referencingFile) {
			@Override
			protected void manipulateFile(IFile file, IProgressMonitor monitor) throws CoreException {
				try {
					Files.asCharSink(nativeFile, charset).write(contents);
				} catch (IOException e) {
					throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to restore test file.", e));
				}
				file.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
			}
		}.go();

		// Wait for the indexing to start. If it doesn't start within five seconds, then
		// something is wrong with the resource change notifications
		sync.syncFromTest();

		// Synchronize with the index and verify that it got the file
		index = fixture.getIndex().get();
		assertIndex(index);
	}

	/**
	 * Verify that derived resources are not indexed.
	 *
	 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=573042
	 */
	@Test
	public void indexNotBuiltForDerivedResources() throws Exception {
		IPath newReferencingFilePath = referencingFile.getProjectRelativePath().removeLastSegments(1).append("derived")
				.addFileExtension(referencingFile.getFileExtension());
		IFile newReferencingFile = referencingFile.getProject().getFile(newReferencingFilePath);

		ICoreRunnable furtherSetup = monitor -> {
			SubMonitor sub = SubMonitor.convert(monitor, 2);
			referencingFile.copy(newReferencingFilePath, false, sub.newChild(1));
			newReferencingFile.setDerived(true, sub.newChild(1));
			sub.done();
		};

		// Interlock with the indexing to ensure that we don't try the index before it hears
		// about the file delta
		sync.init(SyncMode.TEST, IndexMode.INDEX);

		newReferencingFile.getWorkspace().run(furtherSetup, new NullProgressMonitor());

		// Wait for the indexing to start. If it doesn't start within five seconds, then
		// something is wrong with the resource change notifications
		sync.syncFromTest();

		Map<IFile, CrossReferenceIndex> index = fixture.getIndex().get();

		// The new Referencing URI is not included in the index
		assertIndex(index, true, Set.of(), Set.of(referencingURI), true, Set.of(), Set.of(referencedURI));
	}

	//
	// Test framework
	//

	@Before
	public void createFixture() throws Exception {
		if (DEBUG) {
			System.out.printf("[%s] Starting test ...%n", testName.getMethodName());
		}

		IndexManager.getInstance().pause();

		referencedProject = houseKeeper.createProject("referencedProject");
		referencingProject = houseKeeper.createProject("referencingProject");

		referencedFile = referencedProject.getFile("referenced.uml");
		InputStream input = getTestResource("resources/index/referenced.uml");
		try {
			referencedFile.create(input, true, null);
		} finally {
			input.close();
		}
		referencedURI = uri(referencedFile);

		referencingFile = referencingProject.getFile("referencing.uml");
		input = getTestResource("resources/index/referencing.uml");
		try {
			referencingFile.create(input, true, null);
		} finally {
			input.close();
		}
		referencingURI = uri(referencingFile);

		manager = new IndexManager() {
			@Override
			protected Map<QualifiedName, InternalModelIndex> loadIndices() {
				fixture = new WorkspaceModelIndex<>("test", UMLResource.UML_CONTENT_TYPE_IDENTIFIER, index, 2);
				return Collections.singletonMap(fixture.getIndexKey(), (InternalModelIndex) fixture);
			}
		};
		manager.startManager();
	}

	@After
	public void destroyFixture() {
		try {
			sync.clear();

			// This disposes the fixture, too
			manager.dispose();
			manager = null;
			fixture = null;

			if (DEBUG) {
				System.out.printf("[%s] Finished test.%n", testName.getMethodName());
			}
		} finally {
			IndexManager.getInstance().resume();
		}
	}

	static URI uri(IFile file) {
		return URI.createPlatformResourceURI(file.getFullPath().toString(), true);
	}

	private InputStream getTestResource(String path) throws IOException {
		return Platform.getBundle("org.eclipse.papyrus.infra.emf.tests").getEntry(path).openStream();
	}

	void assertIndex(Map<IFile, CrossReferenceIndex> index) {
		assertIndex(index, true, Collections.<URI> emptySet(), Collections.singleton(referencingURI), true, Collections.<URI> emptySet(), Collections.singleton(referencedURI));
	}

	void assertIndex(Map<IFile, CrossReferenceIndex> index, boolean expectReferenced, Set<URI> referencedImports, Set<URI> referencedExports, boolean expectReferencing, Set<URI> referencingImports, Set<URI> referencingExports) {
		CrossReferenceIndex referenced = index.get(referencedFile);
		assertThat(referenced, expectReferenced ? notNullValue() : nullValue());
		if (referenced != null) {
			assertThat(referenced.imports, is(referencedImports));
			assertThat(referenced.exports, is(referencedExports));
		}

		CrossReferenceIndex referencing = index.get(referencingFile);
		assertThat(referencing, expectReferencing ? notNullValue() : nullValue());

		if (referencing != null) {
			assertThat(referencing.exports, is(referencingImports));
			assertThat(referencing.imports, is(referencingExports));
		}
	}

	<T extends IWorkspaceModelIndexListener> T attach(T listener) {
		fixture.addListener(listener);
		return listener;
	}

	Job getJobWrangler(IndexManager indexManager) {
		Job[] family = Job.getJobManager().find(manager);
		Job result = null;
		for (Job job : family) {
			if (job.getClass().getSimpleName().contains("JobWrangler")) { //$NON-NLS-1$
				result = job;
				break;
			}
		}
		assertThat("Control job not found", result, notNullValue());

		return result;
	}

	//
	// Nested types
	//

	class Sync {
		private volatile SyncMode mode = SyncMode.NONE;
		private volatile Map<IndexMode, Semaphore> semaphores = new EnumMap<>(IndexMode.class);
		private volatile Runnable onSyncFromIndex;

		/**
		 * Create (in the test) a semaphore that gates the index on the test or the test on the index,
		 * according to the mode specified.
		 *
		 * @param syncMode
		 *            the direction of synchronization gating
		 * @param indexMode,&nbsp;more
		 *            which kind of index operations are synchronized with the test
		 */
		void init(SyncMode syncMode, IndexMode indexMode, IndexMode... more) {
			clear();

			this.mode = syncMode;
			EnumSet.of(indexMode, more).forEach(im -> semaphores.put(im, new Semaphore(0)));

			if (this.semaphores.containsKey(IndexMode.MANAGER)) {
				IIndexManagerListener[] listener = { null };
				listener[0] = IIndexManagerListener.startingAdapter(__ -> {
					try {
						syncFromIndex(IndexMode.MANAGER);
					} finally {
						manager.removeIndexManagerListener(listener[0]);
					}
				});
				manager.addIndexManagerListener(listener[0]);
			}
		}

		<V> Future<V> init(Callable<V> callable, SyncMode syncMode, IndexMode indexMode, IndexMode... more) {
			CompletableFuture<V> result = new CompletableFuture<>();

			init(syncMode, indexMode, more);
			onSyncFromIndex = () -> {
				try {
					result.complete(callable == null ? null : callable.call());
				} catch (ThreadDeath d) {
					throw d;
				} catch (Throwable e) {
					result.completeExceptionally(e);
				} finally {
					onSyncFromIndex = null;
				}
			};

			return result;
		}

		/** Called by the indexing job when it needs to sync up with the test. */
		void syncFromIndex(IndexMode mode) {
			if (!semaphores.containsKey(mode)) {
				return;
			}

			if (DEBUG && this.mode != SyncMode.NONE) {
				System.out.printf("[%s] Synching from indexer (%s): %s [%s]%n", testName.getMethodName(), mode, this.mode, Thread.currentThread().getName());
			}

			if (onSyncFromIndex != null) {
				onSyncFromIndex.run();
			}

			switch (this.mode) {
			case INDEX:
				// Index is gated on the test
				semaphores.get(mode).acquireUninterruptibly();
				break;
			case TEST:
				// Test is gated on the index
				semaphores.get(mode).release();
				break;
			default:
				// Pass
			}
		}

		/** Called by the test when it needs to sync up with the index. */
		void syncFromTest() throws Exception {
			assertThat("Must specify index mode", semaphores.size(), is(1));
			syncFromTest(Iterables.getOnlyElement(semaphores.keySet()));
		}

		/** Called by the test when it needs to sync up with the index. */
		void syncFromTest(IndexMode mode) throws Exception {
			if (!semaphores.containsKey(mode)) {
				return;
			}

			if (DEBUG && this.mode != SyncMode.NONE) {
				System.out.printf("[%s] Synching from test (%s): %s [%s]%n", testName.getMethodName(), mode, this.mode, Thread.currentThread().getName());
			}

			switch (this.mode) {
			case INDEX:
				// Index is gated on the test
				semaphores.get(mode).release();
				break;
			case TEST:
				// Test is gated on the index
				if (!semaphores.get(mode).tryAcquire(5L, TimeUnit.SECONDS)) {
					fail("Timed out waiting for indexing job");
				}
				break;
			default:
				// Pass
			}
		}

		void clear() {
			onSyncFromIndex = null;

			this.mode = SyncMode.NONE;

			// Make sure that any blocked threads are released to whatever fate
			semaphores.values().forEach(sema -> sema.release(100));
			semaphores.clear();
		}

	}

	private enum SyncMode {
		NONE, INDEX, TEST,
	}

	private enum IndexMode {
		INDEX, UNINDEX, MANAGER;
	}

	static class CrossReferenceIndexer implements WorkspaceModelIndex.IndexHandler<CrossReferenceIndex> {
		private final Map<IFile, CrossReferenceIndex> index = Maps.newHashMap();

		private final Sync sync;

		/**
		 * Initializes me.
		 *
		 * @param sync
		 *            a protocol for handshaking at start and end of indexing a file
		 */
		public CrossReferenceIndexer(Sync sync) {
			super();

			this.sync = sync;
		}

		private CrossReferenceIndex get(IFile file) {
			CrossReferenceIndex result;

			synchronized (index) {
				result = index.get(file);
				if (result == null) {
					result = new CrossReferenceIndex();
					index.put(file, result);
				}
			}

			return result;
		}

		@Override
		public CrossReferenceIndex index(IFile file) {
			final CrossReferenceIndex result = get(file);

			sync.syncFromIndex(IndexMode.INDEX);

			try {
				Set<URI> imports = result.imports;
				imports.clear();

				ResourceSet resourceSet = new IndexingResourceSet();

				try {
					URI uri = uri(file);

					Resource resource = resourceSet.getResource(uri, true);
					for (Map.Entry<EObject, Collection<EStructuralFeature.Setting>> next : EcoreUtil.ProxyCrossReferencer.find(resource).entrySet()) {
						for (EStructuralFeature.Setting setting : next.getValue()) {
							Object references = setting.get(false);

							if (references instanceof EObject) {
								EObject ref = (EObject) references;
								if (ref.eIsProxy()) {
									URI href = EcoreUtil.getURI(ref).trimFragment();
									if (href.isPlatformResource() && imports.add(href)) {
										// add the corresponding export
										IFile other = file.getWorkspace().getRoot().getFile(new Path(href.toPlatformString(true)));
										get(other).exports.add(uri);
									}
								}
							}
						}
					}
				} finally {
					EMFHelper.unload(resourceSet);
				}
			} finally {
				sync.syncFromIndex(IndexMode.INDEX);
			}

			return result;
		}

		@Override
		public void unindex(IFile file) {
			URI uri = uri(file);

			sync.syncFromIndex(IndexMode.UNINDEX);

			synchronized (index) {
				// Remove this file from the index
				index.remove(file);

				// Remove exports because the deleted resource no longer imports resources
				for (CrossReferenceIndex next : index.values()) {
					next.exports.remove(uri);
				}

				// DO NOT remove imports because resources that still exist will still have
				// proxies referencing the resource, despite that it is deleted
			}
		}
	}

	static class CrossReferenceIndex {
		final Set<URI> imports = Sets.newConcurrentHashSet();
		final Set<URI> exports = Sets.newConcurrentHashSet();
	}

	static class IndexingResourceSet extends ResourceSetImpl {
		@Override
		public EObject getEObject(URI uri, boolean loadOnDemand) {
			// Don't resolve any proxies
			return null;
		}
	}

	static abstract class FileManipulationJob extends Job {
		private final IFile file;

		private final CountDownLatch latch = new CountDownLatch(1);

		FileManipulationJob(String jobName, IFile file) {
			super(jobName);

			this.file = file;

			setRule(file.getProject());
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			try {
				manipulateFile(file, monitor);

				// Release the test case
				latch.countDown();

				// Delay a bit to ensure that the test case has to wait for the index to update
				Thread.sleep(TimeUnit.SECONDS.toMillis(3));
			} catch (InterruptedException e) {
				return Status.CANCEL_STATUS;
			} catch (CoreException e) {
				return e.getStatus();
			}

			return Status.OK_STATUS;
		}

		protected abstract void manipulateFile(IFile file, IProgressMonitor monitor) throws CoreException;

		void go() throws InterruptedException {
			schedule();
			await();
		}

		void await() throws InterruptedException {
			latch.await();
		}

	}

	static class RenameJob extends FileManipulationJob {
		private final String newName;

		RenameJob(IFile file, String name) {
			super(String.format("Rename %s as %s", file.getFullPath(), name), file);

			this.newName = name;
		}

		@Override
		protected void manipulateFile(IFile file, IProgressMonitor monitor) throws CoreException {
			file.move(file.getFullPath().removeLastSegments(1).append(newName), true, monitor);
		}
	}

	static class DeleteJob extends FileManipulationJob {
		DeleteJob(IFile file) {
			super(String.format("Delete %s", file.getFullPath()), file);
		}

		@Override
		protected void manipulateFile(IFile file, IProgressMonitor monitor) throws CoreException {
			file.delete(true, monitor);
		}
	}

	final JobWaiter waitForStart(Job job) {
		return new JobWaiter(job, true);
	}

	final JobWaiter waitForEnd(Job job) {
		return new JobWaiter(job, false);
	}

	final class JobWaiter extends JobChangeAdapter {
		private final Job job;
		private final boolean waitForStart;
		private final long startedAt = System.currentTimeMillis();

		private final CountDownLatch jobSignal = new CountDownLatch(1);

		private JobWaiter(Job job, boolean waitForStart) {
			super();

			this.job = job;
			this.waitForStart = waitForStart;

			Job.getJobManager().addJobChangeListener(this);
		}

		public void waitForJob() {

			try {
				jobSignal.await();
			} catch (InterruptedException e) {
				fail("Test was interrupted");
			} finally {
				Job.getJobManager().removeJobChangeListener(this);
			}
		}

		@Override
		public void aboutToRun(IJobChangeEvent event) {
			log(event.getJob(), "about to run");

			if (waitForStart && (event.getJob() == job)) {
				jobSignal.countDown();
			}
		}

		private void log(Job job, String action) {
			if (DEBUG && Strings.nullToEmpty(job.getName()).toLowerCase().contains("index")) {
				long when = System.currentTimeMillis() - startedAt;
				System.out.printf("[%s:%d] Job %s: %s%n", testName.getMethodName(), when, action, job);
			}
		}

		@Override
		public void done(IJobChangeEvent event) {
			log(event.getJob(), "done");

			if (!waitForStart && (event.getJob() == job)) {
				jobSignal.countDown();
			}
		}

	}
}

Back to the top