Skip to content

Commit

Permalink
Further clean-ups and simplifications of smartimport-tests
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
HannesWell committed Sep 16, 2023
1 parent c993045 commit 08286e6
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 182 deletions.
5 changes: 4 additions & 1 deletion ui/org.eclipse.pde.ui.tests.smartimport/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion ui/org.eclipse.pde.ui.tests.smartimport/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 0 additions & 72 deletions ui/org.eclipse.pde.ui.tests.smartimport/pom.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,20 @@
******************************************************************************/
package org.eclipse.ui.tests.smartimport;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

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");
String getProjectName() {
return "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]);
void checkImportedProjectNatures(String[] natureIds) throws CoreException {
assertThat(natureIds).containsExactly("org.eclipse.jdt.core.javanature");
}

@Override
IProject getProject() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(JAVA_ECLIPSE_PROJECT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,20 @@
******************************************************************************/
package org.eclipse.ui.tests.smartimport;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

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]);
String getProjectName() {
return "FeatureProject";
}

@Override
IProject getProject() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME);
void checkImportedProjectNatures(String[] natureIds) throws CoreException {
assertThat(natureIds).containsExactly("org.eclipse.pde.FeatureNature");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,19 @@
******************************************************************************/
package org.eclipse.ui.tests.smartimport;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

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);
String getProjectName() {
return "PlainEclipseProject";
}

@Override
void checkImportedProject() throws CoreException {
IProject project = getProject();
String[] natureIds = project.getDescription().getNatureIds();
assertEquals("This project should not have any nature", 0, natureIds.length);
void checkImportedProjectNatures(String[] natureIds) throws CoreException {
assertThat(natureIds).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,20 @@
******************************************************************************/
package org.eclipse.ui.tests.smartimport;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

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");
String getProjectName() {
return "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]);
void checkImportedProjectNatures(String[] natureIds) throws CoreException {
assertThat(natureIds).containsExactly("org.eclipse.jdt.core.javanature");
}

@Override
IProject getProject() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(PLAIN_JAVA_PROJECT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,71 @@
******************************************************************************/
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 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.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

public abstract class ProjectTestTemplate {

public ProjectTestTemplate() {
@Rule
public final TestRule CLEAR_WORKSPACE = ProjectUtils.DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER;

@BeforeClass
public static void setupClass() throws IOException {
Files.writeString(getErrorLogFile(), ""); // empty error log
}

@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 {
Files.writeString(getErrorLogFile(), ""); // empty error log
}

@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);
String projectName = getProjectName();
File projectPath = new File("target/resources", 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();

checkImportedProject();
}

private void checkErrorLog() throws IOException {
String log = Files.readString(Platform.getLogFileLocation().toFile().toPath());
assertTrue(log.isEmpty());
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);
checkImportedProjectNatures(project.getDescription().getNatureIds());
}

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<IMarker> errorMarkers = Arrays.asList(problems).stream().filter(m -> {
try {
Expand All @@ -96,14 +93,12 @@ private void checkProblemsView() throws CoreException {

}

abstract File getProjectPath();

abstract IProject getProject();
abstract String getProjectName();

/**
* Checks whether the project was imported correctly.
*
*/
abstract void checkImportedProject() throws CoreException;
abstract void checkImportedProjectNatures(String[] natureIds) throws CoreException;

}

0 comments on commit 08286e6

Please sign in to comment.