|
17 | 17 | import static org.junit.Assert.assertFalse;
|
18 | 18 | import static org.junit.Assert.assertTrue;
|
19 | 19 |
|
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; |
29 | 22 | import java.util.PropertyResourceBundle;
|
30 |
| -import java.util.zip.ZipEntry; |
31 |
| -import java.util.zip.ZipFile; |
| 23 | +import java.util.stream.Stream; |
32 | 24 |
|
33 | 25 | import org.eclipse.core.resources.IMarker;
|
34 | 26 | import org.eclipse.core.resources.IProject;
|
| 27 | +import org.eclipse.core.resources.IProjectDescription; |
35 | 28 | import org.eclipse.core.resources.IResource;
|
36 | 29 | import org.eclipse.core.resources.IncrementalProjectBuilder;
|
37 | 30 | import org.eclipse.core.resources.ProjectScope;
|
38 | 31 | import org.eclipse.core.resources.ResourcesPlugin;
|
39 | 32 | import org.eclipse.core.runtime.CoreException;
|
40 |
| -import org.eclipse.core.runtime.FileLocator; |
41 |
| -import org.eclipse.core.runtime.IPath; |
42 | 33 | import org.eclipse.core.runtime.NullProgressMonitor;
|
43 | 34 | import org.eclipse.core.runtime.OperationCanceledException;
|
44 | 35 | import org.eclipse.core.runtime.jobs.Job;
|
|
50 | 41 | import org.eclipse.pde.internal.core.natures.PDE;
|
51 | 42 | import org.eclipse.pde.internal.ui.PDEPlugin;
|
52 | 43 | 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; |
54 | 46 | 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; |
56 | 50 | import org.osgi.service.prefs.BackingStoreException;
|
57 | 51 |
|
58 | 52 | /**
|
|
65 | 59 | */
|
66 | 60 | public abstract class AbstractBuildValidationTest {
|
67 | 61 |
|
| 62 | + @ClassRule |
| 63 | + public static final TestRule CLEAR_WORKSPACE = ProjectUtils.DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER; |
| 64 | + |
68 | 65 | private static final String MARKER = "marker";
|
69 | 66 | private static final String MULTIPLE_MARKERS = "multipleMarkers";
|
70 | 67 |
|
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"); |
77 | 72 |
|
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()); |
96 | 77 |
|
| 78 | + for (String projectName : (Iterable<String>) projectNames::iterator) { |
| 79 | + IProject project = findProject(projectName); |
| 80 | + project.create(new NullProgressMonitor()); |
| 81 | + project.open(new NullProgressMonitor()); |
| 82 | + } |
97 | 83 | }
|
98 | 84 | }
|
99 | 85 |
|
@@ -220,37 +206,6 @@ private String getProperty(PropertyResourceBundle propertyBundle, String entry,
|
220 | 206 | return getProperty(propertyBundle, entry + '.' + property);
|
221 | 207 | }
|
222 | 208 |
|
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 |
| - |
254 | 209 | /**
|
255 | 210 | * Build the given project and wait till the build the complete
|
256 | 211 | * @param project project to be build
|
@@ -293,27 +248,12 @@ protected void setPreferences(IProject project, int severity) throws BackingStor
|
293 | 248 | projectPrefs.sync();
|
294 | 249 | }
|
295 | 250 |
|
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 |
| - |
311 | 251 | /**
|
312 | 252 | * Find the project in workspace with the given id
|
313 | 253 | * @param id project id
|
314 | 254 | * @return project
|
315 | 255 | */
|
316 |
| - protected IProject findProject(String id) { |
| 256 | + protected static IProject findProject(String id) { |
317 | 257 | IPluginModelBase model = PluginRegistry.findModel(id);
|
318 | 258 | if (model != null) {
|
319 | 259 | IResource resource = model.getUnderlyingResource();
|
|
0 commit comments