KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > relation > RoleUnresolvedTestCase


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.relation;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.ObjectInputStream;
14 import java.io.ObjectOutputStream;
15
16 import java.util.ArrayList;
17
18 import javax.management.ObjectName;
19 import javax.management.MalformedObjectNameException;
20 import javax.management.relation.RoleStatus;
21 import javax.management.relation.RoleUnresolved;
22
23 import junit.framework.TestCase;
24
25 /**
26  * Role Unresolved tests.<p>
27  *
28  * Test it to death.<p>
29  *
30  * NOTE: The tests use String literals to ensure the comparisons are
31  * not performed on object references.
32  *
33  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
34  */

35 public class RoleUnresolvedTestCase
36   extends TestCase
37 {
38   // Attributes ----------------------------------------------------------------
39

40   // Constructor ---------------------------------------------------------------
41

42   /**
43    * Construct the test
44    */

45   public RoleUnresolvedTestCase(String s)
46   {
47     super(s);
48   }
49
50   // Tests ---------------------------------------------------------------------
51

52   /**
53    * Basic tests.
54    */

55   public void testBasic()
56   {
57     try
58     {
59       // Create the role Unresolved
60
ArrayList roleValue = new ArrayList();
61       ObjectName a = new ObjectName(":a=a");
62       ObjectName b = new ObjectName(":b=b");
63       roleValue.add(a);
64       roleValue.add(b);
65       RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue,
66                                              RoleStatus.NO_ROLE_WITH_NAME);
67
68       // Check the role name
69
assertEquals("RoleName", roleUnresolved.getRoleName());
70
71       // Check the role value
72
assertEquals(roleValue, roleUnresolved.getRoleValue());
73
74       // Check the problem type
75
assertEquals(RoleStatus.NO_ROLE_WITH_NAME, roleUnresolved.getProblemType());
76
77       // Change the role name
78
roleUnresolved.setRoleName("Changed");
79       assertEquals("Changed", roleUnresolved.getRoleName());
80
81       // Change the role value
82
ArrayList roleValue2 = new ArrayList();
83       ObjectName c = new ObjectName(":c=c");
84       ObjectName d = new ObjectName(":d=d");
85       roleValue2.add(c);
86       roleValue2.add(d);
87       roleUnresolved.setRoleValue(roleValue2);
88
89       // Check the new role value
90
assertEquals(roleValue2, roleUnresolved.getRoleValue());
91
92       // Check the problem type
93
roleUnresolved.setProblemType(RoleStatus.ROLE_NOT_READABLE);
94       assertEquals(RoleStatus.ROLE_NOT_READABLE, roleUnresolved.getProblemType());
95     }
96     catch (MalformedObjectNameException mfone)
97     {
98       fail(mfone.toString());
99     }
100   }
101
102   /**
103    * toString tests.
104    */

105   public void testToString()
106   {
107     try
108     {
109       // Create the role
110
ArrayList roleValue = new ArrayList();
111       ObjectName a = new ObjectName(":a=a");
112       ObjectName b = new ObjectName(":b=b");
113       roleValue.add(a);
114       roleValue.add(b);
115       RoleUnresolved roleUnresolved = new RoleUnresolved("XYZZY", roleValue,
116                                              RoleStatus.NO_ROLE_WITH_NAME);
117
118       // Check the human readable string
119
String result = roleUnresolved.toString();
120       if (result.lastIndexOf("XYZZY") == -1)
121         fail("Missing role name in toString");
122       if (result.lastIndexOf(":a=a") == -1)
123         fail("Missing object name :a=a in toString");
124       if (result.lastIndexOf(":b=b") == -1)
125         fail("Missing object name :b=b in toString");
126
127       // TODO How to test the problem type the string isn't specified?
128
}
129     catch (MalformedObjectNameException mfone)
130     {
131       fail(mfone.toString());
132     }
133   }
134
135   /**
136    * Test Error Handling.
137    */

138   public void testErrorHandling()
139   {
140     try
141     {
142       // Create the role
143
ArrayList roleValue = new ArrayList();
144       ObjectName a = new ObjectName(":a=a");
145       ObjectName b = new ObjectName(":b=b");
146       roleValue.add(a);
147       roleValue.add(b);
148
149       // Shouldn't allow null for the name in constructor
150
boolean caught = false;
151       try
152       {
153         new RoleUnresolved(null, roleValue, RoleStatus.NO_ROLE_WITH_NAME);
154       }
155       catch (IllegalArgumentException e)
156       {
157         caught = true;
158       }
159       if (caught == false)
160         fail ("Constructor accepts null role name");
161
162       // Shouldn't allow an invalid problem type
163
caught = false;
164       try
165       {
166         new RoleUnresolved("RoleName", roleValue, -1000);
167       }
168       catch (IllegalArgumentException e)
169       {
170         caught = true;
171       }
172       if (caught == false)
173         fail ("Constructor accepts an invalid problem type");
174
175       RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue,
176                                            RoleStatus.NO_ROLE_WITH_NAME);
177
178       // Shouldn't allow null for name
179
caught = false;
180       try
181       {
182         roleUnresolved.setRoleName(null);
183       }
184       catch (IllegalArgumentException e)
185       {
186         caught = true;
187       }
188       if (caught == false)
189         fail ("setRoleName accepts null");
190
191       // Shouldn't allow an invalid problem type
192
caught = false;
193       try
194       {
195         roleUnresolved.setProblemType(-1000);
196       }
197       catch (IllegalArgumentException e)
198       {
199         caught = true;
200       }
201       if (caught == false)
202         fail ("setProblemType accepts an invalid problem type");
203     }
204     catch (MalformedObjectNameException mfone)
205     {
206       fail(mfone.toString());
207     }
208   }
209
210   /**
211    * Test clone.
212    */

213   public void testClone()
214   {
215     // Create the role
216
ArrayList roleValue = new ArrayList();
217     try
218     {
219       roleValue.add(new ObjectName(":a=a"));
220       roleValue.add(new ObjectName(":b=b"));
221     }
222     catch (MalformedObjectNameException mfone)
223     {
224       fail(mfone.toString());
225     }
226     RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue,
227                                           RoleStatus.NO_ROLE_WITH_NAME);
228     RoleUnresolved roleUnresolved2 = (RoleUnresolved) roleUnresolved.clone();
229
230     // Did it work?
231
assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName());
232     assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue());
233     assertEquals(roleUnresolved.getProblemType(), roleUnresolved2.getProblemType());
234     if(roleUnresolved.getRoleValue() == roleUnresolved2.getRoleValue())
235       fail("RoleUnresolved.clone() didn't clone");
236   }
237
238   /**
239    * Test serialization.
240    */

241   public void testSerialization()
242   {
243     // Create the role
244
ArrayList roleValue = new ArrayList();
245     try
246     {
247       roleValue.add(new ObjectName(":a=a"));
248       roleValue.add(new ObjectName(":b=b"));
249     }
250     catch (MalformedObjectNameException mfone)
251     {
252       fail(mfone.toString());
253     }
254     RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue,
255                                           RoleStatus.NO_ROLE_WITH_NAME);
256     RoleUnresolved roleUnresolved2 = null;
257
258     try
259     {
260       // Serialize it
261
ByteArrayOutputStream baos = new ByteArrayOutputStream();
262       ObjectOutputStream oos = new ObjectOutputStream(baos);
263       oos.writeObject(roleUnresolved);
264     
265       // Deserialize it
266
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
267       ObjectInputStream ois = new ObjectInputStream(bais);
268       roleUnresolved2 = (RoleUnresolved) ois.readObject();
269     }
270     catch (IOException ioe)
271     {
272       fail(ioe.toString());
273     }
274     catch (ClassNotFoundException cnfe)
275     {
276       fail(cnfe.toString());
277     }
278
279     // Did it work?
280
assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName());
281     assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue());
282   }
283 }
284
Popular Tags