KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Arrays;
17
18 import javax.management.MBeanConstructorInfo;
19 import javax.management.MBeanParameterInfo;
20
21 /**
22  * MBean Parameter Info tests.<p>
23  *
24  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
25  */

26 public class MBeanConstructorInfoTEST
27   extends TestCase
28 {
29    // Static --------------------------------------------------------------------
30

31    MBeanParameterInfo[] params1 = new MBeanParameterInfo[]
32    {
33       new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
34       new MBeanParameterInfo("BarParam", "java.lang.String", "description")
35    };
36    MBeanParameterInfo[] params2 = new MBeanParameterInfo[]
37    {
38       new MBeanParameterInfo("FooParam", "java.lang.Character", "description"),
39       new MBeanParameterInfo("BarParam", "java.lang.String", "description")
40    };
41
42    // Attributes ----------------------------------------------------------------
43

44    // Constructor ---------------------------------------------------------------
45

46    /**
47     * Construct the test
48     */

49    public MBeanConstructorInfoTEST(String s)
50    {
51       super(s);
52    }
53
54    // Tests ---------------------------------------------------------------------
55

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