KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mx4j > tools > jython > JythonRunnerTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package test.mx4j.tools.jython;
9
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationBroadcasterSupport JavaDoc;
14 import javax.management.ObjectInstance JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 import junit.framework.TestCase;
18 import mx4j.tools.jython.JythonRunner;
19
20 /**
21  * @version $Revision: 1.3 $
22  */

23 public class JythonRunnerTest extends TestCase
24 {
25    private MBeanServer JavaDoc svr;
26    private JythonRunner jythonRunner;
27    private ObjectName JavaDoc jythonName;
28    private Int _int;
29    private ObjectName JavaDoc intName;
30
31    public interface IntMBean
32    {
33       int getValue();
34
35       void setValue(int value);
36
37       ObjectInstance JavaDoc getMBeans();
38
39       void setMBeans(ObjectInstance JavaDoc value);
40
41       ObjectInstance JavaDoc getInstances();
42
43       void setInstances(ObjectInstance JavaDoc value);
44
45       void invocation();
46
47       void invocationWithString(String JavaDoc param);
48
49       Long JavaDoc invocationWithLong(Long JavaDoc param);
50
51       Number JavaDoc subtract(Number JavaDoc one, Number JavaDoc two);
52
53       int subtractInts(int one, int two);
54    }
55
56    public class Int extends NotificationBroadcasterSupport JavaDoc implements IntMBean
57    {
58       private int value;
59       private String JavaDoc param;
60       private boolean invoked;
61       private Number JavaDoc subtracted;
62       private int subtractedInts;
63       private Notification JavaDoc notification;
64       private ObjectInstance JavaDoc mbean;
65       private ObjectInstance JavaDoc 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 JavaDoc getMBeans()
78       {
79          return mbean;
80       }
81
82       public void setMBeans(ObjectInstance JavaDoc mbeans)
83       {
84          this.mbean = mbeans;
85       }
86
87       public ObjectInstance JavaDoc getInstances()
88       {
89          return instance;
90       }
91
92       public void setInstances(ObjectInstance JavaDoc instance)
93       {
94          this.instance = instance;
95       }
96
97       public void invocation()
98       {
99          this.invoked = true;
100       }
101
102       public void invocationWithString(String JavaDoc param)
103       {
104          this.param = param;
105       }
106
107       public Long JavaDoc invocationWithLong(Long JavaDoc param)
108       {
109          return param;
110       }
111
112       public Number JavaDoc subtract(Number JavaDoc one, Number JavaDoc two)
113       {
114          if ((one.floatValue() - one.intValue()) == 0)
115          {
116             subtracted = new Integer JavaDoc(one.intValue() - two.intValue());
117          }
118          else
119          {
120             subtracted = new Float JavaDoc(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 JavaDoc name)
133    {
134       super(name);
135    }
136
137    public void setUp() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc(2), _int.subtracted);
181       assertEquals(5, _int.subtractedInts);
182    }
183
184    public void testListenerJythonScript() throws Exception JavaDoc
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 JavaDoc("Type", "Source", 1L));
192       // Make sure notification is sent
193
Thread.sleep(1000L);
194       assertEquals(111, _int.getValue());
195    }
196
197    public void testHelperFunctions() throws Exception JavaDoc
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 JavaDoc 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 JavaDoc
218    {
219       svr.unregisterMBean(jythonName);
220       super.tearDown();
221    }
222
223 }
224
Popular Tags