/*******************************************************************************
 * Copyright (c) 2016 Ulrich Grave.
 * 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
 */
package com.github.ugrave.tools.ws.refresh.commands;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.github.ugrave.tools.rules.EclipseProjectRule;
import com.github.ugrave.tools.rules.EclipseProjectRule.FileDescription;

public class RefreshWorkspaceHandlerTest {

	private static final String FILE_NAME = "Test 2.txt";

	@Rule
	public EclipseProjectRule projects = EclipseProjectRule.create();

	private IProject project_1;
	private IProject project_2;

	private IHandlerService handlerService;

	@Before
	public void setup() {
		project_1 = projects.createProject("Test 1", FileDescription.newFile("Test.txt").content("Test 123"));
		project_2 = projects.createProject("Test 2");

		IWorkbench workbench = PlatformUI.getWorkbench();
		handlerService = workbench.getService(IHandlerService.class);

	}

	@After
	public void shoutdown() {
		handlerService = null;
	}

	@Test
	public void test_new_file_exists_after_refreshAll() throws Exception {

		File newFile_1 = new File(project_1.getLocation().toFile(), FILE_NAME);
		Assert.assertTrue(newFile_1.createNewFile());
		File newFile_2 = new File(project_2.getLocation().toFile(), FILE_NAME);
		Assert.assertTrue(newFile_2.createNewFile());

		IFile ws_file_1 = project_1.getFile(FILE_NAME);
		IFile ws_file_2 = project_1.getFile(FILE_NAME);

		Assert.assertFalse(ws_file_1.exists());
		Assert.assertFalse(ws_file_2.exists());

		handlerService.executeCommand("com.github.ugrave.tools.ws.refresh.commands.RefreshWorkspaceHandler", null);

		Job refreshJob = findJob("refresh");
		if(refreshJob != null) {
			refreshJob.join(5000L, null);
		}

		Assert.assertTrue(ws_file_1.exists());
		Assert.assertTrue(ws_file_2.exists());
	}

	private Job findJob(String name) {
		for (Job job : Job.getJobManager().find(null)) {
			if(job.getName().equals(name)) {
				return job;
			}
		}
		return null;
	}

}

@@ -597,16 +597,56 @@
 kbench;%0A
+import org.eclipse.ui.IWorkbenchWindow;%0A
 import o
@@ -1445,26 +1445,29 @@
 orkbench
- workbench
+Window window
  = Platf
@@ -1486,16 +1486,43 @@
 kbench()
+.getActiveWorkbenchWindow()
 ;%0A%09%09hand
@@ -1535,24 +1535,21 @@
 vice = w
-orkbench
+indow
 .getServ
@@ -1637,24 +1637,64 @@
 ice = null;%0A
+%09%09project_1 = null;%0A%09%09project_2 = null;%0A
 %09%7D%0A%0A%09@Test%0A%09

Use handler service from active workbench window