KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
31
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.relation.Role JavaDoc;
35 import javax.management.relation.RoleList JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Role lists tests.<p>
41  *
42  * Test it to death.<p>
43  *
44  * NOTE: The tests use String literals to ensure the comparisons are
45  * not performed on object references.
46  *
47  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
48  */

49 public class RoleListTestCase
50   extends TestCase
51 {
52   // Attributes ----------------------------------------------------------------
53

54   // Roles used in testing
55
boolean setUpDone = false;
56   Role JavaDoc role1;
57   Role JavaDoc role2;
58
59   // Constructor ---------------------------------------------------------------
60

61   /**
62    * Construct the test
63    */

64   public RoleListTestCase(String JavaDoc s)
65   {
66     super(s);
67   }
68
69   // Tests ---------------------------------------------------------------------
70

71   /**
72    * Empty Constructors.
73    */

74   public void testEmptyConstructors()
75   {
76     setUpRoles();
77
78     // Empty lists
79
RoleList JavaDoc empty = new RoleList JavaDoc();
80     assertEquals(0, empty.size());
81     empty = new RoleList JavaDoc(100);
82     assertEquals(0, empty.size());
83   }
84
85   /**
86    * Basic constructor test.
87    */

88   public void testBasicConstructor()
89   {
90     setUpRoles();
91
92     ArrayList JavaDoc roles = new ArrayList JavaDoc();
93     roles.add(role1);
94     roles.add(role2);
95     RoleList JavaDoc full = new RoleList JavaDoc(roles);
96     assertEquals(2, full.size());
97     assertEquals(role1, full.get(0));
98     assertEquals(role2, full.get(1));
99     Iterator JavaDoc iterator = full.iterator();
100     assertEquals(role1, iterator.next());
101     assertEquals(role2, iterator.next());
102   }
103
104   /**
105    * Basic constructor test, ordering. Do it backwards
106    */

107   public void testBasicConstructorOrdering()
108   {
109     setUpRoles();
110
111     ArrayList JavaDoc roles = new ArrayList JavaDoc();
112     roles.add(role2);
113     roles.add(role1);
114     RoleList JavaDoc full = new RoleList JavaDoc(roles);
115     assertEquals(2, full.size());
116     assertEquals(role2, full.get(0));
117     assertEquals(role1, full.get(1));
118     Iterator JavaDoc iterator = full.iterator();
119     assertEquals(role2, iterator.next());
120     assertEquals(role1, iterator.next());
121   }
122
123   /**
124    * Basic constructor test, allows duplicates
125    */

126   public void testBasicConstructorDuplicates()
127   {
128     setUpRoles();
129
130     // Check duplicates allowed
131
ArrayList JavaDoc roles = new ArrayList JavaDoc();
132     roles.add(role1);
133     roles.add(role1);
134     RoleList JavaDoc full = new RoleList JavaDoc(roles);
135     assertEquals(2, full.size());
136     assertEquals(role1, full.get(0));
137     assertEquals(role1, full.get(1));
138     Iterator JavaDoc iterator = full.iterator();
139     assertEquals(role1, iterator.next());
140     assertEquals(role1, iterator.next());
141   }
142
143   /**
144    * Test Error Handling.
145    */

146   public void testErrorHandling()
147   {
148     setUpRoles();
149
150     // Shouldn't allow new roles
151
ArrayList JavaDoc roles = new ArrayList JavaDoc();
152     roles.add(role1);
153     roles.add(null);
154
155     // Shouldn't allow null for the name in constructor
156
boolean caught = false;
157     try
158     {
159       new RoleList JavaDoc(roles);
160     }
161     catch (IllegalArgumentException JavaDoc e)
162     {
163       caught = true;
164     }
165     if (caught == false)
166       fail ("Constructor accepts null roles");
167
168     // Should only allow roles
169
roles = new ArrayList JavaDoc();
170     roles.add(role1);
171     roles.add(new Object JavaDoc());
172     caught = false;
173     try
174     {
175       new RoleList JavaDoc(roles);
176     }
177     catch (IllegalArgumentException JavaDoc e)
178     {
179       caught = true;
180     }
181     if (caught == false)
182       fail ("Constructor accepts non roles");
183   }
184
185   /**
186    * Single Append tests.
187    */

188   public void testSingleAppend()
189   {
190     setUpRoles();
191
192     // Simple add
193
RoleList JavaDoc list = new RoleList JavaDoc();
194     list.add(role1);
195     assertEquals(1, list.size());
196     assertEquals(role1.toString(), list.get(0).toString());
197     Iterator JavaDoc iterator = list.iterator();
198     assertEquals(role1.toString(), iterator.next().toString());
199
200     // Once more for luck, should append
201
list.add(role2);
202     assertEquals(2, list.size());
203     assertEquals(role1.toString(), list.get(0).toString());
204     assertEquals(role2.toString(), list.get(1).toString());
205     iterator = list.iterator();
206     assertEquals(role1.toString(), iterator.next().toString());
207     assertEquals(role2.toString(), iterator.next().toString());
208
209     // Add a null, shouldn't work
210
boolean caught = false;
211     try
212     {
213       list.add(null);
214     }
215     catch (IllegalArgumentException JavaDoc e)
216     {
217       caught = true;
218     }
219     if (caught == false)
220       fail ("addRole(null) shouldn't work");
221   }
222
223   /**
224    * Add single
225    */

226   public void testSingleAdd()
227   {
228     setUpRoles();
229
230     // Set up a role list
231
RoleList JavaDoc list = new RoleList JavaDoc();
232     list.add(role1);
233     list.add(role2);
234
235     // Add one
236
list.add(1, role1);
237     assertEquals(3, list.size());
238     assertEquals(role1.toString(), list.get(0).toString());
239     assertEquals(role1.toString(), list.get(1).toString());
240     assertEquals(role2.toString(), list.get(2).toString());
241     Iterator JavaDoc iterator = list.iterator();
242     assertEquals(role1.toString(), iterator.next().toString());
243     assertEquals(role1.toString(), iterator.next().toString());
244     assertEquals(role2.toString(), iterator.next().toString());
245
246     // Add a role in the wrong place
247
boolean caught = false;
248     try
249     {
250       list.add(4, role1);
251     }
252     catch (IndexOutOfBoundsException JavaDoc e)
253     {
254       caught = true;
255     }
256     if (caught == false)
257       fail ("Shouldn't be able to add a role outside of valid range");
258
259     // Add a null should not work
260
caught = false;
261     try
262     {
263       list.add(1, null);
264     }
265     catch (IllegalArgumentException JavaDoc e)
266     {
267       caught = true;
268     }
269     if (caught == false)
270       fail ("Shouldn't be able to add a null at an index");
271   }
272
273   /**
274    * Set single
275    */

276   public void testSingleSet()
277   {
278     setUpRoles();
279
280     // Set up a role list
281
RoleList JavaDoc list = new RoleList JavaDoc();
282     list.add(role1);
283     list.add(role2);
284
285     // Add one
286
list.set(1, role1);
287     assertEquals(2, list.size());
288     assertEquals(role1.toString(), list.get(0).toString());
289     assertEquals(role1.toString(), list.get(1).toString());
290     Iterator JavaDoc iterator = list.iterator();
291     assertEquals(role1.toString(), iterator.next().toString());
292     assertEquals(role1.toString(), iterator.next().toString());
293
294     // Add a role in the wrong place
295
boolean caught = false;
296     try
297     {
298       list.set(4, role1);
299     }
300     catch (IndexOutOfBoundsException JavaDoc e)
301     {
302       caught = true;
303     }
304     if (caught == false)
305       fail ("Shouldn't be able to set a role outside of valid range");
306
307     // set a null should not work
308
caught = false;
309     try
310     {
311       list.add(1, null);
312     }
313     catch (IllegalArgumentException JavaDoc e)
314     {
315       caught = true;
316     }
317     if (caught == false)
318       fail ("Shouldn't be able to set a null at an index");
319   }
320
321   /**
322    * Add multiple
323    */

324   public void testMultipleAdd()
325   {
326     setUpRoles();
327
328     // Set up a role list
329
RoleList JavaDoc list = new RoleList JavaDoc();
330     list.add(role1);
331     list.add(role1);
332     RoleList JavaDoc listToAdd = new RoleList JavaDoc();
333     listToAdd.add(role2);
334     listToAdd.add(role2);
335
336     // Add all
337
list.addAll(listToAdd);
338     assertEquals(4, list.size());
339     assertEquals(role1.toString(), list.get(0).toString());
340     assertEquals(role1.toString(), list.get(1).toString());
341     assertEquals(role2.toString(), list.get(2).toString());
342     assertEquals(role2.toString(), list.get(3).toString());
343     Iterator JavaDoc iterator = list.iterator();
344     assertEquals(role1.toString(), iterator.next().toString());
345     assertEquals(role1.toString(), iterator.next().toString());
346     assertEquals(role2.toString(), iterator.next().toString());
347     assertEquals(role2.toString(), iterator.next().toString());
348
349     // Add a null should work (not very standard)
350
boolean caught = false;
351     try
352     {
353       list.addAll(null);
354     }
355     catch (Exception JavaDoc e)
356     {
357       caught = true;
358     }
359     if (caught == true)
360       fail ("Should be able to addAll a null");
361   }
362
363   /**
364    * Add multiple at a location
365    */

366   public void testMultipleLocationAdd()
367   {
368     setUpRoles();
369
370     // Set up a role list
371
RoleList JavaDoc list = new RoleList JavaDoc();
372     list.add(role1);
373     list.add(role1);
374     RoleList JavaDoc listToAdd = new RoleList JavaDoc();
375     listToAdd.add(role2);
376     listToAdd.add(role2);
377
378     // Add all
379
list.addAll(1, listToAdd);
380     assertEquals(4, list.size());
381     assertEquals(role1.toString(), list.get(0).toString());
382     assertEquals(role2.toString(), list.get(1).toString());
383     assertEquals(role2.toString(), list.get(2).toString());
384     assertEquals(role1.toString(), list.get(3).toString());
385     Iterator JavaDoc iterator = list.iterator();
386     assertEquals(role1.toString(), iterator.next().toString());
387     assertEquals(role2.toString(), iterator.next().toString());
388     assertEquals(role2.toString(), iterator.next().toString());
389     assertEquals(role1.toString(), iterator.next().toString());
390
391     // Add a role in the wrong place
392
boolean caught = false;
393     try
394     {
395       list.addAll(6, listToAdd);
396     }
397     catch (IndexOutOfBoundsException JavaDoc e)
398     {
399       caught = true;
400     }
401     if (caught == false)
402       fail ("Shouldn't be able to addAll a role outside of valid range");
403
404     // Add a null should not work
405
caught = false;
406     try
407     {
408       list.addAll(1, null);
409     }
410     catch (IllegalArgumentException JavaDoc e)
411     {
412       caught = true;
413     }
414     if (caught == false)
415       fail ("Shouldn't be able to addAll a null at an index");
416   }
417
418   /**
419    * Test clone.
420    */

421   public void testClone()
422   {
423     setUpRoles();
424
425     ArrayList JavaDoc roles = new ArrayList JavaDoc();
426     roles.add(role1);
427     roles.add(role2);
428     RoleList JavaDoc full = new RoleList JavaDoc(roles);
429     RoleList JavaDoc clone = (RoleList JavaDoc) full.clone();
430     assertEquals(2, clone.size());
431     assertEquals(role1.toString(), clone.get(0).toString());
432     assertEquals(role2.toString(), clone.get(1).toString());
433     Iterator JavaDoc iterator = clone.iterator();
434     assertEquals(role1.toString(), iterator.next().toString());
435     assertEquals(role2.toString(), iterator.next().toString());
436   }
437
438   /**
439    * Test serialization.
440    */

441   public void testSerialization()
442   {
443     setUpRoles();
444
445     ArrayList JavaDoc roles = new ArrayList JavaDoc();
446     roles.add(role1);
447     roles.add(role2);
448     RoleList JavaDoc full = new RoleList JavaDoc(roles);
449     RoleList JavaDoc copy = null;
450
451     try
452     {
453       // Serialize it
454
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
455       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
456       oos.writeObject(full);
457     
458       // Deserialize it
459
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
460       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
461       copy = (RoleList JavaDoc) ois.readObject();
462     }
463     catch (IOException JavaDoc ioe)
464     {
465       fail(ioe.toString());
466     }
467     catch (ClassNotFoundException JavaDoc cnfe)
468     {
469       fail(cnfe.toString());
470     }
471
472     // Did it work?
473
assertEquals(2, copy.size());
474     assertEquals(role1.toString(), copy.get(0).toString());
475     assertEquals(role2.toString(), copy.get(1).toString());
476     Iterator JavaDoc iterator = copy.iterator();
477     assertEquals(role1.toString(), iterator.next().toString());
478     assertEquals(role2.toString(), iterator.next().toString());
479   }
480
481   // Tests ---------------------------------------------------------------------
482

483   private void setUpRoles()
484   {
485     if (setUpDone == true)
486       return;
487     try
488     {
489       // Create the roles
490
ArrayList JavaDoc roleValue1 = new ArrayList JavaDoc();
491       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
492       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
493       roleValue1.add(a);
494       roleValue1.add(b);
495       role1 = new Role JavaDoc("RoleName1", roleValue1);
496
497       ArrayList JavaDoc roleValue2 = new ArrayList JavaDoc();
498       ObjectName JavaDoc c = new ObjectName JavaDoc(":c=c");
499       ObjectName JavaDoc d = new ObjectName JavaDoc(":d=d");
500       roleValue2.add(c);
501       roleValue2.add(d);
502       role2 = new Role JavaDoc("RoleName2", roleValue2);
503     }
504     catch (MalformedObjectNameException JavaDoc mfone)
505     {
506       fail(mfone.toString());
507     }
508     setUpDone = true;
509   }
510 }
511
Popular Tags