KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import javax.management.NotCompliantMBeanException JavaDoc;
30 import javax.management.relation.InvalidRoleInfoException JavaDoc;
31 import javax.management.relation.RelationSupport JavaDoc;
32 import javax.management.relation.RoleInfo JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * Role Info tests.<p>
38  *
39  * Test it to death.<p>
40  *
41  * NOTE: The tests use String literals to ensure the comparisons are
42  * not performed on object references.
43  *
44  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
45  */

46 public class RoleInfoTestCase
47   extends TestCase
48 {
49   // Attributes ----------------------------------------------------------------
50

51   // Constructor ---------------------------------------------------------------
52

53   /**
54    * Construct the test
55    */

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

63   /**
64    * Basic tests.
65    */

66   public void testBasic()
67   {
68     RoleInfo JavaDoc roleInfo = null;
69
70     // Minimal Constructor
71
try
72     {
73       roleInfo = new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName());
74     }
75     catch (Exception JavaDoc e)
76     {
77       fail(e.toString());
78     }
79
80     // Did it work?
81
assertEquals(roleInfo.getName(), "RoleName");
82     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport JavaDoc.class.getName());
83     assertEquals(roleInfo.isReadable(), true);
84     assertEquals(roleInfo.isWritable(), true);
85     assertEquals(roleInfo.getMinDegree(), 1);
86     assertEquals(roleInfo.getMaxDegree(), 1);
87     assertEquals(roleInfo.getDescription(), null);
88
89     // Partial Constructor
90
try
91     {
92       roleInfo = new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName(),
93                               false, false);
94     }
95     catch (Exception JavaDoc e)
96     {
97       fail(e.toString());
98     }
99
100     // Did it work?
101
assertEquals(roleInfo.getName(), "RoleName");
102     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport JavaDoc.class.getName());
103     assertEquals(roleInfo.isReadable(), false);
104     assertEquals(roleInfo.isWritable(), false);
105     assertEquals(roleInfo.getMinDegree(), 1);
106     assertEquals(roleInfo.getMaxDegree(), 1);
107     assertEquals(roleInfo.getDescription(), null);
108
109     // Full Constructor
110
try
111     {
112       roleInfo = new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName(),
113                               false, false, 23, 25, "Description");
114     }
115     catch (Exception JavaDoc e)
116     {
117       fail(e.toString());
118     }
119
120     // Did it work?
121
assertEquals(roleInfo.getName(), "RoleName");
122     assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport JavaDoc.class.getName());
123     assertEquals(roleInfo.isReadable(), false);
124     assertEquals(roleInfo.isWritable(), false);
125     assertEquals(roleInfo.getMinDegree(), 23);
126     assertEquals(roleInfo.getMaxDegree(), 25);
127     assertEquals(roleInfo.getDescription(), "Description");
128   }
129
130   /**
131    * Test Error Handling.
132    */

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

351   public void testConstructorCardinality()
352   {
353     // It's allow by the spec?????
354
try
355     {
356       new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName(),
357                             false, false, 0, 0, "Description");
358     }
359     catch (Exception JavaDoc e)
360     {
361       fail(e.toString());
362     }
363
364     boolean caught = false;
365     try
366     {
367       new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName(),
368                             false, false, 1, 0, "Description");
369     }
370     catch (InvalidRoleInfoException JavaDoc e)
371     {
372       caught = true;
373     }
374     catch (Exception JavaDoc e)
375     {
376       fail(e.toString());
377     }
378     if (caught == false)
379       fail("Shouldn't allow minimum of 1 and maximum of 0");
380
381     caught = false;
382     try
383     {
384       new RoleInfo JavaDoc("RoleName", RelationSupport JavaDoc.class.getName(),
385                             false, false, RoleInfo.ROLE_CARDINALITY_INFINITY,
386                             0, "Description");
387     }
388     catch (InvalidRoleInfoException JavaDoc e)
389     {
390       caught = true;
391     }
392     catch (Exception JavaDoc e)
393     {
394       fail(e.toString());
395     }
396     if (caught == false)
397       fail("Shouldn't allow infinite minimum without infinite maximum");
398   }
399
400   /**
401    * Test the degree checkers.
402    */

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

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

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