Skip to content

Commit 9648456

Browse files
committed
Unify and clean-up pde.ui.tests and replace the activator
Remove unused code and unify and modernize remaining code.
1 parent f8afe21 commit 9648456

19 files changed

+200
-488
lines changed

ui/org.eclipse.pde.ui.tests/META-INF/MANIFEST.MF

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Bundle-Name: PDE JUnit Tests
44
Bundle-SymbolicName: org.eclipse.pde.ui.tests; singleton:=true
55
Bundle-Version: 3.12.500.qualifier
66
Bundle-ClassPath: tests.jar
7-
Bundle-Activator: org.eclipse.pde.ui.tests.PDETestsPlugin
87
Bundle-Vendor: Eclipse.org
98
Require-Bundle: org.eclipse.pde.ui,
109
org.eclipse.ui,

ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestCase.java

+49-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
*******************************************************************************/
1414
package org.eclipse.pde.ui.tests;
1515

16+
import static org.junit.Assert.assertNotNull;
1617
import static org.junit.Assume.assumeTrue;
1718

19+
import java.io.File;
1820
import java.io.IOException;
1921
import java.io.InputStream;
20-
import java.lang.StackWalker.Option;
2122
import java.net.URI;
2223
import java.net.URISyntaxException;
2324
import java.net.URL;
@@ -27,16 +28,19 @@
2728
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.concurrent.TimeUnit;
31+
import java.util.zip.ZipEntry;
32+
import java.util.zip.ZipInputStream;
3033

3134
import org.eclipse.core.resources.IProject;
3235
import org.eclipse.core.resources.IResource;
3336
import org.eclipse.core.resources.IWorkspaceRoot;
3437
import org.eclipse.core.resources.ResourcesPlugin;
3538
import org.eclipse.core.runtime.CoreException;
3639
import org.eclipse.core.runtime.ICoreRunnable;
37-
import org.eclipse.core.runtime.IStatus;
40+
import org.eclipse.core.runtime.ILog;
3841
import org.eclipse.core.runtime.NullProgressMonitor;
3942
import org.eclipse.core.runtime.Platform;
43+
import org.eclipse.core.runtime.Status;
4044
import org.eclipse.core.runtime.URIUtil;
4145
import org.eclipse.core.runtime.jobs.Job;
4246
import org.eclipse.jface.dialogs.ErrorDialog;
@@ -64,8 +68,6 @@
6468
*/
6569
public abstract class PDETestCase {
6670

67-
private static final StackWalker STACK_WALKER = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
68-
6971
private static boolean welcomeClosed;
7072
@Rule
7173
public TestName name = new TestName();
@@ -75,18 +77,18 @@ public void setUp() throws Exception {
7577
MessageDialog.AUTOMATED_MODE = true;
7678
ErrorDialog.AUTOMATED_MODE = true;
7779
FreezeMonitor.expectCompletionInAMinute();
78-
TestUtils.log(IStatus.INFO, name.getMethodName(), "setUp");
80+
ILog.get().log(Status.info("[" + name.getMethodName() + "] " + "setUp"));
7981
assertWelcomeScreenClosed();
8082
}
8183

8284
@After
8385
public void tearDown() throws Exception {
84-
TestUtils.log(IStatus.INFO, name.getMethodName(), "tearDown");
86+
ILog.get().log(Status.info("[" + name.getMethodName() + "] " + "tearDown"));
8587
// Close any editors we opened
8688
IWorkbenchWindow[] workbenchPages = PlatformUI.getWorkbench().getWorkbenchWindows();
8789
for (IWorkbenchWindow workbenchPage : workbenchPages) {
8890
IWorkbenchPage page = workbenchPage.getActivePage();
89-
if (page != null){
91+
if (page != null) {
9092
page.closeAllEditors(false);
9193
}
9294
}
@@ -107,7 +109,7 @@ public void tearDown() throws Exception {
107109
} else {
108110
error.addSuppressed(e);
109111
}
110-
PDETestsPlugin.getDefault().getLog().error(message, e);
112+
ILog.get().error(message, e);
111113
}
112114
}
113115
TestUtils.waitForJobs(name.getMethodName(), 10, 10000);
@@ -195,8 +197,7 @@ public static void assumeRunningInStandaloneEclipseSDK() {
195197
*/
196198
public static void copyFromThisBundleInto(String rootPath, Path targetRoot)
197199
throws IOException, URISyntaxException {
198-
Class<?> caller = STACK_WALKER.getCallerClass();
199-
Bundle bundle = FrameworkUtil.getBundle(caller);
200+
Bundle bundle = FrameworkUtil.getBundle(PDETestCase.class);
200201
URI rootEntry = bundle.getEntry(rootPath).toURI();
201202
List<URL> entries = Collections.list(bundle.findEntries(rootPath, null, true));
202203
for (URL entry : entries) {
@@ -210,4 +211,42 @@ public static void copyFromThisBundleInto(String rootPath, Path targetRoot)
210211
}
211212
}
212213
}
214+
215+
public static Path getThisBundlesStateLocation() {
216+
Bundle bundle = FrameworkUtil.getBundle(PDETestCase.class);
217+
return Platform.getStateLocation(bundle).toPath();
218+
}
219+
220+
public static Path doUnZip(Path targetDirectory, String archivePath) throws IOException {
221+
URL zipURL = FrameworkUtil.getBundle(PDETestCase.class).getEntry(archivePath);
222+
assertNotNull("Zip file not found at path " + archivePath, zipURL);
223+
try (ZipInputStream zipStream = new ZipInputStream(zipURL.openStream())) {
224+
for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream.getNextEntry()) {
225+
if (!entry.isDirectory()) {
226+
Path file = targetDirectory.resolve(entry.getName());
227+
Files.createDirectories(file.getParent());
228+
Files.copy(zipStream, file, StandardCopyOption.REPLACE_EXISTING);
229+
}
230+
}
231+
return targetDirectory;
232+
}
233+
}
234+
235+
/**
236+
* Recursively deletes the directory and files within.
237+
*
238+
* @param dir
239+
* directory to delete
240+
*/
241+
public static void delete(File dir) {
242+
File[] files = dir.listFiles();
243+
for (File file : files) {
244+
if (file.isDirectory()) {
245+
delete(file);
246+
} else {
247+
file.delete();
248+
}
249+
}
250+
dir.delete();
251+
}
213252
}

ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/PDETestsPlugin.java

-43
This file was deleted.

ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/AbstractBuildValidationTest.java

+26-86
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,19 @@
1717
import static org.junit.Assert.assertFalse;
1818
import static org.junit.Assert.assertTrue;
1919

20-
import java.io.BufferedInputStream;
21-
import java.io.BufferedOutputStream;
22-
import java.io.File;
23-
import java.io.FileFilter;
24-
import java.io.FileOutputStream;
25-
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.net.URL;
28-
import java.util.Enumeration;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
2922
import java.util.PropertyResourceBundle;
30-
import java.util.zip.ZipEntry;
31-
import java.util.zip.ZipFile;
23+
import java.util.stream.Stream;
3224

3325
import org.eclipse.core.resources.IMarker;
3426
import org.eclipse.core.resources.IProject;
27+
import org.eclipse.core.resources.IProjectDescription;
3528
import org.eclipse.core.resources.IResource;
3629
import org.eclipse.core.resources.IncrementalProjectBuilder;
3730
import org.eclipse.core.resources.ProjectScope;
3831
import org.eclipse.core.resources.ResourcesPlugin;
3932
import org.eclipse.core.runtime.CoreException;
40-
import org.eclipse.core.runtime.FileLocator;
41-
import org.eclipse.core.runtime.IPath;
4233
import org.eclipse.core.runtime.NullProgressMonitor;
4334
import org.eclipse.core.runtime.OperationCanceledException;
4435
import org.eclipse.core.runtime.jobs.Job;
@@ -50,9 +41,12 @@
5041
import org.eclipse.pde.internal.core.natures.PDE;
5142
import org.eclipse.pde.internal.ui.PDEPlugin;
5243
import org.eclipse.pde.internal.ui.correction.ResolutionGenerator;
53-
import org.eclipse.pde.ui.tests.PDETestsPlugin;
44+
import org.eclipse.pde.ui.tests.PDETestCase;
45+
import org.eclipse.pde.ui.tests.util.ProjectUtils;
5446
import org.eclipse.ui.IMarkerResolution;
55-
import org.junit.Before;
47+
import org.junit.BeforeClass;
48+
import org.junit.ClassRule;
49+
import org.junit.rules.TestRule;
5650
import org.osgi.service.prefs.BackingStoreException;
5751

5852
/**
@@ -65,35 +59,27 @@
6559
*/
6660
public abstract class AbstractBuildValidationTest {
6761

62+
@ClassRule
63+
public static final TestRule CLEAR_WORKSPACE = ProjectUtils.DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER;
64+
6865
private static final String MARKER = "marker";
6966
private static final String MULTIPLE_MARKERS = "multipleMarkers";
7067

71-
@Before
72-
public void setUp() throws Exception {
73-
URL location = PDETestsPlugin.getBundleContext().getBundle().getEntry("/tests/build.properties/build.properties.tests.zip");
74-
File projectFile = new File(FileLocator.toFileURL(location).getFile());
75-
assertTrue("Could not find test zip file at " + projectFile, projectFile.isFile());
76-
doUnZip(PDETestsPlugin.getDefault().getStateLocation().removeLastSegments(2), "/tests/build.properties/build.properties.tests.zip");
68+
@BeforeClass
69+
public static void setUp() throws Exception {
70+
Path workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toPath();
71+
PDETestCase.doUnZip(workspaceLocation, "/tests/build.properties/build.properties.tests.zip");
7772

78-
projectFile = PDETestsPlugin.getDefault().getStateLocation().removeLastSegments(3).toFile();
79-
File[] projects = projectFile.listFiles((FileFilter) pathname -> {
80-
int index = pathname.getName().lastIndexOf('.');
81-
if (index > 1 && pathname.isDirectory()) { // look out for "CVS"
82-
// files in the
83-
// workspace
84-
return true;
85-
}
86-
return false;
87-
});
88-
for (File projectFileName : projects) {
89-
IProject project = findProject(projectFileName.getName());
90-
if (project.exists()) {
91-
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
92-
project.delete(true, new NullProgressMonitor());
93-
}
94-
project.create(new NullProgressMonitor());
95-
project.open(new NullProgressMonitor());
73+
try (Stream<Path> directories = Files.walk(workspaceLocation, 1)) {
74+
var projectNames = directories.filter(Files::isDirectory)
75+
.filter(d -> Files.exists(d.resolve(IProjectDescription.DESCRIPTION_FILE_NAME)))
76+
.map(d -> d.getFileName().toString());
9677

78+
for (String projectName : (Iterable<String>) projectNames::iterator) {
79+
IProject project = findProject(projectName);
80+
project.create(new NullProgressMonitor());
81+
project.open(new NullProgressMonitor());
82+
}
9783
}
9884
}
9985

@@ -220,37 +206,6 @@ private String getProperty(PropertyResourceBundle propertyBundle, String entry,
220206
return getProperty(propertyBundle, entry + '.' + property);
221207
}
222208

223-
/**
224-
* Unzips the given archive to the specified location.
225-
*
226-
* @param location path in the local file system
227-
* @param archivePath path to archive relative to the test plug-in
228-
*/
229-
protected IPath doUnZip(IPath location, String archivePath) throws IOException {
230-
URL zipURL = PDETestsPlugin.getBundleContext().getBundle().getEntry(archivePath);
231-
File zipPath = new File(FileLocator.toFileURL(zipURL).getFile());
232-
try (ZipFile zipFile = new ZipFile(zipPath)) {
233-
Enumeration<? extends ZipEntry> entries = zipFile.entries();
234-
IPath parent = location.removeLastSegments(1);
235-
while (entries.hasMoreElements()) {
236-
ZipEntry entry = entries.nextElement();
237-
if (!entry.isDirectory()) {
238-
IPath entryPath = parent.append(entry.getName());
239-
File dir = entryPath.removeLastSegments(1).toFile();
240-
dir.mkdirs();
241-
File file = entryPath.toFile();
242-
file.createNewFile();
243-
try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
244-
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) {
245-
byte[] bytes = inputStream.readAllBytes();
246-
outputStream.write(bytes);
247-
}
248-
}
249-
}
250-
return parent;
251-
}
252-
}
253-
254209
/**
255210
* Build the given project and wait till the build the complete
256211
* @param project project to be build
@@ -293,27 +248,12 @@ protected void setPreferences(IProject project, int severity) throws BackingStor
293248
projectPrefs.sync();
294249
}
295250

296-
/**
297-
* Sets the given project specific preferences
298-
*
299-
* @param project project for which the preference are to be set
300-
* @param pref the preference
301-
* @param value the value
302-
*/
303-
protected void setPreference(IProject project, String node, String pref, String value) throws BackingStoreException {
304-
ProjectScope scope = new ProjectScope(project);
305-
IEclipsePreferences projectPrefs = scope.getNode(node);
306-
projectPrefs.put(pref, value);
307-
projectPrefs.flush();
308-
projectPrefs.sync();
309-
}
310-
311251
/**
312252
* Find the project in workspace with the given id
313253
* @param id project id
314254
* @return project
315255
*/
316-
protected IProject findProject(String id) {
256+
protected static IProject findProject(String id) {
317257
IPluginModelBase model = PluginRegistry.findModel(id);
318258
if (model != null) {
319259
IResource resource = model.getUnderlyingResource();

ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidationTest.java

-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.eclipse.core.runtime.CoreException;
2828
import org.eclipse.pde.internal.core.builders.CompilerFlags;
2929
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
30-
import org.junit.Before;
3130
import org.junit.Test;
3231
import org.osgi.service.prefs.BackingStoreException;
3332

@@ -39,17 +38,6 @@
3938
*/
4039
public class BuildPropertiesValidationTest extends AbstractBuildValidationTest {
4140

42-
private static boolean fOneTimeSetupComplete = false;
43-
44-
@Override
45-
@Before
46-
public void setUp() throws Exception {
47-
if (fOneTimeSetupComplete)
48-
return;
49-
super.setUp();
50-
fOneTimeSetupComplete = true;
51-
}
52-
5341
@Test
5442
public void testSourceFolder() throws CoreException, BackingStoreException, IOException {
5543
for (int i = 1; i <= 5; i++) {

0 commit comments

Comments
 (0)