KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > openmbean > SimpleTypeTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.openmbean;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.math.BigDecimal;
15 import java.math.BigInteger;
16 import java.util.Date;
17
18 import javax.management.ObjectName;
19 import javax.management.openmbean.SimpleType;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Simple type tests.<p>
25  *
26  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
27  */

28 public class SimpleTypeTestCase
29   extends TestCase
30 {
31    // Static --------------------------------------------------------------------
32

33    static ObjectName objectName;
34
35    static
36    {
37       try
38       {
39          objectName = new ObjectName("test:test=test");
40       }
41       catch (Exception e)
42       {
43          throw new RuntimeException(e.toString());
44       }
45    }
46
47    // Attributes ----------------------------------------------------------------
48

49    SimpleType[] types = new SimpleType[]
50    {
51       SimpleType.BIGDECIMAL,
52       SimpleType.BIGINTEGER,
53       SimpleType.BOOLEAN,
54       SimpleType.BYTE,
55       SimpleType.CHARACTER,
56       SimpleType.DATE,
57       SimpleType.DOUBLE,
58       SimpleType.FLOAT,
59       SimpleType.INTEGER,
60       SimpleType.LONG,
61       SimpleType.OBJECTNAME,
62       SimpleType.SHORT,
63       SimpleType.STRING,
64       SimpleType.VOID
65    };
66
67    Class[] classes = new Class[]
68    {
69       BigDecimal.class,
70       BigInteger.class,
71       Boolean.class,
72       Byte.class,
73       Character.class,
74       Date.class,
75       Double.class,
76       Float.class,
77       Integer.class,
78       Long.class,
79       ObjectName.class,
80       Short.class,
81       String.class,
82       Void.class
83    };
84
85    Object[] objects = new Object[]
86    {
87       new BigDecimal(1),
88       BigInteger.ONE,
89       new Boolean(false),
90       new Byte(Byte.MAX_VALUE),
91       new Character('a'),
92       new Date(System.currentTimeMillis()),
93       new Double(1),
94       new Float(1),
95       new Integer(1),
96       new Long(1),
97       objectName,
98       new Short(Short.MAX_VALUE),
99       new String("hello"),
100       null
101    };
102
103    // Constructor ---------------------------------------------------------------
104

105    /**
106     * Construct the test
107     */

108    public SimpleTypeTestCase(String s)
109    {
110       super(s);
111    }
112
113    // Tests ---------------------------------------------------------------------
114

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