KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > varia > 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 org.jboss.test.jmx.compliance.varia;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import junit.framework.TestCase;
34
35 /**
36  * Object Instance tests.<p>
37  *
38  * NOTE: The tests use String literals to ensure the comparisons are
39  * not performed on object references.
40  *
41  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
42  */

43 public class ObjectInstanceTestCase
44   extends TestCase
45 {
46   // Attributes ----------------------------------------------------------------
47

48   // Constructor ---------------------------------------------------------------
49

50   /**
51    * Construct the test
52    */

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

60   /**
61    * Test String constructor.
62    */

63   public void testStringConstructor()
64   {
65     ObjectInstance JavaDoc instance = null;
66
67     try
68     {
69       instance = new ObjectInstance JavaDoc("test:type=test", "ClassName");
70     }
71     catch (Exception JavaDoc e)
72     {
73       fail(e.toString());
74     }
75
76     // Did it work?
77
assertEquals("test:type=test", instance.getObjectName().toString());
78     assertEquals("ClassName", instance.getClassName());
79   }
80
81   /**
82    * Test ObjectName constructor.
83    */

84   public void testObjectNameConstructor()
85   {
86     ObjectInstance JavaDoc instance = null;
87     ObjectName JavaDoc objectName = null;
88
89     try
90     {
91       objectName = new ObjectName JavaDoc(":type=test");
92       instance = new ObjectInstance JavaDoc(objectName, "ClassName");
93     }
94     catch (Exception JavaDoc e)
95     {
96       fail(e.toString());
97     }
98
99     // Did it work?
100
assertEquals(objectName, instance.getObjectName());
101     assertEquals("ClassName", instance.getClassName());
102   }
103
104   /**
105    * Test Equals.
106    */

107   public void testEquals()
108   {
109     ObjectInstance JavaDoc instanceTest = null;
110     ObjectInstance JavaDoc instanceSame = null;
111     ObjectInstance JavaDoc instanceDiffName = null;
112     ObjectInstance JavaDoc instanceDiffClass = null;
113
114     try
115     {
116       instanceTest = new ObjectInstance JavaDoc("test:type=test", "ClassName");
117       instanceSame = new ObjectInstance JavaDoc("test:type=test", "ClassName");
118       instanceDiffName = new ObjectInstance JavaDoc("test:type=different", "ClassName");
119       instanceDiffClass = new ObjectInstance JavaDoc("test:type=test", "Another");
120     }
121     catch (Exception JavaDoc e)
122     {
123       fail(e.toString());
124     }
125
126     assertEquals(instanceTest, instanceTest);
127     assertEquals(instanceTest, instanceSame);
128     if (instanceTest.equals(instanceDiffName))
129       fail("ObjectInstance.equals broken for object name");
130     if (instanceTest.equals(instanceDiffClass))
131       fail("ObjectInstance.equals broken for class name");
132   }
133
134   /**
135    * Test errors.
136    */

137   public void testErrors()
138   {
139     boolean caught = false;
140     try
141     {
142       new ObjectInstance JavaDoc("rubbish", "ClassName");
143     }
144     catch (MalformedObjectNameException JavaDoc e)
145     {
146       caught = true;
147     }
148     catch (Exception JavaDoc e)
149     {
150       fail(e.toString());
151     }
152     if (caught == false)
153       fail("ObjectInstance(String, String) failed to report malformed object name");
154
155     try
156     {
157       String JavaDoc NULL = null;
158       new ObjectInstance JavaDoc(NULL, "ClassName");
159     }
160     catch (MalformedObjectNameException JavaDoc e)
161     {
162       caught = true;
163     }
164     catch (Exception JavaDoc e)
165     {
166       fail(e.toString());
167     }
168     if (caught == false)
169       fail("ObjectInstance(String, String) failed to report null object name");
170   }
171
172   /**
173    * Test serialization.
174    */

175   public void testSerialization()
176   {
177     ObjectInstance JavaDoc original = null;
178     ObjectInstance JavaDoc result = null;
179     ObjectName JavaDoc objectName = null;
180
181     try
182     {
183       objectName = new ObjectName JavaDoc(":type=test");
184       original = new ObjectInstance JavaDoc(objectName, "ClassName");
185
186       // Serialize it
187
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
188       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
189       oos.writeObject(original);
190     
191       // Deserialize it
192
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
193       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
194       result = (ObjectInstance JavaDoc) ois.readObject();
195     }
196     catch (Exception JavaDoc e)
197     {
198       fail(e.toString());
199     }
200
201     // Did it work?
202
assertEquals(original, result);
203     assertEquals(objectName, result.getObjectName());
204     assertEquals("ClassName", result.getClassName());
205   }
206 }
207
Popular Tags