KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > server > ObjectInstanceTestCase


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.server;
23
24 import junit.framework.TestCase;
25
26 import test.implementation.server.support.Trivial;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.MBeanServerFactory JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.ObjectInstance JavaDoc;
38
39 import org.jboss.mx.server.ServerObjectInstance;
40
41 /**
42  * Tests the ObjectInstance handling which is a bit brain-dead in the RI.<p>
43  *
44  * Maybe one-day these will be part of the compliance testsuite.
45  *
46  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
47  */

48 public class ObjectInstanceTestCase
49   extends TestCase
50 {
51    // Attributes ----------------------------------------------------------------
52

53    // Constructor ---------------------------------------------------------------
54

55    /**
56     * Construct the test
57     */

58    public ObjectInstanceTestCase(String JavaDoc s)
59    {
60       super(s);
61    }
62
63    // Tests that should work in the RI ------------------------------------------
64

65    /**
66     * Test default domain
67     */

68    public void testDefaultDomain()
69    {
70       MBeanServer JavaDoc server =null;
71       ObjectName JavaDoc unqualifiedName = null;
72       ObjectName JavaDoc qualifiedName = null;
73       ObjectInstance JavaDoc instance1 = null;
74       ObjectInstance JavaDoc instance2 = null;
75       try
76       {
77          server = MBeanServerFactory.createMBeanServer();
78          unqualifiedName = new ObjectName JavaDoc(":property=1");
79          qualifiedName = new ObjectName JavaDoc("DefaultDomain:property=1");
80          instance1 = server.registerMBean(new Trivial(), qualifiedName);
81          instance2 = server.getObjectInstance(unqualifiedName);
82       }
83       catch (Exception JavaDoc e)
84       {
85          fail(e.toString());
86       }
87
88       assertEquals(instance1.getObjectName(),qualifiedName);
89       assertEquals(instance1, instance2);
90
91       if (server != null)
92          MBeanServerFactory.releaseMBeanServer(server);
93    }
94
95    /**
96     * Test different servers
97     */

98    public void testDifferentServers()
99    {
100       MBeanServer JavaDoc server =null;
101       ObjectName JavaDoc name = null;
102       ObjectInstance JavaDoc instance1 = null;
103       ObjectInstance JavaDoc instance2 = null;
104       try
105       {
106          server = MBeanServerFactory.createMBeanServer();
107          name = new ObjectName JavaDoc(":property=1");
108          instance1 = server.registerMBean(new Trivial(), name);
109          MBeanServerFactory.releaseMBeanServer(server);
110          server = MBeanServerFactory.createMBeanServer();
111          instance2 = server.registerMBean(new Trivial(), name);
112       }
113       catch (Exception JavaDoc e)
114       {
115          fail(e.toString());
116       }
117
118       if (instance1.equals(instance2) == true)
119          fail("Instances in different servers are the same");
120
121       if (server != null)
122          MBeanServerFactory.releaseMBeanServer(server);
123    }
124
125    // Tests that need to work in JBossMX because of the extra agent id --------
126

127    /**
128     * Test ObjectInstance/ServerObjectInstance Equals
129     */

130    public void testEquals()
131    {
132       // Create the object instances
133
ObjectInstance JavaDoc oi = null;
134       ServerObjectInstance soi = null;
135       ObjectName JavaDoc name = null;
136       String JavaDoc className = "org.jboss.AClass";
137       try
138       {
139          name = new ObjectName JavaDoc(":a=a");
140          oi = new ObjectInstance JavaDoc(name, className);
141          soi = new ServerObjectInstance(name, className, "agentid");
142       }
143       catch (Exception JavaDoc e)
144       {
145         fail(e.toString());
146       }
147       assertEquals(oi, soi);
148    }
149    /**
150     * Test serialization. For moving between implementations, this HAS
151     * to produce an ObjectInstance.
152     */

153    public void testSerialization()
154    {
155       // Create the new object Instance
156
ServerObjectInstance original = null;
157       ObjectInstance JavaDoc result = null;
158       ObjectName JavaDoc name = null;
159       String JavaDoc className = "org.jboss.AClassName";
160       try
161       {
162          name = new ObjectName JavaDoc(":a=a");
163          original = new ServerObjectInstance(name, className, "agentid");
164       }
165       catch (Exception JavaDoc e)
166       {
167          fail(e.toString());
168       }
169
170       try
171       {
172          // Serialize it
173
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
174          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
175          oos.writeObject(original);
176     
177          // Deserialize it
178
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
179          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
180          result = (ObjectInstance JavaDoc) ois.readObject();
181       }
182       catch (IOException JavaDoc ioe)
183       {
184          fail(ioe.toString());
185       }
186       catch (ClassNotFoundException JavaDoc cnfe)
187       {
188          fail(cnfe.toString());
189       }
190
191       // Did it work?
192
assertEquals("javax.management.ObjectInstance", result.getClass().getName());
193       assertEquals(name, result.getObjectName());
194       assertEquals(className, result.getClassName());
195    }
196 }
197
Popular Tags