KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > metadata > MBeanParameterInfoTEST


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.metadata;
9
10 import junit.framework.TestCase;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16
17 import javax.management.MBeanParameterInfo;
18
19 /**
20  * MBean Parameter Info tests.<p>
21  *
22  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
23  */

24 public class MBeanParameterInfoTEST
25   extends TestCase
26 {
27    // Static --------------------------------------------------------------------
28

29    // Attributes ----------------------------------------------------------------
30

31    // Constructor ---------------------------------------------------------------
32

33    /**
34     * Construct the test
35     */

36    public MBeanParameterInfoTEST(String s)
37    {
38       super(s);
39    }
40
41    // Tests ---------------------------------------------------------------------
42

43    public void testMBeanParameterInfo()
44       throws Exception
45    {
46       MBeanParameterInfo info = new MBeanParameterInfo(
47          "name", "type", "description");
48       assertEquals("name", info.getName());
49       assertEquals("type", info.getType());
50       assertEquals("description", info.getDescription());
51    }
52
53    public void testHashCode()
54       throws Exception
55    {
56       MBeanParameterInfo info1 = new MBeanParameterInfo("name", "type", "description");
57       MBeanParameterInfo info2 = new MBeanParameterInfo("name", "type", "description");
58
59       assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode());
60    }
61
62    public void testEquals()
63       throws Exception
64    {
65       MBeanParameterInfo info = new MBeanParameterInfo(
66          "name", "type", "description");
67
68       assertTrue("Null should not be equal", info.equals(null) == false);
69       assertTrue("Only MBeanParameterInfo should be equal", info.equals(new Object()) == false);
70
71       MBeanParameterInfo info2 = new MBeanParameterInfo(
72          "name", "type", "description");
73
74       assertTrue("Different instances of the same data are equal", info.equals(info2));
75       assertTrue("Different instances of the same data are equal", info2.equals(info));
76
77       info2 = new MBeanParameterInfo(
78          "name", "type", "description2");
79
80       assertTrue("Different instances with different descriptions are not equal", info.equals(info2) == false);
81       assertTrue("Different instances with different descritpions are not equal", info2.equals(info) == false);
82
83       info2 = new MBeanParameterInfo(
84          "name2", "type", "description");
85
86       assertTrue("Instances with different names are not equal", info.equals(info2) == false);
87       assertTrue("Instances with different names are not equal", info2.equals(info) == false);
88
89       info2 = new MBeanParameterInfo(
90          "name", "type2", "description");
91
92       assertTrue("Instances with different types are not equal", info.equals(info2) == false);
93       assertTrue("Instances with different types are not equal", info2.equals(info) == false);
94    }
95
96    public void testSerialization()
97       throws Exception
98    {
99       MBeanParameterInfo info = new MBeanParameterInfo(
100          "name", "type", "description");
101
102       // Serialize it
103
ByteArrayOutputStream baos = new ByteArrayOutputStream();
104       ObjectOutputStream oos = new ObjectOutputStream(baos);
105       oos.writeObject(info);
106     
107       // Deserialize it
108
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
109       ObjectInputStream ois = new ObjectInputStream(bais);
110       Object result = ois.readObject();
111
112       assertEquals(info, result);
113    }
114
115    public void testErrors()
116       throws Exception
117    {
118       boolean caught = false;
119       try
120       {
121          MBeanParameterInfo info = new MBeanParameterInfo(
122             null, "type", "description");
123       }
124       catch (IllegalArgumentException e)
125       {
126          caught = true;
127       }
128       if (caught == false)
129          fail("Expected IllegalArgumentException for null name");
130
131       caught = false;
132       try
133       {
134          MBeanParameterInfo info = new MBeanParameterInfo(
135             "", "type", "description");
136       }
137       catch (IllegalArgumentException e)
138       {
139          caught = true;
140       }
141       if (caught == false)
142          fail("Expected IllegalArgumentException for an empty name");
143
144       caught = false;
145       try
146       {
147          MBeanParameterInfo info = new MBeanParameterInfo(
148             "invalid name", "type", "description");
149       }
150       catch (IllegalArgumentException e)
151       {
152          caught = true;
153       }
154       if (caught == false)
155          fail("Expected IllegalArgumentException for an 'invalid name'");
156
157       caught = false;
158       try
159       {
160          MBeanParameterInfo info = new MBeanParameterInfo(
161             "name", null, "description");
162       }
163       catch (IllegalArgumentException e)
164       {
165          caught = true;
166       }
167       if (caught == false)
168          fail("Expected IllegalArgumentException for null type");
169
170       caught = false;
171       try
172       {
173          MBeanParameterInfo info = new MBeanParameterInfo(
174             "name", "", "description");
175       }
176       catch (IllegalArgumentException e)
177       {
178          caught = true;
179       }
180       if (caught == false)
181          fail("Expected IllegalArgumentException for an empty type");
182
183       caught = false;
184       try
185       {
186          MBeanParameterInfo info = new MBeanParameterInfo(
187             "name", "invalid type", "description");
188       }
189       catch (IllegalArgumentException e)
190       {
191          caught = true;
192       }
193       if (caught == false)
194          fail("Expected IllegalArgumentException for an 'invalid type'");
195    }
196 }
197
Popular Tags