Skip to content

Commit

Permalink
Migrate debugoptions, hooks and resources in o.e.osgi.tests to JUnit 4
Browse files Browse the repository at this point in the history
This migrates all tests in org.eclipse.osgi.tests.debugoptions,
org.eclipse.osgi.tests.hooks.framework and
org.eclipse.osgi.tests.resource to JUnit 4

* Add JUnit 4 annotations @test, @before, @after
* Remove inheritance of JUnit 3 TestCase and CoreTest
* Replace try-catch for actual errors with making the test method throw
the exception
  • Loading branch information
HeikoKlare committed Dec 21, 2023
1 parent 35d0284 commit 954b2e1
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 115 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stop;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -24,17 +26,24 @@
import java.util.Enumeration;
import java.util.Map;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.launch.EquinoxFactory;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.osgi.framework.BundleContext;
import org.osgi.framework.connect.FrameworkUtilHelper;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public abstract class AbstractFrameworkHookTests extends CoreTest {
public abstract class AbstractFrameworkHookTests {

@Rule
public TestName testName = new TestName();

protected static class BasicURLClassLoader extends URLClassLoader {
private volatile String testURL;

Expand Down Expand Up @@ -124,12 +133,14 @@ protected Framework restart(Framework framework, Map<String, String> configurati
return framework;
}

protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
setUpBundleInstaller();
setUpClassLoader();
}

protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
bundleInstaller.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stop;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.net.URL;
Expand All @@ -23,6 +24,7 @@
import java.util.List;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Test;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;

Expand All @@ -35,19 +37,20 @@ public class ActivatorOrderTest extends AbstractFrameworkHookTests {
private static final String HOOK_CONFIGURATOR_CLASS3 = "org.eclipse.osgi.tests.hooks.framework.activator.a.TestHookConfigurator3";

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
String loc = bundleInstaller.getBundleLocation(HOOK_CONFIGURATOR_BUNDLE);
loc = loc.substring(loc.indexOf("file:"));
classLoader.addURL(new URL(loc));
File file = OSGiTestsActivator.getContext().getDataFile(getName());
File file = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
HashMap<String, String> configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, file.getAbsolutePath());
configuration.put(HookRegistry.PROP_HOOK_CONFIGURATORS, HOOK_CONFIGURATOR_CLASS1 + "," + HOOK_CONFIGURATOR_CLASS2 + "," + HOOK_CONFIGURATOR_CLASS3);

framework = createFramework(configuration);
}

@Test
public void testActivatorOrder() throws Exception {
List<String> actualEvents = new ArrayList<>();
Class<?> clazz1 = classLoader.loadClass(HOOK_CONFIGURATOR_CLASS1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stopQuietly;
import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -24,6 +25,7 @@
import java.util.Map;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
Expand All @@ -38,21 +40,21 @@ public class BundleFileWrapperFactoryHookTests extends AbstractFrameworkHookTest
private String location;

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
String loc = bundleInstaller.getBundleLocation(HOOK_CONFIGURATOR_BUNDLE);
loc = loc.substring(loc.indexOf("file:"));
classLoader.addURL(new URL(loc));
location = bundleInstaller.getBundleLocation(TEST_BUNDLE);
File file = OSGiTestsActivator.getContext().getDataFile(getName());
File file = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, file.getAbsolutePath());
configuration.put(HookRegistry.PROP_HOOK_CONFIGURATORS_INCLUDE, HOOK_CONFIGURATOR_CLASS);
framework = createFramework(configuration);
}

@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
stopQuietly(framework);
super.tearDown();
}
Expand All @@ -65,6 +67,7 @@ private Bundle installBundle() throws Exception {
return framework.getBundleContext().installBundle(location);
}

@Test
public void testGetResourceURL() throws Exception {
initAndStartFramework();

Expand All @@ -74,19 +77,15 @@ public void testGetResourceURL() throws Exception {
assertEquals("Wrong content found.", "CUSTOM_CONTENT", readURL(url1));
}

private String readURL(URL url) {
private String readURL(URL url) throws IOException {
StringBuilder sb = new StringBuilder();
try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
for (String line = reader.readLine(); line != null;) {
sb.append(line);
line = reader.readLine();
if (line != null)
sb.append('\n');
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
for (String line = reader.readLine(); line != null;) {
sb.append(line);
line = reader.readLine();
if (line != null)
sb.append('\n');
}
} catch (IOException e) {
fail("Unexpected exception reading url: " + url.toExternalForm(), e); //$NON-NLS-1$
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stopQuietly;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URL;
Expand All @@ -25,6 +29,7 @@
import java.util.concurrent.TimeUnit;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkEvent;
Expand Down Expand Up @@ -53,7 +58,7 @@ public class ClassLoaderHookTests extends AbstractFrameworkHookTests {
private String location;

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
setRejectTransformation(false);
setBadTransform(false);
Expand All @@ -66,15 +71,15 @@ protected void setUp() throws Exception {
loc = loc.substring(loc.indexOf("file:"));
classLoader.addURL(new URL(loc));
location = bundleInstaller.getBundleLocation(TEST_BUNDLE);
File file = OSGiTestsActivator.getContext().getDataFile(getName());
File file = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, file.getAbsolutePath());
configuration.put(HookRegistry.PROP_HOOK_CONFIGURATORS_INCLUDE, HOOK_CONFIGURATOR_CLASS);
framework = createFramework(configuration);
}

@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
stopQuietly(framework);
super.tearDown();
}
Expand Down Expand Up @@ -115,6 +120,7 @@ private void setPreventResourceLoadPost(boolean value) {
System.setProperty(PREVENT_RESOURCE_LOAD_POST, Boolean.toString(value));
}

@Test
public void testRejectTransformationFromWeavingHook() throws Exception {
setRejectTransformation(true);
initAndStartFramework();
Expand Down Expand Up @@ -142,6 +148,7 @@ public void testRejectTransformationFromWeavingHook() throws Exception {
assertEquals("Found some imports.", 1, b.adapt(BundleRevision.class).getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE).size());
}

@Test
public void testRejectTransformationFromClassLoadingHook() throws Exception {
setRejectTransformation(true);
setBadTransform(true);
Expand All @@ -160,13 +167,15 @@ public void testRejectTransformationFromClassLoadingHook() throws Exception {
}
}

@Test
public void testRecursionFromClassLoadingHookNotSupported() throws Exception {
setRecursionLoad(true);
initAndStartFramework();
Bundle b = installBundle();
b.loadClass(TEST_CLASSNAME);
}

@Test
public void testRecursionFromClassLoadingHookIsSupported() throws Exception {
setRecursionLoad(true);
setRecursionLoadSupported(true);
Expand All @@ -185,6 +194,7 @@ private void refreshBundles(Collection<Bundle> bundles) throws InterruptedExcept
refreshSignal.await(30, TimeUnit.SECONDS);
}

@Test
public void testFilterClassPaths() throws Exception {
setFilterClassPaths(false);
initAndStartFramework();
Expand All @@ -201,6 +211,7 @@ public void testFilterClassPaths() throws Exception {
}
}

@Test
public void testPreventResourceLoadFromClassLoadingHook() throws Exception {
setPreventResourceLoadPre(false);
setPreventResourceLoadPost(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stopQuietly;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
Expand All @@ -22,6 +24,7 @@
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
Expand All @@ -33,21 +36,22 @@ public class ContextFinderTests extends AbstractFrameworkHookTests {
private Framework framework;

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
File file = OSGiTestsActivator.getContext().getDataFile(getName());
File file = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, file.getAbsolutePath());
framework = createFramework(configuration);
initAndStart(framework);
}

@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
stopQuietly(framework);
super.tearDown();
}

@Test
public void testContextClassLoaderNullLocal() throws InvalidSyntaxException, IOException {
BundleContext bc = framework.getBundleContext();
ClassLoader contextFinder = bc.getService(bc.getServiceReferences(ClassLoader.class, "(equinox.classloader.type=contextClassLoader)").iterator().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.osgi.tests.hooks.framework;

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stopQuietly;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.net.URL;
Expand All @@ -23,6 +25,7 @@
import org.eclipse.osgi.internal.framework.EquinoxConfiguration;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.SystemBundleTests;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
Expand All @@ -33,9 +36,9 @@ public class DevClassPathDuplicateTests extends AbstractFrameworkHookTests {
private String location;

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
File store = OSGiTestsActivator.getContext().getDataFile(getName());
File store = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, store.getAbsolutePath());
configuration.put(EquinoxConfiguration.PROP_DEV, "duplicate/");
Expand All @@ -53,7 +56,7 @@ protected void setUp() throws Exception {
}

@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
stopQuietly(framework);
super.tearDown();
}
Expand All @@ -66,6 +69,7 @@ private Bundle installBundle() throws Exception {
return framework.getBundleContext().installBundle(location);
}

@Test
public void testDevClassPathWithExtension() throws Exception {
initAndStartFramework();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stop;
import static org.eclipse.osgi.tests.bundles.AbstractBundleTests.stopQuietly;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Collection;
Expand All @@ -23,6 +24,7 @@
import java.util.Map;
import org.eclipse.osgi.internal.framework.EquinoxConfiguration;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
Expand All @@ -36,18 +38,18 @@ public class DevClassPathWithExtensionTests extends AbstractFrameworkHookTests {
private String location;

@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
location = bundleInstaller.getBundleLocation(TEST_BUNDLE);
File file = OSGiTestsActivator.getContext().getDataFile(getName());
File file = OSGiTestsActivator.getContext().getDataFile(testName.getMethodName());
configuration = new HashMap<>();
configuration.put(Constants.FRAMEWORK_STORAGE, file.getAbsolutePath());
configuration.put(EquinoxConfiguration.PROP_DEV, "bin/");
framework = createFramework(configuration);
}

@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
stopQuietly(framework);
super.tearDown();
}
Expand All @@ -60,6 +62,7 @@ private Bundle installBundle() throws Exception {
return framework.getBundleContext().installBundle(location);
}

@Test
public void testDevClassPathWithExtension() throws Exception {
initAndStartFramework();

Expand Down
Loading

0 comments on commit 954b2e1

Please sign in to comment.