Skip to content

Commit

Permalink
Cleanup classpath & workspace between suites, improve common setup
Browse files Browse the repository at this point in the history
The main goal is to be more verbose on any unexpected error, do not hide
them, have clean and consistent environment between testsuites
executions and between local/Jenkins executions.

Many testsuites are written in a different style and do some assertions
related to the way how testsuite shares common setup data between
different tests extending the testsuite. Unfortunately the logic is
complicated to understand and so many testsuites leave environment in a
different state as before (e.g. leraking created projects, settings like
classpath variables etc). As a short term solution
CleanupAfterSuiteTests can be added after such "messy" test suites to
cleanup environment for the following testsuites.

Extracted / added common code for cleanup:

- org.eclipse.jdt.core.tests.util.Util.cleanupWorkspace(String)
- org.eclipse.jdt.core.tests.util.Util.cleanupClassPathVariablesAndContainers()

Added resource settings in bug.test.b534624 for
ModuleBuilderTests.testBug534624a, otherwise the test fails with
unrelated warning about missing encoding.

Changes setup of CompletionTests, CompletionTests2 and parent class to
avoid errors that happen if both testsuites run together.

See eclipse-jdt#2536
  • Loading branch information
iloveeclipse authored and gayanper committed Sep 7, 2024
1 parent 2791d16 commit d00b5e4
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2024 Andrey Loskutov ([email protected]) 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
*
* Contributors:
* Andrey Loskutov ([email protected]) - initial implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.util;

import junit.framework.Test;
import junit.framework.TestSuite;

public class CleanupAfterSuiteTests extends junit.framework.TestCase {

private static int count = 1;

public CleanupAfterSuiteTests(String name) {
super(name);
}

public static Test suite() {
TestSuite testSuite = new TestSuite("HouseKeeping_" + count);
testSuite.addTest(new TestSuite(CleanupAfterSuiteTests.class));
return testSuite;
}

public void testCleanupWorkspace() throws Exception {
System.out.println("Starting cleaning up workspace");
Util.cleanupWorkspace(getClass().getName());
System.out.println("Done cleaning up workspace");
}

public void testCleanupJavaModel() throws Exception {
System.out.println("Starting cleaning up Java Model");
Util.cleanupClassPathVariablesAndContainers();
System.out.println("Done cleaning up Java Model");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.util.zip.*;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -48,6 +51,7 @@
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.core.JavaModelManager;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class Util {
Expand Down Expand Up @@ -1595,4 +1599,42 @@ public static long getMajorMinorVMVersion() {
}
return -1;
}

/**
* Discards all classpath variables and containers
*/
public static void cleanupClassPathVariablesAndContainers() {
// TODO the following code is not the correct way to remove CP containers
// but it was used since years...
JavaModelManager manager = JavaModelManager.getJavaModelManager();
manager.containers = new HashMap<>(5);
manager.variables = new HashMap<>(5);
}

/**
* Deletes every single project in the workspace
*/
public static void cleanupWorkspace(String testName) throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
// Remove whatever left from previous tests
IProject[] projects = workspace.getRoot().getProjects(IContainer.INCLUDE_HIDDEN);
if(projects.length > 0) {
System.out.println("\n\nWorkspace cleanup before " + testName);
System.out.println("Found following projects: " + Arrays.toString(projects));
for (IProject project : projects) {
System.out.println("Deleting project: " + project);
try {
project.delete(true, null);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
projects = workspace.getRoot().getProjects(IContainer.INCLUDE_HIDDEN);
if(projects.length > 0) {
System.out.println("Still found projects: " + Arrays.toString(projects));
} else {
System.out.println("Workspace cleanup done for " + testName + "\n\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jdt.core.tests.formatter.*;
import org.eclipse.jdt.core.tests.formatter.comment.CommentsTestSuite;
import org.eclipse.jdt.core.tests.junit.extension.TestCase;
import org.eclipse.jdt.core.tests.util.CleanupAfterSuiteTests;

/**
* Runs all formatter tests.
Expand All @@ -42,6 +43,9 @@ public class RunFormatterTests extends junit.framework.TestCase {
TEST_SUITES.add(FormatterJavadocDontIndentTagsTests.class);
TEST_SUITES.add(FormatterJavadocDontIndentTagsDescriptionTests.class);
TEST_SUITES.add(FormatterOldBugsGistTests.class);

// should always be the last one, to cleanup environment after messy tests
TEST_SUITES.add(CleanupAfterSuiteTests.class);
}

public static Class[] getTestClasses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import java.lang.reflect.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.jdt.core.tests.junit.extension.TestCase;
import org.eclipse.jdt.core.tests.util.CleanupAfterSuiteTests;

import junit.framework.Test;
import junit.framework.TestSuite;
Expand All @@ -40,7 +42,10 @@ public static Class[] getAllTestClasses() {
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingTest.class,
ASTPositionsTest.class,
ASTNodeFinderTest.class,
org.eclipse.jdt.core.tests.dom.APIDocumentationTests.class
org.eclipse.jdt.core.tests.dom.APIDocumentationTests.class,

// should always be the last one, to cleanup environment after messy tests
CleanupAfterSuiteTests.class
};
}
public static Test suite() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;

import junit.framework.*;
import junit.framework.ComparisonFailure;

@SuppressWarnings("rawtypes")
public abstract class AbstractJavaModelCompletionTests extends AbstractJavaModelTests implements RelevanceConstants {
Expand Down Expand Up @@ -166,7 +167,7 @@ protected CompletionResult snippetContextComplete(
@Override
public void setUpSuite() throws Exception {
super.setUpSuite();
this.oldOptions = JavaCore.getOptions();
this.oldOptions = JavaCore.getDefaultOptions();
Hashtable<String, String> options = new Hashtable<>(this.oldOptions);
options.put(JavaCore.CODEASSIST_SUBWORD_MATCH, JavaCore.DISABLED);
JavaCore.setOptions(options);
Expand All @@ -187,13 +188,16 @@ public void tearDownSuite() throws Exception {
this.oldOptions = null;
if (COMPLETION_SUITES == null) {
deleteProject("Completion");
COMPLETION_PROJECT = null;
} else {
COMPLETION_SUITES.remove(getClass());
if (COMPLETION_SUITES.size() == 0) {
deleteProject("Completion");
COMPLETION_SUITES = null;
COMPLETION_PROJECT = null;
}
}
Util.cleanupClassPathVariablesAndContainers();
super.tearDownSuite();
}

Expand Down
Loading

0 comments on commit d00b5e4

Please sign in to comment.