Update of /cvsroot/mocklib/osgimgr/input/javasrc/biz/xsoftware/test/osgimgr
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5023/input/javasrc/biz/xsoftware/test/osgimgr
Added Files:
MockInterface.java TestBeanStuff.java TestOsgiMgr.java
TestProxy.java
Log Message:
add osgimgr to mocklib project for now.
--- NEW FILE: TestBeanStuff.java ---
package biz.xsoftware.test.osgimgr;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.StandardMBean;
import javax.management.remote.JMXConnectorServerMBean;
import javax.management.remote.JMXServiceURL;
import junit.framework.TestCase;
import biz.xsoftware.api.osgimgr.ConfigService;
import biz.xsoftware.api.osgimgr.ServerFactory;
import biz.xsoftware.api.osgimgr.Bootstrap;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObject;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.test.osgimgr.mock.ADummyInterface;
import biz.xsoftware.test.osgimgr.mock.Mock2MBean;
import biz.xsoftware.test.osgimgr.mock.MockMBean;
public class TestBeanStuff extends TestCase {
private static final Logger log = Logger.getLogger(TestBeanStuff.class.getName());
private MockObject svc2;
private MockMBean theBean;
private String bundleId = "1";
private StandardMBean stdBean;
private MockObject mockFactory;
private MockObject mockServer;
private MockObject mockJMXConnect;
private ConfigService svc;
public TestBeanStuff(String arg0) {
super(arg0);
}
public void setUp() throws Exception {
mockFactory = MockObjectFactory.createMock(ServerFactory.class);
mockServer = MockObjectFactory.createMock(MBeanServer.class);
mockJMXConnect = MockObjectFactory.createMock(JMXConnectorServerMBean.class);
mockJMXConnect.setIgnoredMethods(new String[] { "getAddress" });
svc = Bootstrap.createConfigService((ServerFactory)mockFactory);
start();
Class[] classes = new Class[] { ADummyInterface.class, MockMBean.class, Mock2MBean.class };
svc2 = MockObjectFactory.createMock(classes);
svc.configureAndRegisterService(svc2, bundleId);
String[] methodNames = new String[2];
methodNames[0] = "registerMBean";
methodNames[1] = "registerMBean";
CalledMethod[] methods = mockServer.expectOrderedCalls(methodNames);
stdBean = (StandardMBean)methods[0].getAllParams()[0];
theBean = (MockMBean)stdBean.getImplementation();
}
public void tearDown() throws Exception {
svc.unregisterService(svc2, bundleId);
mockServer.expectCall(MockObject.NONE);
svc.uninstalled(bundleId);
String[] methodNames = new String[2];
methodNames[0] = "unregisterMBean";
methodNames[1] = "unregisterMBean";
mockServer.expectOrderedCalls(methodNames);
//because proxies stay registered while service is unregistered, stop
//will unregiser the two additional proxies too.
stop(11);
svc2.expectCall(MockObject.NONE);
mockFactory.expectCall(MockObject.NONE);
mockServer.expectCall(MockObject.NONE);
mockJMXConnect.expectCall(MockObject.NONE);
}
//TODO: write this test case
public void testConfigureServiceWhileStopped() {
}
//TODO: write this test case
public void testGetSetFullBlownObject() {
}
public void testConfigureServiceWhileRunning() throws Exception {
int expectedProp = 5;
svc2.addReturnValue("getProperty", expectedProp);
int actualProp = theBean.getProperty();
svc2.expectCall("getProperty");
assertEquals(expectedProp, actualProp);
expectedProp = 9;
theBean.setProperty(expectedProp);
CalledMethod m = svc2.expectCall("setProperty");
actualProp = ((Integer)m.getAllParams()[0]).intValue();
assertEquals(expectedProp, actualProp);
}
public void testGetDocumentation() throws Exception {
MBeanInfo info = stdBean.getMBeanInfo();
String actual = info.getDescription();
assertEquals("Test Documentation", actual);
MBeanAttributeInfo[] attrInfo = info.getAttributes();
assertEquals("array should only contain one property", 1, attrInfo.length);
assertEquals("Test get method docs", attrInfo[0].getDescription());
MBeanOperationInfo[] operInfo = info.getOperations();
assertEquals("Should only be one operation", 1, operInfo.length);
assertEquals("Test for some operation", operInfo[0].getDescription());
}
private void start() throws Exception {
if(log.isLoggable(Level.FINE))
log.fine("mockSvc="+mockServer);
mockFactory.addReturnValue("createServer", mockServer);
mockFactory.addReturnValue("createJMXConnector", mockJMXConnect);
svc.start();
String[] methodNames = new String[2];
methodNames[0] = "createServer";
methodNames[1] = "createJMXConnector";
CalledMethod[] methods = mockFactory.expectOrderedCalls(methodNames);
JMXServiceURL url = (JMXServiceURL)methods[1].getAllParams()[0];
assertEquals("port should be correct", 1099, url.getPort());
assertEquals("host should be correct", "localhost", url.getHost());
MBeanServer svr = (MBeanServer)methods[1].getAllParams()[2];
assertEquals("Should be correct server", mockServer, svr);
methodNames = new String[12];
for(int i = 0; i < 11; i++) {
methodNames[i] = "registerMBean";
}
methodNames[11] = "setAttribute";
mockServer.expectOrderedCalls(methodNames);
mockJMXConnect.expectCall("start");
}
private CalledMethod[] stop(int numUnreg) throws Exception {
svc.stop();
String[] methodNames = new String[numUnreg];
for(int i = 0; i < numUnreg; i++) {
methodNames[i] = "unregisterMBean";
}
CalledMethod[] methods = mockServer.expectOrderedCalls(methodNames);
mockJMXConnect.expectCall("stop");
return methods;
}
}
--- NEW FILE: TestProxy.java ---
package biz.xsoftware.test.osgimgr;
import junit.framework.TestCase;
public class TestProxy extends TestCase {
public TestProxy(String arg0) {
super(arg0);
}
public void testProxy() {
// MBeanInvokeHandler proxy = new MBeanInvokeHandler();
// Class[] classes = new Class[] {MockInterface.class};
// MockInterface mock = (MockInterface)Proxy.newProxyInstance(TestProxy.class.getClassLoader(), classes, proxy);
//
// mock.someMethod();
//
// String s = "hello";
// proxy.setRealService(s);
//
// mock.someMethod();
}
}
--- NEW FILE: TestOsgiMgr.java ---
package biz.xsoftware.test.osgimgr;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.management.remote.JMXConnectorServerMBean;
import javax.management.remote.JMXServiceURL;
import junit.framework.TestCase;
import biz.xsoftware.api.osgimgr.ConfigService;
import biz.xsoftware.api.osgimgr.ServerFactory;
import biz.xsoftware.api.osgimgr.Bootstrap;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObject;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.test.osgimgr.mock.ADummyInterface;
import biz.xsoftware.test.osgimgr.mock.Mock2MBean;
import biz.xsoftware.test.osgimgr.mock.MockMBean;
import biz.xsoftware.test.osgimgr.mock.MockService1;
public class TestOsgiMgr extends TestCase {
private MockObject mockFactory;
private MockObject mockServer;
private MockObject mockJMXConnect;
private ConfigService svc;
public TestOsgiMgr(String arg0) {
super(arg0);
}
public void setUp() throws Exception {
mockFactory = MockObjectFactory.createMock(ServerFactory.class);
mockServer = MockObjectFactory.createMock(MBeanServer.class);
mockJMXConnect = MockObjectFactory.createMock(JMXConnectorServerMBean.class);
mockJMXConnect.setIgnoredMethods(new String[] { "getAddress" });
svc = Bootstrap.createConfigService((ServerFactory)mockFactory);
start();
}
public void tearDown() throws Exception {
mockFactory.expectCall(MockObject.NONE);
mockServer.expectCall(MockObject.NONE);
mockJMXConnect.expectCall(MockObject.NONE);
}
public void testBasic() throws Exception {
//start was done in setup for all tests...
stop(11);
}
public void testAddingRemovingBundle() throws Exception {
//use one bundleId for both services which can happen sometimes....
String bundleId = "1";
Class[] classes = new Class[] { ADummyInterface.class, MockMBean.class, Mock2MBean.class };
MockObject svc2 = MockObjectFactory.createMock(classes);
MockService1 svc1 = new MockService1();
svc.configureAndRegisterService(svc1, bundleId);
mockServer.expectCall(MockObject.NONE);
svc.configureAndRegisterService(svc2, bundleId);
String[] methodNames = new String[2];
methodNames[0] = "registerMBean";
methodNames[1] = "registerMBean";
CalledMethod[] methods = mockServer.expectOrderedCalls(methodNames);
StandardMBean stdBean = (StandardMBean)methods[0].getAllParams()[0];
ObjectName beanName = (ObjectName)methods[0].getAllParams()[1];
assertTrue(stdBean.getImplementation() instanceof MockMBean);
assertEquals(MockMBean.class, stdBean.getMBeanInterface());
assertEquals("Zeus.Level3.TestDomain", beanName.getDomain());
assertEquals(MockMBean.class.getName(), beanName.getKeyProperty("name"));
assertEquals(bundleId, beanName.getKeyProperty("id"));
stdBean = (StandardMBean)methods[1].getAllParams()[0];
beanName = (ObjectName)methods[1].getAllParams()[1];
assertTrue(stdBean.getImplementation() instanceof Mock2MBean);
assertEquals(Mock2MBean.class, stdBean.getMBeanInterface());
assertEquals("Zeus.Level3.Services", beanName.getDomain());
assertEquals(Mock2MBean.class.getName(), beanName.getKeyProperty("name"));
assertEquals(bundleId, beanName.getKeyProperty("id"));
svc.unregisterService(svc1, bundleId);
svc.unregisterService(svc2, bundleId);
mockServer.expectCall(MockObject.NONE);
svc.uninstalled(bundleId);
methodNames = new String[2];
methodNames[0] = "unregisterMBean";
methodNames[1] = "unregisterMBean";
methods = mockServer.expectOrderedCalls(methodNames);
//because proxies stay registered while service is unregistered, stop
//will unregiser the two additional proxies too.
stop(11);
}
//TODO: finish this test case
public void testUninstalledWithoutUnregister() {
}
private static final Logger log = Logger.getLogger(TestOsgiMgr.class.getName());
private void start() throws Exception {
if(log.isLoggable(Level.FINE))
log.fine("mockSvc="+mockServer);
mockFactory.addReturnValue("createServer", mockServer);
mockFactory.addReturnValue("createJMXConnector", mockJMXConnect);
svc.start();
String[] methodNames = new String[2];
methodNames[0] = "createServer";
methodNames[1] = "createJMXConnector";
CalledMethod[] methods = mockFactory.expectOrderedCalls(methodNames);
JMXServiceURL url = (JMXServiceURL)methods[1].getAllParams()[0];
assertEquals("port should be correct", 1099, url.getPort());
assertEquals("host should be correct", "localhost", url.getHost());
MBeanServer svr = (MBeanServer)methods[1].getAllParams()[2];
assertEquals("Should be correct server", mockServer, svr);
methodNames = new String[12];
for(int i = 0; i < 11; i++) {
methodNames[i] = "registerMBean";
}
methodNames[11] = "setAttribute";
mockServer.expectOrderedCalls(methodNames);
mockJMXConnect.expectCall("start");
}
private CalledMethod[] stop(int numUnreg) throws Exception {
svc.stop();
String[] methodNames = new String[numUnreg];
for(int i = 0; i < numUnreg; i++) {
methodNames[i] = "unregisterMBean";
}
CalledMethod[] methods = mockServer.expectOrderedCalls(methodNames);
mockJMXConnect.expectCall("stop");
return methods;
}
}
--- NEW FILE: MockInterface.java ---
package biz.xsoftware.test.osgimgr;
public interface MockInterface {
public void someMethod();
}
|