Skip to content

Commit

Permalink
refactor builder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 20, 2020
1 parent 8fe6793 commit 05a6cc3
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 296 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public final class BuildAccessRulesTests extends BuilderTestSuite {
@Before
public void setUp() throws Exception {
prj = env.addProject("Project", "1.7");
src = prj.append("src");
env.createFolder(src);
src = env.getPackageFragmentRootPath(prj, "src");
env.setClasspath(prj, new IClasspathEntry[] {
JavaCore.newSourceEntry(src),
GroovyRuntime.newGroovyClasspathContainerEntry(false, false, null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.codehaus.groovy.eclipse.core.model.GroovyRuntime;
import org.codehaus.groovy.runtime.StringGroovyMethods;
Expand All @@ -30,7 +29,6 @@
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
Expand All @@ -49,7 +47,6 @@
import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.util.Util;
import org.eclipse.jdt.launching.JavaRuntime;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -131,7 +128,7 @@ protected final void fullBuild() {
}
}

protected final void fullBuild(IPath projectPath) {
protected final void fullBuild(final IPath projectPath) {
debugRequestor.clearResult();
debugRequestor.activate();
try {
Expand All @@ -151,7 +148,7 @@ protected final void incrementalBuild() {
}
}

protected final void incrementalBuild(IPath projectPath) {
protected final void incrementalBuild(final IPath projectPath) {
debugRequestor.clearResult();
debugRequestor.activate();
try {
Expand All @@ -161,8 +158,7 @@ protected final void incrementalBuild(IPath projectPath) {
}
}

@SuppressWarnings("cast")
protected void executeClass(IPath projectPath, String className, String expectingOutput, String expectedError) {
protected void executeClass(final IPath projectPath, final String className, final String expectingOutput, final String expectedError) {
List<String> classpath = new ArrayList<>();
IPath workspacePath = env.getWorkspaceRootPath();
classpath.add(workspacePath.append(env.getOutputLocation(projectPath)).toOSString());
Expand Down Expand Up @@ -209,34 +205,18 @@ protected void executeClass(IPath projectPath, String className, String expectin

//--------------------------------------------------------------------------

protected final void expectingCompiledClasses(String... expected) {
protected final void expectingCompiledClasses(final String... expected) {
String[] actual = ReflectionUtils.executePrivateMethod(debugRequestor.getClass(), "getCompiledClasses", debugRequestor);
Util.sort(actual);
Util.sort(expected);
expectingCompiling(actual, expected, "unexpected recompiled units. lenExpected=" + expected.length + " lenActual=" + actual.length);
Arrays.sort(actual);
Arrays.sort(expected);
Assert.assertArrayEquals(expected, actual);
}

private void expectingCompiling(String[] actual, String[] expected, String message) {
StringBuilder actualBuffer = new StringBuilder("{");
for (int i = 0; i < actual.length; i += 1) {
if (i > 0) actualBuffer.append(",");
actualBuffer.append(actual[i]);
}
actualBuffer.append("}");
StringBuilder expectedBuffer = new StringBuilder("{");
for (int i = 0; i < expected.length; i += 1) {
if (i > 0) expectedBuffer.append(",");
expectedBuffer.append(expected[i]);
}
expectedBuffer.append("}");
Assert.assertEquals(message, expectedBuffer.toString(), actualBuffer.toString());
}

protected final void expectingProblemsFor(IPath root, List<String> expected) {
protected final void expectingProblemsFor(final IPath root, final List<String> expected) {
expectingProblemsFor(new IPath[] {root}, expected);
}

protected final void expectingProblemsFor(IPath[] roots, List<String> expected) {
protected final void expectingProblemsFor(final IPath[] roots, final List<String> expected) {
Problem[] allProblems = getSortedProblems(roots);
TestCase.assertStringEquals(toString(expected), toString(Arrays.asList(allProblems)), false);
}
Expand All @@ -245,16 +225,16 @@ protected final void expectingNoProblems() {
expectingNoProblemsFor(env.getWorkspaceRootPath());
}

protected final void expectingNoProblemsFor(IPath... roots) {
protected final void expectingNoProblemsFor(final IPath... roots) {
Problem[] allProblems = getSortedProblems(roots);
TestCase.assertStringEquals("", toString(Arrays.asList(allProblems)), false);
}

protected final void expectingSpecificProblemFor(IPath root, Problem problem) {
protected final void expectingSpecificProblemFor(final IPath root, final Problem problem) {
expectingSpecificProblemsFor(root, new Problem[] {problem});
}

protected void expectingSpecificProblemsFor(IPath root, Problem[] problems) {
protected void expectingSpecificProblemsFor(final IPath root, final Problem[] problems) {
Problem[] rootProblems = env.getProblemsFor(root);
next: for (int i = 0; i < problems.length; i += 1) {
Problem problem = problems[i];
Expand All @@ -275,15 +255,15 @@ protected void expectingSpecificProblemsFor(IPath root, Problem[] problems) {
}
}

protected final void printProblemsFor(IPath... roots) {
protected final void printProblemsFor(final IPath... roots) {
for (IPath root : roots) {
Problem[] problems = env.getProblemsFor(root);
System.out.println(toString(Arrays.asList(problems)));
System.out.println();
}
}

private Problem[] getSortedProblems(IPath[] roots) {
private Problem[] getSortedProblems(final IPath[] roots) {
List<Problem> allProblems = new ArrayList<>();
for (IPath root : roots) {
Collections.addAll(allProblems, env.getProblemsFor(root));
Expand All @@ -294,7 +274,7 @@ private Problem[] getSortedProblems(IPath[] roots) {
return allProblems.toArray(new Problem[0]);
}

private static String toString(Iterable<?> seq) {
private static String toString(final Iterable<?> seq) {
StringBuilder buf = new StringBuilder();
for (Object obj : seq) {
buf.append(obj).append('\n');
Expand All @@ -307,17 +287,19 @@ private static String toString(Iterable<?> seq) {
protected static class TestingEnvironment extends org.eclipse.jdt.core.tests.builder.TestingEnvironment {

@Override
public IPath addProject(String projectName) {
public IPath addProject(final String projectName) {
return addProject(projectName, "1.6");
}

@Override
public IPath addProject(String projectName, String compliance) {
public IPath addProject(final String projectName, final String compliance) {
try {
IPath projectPath = super.addProject(projectName, compliance);
removePackageFragmentRoot(projectPath, "");
addPackageFragmentRoot(projectPath, "src");
addGroovyNature(projectName);

new ProjectScope(getProject(projectName)).getNode(JavaRuntime.ID_PLUGIN)
.put(JavaRuntime.PREF_COMPILER_COMPLIANCE_DOES_NOT_MATCH_JRE, JavaCore.IGNORE);
// add JRE container to classpath
IClasspathAttribute[] attributes;
if (JavaCore.compareJavaVersions(compliance, "9") < 0) {
attributes = new IClasspathAttribute[0];
Expand All @@ -326,15 +308,13 @@ public IPath addProject(String projectName, String compliance) {
}
addEntry(projectPath, JavaCore.newContainerEntry(JavaRuntime.newDefaultJREContainerPath(), new IAccessRule[0], attributes, false));

addGroovyNature(projectName);

return projectPath;
} catch (JavaModelException e) {
throw new RuntimeException(e);
}
}

public void addGroovyNature(String projectName) {
public void addGroovyNature(final String projectName) {
try {
IProject project = getProject(projectName);
IProjectDescription description = project.getDescription();
Expand All @@ -345,7 +325,7 @@ public void addGroovyNature(String projectName) {
}
}

public void removeGroovyNature(String projectName) {
public void removeGroovyNature(final String projectName) {
try {
IProject project = getProject(projectName);
IProjectDescription description = project.getDescription();
Expand All @@ -356,7 +336,7 @@ public void removeGroovyNature(String projectName) {
}
}

public void addGroovyJars(IPath projectPath) throws Exception {
public void addGroovyJars(final IPath projectPath) throws Exception {
boolean minimal = false, modular = false;
for (IClasspathEntry cpe : getJavaProject(projectPath).getRawClasspath()) {
if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER &&
Expand All @@ -373,96 +353,36 @@ public void addGroovyJars(IPath projectPath) throws Exception {
addEntry(projectPath, GroovyRuntime.newGroovyClasspathContainerEntry(minimal, modular, null));
}

public void addJar(IPath projectPath, String path) throws Exception {
URL jar = Platform.getBundle("org.eclipse.jdt.groovy.core.tests.builder").getEntry(path);
/**
* @param jarPath resource in builder tests project, e.g. "lib/xyz.jar"
*/
public void addJar(final IPath projectPath, final String jarPath) throws Exception {
URL jar = Platform.getBundle("org.eclipse.jdt.groovy.core.tests.builder").getEntry(jarPath);
addExternalJar(projectPath, FileLocator.resolve(jar).getFile());
}

@Override
public void addEntry(IPath projectPath, IClasspathEntry entryPath) throws JavaModelException {
IClasspathEntry[] classpath = getClasspath(projectPath);
// first look to see if the entry already exists
for (IClasspathEntry entry : classpath) {
if (entry.equals(entryPath)) {
return;
}
public void addEntry(final IPath projectPath, final IClasspathEntry entry) throws JavaModelException {
if (Arrays.stream(getClasspath(projectPath)).noneMatch(entry::equals)) {
super.addEntry(projectPath, entry);
}
super.addEntry(projectPath, entryPath);
}

/**
* Adds a groovy class with the given contents to the given
* package in the workspace. The package is created
* if necessary. If a class with the same name already
* exists, it is replaced. A workspace must be open,
* and the given class name must not end with ".java".
* Returns the path of the added class.
*/
public IPath addGroovyClass(IPath packagePath, String className, String contents) {
return addGroovyClassExtension(packagePath, className, contents, null);
}
public IPath addGroovyClass(final IPath packagePath, final String className, final String contents) {
IPath filePath = packagePath.append(className.endsWith(".groovy") ? className : className + ".groovy");
createFile(filePath, contents.getBytes(StandardCharsets.US_ASCII));

/**
* Adds a groovy class with the given contents to the given
* package in the workspace. The package is created
* if necessary. If a class with the same name already
* exists, it is replaced. A workspace must be open,
* and the given class name must not end with ".java".
* Returns the path of the added class.
*/
public IPath addGroovyClass(IPath packageFragmentRootPath, String packageName, String className, String contents) {
return addGroovyClassExtension(packageFragmentRootPath, packageName, className, contents, null);
}

/**
* Adds a groovy class with the given contents to the given
* package in the workspace, the file will use the specified file suffix.
* The package is created if necessary. If a class with the same name already
* exists, it is replaced.
* Returns the path of the added class.
*/
public IPath addGroovyClassWithSuffix(IPath packagePath, String className, String suffix, String contents) {
return addGroovyClassExtension(packagePath, className, suffix, contents, suffix);
}

public IPath addGroovyClassWithSuffix(IPath packageFragmentRootPath, String packageName, String className, String suffix, String contents) {
return addGroovyClassExtension(packageFragmentRootPath, packageName, className, contents, suffix);
return filePath;
}

/**
* Adds a groovy class with the given contents to the given
* package in the workspace. The package is created
* if necessary. If a class with the same name already
* exists, it is replaced. A workspace must be open,
* and the given class name must not end with ".java".
* Returns the path of the added class.
* @param fileExtension file extension of the groovy class to create (without a '.')
*/
public IPath addGroovyClassExtension(IPath packagePath, String className, String contents, String fileExtension) {
IPath classPath = packagePath.append(className + "." + Optional.ofNullable(fileExtension).orElse("groovy"));
createFile(classPath, contents.getBytes(StandardCharsets.US_ASCII));
return classPath;
}

/**
* Adds a groovy class with the given contents to the given
* package in the workspace. The package is created
* if necessary. If a class with the same name already
* exists, it is replaced. A workspace must be open,
* and the given class name must not end with ".java".
* Returns the path of the added class.
* @param fileExtension file extension of the groovy class to create (without a '.')
*/
public IPath addGroovyClassExtension(IPath packageFragmentRootPath, String packageName, String className, String contents, String fileExtension) {
// make sure the package exists
if (packageName != null && packageName.length() > 0) {
IPath packagePath = addPackage(packageFragmentRootPath, packageName);
return addGroovyClassExtension(packagePath, className, contents, fileExtension);
public IPath addGroovyClass(final IPath packageFragmentRootPath, final String packageName, final String className, final String contents) {
if (packageName == null || packageName.length() < 1) {
return addGroovyClass(packageFragmentRootPath, className, contents);
}
return addGroovyClassExtension(packageFragmentRootPath, className, contents, fileExtension);
return addGroovyClass(addPackage(packageFragmentRootPath, packageName), className, contents);
}

public <U extends ICompilationUnit> U getUnit(IPath path) {
public <U extends ICompilationUnit> U getUnit(final IPath path) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
@SuppressWarnings("unchecked")
U unit = (U) JavaCore.createCompilationUnitFrom(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private static void assertContainsProblem(Set<IProblem> problems, String expecte
private IPath[] createGroovyProject() throws Exception {
IPath prj = env.addProject("Project");
env.addGroovyJars(prj);
env.setOutputFolder(prj, "bin");
env.removePackageFragmentRoot(prj, "");
return new IPath[] {prj, env.addPackageFragmentRoot(prj, "src")};
return new IPath[] {prj, env.getPackageFragmentRootPath(prj, "src")};
}

//--------------------------------------------------------------------------
Expand All @@ -70,7 +68,7 @@ public void testReconcilingWithTransforms_single() throws Exception {
IPath[] paths = createGroovyProject();

//@formatter:off
IPath foo = env.addGroovyClass(paths[1], "", "Foo",
IPath foo = env.addGroovyClass(paths[1], "Foo",
"@Singleton\n" +
"class Foo {\n" +
" void mone() {}\n" +
Expand All @@ -90,7 +88,7 @@ public void testReconcilingWithTransforms_multiple() throws Exception {
IPath[] paths = createGroovyProject();

//@formatter:off
IPath foo = env.addGroovyClass(paths[1], "", "Foo",
IPath foo = env.addGroovyClass(paths[1], "Foo",
"@Singleton\n" +
"class Foo {\n" +
" @Delegate Bar b = new BarImpl();\n" +
Expand All @@ -114,7 +112,7 @@ public void testReconcilingWithTransforms_typeChecked() throws Exception {
IPath[] paths = createGroovyProject();

//@formatter:off
IPath foo = env.addGroovyClass(paths[1], "", "Foo",
IPath foo = env.addGroovyClass(paths[1], "Foo",
"@groovy.transform.TypeChecked\n" +
"class Foo {\n" +
" void xxx(int i) { xxx('abc') }\n" +
Expand All @@ -131,7 +129,7 @@ public void testReconcilingWithTransforms_compileStatic() throws Exception {
IPath[] paths = createGroovyProject();

//@formatter:off
IPath foo = env.addGroovyClass(paths[1], "", "Foo",
IPath foo = env.addGroovyClass(paths[1], "Foo",
"@groovy.transform.CompileStatic\n" +
"class Foo {\n" +
" void xxx(int i) { xxx('abc') }\n" +
Expand Down
Loading

0 comments on commit 05a6cc3

Please sign in to comment.