Skip to content

Commit

Permalink
Unify and clean-up pde.ui.tests and replace the activator
Browse files Browse the repository at this point in the history
Remove unused code and unify and modernize remaining code.
  • Loading branch information
HannesWell committed May 19, 2024
1 parent f8afe21 commit 9648456
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 488 deletions.
1 change: 0 additions & 1 deletion ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,16 +28,19 @@
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;
import org.eclipse.core.resources.IWorkspaceRoot;
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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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<URL> entries = Collections.list(bundle.findEntries(rootPath, null, true));
for (URL entry : entries) {
Expand All @@ -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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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<Path> 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<String>) projectNames::iterator) {
IProject project = findProject(projectName);
project.create(new NullProgressMonitor());
project.open(new NullProgressMonitor());
}
}
}

Expand Down Expand Up @@ -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<? extends ZipEntry> 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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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++) {
Expand Down
Loading

0 comments on commit 9648456

Please sign in to comment.