NEW - bug 191522: Provide full text search functionality over task
comments 
https://bugs.eclipse.org/bugs/show_bug.cgi?id=191522

improve test reliability by implementing synchronous index updates for
the purpose of testing. Job.join() has different behaviour when job
manager is not running
diff --git a/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java b/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java
index f21d8fc..e024554 100644
--- a/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java
+++ b/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java
@@ -49,6 +49,7 @@
 import org.eclipse.core.runtime.CoreException;

 import org.eclipse.core.runtime.IProgressMonitor;

 import org.eclipse.core.runtime.IStatus;

+import org.eclipse.core.runtime.NullProgressMonitor;

 import org.eclipse.core.runtime.Platform;

 import org.eclipse.core.runtime.Status;

 import org.eclipse.core.runtime.SubMonitor;

@@ -114,9 +115,13 @@
 		}

 	}

 

+	private static enum MaintainIndexType {

+		STARTUP, REINDEX

+	}

+

 	private Directory directory;

 

-	private Job maintainIndexJob;

+	private MaintainIndexJob maintainIndexJob;

 

 	private final Map<ITask, TaskData> reindexQueue = new HashMap<ITask, TaskData>();

 

@@ -218,7 +223,32 @@
 		dataManager.addListener(this);

 		taskList.addChangeListener(this);

 

-		maintainIndexJob.schedule(startupDelay);

+		scheduleIndexMaintenance(MaintainIndexType.STARTUP);

+	}

+

+	private void scheduleIndexMaintenance(MaintainIndexType type) {

+		long delay = 0L;

+		switch (type) {

+		case STARTUP:

+			delay = startupDelay;

+			break;

+		case REINDEX:

+			delay = reindexDelay;

+		}

+

+		if (delay == 0L) {

+			// primarily for testing purposes

+

+			maintainIndexJob.cancel();

+			try {

+				maintainIndexJob.join();

+			} catch (InterruptedException e) {

+				// ignore

+			}

+			maintainIndexJob.run(new NullProgressMonitor());

+		} else {

+			maintainIndexJob.schedule(delay);

+		}

 	}

 

 	public boolean matches(ITask task, String patternString) {

@@ -272,7 +302,7 @@
 

 	public void reindex() {

 		rebuildIndex = true;

-		maintainIndexJob.schedule(reindexDelay);

+		scheduleIndexMaintenance(MaintainIndexType.REINDEX);

 	}

 

 	/**

@@ -281,9 +311,10 @@
 	 * @throws InterruptedException

 	 */

 	public void waitUntilIdle() throws InterruptedException {

-		// FIXME: this doesn't work with unit tests, since the job manager is not running

 		if (!Platform.isRunning()) {

-			Thread.sleep(150L);

+			// job join() behaviour is not the same when platform is not running

+			Logger.getLogger(TaskListIndex.class.getName()).warning(

+					"Index job joining may not work properly when Eclipse platform is not running"); //$NON-NLS-1$

 		}

 		maintainIndexJob.join();

 	}

@@ -396,7 +427,7 @@
 		} catch (CorruptIndexException e) {

 			rebuildIndex = true;

 			if (maintainIndexJob != null) {

-				maintainIndexJob.schedule(reindexDelay);

+				scheduleIndexMaintenance(MaintainIndexType.REINDEX);

 			}

 		} catch (FileNotFoundException e) {

 			rebuildIndex = true;

@@ -440,7 +471,7 @@
 		synchronized (reindexQueue) {

 			reindexQueue.put(task, taskData);

 		}

-		maintainIndexJob.schedule(reindexDelay);

+		scheduleIndexMaintenance(MaintainIndexType.REINDEX);

 	}

 

 	private void addIndexedAttributes(Document document, TaskAttribute parentAttribute) {

@@ -510,7 +541,7 @@
 		}

 	}

 

-	public class MaintainIndexJob extends Job {

+	private class MaintainIndexJob extends Job {

 

 		public MaintainIndexJob() {

 			super("Task List Indexer");

@@ -520,7 +551,7 @@
 		}

 

 		@Override

-		protected IStatus run(IProgressMonitor m) {

+		public IStatus run(IProgressMonitor m) {

 			final int WORK_PER_SEGMENT = 1000;

 			SubMonitor monitor = SubMonitor.convert(m, 3 * WORK_PER_SEGMENT);

 			try {

diff --git a/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java b/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java
index 818ceed..5781fdf 100644
--- a/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java
+++ b/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java
@@ -83,7 +83,7 @@
 		}

 		if (tempDir != null) {

 			delete(tempDir);

-			// FIXME assertFalse(tempDir.exists());

+			assertFalse(tempDir.exists());

 		}

 	}