KickJava   Java API By Example, From Geeks To Geeks.

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


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.Role;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Role tests.<p>
26  *
27  * Test it to death.<p>
28  *
29  * NOTE: The tests use String literals to ensure the comparisons are
30  * not performed on object references.
31  *
32  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
33  */

34 public class RoleTestCase
35   extends TestCase
36 {
37   // Attributes ----------------------------------------------------------------
38

39   // Constructor ---------------------------------------------------------------
40

41   /**
42    * Construct the test
43    */

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

51   /**
52    * Basic tests.
53    */

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

96   public void testToString()
97   {
98     try
99     {
100       // Create the role
101
ArrayList roleValue = new ArrayList();
102       ObjectName a = new ObjectName(":a=a");
103       ObjectName b = new ObjectName(":b=b");
104       roleValue.add(a);
105       roleValue.add(b);
106       Role role = new Role("XYZZY", roleValue);
107
108       // Check the value formatter
109
String result = Role.roleValueToString(roleValue);
110       if (result.lastIndexOf(":a=a") == -1)
111         fail("Missing object name :a=a in roleValueToString");
112       if (result.lastIndexOf(":b=b") == -1)
113         fail("Missing object name :b=b in roleValueToString");
114
115       // Check the human readable string
116
result = role.toString();
117       if (result.lastIndexOf("XYZZY") == -1)
118         fail("Missing role name in toString");
119       if (result.lastIndexOf(":a=a") == -1)
120         fail("Missing object name :a=a in toString");
121       if (result.lastIndexOf(":b=b") == -1)
122         fail("Missing object name :b=b in toString");
123     }
124     catch (MalformedObjectNameException mfone)
125     {
126       fail(mfone.toString());
127     }
128   }
129
130   /**
131    * Test Error Handling.
132    */

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

220   public void testClone()
221   {
222     // Create the role
223
ArrayList roleValue = new ArrayList();
224     try
225     {
226       roleValue.add(new ObjectName(":a=a"));
227       roleValue.add(new ObjectName(":b=b"));
228     }
229     catch (MalformedObjectNameException mfone)
230     {
231       fail(mfone.toString());
232     }
233     Role role = new Role("RoleName", roleValue);
234     Role role2 = (Role) role.clone();
235
236     // Did it work?
237
assertEquals(role.getRoleName(), role2.getRoleName());
238     assertEquals(role.getRoleValue(), role2.getRoleValue());
239     if(role.getRoleValue() == role2.getRoleValue())
240       fail("Role.clone() didn't clone");
241   }
242
243   /**
244    * Test serialization.
245    */

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