KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > remote > JMXConnectorTestCase


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;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import javax.management.ListenerNotFoundException JavaDoc;
22 import javax.management.MBeanNotificationInfo JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.Notification JavaDoc;
26 import javax.management.NotificationEmitter JavaDoc;
27 import javax.management.NotificationFilter JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.remote.JMXAuthenticator JavaDoc;
31 import javax.management.remote.JMXConnector JavaDoc;
32 import javax.management.remote.JMXConnectorFactory JavaDoc;
33 import javax.management.remote.JMXConnectorServer JavaDoc;
34 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
35 import javax.management.remote.JMXPrincipal JavaDoc;
36 import javax.management.remote.JMXServiceURL JavaDoc;
37 import javax.security.auth.Subject JavaDoc;
38
39 import test.MX4JTestCase;
40
41 /**
42  * @version $Revision: 1.21 $
43  */

44 public abstract class JMXConnectorTestCase extends MX4JTestCase
45 {
46    public JMXConnectorTestCase(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    protected void tearDown() throws Exception JavaDoc
52    {
53       sleep(5000);
54    }
55
56    public abstract JMXServiceURL JavaDoc createJMXConnectorServerAddress() throws MalformedURLException JavaDoc;
57
58    public abstract Map JavaDoc getEnvironment();
59
60    public void testNewJMXConnectorWithNullURL() throws Exception JavaDoc
61    {
62       try
63       {
64          JMXConnectorFactory.connect(null);
65          fail();
66       }
67       catch (NullPointerException JavaDoc x)
68       {
69       }
70    }
71
72    public void testConnectionId() throws Exception JavaDoc
73    {
74       // Format is:
75
// protocol:[[host]:port] [clientId] [arbitrary]
76
// Spaces are mandatory, brackets indicates optional parts
77

78       JMXConnectorServer JavaDoc cntorServer = null;
79       JMXConnector JavaDoc cntor = null;
80       try
81       {
82          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
83          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
84          cntorServer.start();
85          sleep(5000);
86
87          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
88          String JavaDoc connectionId = cntor.getConnectionId();
89          String JavaDoc protocol = connectionId.substring(0, connectionId.indexOf(':'));
90          assertEquals(protocol, url.getProtocol());
91
92          // Match first mandatory space
93
int space = connectionId.indexOf(' ');
94          String JavaDoc remaining = connectionId.substring(space + 1);
95          // Match second mandatory space
96
space = remaining.indexOf(' ');
97          String JavaDoc arbitrary = remaining.substring(space + 1);
98          if (arbitrary.length() < 1) fail("Missing MX4J arbitrary test");
99       }
100       finally
101       {
102          if (cntor != null) cntor.close();
103          if (cntorServer != null) cntorServer.stop();
104       }
105    }
106
107    public void testConnectionWithNoPath() throws Exception JavaDoc
108    {
109       JMXConnectorServer JavaDoc cntorServer = null;
110       JMXConnector JavaDoc cntor = null;
111       try
112       {
113          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
114          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
115          cntorServer.start();
116          sleep(5000);
117
118          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
119       }
120       finally
121       {
122          if (cntor != null) cntor.close();
123          if (cntorServer != null) cntorServer.stop();
124       }
125    }
126
127    public void testJMXAuthenticator() throws Exception JavaDoc
128    {
129       final String JavaDoc password = "mx4j";
130       JMXAuthenticator JavaDoc authenticator = new JMXAuthenticator JavaDoc()
131       {
132          public Subject JavaDoc authenticate(Object JavaDoc credentials) throws SecurityException JavaDoc
133          {
134             if (password.equals(credentials))
135             {
136                JMXPrincipal JavaDoc principal = new JMXPrincipal JavaDoc("mx4j");
137                Subject JavaDoc subject = new Subject JavaDoc();
138                subject.getPrincipals().add(principal);
139                subject.setReadOnly();
140                return subject;
141             }
142             throw new SecurityException JavaDoc("Authentication Failed");
143          }
144       };
145
146       JMXConnectorServer JavaDoc cntorServer = null;
147       JMXConnector JavaDoc cntor = null;
148       try
149       {
150          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
151          MBeanServer JavaDoc server = newMBeanServer();
152          Map JavaDoc serverEnv = getEnvironment();
153          serverEnv.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
154          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, server);
155          cntorServer.start();
156          sleep(5000);
157
158          // Try to provide wrong password
159
Map JavaDoc clientEnv = getEnvironment();
160          try
161          {
162             testJMXAuthenticatorConnect(cntorServer.getAddress(), clientEnv);
163             fail();
164          }
165          catch (SecurityException JavaDoc x)
166          {
167          }
168
169          // Try now with a correct password
170
clientEnv.put(JMXConnector.CREDENTIALS, password);
171          testJMXAuthenticatorConnect(cntorServer.getAddress(), clientEnv);
172       }
173       finally
174       {
175          if (cntor != null) cntor.close();
176          if (cntorServer != null) cntorServer.stop();
177       }
178    }
179
180    protected void testJMXAuthenticatorConnect(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws SecurityException JavaDoc, IOException JavaDoc
181    {
182       JMXConnectorFactory.connect(url, environment);
183    }
184
185    public void testStopServerBeforeClosingClient() throws Exception JavaDoc
186    {
187       JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
188       JMXConnectorServer JavaDoc cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
189       cntorServer.start();
190       sleep(5000);
191
192       JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
193       MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
194
195       cntorServer.stop();
196
197       try
198       {
199          mbsc.getDefaultDomain();
200          fail();
201       }
202       catch (IOException JavaDoc x)
203       {
204       }
205    }
206
207    public void testStopServerAndCloseClientThenInvoke() throws Exception JavaDoc
208    {
209       JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
210       JMXConnectorServer JavaDoc cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
211       cntorServer.start();
212       sleep(5000);
213
214       JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
215       MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
216
217       cntor.close();
218       cntorServer.stop();
219
220       try
221       {
222          mbsc.getDefaultDomain();
223          fail();
224       }
225       catch (IOException JavaDoc x)
226       {
227       }
228    }
229
230    public void testSerializedConnectorCanConnect() throws Exception JavaDoc
231    {
232       JMXConnectorServer JavaDoc cntorServer = null;
233       JMXConnector JavaDoc cntor = null;
234       try
235       {
236          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
237          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
238          cntorServer.start();
239          sleep(5000);
240
241          cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment());
242
243          // Serialize it: we want to test serialization does no reset data members
244
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
245          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
246          oos.writeObject(cntor);
247          oos.close();
248          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
249          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
250          cntor = (JMXConnector JavaDoc)ois.readObject();
251          ois.close();
252
253          cntor.connect();
254          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
255          mbsc.getDefaultDomain();
256
257          // Again
258
cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
259
260          // Serialize it: we want to test serialization does no reset data members
261
baos = new ByteArrayOutputStream JavaDoc();
262          oos = new ObjectOutputStream JavaDoc(baos);
263          oos.writeObject(cntor);
264          oos.close();
265          bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
266          ois = new ObjectInputStream JavaDoc(bais);
267          cntor = (JMXConnector JavaDoc)ois.readObject();
268          ois.close();
269
270          cntor.connect();
271          mbsc = cntor.getMBeanServerConnection();
272          mbsc.getDefaultDomain();
273       }
274       finally
275       {
276          if (cntor != null) cntor.close();
277          if (cntorServer != null) cntorServer.stop();
278       }
279    }
280
281    public void testDefaultClassLoader() throws Exception JavaDoc
282    {
283       JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
284       Map JavaDoc environment = new HashMap JavaDoc(getEnvironment());
285       environment.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, new Object JavaDoc());
286       try
287       {
288          JMXConnectorFactory.newJMXConnector(url, environment);
289          fail();
290       }
291       catch (IllegalArgumentException JavaDoc x)
292       {
293       }
294
295       JMXConnector JavaDoc cntor = JMXConnectorFactory.newJMXConnector(url, getEnvironment());
296       try
297       {
298          cntor.connect(environment);
299          fail();
300       }
301       catch (IllegalArgumentException JavaDoc x)
302       {
303       }
304    }
305
306    public void testListenersAreRemovedOnConnectorClose() throws Exception JavaDoc
307    {
308       JMXConnectorServer JavaDoc cntorServer = null;
309       JMXConnector JavaDoc cntor = null;
310       try
311       {
312          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
313          MBeanServer JavaDoc server = newMBeanServer();
314          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
315          cntorServer.start();
316          sleep(5000);
317
318          Emitter JavaDoc emitter = new Emitter JavaDoc();
319          ObjectName JavaDoc emitterName = ObjectName.getInstance(":name=emitter");
320          server.registerMBean(emitter, emitterName);
321
322          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
323          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
324
325          NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
326          {
327             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
328             {
329             }
330          };
331
332          // Add the listener and be sure the mechanism of removal works fine
333
mbsc.addNotificationListener(emitterName, listener, null, null);
334          assertEquals(emitter.getSize(), 1);
335          mbsc.removeNotificationListener(emitterName, listener, null, null);
336          assertEquals(emitter.getSize(), 0);
337
338          // Add the listener and close the connector
339
mbsc.addNotificationListener(emitterName, listener, null, null);
340          assertEquals(emitter.getSize(), 1);
341          cntor.close();
342          assertEquals(emitter.getSize(), 0);
343       }
344       finally
345       {
346          if (cntorServer != null) cntorServer.stop();
347       }
348    }
349
350    public void testConnectWithProviderClassLoader() throws Exception JavaDoc
351    {
352       JMXConnectorServer JavaDoc cntorServer = null;
353       JMXConnector JavaDoc cntor = null;
354       try
355       {
356          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
357          MBeanServer JavaDoc server = newMBeanServer();
358          Map JavaDoc serverEnv = getEnvironment();
359          serverEnv.put(JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER, getClass().getClassLoader());
360          ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
361          Thread.currentThread().setContextClassLoader(getClass().getClassLoader().getParent());
362          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, server);
363          cntorServer.start();
364          Thread.currentThread().setContextClassLoader(old);
365          sleep(5000);
366
367          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
368          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
369          assertNotNull(mbsc);
370       }
371       finally
372       {
373          if (cntor != null) cntor.close();
374          if (cntorServer != null) cntorServer.stop();
375       }
376    }
377
378    public interface EmitterMBean
379    {
380    }
381
382    private static class Emitter implements NotificationEmitter JavaDoc, EmitterMBean
383    {
384       private List JavaDoc listeners = new ArrayList JavaDoc();
385
386       public void addNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws IllegalArgumentException JavaDoc
387       {
388          listeners.add(listener);
389       }
390
391       public void removeNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
392       {
393          listeners.remove(listener);
394       }
395
396       public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
397       {
398          return new MBeanNotificationInfo JavaDoc[0];
399       }
400
401       public void removeNotificationListener(NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
402       {
403       }
404
405       public int getSize()
406       {
407          return listeners.size();
408       }
409    }
410 }
411
Popular Tags