KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > registry > ValuesTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package test.implementation.registry;
23
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MBeanServerFactory JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import junit.framework.TestCase;
33
34 import org.jboss.mx.server.ServerConstants;
35
36 import test.implementation.registry.support.Trivial;
37
38 /**
39  * Tests the value map processing in the managed mbean registry
40  *
41  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
42  */

43 public class ValuesTestCase
44    extends TestCase
45    implements ServerConstants
46 {
47    // Attributes ----------------------------------------------------------------
48

49    // Constructor ---------------------------------------------------------------
50

51    /**
52     * Construct the test
53     */

54    public ValuesTestCase(String JavaDoc s)
55    {
56       super(s);
57    }
58
59    // Tests ---------------------------------------------------------------------
60

61    /**
62     * Test classloader
63     */

64    public void testClassLoader()
65       throws Exception JavaDoc
66    {
67       // Get the previous registry value
68
String JavaDoc previous = System.getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
69       if (previous == null)
70          previous = DEFAULT_MBEAN_REGISTRY_CLASS;
71       //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
72
// "org.jboss.mx.server.registry.ManagedMBeanRegistry");
73
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
74       try
75       {
76          HashMap JavaDoc valuesMap = new HashMap JavaDoc();
77          ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
78          valuesMap.put(CLASSLOADER, Thread.currentThread().getContextClassLoader());
79          ObjectName JavaDoc mbean = new ObjectName JavaDoc("test:test=test");
80          server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "registerMBean",
81             new Object JavaDoc[]
82             {
83                new Trivial(),
84                mbean,
85                valuesMap
86             },
87             new String JavaDoc[]
88             {
89                Object JavaDoc.class.getName(),
90                ObjectName JavaDoc.class.getName(),
91                Map JavaDoc.class.getName()
92             }
93          );
94          Object JavaDoc result = server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "getValue",
95             new Object JavaDoc[]
96             {
97                mbean,
98                CLASSLOADER
99             },
100             new String JavaDoc[]
101             {
102                ObjectName JavaDoc.class.getName(),
103                String JavaDoc.class.getName()
104             }
105          );
106          assertEquals(cl, result);
107       }
108       finally
109       {
110          MBeanServerFactory.releaseMBeanServer(server);
111          System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
112       }
113    }
114
115    /**
116     * Test value
117     */

118    public void testValue()
119       throws Exception JavaDoc
120    {
121       // Get the previous registry value
122
String JavaDoc previous = System.getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
123       if (previous == null)
124          previous = DEFAULT_MBEAN_REGISTRY_CLASS;
125       //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
126
// "org.jboss.mx.server.registry.ManagedMBeanRegistry");
127
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
128       try
129       {
130          HashMap JavaDoc valuesMap = new HashMap JavaDoc();
131          Date JavaDoc date = new Date JavaDoc(System.currentTimeMillis());
132          valuesMap.put("date", date);
133          ObjectName JavaDoc mbean = new ObjectName JavaDoc("test:test=test");
134          server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "registerMBean",
135             new Object JavaDoc[]
136             {
137                new Trivial(),
138                mbean,
139                valuesMap
140             },
141             new String JavaDoc[]
142             {
143                Object JavaDoc.class.getName(),
144                ObjectName JavaDoc.class.getName(),
145                Map JavaDoc.class.getName()
146             }
147          );
148          Object JavaDoc result = server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "getValue",
149             new Object JavaDoc[]
150             {
151                mbean,
152                "date"
153             },
154             new String JavaDoc[]
155             {
156                ObjectName JavaDoc.class.getName(),
157                String JavaDoc.class.getName()
158             }
159          );
160          assertEquals(date, result);
161       }
162       finally
163       {
164          MBeanServerFactory.releaseMBeanServer(server);
165          System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
166       }
167    }
168
169    /**
170     * Test value registered
171     */

172    public void testValueRegistered()
173       throws Exception JavaDoc
174    {
175       // Get the previous registry value
176
String JavaDoc previous = System.getProperty(MBEAN_REGISTRY_CLASS_PROPERTY);
177       if (previous == null)
178          previous = DEFAULT_MBEAN_REGISTRY_CLASS;
179       //System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY,
180
// "org.jboss.mx.server.registry.ManagedMBeanRegistry");
181
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
182       try
183       {
184          HashMap JavaDoc valuesMap = new HashMap JavaDoc();
185          Date JavaDoc date = new Date JavaDoc(System.currentTimeMillis());
186          valuesMap.put("date", date);
187          ObjectName JavaDoc mbean = new ObjectName JavaDoc("test:test=test");
188          server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "registerMBean",
189             new Object JavaDoc[]
190             {
191                new Trivial(),
192                mbean,
193                valuesMap
194             },
195             new String JavaDoc[]
196             {
197                Object JavaDoc.class.getName(),
198                ObjectName JavaDoc.class.getName(),
199                Map JavaDoc.class.getName()
200             }
201          );
202          Object JavaDoc result = server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "getValue",
203             new Object JavaDoc[]
204             {
205                mbean,
206                "date"
207             },
208             new String JavaDoc[]
209             {
210                ObjectName JavaDoc.class.getName(),
211                String JavaDoc.class.getName()
212             }
213          );
214          assertEquals(date, result);
215
216          // Now remove it, reregister it and make sure it returns the new value
217
server.unregisterMBean(mbean);
218
219          Thread.sleep(2);
220          date = new Date JavaDoc(System.currentTimeMillis());
221          HashMap JavaDoc valueMap2 = new HashMap JavaDoc();
222          valueMap2.put("date", date);
223          server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "registerMBean",
224             new Object JavaDoc[]
225             {
226                new Trivial(),
227                mbean,
228                valueMap2
229             },
230             new String JavaDoc[]
231             {
232                Object JavaDoc.class.getName(),
233                ObjectName JavaDoc.class.getName(),
234                Map JavaDoc.class.getName()
235             }
236          );
237          result = server.invoke(new ObjectName JavaDoc(MBEAN_REGISTRY), "getValue",
238             new Object JavaDoc[]
239             {
240                mbean,
241                "date"
242             },
243             new String JavaDoc[]
244             {
245                ObjectName JavaDoc.class.getName(),
246                String JavaDoc.class.getName()
247             }
248          );
249          assertEquals(date, result);
250       }
251       finally
252       {
253          MBeanServerFactory.releaseMBeanServer(server);
254          System.setProperty(MBEAN_REGISTRY_CLASS_PROPERTY, previous);
255       }
256    }
257 }
258
Popular Tags