KickJava   Java API By Example, From Geeks To Geeks.

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


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

50 public class RoleUnresolvedListTestCase
51   extends TestCase
52 {
53   // Attributes ----------------------------------------------------------------
54

55   // Role Unresolveds used in testing
56
boolean setUpDone = false;
57   RoleUnresolved JavaDoc roleUnresolved1;
58   RoleUnresolved JavaDoc roleUnresolved2;
59
60   // Constructor ---------------------------------------------------------------
61

62   /**
63    * Construct the test
64    */

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

72   /**
73    * Empty Constructors.
74    */

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

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

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

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

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

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

227   public void testSingleAdd()
228   {
229     setUpRoleUnresolveds();
230
231     RoleUnresolvedList JavaDoc list = new RoleUnresolvedList JavaDoc();
232     list.add(roleUnresolved1);
233     list.add(roleUnresolved2);
234
235     // Add one
236
list.add(1, roleUnresolved1);
237     assertEquals(3, list.size());
238     assertEquals(roleUnresolved1.toString(), list.get(0).toString());
239     assertEquals(roleUnresolved1.toString(), list.get(1).toString());
240     assertEquals(roleUnresolved2.toString(), list.get(2).toString());
241     Iterator JavaDoc iterator = list.iterator();
242     assertEquals(roleUnresolved1.toString(), iterator.next().toString());
243     assertEquals(roleUnresolved1.toString(), iterator.next().toString());
244     assertEquals(roleUnresolved2.toString(), iterator.next().toString());
245
246     // Add a roleUnresolved in the wrong place
247
boolean caught = false;
248     try
249     {
250       list.add(4, roleUnresolved1);
251     }
252     catch (IndexOutOfBoundsException JavaDoc e)
253     {
254       caught = true;
255     }
256     if (caught == false)
257       fail ("Shouldn't be able to add a roleUnresolved 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     setUpRoleUnresolveds();
279
280     RoleUnresolvedList JavaDoc list = new RoleUnresolvedList JavaDoc();
281     list.add(roleUnresolved1);
282     list.add(roleUnresolved2);
283
284     // Add one
285
list.set(1, roleUnresolved1);
286     assertEquals(2, list.size());
287     assertEquals(roleUnresolved1.toString(), list.get(0).toString());
288     assertEquals(roleUnresolved1.toString(), list.get(1).toString());
289     Iterator JavaDoc iterator = list.iterator();
290     assertEquals(roleUnresolved1.toString(), iterator.next().toString());
291     assertEquals(roleUnresolved1.toString(), iterator.next().toString());
292
293     // Add a role Unresolved in the wrong place
294
boolean caught = false;
295     try
296     {
297       list.set(4, roleUnresolved1);
298     }
299     catch (IndexOutOfBoundsException JavaDoc e)
300     {
301       caught = true;
302     }
303     if (caught == false)
304       fail ("Shouldn't be able to set a roleUnresolved outside of valid range");
305
306     // set a null should not work
307
caught = false;
308     try
309     {
310       list.add(1, null);
311     }
312     catch (IllegalArgumentException JavaDoc e)
313     {
314       caught = true;
315     }
316     if (caught == false)
317       fail ("Shouldn't be able to set a null at an index");
318   }
319
320   /**
321    * Add multiple
322    */

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

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

418   public void testClone()
419   {
420     setUpRoleUnresolveds();
421
422     try
423     {
424       ArrayList JavaDoc roleUnresolveds = new ArrayList JavaDoc();
425       roleUnresolveds.add(roleUnresolved1);
426       roleUnresolveds.add(roleUnresolved2);
427       RoleUnresolvedList JavaDoc full = new RoleUnresolvedList JavaDoc(roleUnresolveds);
428       RoleUnresolvedList JavaDoc clone = (RoleUnresolvedList JavaDoc) full.clone();
429       assertEquals(2, clone.size());
430       assertEquals(roleUnresolved1.toString(), clone.get(0).toString());
431       assertEquals(roleUnresolved2.toString(), clone.get(1).toString());
432       Iterator JavaDoc iterator = clone.iterator();
433       assertEquals(roleUnresolved1.toString(), iterator.next().toString());
434       assertEquals(roleUnresolved2.toString(), iterator.next().toString());
435     }
436     catch (IllegalArgumentException JavaDoc e)
437     {
438       fail("FAILS IN RI: roleUnresolvedList -> RoleList?");
439     }
440   }
441
442   /**
443    * Test serialization.
444    */

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

487   private void setUpRoleUnresolveds()
488   {
489     if (setUpDone == true)
490       return;
491     try
492     {
493       // Create the roleUnresolveds
494
ArrayList JavaDoc roleValue1 = new ArrayList JavaDoc();
495       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
496       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
497       roleValue1.add(a);
498       roleValue1.add(b);
499       roleUnresolved1 = new RoleUnresolved JavaDoc("RoleName1", roleValue1,
500                                            RoleStatus.ROLE_NOT_READABLE);
501
502       ArrayList JavaDoc roleValue2 = new ArrayList JavaDoc();
503       ObjectName JavaDoc c = new ObjectName JavaDoc(":c=c");
504       ObjectName JavaDoc d = new ObjectName JavaDoc(":d=d");
505       roleValue2.add(c);
506       roleValue2.add(d);
507       roleUnresolved2 = new RoleUnresolved JavaDoc("RoleName2", roleValue2,
508                                            RoleStatus.ROLE_NOT_READABLE);
509     }
510     catch (MalformedObjectNameException JavaDoc mfone)
511     {
512       fail(mfone.toString());
513     }
514     setUpDone = true;
515   }
516 }
517
Popular Tags