From 64d1dcc72f444d6b6236902c6a63a6574130ec6d Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 19 Feb 2022 16:41:54 +0100 Subject: [PATCH] Unify and clean-up pde.ui.tests and replace the activator Remove unused code and unify and modernize remaining code. --- .../META-INF/MANIFEST.MF | 1 - .../org/eclipse/pde/ui/tests/PDETestCase.java | 59 +++++-- .../eclipse/pde/ui/tests/PDETestsPlugin.java | 43 ----- .../AbstractBuildValidationTest.java | 112 +++---------- .../BuildPropertiesValidationTest.java | 12 -- .../pde/ui/tests/ee/ExportBundleTests.java | 59 ++----- .../parts/AbstractSchemaPerfTest.java | 7 +- .../parts/InitializeModelsPerfTest.java | 26 +-- .../parts/OpenManifestEditorPerfTest.java | 8 +- .../parts/TargetPlatformPerfTest.java | 64 ++------ .../tests/project/ProjectCreationTests.java | 10 +- .../runtime/AbstractRegistryModelTest.java | 8 +- .../pde/ui/tests/runtime/TestUtils.java | 23 +-- .../ui/tests/target/AbstractTargetTest.java | 151 +++--------------- .../tests/target/IUBundleContainerTests.java | 4 +- .../target/LocalTargetDefinitionTests.java | 30 ++-- ...argetDefinitionFeatureResolutionTests.java | 26 +-- .../TargetDefinitionPersistenceTests.java | 12 +- .../TargetDefinitionResolutionTests.java | 33 ++-- 19 files changed, 200 insertions(+), 488 deletions(-) delete mode 100644 ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestsPlugin.java diff --git a/ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF b/ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF index 9531835e30..7b6b867c18 100644 --- a/ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF +++ b/ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF @@ -4,7 +4,6 @@ Bundle-Name: PDE JUnit Tests Bundle-SymbolicName: org.eclipse.pde.ui.tests; singleton:=true Bundle-Version: 3.12.500.qualifier Bundle-ClassPath: tests.jar -Bundle-Activator: org.eclipse.pde.ui.tests.PDETestsPlugin Bundle-Vendor: Eclipse.org Require-Bundle: org.eclipse.pde.ui, org.eclipse.ui, diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestCase.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestCase.java index ed5cc4a152..c3bf6b38d9 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestCase.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestCase.java @@ -13,11 +13,12 @@ *******************************************************************************/ package org.eclipse.pde.ui.tests; +import static org.junit.Assert.assertNotNull; import static org.junit.Assume.assumeTrue; +import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.StackWalker.Option; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -27,6 +28,8 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -34,9 +37,10 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.ICoreRunnable; -import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.URIUtil; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.dialogs.ErrorDialog; @@ -64,8 +68,6 @@ */ public abstract class PDETestCase { - private static final StackWalker STACK_WALKER = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE); - private static boolean welcomeClosed; @Rule public TestName name = new TestName(); @@ -75,18 +77,18 @@ public void setUp() throws Exception { MessageDialog.AUTOMATED_MODE = true; ErrorDialog.AUTOMATED_MODE = true; FreezeMonitor.expectCompletionInAMinute(); - TestUtils.log(IStatus.INFO, name.getMethodName(), "setUp"); + ILog.get().log(Status.info("[" + name.getMethodName() + "] " + "setUp")); assertWelcomeScreenClosed(); } @After public void tearDown() throws Exception { - TestUtils.log(IStatus.INFO, name.getMethodName(), "tearDown"); + ILog.get().log(Status.info("[" + name.getMethodName() + "] " + "tearDown")); // Close any editors we opened IWorkbenchWindow[] workbenchPages = PlatformUI.getWorkbench().getWorkbenchWindows(); for (IWorkbenchWindow workbenchPage : workbenchPages) { IWorkbenchPage page = workbenchPage.getActivePage(); - if (page != null){ + if (page != null) { page.closeAllEditors(false); } } @@ -107,7 +109,7 @@ public void tearDown() throws Exception { } else { error.addSuppressed(e); } - PDETestsPlugin.getDefault().getLog().error(message, e); + ILog.get().error(message, e); } } TestUtils.waitForJobs(name.getMethodName(), 10, 10000); @@ -195,8 +197,7 @@ public static void assumeRunningInStandaloneEclipseSDK() { */ public static void copyFromThisBundleInto(String rootPath, Path targetRoot) throws IOException, URISyntaxException { - Class caller = STACK_WALKER.getCallerClass(); - Bundle bundle = FrameworkUtil.getBundle(caller); + Bundle bundle = FrameworkUtil.getBundle(PDETestCase.class); URI rootEntry = bundle.getEntry(rootPath).toURI(); List entries = Collections.list(bundle.findEntries(rootPath, null, true)); for (URL entry : entries) { @@ -210,4 +211,42 @@ public static void copyFromThisBundleInto(String rootPath, Path targetRoot) } } } + + public static Path getThisBundlesStateLocation() { + Bundle bundle = FrameworkUtil.getBundle(PDETestCase.class); + return Platform.getStateLocation(bundle).toPath(); + } + + public static Path doUnZip(Path targetDirectory, String archivePath) throws IOException { + URL zipURL = FrameworkUtil.getBundle(PDETestCase.class).getEntry(archivePath); + assertNotNull("Zip file not found at path " + archivePath, zipURL); + try (ZipInputStream zipStream = new ZipInputStream(zipURL.openStream())) { + for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream.getNextEntry()) { + if (!entry.isDirectory()) { + Path file = targetDirectory.resolve(entry.getName()); + Files.createDirectories(file.getParent()); + Files.copy(zipStream, file, StandardCopyOption.REPLACE_EXISTING); + } + } + return targetDirectory; + } + } + + /** + * Recursively deletes the directory and files within. + * + * @param dir + * directory to delete + */ + public static void delete(File dir) { + File[] files = dir.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + delete(file); + } else { + file.delete(); + } + } + dir.delete(); + } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestsPlugin.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestsPlugin.java deleted file mode 100644 index bc8d434965..0000000000 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestsPlugin.java +++ /dev/null @@ -1,43 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.pde.ui.tests; - -import org.eclipse.ui.plugin.AbstractUIPlugin; -import org.osgi.framework.BundleContext; - -/** - * The main plugin class to be used in the desktop. - */ -public class PDETestsPlugin extends AbstractUIPlugin { - - private static PDETestsPlugin fgDefault = null; - - public PDETestsPlugin() { - fgDefault = this; - } - - /** - * Returns the test plug-in. - * - * @return the test plug-in - */ - public static PDETestsPlugin getDefault() { - return fgDefault; - } - - public static BundleContext getBundleContext() { - return getDefault().getBundle().getBundleContext(); - } - -} diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/AbstractBuildValidationTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/AbstractBuildValidationTest.java index d35e7d057b..34c203eac9 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/AbstractBuildValidationTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/AbstractBuildValidationTest.java @@ -17,28 +17,19 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileFilter; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Enumeration; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.PropertyResourceBundle; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; +import java.util.stream.Stream; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.jobs.Job; @@ -50,9 +41,12 @@ import org.eclipse.pde.internal.core.natures.PDE; import org.eclipse.pde.internal.ui.PDEPlugin; import org.eclipse.pde.internal.ui.correction.ResolutionGenerator; -import org.eclipse.pde.ui.tests.PDETestsPlugin; +import org.eclipse.pde.ui.tests.PDETestCase; +import org.eclipse.pde.ui.tests.util.ProjectUtils; import org.eclipse.ui.IMarkerResolution; -import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.rules.TestRule; import org.osgi.service.prefs.BackingStoreException; /** @@ -65,35 +59,27 @@ */ public abstract class AbstractBuildValidationTest { + @ClassRule + public static final TestRule CLEAR_WORKSPACE = ProjectUtils.DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER; + private static final String MARKER = "marker"; private static final String MULTIPLE_MARKERS = "multipleMarkers"; - @Before - public void setUp() throws Exception { - URL location = PDETestsPlugin.getBundleContext().getBundle().getEntry("/tests/build.properties/build.properties.tests.zip"); - File projectFile = new File(FileLocator.toFileURL(location).getFile()); - assertTrue("Could not find test zip file at " + projectFile, projectFile.isFile()); - doUnZip(PDETestsPlugin.getDefault().getStateLocation().removeLastSegments(2), "/tests/build.properties/build.properties.tests.zip"); + @BeforeClass + public static void setUp() throws Exception { + Path workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toPath(); + PDETestCase.doUnZip(workspaceLocation, "/tests/build.properties/build.properties.tests.zip"); - projectFile = PDETestsPlugin.getDefault().getStateLocation().removeLastSegments(3).toFile(); - File[] projects = projectFile.listFiles((FileFilter) pathname -> { - int index = pathname.getName().lastIndexOf('.'); - if (index > 1 && pathname.isDirectory()) { // look out for "CVS" - // files in the - // workspace - return true; - } - return false; - }); - for (File projectFileName : projects) { - IProject project = findProject(projectFileName.getName()); - if (project.exists()) { - project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); - project.delete(true, new NullProgressMonitor()); - } - project.create(new NullProgressMonitor()); - project.open(new NullProgressMonitor()); + try (Stream directories = Files.walk(workspaceLocation, 1)) { + var projectNames = directories.filter(Files::isDirectory) + .filter(d -> Files.exists(d.resolve(IProjectDescription.DESCRIPTION_FILE_NAME))) + .map(d -> d.getFileName().toString()); + for (String projectName : (Iterable) projectNames::iterator) { + IProject project = findProject(projectName); + project.create(new NullProgressMonitor()); + project.open(new NullProgressMonitor()); + } } } @@ -220,37 +206,6 @@ private String getProperty(PropertyResourceBundle propertyBundle, String entry, return getProperty(propertyBundle, entry + '.' + property); } - /** - * Unzips the given archive to the specified location. - * - * @param location path in the local file system - * @param archivePath path to archive relative to the test plug-in - */ - protected IPath doUnZip(IPath location, String archivePath) throws IOException { - URL zipURL = PDETestsPlugin.getBundleContext().getBundle().getEntry(archivePath); - File zipPath = new File(FileLocator.toFileURL(zipURL).getFile()); - try (ZipFile zipFile = new ZipFile(zipPath)) { - Enumeration entries = zipFile.entries(); - IPath parent = location.removeLastSegments(1); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (!entry.isDirectory()) { - IPath entryPath = parent.append(entry.getName()); - File dir = entryPath.removeLastSegments(1).toFile(); - dir.mkdirs(); - File file = entryPath.toFile(); - file.createNewFile(); - try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); - BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) { - byte[] bytes = inputStream.readAllBytes(); - outputStream.write(bytes); - } - } - } - return parent; - } - } - /** * Build the given project and wait till the build the complete * @param project project to be build @@ -293,27 +248,12 @@ protected void setPreferences(IProject project, int severity) throws BackingStor projectPrefs.sync(); } - /** - * Sets the given project specific preferences - * - * @param project project for which the preference are to be set - * @param pref the preference - * @param value the value - */ - protected void setPreference(IProject project, String node, String pref, String value) throws BackingStoreException { - ProjectScope scope = new ProjectScope(project); - IEclipsePreferences projectPrefs = scope.getNode(node); - projectPrefs.put(pref, value); - projectPrefs.flush(); - projectPrefs.sync(); - } - /** * Find the project in workspace with the given id * @param id project id * @return project */ - protected IProject findProject(String id) { + protected static IProject findProject(String id) { IPluginModelBase model = PluginRegistry.findModel(id); if (model != null) { IResource resource = model.getUnderlyingResource(); diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidationTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidationTest.java index 3bda49c596..4143e272b6 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidationTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidationTest.java @@ -27,7 +27,6 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.pde.internal.core.builders.CompilerFlags; import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; -import org.junit.Before; import org.junit.Test; import org.osgi.service.prefs.BackingStoreException; @@ -39,17 +38,6 @@ */ public class BuildPropertiesValidationTest extends AbstractBuildValidationTest { - private static boolean fOneTimeSetupComplete = false; - - @Override - @Before - public void setUp() throws Exception { - if (fOneTimeSetupComplete) - return; - super.setUp(); - fOneTimeSetupComplete = true; - } - @Test public void testSourceFolder() throws CoreException, BackingStoreException, IOException { for (int i = 1; i <= 5; i++) { diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/ee/ExportBundleTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/ee/ExportBundleTests.java index 7c6fad191f..dc84fd79de 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/ee/ExportBundleTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/ee/ExportBundleTests.java @@ -19,14 +19,12 @@ import static org.junit.Assert.fail; import java.io.File; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.ToolFactory; @@ -37,7 +35,6 @@ import org.eclipse.pde.internal.core.exports.FeatureExportInfo; import org.eclipse.pde.internal.core.exports.PluginExportOperation; import org.eclipse.pde.ui.tests.PDETestCase; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.eclipse.pde.ui.tests.runtime.TestUtils; import org.eclipse.pde.ui.tests.util.ProjectUtils; import org.junit.Before; @@ -54,7 +51,7 @@ public static void requireStandaloneEclipseSDKEnvironment() { PDETestCase.assumeRunningInStandaloneEclipseSDK(); } - private static final IPath EXPORT_PATH = PDETestsPlugin.getDefault().getStateLocation().append(".export"); + private static final Path EXPORT_PATH = getThisBundlesStateLocation().resolve(".export"); @Override @Before @@ -77,30 +74,6 @@ protected void deleteProject(String name) throws CoreException { } } - /** - * Deletes the specified folder. - * - * @param dir - * the file to delete - */ - protected void deleteFolder(File dir) throws IOException { - if (dir.isDirectory()) { - if (dir.list().length == 0) - Files.delete(dir.toPath()); - else { - File[] files = dir.listFiles(); - for (File file : files) { - deleteFolder(file); - } - if (dir.list().length == 0) { - Files.delete(dir.toPath()); - } - } - } else { - Files.delete(dir.toPath()); - } - } - /** * Validates the target level of a generated class file. * @@ -130,7 +103,7 @@ public void testExportCustomEnvironment() throws Exception { info.exportSource = false; info.allowBinaryCycles = false; info.useWorkspaceCompiledClasses = false; - info.destinationDirectory = EXPORT_PATH.toOSString(); + info.destinationDirectory = EXPORT_PATH.toString(); info.zipFileName = null; info.items = new Object[]{PluginRegistry.findModel(project.getProject())}; info.signingInfo = null; @@ -149,19 +122,19 @@ public void testExportCustomEnvironment() throws Exception { TestUtils.waitForJobs(name.getMethodName(), 100, 10000); // verify exported bundle exists - IPath path = EXPORT_PATH.append("plugins/no.sound.export_1.0.0.jar"); + Path path = EXPORT_PATH.resolve("plugins/no.sound.export_1.0.0.jar"); // The jar file may not have been copied to the file system yet, see Bug 424597 - if (!path.toFile().exists()) { + if (!Files.exists(path)) { TestUtils.waitForJobs(name.getMethodName(), 100, 30000); } - assertTrue("Missing exported bundle", path.toFile().exists()); - validateTargetLevel(path.toOSString(), "no/sound/export/Activator.class", 47); + assertTrue("Missing exported bundle", Files.exists(path)); + validateTargetLevel(path.toString(), "no/sound/export/Activator.class", 47); } finally { TestUtils.waitForJobs(name.getMethodName(), 10, 5000); deleteProject("no.sound.export"); - deleteFolder(EXPORT_PATH.toFile()); + delete(EXPORT_PATH.toFile()); } } @@ -182,7 +155,7 @@ public void testExport14Environment() throws Exception { info.exportSource = false; info.allowBinaryCycles = false; info.useWorkspaceCompiledClasses = false; - info.destinationDirectory = EXPORT_PATH.toOSString(); + info.destinationDirectory = EXPORT_PATH.toString(); info.zipFileName = null; info.items = new Object[]{PluginRegistry.findModel(project.getProject())}; info.signingInfo = null; @@ -204,19 +177,19 @@ public void testExport14Environment() throws Exception { long l6 = System.currentTimeMillis(); // veriry exported bundle exists - IPath path = EXPORT_PATH.append("plugins/j2se14.export_1.0.0.jar"); + Path path = EXPORT_PATH.resolve("plugins/j2se14.export_1.0.0.jar"); long l7 = System.currentTimeMillis(); TestUtils.processUIEvents(100); TestUtils.waitForJobs(name.getMethodName(), 100, 10000); - boolean didPathExistBeforeSleep = path.toFile().exists(); + boolean didPathExistBeforeSleep = Files.exists(path); /* give a 30 second delay when the path doesn't exist ( JUST IN CASE - unlikely to work but worth trying)*/ - if (!path.toFile().exists()) { + if (!Files.exists(path)) { TestUtils.waitForJobs(name.getMethodName(), 3000, 30000); } - boolean didPathExistAfterSleep = path.toFile().exists(); + boolean didPathExistAfterSleep = Files.exists(path); long l8 = System.currentTimeMillis(); @@ -247,7 +220,7 @@ public void testExport14Environment() throws Exception { // Should only have plugin/feature folders printContents(exportContents); } else { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); + Path stateLocation = getThisBundlesStateLocation(); if (stateLocation.toFile().exists()) { System.out.println("Exported directory is missing, parent: "); printContents(stateLocation.toFile()); @@ -257,12 +230,12 @@ public void testExport14Environment() throws Exception { } System.out.println("================================\nEnd of BUG 424597"); - assertTrue("Missing exported bundle", path.toFile().exists()); - validateTargetLevel(path.toOSString(), "j2se14/export/Activator.class", 46); + assertTrue("Missing exported bundle", Files.exists(path)); + validateTargetLevel(path.toString(), "j2se14/export/Activator.class", 46); } finally { TestUtils.waitForJobs(name.getMethodName(), 10, 5000); deleteProject("j2se14.export"); - deleteFolder(EXPORT_PATH.toFile()); + delete(EXPORT_PATH.toFile()); } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/AbstractSchemaPerfTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/AbstractSchemaPerfTest.java index 01cfb4f802..025b80cac6 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/AbstractSchemaPerfTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/AbstractSchemaPerfTest.java @@ -19,9 +19,9 @@ import java.net.URL; import org.eclipse.core.runtime.FileLocator; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.eclipse.test.performance.PerformanceTestCase; import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; /** * AbstractSchemaPerfTest @@ -52,10 +52,7 @@ protected void setUpIterations() { } private void setUpSchemaFile() throws Exception, IOException { - PDETestsPlugin plugin = PDETestsPlugin.getDefault(); - if (plugin == null) - throw new Exception("ERROR: Macro plug-in uninitialized"); //$NON-NLS-1$ - Bundle bundle = plugin.getBundle(); + Bundle bundle = FrameworkUtil.getBundle(AbstractSchemaPerfTest.class); if (bundle == null) throw new Exception("ERROR: Bundle uninitialized"); //$NON-NLS-1$ URL url = bundle.getEntry(F_FILENAME); diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java index 83f4700f7d..689d76ffba 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/InitializeModelsPerfTest.java @@ -14,8 +14,8 @@ package org.eclipse.pde.ui.tests.performance.parts; import java.io.File; +import java.nio.file.Path; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.pde.core.target.ITargetDefinition; import org.eclipse.pde.core.target.ITargetLocation; @@ -23,6 +23,7 @@ import org.eclipse.pde.core.target.LoadTargetDefinitionJob; import org.eclipse.pde.internal.core.PDECore; import org.eclipse.pde.internal.core.TargetPlatformHelper; +import org.eclipse.pde.ui.tests.PDETestCase; import org.eclipse.test.performance.Dimension; import org.eclipse.test.performance.PerformanceTestCase; @@ -35,16 +36,16 @@ public class InitializeModelsPerfTest extends PerformanceTestCase { @Override protected void setUp() throws Exception { super.setUp(); - deleteContent(new File(PDECore.getDefault().getStateLocation().toOSString())); + PDETestCase.delete(PDECore.getDefault().getStateLocation().toFile()); TargetPlatformHelper.getKnownExecutionEnvironments(); } public void testModels() throws Exception { tagAsGlobalSummary("Initialize PDE Models", Dimension.ELAPSED_PROCESS); - IPath testBundles = TargetPlatformPerfTest.extractTargetPerfTestPlugins(); + Path testBundles = TargetPlatformPerfTest.extractTargetPerfTestPlugins(); ITargetPlatformService tps = PDECore.getDefault().acquireService(ITargetPlatformService.class); ITargetDefinition originalTarget = tps.newTarget(); - originalTarget.setTargetLocations(new ITargetLocation[]{tps.newDirectoryLocation(testBundles.toPortableString())}); + originalTarget.setTargetLocations(new ITargetLocation[] { tps.newDirectoryLocation(testBundles.toString()) }); tps.saveTargetDefinition(originalTarget); // Target resolution performance handled in TargetPlatformPerfTest @@ -75,21 +76,6 @@ public void testModels() throws Exception { @Override protected void tearDown() throws Exception { - deleteContent(new File(PDECore.getDefault().getStateLocation().toOSString())); + PDETestCase.delete(new File(PDECore.getDefault().getStateLocation().toOSString())); } - - private void deleteContent(File curr) { - if (curr.exists()) { - if (curr.isDirectory()) { - File[] children = curr.listFiles(); - if (children != null) { - for (File element : children) { - deleteContent(element); - } - } - } - curr.delete(); - } - } - } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/OpenManifestEditorPerfTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/OpenManifestEditorPerfTest.java index ea36693dac..2151ba12d1 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/OpenManifestEditorPerfTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/OpenManifestEditorPerfTest.java @@ -23,7 +23,6 @@ import org.eclipse.pde.internal.ui.IPDEUIConstants; import org.eclipse.pde.internal.ui.IPreferenceConstants; import org.eclipse.pde.internal.ui.PDEPlugin; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.eclipse.test.performance.Dimension; import org.eclipse.test.performance.PerformanceTestCase; import org.eclipse.ui.IEditorInput; @@ -33,6 +32,7 @@ import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.ide.IDE; import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; public class OpenManifestEditorPerfTest extends PerformanceTestCase { @@ -72,11 +72,7 @@ private void disableCodeFoldingFeature() throws Exception { } private Bundle getBundle() throws Exception { - PDETestsPlugin plugin = PDETestsPlugin.getDefault(); - if (plugin == null) { - throw new Exception("ERROR: Macro plug-in uninitialized"); //$NON-NLS-1$ - } - Bundle bundle = plugin.getBundle(); + Bundle bundle = FrameworkUtil.getBundle(OpenManifestEditorPerfTest.class); if (bundle == null) { throw new Exception("ERROR: Bundle uninitialized"); //$NON-NLS-1$ } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/TargetPlatformPerfTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/TargetPlatformPerfTest.java index eb076aaad3..bd9c4572ab 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/TargetPlatformPerfTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/performance/parts/TargetPlatformPerfTest.java @@ -13,25 +13,15 @@ *******************************************************************************/ package org.eclipse.pde.ui.tests.performance.parts; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Enumeration; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashSet; import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.osgi.service.resolver.BundleSpecification; import org.eclipse.pde.core.plugin.IPluginModelBase; @@ -44,7 +34,7 @@ import org.eclipse.pde.core.target.ITargetPlatformService; import org.eclipse.pde.core.target.LoadTargetDefinitionJob; import org.eclipse.pde.internal.core.PDECore; -import org.eclipse.pde.ui.tests.PDETestsPlugin; +import org.eclipse.pde.ui.tests.PDETestCase; import org.eclipse.pde.ui.tests.util.TargetPlatformUtil; import org.eclipse.pde.ui.tests.util.TestBundleCreator; import org.eclipse.test.performance.Dimension; @@ -76,12 +66,12 @@ public class TargetPlatformPerfTest extends PerformanceTestCase { */ public void testResolveTargetDefinition() throws Exception { tagAsSummary("Resolve target definition", Dimension.ELAPSED_PROCESS); //$NON-NLS-1$ - IPath testBundles = extractTargetPerfTestPlugins(); + Path testBundles = extractTargetPerfTestPlugins(); ITargetPlatformService tps = PDECore.getDefault().acquireService(ITargetPlatformService.class); ITargetDefinition originalTarget = tps.newTarget(); - originalTarget.setTargetLocations(new ITargetLocation[]{tps.newDirectoryLocation(testBundles.toPortableString())}); + originalTarget.setTargetLocations(new ITargetLocation[] { tps.newDirectoryLocation(testBundles.toString()) }); tps.saveTargetDefinition(originalTarget); ITargetHandle handle = originalTarget.getHandle(); @@ -109,7 +99,7 @@ public void testResolveTargetDefinition() throws Exception { */ public void testSearchModelRegistry() throws Exception { tagAsSummary("Resolve target definition", Dimension.ELAPSED_PROCESS); //$NON-NLS-1$ - IPath testBundles = extractTargetPerfTestPlugins(); + Path testBundles = extractTargetPerfTestPlugins(); IBundleProjectService service = PDECore.getDefault().acquireService(IBundleProjectService.class); ITargetPlatformService tps = PDECore.getDefault().acquireService(ITargetPlatformService.class); @@ -130,7 +120,7 @@ public void testSearchModelRegistry() throws Exception { // Set the example target as active ITargetDefinition target = tps.newTarget(); - target.setTargetLocations(new ITargetLocation[] {tps.newDirectoryLocation(testBundles.toPortableString())}); + target.setTargetLocations(new ITargetLocation[] { tps.newDirectoryLocation(testBundles.toString()) }); TargetPlatformUtil.loadAndSetTarget(target); // Warm-up Iterations @@ -242,43 +232,13 @@ private void openRequiredBundles(IPluginModelBase model, Set allBundleNa } - static IPath extractTargetPerfTestPlugins() throws Exception { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); - IPath location = stateLocation.append("targetPerfTestPlugins"); - if (!location.toFile().exists()) { - return doUnZip(location, TEST_PLUGIN_LOCATION); + static Path extractTargetPerfTestPlugins() throws Exception { + Path stateLocation = PDETestCase.getThisBundlesStateLocation(); + Path location = stateLocation.resolve("targetPerfTestPlugins"); + if (!Files.exists(location)) { + return PDETestCase.doUnZip(location.getParent(), TEST_PLUGIN_LOCATION); } return location; } - /** - * Unzips the given archive to the specified location. - * - * @param location path in the local file system - * @param archivePath path to archive relative to the test plug-in - */ - private static IPath doUnZip(IPath location, String archivePath) throws IOException { - URL zipURL = PDETestsPlugin.getBundleContext().getBundle().getEntry(archivePath); - File zipPath = new File(FileLocator.toFileURL(zipURL).getFile()); - try (ZipFile zipFile = new ZipFile(zipPath)) { - Enumeration entries = zipFile.entries(); - IPath parent = location.removeLastSegments(1); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (!entry.isDirectory()) { - IPath entryPath = parent.append(entry.getName()); - File dir = entryPath.removeLastSegments(1).toFile(); - dir.mkdirs(); - File file = entryPath.toFile(); - file.createNewFile(); - try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); - BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) { - byte[] bytes = inputStream.readAllBytes(); - outputStream.write(bytes); - } - } - } - return parent; - } - } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/project/ProjectCreationTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/project/ProjectCreationTests.java index 1ba2ae4303..7ae54f27b0 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/project/ProjectCreationTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/project/ProjectCreationTests.java @@ -20,8 +20,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -39,7 +37,6 @@ import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.jobs.Job; @@ -70,11 +67,11 @@ import org.eclipse.pde.internal.core.ibundle.IManifestHeader; import org.eclipse.pde.internal.core.project.PDEProject; import org.eclipse.pde.internal.core.text.bundle.BundleModelFactory; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; import org.osgi.framework.Constants; +import org.osgi.framework.FrameworkUtil; import org.osgi.framework.Version; /** @@ -1014,9 +1011,8 @@ protected void createBogusJar(IFile file) throws CoreException, IOException { } parent = parent.getParent(); } - URL zipURL = PDETestsPlugin.getBundleContext().getBundle().getEntry("tests/A.jar"); - File ioFile = new File(FileLocator.toFileURL(zipURL).getFile()); - try (FileInputStream stream = new FileInputStream(ioFile)) { + URL zipURL = FrameworkUtil.getBundle(ProjectCreationTests.class).getEntry("tests/A.jar"); + try (InputStream stream = zipURL.openStream()) { file.create(stream, false, null); } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/AbstractRegistryModelTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/AbstractRegistryModelTest.java index f9820c09c1..4c099a26a7 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/AbstractRegistryModelTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/AbstractRegistryModelTest.java @@ -32,12 +32,12 @@ import org.eclipse.pde.internal.runtime.registry.model.RegistryModel; import org.eclipse.pde.internal.runtime.registry.model.ServiceName; import org.eclipse.pde.internal.runtime.registry.model.ServiceRegistration; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; +import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceEvent; import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; @@ -105,8 +105,8 @@ public void modelChanged(ModelChangeDelta[] deltas) { abstract protected RegistryModel createModel() throws URISyntaxException; public AbstractRegistryModelTest() { - testBundle = PDETestsPlugin.getBundleContext().getBundle(); - org.osgi.framework.ServiceRegistration registration = PDETestsPlugin.getBundleContext() + testBundle = FrameworkUtil.getBundle(AbstractRegistryModelTest.class); + org.osgi.framework.ServiceRegistration registration = testBundle.getBundleContext() .registerService(getClass().getName(), this, null); testServiceReference = registration.getReference(); @@ -134,7 +134,7 @@ public void tearDown() { */ @Test public void testInstalledBundles() { - org.osgi.framework.Bundle[] origBundles = PDETestsPlugin.getBundleContext().getBundles(); + org.osgi.framework.Bundle[] origBundles = FrameworkUtil.getBundle(AbstractRegistryModelTest.class).getBundleContext().getBundles(); model.initialize(new NullProgressMonitor()); Bundle[] bundles = model.getBundles(); diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/TestUtils.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/TestUtils.java index b4aef54e46..0e138f06c9 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/TestUtils.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/runtime/TestUtils.java @@ -24,10 +24,9 @@ import java.util.List; import java.util.Set; -import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.eclipse.swt.widgets.Display; import org.junit.Assert; import org.junit.jupiter.api.function.ThrowingConsumer; @@ -55,7 +54,8 @@ public static void cleanUp(String owner) { if (timedOut) { // We don't expect any extra jobs run during the test: try to cancel // them - log(IStatus.INFO, owner, "Trying to cancel running jobs: " + getRunningOrWaitingJobs(null)); + ILog.get().log(Status + .info("[" + owner + "] Trying to cancel running jobs: " + getRunningOrWaitingJobs(null))); getRunningOrWaitingJobs(null).forEach(job -> job.cancel()); waitForJobs(owner, 5, 1000); } @@ -64,16 +64,6 @@ public static void cleanUp(String owner) { Assert.assertFalse("The main thread should not be interrupted at the end of a test", Thread.interrupted()); } - public static void log(int severity, String owner, String message, Throwable... optionalError) { - message = "[" + owner + "] " + message; - Throwable error = null; - if (optionalError != null && optionalError.length > 0) { - error = optionalError[0]; - } - Status status = new Status(severity, PDETestsPlugin.getDefault().getBundle().getSymbolicName(), message, error); - PDETestsPlugin.getDefault().getLog().log(status); - } - /** * Process all queued UI events. If called from background thread, does * nothing. @@ -189,11 +179,12 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, return false; } - static Set runningJobs = new LinkedHashSet<>(); + private static Set runningJobs = new LinkedHashSet<>(); private static void dumpRunningOrWaitingJobs(String owner, List jobs) { - String message = "Some job is still running or waiting to run: " + dumpRunningOrWaitingJobs(jobs); - log(IStatus.ERROR, owner, message); + String message = "[" + owner + "] Some job is still running or waiting to run: " + + dumpRunningOrWaitingJobs(jobs); + ILog.get().log(Status.error(message)); } private static String dumpRunningOrWaitingJobs(List jobs) { diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/AbstractTargetTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/AbstractTargetTest.java index 7c6becee4e..5a6d7b2fa7 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/AbstractTargetTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/AbstractTargetTest.java @@ -16,33 +16,17 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import org.eclipse.core.filebuffers.FileBuffers; -import org.eclipse.core.filebuffers.ITextFileBuffer; -import org.eclipse.core.filebuffers.ITextFileBufferManager; -import org.eclipse.core.filebuffers.LocationKind; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Platform; import org.eclipse.e4.core.contexts.EclipseContextFactory; import org.eclipse.e4.core.contexts.IEclipseContext; @@ -57,11 +41,9 @@ import org.eclipse.pde.core.target.TargetEvents; import org.eclipse.pde.internal.core.PDECore; import org.eclipse.pde.ui.tests.PDETestCase; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.eclipse.pde.ui.tests.runtime.TestUtils; import org.eclipse.pde.ui.tests.util.TargetPlatformUtil; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; import org.osgi.service.event.EventHandler; /** @@ -74,11 +56,8 @@ public abstract class AbstractTargetTest extends PDETestCase { * * @return target platform service */ - protected ITargetPlatformService getTargetService() { - ServiceReference reference = PDETestsPlugin.getBundleContext() - .getServiceReference(ITargetPlatformService.class); - assertNotNull("Missing target platform service", reference); - return PDETestsPlugin.getBundleContext().getService(reference); + protected static ITargetPlatformService getTargetService() { + return org.eclipse.pde.internal.core.target.TargetPlatformService.getDefault(); } /** @@ -87,33 +66,14 @@ protected ITargetPlatformService getTargetService() { * * @return path to the plug-ins directory */ - protected IPath extractAbcdePlugins() throws Exception { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); - IPath location = stateLocation.append("abcde-plugins"); - if (location.toFile().exists()) { + protected Path extractAbcdePlugins() throws Exception { + Path stateLocation = getThisBundlesStateLocation(); + Path location = stateLocation.resolve("abcde-plugins"); + if (Files.exists(location)) { // recursively delete - File dir = location.toFile(); - delete(dir); + delete(location.toFile()); } - return doUnZip(location, "/tests/targets/abcde-plugins.zip"); - } - - /** - * Extracts the same plugins as {@link #extractAbcdePlugins()}, but puts them - * in a linked folder setup (linked/eclipse/plugins). Returns the location - * of the plugins directory. - * - * @return path to the plug-ins directory - */ - protected IPath extractLinkedPlugins() throws Exception { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); - IPath location = stateLocation.append("abcde/linked/eclipse/plugins"); - if (location.toFile().exists()) { - // recursively delete - File dir = location.toFile(); - delete(dir); - } - return doUnZip(location, "/tests/targets/abcde-plugins.zip"); + return doUnZip(stateLocation, "/tests/targets/abcde-plugins.zip"); } /** @@ -122,19 +82,18 @@ protected IPath extractLinkedPlugins() throws Exception { * * @return path to the root directory */ - protected IPath extractModifiedFeatures() throws Exception { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); - IPath location = stateLocation.append("modified-jdt-features"); - if (location.toFile().exists()) { + protected Path extractModifiedFeatures() throws Exception { + Path stateLocation = getThisBundlesStateLocation(); + Path location = stateLocation.resolve("modified-jdt-features"); + if (Files.exists(location)) { return location; } - doUnZip(location,"/tests/targets/modified-jdt-features.zip"); + doUnZip(stateLocation, "/tests/targets/modified-jdt-features.zip"); // If we are not on the mac, delete the mac launching bundle (in a standard non Mac build, the plug-in wouldn't exist) if (!Platform.getOS().equals(Platform.OS_MACOSX)) { - File macBundle = location.append("plugins").append("org.eclipse.jdt.launching.macosx_3.2.0.v20090527.jar").toFile(); - if (macBundle.exists()){ - assertTrue("Unable to delete test mac launching bundle",macBundle.delete()); - } + Path macBundle = location.resolve("plugins") + .resolve("org.eclipse.jdt.launching.macosx_3.2.0.v20090527.jar"); + Files.deleteIfExists(macBundle); } return location; } @@ -146,67 +105,19 @@ protected IPath extractModifiedFeatures() throws Exception { * * @return path to the directory containing the bundles */ - protected IPath extractMultiVersionPlugins() throws Exception { - IPath stateLocation = PDETestsPlugin.getDefault().getStateLocation(); - IPath location = stateLocation.append("multi-versions"); - if (location.toFile().exists()) { + protected Path extractMultiVersionPlugins() throws Exception { + Path stateLocation = getThisBundlesStateLocation(); + Path location = stateLocation.resolve("multi-versions"); + if (Files.exists(location)) { return location; } - doUnZip(location,"/tests/targets/multi-versions.zip"); + doUnZip(stateLocation, "/tests/targets/multi-versions.zip"); return location; } /** - * Unzips the given archive to the specified location. - * - * @param location path in the local file system - * @param archivePath path to archive relative to the test plug-in - */ - private IPath doUnZip(IPath location, String archivePath) throws IOException { - URL zipURL = PDETestsPlugin.getBundleContext().getBundle().getEntry(archivePath); - File zipPath = new File(FileLocator.toFileURL(zipURL).getFile()); - try (ZipFile zipFile = new ZipFile(zipPath)) { - Enumeration entries = zipFile.entries(); - IPath parent = location.removeLastSegments(1); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (!entry.isDirectory()) { - IPath entryPath = parent.append(entry.getName()); - File dir = entryPath.removeLastSegments(1).toFile(); - dir.mkdirs(); - File file = entryPath.toFile(); - file.createNewFile(); - try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); - BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) { - byte[] bytes = inputStream.readAllBytes(); - outputStream.write(bytes); - } - } - } - return parent; - } - } - - /** - * Recursively deletes the directory and files within. - * - * @param dir directory to delete - */ - protected void delete(File dir) { - File[] files = dir.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - delete(file); - } else { - file.delete(); - } - } - dir.delete(); - } - - /** - * Used to reset the target platform to original settings after a test that changes - * the target platform. + * Used to reset the target platform to original settings after a test that + * changes the target platform. */ protected void resetTargetPlatform() throws CoreException { ITargetDefinition definition = getDefaultTargetPlatorm(); @@ -325,16 +236,4 @@ protected List getBundleInfos(ITargetLocation container) throws Exce } return list; } - - public static ITextFileBuffer getTextFileBufferFromFile(File file) { - try { - IPath path = IPath.fromOSString(file.getAbsolutePath()); - ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); - manager.connect(path, LocationKind.LOCATION, null); - return manager.getTextFileBuffer(path, LocationKind.LOCATION); - } catch (CoreException e) { - fail("Unable to retrive target definition file"); - } - return null; - } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/IUBundleContainerTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/IUBundleContainerTests.java index f23a2e374e..637f129c4b 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/IUBundleContainerTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/IUBundleContainerTests.java @@ -60,8 +60,8 @@ import org.eclipse.pde.internal.core.target.TargetDefinitionPersistenceHelper; import org.eclipse.pde.internal.core.target.TargetPersistence38Helper; import org.eclipse.pde.internal.core.target.VirtualArtifactRepository; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.junit.Test; +import org.osgi.framework.FrameworkUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; @@ -92,7 +92,7 @@ protected IMetadataRepository getRepository(URI uri) throws Exception { * @return URI */ public static URI getURI(String relativePath) throws Exception { - URL url = PDETestsPlugin.getBundleContext().getBundle().getEntry(relativePath); + URL url = FrameworkUtil.getBundle(IUBundleContainerTests.class).getEntry(relativePath); IPath path = IPath.fromOSString(new File(FileLocator.toFileURL(url).getFile()).getAbsolutePath()); return URIUtil.toURI(path); } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/LocalTargetDefinitionTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/LocalTargetDefinitionTests.java index b5b3983e7c..d21dd5502e 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/LocalTargetDefinitionTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/LocalTargetDefinitionTests.java @@ -33,7 +33,6 @@ import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.equinox.frameworkadmin.BundleInfo; @@ -283,12 +282,11 @@ public void testVariableDirectoryBundleContainer() throws Exception { @Test public void testFeatureBundleContainer() throws Exception { // extract the feature - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); // the new way ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", - null); + ITargetLocation container = getTargetService().newFeatureLocation(location.toString(), "org.eclipse.jdt", null); container.resolve(definition, null); TargetBundle[] bundles = container.getBundles(); @@ -322,12 +320,11 @@ public void testFeatureBundleContainer() throws Exception { @Test public void testMacOSFeatureBundleContainer() throws Exception { // extract the feature - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); ITargetDefinition definition = getNewTarget(); definition.setOS(Platform.OS_MACOSX); - ITargetLocation container = getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", - null); + ITargetLocation container = getTargetService().newFeatureLocation(location.toString(), "org.eclipse.jdt", null); container.resolve(definition, null); TargetBundle[] bundles = container.getBundles(); @@ -371,11 +368,10 @@ public void testMacOSFeatureBundleContainer() throws Exception { @Test public void testRestrictedFeatureBundleContainer() throws Exception { // extract the feature - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", - null); + ITargetLocation container = getTargetService().newFeatureLocation(location.toString(), "org.eclipse.jdt", null); NameVersionDescriptor[] restrictions = new NameVersionDescriptor[] { new NameVersionDescriptor("org.eclipse.jdt", null), new NameVersionDescriptor("org.junit", "3.8.2.v20090203-1005") }; @@ -398,11 +394,11 @@ public void testRestrictedFeatureBundleContainer() throws Exception { @Test public void testSourceFeatureBundleContainer() throws Exception { // extract the feature - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newFeatureLocation(location.toOSString(), - "org.eclipse.jdt.source", null); + ITargetLocation container = getTargetService().newFeatureLocation(location.toString(), "org.eclipse.jdt.source", + null); container.resolve(definition, null); TargetBundle[] bundles = container.getBundles(); @@ -435,11 +431,11 @@ public void testSourceFeatureBundleContainer() throws Exception { public void testSetTargetPlatformToJdtFeature() throws Exception { try { // extract the feature - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); // org.eclipse.jdt_3.6.0.v20100105-0800-7z8VFR9FMTb52_pOyKHhoek1 ITargetDefinition target = getNewTarget(); - ITargetLocation container = getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", + ITargetLocation container = getTargetService().newFeatureLocation(location.toString(), "org.eclipse.jdt", "3.6.0.v20100105-0800-7z8VFR9FMTb52_pOyKHhoek1"); target.setTargetLocations(new ITargetLocation[] { container }); @@ -656,9 +652,9 @@ public void testAllVersionsOfBundleExplicit() throws Exception { private void doIncludeVersions(NameVersionDescriptor[] descriptions) throws Exception { String bsn = MULTI_VERSION_LOW_DESCRIPTION.getId(); - IPath extras = extractMultiVersionPlugins(); + Path extras = extractMultiVersionPlugins(); ITargetDefinition target = getNewTarget(); - ITargetLocation container = getTargetService().newDirectoryLocation(extras.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(extras.toString()); target.setTargetLocations(new ITargetLocation[] { container }); target.setIncluded(descriptions); try { diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionFeatureResolutionTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionFeatureResolutionTests.java index 62428d7573..d93d90af5f 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionFeatureResolutionTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionFeatureResolutionTests.java @@ -19,12 +19,12 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.pde.core.plugin.TargetPlatform; @@ -113,9 +113,9 @@ public void testProfileBundleContainer() throws Exception { public void testExplicitIncludes() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); @@ -168,9 +168,9 @@ public void testExplicitIncludes() throws Exception { public void testSingleInclude() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); @@ -215,9 +215,9 @@ public void testSingleInclude() throws Exception { public void testMixedIncludes() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); @@ -263,9 +263,9 @@ public void testMixedIncludes() throws Exception { public void testMissingFeatures() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); @@ -297,9 +297,9 @@ public void testMissingFeatures() throws Exception { public void testMissingFeatureVersion() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); @@ -344,9 +344,9 @@ public void testMissingFeatureVersion() throws Exception { public void testMissingMixed() throws Exception { // Use the modified JDT features as we know their versions ITargetDefinition definition = getNewTarget(); - IPath location = extractModifiedFeatures(); + Path location = extractModifiedFeatures(); - ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(location.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionPersistenceTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionPersistenceTests.java index 2eec51c38d..1987e245a2 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionPersistenceTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionPersistenceTests.java @@ -52,9 +52,8 @@ import org.eclipse.pde.internal.core.target.TargetDefinition; import org.eclipse.pde.internal.core.target.TargetDefinitionPersistenceHelper; import org.eclipse.pde.ui.tests.PDETestCase; -import org.eclipse.pde.ui.tests.PDETestsPlugin; import org.junit.Test; -import org.osgi.framework.ServiceReference; +import org.osgi.framework.FrameworkUtil; /** * Tests the persistence of target definitions. Tests memento creation, reading @@ -584,10 +583,9 @@ public void testPersistComplexWorkspaceDefinition() throws Exception { * @return target platform service */ private ITargetPlatformService getTargetService() { - ServiceReference reference = PDETestsPlugin.getBundleContext() - .getServiceReference(ITargetPlatformService.class); - assertNotNull("Missing target platform service", reference); - return PDETestsPlugin.getBundleContext().getService(reference); + ITargetPlatformService service = AbstractTargetTest.getTargetService(); + assertNotNull("Missing target platform service", service); + return service; } /** @@ -595,7 +593,7 @@ private ITargetPlatformService getTargetService() { * location with the given name. Note that ".target" will be appended. */ private ITargetDefinition readOldTarget(String name) throws Exception { - URL url = PDETestsPlugin.getBundleContext().getBundle() + URL url = FrameworkUtil.getBundle(TargetDefinitionPersistenceTests.class) .getEntry("/tests/targets/target-files/" + name + ".trgt"); File file = new File(FileLocator.toFileURL(url).getFile()); ITargetDefinition target = getTargetService().newTarget(); diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionResolutionTests.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionResolutionTests.java index f134d37e9a..8404874da9 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionResolutionTests.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/target/TargetDefinitionResolutionTests.java @@ -20,8 +20,9 @@ import static org.junit.Assert.assertTrue; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.equinox.frameworkadmin.BundleInfo; import org.eclipse.equinox.p2.metadata.IInstallableUnit; @@ -174,10 +175,9 @@ public void testResolutionCaching() throws Exception { */ @Test public void testTargetInSynch() throws Exception { - IPath location = extractAbcdePlugins(); - IPath dirPath = location.append("plugins"); + Path dirPath = extractAbcdePlugins().resolve("plugins"); ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); TargetBundle[] allBundles = definition.getAllBundles(); @@ -199,11 +199,10 @@ public void testTargetInSynch() throws Exception { */ @Test public void testTargetInSynchWithDuplicates() throws Exception { - IPath location = extractAbcdePlugins(); - IPath dirPath = location.append("plugins"); + Path dirPath = extractAbcdePlugins().resolve("plugins"); ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toOSString()); - ITargetLocation container2 = getTargetService().newDirectoryLocation(dirPath.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toString()); + ITargetLocation container2 = getTargetService().newDirectoryLocation(dirPath.toString()); definition.setTargetLocations(new ITargetLocation[] { container, container2 }); definition.resolve(null); TargetBundle[] allBundles = definition.getAllBundles(); @@ -225,18 +224,17 @@ public void testTargetInSynchWithDuplicates() throws Exception { */ @Test public void testTargetMissingBundle() throws Exception { - IPath location = extractAbcdePlugins(); - IPath dirPath = location.append("plugins"); + Path dirPath = extractAbcdePlugins().resolve("plugins"); ITargetDefinition definition = getNewTarget(); - ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); try { setTargetPlatform(definition); // delete a bundle - IPath bundle = dirPath.append("bundle.a_1.0.0.jar"); - assertTrue(bundle.toFile().exists()); - bundle.toFile().delete(); + Path bundle = dirPath.resolve("bundle.a_1.0.0.jar"); + assertTrue(Files.exists(bundle)); + Files.delete(bundle); // force definition to re-resolve ITargetDefinition copy = getTargetService().newTarget(); getTargetService().copyTargetDefinition(definition, copy); @@ -259,16 +257,15 @@ public void testTargetMissingBundle() throws Exception { */ @Test public void testTargetPlatformMissingBundle() throws Exception { - IPath location = extractAbcdePlugins(); - IPath dirPath = location.append("plugins"); + Path dirPath = extractAbcdePlugins().resolve("plugins"); // delete a bundle - IPath bundle = dirPath.append("bundle.a_1.0.0.jar"); + Path bundle = dirPath.resolve("bundle.a_1.0.0.jar"); File jar = bundle.toFile(); assertTrue(jar.exists()); jar.delete(); ITargetDefinition definition = getTargetService().newTarget(); - ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toOSString()); + ITargetLocation container = getTargetService().newDirectoryLocation(dirPath.toString()); definition.setTargetLocations(new ITargetLocation[] { container }); definition.resolve(null); TargetBundle[] allBundles = definition.getAllBundles();