Skip to content

Commit

Permalink
Replace remaining usages of CoreTest
Browse files Browse the repository at this point in the history
This change replaces all remaining usages of the JUnit 3-specific
CoreTest class as follows:
- Provide plug-in ID of test harness plug-in via utility class rather
than JUnit 3 base test class (CoreTest)
- Provide test logging utility using ILog via utility class rather than
JUnit 3 base test class (CoreTest)
- Directly use delegated getRandomLocation() method from
FileSystemHelper
  • Loading branch information
HeikoKlare committed Dec 18, 2023
1 parent d28f54e commit 95bde25
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.filesystem.wrapper;

import static org.eclipse.core.runtime.Platform.getBundle;
import static org.eclipse.core.tests.resources.ResourceTestPluginConstants.PI_RESOURCES_TESTS;

import java.net.URI;
Expand All @@ -23,8 +24,10 @@
import org.eclipse.core.filesystem.provider.FileSystem;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
* A simple file system implementation that acts as a wrapper around the
Expand Down Expand Up @@ -93,7 +96,8 @@ public IFileStore getStore(URI uri) {
try {
baseStore = EFS.getStore(getBasicURI(uri));
} catch (CoreException e) {
CoreTest.log(PI_RESOURCES_TESTS, e);
ILog.of(getBundle(PI_RESOURCES_TESTS))
.log(new Status(IStatus.ERROR, PI_RESOURCES_TESTS, IStatus.ERROR, "Error", e));
return NULL_ROOT;
}
return WrapperFileStore.newInstance(customFS, baseStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ public static void runWithBundles(String tag, Runnable runnable, BundleContext c
try {
installed[i] = installBundle(tag + ".setup.0", context, locations[i]);
Assert.assertEquals(tag + ".setup.1." + locations[i], Bundle.INSTALLED, installed[i].getState());
} catch (BundleException e) {
CoreTest.fail(tag + ".setup.2" + locations[i], e);
} catch (IOException e) {
CoreTest.fail(tag + ".setup.3" + locations[i], e);
} catch (BundleException | IOException e) {
throw new IllegalStateException(
"Exception occurred when setting up bundle at location " + locations[i] + ": " + e);
}
}
if (listener != null) {
Expand All @@ -131,7 +130,8 @@ public static void runWithBundles(String tag, Runnable runnable, BundleContext c
try {
installed[i].uninstall();
} catch (BundleException e) {
CoreTest.fail(tag + ".tearDown.1." + locations[i], e);
throw new IllegalStateException(
"Exception occurred when removing bundle at location " + locations[i] + ": " + e);
}
}
BundleTestingHelper.resolveBundles(context, installed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*******************************************************************************/
package org.eclipse.core.tests.harness;

import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -94,8 +97,8 @@ public static void clear(java.io.File file) {
}
}
if (!file.delete()) {
String message = "ensureDoesNotExistInFileSystem(File) could not delete: " + file.getPath();
CoreTest.log(CoreTest.PI_HARNESS, new Status(IStatus.WARNING, CoreTest.PI_HARNESS, IStatus.OK, message, null));
String message = "FileSystemHelper#clear() could not delete: " + file.getPath();
log(new Status(IStatus.WARNING, PI_HARNESS, IStatus.OK, message, null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.harness;

import static org.junit.Assert.fail;

import junit.framework.TestCase;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.test.performance.Dimension;
Expand Down Expand Up @@ -70,7 +72,7 @@ public final void run(TestCase testCase, String localName, int outer, int inner)
try {
runTest(meter, localName, outer, inner);
} catch (Exception e) {
CoreTest.fail("Failed performance test", e);
fail("Failed performance test with exception:" + e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2023 Vector Informatik GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*******************************************************************************/
package org.eclipse.core.tests.harness;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;

public final class TestHarnessPlugin {
/**
* ID of this plug-in
*/
public static final String PI_HARNESS = "org.eclipse.core.tests.harness";

private TestHarnessPlugin() {
}

/**
* Logs the given status via {@link ILog} for the ID of this plug-in
* {@link #PI_HARNESS}.
*/
public static void log(IStatus status) {
ILog.of(Platform.getBundle(PI_HARNESS)).log(status);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.core.tests.session;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -36,7 +37,6 @@
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.harness.FileSystemHelper;
import org.eclipse.core.tests.session.SetupManager.SetupException;
import org.eclipse.osgi.service.datalocation.Location;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void addMinimalBundleSet() {
addBundle(org.eclipse.core.runtime.content.IContentType.class); // org.eclipse.core.contenttype
addBundle(org.eclipse.equinox.app.IApplication.class); // org.eclipse.equinox.app

addBundle(org.eclipse.core.tests.harness.CoreTest.class); // org.eclipse.core.tests.harness
addBundle(org.eclipse.core.tests.harness.TestHarnessPlugin.class); // org.eclipse.core.tests.harness
addBundle(org.eclipse.test.performance.Performance.class); // org.eclipse.test.performance

addBundle(org.eclipse.jdt.internal.junit.runner.ITestLoader.class); // org.eclipse.jdt.junit.runtime
Expand Down Expand Up @@ -214,8 +214,7 @@ private String getBundleReference(Bundle bundle, String suffix) {
try {
externalForm = location.get().toURI().toURL().toExternalForm();
} catch (Exception e) {
CoreTest.fail("Failed to convert file to URL string:" + location.get(), e);
return null; // Cannot happen
throw new IllegalArgumentException("Failed to convert file to URL string:" + location.get(), e);
}
// workaround for bug 88070
return "reference:" + externalForm + (suffix != null ? suffix : "");
Expand Down Expand Up @@ -254,7 +253,7 @@ public void run(TestResult result) {
try {
createConfigINI();
} catch (IOException e) {
CoreTest.fail("0.1", e);
fail(e);
}
}
if (!shouldSort || isSharedSession()) {
Expand All @@ -276,7 +275,7 @@ public void run(TestResult result) {
try {
createConfigINI();
} catch (IOException e) {
CoreTest.fail("0.1", e);
fail(e);
}
// end of KLUDGE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*******************************************************************************/
package org.eclipse.core.tests.session;

import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -29,7 +32,6 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.tests.harness.CoreTest;

/**
* This class is responsible for launching JUnit tests on a separate Eclipse session and collect
Expand Down Expand Up @@ -203,7 +205,7 @@ public void run() {
// no need to throw exception
return;
}
CoreTest.log(CoreTest.PI_HARNESS, e);
log(new Status(IStatus.WARNING, PI_HARNESS, IStatus.ERROR, "Error", e));
} finally {
// remember we are already finished
markAsFinished();
Expand All @@ -227,7 +229,7 @@ public void shutdown() {
try {
serverSocket.close();
} catch (IOException e) {
CoreTest.log(CoreTest.PI_HARNESS, e);
log(new Status(IStatus.ERROR, PI_HARNESS, IStatus.ERROR, "Error", e));
}
notifyAll();
}
Expand Down Expand Up @@ -292,7 +294,7 @@ public final void run(Test test, TestResult result, Setup setup, boolean crashTe
collector.shutdown();
// ensure the session ran without any errors
if (!status.isOK()) {
CoreTest.log(CoreTest.PI_HARNESS, status);
log(status);
if (status.getSeverity() == IStatus.ERROR) {
result.addError(test, new CoreException(status));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import junit.framework.TestCase;
import junit.framework.TestResult;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.session.SessionTestRunner;
import org.eclipse.core.tests.session.SessionTestSuite;
import org.eclipse.core.tests.session.SetupManager;
Expand All @@ -29,7 +30,7 @@ public void testMultipleRuns() throws SetupManager.SetupException {
// the test case to run multiple times
TestDescriptor test = new TestDescriptor(SampleSessionTest.class.getName(), "testApplicationStartup");
test.setApplicationId(SessionTestSuite.CORE_TEST_APPLICATION);
test.setPluginId(CoreTest.PI_HARNESS);
test.setPluginId(PI_HARNESS);
test.setTestRunner(new SessionTestRunner());
// setup the command line to be passed to the multiple runs so it has the right system properties
test.setSetup(SetupManager.getInstance().getDefaultSetup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import junit.framework.*;
import org.eclipse.core.tests.harness.CoreTest;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.tests.session.PerformanceSessionTestSuite;
import org.eclipse.core.tests.session.TestDescriptor;

public class MultipleRunsTest2 extends TestCase {
public static Test suite() {
PerformanceSessionTestSuite suite = new PerformanceSessionTestSuite(CoreTest.PI_HARNESS, 10);
PerformanceSessionTestSuite suite = new PerformanceSessionTestSuite(PI_HARNESS, 10);
suite.addTest(new TestDescriptor(SampleSessionTest.class.getName(), "testApplicationStartup"));
return suite;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.session.SessionTestSuite;

public class SampleCrashTest extends TestCase {
Expand All @@ -40,7 +41,7 @@ public void test3() {
}

public static Test suite() {
SessionTestSuite sameSession = new SessionTestSuite(CoreTest.PI_HARNESS);
SessionTestSuite sameSession = new SessionTestSuite(PI_HARNESS);
sameSession.addTest(new SampleCrashTest("test1"));
sameSession.addCrashTest(new SampleCrashTest("test2"));
sameSession.addTest(new SampleCrashTest("test3"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import junit.framework.*;
import org.eclipse.core.tests.harness.CoreTest;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.tests.session.SessionTestSuite;
import org.eclipse.test.performance.*;
import org.eclipse.test.performance.Dimension;
import org.eclipse.test.performance.Performance;
import org.eclipse.test.performance.PerformanceMeter;

public class SampleSessionTest extends TestCase {
public SampleSessionTest(String methodName) {
Expand Down Expand Up @@ -47,7 +51,7 @@ public void testApplicationStartup() {
}

public static Test suite() {
return new SessionTestSuite(CoreTest.PI_HARNESS, SampleSessionTest.class);
return new SessionTestSuite(PI_HARNESS, SampleSessionTest.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.session.SessionTestSuite;

public class SampleTests extends TestSuite {
public SampleTests() {
addTest(SampleSessionTest.suite());
addTest(UISampleSessionTest.suite());
TestSuite another = new SessionTestSuite(CoreTest.PI_HARNESS);
TestSuite another = new SessionTestSuite(PI_HARNESS);
another.addTestSuite(SampleSessionTest.class);
addTest(another);
// these tests should run in the same session (don't add to a non-shared session test suite)
SessionTestSuite shared = new SessionTestSuite(CoreTest.PI_HARNESS);
SessionTestSuite shared = new SessionTestSuite(PI_HARNESS);
shared.addTestSuite(SameSessionTest.class);
shared.setSharedSession(true);
addTest(shared);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
*******************************************************************************/
package org.eclipse.core.tests.session.samples;

import java.util.Date;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;

import java.util.Date;
import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.session.SessionTestSuite;
import org.eclipse.test.performance.*;
import org.eclipse.test.performance.Dimension;
import org.eclipse.test.performance.Performance;
import org.eclipse.test.performance.PerformanceMeter;

public class UISampleSessionTest extends TestCase {
public UISampleSessionTest(String methodName) {
Expand Down Expand Up @@ -53,7 +55,7 @@ public void testApplicationStartup() {
}

public static Test suite() {
SessionTestSuite suite = new SessionTestSuite(CoreTest.PI_HARNESS);
SessionTestSuite suite = new SessionTestSuite(PI_HARNESS);
suite.setApplicationId(SessionTestSuite.UI_TEST_APPLICATION);
for (int i = 0; i < 3; i++)
suite.addTest(new UISampleSessionTest("testApplicationStartup"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.preferences;

import static org.eclipse.core.tests.harness.FileSystemHelper.getRandomLocation;

import java.util.Properties;
import org.eclipse.core.internal.preferences.EclipsePreferences;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.tests.harness.CoreTest;
import org.osgi.service.prefs.BackingStoreException;

/*
Expand All @@ -38,7 +39,7 @@ public class TestScope2 extends EclipsePreferences implements IScopeContext {
private IPath location;

static {
baseLocation = new CoreTest().getRandomLocation();
baseLocation = getRandomLocation();
}

public TestScope2() {
Expand Down

0 comments on commit 95bde25

Please sign in to comment.