Create test bundles

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@752324 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-03-11 00:51:55 +00:00
parent fcf0272f95
commit a8a286315d
15 changed files with 162 additions and 28 deletions

View file

@ -21,6 +21,7 @@ package calculator;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.logging.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@ -30,18 +31,20 @@ import org.osgi.framework.ServiceRegistration;
*
*/
public class CalculatorActivator implements BundleActivator {
private Logger logger = Logger.getLogger(CalculatorActivator.class.getName());
private ServiceRegistration registration;
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("sca.service", "CalculatorComponent#service-name(Calculator)");
registration = context.registerService(CalculatorService.class.getName(),
new CalculatorServiceImpl(),
props);
logger.info("Registering " + CalculatorService.class.getName());
registration = context.registerService(CalculatorService.class.getName(), new CalculatorServiceImpl(), props);
}
public void stop(BundleContext context) throws Exception {
context.ungetService(registration.getReference());
logger.info("UnRegistering " + registration);
registration.unregister();
}
}

View file

@ -18,7 +18,11 @@
*/
package calculator;
import org.oasisopen.sca.annotation.Reference;
import calculator.operations.AddService;
import calculator.operations.DivideService;
import calculator.operations.MultiplyService;
import calculator.operations.SubtractService;
/**
* An implementation of the Calculator service.
@ -30,22 +34,18 @@ public class CalculatorServiceImpl implements CalculatorService {
private MultiplyService multiplyService;
private DivideService divideService;
@Reference
public void setAddService(AddService addService) {
this.addService = addService;
}
@Reference
public void setSubtractService(SubtractService subtractService) {
this.subtractService = subtractService;
}
@Reference
public void setDivideService(DivideService divideService) {
this.divideService = divideService;
}
@Reference
public void setMultiplyService(MultiplyService multiplyService) {
this.multiplyService = multiplyService;
}

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import org.oasisopen.sca.annotation.Remotable;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import java.util.logging.Level;
import java.util.logging.Logger;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import org.oasisopen.sca.annotation.Remotable;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import java.util.logging.Level;
import java.util.logging.Logger;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import org.oasisopen.sca.annotation.Remotable;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import java.util.logging.Level;
import java.util.logging.Logger;

View file

@ -17,7 +17,7 @@
* under the License.
*/
package calculator;
package calculator.operations;
import java.util.ArrayList;
import java.util.Dictionary;
@ -39,12 +39,20 @@ public class OperationsActivator implements BundleActivator {
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> props = new Hashtable<String, Object>();
logger.info("Registering " + AddService.class.getName());
props.put("sca.service", "AddComponent#service-name(Add)");
registrations.add(context.registerService(AddService.class.getName(), new AddServiceImpl(), props));
logger.info("Registering " + SubtractService.class.getName());
props.put("sca.service", "SubtractComponent#service-name(Subtract)");
registrations.add(context.registerService(SubtractService.class.getName(), new SubtractServiceImpl(), props));
logger.info("Registering " + MultiplyService.class.getName());
props.put("sca.service", "MultiplyComponent#service-name(Multiply)");
registrations.add(context.registerService(MultiplyService.class.getName(), new MultiplyServiceImpl(), props));
logger.info("Registering " + DivideService.class.getName());
props.put("sca.service", "DivideComponent#service-name(Divide)");
registrations.add(context.registerService(DivideService.class.getName(), new DivideServiceImpl(), props));
}
@ -52,7 +60,7 @@ public class OperationsActivator implements BundleActivator {
public void stop(BundleContext context) throws Exception {
for (ServiceRegistration registration : registrations) {
logger.info("Unregistering " + registration);
context.ungetService(registration.getReference());
registration.unregister();
}
}

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import org.oasisopen.sca.annotation.Remotable;

View file

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package calculator;
package calculator.operations;
import java.util.logging.Level;
import java.util.logging.Logger;

View file

@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package calculator.test;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestBundles;
import org.apache.tuscany.sca.node.equinox.launcher.EquinoxHost;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import calculator.CalculatorActivator;
import calculator.CalculatorService;
import calculator.CalculatorServiceImpl;
import calculator.operations.AddService;
import calculator.operations.AddServiceImpl;
import calculator.operations.DivideService;
import calculator.operations.DivideServiceImpl;
import calculator.operations.MultiplyService;
import calculator.operations.MultiplyServiceImpl;
import calculator.operations.OperationsActivator;
import calculator.operations.SubtractService;
import calculator.operations.SubtractServiceImpl;
/**
*
*/
public class CalculatorOSGiTestCase {
private static EquinoxHost host;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Set<URL> bundles = new HashSet<URL>();
bundles.add(OSGiTestBundles.createBundle("target/test-classes/calculator-bundle.jar",
"calculator",
"calculator.operations",
CalculatorService.class,
CalculatorServiceImpl.class, CalculatorActivator.class));
bundles.add(OSGiTestBundles.createBundle("target/test-classes/operations-bundle.jar",
"operations",
null,
OperationsActivator.class,
AddService.class,
AddServiceImpl.class,
SubtractService.class,
SubtractServiceImpl.class,
MultiplyService.class,
MultiplyServiceImpl.class,
DivideService.class, DivideServiceImpl.class));
try {
host = new EquinoxHost(bundles);
BundleContext context = host.start();
for (Bundle b : context.getBundles()) {
System.out.println(b);
b.start();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testOSGi() {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (host != null) {
host.stop();
}
}
}

View file

@ -52,8 +52,8 @@ public class OSGiTestCase {
compositeName = "osgitest.composite";
OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar",
OSGiTestInterface.class.getName(),
OSGiTestImpl.class,
OSGiTestInterface.class);
null,
OSGiTestImpl.class, OSGiTestInterface.class);
node =
host.createNode("osgitest.composite", new Contribution("c1", new File("target/test-classes").toURI()

View file

@ -20,11 +20,14 @@
package org.apache.tuscany.sca.implementation.osgi.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
@ -45,16 +48,21 @@ public class OSGiTestBundles {
return index == -1 ? "" : name.substring(0, index);
}
public static void createBundle(String jarName, String bundleName, Class<?>... classes) throws Exception {
public static URL createBundle(String jarName, String bundleName, String imports, Class<?>... classes)
throws Exception {
Class<?> activator = null;
Set<String> packages = new HashSet<String>();
StringBuffer exports = new StringBuffer();
for (Class<?> cls : classes) {
if (cls.isAssignableFrom(BundleActivator.class)) {
if (BundleActivator.class.isAssignableFrom(cls)) {
activator = cls;
}
if (cls.isInterface()) {
exports.append(getPackageName(cls)).append(",");
String pkg = getPackageName(cls);
if (packages.add(pkg)) {
exports.append(pkg).append(",");
}
}
}
if (exports.length() > 0) {
@ -62,12 +70,20 @@ public class OSGiTestBundles {
}
Manifest manifest = new Manifest();
// This attribute Manifest-Version is required so that the MF will be added to the jar
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, bundleName);
manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0.0");
manifest.getMainAttributes().putValue(Constants.BUNDLE_NAME, bundleName);
manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, exports.toString());
manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, "org.osgi.framework," + exports.toString());
String importPackages = null;
if (imports != null) {
importPackages = "org.osgi.framework," + imports;
} else {
importPackages = "org.osgi.framework";
}
manifest.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, importPackages);
if (activator != null) {
manifest.getMainAttributes().putValue(Constants.BUNDLE_ACTIVATOR, activator.getName());
@ -83,10 +99,12 @@ public class OSGiTestBundles {
jarOut.close();
out.close();
FileOutputStream fileOut = new FileOutputStream(jarName);
File jar = new File(jarName);
FileOutputStream fileOut = new FileOutputStream(jar);
fileOut.write(out.toByteArray());
fileOut.close();
return jar.toURI().toURL();
}
private static void addClass(JarOutputStream jarOut, Class<?> javaClass) throws IOException, FileNotFoundException {
@ -102,5 +120,6 @@ public class OSGiTestBundles {
byte[] fileContents = new byte[file.available()];
file.read(fileContents);
jarOut.write(fileContents);
jarOut.closeEntry();
}
}

View file

@ -72,8 +72,8 @@ public class OSGiReadImplTestCase {
OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar",
OSGiTestInterface.class.getName(),
OSGiTestImpl.class,
OSGiTestInterface.class);
null,
OSGiTestImpl.class, OSGiTestInterface.class);
}