tests: ProjectSetup helper class to simplify workspace initialization in
a junit4 way
diff --git a/core/tests/org.eclipse.dltk.core.tests/META-INF/MANIFEST.MF b/core/tests/org.eclipse.dltk.core.tests/META-INF/MANIFEST.MF
index 7d5da49..20a5cf2 100644
--- a/core/tests/org.eclipse.dltk.core.tests/META-INF/MANIFEST.MF
+++ b/core/tests/org.eclipse.dltk.core.tests/META-INF/MANIFEST.MF
@@ -6,7 +6,7 @@
Bundle-Activator: org.eclipse.dltk.core.tests.model.ModelTestsPlugin
Bundle-Vendor: %pluginProvider
Bundle-Localization: plugin
-Require-Bundle: org.junit;bundle-version="3.8.2",
+Require-Bundle: org.junit;bundle-version="4.8",
org.eclipse.core.runtime,
org.eclipse.core.resources,
org.eclipse.team.core,
diff --git a/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/AbstractProjectSetup.java b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/AbstractProjectSetup.java
new file mode 100644
index 0000000..c94b4db
--- /dev/null
+++ b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/AbstractProjectSetup.java
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ * Copyright (c) 2012 NumberFour AG
+ *
+ * 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:
+ * NumberFour AG - initial API and Implementation (Alex Panchenko)
+ *******************************************************************************/
+package org.eclipse.dltk.core.tests;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.IProjectFragment;
+import org.eclipse.dltk.core.IScriptFolder;
+import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.dltk.core.ISourceModule;
+import org.eclipse.dltk.core.ModelException;
+import org.junit.rules.ExternalResource;
+
+public abstract class AbstractProjectSetup extends ExternalResource {
+
+ public abstract IProject get();
+
+ /**
+ * Returns this project as {@link IScriptProject}
+ */
+ public IScriptProject getScriptProject() {
+ return DLTKCore.create(get());
+ }
+
+ /**
+ * Returns the specified file from this project.
+ */
+ public IFile getFile(String name) {
+ return get().getFile(name);
+ }
+
+ /**
+ * Returns the specified file from this project.
+ */
+ public IFile getFile(IPath path) {
+ return get().getFile(path);
+ }
+
+ /**
+ * Returns the specified package fragment root in this project, or
+ * <code>null</code> if it does not exist. If relative, the rootPath must be
+ * specified as a project relative path. The empty path refers to the
+ * package fragment root that is the project folder iteslf. If absolute, the
+ * rootPath refers to either an external zip, or a resource internal to the
+ * workspace
+ */
+ public IProjectFragment getProjectFragment(String fragmentPath)
+ throws ModelException {
+ final IScriptProject project = getScriptProject();
+ if (project == null) {
+ return null;
+ }
+ final IPath path = new Path(fragmentPath);
+ if (path.isAbsolute()) {
+ final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace()
+ .getRoot();
+ final IResource resource = workspaceRoot.findMember(path);
+ if (resource == null) {
+ return null;
+ }
+ // resource in the workspace
+ return project.getProjectFragment(resource);
+ } else {
+ final IProjectFragment[] roots = project.getProjectFragments();
+ if (roots == null || roots.length == 0) {
+ return null;
+ }
+ for (IProjectFragment root : roots) {
+ if (root.getUnderlyingResource().getProjectRelativePath()
+ .equals(path)) {
+ return root;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the specified script folder in this project and the given
+ * fragment, or <code>null</code> if it does not exist. The rootPath must be
+ * specified as a project relative path. The empty path refers to the
+ * default package fragment.
+ */
+ public IScriptFolder getScriptFolder(String fragmentPath, IPath path)
+ throws ModelException {
+ final IProjectFragment root = getProjectFragment(fragmentPath);
+ if (root == null) {
+ return null;
+ }
+ return root.getScriptFolder(path);
+ }
+
+ /**
+ * Returns the specified source module in this project and the given root
+ * and folder or <code>null</code> if it does not exist.
+ */
+ public ISourceModule getSourceModule(String rootPath, IPath path)
+ throws ModelException {
+ final IScriptFolder folder = getScriptFolder(rootPath,
+ path.removeLastSegments(1));
+ if (folder == null) {
+ return null;
+ }
+ return folder.getSourceModule(path.lastSegment());
+ }
+
+ /**
+ * Returns the specified source module in this project and the given root
+ * and folder or <code>null</code> if it does not exist.
+ */
+ public ISourceModule getSourceModule(String rootPath, String path)
+ throws ModelException {
+ return getSourceModule(rootPath, new Path(path));
+ }
+
+}
diff --git a/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/IWorkspaceSetup.java b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/IWorkspaceSetup.java
new file mode 100644
index 0000000..f777cc9
--- /dev/null
+++ b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/IWorkspaceSetup.java
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2012 NumberFour AG
+ *
+ * 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:
+ * NumberFour AG - initial API and Implementation (Alex Panchenko)
+ *******************************************************************************/
+package org.eclipse.dltk.core.tests;
+
+public interface IWorkspaceSetup {
+
+ void before() throws Throwable;
+
+ String getBundleName();
+
+ void after();
+
+}
diff --git a/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/ProjectSetup.java b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/ProjectSetup.java
new file mode 100644
index 0000000..e12db9d
--- /dev/null
+++ b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/ProjectSetup.java
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * Copyright (c) 2012 NumberFour AG
+ *
+ * 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:
+ * NumberFour AG - initial API and Implementation (Alex Panchenko)
+ *******************************************************************************/
+package org.eclipse.dltk.core.tests;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.dltk.internal.core.util.Util;
+import org.junit.Assert;
+
+// TODO waitUntilIndexesReady
+// TODO DISABLE_INDEXER
+// TODO BUILD
+public class ProjectSetup extends AbstractProjectSetup {
+
+ private final IWorkspaceSetup workspaceSetup;
+ private final String projectName;
+ private BundledProjectSetup.Helper helper;
+ private IProject project;
+
+ public ProjectSetup(IWorkspaceSetup workspaceSetup, String projectName) {
+ this.workspaceSetup = workspaceSetup;
+ this.projectName = projectName;
+ }
+
+ @Override
+ public void before() throws Throwable {
+ workspaceSetup.before();
+ helper = new BundledProjectSetup.Helper(workspaceSetup.getBundleName());
+ project = helper.setUpProject(projectName);
+ }
+
+ @Override
+ public void after() {
+ if (helper != null) {
+ try {
+ helper.deleteProject(projectName);
+ } catch (CoreException e) {
+ e.printStackTrace();
+ }
+ helper = null;
+ }
+ project = null;
+ workspaceSetup.after();
+ }
+
+ public String getProjectName() {
+ return projectName;
+ }
+
+ /**
+ * Returns this project as IProject
+ */
+ @Override
+ public IProject get() {
+ Assert.assertNotNull(
+ "ProjectSetup " + projectName + " not initialized", project);
+ return project;
+ }
+
+ public String getFileContentsAsString(String name) throws CoreException {
+ return new String(Util.getResourceContentsAsCharArray(getFile(name)));
+ }
+}
diff --git a/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/WorkspaceSetup.java b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/WorkspaceSetup.java
new file mode 100644
index 0000000..a774a88
--- /dev/null
+++ b/core/tests/org.eclipse.dltk.core.tests/src/org/eclipse/dltk/core/tests/WorkspaceSetup.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2012 NumberFour AG
+ *
+ * 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:
+ * NumberFour AG - initial API and Implementation (Alex Panchenko)
+ *******************************************************************************/
+package org.eclipse.dltk.core.tests;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
+
+public class WorkspaceSetup implements IWorkspaceSetup {
+
+ private final String bundleName;
+
+ public WorkspaceSetup(String bundleName) {
+ this.bundleName = bundleName;
+ }
+
+ public WorkspaceSetup(Bundle bundle) {
+ this.bundleName = bundle.getSymbolicName();
+ }
+
+ public WorkspaceSetup(Class<?> classFromBundle) {
+ this(FrameworkUtil.getBundle(classFromBundle));
+ }
+
+ public String getBundleName() {
+ return bundleName;
+ }
+
+ public void before() throws Throwable {
+ WorkspaceAutoBuild.disable();
+ }
+
+ public void after() {
+ }
+
+}
diff --git a/core/tests/org.eclipse.dltk.ui.tests/src/org/eclipse/dltk/ui/tests/UITestUtils.java b/core/tests/org.eclipse.dltk.ui.tests/src/org/eclipse/dltk/ui/tests/UITestUtils.java
new file mode 100644
index 0000000..1efb828
--- /dev/null
+++ b/core/tests/org.eclipse.dltk.ui.tests/src/org/eclipse/dltk/ui/tests/UITestUtils.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2012 NumberFour AG
+ *
+ * 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:
+ * NumberFour AG - initial API and Implementation (Alex Panchenko)
+ *******************************************************************************/
+package org.eclipse.dltk.ui.tests;
+
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+
+public class UITestUtils {
+
+ private static void closeEditor(IEditorPart editor) {
+ IWorkbenchPartSite site;
+ IWorkbenchPage page;
+ if (editor != null && (site = editor.getSite()) != null
+ && (page = site.getPage()) != null)
+ page.closeEditor(editor, false);
+ }
+
+ public static void closeAllEditors() {
+ IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
+ .getWorkbenchWindows();
+ for (int i = 0; i < windows.length; i++) {
+ IWorkbenchPage[] pages = windows[i].getPages();
+ for (int j = 0; j < pages.length; j++) {
+ IEditorReference[] editorReferences = pages[j]
+ .getEditorReferences();
+ for (int k = 0; k < editorReferences.length; k++)
+ closeEditor(editorReferences[k].getEditor(false));
+ }
+ }
+ }
+}