Skip to content

Commit

Permalink
Make session tests in o.e.osgi.tests independent from OSGiTest/CoreTest
Browse files Browse the repository at this point in the history
Several session test classes in org.eclipse.osgi.tests are subclasses of
either CoreTest or the OSGiTest subclass of CoreTest. This change make
the test classes directly inherit from JUnit 3's TestCase to become
independent of the CoreTest class. The TestCase inheritance is necessary
until session tests do not rely on JUnit 3 anymore.

* Remove inheritance of CoreTest and replace OSGiTest inheritance with
static imports of inherited helper methods
* Replace try-catch for actual errors with making the test method throw
the exception
  • Loading branch information
HeikoKlare authored and akurtakov committed Jan 10, 2024
1 parent 92fc0a5 commit 7290a93
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 663 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
*******************************************************************************/
package org.eclipse.osgi.tests.appadmin;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.core.tests.session.SetupManager.SetupException;
import org.eclipse.osgi.tests.OSGiTest;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.osgi.framework.Bundle;
Expand All @@ -37,7 +42,7 @@
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class ApplicationAdminTest extends OSGiTest {
public class ApplicationAdminTest extends TestCase {
public static final String testRunnerApp = "org.eclipse.pde.junit.runtime.coretestapplicationnonmain"; //$NON-NLS-1$
public static final String testResults = "test.results"; //$NON-NLS-1$
public static final String SUCCESS = "success"; //$NON-NLS-1$
Expand Down Expand Up @@ -108,6 +113,12 @@ private HashMap getArguments() {
return args;
}

private void fail(String message, Throwable throwable) {
AssertionFailedError error = new AssertionFailedError(message);
error.initCause(throwable);
throw error;
}

public void testSimpleApp() {
ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp"); //$NON-NLS-1$
HashMap args = getArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@
*******************************************************************************/
package org.eclipse.osgi.tests.appadmin;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;

import java.util.HashMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.core.tests.session.SetupManager.SetupException;
import org.eclipse.osgi.tests.OSGiTest;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.application.ApplicationDescriptor;
import org.osgi.service.application.ApplicationHandle;

// This is for the most part a stripped down copy of ApplicationAdminTest.
public class ApplicationRelaunchTest extends OSGiTest {
public class ApplicationRelaunchTest extends TestCase {
public static final String testRunnerRelauncherApp = PI_OSGI_TESTS + ".relaunchApp"; //$NON-NLS-1$
public static final String testResults = "test.results"; //$NON-NLS-1$
public static final String SUCCESS = "success"; //$NON-NLS-1$
Expand Down Expand Up @@ -57,35 +61,31 @@ public ApplicationRelaunchTest(String name) {
super(name);
}

private ApplicationDescriptor getApplication(String appName) {
try {
BundleContext context = getContext();
assertNotNull("BundleContext is null!!", context); //$NON-NLS-1$
Class appDescClass = ApplicationDescriptor.class;
assertNotNull("ApplicationDescriptor.class is null!!", appDescClass); //$NON-NLS-1$
ServiceReference[] refs = context.getServiceReferences(appDescClass.getName(), "(" + ApplicationDescriptor.APPLICATION_PID + "=" + appName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (refs == null || refs.length == 0) {
refs = getContext().getServiceReferences(ApplicationDescriptor.class.getName(), null);
String availableApps = ""; //$NON-NLS-1$
if (refs != null) {
for (int i = 0; i < refs.length; i++) {
availableApps += refs[i].getProperty(ApplicationDescriptor.APPLICATION_PID);
if (i < refs.length - 1)
availableApps += ","; //$NON-NLS-1$
}
private ApplicationDescriptor getApplication(String appName) throws InvalidSyntaxException {
BundleContext context = getContext();
assertNotNull("BundleContext is null!!", context); //$NON-NLS-1$
Class appDescClass = ApplicationDescriptor.class;
assertNotNull("ApplicationDescriptor.class is null!!", appDescClass); //$NON-NLS-1$
ServiceReference[] refs = context.getServiceReferences(appDescClass.getName(),
"(" + ApplicationDescriptor.APPLICATION_PID + "=" + appName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (refs == null || refs.length == 0) {
refs = getContext().getServiceReferences(ApplicationDescriptor.class.getName(), null);
String availableApps = ""; //$NON-NLS-1$
if (refs != null) {
for (int i = 0; i < refs.length; i++) {
availableApps += refs[i].getProperty(ApplicationDescriptor.APPLICATION_PID);
if (i < refs.length - 1)
availableApps += ","; //$NON-NLS-1$
}
fail("Could not find app pid: " + appName + " available apps are: " + availableApps); //$NON-NLS-1$ //$NON-NLS-2$
}
ApplicationDescriptor result = (ApplicationDescriptor) getContext().getService(refs[0]);
if (result != null)
getContext().ungetService(refs[0]);
else
fail("Could not get application descriptor service: " + appName); //$NON-NLS-1$
return result;
} catch (InvalidSyntaxException e) {
fail("Could not create app filter", e); //$NON-NLS-1$
fail("Could not find app pid: " + appName + " available apps are: " + availableApps); //$NON-NLS-1$ //$NON-NLS-2$
}
return null;
ApplicationDescriptor result = (ApplicationDescriptor) getContext().getService(refs[0]);
if (result != null)
getContext().ungetService(refs[0]);
else
fail("Could not get application descriptor service: " + appName); //$NON-NLS-1$
return result;
}

private HashMap getArguments() {
Expand All @@ -94,19 +94,15 @@ private HashMap getArguments() {
return args;
}

public void testRelaunch() {
public void testRelaunch() throws Exception {
// this is the same as ApplicationAdminTest.testSimpleApp() (but launched
// through a different test runner app RelaunchApp which is the thing being
// tested)
ApplicationDescriptor app = getApplication(PI_OSGI_TESTS + ".simpleApp"); //$NON-NLS-1$
HashMap args = getArguments();
HashMap results = (HashMap) args.get(testResults);
try {
ApplicationHandle handle = app.launch(args);
handle.destroy();
} catch (Throwable e) {
fail("failed to launch simpleApp", e); //$NON-NLS-1$
}
ApplicationHandle handle = app.launch(args);
handle.destroy();
String result = (String) results.get(simpleResults);
assertEquals("Check application result", SUCCESS, result); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@
*******************************************************************************/
package org.eclipse.osgi.tests.configuration;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.junit.Assert.assertThrows;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.osgi.tests.OSGiTest;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

public class EclipseStarterConfigIniTest extends OSGiTest {
public class EclipseStarterConfigIniTest extends TestCase {

public static Test suite() {
TestSuite suite = new TestSuite(EclipseStarterConfigIniTest.class.getName());
Expand Down Expand Up @@ -65,14 +70,17 @@ public void doTestCompatBootDelegation(boolean expectFailure) throws Exception {
Bundle b = context.installBundle(getName(), new ByteArrayInputStream(bytesOut.toByteArray()));
String testClassName = javax.net.SocketFactory.class.getName();
// The bundle does not import anything so should not find javax stuff
try {
b.loadClass(testClassName);
if (expectFailure) {
fail("Expected to fail to load VM class from bundle that does not import it");
}
} catch (ClassNotFoundException e) {
if (!expectFailure) {
fail("Expected to successfully load VM class from bundle that does not import it", e);
if (expectFailure) {
assertThrows("Expected to fail to load VM class from bundle that does not import it",
ClassNotFoundException.class, () -> b.loadClass(testClassName));
} else {
try {
b.loadClass(testClassName);
} catch (ClassNotFoundException e) {
AssertionFailedError error = new AssertionFailedError(
"Expected to successfully load VM class from bundle that does not import it");
error.initCause(e);
throw error;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
*******************************************************************************/
package org.eclipse.osgi.tests.configuration;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;

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.osgi.tests.OSGiTest;
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 OSGiTest {
public class EclipseStarterConfigurationAreaTest extends TestCase {

public static Test suite() {
TestSuite suite = new TestSuite(EclipseStarterConfigurationAreaTest.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*******************************************************************************/
package org.eclipse.osgi.tests.configuration;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -24,12 +28,11 @@
import org.eclipse.core.tests.harness.FileSystemComparator;
import org.eclipse.core.tests.harness.FileSystemHelper;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.osgi.tests.OSGiTest;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;

public class MovableConfigurationAreaTest extends OSGiTest {
public class MovableConfigurationAreaTest extends TestCase {

static void doMove(final IPath sourcePath, final IPath destinationPath) {
assertTrue("Failed moving " + sourcePath + " to " + destinationPath, sourcePath.toFile().renameTo(destinationPath.toFile()));
Expand Down Expand Up @@ -109,16 +112,13 @@ public void testAfterMoving() throws MalformedURLException, IOException, BundleE
}
}

public void testInitialization() throws MalformedURLException, IOException {
public void testInitialization() throws Exception {
// initialization session
try {
Bundle installed = BundleTestingHelper.installBundle("1.0", getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "configuration/bundle01");
// not read-only yet, should work fine
if (!BundleTestingHelper.resolveBundles(getContext(), new Bundle[] {installed}))
fail("1.1");
} catch (BundleException be) {
fail("1.2", be);
}
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 }));
}

public void testVerifySnapshot() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
*******************************************************************************/
package org.eclipse.osgi.tests.configuration;

import static org.eclipse.osgi.tests.OSGiTest.PI_OSGI_TESTS;
import static org.eclipse.osgi.tests.OSGiTest.addRequiredOSGiTestsBundles;
import static org.eclipse.osgi.tests.OSGiTestsActivator.getContext;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.tests.harness.BundleTestingHelper;
import org.eclipse.core.tests.harness.FileSystemComparator;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.osgi.tests.OSGiTest;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;

public class ReadOnlyConfigurationAreaTest extends OSGiTest {
public class ReadOnlyConfigurationAreaTest extends TestCase {

public static Test suite() {
ConfigurationSessionTestSuite suite = new ConfigurationSessionTestSuite(PI_OSGI_TESTS, ReadOnlyConfigurationAreaTest.class);
Expand All @@ -38,31 +41,26 @@ public ReadOnlyConfigurationAreaTest(String name) {
super(name);
}

public void test0thSession() throws MalformedURLException, IOException {
public void test0thSession() throws Exception {
// initialization session
try {
Bundle installed = BundleTestingHelper.installBundle("1.0", getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "configuration/bundle01");
// not read-only yet, should work fine
if (!BundleTestingHelper.resolveBundles(getContext(), new Bundle[] {installed}))
fail("1.1");
} catch (BundleException be) {
fail("1.2", be);
}
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 }));
}

/**
* Takes a snapshot of the file system.
*
* @throws IOException
*/
public void test1stSession() {
public void test1stSession() throws IOException {
// compute and save tree image
File configurationDir = ConfigurationSessionTestSuite.getConfigurationDir();
FileSystemComparator comparator = new FileSystemComparator();
Object snapshot = comparator.takeSnapshot(configurationDir, true);
try {
comparator.saveSnapshot(snapshot, configurationDir);
} catch (IOException e) {
fail("1.0");
}
comparator.saveSnapshot(snapshot, configurationDir);
}

public void test1stSessionFollowUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Hashtable;
import junit.framework.TestCase;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.core.tests.session.ConfigurationSessionTestSuite;
import org.eclipse.osgi.internal.provisional.service.security.AuthorizationEngine;
import org.eclipse.osgi.internal.service.security.KeyStoreTrustEngine;
import org.eclipse.osgi.service.security.TrustEngine;
import org.eclipse.osgi.signedcontent.SignedContentFactory;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

public class BaseSecurityTest extends CoreTest {
public class BaseSecurityTest extends TestCase {

private static char[] PASSWORD_DEFAULT = {'c', 'h', 'a', 'n', 'g', 'e', 'i', 't'};
private static String TYPE_DEFAULT = "JKS";
Expand Down Expand Up @@ -133,15 +134,10 @@ protected AuthorizationEngine getAuthorizationEngine() {
return engine;
}

protected Bundle installBundle(String bundlePath) {
protected Bundle installBundle(String bundlePath) throws BundleException, IOException {
URL bundleURL = OSGiTestsActivator.getBundle().getEntry(bundlePath);
assertNotNull("Bundle URL is null " + bundlePath, bundleURL);
try {
return OSGiTestsActivator.getContext().installBundle(bundlePath, bundleURL.openStream());
} catch (Exception e) {
fail("unexpected install exception", e);
}
return null;
return OSGiTestsActivator.getContext().installBundle(bundlePath, bundleURL.openStream());
}

protected static File getEntryFile(String entryPath) throws IOException {
Expand Down
Loading

0 comments on commit 7290a93

Please sign in to comment.