Update of /cvsroot/mocklib/osgimgr/input/javasrc/osgi/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5023/input/javasrc/osgi/test
Added Files:
MockBundle.java MockGetComponent.java MockSvcReference.java
TestActivator.java
Log Message:
add osgimgr to mocklib project for now.
--- NEW FILE: MockGetComponent.java ---
package osgi.test;
import biz.xsoftware.api.osgimgr.ConfigService;
import biz.xsoftware.api.osgimgr.GetComponent;
public class MockGetComponent implements GetComponent {
private ConfigService svc;
public MockGetComponent(ConfigService svc) {
this.svc = svc;
}
public ConfigService getConfigService() {
return svc;
}
}
--- NEW FILE: TestActivator.java ---
package osgi.test;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.management.ObjectName;
import junit.framework.TestCase;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.ungoverned.gravity.servicebinder.impl.ArchitectureServiceImpl;
import osgi.Activator;
import osgi.BundleManager;
import osgi.BundleManagerMBean;
import osgi.Lifecycle;
import osgi.LifecycleMBean;
import biz.xsoftware.api.osgimgr.ConfigService;
import biz.xsoftware.api.osgimgr.GetComponent;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObject;
import biz.xsoftware.mock.MockObjectFactory;
public class TestActivator extends TestCase {
private MockObject mockCtx;
private MockObject mockConfig;
private MockBundle[] installed;
private String osgiMgrBundleName = "osgibundleName";
private long osgiMgrId = 56;
private long[] bundleIds;
private String[] bundleNames;
private Activator a;
private BundleListener bundleList;
private MockBundle osgiMgrBundle;
public TestActivator() {
super();
}
public void setUp() throws Exception {
mockCtx = MockObjectFactory.createMock(BundleContext.class);
mockCtx.setIgnoredMethods(new String[] {"getBundle"});
mockConfig = MockObjectFactory.createMock(ConfigService.class);
MockGetComponent mockGet = new MockGetComponent((ConfigService)mockConfig);
new ArchitectureServiceImpl();
a = new Activator((GetComponent)mockGet);
Dictionary<String, String> d = new Hashtable<String, String>();
d.put("Bundle-Name", osgiMgrBundleName);
osgiMgrBundle = new MockBundle(d, osgiMgrId);
//create already up and running Bundles....
installed = new MockBundle[3];
bundleIds = new long[2];
bundleIds[0] = 7;
bundleIds[1] = 8;
bundleNames = new String[2];
bundleNames[0] = "bundle0";
bundleNames[1] = "bundle1";
//mock bundle 1....
Dictionary<String, String> d1 = new Hashtable<String, String>();
d1.put("Bundle-Name", bundleNames[0]);
installed[0] = new MockBundle(d1, bundleIds[0]);
//mock bundle 2 has 2 ServiceReferences....
Dictionary<String, String> d2 = new Hashtable<String, String>();
d2.put("Bundle-Name", bundleNames[1]);
installed[1] = new MockBundle(d2, bundleIds[1]);
installed[2] = osgiMgrBundle;
start();
}
public void tearDown() throws Exception {
a.stop((BundleContext)mockCtx);
mockConfig.expectCall(MockObject.NONE);
mockCtx.expectCall(MockObject.NONE);
}
public void testBasic() {
//this is just setUp and teardown
}
public void testUnregisterAlreadyInstalledBundle() {
BundleEvent evt = new BundleEvent(BundleEvent.UNINSTALLED, installed[1]);
bundleList.bundleChanged(evt);
CalledMethod m = mockConfig.expectCall("unregisterBean");
ObjectName name = (ObjectName)m.getAllParams()[0];
assertEquals(bundleNames[1], name.getKeyProperty("name"));
assertEquals(bundleIds[1]+"", name.getKeyProperty("BundleId"));
}
public void testRegistrationOfNewBundle() {
String bundleName = "xyz";
long bundleId = 101;
Dictionary<String, String> d2 = new Hashtable<String, String>();
d2.put("Bundle-Name", bundleName);
MockBundle bundle = new MockBundle(d2, bundleId);
BundleEvent evt = new BundleEvent(BundleEvent.INSTALLED, bundle);
bundleList.bundleChanged(evt);
CalledMethod m = mockConfig.expectCall("registerBean");
Class c = (Class)m.getAllParams()[0];
ObjectName beanName = (ObjectName)m.getAllParams()[1];
Object bean = m.getAllParams()[2];
assertEquals(LifecycleMBean.class, c);
assertEquals(bundleName, beanName.getKeyProperty("name"));
assertEquals(bundleId+"", beanName.getKeyProperty("BundleId"));
assertEquals(Lifecycle.class, bean.getClass());
evt = new BundleEvent(BundleEvent.UNINSTALLED, bundle);
bundleList.bundleChanged(evt);
m = mockConfig.expectCall("unregisterBean");
ObjectName name = (ObjectName)m.getAllParams()[0];
assertEquals(bundleName, name.getKeyProperty("name"));
assertEquals(bundleId+"", name.getKeyProperty("BundleId"));
}
private void start() throws Exception {
for(int i = 0; i < 10; i++) {
mockCtx.addReturnValue("getBundle", osgiMgrBundle);
}
mockCtx.addReturnValue("getBundles", installed);
a.start((BundleContext)mockCtx);
String[] methodNames = new String[3];
methodNames[0] = "registerService";
methodNames[1] = "addBundleListener";
methodNames[2] = "getBundles";
CalledMethod[] methods = mockCtx.expectOrderedCalls(methodNames);
bundleList = (BundleListener)methods[1].getAllParams()[0];
methodNames = new String[4];
for(int i = 0; i < methodNames.length; i++) {
methodNames[i] = "registerBean";
}
methods = mockConfig.expectOrderedCalls(methodNames);
//first two bundles are BundleManagerMBean and LifecycleMBean for osgimgr bundle
Class c = (Class)methods[0].getAllParams()[0];
ObjectName beanName = (ObjectName)methods[0].getAllParams()[1];
Object bean = methods[0].getAllParams()[2];
assertEquals(BundleManagerMBean.class, c);
assertEquals("BundleManager", beanName.getKeyProperty("name"));
assertEquals(BundleManager.class, bean.getClass());
//lifecycle mbean
c = (Class)methods[1].getAllParams()[0];
beanName = (ObjectName)methods[1].getAllParams()[1];
bean = methods[1].getAllParams()[2];
assertEquals(LifecycleMBean.class, c);
assertEquals(osgiMgrBundleName, beanName.getKeyProperty("name"));
assertEquals(osgiMgrId+"", beanName.getKeyProperty("BundleId"));
assertEquals(Lifecycle.class, bean.getClass());
//already installed bundle 1....
c = (Class)methods[2].getAllParams()[0];
beanName = (ObjectName)methods[2].getAllParams()[1];
bean = methods[2].getAllParams()[2];
assertEquals(LifecycleMBean.class, c);
assertEquals(bundleNames[0], beanName.getKeyProperty("name"));
assertEquals(bundleIds[0]+"", beanName.getKeyProperty("BundleId"));
assertEquals(Lifecycle.class, bean.getClass());
//already installed bundle 2....
c = (Class)methods[3].getAllParams()[0];
beanName = (ObjectName)methods[3].getAllParams()[1];
bean = methods[3].getAllParams()[2];
assertEquals(LifecycleMBean.class, c);
assertEquals(bundleNames[1], beanName.getKeyProperty("name"));
assertEquals(bundleIds[1]+"", beanName.getKeyProperty("BundleId"));
assertEquals(Lifecycle.class, bean.getClass());
}
}
--- NEW FILE: MockSvcReference.java ---
package osgi.test;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
public class MockSvcReference implements ServiceReference {
public Object getProperty(String arg0) {
throw new UnsupportedOperationException("not supported");
}
public String[] getPropertyKeys() {
throw new UnsupportedOperationException("not supported");
}
public Bundle getBundle() {
throw new UnsupportedOperationException("not supported");
}
public Bundle[] getUsingBundles() {
throw new UnsupportedOperationException("not supported");
}
}
--- NEW FILE: MockBundle.java ---
package osgi.test;
import java.io.InputStream;
import java.net.URL;
import java.util.Dictionary;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
public class MockBundle implements Bundle {
private Dictionary dic;
private long id;
private ServiceReference[] registered;
public MockBundle(Dictionary dic, long id) {
this.dic = dic;
this.id = id;
}
public MockBundle(Dictionary dic, long id, ServiceReference[] registered) {
this.dic = dic;
this.id = id;
this.registered = registered;
}
public int getState() {
throw new UnsupportedOperationException("not supported");
}
public void start() throws BundleException {
throw new UnsupportedOperationException("not supported");
}
public void stop() throws BundleException {
throw new UnsupportedOperationException("not supported");
}
public void update() throws BundleException {
throw new UnsupportedOperationException("not supported");
}
public void update(InputStream arg0) throws BundleException {
throw new UnsupportedOperationException("not supported");
}
public void uninstall() throws BundleException {
throw new UnsupportedOperationException("not supported");
}
public Dictionary getHeaders() {
return dic;
}
public long getBundleId() {
return id;
}
public String getLocation() {
throw new UnsupportedOperationException("not supported");
}
public ServiceReference[] getRegisteredServices() {
return registered;
}
public ServiceReference[] getServicesInUse() {
throw new UnsupportedOperationException("not supported");
}
public boolean hasPermission(Object arg0) {
throw new UnsupportedOperationException("not supported");
}
public URL getResource(String arg0) {
throw new UnsupportedOperationException("not supported");
}
}
|