KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > openmbean > SimpleTypeTestCase


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.openmbean;
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 import java.math.BigDecimal JavaDoc;
29 import java.math.BigInteger JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.openmbean.SimpleType JavaDoc;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Simple type tests.<p>
39  *
40  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
41  */

42 public class SimpleTypeTestCase
43   extends TestCase
44 {
45    // Static --------------------------------------------------------------------
46

47    static ObjectName JavaDoc objectName;
48
49    static
50    {
51       try
52       {
53          objectName = new ObjectName JavaDoc("test:test=test");
54       }
55       catch (Exception JavaDoc e)
56       {
57          throw new RuntimeException JavaDoc(e.toString());
58       }
59    }
60
61    // Attributes ----------------------------------------------------------------
62

63    SimpleType JavaDoc[] types = new SimpleType JavaDoc[]
64    {
65       SimpleType.BIGDECIMAL,
66       SimpleType.BIGINTEGER,
67       SimpleType.BOOLEAN,
68       SimpleType.BYTE,
69       SimpleType.CHARACTER,
70       SimpleType.DATE,
71       SimpleType.DOUBLE,
72       SimpleType.FLOAT,
73       SimpleType.INTEGER,
74       SimpleType.LONG,
75       SimpleType.OBJECTNAME,
76       SimpleType.SHORT,
77       SimpleType.STRING,
78       SimpleType.VOID
79    };
80
81    Class JavaDoc[] classes = new Class JavaDoc[]
82    {
83       BigDecimal JavaDoc.class,
84       BigInteger JavaDoc.class,
85       Boolean JavaDoc.class,
86       Byte JavaDoc.class,
87       Character JavaDoc.class,
88       Date JavaDoc.class,
89       Double JavaDoc.class,
90       Float JavaDoc.class,
91       Integer JavaDoc.class,
92       Long JavaDoc.class,
93       ObjectName JavaDoc.class,
94       Short JavaDoc.class,
95       String JavaDoc.class,
96       Void JavaDoc.class
97    };
98
99    Object JavaDoc[] objects = new Object JavaDoc[]
100    {
101       new BigDecimal JavaDoc(1),
102       BigInteger.ONE,
103       new Boolean JavaDoc(false),
104       new Byte JavaDoc(Byte.MAX_VALUE),
105       new Character JavaDoc('a'),
106       new Date JavaDoc(System.currentTimeMillis()),
107       new Double JavaDoc(1),
108       new Float JavaDoc(1),
109       new Integer JavaDoc(1),
110       new Long JavaDoc(1),
111       objectName,
112       new Short JavaDoc(Short.MAX_VALUE),
113       new String JavaDoc("hello"),
114       null
115    };
116
117    // Constructor ---------------------------------------------------------------
118

119    /**
120     * Construct the test
121     */

122    public SimpleTypeTestCase(String JavaDoc s)
123    {
124       super(s);
125    }
126
127    // Tests ---------------------------------------------------------------------
128

129    public void testSimpleTypes()
130       throws Exception JavaDoc
131    {
132       for (int i = 0; i < types.length; i++)
133       {
134          String JavaDoc className = classes[i].getName();
135          assertEquals(className, types[i].getClassName());
136          assertEquals(className, types[i].getTypeName());
137          assertEquals(className, types[i].getDescription());
138       }
139    }
140
141    public void testEquals()
142       throws Exception JavaDoc
143    {
144       for (int i = 0; i < types.length; i++)
145         for (int j = 0; j < types.length; j++)
146         {
147            if (i == j)
148               assertEquals(types[i], types[j]);
149            else
150               assertTrue("Simple Types should be different " + classes[i],
151                          types[i] != types[j]);
152         }
153    }
154
155    public void testIsValue()
156       throws Exception JavaDoc
157    {
158       for (int i = 0; i < types.length; i++)
159       {
160          for (int j = 0; j < types.length; j++)
161          {
162             // isValue makes no sense for Void
163
if (objects[i] == null)
164                continue;
165
166             if (i == j)
167                assertTrue(classes[i] + " should be a simple value of " + types[j],
168                           types[j].isValue(objects[i]));
169             else
170                assertTrue(classes[i] + " should NOT be a simple value of " + types[j],
171                           types[j].isValue(objects[i]) == false);
172          }
173
174          assertTrue("null should NOT be a simple value of " + types[i], types[i].isValue(null) == false);
175       }
176    }
177
178    public void testHashCode()
179       throws Exception JavaDoc
180    {
181       for (int i = 0; i < types.length; i++)
182          assertEquals(classes[i].getName().hashCode(), types[i].hashCode());
183    }
184
185    public void testToString()
186       throws Exception JavaDoc
187    {
188       for (int i = 0; i < types.length; i++)
189       {
190          assertTrue("Simple Type " + classes[i].getName() +
191                     " should contain " + SimpleType JavaDoc.class.getName(),
192                     types[i].toString().indexOf(SimpleType JavaDoc.class.getName()) != -1);
193          assertTrue("Simple Type " + classes[i].getName() +
194                     " should contain " + classes[i].getName(),
195                     types[i].toString().indexOf(classes[i].getName()) != -1);
196       }
197    }
198
199    public void testSerialization()
200       throws Exception JavaDoc
201    {
202       for (int i = 0; i < types.length; i++)
203       {
204          // Serialize it
205
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
206          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
207          oos.writeObject(types[i]);
208     
209          // Deserialize it
210
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
211          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
212          SimpleType JavaDoc result = (SimpleType JavaDoc) ois.readObject();
213
214          assertTrue("Should resolve to same object after serialization " + types[i], types[i] == result);
215       }
216    }
217 }
218
Popular Tags