/** Test notifying job state to a NotificationListener. */ public void testNotifyState() throws Exception { /** Fixture Stateful */ class MyStateful extends MockStateful { StateListener jsl; public void addStateListener(StateListener listener) { jsl = listener; listener.jobStateChange(new StateEvent(this, JobState.READY, null)); } public void removeStateListener(StateListener listener) {} public void foo() { jsl.jobStateChange(new StateEvent(this, JobState.COMPLETE, null)); } }; MyStateful myJob = new MyStateful(); MyNotLis myNotLis = new MyNotLis(); ServerContext serverContext = new ServerContextImpl(myJob, sm, new OurHierarchicalRegistry()); // create and register MBean. OddjobMBean ojmb = new OddjobMBean( myJob, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext); ObjectName on = OddjobMBeanFactory.objectName(0); MBeanServer mbs = MBeanServerFactory.createMBeanServer(); mbs.registerMBean(ojmb, on); // add notification listener. mbs.addNotificationListener(on, myNotLis, null, null); // check null state to begin with assertEquals("number", 0, myNotLis.getNum()); // change state. myJob.foo(); // check state assertEquals("number", 1, myNotLis.getNum()); assertEquals("source", on, myNotLis.getNotification(0).getSource()); assertEquals( "type", StatefulHandlerFactory.STATE_CHANGE_NOTIF_TYPE, myNotLis.getNotification(0).getType()); mbs.unregisterMBean(on); }
/** Test retrieving log events */ public void testLogging() throws Exception { LoggingBean bean = new LoggingBean(); ((ServerModelImpl) sm).setLogFormat("%m"); ServerContext serverContext = new ServerContextImpl(bean, sm, new OurHierarchicalRegistry()); OddjobMBean ojmb = new OddjobMBean( bean, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext); Logger testLogger = Logger.getLogger(bean.loggerName()); testLogger.setLevel(Level.DEBUG); testLogger.info("Test"); LogEvent[] events = (LogEvent[]) ojmb.invoke( SharedConstants.RETRIEVE_LOG_EVENTS_METHOD, new Object[] {new Long(-1), new Integer(10)}, new String[] {Long.TYPE.getName(), Integer.TYPE.getName()}); assertEquals("num events", 1, events.length); assertEquals("event 0", "Test", events[0].getMessage()); }
/** * Test DynaClass of a RemoteBean. * * @throws Exception */ public void testGetDynaClass() throws Exception { MyBean sampleBean = new MyBean(); ServerContext serverContext = new ServerContextImpl(sampleBean, sm, new OurHierarchicalRegistry()); OddjobMBean test = new OddjobMBean( sampleBean, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext); DynaClass dc = (DynaClass) test.invoke("getDynaClass", new Object[] {}, new String[] {}); assertNotNull(dc); DynaProperty dp = dc.getDynaProperty("fruit"); assertEquals(String.class, dp.getType()); }
/** Test creating and registering an OddjobMBean. */ public void testRegister() throws Exception { Runnable myJob = new Runnable() { public void run() {} }; ServerContext serverContext = new ServerContextImpl(myJob, sm, new OurHierarchicalRegistry()); OddjobMBean ojmb = new OddjobMBean( myJob, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext); ObjectName on = new ObjectName("Oddjob:name=whatever"); MBeanServer mbs = MBeanServerFactory.createMBeanServer(); mbs.registerMBean(ojmb, on); assertTrue(mbs.isRegistered(on)); mbs.unregisterMBean(on); }
/** Test notification of structural change. */ public void testNotifyStructure() throws JMException { final Object myChild = new Object() { public String toString() { return "my child"; } }; class MyStructural implements Structural { StructuralListener jsl; boolean foo = false; public void addStructuralListener(StructuralListener listener) { jsl = listener; } public void removeStructuralListener(StructuralListener listener) {} public void foo() { if (foo) { jsl.childRemoved(new StructuralEvent(this, myChild, 0)); } else { jsl.childAdded(new StructuralEvent(this, myChild, 0)); } foo = !foo; } }; MyStructural myJob = new MyStructural(); MyNotLis myNotLis = new MyNotLis(); MBeanServer mbs = MBeanServerFactory.createMBeanServer(); OddjobMBeanFactory f = new OddjobMBeanFactory(mbs, new StandardArooaSession()); ServerContext serverContext = new ServerContextImpl(myJob, sm, new OurHierarchicalRegistry()); ObjectName on = f.createMBeanFor(myJob, serverContext); mbs.addNotificationListener(on, myNotLis, null, null); Notification[] notifications = (Notification[]) mbs.invoke(on, "structuralSynchronize", new Object[0], new String[0]); assertEquals(1, notifications.length); myJob.foo(); assertEquals("notifications", 1, myNotLis.getNum()); assertEquals("source", on, myNotLis.getNotification(0).getSource()); assertEquals( "type", StructuralHandlerFactory.STRUCTURAL_NOTIF_TYPE, myNotLis.getNotification(0).getType()); myJob.foo(); assertEquals("notifications", 2, myNotLis.getNum()); assertEquals("source", on, myNotLis.getNotification(1).getSource()); assertEquals( "type", StructuralHandlerFactory.STRUCTURAL_NOTIF_TYPE, myNotLis.getNotification(1).getType()); mbs.unregisterMBean(on); }