From 40a9876b3a54f1d5fdc393720f0407f96a0c991b Mon Sep 17 00:00:00 2001 From: spingel Date: Wed, 23 Sep 2009 00:56:34 +0000 Subject: NEW - bug 290198: move test utilities to a separate plug-in https://bugs.eclipse.org/bugs/show_bug.cgi?id=290198 --- .../src/org/eclipse/mylyn/tests/util/FileUtil.java | 253 --------------------- .../src/org/eclipse/mylyn/tests/util/TestUtil.java | 78 +++---- .../org/eclipse/mylyn/tests/util/UiTestUtil.java | 75 ------ 3 files changed, 39 insertions(+), 367 deletions(-) delete mode 100644 org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java delete mode 100644 org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java (limited to 'org.eclipse.mylyn.tests.util/src/org') 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 deleted file mode 100644 index 84208ff67..000000000 --- a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/FileUtil.java +++ /dev/null @@ -1,253 +0,0 @@ -/******************************************************************************* - * 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 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/TestUtil.java b/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/TestUtil.java index 86d3f94f5..b0eb3d490 100644 --- 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 @@ -14,15 +14,11 @@ 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; +import org.eclipse.mylyn.commons.tests.support.CommonTestUtil; /** * @author Steffen Pingel @@ -66,7 +62,7 @@ public class TestUtil { File file; String filename = System.getProperty(KEY_CREDENTIALS_FILE); if (filename == null) { - file = getFile("org.eclipse.mylyn.context.tests", TestUtil.class, "credentials.properties"); + file = getFile(TestUtil.class, "credentials.properties"); if (!file.exists()) { // lookup may have reverted to this plug-in, try to lookup file in org.eclipse.context.tests plug-in File path = new File(file.getParentFile().getParentFile(), "org.eclipse.mylyn.context.tests"); @@ -118,38 +114,42 @@ public class TestUtil { 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 = clazz.getName().split("\\."); - for (int j = 0; j < tokens.length - 1; j++) { - path += ".." + File.separator; - } - if (path.contains("bin" + File.separator)) { - path += ".." + File.separator; - } - } - filename = path + filename.replaceAll("/", File.separator); - } - return new File(filename).getCanonicalFile(); + public static File getFile(Object source, String filename) throws IOException { + return CommonTestUtil.getFile(source, filename); } + +// 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 = clazz.getName().split("\\."); +// for (int j = 0; j < tokens.length - 1; j++) { +// path += ".." + File.separator; +// } +// if (path.contains("bin" + File.separator)) { +// path += ".." + File.separator; +// } +// } +// filename = path + filename.replaceAll("/", File.separator); +// } +// return new File(filename).getCanonicalFile(); +// } } 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 deleted file mode 100644 index f58068409..000000000 --- a/org.eclipse.mylyn.tests.util/src/org/eclipse/mylyn/tests/util/UiTestUtil.java +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - * 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 collectedItems = new ArrayList(); - collectTreeItemsInView(tree.getItems(), collectedItems); - return collectedItems.size(); - } - - public static void collectTreeItemsInView(TreeItem[] items, List collectedItems) { - if (items.length > 0) { - for (TreeItem childItem : Arrays.asList(items)) { - collectedItems.add(childItem); - collectTreeItemsInView(childItem.getItems(), collectedItems); - } - } - } - - public static List getAllData(Tree tree) { - List items = new ArrayList(); - collectTreeItemsInView(tree.getItems(), items); - List dataList = new ArrayList(); - 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); - } -} -- cgit v1.2.3