001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008: package test.mx4j.tools.jython;
009:
010: import javax.management.MBeanServer;
011: import javax.management.MBeanServerFactory;
012: import javax.management.Notification;
013: import javax.management.NotificationBroadcasterSupport;
014: import javax.management.ObjectInstance;
015: import javax.management.ObjectName;
016:
017: import junit.framework.TestCase;
018: import mx4j.tools.jython.JythonRunner;
019:
020: /**
021: * @version $Revision: 1.3 $
022: */
023: public class JythonRunnerTest extends TestCase {
024: private MBeanServer svr;
025: private JythonRunner jythonRunner;
026: private ObjectName jythonName;
027: private Int _int;
028: private ObjectName intName;
029:
030: public interface IntMBean {
031: int getValue();
032:
033: void setValue(int value);
034:
035: ObjectInstance getMBeans();
036:
037: void setMBeans(ObjectInstance value);
038:
039: ObjectInstance getInstances();
040:
041: void setInstances(ObjectInstance value);
042:
043: void invocation();
044:
045: void invocationWithString(String param);
046:
047: Long invocationWithLong(Long param);
048:
049: Number subtract(Number one, Number two);
050:
051: int subtractInts(int one, int two);
052: }
053:
054: public class Int extends NotificationBroadcasterSupport implements
055: IntMBean {
056: private int value;
057: private String param;
058: private boolean invoked;
059: private Number subtracted;
060: private int subtractedInts;
061: private Notification notification;
062: private ObjectInstance mbean;
063: private ObjectInstance instance;
064:
065: public int getValue() {
066: return value;
067: }
068:
069: public void setValue(int value) {
070: this .value = value;
071: }
072:
073: public ObjectInstance getMBeans() {
074: return mbean;
075: }
076:
077: public void setMBeans(ObjectInstance mbeans) {
078: this .mbean = mbeans;
079: }
080:
081: public ObjectInstance getInstances() {
082: return instance;
083: }
084:
085: public void setInstances(ObjectInstance instance) {
086: this .instance = instance;
087: }
088:
089: public void invocation() {
090: this .invoked = true;
091: }
092:
093: public void invocationWithString(String param) {
094: this .param = param;
095: }
096:
097: public Long invocationWithLong(Long param) {
098: return param;
099: }
100:
101: public Number subtract(Number one, Number two) {
102: if ((one.floatValue() - one.intValue()) == 0) {
103: subtracted = new Integer(one.intValue()
104: - two.intValue());
105: } else {
106: subtracted = new Float(one.floatValue()
107: - two.floatValue());
108: }
109: return subtracted;
110: }
111:
112: public int subtractInts(int one, int two) {
113: subtractedInts = one - two;
114: return subtractedInts;
115: }
116: }
117:
118: public JythonRunnerTest(String name) {
119: super (name);
120: }
121:
122: public void setUp() throws Exception {
123: super .setUp();
124: svr = MBeanServerFactory.createMBeanServer();
125: jythonRunner = new JythonRunner();
126: jythonName = ObjectName.getInstance("tools", "type",
127: "JythonRunner");
128: svr.registerMBean(jythonRunner, jythonName);
129: _int = new Int();
130: intName = ObjectName.getInstance("test", "type", "Int");
131: svr.registerMBean(_int, intName);
132: }
133:
134: public void testInvokeFromJython() {
135: jythonRunner
136: .setScript("import jarray\n"
137: + "from java.lang import String,Long,Integer,Float\n"
138: + "from javax.management import Attribute\n"
139: + "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n"
140: + "server.setAttribute(o,Attribute(\"Value\",1))\n"
141: + "server.invoke(o,\"invocation\",None,None)\n"
142: + "params = ['parameter']\n"
143: + "paramTypes = ['java.lang.String']\n"
144: + "c = String().getClass()\n"
145: + "aryParm = jarray.array(params,c)\n"
146: + "aryType = jarray.array(paramTypes,c)\n"
147: + "server.invoke(o,\"invocationWithString\",aryParm,aryType)\n");
148: jythonRunner.runScript();
149: assertTrue(_int.invoked);
150: assertEquals(1, _int.value);
151: assertEquals(_int.param, "parameter");
152: }
153:
154: public void testHelperJythonObjects() throws Exception {
155: jythonRunner
156: .setScript("from java.lang import String,Long,Integer,Float\n"
157: + "from javax.management import Attribute\n"
158: + "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n"
159: + "p = proxy(server,o)\n"
160: + "p.invocationWithLong(Long(10000))\n"
161: + "p.subtract(Float(2.1),Float(1.9))\n"
162: + "p.subtract(Integer(3),Integer(1))\n"
163: + "p.subtractInts(10,5)");
164: jythonRunner.runScript();
165: assertEquals(new Integer(2), _int.subtracted);
166: assertEquals(5, _int.subtractedInts);
167: }
168:
169: public void testListenerJythonScript() throws Exception {
170: jythonRunner
171: .setScript("o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n"
172: + "p = Proxy(server,o)\n" + "p.Value=111");
173: jythonRunner.setObservedObject(intName);
174: jythonRunner.setNotificationType("Type");
175: _int.sendNotification(new Notification("Type", "Source", 1L));
176: // Make sure notification is sent
177: Thread.sleep(1000L);
178: assertEquals(111, _int.getValue());
179: }
180:
181: public void testHelperFunctions() throws Exception {
182: jythonRunner
183: .setScript("import jarray\n"
184: + "s = 'test.mx4j.tools.jython.JythonRunnerTest$Int'\n"
185: + "o = ObjectName.getInstance(\"test\",\"type\",\"Int\")\n"
186: + "p = Proxy(server,o)\n"
187: + "p.MBeans=mbeans('test:type=Int')[0]\n"
188: + "p.Instances=instances(s,'*:*')[0]");
189: jythonRunner.runScript();
190:
191: ObjectInstance oinst = _int.instance;
192: assertEquals(intName, oinst.getObjectName());
193: assertEquals("test.mx4j.tools.jython.JythonRunnerTest$Int",
194: oinst.getClassName());
195: oinst = _int.mbean;
196: assertEquals(intName, oinst.getObjectName());
197: assertEquals("test.mx4j.tools.jython.JythonRunnerTest$Int",
198: oinst.getClassName());
199: }
200:
201: public void tearDown() throws Exception {
202: svr.unregisterMBean(jythonName);
203: super.tearDown();
204: }
205:
206: }
|