1 8 package test.mx4j.tools.jython; 9 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.Notification ; 13 import javax.management.NotificationBroadcasterSupport ; 14 import javax.management.ObjectInstance ; 15 import javax.management.ObjectName ; 16 17 import junit.framework.TestCase; 18 import mx4j.tools.jython.JythonRunner; 19 20 23 public class JythonRunnerTest extends TestCase 24 { 25 private MBeanServer svr; 26 private JythonRunner jythonRunner; 27 private ObjectName jythonName; 28 private Int _int; 29 private ObjectName intName; 30 31 public interface IntMBean 32 { 33 int getValue(); 34 35 void setValue(int value); 36 37 ObjectInstance getMBeans(); 38 39 void setMBeans(ObjectInstance value); 40 41 ObjectInstance getInstances(); 42 43 void setInstances(ObjectInstance value); 44 45 void invocation(); 46 47 void invocationWithString(String param); 48 49 Long invocationWithLong(Long param); 50 51 Number subtract(Number one, Number two); 52 53 int subtractInts(int one, int two); 54 } 55 56 public class Int extends NotificationBroadcasterSupport implements IntMBean 57 { 58 private int value; 59 private String param; 60 private boolean invoked; 61 private Number subtracted; 62 private int subtractedInts; 63 private Notification notification; 64 private ObjectInstance mbean; 65 private ObjectInstance instance; 66 67 public int getValue() 68 { 69 return value; 70 } 71 72 public void setValue(int value) 73 { 74 this.value = value; 75 } 76 77 public ObjectInstance getMBeans() 78 { 79 return mbean; 80 } 81 82 public void setMBeans(ObjectInstance mbeans) 83 { 84 this.mbean = mbeans; 85 } 86 87 public ObjectInstance getInstances() 88 { 89 return instance; 90 } 91 92 public void setInstances(ObjectInstance instance) 93 { 94 this.instance = instance; 95 } 96 97 public void invocation() 98 { 99 this.invoked = true; 100 } 101 102 public void invocationWithString(String param) 103 { 104 this.param = param; 105 } 106 107 public Long invocationWithLong(Long param) 108 { 109 return param; 110 } 111 112 public Number subtract(Number one, Number two) 113 { 114 if ((one.floatValue() - one.intValue()) == 0) 115 { 116 subtracted = new Integer (one.intValue() - two.intValue()); 117 } 118 else 119 { 120 subtracted = new Float (one.floatValue() - two.floatValue()); 121 } 122 return subtracted; 123 } 124 125 public int subtractInts(int one, int two) 126 { 127 subtractedInts = one - two; 128 return subtractedInts; 129 } 130 } 131 132 public JythonRunnerTest(String name) 133 { 134 super(name); 135 } 136 137 public void setUp() throws Exception 138 { 139 super.setUp(); 140 svr = MBeanServerFactory.createMBeanServer(); 141 jythonRunner = new JythonRunner(); 142 jythonName = ObjectName.getInstance("tools", "type", "JythonRunner"); 143 svr.registerMBean(jythonRunner, jythonName); 144 _int = new Int(); 145 intName = ObjectName.getInstance("test", "type", "Int"); 146 svr.registerMBean(_int, intName); 147 } 148 149 public void testInvokeFromJython() 150 { 151 jythonRunner.setScript("import jarray\n" + 152 "from java.lang import String,Long,Integer,Float\n" + 153 "from javax.management import Attribute\n" + 154 "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n" + 155 "server.setAttribute(o,Attribute(\"Value\",1))\n" + 156 "server.invoke(o,\"invocation\",None,None)\n" + 157 "params = ['parameter']\n" + 158 "paramTypes = ['java.lang.String']\n" + 159 "c = String().getClass()\n" + 160 "aryParm = jarray.array(params,c)\n" + 161 "aryType = jarray.array(paramTypes,c)\n" + 162 "server.invoke(o,\"invocationWithString\",aryParm,aryType)\n"); 163 jythonRunner.runScript(); 164 assertTrue(_int.invoked); 165 assertEquals(1, _int.value); 166 assertEquals(_int.param, "parameter"); 167 } 168 169 public void testHelperJythonObjects() throws Exception 170 { 171 jythonRunner.setScript("from java.lang import String,Long,Integer,Float\n" + 172 "from javax.management import Attribute\n" + 173 "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n" + 174 "p = proxy(server,o)\n" + 175 "p.invocationWithLong(Long(10000))\n" + 176 "p.subtract(Float(2.1),Float(1.9))\n" + 177 "p.subtract(Integer(3),Integer(1))\n" + 178 "p.subtractInts(10,5)"); 179 jythonRunner.runScript(); 180 assertEquals(new Integer (2), _int.subtracted); 181 assertEquals(5, _int.subtractedInts); 182 } 183 184 public void testListenerJythonScript() throws Exception 185 { 186 jythonRunner.setScript("o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n" + 187 "p = Proxy(server,o)\n" + 188 "p.Value=111"); 189 jythonRunner.setObservedObject(intName); 190 jythonRunner.setNotificationType("Type"); 191 _int.sendNotification(new Notification ("Type", "Source", 1L)); 192 Thread.sleep(1000L); 194 assertEquals(111, _int.getValue()); 195 } 196 197 public void testHelperFunctions() throws Exception 198 { 199 jythonRunner.setScript("import jarray\n" + 200 "s = 'test.mx4j.tools.jython.JythonRunnerTest$Int'\n" + 201 "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n" + 202 "p = Proxy(server,o)\n" + 203 "p.MBeans=mbeans('test:type=Int')[0]\n" + 204 "p.Instances=instances(s,'*:*')[0]"); 205 jythonRunner.runScript(); 206 207 ObjectInstance oinst = _int.instance; 208 assertEquals(intName, oinst.getObjectName()); 209 assertEquals("test.mx4j.tools.jython.JythonRunnerTest$Int", 210 oinst.getClassName()); 211 oinst = _int.mbean; 212 assertEquals(intName, oinst.getObjectName()); 213 assertEquals("test.mx4j.tools.jython.JythonRunnerTest$Int", 214 oinst.getClassName()); 215 } 216 217 public void tearDown() throws Exception 218 { 219 svr.unregisterMBean(jythonName); 220 super.tearDown(); 221 } 222 223 } 224 | Popular Tags |