1 4 package org.oddjob.jmx.server; 5 6 import javax.management.InstanceAlreadyExistsException ; 7 import javax.management.InstanceNotFoundException ; 8 import javax.management.JMException ; 9 import javax.management.MBeanRegistrationException ; 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.MalformedObjectNameException ; 13 import javax.management.NotCompliantMBeanException ; 14 import javax.management.Notification ; 15 import javax.management.NotificationListener ; 16 import javax.management.ObjectName ; 17 18 import junit.framework.TestCase; 19 20 import org.apache.commons.beanutils.DynaClass; 21 import org.apache.commons.beanutils.DynaProperty; 22 import org.apache.log4j.Logger; 23 import org.oddjob.Stateful; 24 import org.oddjob.Structural; 25 import org.oddjob.state.JobState; 26 import org.oddjob.state.JobStateEvent; 27 import org.oddjob.state.JobStateListener; 28 import org.oddjob.structural.StructuralEvent; 29 import org.oddjob.structural.StructuralListener; 30 import org.oddjob.util.ThreadManager; 31 32 35 public class OddjobMBeanTest extends TestCase { 36 private static final Logger logger = Logger.getLogger(OddjobMBeanTest.class); 37 private ServerModel sm; 38 39 public void setUp() { 40 logger.debug("==================== Running " + getName() + " ====================="); 41 InterfaceManagerFactory imf = new InterfaceManagerFactory(); 42 imf.addInterfaceInfo(new StatefulInfo()); 43 imf.addInterfaceInfo(new StructuralInfo()); 44 sm = new ServerModel( 45 "//test", 46 new ThreadManager(), 47 imf); 48 } 49 50 public void testRegister() 51 throws MalformedObjectNameException , MBeanRegistrationException , 52 InstanceAlreadyExistsException , NotCompliantMBeanException { 53 Runnable myJob = new Runnable () { 54 public void run() { 55 56 } 57 }; 58 59 sm.setRoot(myJob); 60 OddjobMBean ojmb = new OddjobMBean(myJob, null, new ServerContext(sm)); 61 62 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 63 mbs.registerMBean(ojmb, new ObjectName ("Oddjob:name=whatever")); 64 } 65 66 public void testNotifyState() 67 throws MalformedObjectNameException , MBeanRegistrationException , 68 InstanceAlreadyExistsException , NotCompliantMBeanException , 69 InstanceNotFoundException { 70 class MyStateful implements Stateful { 71 JobStateListener jsl; 72 public void addJobStateListener(JobStateListener listener) { 73 jsl = listener; 74 listener.jobStateChange(new JobStateEvent(this, JobState.READY, null)); 75 } 76 public void removeJobStateListener(JobStateListener listener) { 77 } 78 public void foo() { 79 jsl.jobStateChange(new JobStateEvent(this, JobState.COMPLETE, null)); 80 } 81 }; 82 MyStateful myJob = new MyStateful(); 83 84 class MyNotLis implements NotificationListener { 85 Notification n; 86 int num; 87 public void handleNotification(Notification arg0, Object arg1) { 88 n = arg0; 89 num++; 90 } 91 }; 92 MyNotLis myNotLis = new MyNotLis(); 93 94 OddjobMBean ojmb = new OddjobMBean(myJob, null, new ServerContext(sm)); 95 ObjectName on = new ObjectName ("Oddjob:name=whatever"); 96 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 97 mbs.registerMBean(ojmb, on); 98 99 mbs.addNotificationListener(on, myNotLis, null, null); 100 101 myJob.foo(); 102 assertEquals("source", on, myNotLis.n.getSource()); 103 assertEquals("type", StatefulInfo.STATE_CHANGE_NOTIF_TYPE, myNotLis.n.getType()); 104 105 mbs.unregisterMBean(on); 106 } 107 108 111 public void testNotifyStructure() 112 throws JMException { 113 final Object myChild = new Object () { 114 public String toString() { 115 return "my child"; 116 } 117 }; 118 119 class MyStructural implements Structural { 120 StructuralListener jsl; 121 boolean foo = false; 122 public void addStructuralListener(StructuralListener listener) { 123 jsl = listener; 124 } 125 public void removeStructuralListener(StructuralListener listener) { 126 } 127 public void foo() { 128 if (foo) { 129 jsl.childRemoved(new StructuralEvent(this, myChild, 0)); 130 } 131 else { 132 jsl.childAdded(new StructuralEvent(this, myChild, 0)); 133 } 134 foo = !foo; 135 } 136 }; 137 MyStructural myJob = new MyStructural(); 138 139 class MyNotLis implements NotificationListener { 140 Notification n; 141 public void handleNotification(Notification arg0, Object arg1) { 142 n = arg0; 143 } 144 }; 145 MyNotLis myNotLis = new MyNotLis(); 146 147 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 148 OddjobMBeanFactory f = new OddjobMBeanFactory(mbs); 149 ObjectName on = f.createMBeanFor(myJob, new ServerContext(sm)); 150 151 mbs.addNotificationListener(on, myNotLis, null, null); 152 153 myJob.foo(); 154 assertEquals("source", on, myNotLis.n.getSource()); 155 assertEquals("type", StructuralInfo.CHILD_ADDED_NOTIF_TYPE, myNotLis.n.getType()); 156 157 mbs.invoke(on, "resync", new Object [0], new String [0]); 158 assertEquals("source", on, myNotLis.n.getSource()); 159 assertEquals("type", OddjobMBean.RESYNC_FINISHED_NOTIF_TYPE, myNotLis.n.getType()); 160 161 myJob.foo(); 162 assertEquals("source", on, myNotLis.n.getSource()); 163 assertEquals("type", StructuralInfo.CHILD_REMOVED_NOTIF_TYPE, myNotLis.n.getType()); 164 165 mbs.unregisterMBean(on); 166 } 167 168 public static class MyBean { 169 public String getFruit() { return "apple"; } 170 } 171 172 public void voidTestGetDynaClass() throws Exception { 173 MyBean b = new MyBean(); 174 175 ServerModel sm = new ServerModel( 176 "//test", 177 new ThreadManager(), 178 new InterfaceManagerFactory()); 179 sm.setRoot(b); 180 181 OddjobMBean ojmb = new OddjobMBean(b, null, 182 new ServerContext(sm)); 183 DynaClass dc = (DynaClass) ojmb.invoke( 184 "getDynaClass", new Object [] {}, new String [] {}); 185 assertNotNull(dc); 186 DynaProperty dp = dc.getDynaProperty("fruit"); 187 assertEquals(String .class, dp.getType()); 188 } 189 } 190 | Popular Tags |