KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > remote > rmi > RMIMarshallingTest


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
9 package test.javax.management.remote.rmi;
10
11 import java.io.File JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.management.Attribute JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerConnection JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationFilter JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import javax.management.ObjectInstance JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.loading.PrivateMLet JavaDoc;
24 import javax.management.remote.JMXConnector JavaDoc;
25 import javax.management.remote.JMXConnectorFactory JavaDoc;
26 import javax.management.remote.JMXConnectorServer JavaDoc;
27 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
28 import javax.management.remote.JMXServiceURL JavaDoc;
29
30 import test.MX4JTestCase;
31 import test.javax.management.remote.support.Marshalling;
32 import test.javax.management.remote.support.Unknown;
33
34 /**
35  * @version $Revision: 1.5 $
36  */

37 public class RMIMarshallingTest extends MX4JTestCase
38 {
39    private MBeanServer JavaDoc server = null;
40    private MBeanServerConnection JavaDoc conn = null;
41    private JMXConnectorServer JavaDoc cntorServer = null;
42    private JMXConnector JavaDoc cntor = null;
43    private ObjectName JavaDoc mbeanName;
44    private ObjectName JavaDoc mbeanLoaderName;
45
46
47    public RMIMarshallingTest(String JavaDoc s)
48    {
49       super(s);
50    }
51
52    public void setUp() throws Exception JavaDoc
53    {
54       super.setUp();
55       // Create a classloader that sees only the MBean and its parameter classes (Unknown)
56
File JavaDoc mbeanJar = new File JavaDoc("dist/test/mx4j-tests.jar");
57       PrivateMLet JavaDoc mbeanLoader = new PrivateMLet JavaDoc(new URL JavaDoc[]{mbeanJar.toURL()}, getClass().getClassLoader().getParent(), false);
58       mbeanLoaderName = ObjectName.getInstance("marshal:type=mlet");
59
60       server = newMBeanServer();
61       server.registerMBean(mbeanLoader, mbeanLoaderName);
62
63       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("rmi", "localhost", 0);
64       cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
65       cntorServer.start();
66
67       cntor = JMXConnectorFactory.connect(cntorServer.getAddress());
68       conn = cntor.getMBeanServerConnection();
69       mbeanName = ObjectName.getInstance("marshal:type=mbean");
70
71    }
72
73    public void tearDown() throws Exception JavaDoc
74    {
75       if (cntor != null) cntor.close();
76       if (cntorServer != null) cntorServer.stop();
77    }
78
79    protected static class MockNotificationListener implements NotificationListener JavaDoc, Serializable JavaDoc
80    {
81       long numberOfNotifications = 0;
82
83       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
84       {
85 // System.out.println("[MockNotificationListener] Notification: "+notification+" Handback: "+handback);
86
assertEquals(notification.getSequenceNumber(), numberOfNotifications);
87          assertEquals(notification.getType(), Unknown.class.getName());
88          numberOfNotifications++;
89          synchronized (this)
90          {
91             this.notify();
92          }
93       }
94
95       public synchronized void waitOnNotification(long timeout, long sequence) throws InterruptedException JavaDoc
96       {
97          long to;
98          if (timeout > 0)
99             to = System.currentTimeMillis() + timeout;
100          else
101             to = -1;
102          while (numberOfNotifications < sequence) // Check for missed notification
103
{
104             this.wait(timeout);
105             if (to > -1 && System.currentTimeMillis() >= to) // Check if waited for full timeout
106
{
107                Thread.currentThread().interrupt();
108                break;
109             }
110          }
111       }
112
113    }
114
115    protected static class MockNotificationFilter implements NotificationFilter JavaDoc, Serializable JavaDoc
116    {
117
118       public boolean isNotificationEnabled(Notification JavaDoc notification)
119       {
120 // System.out.println("[MockNotificationFilter] Notification: "+notification);
121
return true;
122       }
123
124    }
125
126    private void createMBean() throws Exception JavaDoc
127    {
128       ObjectInstance JavaDoc inst = conn.createMBean(Marshalling.class.getName(), mbeanName, new Object JavaDoc[]{new Unknown()}, new String JavaDoc[]{Unknown.class.getName()});
129    }
130
131    public void testCreateMBean() throws Exception JavaDoc
132    {
133       conn.createMBean(Marshalling.class.getName(), mbeanName);
134       checkRegistration();
135
136       conn.createMBean(Marshalling.class.getName(),
137                        mbeanName, new Object JavaDoc[]{new Unknown()},
138                        new String JavaDoc[]{Unknown.class.getName()});
139       checkRegistration();
140    }
141
142    private void checkRegistration() throws Exception JavaDoc
143    {
144       // Check registrations
145
if (!conn.isRegistered(mbeanName)) fail();
146       if (!server.isRegistered(mbeanName)) fail();
147       conn.unregisterMBean(mbeanName);
148    }
149
150    public void testInstanceOf() throws Exception JavaDoc
151    {
152       createMBean();
153       // Check instanceof
154
if (!conn.isInstanceOf(mbeanName, Marshalling.class.getName())) fail();
155       if (!server.isInstanceOf(mbeanName, Marshalling.class.getName())) fail();
156    }
157
158    public void testInvocationUnknownReturn() throws Exception JavaDoc
159    {
160       createMBean();
161       // Check invocation
162
Object JavaDoc returned = conn.invoke(mbeanName, "unknownReturnValue", new Object JavaDoc[0], new String JavaDoc[0]);
163       if (!returned.getClass().getName().equals(Unknown.class.getName())) fail();
164       returned = server.invoke(mbeanName, "unknownReturnValue", new Object JavaDoc[0], new String JavaDoc[0]);
165       if (!returned.getClass().getName().equals(Unknown.class.getName())) fail();
166       Object JavaDoc remoteUnk = conn.invoke(mbeanName, "unknownArgument", new Object JavaDoc[]{new Unknown()}, new String JavaDoc[]{Unknown.class.getName()});
167       Object JavaDoc localUnk = server.invoke(mbeanName, "unknownArgument", new Object JavaDoc[]{new Unknown()}, new String JavaDoc[]{Unknown.class.getName()});
168       assertEquals(remoteUnk, localUnk);
169    }
170
171    public void testUnregisterMBean() throws Exception JavaDoc
172    {
173       createMBean();
174       // Check unregistration
175
conn.unregisterMBean(mbeanName);
176       if (conn.isRegistered(mbeanName)) fail();
177       if (server.isRegistered(mbeanName)) fail();
178
179    }
180
181    public void testNotifications() throws Exception JavaDoc
182    {
183       createMBean();
184       MockNotificationListener listener = new MockNotificationListener();
185       conn.addNotificationListener(mbeanName,
186                                    listener,
187                                    new MockNotificationFilter(),
188                                    new Object JavaDoc());
189
190       Thread.sleep(1000L);
191       Attribute JavaDoc att = new Attribute JavaDoc("UnknownAttribute", new Unknown());
192
193       conn.setAttribute(mbeanName, att);
194
195       Thread.sleep(1000L);
196       // This triggers a notification
197
try
198       {
199          listener.waitOnNotification(1000L, 1);
200       }
201       catch (InterruptedException JavaDoc ignore)
202       {
203       }
204
205       assertEquals(1, listener.numberOfNotifications);
206
207       conn.removeNotificationListener(mbeanName, listener);
208
209       Thread.sleep(1000L);
210       conn.setAttribute(mbeanName, att);
211
212       Thread.sleep(1000L);
213       // This triggers a notification
214
try
215       {
216          listener.waitOnNotification(1000L, 2);
217       }
218       catch (InterruptedException JavaDoc ignore)
219       {
220       }
221
222       assertEquals(1, listener.numberOfNotifications);
223    }
224
225    public void testQuery() throws Exception JavaDoc
226    {
227       createMBean();
228       ObjectName JavaDoc pattern = mbeanName;
229       ObjectName JavaDoc query = mbeanName;
230       Set JavaDoc beans = conn.queryMBeans(pattern, query);
231       Object JavaDoc[] set = beans.toArray();
232       assertEquals(1, set.length);
233 // System.out.println("set[0]: "+set[0]+" class: "+set[0].getClass());
234
ObjectInstance JavaDoc inst = (ObjectInstance JavaDoc)set[0];
235       assertTrue(inst.getClassName().equals(Marshalling.class.getName()));
236
237       beans = conn.queryNames(pattern, query);
238       set = beans.toArray();
239       assertEquals(1, set.length);
240 // System.out.println("set[0]: "+set[0]+" class: "+set[0].getClass());
241
ObjectName JavaDoc nm = (ObjectName JavaDoc)set[0];
242       assertTrue(nm.equals(mbeanName));
243    }
244
245    public void testAttributes() throws Exception JavaDoc
246    {
247       createMBean();
248       Unknown value = new Unknown();
249       Attribute JavaDoc att = new Attribute JavaDoc("UnknownAttribute", value);
250       conn.setAttribute(mbeanName, att);
251       Object JavaDoc returned = conn.getAttribute(mbeanName, "UnknownAttribute");
252       assertTrue(returned.getClass().getName().equals(Unknown.class.getName()));
253    }
254 }
255
Popular Tags