KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > relation > RoleTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.relation;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.relation.Role JavaDoc;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Role tests.<p>
39  *
40  * Test it to death.<p>
41  *
42  * NOTE: The tests use String literals to ensure the comparisons are
43  * not performed on object references.
44  *
45  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
46  */

47 public class RoleTestCase
48   extends TestCase
49 {
50   // Attributes ----------------------------------------------------------------
51

52   // Constructor ---------------------------------------------------------------
53

54   /**
55    * Construct the test
56    */

57   public RoleTestCase(String JavaDoc s)
58   {
59     super(s);
60   }
61
62   // Tests ---------------------------------------------------------------------
63

64   /**
65    * Basic tests.
66    */

67   public void testBasic()
68   {
69     try
70     {
71       // Create the role
72
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
73       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
74       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
75       roleValue.add(a);
76       roleValue.add(b);
77       Role JavaDoc role = new Role JavaDoc("RoleName", roleValue);
78
79       // Check the role name
80
assertEquals("RoleName", role.getRoleName());
81
82       // Check the role value
83
assertEquals(roleValue, role.getRoleValue());
84
85       // Change the role name
86
role.setRoleName("Changed");
87       assertEquals("Changed", role.getRoleName());
88
89       // Change the role value
90
ArrayList JavaDoc roleValue2 = new ArrayList JavaDoc();
91       ObjectName JavaDoc c = new ObjectName JavaDoc(":c=c");
92       ObjectName JavaDoc d = new ObjectName JavaDoc(":d=d");
93       roleValue2.add(c);
94       roleValue2.add(d);
95       role.setRoleValue(roleValue2);
96
97       // Check the new role value
98
assertEquals(roleValue2, role.getRoleValue());
99     }
100     catch (MalformedObjectNameException JavaDoc mfone)
101     {
102       fail(mfone.toString());
103     }
104   }
105
106   /**
107    * toString tests.
108    */

109   public void testToString()
110   {
111     try
112     {
113       // Create the role
114
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
115       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
116       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
117       roleValue.add(a);
118       roleValue.add(b);
119       Role JavaDoc role = new Role JavaDoc("XYZZY", roleValue);
120
121       // Check the value formatter
122
String JavaDoc result = Role.roleValueToString(roleValue);
123       if (result.lastIndexOf(":a=a") == -1)
124         fail("Missing object name :a=a in roleValueToString");
125       if (result.lastIndexOf(":b=b") == -1)
126         fail("Missing object name :b=b in roleValueToString");
127
128       // Check the human readable string
129
result = role.toString();
130       if (result.lastIndexOf("XYZZY") == -1)
131         fail("Missing role name in toString");
132       if (result.lastIndexOf(":a=a") == -1)
133         fail("Missing object name :a=a in toString");
134       if (result.lastIndexOf(":b=b") == -1)
135         fail("Missing object name :b=b in toString");
136     }
137     catch (MalformedObjectNameException JavaDoc mfone)
138     {
139       fail(mfone.toString());
140     }
141   }
142
143   /**
144    * Test Error Handling.
145    */

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

233   public void testClone()
234   {
235     // Create the role
236
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
237     try
238     {
239       roleValue.add(new ObjectName JavaDoc(":a=a"));
240       roleValue.add(new ObjectName JavaDoc(":b=b"));
241     }
242     catch (MalformedObjectNameException JavaDoc mfone)
243     {
244       fail(mfone.toString());
245     }
246     Role JavaDoc role = new Role JavaDoc("RoleName", roleValue);
247     Role JavaDoc role2 = (Role JavaDoc) role.clone();
248
249     // Did it work?
250
assertEquals(role.getRoleName(), role2.getRoleName());
251     assertEquals(role.getRoleValue(), role2.getRoleValue());
252     if(role.getRoleValue() == role2.getRoleValue())
253       fail("Role.clone() didn't clone");
254   }
255
256   /**
257    * Test serialization.
258    */

259   public void testSerialization()
260   {
261     // Create the role
262
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
263     try
264     {
265       roleValue.add(new ObjectName JavaDoc(":a=a"));
266       roleValue.add(new ObjectName JavaDoc(":b=b"));
267     }
268     catch (MalformedObjectNameException JavaDoc mfone)
269     {
270       fail(mfone.toString());
271     }
272     Role JavaDoc role = new Role JavaDoc("RoleName", roleValue);
273     Role JavaDoc role2 = null;
274
275     try
276     {
277       // Serialize it
278
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
279       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
280       oos.writeObject(role);
281     
282       // Deserialize it
283
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
284       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
285       role2 = (Role JavaDoc) ois.readObject();
286     }
287     catch (IOException JavaDoc ioe)
288     {
289       fail(ioe.toString());
290     }
291     catch (ClassNotFoundException JavaDoc cnfe)
292     {
293       fail(cnfe.toString());
294     }
295
296     // Did it work?
297
assertEquals(role.getRoleName(), role2.getRoleName());
298     assertEquals(role.getRoleValue(), role2.getRoleValue());
299   }
300 }
301
Popular Tags