Skip to content

Commit

Permalink
Remove projects from workspace at test startup
Browse files Browse the repository at this point in the history
Various tests are not cleaning up properly after
themselves, causing test failures on subsequent
tests. Therefore start each test by deleting
all projects.

In addition, some tests were creating their test
projects in their constructor. As all the constructors
run before all the tests as part of test discovery
it means that projects were being created in
constructor and interfering with other tests
later. With the deleting of all projects in @AfterEach
these tests would have started failing. Therefore,
change these tests to create their projects
and do other initialize tasks in the setUp method.

For older JUnit3 style tests:
This substantially slows down tests as many tests
rely on sharing the project between multiple tests and
recreating those projects on each run is slow.
Therefore this is not applied universally to
all JUnit3 tests.

For tests that are affected, those tests are moved
to JUnit5 base test.

Part of #117
  • Loading branch information
jonahgraham committed Oct 26, 2022
1 parent ad478ce commit 306708f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static Test suite() {
suite.addTest(LanguageSettingsManagerTests.suite());
suite.addTest(LanguageSettingsSerializableProviderTests.suite());
// Test converted to JUnit5: suite.addTest(LanguageSettingsPersistenceProjectTests.suite());
suite.addTest(LanguageSettingsListenersTests.suite());
// Test converted to JUnit5: suite.addTest(LanguageSettingsListenersTests.suite());
suite.addTest(LanguageSettingsScannerInfoProviderTests.suite());
suite.addTest(LanguageSettingsProviderReferencedProjectsTests.suite());
return suite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

package org.eclipse.cdt.core.language.settings.providers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -25,7 +32,7 @@
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.testplugin.ResourceHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase5;
import org.eclipse.cdt.internal.core.language.settings.providers.LanguageSettingsProvidersSerializer;
import org.eclipse.cdt.internal.core.settings.model.CProjectDescriptionManager;
import org.eclipse.core.resources.IFile;
Expand All @@ -35,13 +42,13 @@
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.jobs.Job;

import junit.framework.TestSuite;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

/**
* Test cases to cover {@link ILanguageSettingsChangeListener} capabilities.
*/
public class LanguageSettingsListenersTests extends BaseTestCase {
public class LanguageSettingsListenersTests extends BaseTestCase5 {
// These should match corresponding entries defined in plugin.xml
private static final String EXTENSION_REGISTERER_PROVIDER_ID = LanguageSettingsExtensionsTests.EXTENSION_REGISTERER_PROVIDER_ID;
private static final String EXTENSION_EDITABLE_PROVIDER_ID = LanguageSettingsExtensionsTests.EXTENSION_EDITABLE_PROVIDER_ID;
Expand Down Expand Up @@ -82,22 +89,8 @@ public ILanguageSettingsChangeEvent getLastEvent() {

private MockLanguageSettingsChangeListener mockLseListener = new MockLanguageSettingsChangeListener();

/**
* Constructor.
* @param name - name of the test.
*/
public LanguageSettingsListenersTests(String name) {
super(name);

}

@Override
protected void setUp() throws Exception {
super.setUp();
}

@Override
protected void tearDown() throws Exception {
@AfterEach
protected void afterEachCleanup() throws Exception {
LanguageSettingsManager.unregisterLanguageSettingsChangeListener(mockLseListener);
LanguageSettingsManager.setWorkspaceProviders(null);
try {
Expand All @@ -108,28 +101,12 @@ protected void tearDown() throws Exception {
} catch (Exception e) {
// ignore
}
super.tearDown(); // includes ResourceHelper cleanup
}

/**
* @return - new TestSuite.
*/
public static TestSuite suite() {
return new TestSuite(LanguageSettingsListenersTests.class);
}

/**
* main function of the class.
*
* @param args - arguments
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}

/**
* Check that global provider does not get unnecessarily registered on start.
*/
@Test
public void testListenerRegisterer_CheckExtensionProvider() throws Exception {
// check if extension provider exists
ILanguageSettingsProvider workspaceProvider = LanguageSettingsManager
Expand All @@ -143,6 +120,7 @@ public void testListenerRegisterer_CheckExtensionProvider() throws Exception {
/**
* Test events triggered for non-shared configuration owned provider.
*/
@Test
public void testListenerRegisterer_OneOwnedByCfg() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -192,6 +170,7 @@ public void testListenerRegisterer_OneOwnedByCfg() throws Exception {
/**
* Test events triggered for non-shared configuration owned multiple providers.
*/
@Test
public void testListenerRegisterer_TwoOwnedByCfgs() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProject(this.getName(), null, new String[] {
Expand Down Expand Up @@ -256,6 +235,7 @@ public void testListenerRegisterer_TwoOwnedByCfgs() throws Exception {
/**
* Test events triggered for shared provider.
*/
@Test
public void testListenerRegisterer_OneGlobal() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -306,6 +286,7 @@ public void testListenerRegisterer_OneGlobal() throws Exception {
/**
* Test events triggered for multiple shared providers.
*/
@Test
public void testListenerRegisterer_TwoGlobal() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProject(this.getName(), null, new String[] {
Expand Down Expand Up @@ -369,6 +350,7 @@ public void testListenerRegisterer_TwoGlobal() throws Exception {
/**
* Test events triggered for shared provider when the provider removed from the list.
*/
@Test
public void testListenerRegisterer_TwoGlobalMinusOne() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProject(this.getName(), null, new String[] {
Expand Down Expand Up @@ -449,6 +431,7 @@ public void testListenerRegisterer_TwoGlobalMinusOne() throws Exception {
/**
* Test events triggered for shared provider define in multiple projects.
*/
@Test
public void testListenerRegisterer_GlobalProviderTwoProjects() throws Exception {
// create project 1
IProject project_1 = ResourceHelper.createCDTProjectWithConfig(this.getName() + ".1");
Expand Down Expand Up @@ -522,6 +505,7 @@ public void testListenerRegisterer_GlobalProviderTwoProjects() throws Exception
/**
* Test events triggered for shared global providers not included in any configuration.
*/
@Test
public void testListenerRegisterer_GlobalProviderNotInUse() throws Exception {
// create project
ILanguageSettingsProvider workspaceProvider = LanguageSettingsManager
Expand Down Expand Up @@ -553,6 +537,7 @@ public void testListenerRegisterer_GlobalProviderNotInUse() throws Exception {
/**
* Test events triggered for shared global provider replacing another one in global list.
*/
@Test
public void testListenerRegisterer_GlobalProviderAddRemoveOutsideTheProject() throws Exception {
// create project
ILanguageSettingsProvider workspaceProvider = LanguageSettingsManager
Expand Down Expand Up @@ -620,6 +605,7 @@ public void testListenerRegisterer_GlobalProviderAddRemoveOutsideTheProject() th
/**
* Test events triggered when empty provider added and the resulting list of entries does not change.
*/
@Test
public void testNotification_cfgProvider_AddEmptyProvider() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -684,6 +670,7 @@ public void testNotification_cfgProvider_AddEmptyProvider() throws Exception {
/**
* Test events triggered where non-empty provider added.
*/
@Test
public void testNotification_cfgProvider_AddNonEmptyProvider() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -757,6 +744,7 @@ public void testNotification_cfgProvider_AddNonEmptyProvider() throws Exception
/**
* Test events triggered during serialization.
*/
@Test
public void testNotification_cfgProvider_SerializeEntries() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -831,6 +819,7 @@ public void testNotification_cfgProvider_SerializeEntries() throws Exception {
/**
* Test events triggered when providers are being added by 2 independent parties in parallel.
*/
@Test
public void testNotification_cfgProvider_SerializeEntriesConcurrent() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -941,6 +930,7 @@ public void testNotification_cfgProvider_SerializeEntriesConcurrent() throws Exc
/**
* Test events triggered during adding global empty provider.
*/
@Test
public void testNotification_globalProvider_AddEmptyProvider() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1012,6 +1002,7 @@ public void testNotification_globalProvider_AddEmptyProvider() throws Exception
/**
* Test events triggered during adding global non-empty provider.
*/
@Test
public void testNotification_globalProvider_AddNonEmptyProvider() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1094,6 +1085,7 @@ public void testNotification_globalProvider_AddNonEmptyProvider() throws Excepti
/**
* Test events triggered during serialization of global shared providers.
*/
@Test
public void testNotification_globalProvider_SerializeEntries() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1268,6 +1260,7 @@ public void testNotification_globalProvider_SerializeEntries() throws Exception
/**
* Test case when a project is present in the list of resources in delta.
*/
@Test
public void testDelta_AffectedResources_Project() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1344,6 +1337,7 @@ public void testDelta_AffectedResources_Project() throws Exception {
/**
* Test case when a default resource (null) is represented in the list of resources in delta.
*/
@Test
public void testDelta_AffectedResources_DefaultResource() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1420,6 +1414,7 @@ public void testDelta_AffectedResources_DefaultResource() throws Exception {
/**
* Test case when a folder is present in the list of resources in delta.
*/
@Test
public void testDelta_AffectedResources_Folder() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1497,6 +1492,7 @@ public void testDelta_AffectedResources_Folder() throws Exception {
/**
* Test case when a file is present in the list of resources in delta.
*/
@Test
public void testDelta_AffectedResources_File() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down Expand Up @@ -1574,6 +1570,7 @@ public void testDelta_AffectedResources_File() throws Exception {
/**
* Test case when a mix of files and folders is present in the list of resources in delta.
*/
@Test
public void testDelta_AffectedResources_Mix() throws Exception {
// create project
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public abstract class FileBasePluginTestCase extends TestCase {
static int numProjects;
static Class className;
static ICProject cPrj;
private Class className2;

public FileBasePluginTestCase() {
}
Expand Down Expand Up @@ -80,7 +81,13 @@ private void initialize(Class aClassName) {

public FileBasePluginTestCase(String name, Class className) {
super(name);
initialize(className);
className2 = className;
}

@Override
protected void setUp() throws Exception {
super.setUp();
initialize(className2);
}

public void cleanupProject() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class DOMFileBasePluginTest extends TestCase {
static int numProjects = 0;
static Class className;
static ICProject cPrj;
private Class className2;

public DOMFileBasePluginTest() {
}
Expand Down Expand Up @@ -84,7 +85,7 @@ private void initialize(Class aClassName) {

public DOMFileBasePluginTest(String name, Class className) {
super(name);
initialize(className);
className2 = className;
}

public void cleanupProject() throws Exception {
Expand All @@ -100,6 +101,12 @@ public void cleanupProject() throws Exception {
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
initialize(className2);
}

@Override
protected void tearDown() throws Exception {
if (project == null || !project.exists())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.pdom.CModelListener;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -115,6 +119,7 @@ protected void setupBase(TestInfo testInfo) throws Exception {
this.testInfo = testInfo;

logMonitoring.start();
removeLeftOverProjects();

CPPASTNameBase.sAllowRecursionBindings = false;
CPPASTNameBase.sAllowNameComputation = false;
Expand All @@ -130,6 +135,7 @@ protected void tearDownBase() throws Exception {
TestScannerProvider.clear();

logMonitoring.stop(fExpectedLoggedNonOK);
BaseTestCase5.removeLeftOverProjects();
}

protected void deleteOnTearDown(File file) {
Expand Down Expand Up @@ -160,6 +166,26 @@ public void setExpectedNumberOfLoggedNonOKStatusObjects(int count) {
fExpectedLoggedNonOK = count;
}

/**
* Some tests don't cleanup after themselves well and leave projects
* in the workspace. Therefore run this code before each test
* to make sure all left over projects are deleted.
*/
public static void removeLeftOverProjects() throws CoreException {
MultiStatus multiStatus = new MultiStatus(BaseTestCase5.class, 0,
"Failed to remove left over projects from previous tests");
for (IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
try {
p.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
} catch (CoreException e) {
multiStatus.add(Status.error("failed to delete " + p.toString(), e));
}
}
if (multiStatus.getChildren().length > 0) {
throw new CoreException(multiStatus);
}
}

public static void waitForIndexer(ICProject project) throws InterruptedException {
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null);
assertTrue(CCoreInternals.getPDOMManager().joinIndexer(INDEXER_TIMEOUT_SEC * 1000, npm()));
Expand Down

0 comments on commit 306708f

Please sign in to comment.