Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorspingel2009-09-22 23:26:02 +0000
committerspingel2009-09-22 23:26:02 +0000
commit83e34d1edb4f7c9965ea92ba23e328458224dd55 (patch)
tree4994a541c47b4276337952f0a2e144226f5fbf05 /org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests
parentc08caedce5e55fb8a4c6fae0e2c156f25d428dce (diff)
downloadorg.eclipse.mylyn.tasks-83e34d1edb4f7c9965ea92ba23e328458224dd55.tar.gz
org.eclipse.mylyn.tasks-83e34d1edb4f7c9965ea92ba23e328458224dd55.tar.xz
org.eclipse.mylyn.tasks-83e34d1edb4f7c9965ea92ba23e328458224dd55.zip
NEW - bug 277254: use a single JiraClient instance per repository
https://bugs.eclipse.org/bugs/show_bug.cgi?id=277254
Diffstat (limited to 'org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests')
-rw-r--r--org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java253
-rw-r--r--org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestFixture.java160
-rw-r--r--org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestUtil.java151
-rw-r--r--org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java75
4 files changed, 639 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java
new file mode 100644
index 000000000..84208ff67
--- /dev/null
+++ b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java
@@ -0,0 +1,253 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 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
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tests.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Plugin;
+
+public class FileUtil {
+
+ private final static int MAX_RETRY = 5;
+
+ /**
+ * A buffer.
+ */
+ private static byte[] buffer = new byte[8192];
+
+ /**
+ * Unzips the given zip file to the given destination directory extracting only those entries the pass through the
+ * given filter.
+ *
+ * @param zipFile
+ * the zip file to unzip
+ * @param dstDir
+ * the destination directory
+ * @throws IOException
+ * in case of problem
+ */
+ public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
+ unzip(zipFile, dstDir, dstDir, 0);
+ }
+
+ private static void unzip(ZipFile zipFile, File rootDstDir, File dstDir, int depth) throws IOException {
+
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
+
+ try {
+ while (entries.hasMoreElements()) {
+ ZipEntry entry = entries.nextElement();
+ if (entry.isDirectory()) {
+ continue;
+ }
+ String entryName = entry.getName();
+ File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
+ file.getParentFile().mkdirs();
+ InputStream src = null;
+ OutputStream dst = null;
+ try {
+ src = zipFile.getInputStream(entry);
+ dst = new FileOutputStream(file);
+ transferData(src, dst);
+ } finally {
+ if (dst != null) {
+ try {
+ dst.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ if (src != null) {
+ try {
+ src.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ }
+ }
+ } finally {
+ try {
+ zipFile.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ }
+
+ /**
+ * Returns the given file path with its separator character changed from the given old separator to the given new
+ * separator.
+ *
+ * @param path
+ * a file path
+ * @param oldSeparator
+ * a path separator character
+ * @param newSeparator
+ * a path separator character
+ * @return the file path with its separator character changed from the given old separator to the given new
+ * separator
+ */
+ public static String changeSeparator(String path, char oldSeparator, char newSeparator) {
+ return path.replace(oldSeparator, newSeparator);
+ }
+
+ /**
+ * Copies all bytes in the given source file to the given destination file.
+ *
+ * @param source
+ * the given source file
+ * @param destination
+ * the given destination file
+ * @throws IOException
+ * in case of error
+ */
+ public static void transferData(File source, File destination) throws IOException {
+ destination.getParentFile().mkdirs();
+ InputStream is = null;
+ OutputStream os = null;
+ try {
+ is = new FileInputStream(source);
+ os = new FileOutputStream(destination);
+ transferData(is, os);
+ } finally {
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ }
+ }
+
+ /**
+ * Copies all bytes in the given source stream to the given destination stream. Neither streams are closed.
+ *
+ * @param source
+ * the given source stream
+ * @param destination
+ * the given destination stream
+ * @throws IOException
+ * in case of error
+ */
+ public static void transferData(InputStream source, OutputStream destination) throws IOException {
+ int bytesRead = 0;
+ while (bytesRead != -1) {
+ bytesRead = source.read(buffer, 0, buffer.length);
+ if (bytesRead != -1) {
+ destination.write(buffer, 0, bytesRead);
+ }
+ }
+ }
+
+ /**
+ * Copies the given source file to the given destination file.
+ *
+ * @param src
+ * the given source file
+ * @param dst
+ * the given destination file
+ * @throws IOException
+ * in case of error
+ */
+ public static void copy(File src, File dst) throws IOException {
+ if (src.isDirectory()) {
+ String[] srcChildren = src.list();
+ for (int i = 0; i < srcChildren.length; ++i) {
+ File srcChild = new File(src, srcChildren[i]);
+ File dstChild = new File(dst, srcChildren[i]);
+ copy(srcChild, dstChild);
+ }
+ } else {
+ transferData(src, dst);
+ }
+ }
+
+ public static File createTempFileInPlugin(Plugin plugin, IPath path) {
+ IPath stateLocation = plugin.getStateLocation();
+ stateLocation = stateLocation.append(path);
+ return stateLocation.toFile();
+ }
+
+ public static StringBuffer read(String fileName) throws IOException {
+ return read(new FileReader(fileName));
+ }
+
+ public static StringBuffer read(Reader reader) throws IOException {
+ StringBuffer s = new StringBuffer();
+ try {
+ char[] charBuffer = new char[8196];
+ int chars = reader.read(charBuffer);
+ while (chars != -1) {
+ s.append(charBuffer, 0, chars);
+ chars = reader.read(charBuffer);
+ }
+ } finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ return s;
+ }
+
+ public static void write(String fileName, StringBuffer content) throws IOException {
+ Writer writer = new FileWriter(fileName);
+ try {
+ writer.write(content.toString());
+ } finally {
+ try {
+ writer.close();
+ } catch (IOException e) {
+ // don't need to catch this
+ }
+ }
+ }
+
+ public static void delete(File file) {
+ if (file.exists()) {
+ for (int i = 0; i < MAX_RETRY; i++) {
+ if (file.delete()) {
+ i = MAX_RETRY;
+ } else {
+ try {
+ Thread.sleep(1000); // sleep a second
+ } catch (InterruptedException e) {
+ // don't need to catch this
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestFixture.java b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestFixture.java
new file mode 100644
index 000000000..c99090b7e
--- /dev/null
+++ b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestFixture.java
@@ -0,0 +1,160 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Tasktop Technologies 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:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tests.util;
+
+import java.net.Proxy;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.mylyn.commons.net.AbstractWebLocation;
+import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
+import org.eclipse.mylyn.commons.net.AuthenticationType;
+import org.eclipse.mylyn.commons.net.IProxyProvider;
+import org.eclipse.mylyn.commons.net.WebLocation;
+import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
+import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
+import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
+import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
+import org.eclipse.mylyn.tasks.core.TaskRepository;
+import org.eclipse.mylyn.tasks.ui.TasksUi;
+import org.eclipse.mylyn.tests.util.TestUtil.Credentials;
+import org.eclipse.mylyn.tests.util.TestUtil.PrivilegeLevel;
+
+/**
+ * @author Steffen Pingel
+ * @author Thomas Ehrnhoefer
+ */
+public abstract class TestFixture {
+
+ private final String connectorKind;
+
+ private String info;
+
+ protected final String repositoryUrl;
+
+ protected AbstractRepositoryConnector connector;
+
+ public TestFixture(String connectorKind, String repositoryUrl) {
+ this.connectorKind = connectorKind;
+ this.repositoryUrl = repositoryUrl;
+ }
+
+ protected abstract TestFixture activate();
+
+ public void add(TestSuite suite, Class<? extends TestCase> clazz) {
+ if (Boolean.parseBoolean(System.getProperty("mylyn.tests.annotate")) && getInfo() != null) {
+ suite.addTest(new TestSuite(clazz, clazz.getName() + " [" + getInfo() + "]"));
+ } else {
+ suite.addTestSuite(clazz);
+ }
+ }
+
+ public TestSuite createSuite() {
+ TestSuite suite = new TestSuite("Testing on " + getInfo());
+ suite.addTest(new TestCase("activiating " + getRepositoryUrl()) {
+ @Override
+ protected void runTest() throws Throwable {
+ activate();
+ }
+ });
+ return suite;
+ }
+
+ public String getConnectorKind() {
+ return connectorKind;
+ }
+
+ public String getInfo() {
+ return info;
+ }
+
+ public String getRepositoryUrl() {
+ return repositoryUrl;
+ }
+
+ public AbstractWebLocation location() throws Exception {
+ return location(PrivilegeLevel.USER);
+ }
+
+ public AbstractWebLocation location(PrivilegeLevel level) throws Exception {
+ return location(level, Proxy.NO_PROXY);
+ }
+
+ public AbstractWebLocation location(PrivilegeLevel level, Proxy proxy) throws Exception {
+ Credentials credentials = TestUtil.readCredentials(level);
+ return location(credentials.username, credentials.password, proxy);
+ }
+
+ public AbstractWebLocation location(String username, String password) throws Exception {
+ return location(username, password, Proxy.NO_PROXY);
+ }
+
+ public AbstractWebLocation location(String username, String password, final Proxy proxy) throws Exception {
+ return new WebLocation(repositoryUrl, username, password, new IProxyProvider() {
+ public Proxy getProxyForHost(String host, String proxyType) {
+ return proxy;
+ }
+ });
+ }
+
+ public TaskRepository repository() {
+ TaskRepository repository = new TaskRepository(connectorKind, repositoryUrl);
+ Credentials credentials = TestUtil.readCredentials(PrivilegeLevel.USER);
+ repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials(credentials.username,
+ credentials.password), false);
+ return repository;
+ }
+
+ protected void setInfo(String info) {
+ this.info = info;
+ }
+
+ public TaskRepository singleRepository() {
+ TaskRepositoryManager manager = TasksUiPlugin.getRepositoryManager();
+ manager.clearRepositories();
+
+ TaskRepository repository = new TaskRepository(connectorKind, repositoryUrl);
+ Credentials credentials = TestUtil.readCredentials(PrivilegeLevel.USER);
+ repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials(credentials.username,
+ credentials.password), false);
+ manager.addRepository(repository);
+ return repository;
+ }
+
+ public AbstractRepositoryConnector connector() {
+ return connector;
+ }
+
+ /**
+ * Clears tasks and repositories. When this method returns only the local task repository will exist and the task
+ * list will only have default categories but no tasks.
+ */
+ public static void resetTaskListAndRepositories() throws Exception {
+ TasksUiPlugin.getRepositoryManager().clearRepositories();
+ TasksUiPlugin.getDefault().getLocalTaskRepository();
+ resetTaskList();
+ }
+
+ /**
+ * Clears all tasks.
+ */
+ public static void resetTaskList() throws Exception {
+ TasksUi.getTaskActivityManager().deactivateActiveTask();
+ TasksUiPlugin.getTaskListExternalizationParticipant().resetTaskList();
+ TaskListView view = TaskListView.getFromActivePerspective();
+ if (view != null) {
+ view.refresh();
+ }
+ }
+
+}
diff --git a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestUtil.java b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestUtil.java
new file mode 100644
index 000000000..c5231550c
--- /dev/null
+++ b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestUtil.java
@@ -0,0 +1,151 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2008 Tasktop Technologies 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:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tests.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Properties;
+
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
+import org.osgi.framework.Bundle;
+
+/**
+ * @author Steffen Pingel
+ */
+public class TestUtil {
+
+ public static final String KEY_CREDENTIALS_FILE = "mylyn.credentials";
+
+ public enum PrivilegeLevel {
+ ANONYMOUS, GUEST, USER, ADMIN
+ };
+
+ public static class Credentials {
+
+ public final String username;
+
+ public final String password;
+
+ public Credentials(String username, String password) {
+ this.username = username;
+ this.password = password;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getName() + " [username=" + username + ",password=" + password + "]";
+ }
+ }
+
+ public static Credentials readCredentials() {
+ return readCredentials(PrivilegeLevel.USER, null);
+ }
+
+ public static Credentials readCredentials(PrivilegeLevel level) {
+ return readCredentials(level, null);
+ }
+
+ public static Credentials readCredentials(PrivilegeLevel level, String realm) {
+ Properties properties = new Properties();
+ try {
+ File file;
+ String filename = System.getProperty(KEY_CREDENTIALS_FILE);
+ if (filename == null) {
+ file = getFile("org.eclipse.context.tests", TestUtil.class, "credentials.properties");
+ } else {
+ file = new File(filename);
+ }
+ properties.load(new FileInputStream(file));
+ } catch (Exception e) {
+ throw new AssertionFailedError("must define credentials in <plug-in dir>/credentials.properties");
+ }
+
+ String defaultPassword = properties.getProperty("pass");
+
+ realm = (realm != null) ? realm + "." : "";
+ switch (level) {
+ case ANONYMOUS:
+ return createCredentials(properties, realm + "anon.", "", "");
+ case GUEST:
+ return createCredentials(properties, realm + "guest.", "guest@mylyn.eclipse.org", defaultPassword);
+ case USER:
+ return createCredentials(properties, realm, "tests@mylyn.eclipse.org", defaultPassword);
+ case ADMIN:
+ return createCredentials(properties, realm + "admin.", "admin@mylyn.eclipse.org", null);
+ }
+
+ throw new AssertionFailedError("invalid privilege level");
+ }
+
+ private static Credentials createCredentials(Properties properties, String prefix, String defaultUsername,
+ String defaultPassword) {
+ String username = properties.getProperty(prefix + "user");
+ String password = properties.getProperty(prefix + "pass");
+
+ if (username == null) {
+ username = defaultUsername;
+ }
+
+ if (password == null) {
+ password = defaultPassword;
+ }
+
+ if (username == null || password == null) {
+ throw new AssertionFailedError(
+ "username or password not found in <plug-in dir>/credentials.properties, make sure file is valid");
+ }
+
+ return new Credentials(username, password);
+ }
+
+ public static File getFile(String bundleId, Class<?> clazz, String filename) throws IOException {
+ Bundle bundle = Platform.getBundle(bundleId);
+ if (bundle != null) {
+ URL localURL = FileLocator.toFileURL(bundle.getEntry(filename));
+ filename = localURL.getFile();
+ } else {
+ URL localURL = clazz.getResource("");
+ String path = localURL.getFile();
+ int i = path.indexOf("!");
+ if (i != -1) {
+ int j = path.lastIndexOf(File.separatorChar, i);
+ if (j != -1) {
+ path = path.substring(0, j) + File.separator;
+ } else {
+ Assert.fail("Unable to determine location for '" + filename + "' at '" + path + "'");
+ }
+ // class file is nested in jar, use jar path as base
+ if (path.startsWith("file:")) {
+ path = path.substring(5);
+ }
+ } else {
+ // create relative path to base of class file location
+ String[] tokens = path.split("\\.");
+ for (@SuppressWarnings("unused")
+ String token : tokens) {
+ path += ".." + File.separator;
+ }
+ if (path.contains("bin" + File.separator)) {
+ path += ".." + File.separator;
+ }
+ }
+ filename = path + filename.replaceAll("/", File.separator);
+ }
+ return new File(filename);
+ }
+}
diff --git a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java
new file mode 100644
index 000000000..f58068409
--- /dev/null
+++ b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2009 Tasktop Technologies 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:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.tests.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IViewReference;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * @author Mik Kersten
+ */
+public class UiTestUtil {
+
+ public static int countItemsInTree(Tree tree) {
+ List<TreeItem> collectedItems = new ArrayList<TreeItem>();
+ collectTreeItemsInView(tree.getItems(), collectedItems);
+ return collectedItems.size();
+ }
+
+ public static void collectTreeItemsInView(TreeItem[] items, List<TreeItem> collectedItems) {
+ if (items.length > 0) {
+ for (TreeItem childItem : Arrays.asList(items)) {
+ collectedItems.add(childItem);
+ collectTreeItemsInView(childItem.getItems(), collectedItems);
+ }
+ }
+ }
+
+ public static List<Object> getAllData(Tree tree) {
+ List<TreeItem> items = new ArrayList<TreeItem>();
+ collectTreeItemsInView(tree.getItems(), items);
+ List<Object> dataList = new ArrayList<Object>();
+ for (TreeItem item : items) {
+ dataList.add(item.getData());
+ }
+ return dataList;
+ }
+
+ /**
+ * Ensures that the editor area is visible.
+ */
+ public static void closeWelcomeView() {
+ IViewReference[] views = PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow()
+ .getActivePage()
+ .getViewReferences();
+ for (IViewReference view : views) {
+ if ("org.eclipse.ui.internal.introview".equals(view.getId())) {
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view);
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().resetPerspective();
+ return;
+ }
+ }
+ }
+
+ public static IViewPart openView(String id) throws PartInitException {
+ return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
+ }
+}

Back to the top