diff options
author | Pascal Rapicault | 2008-02-08 21:19:12 +0000 |
---|---|---|
committer | Pascal Rapicault | 2008-02-08 21:19:12 +0000 |
commit | 7829bf04ba84abb878e308d4ee67a366f394ee33 (patch) | |
tree | f8db4de6655845e22ec5ec37ab9dc5230a63cc4e /bundles/org.eclipse.equinox.p2.tests | |
parent | 1530132495aacd97b93dcd2b080259b8b6e14e05 (diff) | |
download | rt.equinox.p2-7829bf04ba84abb878e308d4ee67a366f394ee33.tar.gz rt.equinox.p2-7829bf04ba84abb878e308d4ee67a366f394ee33.tar.xz rt.equinox.p2-7829bf04ba84abb878e308d4ee67a366f394ee33.zip |
bug 212774 - [prov] tests for simpleconfigurator
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.tests')
8 files changed, 338 insertions, 0 deletions
diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AutomatedTests.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AutomatedTests.java index 8a6cd7c0f..ecb416765 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AutomatedTests.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AutomatedTests.java @@ -32,6 +32,7 @@ public class AutomatedTests extends TestCase { suite.addTest(org.eclipse.equinox.p2.tests.metadata.repository.AllTests.suite()); suite.addTest(org.eclipse.equinox.p2.tests.sar.AllTests.suite()); suite.addTest(org.eclipse.equinox.p2.tests.directorywatcher.AllTests.suite()); + suite.addTest(org.eclipse.equinox.p2.tests.simpleconfigurator.SimpleConfiguratorTests.suite()); return suite; } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestData.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestData.java index bca2cdd25..98ced0bf8 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestData.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestData.java @@ -7,6 +7,7 @@ * * Contributors: * compeople AG (Stefan Liebig) - initial API and implementation + * Red Hat, Inc. (Andrew Overholt) - add getFile method *******************************************************************************/ package org.eclipse.equinox.p2.tests; @@ -15,6 +16,7 @@ import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import junit.framework.Assert; +import org.eclipse.core.runtime.FileLocator; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; /** @@ -38,6 +40,18 @@ public class TestData { } /** + * Get a File from the resource testDataName within the folder + * testDataFolder of the testData folder of this project. + * @param testDataFolder + * @param testDataFilename + * @return test data File + * @throws IOException + */ + public static File getFile(String testDataFolder, String testDataName) throws IOException { + return new File(FileLocator.toFileURL(TestActivator.getContext().getBundle().getEntry(TEST_DATA_ROOT_FOLDER + "/" + testDataFolder + "/" + testDataName)).getPath()); + } + + /** * Create a temporary file for the test data. The temporary file will be deleted * when the jvm exists. If testDataName contains an extension this extension will * be used as suffix for the temporary file. diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/embeddedequinox/EmbeddedEquinox.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/embeddedequinox/EmbeddedEquinox.java new file mode 100644 index 000000000..fe74ce627 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/embeddedequinox/EmbeddedEquinox.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.embeddedequinox; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.Map; +import org.osgi.framework.BundleContext; + +/* + * This class assumes the bundle's classloader only has access to org.osgi.framework + * from the hosting framework. Any more access could result in ClassVerifier errors. + */ +public class EmbeddedEquinox { + private final Map frameworkProperties; + private final String[] frameworkArgs; + private Class eclipseStarterClazz; + private BundleContext context; + private URL[] frameworkClassPath; + + public EmbeddedEquinox(Map frameworkProperties, String[] frameworkArgs, URL[] frameworkClassPath) { + this.frameworkProperties = frameworkProperties; + this.frameworkArgs = frameworkArgs; + this.frameworkClassPath = frameworkClassPath; + } + + public BundleContext startFramework() { + System.setProperty("osgi.framework.useSystemProperties", "false"); + try { + URLClassLoader frameworkLoader = new FrameworkClassLoader(frameworkClassPath, this.getClass().getClassLoader()); + eclipseStarterClazz = frameworkLoader.loadClass("org.eclipse.core.runtime.adaptor.EclipseStarter"); + + Method setInitialProperties = eclipseStarterClazz.getMethod("setInitialProperties", new Class[] {Map.class}); //$NON-NLS-1$ + setInitialProperties.invoke(null, new Object[] {frameworkProperties}); + + Method runMethod = eclipseStarterClazz.getMethod("startup", new Class[] {String[].class, Runnable.class}); //$NON-NLS-1$ + context = (BundleContext) runMethod.invoke(null, new Object[] {frameworkArgs, null}); + } catch (Throwable t) { + if (t instanceof RuntimeException) + throw (RuntimeException) t; + throw new RuntimeException(t); + } + return context; + } + + public void shutdown() { + try { + Method shutdownMethod = eclipseStarterClazz.getMethod("shutdown", null); //$NON-NLS-1$ + shutdownMethod.invoke(null, null); + + } catch (Throwable t) { + if (t instanceof RuntimeException) + throw (RuntimeException) t; + throw new RuntimeException(t); + } + } + + public class FrameworkClassLoader extends URLClassLoader { + ClassLoader embeddedBundleLoader; + + public FrameworkClassLoader(URL[] urls, ClassLoader parent) { + super(urls, null); + this.embeddedBundleLoader = parent; + } + + protected Class findClass(String name) throws ClassNotFoundException { + if (name.startsWith("org.osgi.framework.")) + // TODO this should call findClass; but then have to use reflection!! + return embeddedBundleLoader.loadClass(name); + return super.findClass(name); + } + + public URL findResource(String name) { + if (name.startsWith("org/osgi/framework/")) + // TODO this should call findResource; but then have to use reflection!! + return embeddedBundleLoader.getResource(name); + return super.findResource(name); + } + + public Enumeration findResources(String name) throws IOException { + if (name.startsWith("org/osgi/framework/")) + // TODO this should call findResources; but then have to use reflection!! + return embeddedBundleLoader.getResources(name); + return super.findResources(name); + } + + } +} diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/BundlesTxtTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/BundlesTxtTest.java new file mode 100644 index 000000000..20035167c --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/BundlesTxtTest.java @@ -0,0 +1,200 @@ +/******************************************************************************* + * Copyright (c) 2007, 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Red Hat, Inc. - tests + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.simpleconfigurator; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.jar.JarFile; +import org.eclipse.equinox.p2.tests.*; +import org.eclipse.equinox.p2.tests.embeddedequinox.EmbeddedEquinox; +import org.osgi.framework.*; + +public class BundlesTxtTest extends AbstractProvisioningTest { + + static String BUNDLE_JAR_DIRECTORY = "bundlesTxt"; + int numBundles = -1; + EmbeddedEquinox equinox = null; + BundleContext equinoxContext = null; + File[] bundleJars = null; + File bundlesTxt = null; + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + equinox.shutdown(); + } + + public void testBundlesTxt() throws Exception { + File simpleconfiguratorDataDir = TestData.getFile(BUNDLE_JAR_DIRECTORY, ""); + bundleJars = getBundleJars(simpleconfiguratorDataDir); + + numBundles = bundleJars.length; + + // Create a test bundles.txt + createBundlesTxt(); + + // for demonstration purposes this is just using an install.area and configuration.area located + // in the local bundle data area. For p2 testing you will want to point this to your own properties + File installarea = TestActivator.context.getDataFile("eclipse"); + File configarea = new File(installarea, "configuration"); + + File simpleConfiguratorBundle = getSimpleConfiguratorBundle(); + URL osgiBundle = getFrameworkBundle(); + + Map frameworkProperties = new HashMap(); + // note that any properties you do not want to be inherited from the hosting Equinox will need + // to be nulled out. Otherwise you will pick them up from the hosting env. + frameworkProperties.put("osgi.framework", null); + frameworkProperties.put("osgi.install.area", installarea.toURL().toExternalForm()); + frameworkProperties.put("osgi.configuration.area", configarea.toURL().toExternalForm()); + frameworkProperties.put("osgi.bundles", "reference:" + simpleConfiguratorBundle.toURL().toExternalForm() + "@1:start"); // should point to simple configurator + frameworkProperties.put("org.eclipse.equinox.simpleconfigurator.configUrl", bundlesTxt.toURL().toExternalForm()); + try { + equinox = new EmbeddedEquinox(frameworkProperties, new String[] {}, new URL[] {osgiBundle}); + equinoxContext = equinox.startFramework(); + + // verify that loaded bundles should be + Bundle[] bundlesInContext = equinoxContext.getBundles(); + // are all bundles in list of expected bundles? + for (int i = 0; i < bundlesInContext.length; i++) { + Bundle bundle = bundlesInContext[i]; + boolean found = false; + Bundle notFoundBundle = bundle; + for (int j = 0; j < bundleJars.length; j++) { + File expectedBundleFile = bundleJars[j]; + String name = getManifestEntry(expectedBundleFile, Constants.BUNDLE_SYMBOLICNAME); + String bundleName = bundle.getSymbolicName(); + if (bundleName.equalsIgnoreCase(name)) + found = true; + } + if (!found) + fail("Bundle should not be present: " + notFoundBundle); + } + + // TODO: should we check start levels? + + // verify that all bundles expected to be loaded are in fact loaded + for (int i = 0; i < bundleJars.length; i++) { + File bundle = bundleJars[i]; + String name = getManifestEntry(bundle, Constants.BUNDLE_SYMBOLICNAME); + boolean loaded = false; + for (int j = 0; j < bundlesInContext.length; j++) { + Bundle loadedBundle = bundlesInContext[j]; + if (loadedBundle.getSymbolicName().equalsIgnoreCase(name)) + loaded = true; + } + if (!loaded) + fail("Bundle expected to be loaded but is not: " + name); + } + + assertEquals("Too many/too few bundles!!", numBundles, bundlesInContext.length); + } finally { + equinox.shutdown(); + } + } + + private File getSimpleConfiguratorBundle() { + for (int i = 0; i < bundleJars.length; i++) { + File bundleJar = bundleJars[i]; + if (bundleJar.getName().startsWith("org.eclipse.equinox.simpleconfigurator")) + return bundleJar; + } + return null; + } + + private void createBundlesTxt() throws IOException { + bundlesTxt = File.createTempFile("bundles", ".txt"); + bundlesTxt.deleteOnExit(); + + BufferedWriter bundlesTxtOut = new BufferedWriter(new FileWriter(bundlesTxt)); + + for (int i = 0; i < bundleJars.length; i++) { + File bundleJar = bundleJars[i]; + bundlesTxtOut.write(getBundlesTxtEntry(bundleJar) + "\n"); + } + + bundlesTxtOut.close(); + + // FileInputStream f = new FileInputStream(bundlesTxt); + // BufferedInputStream b = new BufferedInputStream(f); + // DataInputStream d = new DataInputStream(b); + // while (d.available() != 0) { + // System.out.println(d.readLine()); + // } + } + + private String getBundlesTxtEntry(File bundleJar) throws IOException { + String name = getManifestEntry(bundleJar, Constants.BUNDLE_SYMBOLICNAME); + String version = getManifestEntry(bundleJar, Constants.BUNDLE_VERSION); + // <name>,<version>,file:<file>,<startlevel>,true + return name + "," + version + "," + "file:" + bundleJar.getAbsolutePath() + "," + getStartLevel(name) + ",true"; + } + + private String getManifestEntry(File bundleFile, String entry) { + try { + JarFile bundleJar = new JarFile(bundleFile); + String value = bundleJar.getManifest().getMainAttributes().getValue(entry); + if (value.indexOf(";") > -1) { + String[] valueElements = value.split(";"); + return valueElements[0]; + } else + return value; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + } + + private URL getFrameworkBundle() { + for (int i = 0; i < bundleJars.length; i++) { + File bundleJar = bundleJars[i]; + if (bundleJar.getName().startsWith("org.eclipse.osgi")) + try { + return bundleJar.toURL(); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return null; + } + + private int getStartLevel(String bundleName) { + int startLevel = 4; + // some special case start levels + if (bundleName.matches("org.eclipse.osgi")) { + startLevel = -1; + } else if (bundleName.matches("org.eclipse.equinox.common")) { + startLevel = 2; + } else if (bundleName.matches("org.eclipse.equinox.simpleconfigurator")) { + startLevel = 1; + } + return startLevel; + } + + private File[] getBundleJars(File directory) { + FilenameFilter bundleFilter = new FilenameFilter() { + public boolean accept(File directoryName, String filename) { + return !filename.startsWith("."); + } + }; + return directory.listFiles(bundleFilter); + } + +} diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/SimpleConfiguratorTests.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/SimpleConfiguratorTests.java new file mode 100644 index 000000000..01ffc7bdb --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/simpleconfigurator/SimpleConfiguratorTests.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2008 Red Hat, Inc. and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.equinox.p2.tests.simpleconfigurator; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class SimpleConfiguratorTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Tests for org.eclipse.equinox.simpleconfigurator"); + //$JUnit-BEGIN$ + suite.addTestSuite(BundlesTxtTest.class); + //$JUnit-END$ + return suite; + } + +} diff --git a/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.common_3.4.0.v20070910.jar b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.common_3.4.0.v20070910.jar Binary files differnew file mode 100644 index 000000000..60b2b76e3 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.common_3.4.0.v20070910.jar diff --git a/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.simpleconfigurator.jar b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.simpleconfigurator.jar Binary files differnew file mode 100644 index 000000000..04542996f --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.equinox.simpleconfigurator.jar diff --git a/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.osgi_3.4.0.v20071030.jar b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.osgi_3.4.0.v20071030.jar Binary files differnew file mode 100644 index 000000000..558b01f5b --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/testData/bundlesTxt/org.eclipse.osgi_3.4.0.v20071030.jar |