KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14
15 import javax.management.NotCompliantMBeanException;
16 import javax.management.relation.InvalidRoleInfoException;
17 import javax.management.relation.RelationSupport;
18 import javax.management.relation.RoleInfo;
19
20 import junit.framework.TestCase;
21
22 /**
23  * Role Info tests.<p>
24  *
25  * Test it to death.<p>
26  *
27  * NOTE: The tests use String literals to ensure the comparisons are
28  * not performed on object references.
29  *
30  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
31  */

32 public class RoleInfoTestCase
33   extends TestCase
34 {
35   // Attributes ----------------------------------------------------------------
36

37   // Constructor ---------------------------------------------------------------
38

39   /**
40    * Construct the test
41    */

42   public RoleInfoTestCase(String s)
43   {
44     super(s);
45   }
46
47   // Tests ---------------------------------------------------------------------
48

49   /**
50    * Basic tests.
51    */

52   public void testBasic()
53   {
54     RoleInfo roleInfo = null;
55
56     // Minimal Constructor
57
try
58     {
59       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName());
60     }
61     catch (Exception e)
62     {
63       fail(e.toString());
64     }
65
66     // Did it work?
67
assertEquals(roleInfo.getName(), "RoleName");
68     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport.class.getName());
69     assertEquals(roleInfo.isReadable(), true);
70     assertEquals(roleInfo.isWritable(), true);
71     assertEquals(roleInfo.getMinDegree(), 1);
72     assertEquals(roleInfo.getMaxDegree(), 1);
73     assertEquals(roleInfo.getDescription(), null);
74
75     // Partial Constructor
76
try
77     {
78       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
79                               false, false);
80     }
81     catch (Exception e)
82     {
83       fail(e.toString());
84     }
85
86     // Did it work?
87
assertEquals(roleInfo.getName(), "RoleName");
88     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport.class.getName());
89     assertEquals(roleInfo.isReadable(), false);
90     assertEquals(roleInfo.isWritable(), false);
91     assertEquals(roleInfo.getMinDegree(), 1);
92     assertEquals(roleInfo.getMaxDegree(), 1);
93     assertEquals(roleInfo.getDescription(), null);
94
95     // Full Constructor
96
try
97     {
98       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
99                               false, false, 23, 25, "Description");
100     }
101     catch (Exception e)
102     {
103       fail(e.toString());
104     }
105
106     // Did it work?
107
assertEquals(roleInfo.getName(), "RoleName");
108     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport.class.getName());
109     assertEquals(roleInfo.isReadable(), false);
110     assertEquals(roleInfo.isWritable(), false);
111     assertEquals(roleInfo.getMinDegree(), 23);
112     assertEquals(roleInfo.getMaxDegree(), 25);
113     assertEquals(roleInfo.getDescription(), "Description");
114   }
115
116   /**
117    * Test Error Handling.
118    */

119   public void testErrorHandling()
120   {
121     RoleInfo roleInfo = null;
122
123     boolean caught = false;
124     try
125     {
126       roleInfo = new RoleInfo(null);
127     }
128     catch (IllegalArgumentException e)
129     {
130       caught = true;
131     }
132     catch (Exception e)
133     {
134       fail(e.toString());
135     }
136     if (caught == false)
137       fail("Copy Constructor accepts null role info");
138
139     caught = false;
140     try
141     {
142       roleInfo = new RoleInfo(null, RelationSupport.class.getName());
143     }
144     catch (IllegalArgumentException e)
145     {
146       caught = true;
147     }
148     catch (Exception e)
149     {
150       fail(e.toString());
151     }
152     if (caught == false)
153       fail("Constructor accepts null role name (1)");
154
155     caught = false;
156     try
157     {
158       roleInfo = new RoleInfo(null, RelationSupport.class.getName(), true, true);
159     }
160     catch (IllegalArgumentException e)
161     {
162       caught = true;
163     }
164     catch (Exception e)
165     {
166       fail(e.toString());
167     }
168     if (caught == false)
169       fail("Constructor accepts null role name (2)");
170
171     caught = false;
172     try
173     {
174       roleInfo = new RoleInfo(null, RelationSupport.class.getName(), true, true,
175                               1, 1, "blah");
176     }
177     catch (IllegalArgumentException e)
178     {
179       caught = true;
180     }
181     catch (Exception e)
182     {
183       fail(e.toString());
184     }
185     if (caught == false)
186       fail("Constructor accepts null role name (3)");
187
188     caught = false;
189     try
190     {
191       roleInfo = new RoleInfo("RoleName", null);
192     }
193     catch (IllegalArgumentException e)
194     {
195       caught = true;
196     }
197     catch (Exception e)
198     {
199       fail(e.toString());
200     }
201     if (caught == false)
202       fail("Constructor accepts null class name (1)");
203
204     caught = false;
205     try
206     {
207       roleInfo = new RoleInfo("RoleName", null, true, true);
208     }
209     catch (IllegalArgumentException e)
210     {
211       caught = true;
212     }
213     catch (Exception e)
214     {
215       fail(e.toString());
216     }
217     if (caught == false)
218       fail("Constructor accepts null class name (2)");
219
220     caught = false;
221     try
222     {
223       roleInfo = new RoleInfo("RoleName", null, true, true,
224                               1, 1, "blah");
225     }
226     catch (IllegalArgumentException e)
227     {
228       caught = true;
229     }
230     catch (Exception e)
231     {
232       fail(e.toString());
233     }
234     if (caught == false)
235       fail("Constructor accepts null class name (3)");
236
237     caught = false;
238     try
239     {
240       roleInfo = new RoleInfo("RoleName", "Inv alid");
241     }
242     catch (ClassNotFoundException e)
243     {
244       caught = true;
245     }
246     catch (Exception e)
247     {
248       fail(e.toString());
249     }
250     if (caught)
251       fail("Constructor accepts invalid class name (1) - disabled JMX1.2");
252
253     caught = false;
254     try
255     {
256       roleInfo = new RoleInfo("RoleName", "Inv alid", true, true);
257     }
258     catch (ClassNotFoundException e)
259     {
260       caught = true;
261     }
262     catch (Exception e)
263     {
264       fail(e.toString());
265     }
266     if (caught)
267       fail("Constructor accepts invalid class name (2) - disabled JMX1.2");
268
269     caught = false;
270     try
271     {
272       roleInfo = new RoleInfo("RoleName", "Inv alid", true, true,
273                               1, 1, "blah");
274     }
275     catch (ClassNotFoundException e)
276     {
277       caught = true;
278     }
279     catch (Exception e)
280     {
281       fail(e.toString());
282     }
283     if (caught)
284       fail("Constructor accepts invalid class name (3) - disabled JMX1.2");
285
286     caught = false;
287     try
288     {
289       roleInfo = new RoleInfo("RoleName", RoleInfo.class.getName());
290     }
291     catch (NotCompliantMBeanException e)
292     {
293       caught = true;
294     }
295     catch (Exception e)
296     {
297       fail(e.toString());
298     }
299     if (caught)
300       fail("Constructor accepts not compliant mbean (1) - disabled JMX1.2");
301
302     caught = false;
303     try
304     {
305       roleInfo = new RoleInfo("RoleName", RoleInfo.class.getName(), true, true);
306     }
307     catch (NotCompliantMBeanException e)
308     {
309       caught = true;
310     }
311     catch (Exception e)
312     {
313       fail(e.toString());
314     }
315     if (caught)
316       fail("Constructor accepts not compliant mbean (2) - disabled JMX1.2");
317
318     caught = false;
319     try
320     {
321       roleInfo = new RoleInfo("RoleName", RoleInfo.class.getName(), true, true,
322                               1, 1, "blah");
323     }
324     catch (NotCompliantMBeanException e)
325     {
326       caught = true;
327     }
328     catch (Exception e)
329     {
330       fail(e.toString());
331     }
332     if (caught)
333       fail("Constructor accepts not compliant mbean (3) - disabled JMX1.2");
334   }
335
336   /**
337    * Test constructor cardinality.
338    */

339   public void testConstructorCardinality()
340   {
341     // Create the role info
342
RoleInfo roleInfo = null;
343
344     // It's allow by the spec?????
345
try
346     {
347       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
348                             false, false, 0, 0, "Description");
349     }
350     catch (Exception e)
351     {
352       fail(e.toString());
353     }
354
355     boolean caught = false;
356     try
357     {
358       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
359                             false, false, 1, 0, "Description");
360     }
361     catch (InvalidRoleInfoException e)
362     {
363       caught = true;
364     }
365     catch (Exception e)
366     {
367       fail(e.toString());
368     }
369     if (caught == false)
370       fail("Shouldn't allow minimum of 1 and maximum of 0");
371
372     caught = false;
373     try
374     {
375       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
376                             false, false, RoleInfo.ROLE_CARDINALITY_INFINITY,
377                             0, "Description");
378     }
379     catch (InvalidRoleInfoException e)
380     {
381       caught = true;
382     }
383     catch (Exception e)
384     {
385       fail(e.toString());
386     }
387     if (caught == false)
388       fail("Shouldn't allow infinite minimum without infinite maximum");
389   }
390
391   /**
392    * Test the degree checkers.
393    */

394   public void testCheckDegrees()
395   {
396     // Create the role info
397
RoleInfo roleInfo = null;
398     RoleInfo roleInfo2 = null;
399
400     try
401     {
402       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
403                             false, false, 23, 25, "Description");
404     }
405     catch (Exception e)
406     {
407       fail(e.toString());
408     }
409     assertEquals(true, roleInfo.checkMaxDegree(0));
410     assertEquals(true, roleInfo.checkMaxDegree(22));
411     assertEquals(true, roleInfo.checkMaxDegree(23));
412     assertEquals(true, roleInfo.checkMaxDegree(24));
413     assertEquals(true, roleInfo.checkMaxDegree(25));
414     assertEquals(false, roleInfo.checkMaxDegree(26));
415     assertEquals(false, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
416
417     assertEquals(false, roleInfo.checkMinDegree(0));
418     assertEquals(false, roleInfo.checkMinDegree(22));
419     assertEquals(true, roleInfo.checkMinDegree(23));
420     assertEquals(true, roleInfo.checkMinDegree(24));
421     assertEquals(true, roleInfo.checkMinDegree(25));
422     assertEquals(true, roleInfo.checkMinDegree(26));
423     assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
424
425     try
426     {
427       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
428                             false, false, 25,
429                             RoleInfo.ROLE_CARDINALITY_INFINITY, "Description");
430     }
431     catch (Exception e)
432     {
433       fail(e.toString());
434     }
435     assertEquals(true, roleInfo.checkMaxDegree(0));
436     assertEquals(true, roleInfo.checkMaxDegree(24));
437     assertEquals(true, roleInfo.checkMaxDegree(25));
438     assertEquals(true, roleInfo.checkMaxDegree(26));
439     assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
440
441     assertEquals(false, roleInfo.checkMinDegree(0));
442     assertEquals(false, roleInfo.checkMinDegree(24));
443     assertEquals(true, roleInfo.checkMinDegree(25));
444     assertEquals(true, roleInfo.checkMinDegree(26));
445     assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
446
447     try
448     {
449       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
450                             false, false, RoleInfo.ROLE_CARDINALITY_INFINITY,
451                             RoleInfo.ROLE_CARDINALITY_INFINITY, "Description");
452     }
453     catch (Exception e)
454     {
455       fail(e.toString());
456     }
457     assertEquals(true, roleInfo.checkMaxDegree(0));
458     assertEquals(true, roleInfo.checkMaxDegree(26));
459     assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
460
461     assertEquals(true, roleInfo.checkMinDegree(0));
462     assertEquals(true, roleInfo.checkMinDegree(24));
463     assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
464   }
465
466   /**
467    * Test copy constructor.
468    */

469   public void testCopy()
470   {
471     // Create the role info
472
RoleInfo roleInfo = null;
473     RoleInfo roleInfo2 = null;
474
475     try
476     {
477       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
478                             false, false, 23, 25, "Description");
479       roleInfo2 = new RoleInfo(roleInfo);
480     }
481     catch (Exception e)
482     {
483       fail(e.toString());
484     }
485
486     // Did it work?
487
assertEquals(roleInfo.getName(), roleInfo2.getName());
488     assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2.getRefMBeanClassName());
489     assertEquals(roleInfo.isReadable(), roleInfo2.isReadable());
490     assertEquals(roleInfo.isWritable(), roleInfo2.isWritable());
491     assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree());
492     assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree());
493     assertEquals(roleInfo.getDescription(), roleInfo2.getDescription());
494   }
495
496   /**
497    * Test serialization.
498    */

499   public void testSerialization()
500   {
501     // Create the role info
502
RoleInfo roleInfo = null;
503     RoleInfo roleInfo2 = null;
504
505     try
506     {
507       roleInfo = new RoleInfo("RoleName", RelationSupport.class.getName(),
508                               false, false, 23, 25, "Description");
509       // Serialize it
510
ByteArrayOutputStream baos = new ByteArrayOutputStream();
511       ObjectOutputStream oos = new ObjectOutputStream(baos);
512       oos.writeObject(roleInfo);
513     
514       // Deserialize it
515
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
516       ObjectInputStream ois = new ObjectInputStream(bais);
517       roleInfo2 = (RoleInfo) ois.readObject();
518     }
519     catch (Exception e)
520     {
521       fail(e.toString());
522     }
523
524     // Did it work?
525
assertEquals(roleInfo.getName(), roleInfo2.getName());
526     assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2.getRefMBeanClassName());
527     assertEquals(roleInfo.isReadable(), roleInfo2.isReadable());
528     assertEquals(roleInfo.isWritable(), roleInfo2.isWritable());
529     assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree());
530     assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree());
531     assertEquals(roleInfo.getDescription(), roleInfo2.getDescription());
532   }
533 }
534
Popular Tags