Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate ConfigurationSessionTests in configuration package to JUnit 5 #654

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.eclipse.osgi.tests/All OSGi Tests.launch
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit5"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
Expand Down
7 changes: 6 additions & 1 deletion bundles/org.eclipse.osgi.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ Require-Bundle:
org.eclipse.test.performance,
org.junit
Bundle-ActivationPolicy: lazy
Import-Package: org.osgi.service.event,
Import-Package: org.junit.jupiter.api,
org.junit.jupiter.api.extension,
org.junit.jupiter.api.function,
org.junit.jupiter.api.io,
org.junit.platform.suite.api,
org.osgi.service.event,
org.osgi.util.function,
org.osgi.util.measurement,
org.osgi.util.position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
*******************************************************************************/
package org.eclipse.osgi.tests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ org.eclipse.osgi.tests.container.AllTests.class, //
@Suite
@SelectClasses({ //
org.eclipse.osgi.tests.container.AllTests.class, //
org.eclipse.osgi.tests.hooks.framework.AllFrameworkHookTests.class, //
org.eclipse.osgi.tests.internal.plugins.InstallTests.class, //
org.eclipse.osgi.tests.eclipseadaptor.AllTests.class, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*******************************************************************************/
package org.eclipse.osgi.tests;

import java.util.stream.Stream;
import org.eclipse.core.tests.harness.session.CustomSessionConfiguration;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
Expand All @@ -38,12 +40,22 @@ public static BundleContext getContext() {
public static void addRequiredOSGiTestsBundles(ConfigurationSessionTestSuite suite) {
suite.addMinimalBundleSet();
suite.addThisBundle();
suite.addBundle(org.osgi.util.function.Function.class);
suite.addBundle(org.osgi.util.measurement.Measurement.class);
suite.addBundle(org.osgi.util.position.Position.class);
suite.addBundle(org.osgi.util.promise.Promise.class);
suite.addBundle(org.osgi.util.xml.XMLParserActivator.class);
suite.addBundle(org.osgi.service.event.Event.class);
getClassesFromRequiredOSGITestsBundles().forEach(suite::addBundle);
}

public static void addRequiredOSGiTestsBundles(CustomSessionConfiguration sessionConfiguration) {
getClassesFromRequiredOSGITestsBundles().forEach(sessionConfiguration::addBundle);
}

private static Stream<Class<?>> getClassesFromRequiredOSGITestsBundles() {
return Stream.of( //
org.osgi.util.function.Function.class, //
org.osgi.util.measurement.Measurement.class, //
org.osgi.util.position.Position.class, //
org.osgi.util.promise.Promise.class, //
org.osgi.util.xml.XMLParserActivator.class, //
org.osgi.service.event.Event.class //
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*******************************************************************************/
package org.eclipse.osgi.tests.configuration;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ //
@Suite
@SelectClasses({ //
EclipseStarterConfigIniTest.class, //
EclipseStarterConfigurationAreaTest.class, //
ReadOnlyConfigurationAreaTest.class, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,62 @@
import static org.eclipse.osgi.tests.OSGiTestsActivator.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTestsActivator.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.tests.harness.session.CustomSessionConfiguration;
import org.eclipse.core.tests.harness.session.ExecuteInHost;
import org.eclipse.core.tests.harness.session.SessionTestExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.osgi.framework.Constants;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;

public class EclipseStarterConfigurationAreaTest extends TestCase {
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class EclipseStarterConfigurationAreaTest {
private static final String WITH_COMPATIBILITY_FRAGMENT = "withCompatibilityFragment";

public static Test suite() {
TestSuite suite = new TestSuite(EclipseStarterConfigurationAreaTest.class.getName());
private CustomSessionConfiguration sessionConfiguration = SessionTestExtension.createCustomConfiguration();

ConfigurationSessionTestSuite initialization = new ConfigurationSessionTestSuite(PI_OSGI_TESTS,
EclipseStarterConfigurationAreaTest.class.getName());
addRequiredOSGiTestsBundles(initialization);
initialization.addBundle("org.eclipse.osgi.compatibility.state");
// disable clean-up, we want to reuse the configuration
initialization.setCleanup(false);
initialization.addTest(new EclipseStarterConfigurationAreaTest("testInitializeExtension"));
suite.addTest(initialization);
@RegisterExtension
SessionTestExtension extension = SessionTestExtension.forPlugin(PI_OSGI_TESTS)
.withCustomization(sessionConfiguration).create();

// restart with cache but remove the compatibility fragment
IPath configPath = initialization.getConfigurationPath();

ConfigurationSessionTestSuite removeExtension = new ConfigurationSessionTestSuite(PI_OSGI_TESTS,
EclipseStarterConfigurationAreaTest.class.getName());
removeExtension.setConfigurationPath(configPath);
addRequiredOSGiTestsBundles(removeExtension);
removeExtension.addTest(new EclipseStarterConfigurationAreaTest("testRemoveExtension"));
suite.addTest(removeExtension);
return suite;
}

public EclipseStarterConfigurationAreaTest(String name) {
super(name);
@BeforeEach
@ExecuteInHost
public void setUpSession(TestInfo testInfo) {
addRequiredOSGiTestsBundles(sessionConfiguration);
if (testInfo.getTags().contains(WITH_COMPATIBILITY_FRAGMENT)) {
sessionConfiguration.addBundle(Platform.getBundle("org.eclipse.osgi.compatibility.state"));
}
}

@Test
@Tag(WITH_COMPATIBILITY_FRAGMENT)
@Order(1)
public void testInitializeExtension() {
// initialization session
List<BundleWire> fragWires = getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(BundleWiring.class)
.getProvidedWires(HostNamespace.HOST_NAMESPACE);
assertEquals("Wrong number of system fragments.", 1, fragWires.size());
assertEquals(1, fragWires.size(), "Wrong number of system fragments.");
}

@Test
@Order(2)
public void testRemoveExtension() {
// removed extension session
List<BundleWire> fragWires = getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(BundleWiring.class)
.getProvidedWires(HostNamespace.HOST_NAMESPACE);
assertEquals("Wrong number of system fragments.", 0, fragWires.size());
assertEquals(0, fragWires.size(), "Wrong number of system fragments.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,123 +16,105 @@
import static org.eclipse.osgi.tests.OSGiTestsActivator.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTestsActivator.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.runtime.IPath;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.core.tests.harness.BundleTestingHelper;
import org.eclipse.core.tests.harness.FileSystemComparator;
import org.eclipse.core.tests.harness.FileSystemHelper;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.core.tests.harness.session.CustomSessionConfiguration;
import org.eclipse.core.tests.harness.session.ExecuteInHost;
import org.eclipse.core.tests.harness.session.SessionTestExtension;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;

public class MovableConfigurationAreaTest extends TestCase {
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MovableConfigurationAreaTest {

static void doMove(final IPath sourcePath, final IPath destinationPath) {
assertTrue("Failed moving " + sourcePath + " to " + destinationPath,
sourcePath.toFile().renameTo(destinationPath.toFile()));
@TempDir
static Path destinationConfigurationDir;

private static CustomSessionConfiguration sessionConfiguration = createSessionConfiguration();

@RegisterExtension
static SessionTestExtension extension = SessionTestExtension.forPlugin(PI_OSGI_TESTS)
.withCustomization(sessionConfiguration).create();

private static CustomSessionConfiguration createSessionConfiguration() {
CustomSessionConfiguration configuration = SessionTestExtension.createCustomConfiguration().setReadOnly();
addRequiredOSGiTestsBundles(configuration);
return configuration;
}

static void doTakeSnapshot(final IPath destinationPath) {
private static void takeSnapshot(final Path path) throws IOException {
// compute and save tree image
File configurationDir = destinationPath.toFile();
FileSystemComparator comparator = new FileSystemComparator();
Object snapshot = comparator.takeSnapshot(configurationDir, true);
try {
comparator.saveSnapshot(snapshot, configurationDir);
} catch (IOException e) {
fail("1.0");
}
Object snapshot = comparator.takeSnapshot(path.toFile(), true);
comparator.saveSnapshot(snapshot, path.toFile());
}

public static Test suite() {
TestSuite suite = new TestSuite(MovableConfigurationAreaTest.class.getName());

ConfigurationSessionTestSuite initialization = new ConfigurationSessionTestSuite(PI_OSGI_TESTS,
MovableConfigurationAreaTest.class.getName());
addRequiredOSGiTestsBundles(initialization);
initialization.setReadOnly(true);
// disable clean-up, we want to reuse the configuration
initialization.setCleanup(false);
initialization.addTest(new MovableConfigurationAreaTest("testInitialization"));
suite.addTest(initialization);

// add a helper test that just moves the configuration area
final IPath sourcePath = initialization.getConfigurationPath();
final IPath destinationPath = FileSystemHelper.getRandomLocation(FileSystemHelper.getTempDir());
suite.addTest(new TestCase("testMove") {
public void runBare() throws Throwable {
doMove(sourcePath, destinationPath);
}
});
suite.addTest(new TestCase("testTakeSnapshot") {
public void runBare() throws Throwable {
doTakeSnapshot(destinationPath);
}
});

ConfigurationSessionTestSuite afterMoving = new ConfigurationSessionTestSuite(PI_OSGI_TESTS,
MovableConfigurationAreaTest.class.getName());
afterMoving.setConfigurationPath(destinationPath);
afterMoving.addMinimalBundleSet();
afterMoving.setReadOnly(true);
// make sure we don't allow priming for the first run
afterMoving.setPrime(false);
afterMoving.addTest(new MovableConfigurationAreaTest("testAfterMoving"));
afterMoving.addTest(new MovableConfigurationAreaTest("testVerifySnapshot"));
suite.addTest(afterMoving);
return suite;
@Test
@Order(1)
public void testInitialization() throws Exception {
// initialization session
Bundle installed = BundleTestingHelper.installBundle("1.0", getContext(),
OSGiTestsActivator.TEST_FILES_ROOT + "configuration/bundle01");
// not read-only yet, should work fine
assertTrue(BundleTestingHelper.resolveBundles(getContext(), new Bundle[] { installed }),
"installed bundle could not be resolved: " + installed);
}

public MovableConfigurationAreaTest(String name) {
super(name);
@Test
@Order(2)
@ExecuteInHost
public void moveConfigurationDirectory() throws IOException {
Files.deleteIfExists(destinationConfigurationDir);
Files.move(sessionConfiguration.getConfigurationDirectory(), destinationConfigurationDir);
takeSnapshot(destinationConfigurationDir);
sessionConfiguration.setConfigurationDirectory(destinationConfigurationDir);
}

/**
* Tries to install a plug-in that has no manifest. Should fail because by
* default the manifest generation area is under the configuration area (which
* is read-only here)
*/
@SuppressWarnings("deprecation") // installBundle
public void testAfterMoving() throws MalformedURLException, IOException, BundleException {
// try to install plug-in
// ensure it is not installed
Bundle installed = null;
try {
installed = BundleTestingHelper.installBundle(getContext(),
@Test
@Order(3)
@SuppressWarnings("deprecation")
public void testAfterMoving() throws BundleException {
// try to install plug-in and ensure it is not installed
AtomicReference<Bundle> installed = new AtomicReference<>();
assertThrows(BundleException.class, () -> {
Bundle bundle = BundleTestingHelper.installBundle(getContext(),
OSGiTestsActivator.TEST_FILES_ROOT + "configuration/bundle02");
// should have failed with BundleException, does not have a bundle manifest
fail("1.0");
} catch (BundleException be) {
// success
} finally {
if (installed != null)
// clean-up - only runs if we end-up accepting an invalid manifest
installed.uninstall();
installed.set(bundle);
});
if (installed.get() != null) {
// clean-up - only runs if we end-up accepting an invalid manifest
installed.get().uninstall();
}
}

public void testInitialization() throws Exception {
// initialization session
Bundle installed = BundleTestingHelper.installBundle("1.0", getContext(),
OSGiTestsActivator.TEST_FILES_ROOT + "configuration/bundle01");
// not read-only yet, should work fine
assertTrue("installed bundle could not be resolved: " + installed,
BundleTestingHelper.resolveBundles(getContext(), new Bundle[] { installed }));
}

@Test
@Order(4)
@ExecuteInHost
public void testVerifySnapshot() throws IOException {
FileSystemComparator comparator = new FileSystemComparator();
File configurationDir = ConfigurationSessionTestSuite.getConfigurationDir();
Object oldSnaphot = comparator.loadSnapshot(configurationDir);
Object newSnapshot = comparator.takeSnapshot(configurationDir, true);
comparator.compareSnapshots("1.0", oldSnaphot, newSnapshot);
Object oldSnaphot = comparator.loadSnapshot(destinationConfigurationDir.toFile());
takeSnapshot(destinationConfigurationDir);
Object newSnapshot = comparator.loadSnapshot(destinationConfigurationDir.toFile());
comparator.compareSnapshots("", oldSnaphot, newSnapshot);
}

}
Loading
Loading