From 6dc47b2ac824822d6dc2245b8ae7868c0934b655 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 16 Sep 2023 12:19:06 +0200 Subject: [PATCH] Further clean-ups and simplifications of smartimport-tests Generalize redundant code into the template class. Replace the o.e.pde.ui.tests.smartimport/pom.xml by the still required pom.model. configuration in the build.properties (many properties were obsolete anyway). Follow-up on "Rewrite smartimport test as plain JUnit test" --- .../META-INF/MANIFEST.MF | 5 +- .../build.properties | 5 +- .../pom.xml | 72 ------------ .../ui/tests/smartimport/AllTests.java | 29 ----- .../smartimport/EclipseJavaProjectTest.java | 45 -------- .../tests/smartimport/FeatureProjectTest.java | 46 -------- .../smartimport/PlainEclipseProjectTest.java | 44 -------- .../smartimport/PlainJavaProjectTest.java | 46 -------- .../smartimport/ProjectTestTemplate.java | 104 +++++++++++------- .../pde/ui/tests/util/ProjectUtils.java | 8 +- 10 files changed, 78 insertions(+), 326 deletions(-) delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/pom.xml delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/AllTests.java delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/EclipseJavaProjectTest.java delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/FeatureProjectTest.java delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainEclipseProjectTest.java delete mode 100644 ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainJavaProjectTest.java diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/META-INF/MANIFEST.MF b/ui/org.eclipse.pde.ui.tests.smartimport/META-INF/MANIFEST.MF index 9639b9e7758..d73d3d946f7 100644 --- a/ui/org.eclipse.pde.ui.tests.smartimport/META-INF/MANIFEST.MF +++ b/ui/org.eclipse.pde.ui.tests.smartimport/META-INF/MANIFEST.MF @@ -11,11 +11,14 @@ Require-Bundle: org.eclipse.core.resources, org.eclipse.swt, org.eclipse.ui.ide, org.eclipse.jdt.ui, - org.eclipse.pde.ui + org.eclipse.pde.ui, + org.eclipse.pde.ui.tests, + assertj-core Bundle-ActivationPolicy: lazy Eclipse-BundleShape: dir Bundle-RequiredExecutionEnvironment: JavaSE-17 Automatic-Module-Name: org.eclipse.pde.ui.tests.smartimport Import-Package: org.junit, + org.junit.rules, org.junit.runner, org.junit.runners diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/build.properties b/ui/org.eclipse.pde.ui.tests.smartimport/build.properties index 38c2fd61755..6585eb9cbe2 100644 --- a/ui/org.eclipse.pde.ui.tests.smartimport/build.properties +++ b/ui/org.eclipse.pde.ui.tests.smartimport/build.properties @@ -11,6 +11,9 @@ bin.includes = about.html,\ .,\ test.xml,\ - META-INF/ + META-INF/,\ + resources/ src.includes = about.html source.. = src/ +pom.model.packaging = eclipse-test-plugin +pom.model.property.testClass = org.eclipse.ui.tests.smartimport.AllTests diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/pom.xml b/ui/org.eclipse.pde.ui.tests.smartimport/pom.xml deleted file mode 100644 index 2601569976c..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - - 4.0.0 - - org.eclipse.pde - eclipse.pde - 4.30.0-SNAPSHOT - ../../ - - org.eclipse.pde.ui.tests.smartimport - 1.2.0-SNAPSHOT - eclipse-test-plugin - - - -warn:-discouraged - -Dignored.errors.regexp=${ignored.errors.regexp} - -Xmx512m - 1200 - - - - - - - org.eclipse.tycho - tycho-surefire-plugin - - true - false - org.eclipse.ui.tests.smartimport - org.eclipse.ui.tests.smartimport.AllTests - - ${tycho.test.jvmArgs} ${platformSystemProperties} - - **/*Test.class - - - - - - - resources - - ** - - true - - - - - - - osx - - - mac - - - - - -XstartOnFirstThread - - - - diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/AllTests.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/AllTests.java deleted file mode 100644 index 71aa06b7634..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/AllTests.java +++ /dev/null @@ -1,29 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Red Hat, Inc. 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 - * - * Contributor: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package org.eclipse.ui.tests.smartimport; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({ - PlainJavaProjectTest.class, - PlainEclipseProjectTest.class, - EclipseJavaProjectTest.class, - FeatureProjectTest.class - }) -public class AllTests { - -} diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/EclipseJavaProjectTest.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/EclipseJavaProjectTest.java deleted file mode 100644 index 4269ae595d8..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/EclipseJavaProjectTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Red Hat, Inc. 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 - * - * Contributor: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package org.eclipse.ui.tests.smartimport; - -import static org.junit.Assert.assertEquals; - -import java.io.File; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; - -public class EclipseJavaProjectTest extends ProjectTestTemplate { - - private static final String JAVA_ECLIPSE_PROJECT = "JavaEclipseProject"; - - @Override - File getProjectPath() { - return new File("target/resources/JavaEclipseProject"); - } - - @Override - void checkImportedProject() throws CoreException { - IProject project = getProject(); - String[] natureIds = project.getDescription().getNatureIds(); - assertEquals("Project should have exactly 1 nature", 1, natureIds.length); - assertEquals("Project should have java nature", "org.eclipse.jdt.core.javanature", natureIds[0]); - } - - @Override - IProject getProject() { - return ResourcesPlugin.getWorkspace().getRoot().getProject(JAVA_ECLIPSE_PROJECT); - } -} diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/FeatureProjectTest.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/FeatureProjectTest.java deleted file mode 100644 index ada330d8a41..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/FeatureProjectTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Red Hat, Inc. 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 - * - * Contributor: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package org.eclipse.ui.tests.smartimport; - -import static org.junit.Assert.assertEquals; - -import java.io.File; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; - -public class FeatureProjectTest extends ProjectTestTemplate { - - private static final String PROJECT_NAME = "FeatureProject"; - - @Override - File getProjectPath() { - return new File("target/resources/FeatureProject"); - } - - @Override - void checkImportedProject() throws CoreException { - IProject project = getProject(); - String[] natureIds = project.getDescription().getNatureIds(); - assertEquals("Project should have exactly 1 nature", 1, natureIds.length); - assertEquals("Project should have feature nature", "org.eclipse.pde.FeatureNature", natureIds[0]); - } - - @Override - IProject getProject() { - return ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME); - } - -} diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainEclipseProjectTest.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainEclipseProjectTest.java deleted file mode 100644 index 704a1bbbb60..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainEclipseProjectTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Red Hat, Inc. 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 - * - * Contributor: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package org.eclipse.ui.tests.smartimport; - -import static org.junit.Assert.assertEquals; - -import java.io.File; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; - -public class PlainEclipseProjectTest extends ProjectTestTemplate { - - private static final String PLAIN_ECLIPSE_PROJECT = "PlainEclipseProject"; - - @Override - File getProjectPath() { - return new File("target/resources/PlainEclipseProject"); - } - - @Override - IProject getProject() { - return ResourcesPlugin.getWorkspace().getRoot().getProject(PLAIN_ECLIPSE_PROJECT); - } - - @Override - void checkImportedProject() throws CoreException { - IProject project = getProject(); - String[] natureIds = project.getDescription().getNatureIds(); - assertEquals("This project should not have any nature", 0, natureIds.length); - } -} diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainJavaProjectTest.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainJavaProjectTest.java deleted file mode 100644 index 626d813317a..00000000000 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/PlainJavaProjectTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Red Hat, Inc. 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 - * - * Contributor: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package org.eclipse.ui.tests.smartimport; - -import static org.junit.Assert.assertEquals; - -import java.io.File; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; - -public class PlainJavaProjectTest extends ProjectTestTemplate { - - private static final String PLAIN_JAVA_PROJECT = "PlainJavaProject"; - - @Override - public File getProjectPath() { - return new File("target/resources/PlainJavaProject"); - } - - - @Override - void checkImportedProject() throws CoreException { - IProject project = getProject(); - String[] natureIds = project.getDescription().getNatureIds(); - assertEquals("Project should have exactly 1 nature", 1, natureIds.length); - assertEquals("Project should have java nature", "org.eclipse.jdt.core.javanature", natureIds[0]); - } - - @Override - IProject getProject() { - return ResourcesPlugin.getWorkspace().getRoot().getProject(PLAIN_JAVA_PROJECT); - } -} diff --git a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/ProjectTestTemplate.java b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/ProjectTestTemplate.java index 0c24ebe383b..85c68796771 100644 --- a/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/ProjectTestTemplate.java +++ b/ui/org.eclipse.pde.ui.tests.smartimport/src/org/eclipse/ui/tests/smartimport/ProjectTestTemplate.java @@ -13,74 +13,108 @@ ******************************************************************************/ package org.eclipse.ui.tests.smartimport; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.assertj.core.api.Condition; import org.eclipse.core.resources.IMarker; 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.NullProgressMonitor; import org.eclipse.core.runtime.Platform; -import org.eclipse.ui.internal.wizards.datatransfer.SmartImportJob; +import org.eclipse.pde.ui.tests.util.ProjectUtils; import org.junit.After; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class ProjectTestTemplate { + + @Parameters(name = "{0}") + public static Object[][] data() { + return new Object[][] { // + { "JavaEclipseProject", List.of("org.eclipse.jdt.core.javanature") }, // + { "FeatureProject", List.of("org.eclipse.pde.FeatureNature") }, // + { "PlainEclipseProject", List.of() }, // + { "PlainJavaProject", List.of("org.eclipse.jdt.core.javanature") }, // + }; + } + + @Parameter(0) + public String projectName; -public abstract class ProjectTestTemplate { + @Parameter(1) + public List expectedNatures; - public ProjectTestTemplate() { + @ClassRule + public static TemporaryFolder workingDirectory = new TemporaryFolder(); + + @BeforeClass + public static void setupClass() throws IOException, CoreException { + // Copy imported projects to temp-directory to not pollute this project + Path source = Path.of("resources").toRealPath(); + Path target = workingDirectory.getRoot().toPath(); + try (Stream files = Files.walk(source).filter(Files::isRegularFile)) { + for (Path file : (Iterable) files::iterator) { + Path targetFile = target.resolve(source.relativize(file)); + Files.createDirectories(targetFile.getParent()); + Files.copy(file, targetFile); + } + } + Files.writeString(getErrorLogFile(), ""); // empty error log + ProjectUtils.deleteAllWorkspaceProjects(); } @After - public void cleanup() throws CoreException, FileNotFoundException, IOException { - for (IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { - p.delete(true, new NullProgressMonitor()); - } - // empty error log - new FileWriter(Platform.getLogFileLocation().toFile(), false).close(); + public void cleanup() throws IOException, CoreException { + Files.writeString(getErrorLogFile(), ""); // empty error log + ProjectUtils.deleteAllWorkspaceProjects(); } - @BeforeClass - public static void setupClass() { - // empty error log - try { - new FileWriter(Platform.getLogFileLocation().toFile(), false).close(); - } catch (IOException e) { - // ignore - } + private static Path getErrorLogFile() { + return Platform.getLogFileLocation().toFile().toPath(); } @Test @SuppressWarnings("restriction") - public void testImport() throws CoreException, InterruptedException, IOException { - SmartImportJob job = new SmartImportJob(getProjectPath(), null, true, false); + public void testImport() throws CoreException, InterruptedException { + File projectPath = new File(workingDirectory.getRoot(), projectName); + + var job = new org.eclipse.ui.internal.wizards.datatransfer.SmartImportJob(projectPath, null, true, false); job.run(new NullProgressMonitor()); job.join(); // check imported project - checkErrorLog(); - checkProblemsView(); + assertThat(getErrorLogFile()).isEmptyFile(); + IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot(); + assertThat(workspace.getProjects()).hasSize(1).allMatch(p -> p.getName().equals(projectName)); + IProject project = workspace.getProject(projectName); + checkProblemsView(project); - checkImportedProject(); - } + assertThat(project).satisfies(new Condition<>(IProject::isOpen, "is open")); - private void checkErrorLog() throws IOException { - String log = Files.readString(Platform.getLogFileLocation().toFile().toPath()); - assertTrue(log.isEmpty()); + assertThat(project.getDescription().getNatureIds()).containsExactlyElementsOf(expectedNatures); } - private void checkProblemsView() throws CoreException { - IProject project = getProject(); + private void checkProblemsView(IProject project) throws CoreException { IMarker[] problems = project.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); List errorMarkers = Arrays.asList(problems).stream().filter(m -> { try { @@ -96,14 +130,4 @@ private void checkProblemsView() throws CoreException { } - abstract File getProjectPath(); - - abstract IProject getProject(); - - /** - * Checks whether the project was imported correctly. - * - */ - abstract void checkImportedProject() throws CoreException; - } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/ProjectUtils.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/ProjectUtils.java index dd06a8b1c27..6c55b160ab2 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/ProjectUtils.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/ProjectUtils.java @@ -234,9 +234,9 @@ public static IProject createPluginProject(String projectName, String bundleSymb */ public static final TestRule DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER = TestUtils.getThrowingTestRule( // () -> { // Clean-up garbage of other test-classes - ProjectUtils.deleteWorkspaceProjects(Set.of()); + deleteAllWorkspaceProjects(); return null; - }, o -> deleteWorkspaceProjects(Set.of())); + }, o -> deleteAllWorkspaceProjects()); /** * An (intended) {@link org.junit.Rule} that deletes the projects from the @@ -258,6 +258,10 @@ public static IProject createPluginProject(String projectName, String bundleSymb () -> Set.of(ResourcesPlugin.getWorkspace().getRoot().getProjects()), // projectsBefore -> deleteWorkspaceProjects(projectsBefore)); + public static void deleteAllWorkspaceProjects() throws CoreException { + deleteWorkspaceProjects(Set.of()); + } + private static void deleteWorkspaceProjects(Set retainedProjects) throws CoreException { IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); for (IProject project : projects) {