001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.relation;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028:
029: import javax.management.NotCompliantMBeanException;
030: import javax.management.relation.InvalidRoleInfoException;
031: import javax.management.relation.RelationSupport;
032: import javax.management.relation.RoleInfo;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * Role Info tests.<p>
038: *
039: * Test it to death.<p>
040: *
041: * NOTE: The tests use String literals to ensure the comparisons are
042: * not performed on object references.
043: *
044: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
045: */
046: public class RoleInfoTestCase extends TestCase {
047: // Attributes ----------------------------------------------------------------
048:
049: // Constructor ---------------------------------------------------------------
050:
051: /**
052: * Construct the test
053: */
054: public RoleInfoTestCase(String s) {
055: super (s);
056: }
057:
058: // Tests ---------------------------------------------------------------------
059:
060: /**
061: * Basic tests.
062: */
063: public void testBasic() {
064: RoleInfo roleInfo = null;
065:
066: // Minimal Constructor
067: try {
068: roleInfo = new RoleInfo("RoleName", RelationSupport.class
069: .getName());
070: } catch (Exception e) {
071: fail(e.toString());
072: }
073:
074: // Did it work?
075: assertEquals(roleInfo.getName(), "RoleName");
076: assertEquals(roleInfo.getRefMBeanClassName(),
077: RelationSupport.class.getName());
078: assertEquals(roleInfo.isReadable(), true);
079: assertEquals(roleInfo.isWritable(), true);
080: assertEquals(roleInfo.getMinDegree(), 1);
081: assertEquals(roleInfo.getMaxDegree(), 1);
082: assertEquals(roleInfo.getDescription(), null);
083:
084: // Partial Constructor
085: try {
086: roleInfo = new RoleInfo("RoleName", RelationSupport.class
087: .getName(), false, false);
088: } catch (Exception e) {
089: fail(e.toString());
090: }
091:
092: // Did it work?
093: assertEquals(roleInfo.getName(), "RoleName");
094: assertEquals(roleInfo.getRefMBeanClassName(),
095: RelationSupport.class.getName());
096: assertEquals(roleInfo.isReadable(), false);
097: assertEquals(roleInfo.isWritable(), false);
098: assertEquals(roleInfo.getMinDegree(), 1);
099: assertEquals(roleInfo.getMaxDegree(), 1);
100: assertEquals(roleInfo.getDescription(), null);
101:
102: // Full Constructor
103: try {
104: roleInfo = new RoleInfo("RoleName", RelationSupport.class
105: .getName(), false, false, 23, 25, "Description");
106: } catch (Exception e) {
107: fail(e.toString());
108: }
109:
110: // Did it work?
111: assertEquals(roleInfo.getName(), "RoleName");
112: assertEquals(roleInfo.getRefMBeanClassName(),
113: RelationSupport.class.getName());
114: assertEquals(roleInfo.isReadable(), false);
115: assertEquals(roleInfo.isWritable(), false);
116: assertEquals(roleInfo.getMinDegree(), 23);
117: assertEquals(roleInfo.getMaxDegree(), 25);
118: assertEquals(roleInfo.getDescription(), "Description");
119: }
120:
121: /**
122: * Test Error Handling.
123: */
124: public void testErrorHandling() {
125: RoleInfo roleInfo = null;
126:
127: boolean caught = false;
128: try {
129: roleInfo = new RoleInfo(null);
130: } catch (IllegalArgumentException e) {
131: caught = true;
132: } catch (Exception e) {
133: fail(e.toString());
134: }
135: if (caught == false)
136: fail("Copy Constructor accepts null role info");
137:
138: caught = false;
139: try {
140: roleInfo = new RoleInfo(null, RelationSupport.class
141: .getName());
142: } catch (IllegalArgumentException e) {
143: caught = true;
144: } catch (Exception e) {
145: fail(e.toString());
146: }
147: if (caught == false)
148: fail("Constructor accepts null role name (1)");
149:
150: caught = false;
151: try {
152: roleInfo = new RoleInfo(null, RelationSupport.class
153: .getName(), true, true);
154: } catch (IllegalArgumentException e) {
155: caught = true;
156: } catch (Exception e) {
157: fail(e.toString());
158: }
159: if (caught == false)
160: fail("Constructor accepts null role name (2)");
161:
162: caught = false;
163: try {
164: roleInfo = new RoleInfo(null, RelationSupport.class
165: .getName(), true, true, 1, 1, "blah");
166: } catch (IllegalArgumentException e) {
167: caught = true;
168: } catch (Exception e) {
169: fail(e.toString());
170: }
171: if (caught == false)
172: fail("Constructor accepts null role name (3)");
173:
174: caught = false;
175: try {
176: roleInfo = new RoleInfo("RoleName", null);
177: } catch (IllegalArgumentException e) {
178: caught = true;
179: } catch (Exception e) {
180: fail(e.toString());
181: }
182: if (caught == false)
183: fail("Constructor accepts null class name (1)");
184:
185: caught = false;
186: try {
187: roleInfo = new RoleInfo("RoleName", null, true, true);
188: } catch (IllegalArgumentException e) {
189: caught = true;
190: } catch (Exception e) {
191: fail(e.toString());
192: }
193: if (caught == false)
194: fail("Constructor accepts null class name (2)");
195:
196: caught = false;
197: try {
198: roleInfo = new RoleInfo("RoleName", null, true, true, 1, 1,
199: "blah");
200: } catch (IllegalArgumentException e) {
201: caught = true;
202: } catch (Exception e) {
203: fail(e.toString());
204: }
205: if (caught == false)
206: fail("Constructor accepts null class name (3)");
207:
208: caught = false;
209: try {
210: roleInfo = new RoleInfo("RoleName", "Inv alid");
211: } catch (ClassNotFoundException e) {
212: caught = true;
213: } catch (Exception e) {
214: fail(e.toString());
215: }
216: if (caught)
217: fail("Constructor accepts invalid class name (1) - disabled JMX1.2");
218:
219: caught = false;
220: try {
221: roleInfo = new RoleInfo("RoleName", "Inv alid", true, true);
222: } catch (ClassNotFoundException e) {
223: caught = true;
224: } catch (Exception e) {
225: fail(e.toString());
226: }
227: if (caught)
228: fail("Constructor accepts invalid class name (2) - disabled JMX1.2");
229:
230: caught = false;
231: try {
232: roleInfo = new RoleInfo("RoleName", "Inv alid", true, true,
233: 1, 1, "blah");
234: } catch (ClassNotFoundException e) {
235: caught = true;
236: } catch (Exception e) {
237: fail(e.toString());
238: }
239: if (caught)
240: fail("Constructor accepts invalid class name (3) - disabled JMX1.2");
241:
242: caught = false;
243: try {
244: roleInfo = new RoleInfo("RoleName", RoleInfo.class
245: .getName());
246: } catch (NotCompliantMBeanException e) {
247: caught = true;
248: } catch (Exception e) {
249: fail(e.toString());
250: }
251: if (caught)
252: fail("Constructor accepts not compliant mbean (1) - disabled JMX1.2");
253:
254: caught = false;
255: try {
256: roleInfo = new RoleInfo("RoleName", RoleInfo.class
257: .getName(), true, true);
258: } catch (NotCompliantMBeanException e) {
259: caught = true;
260: } catch (Exception e) {
261: fail(e.toString());
262: }
263: if (caught)
264: fail("Constructor accepts not compliant mbean (2) - disabled JMX1.2");
265:
266: caught = false;
267: try {
268: roleInfo = new RoleInfo("RoleName", RoleInfo.class
269: .getName(), true, true, 1, 1, "blah");
270: } catch (NotCompliantMBeanException e) {
271: caught = true;
272: } catch (Exception e) {
273: fail(e.toString());
274: }
275: if (caught)
276: fail("Constructor accepts not compliant mbean (3) - disabled JMX1.2");
277: }
278:
279: /**
280: * Test constructor cardinality.
281: */
282: public void testConstructorCardinality() {
283: // Create the role info
284: RoleInfo roleInfo = null;
285:
286: // It's allow by the spec?????
287: try {
288: roleInfo = new RoleInfo("RoleName", RelationSupport.class
289: .getName(), false, false, 0, 0, "Description");
290: } catch (Exception e) {
291: fail(e.toString());
292: }
293:
294: boolean caught = false;
295: try {
296: roleInfo = new RoleInfo("RoleName", RelationSupport.class
297: .getName(), false, false, 1, 0, "Description");
298: } catch (InvalidRoleInfoException e) {
299: caught = true;
300: } catch (Exception e) {
301: fail(e.toString());
302: }
303: if (caught == false)
304: fail("Shouldn't allow minimum of 1 and maximum of 0");
305:
306: caught = false;
307: try {
308: roleInfo = new RoleInfo("RoleName", RelationSupport.class
309: .getName(), false, false,
310: RoleInfo.ROLE_CARDINALITY_INFINITY, 0,
311: "Description");
312: } catch (InvalidRoleInfoException e) {
313: caught = true;
314: } catch (Exception e) {
315: fail(e.toString());
316: }
317: if (caught == false)
318: fail("Shouldn't allow infinite minimum without infinite maximum");
319: }
320:
321: /**
322: * Test the degree checkers.
323: */
324: public void testCheckDegrees() {
325: // Create the role info
326: RoleInfo roleInfo = null;
327: RoleInfo roleInfo2 = null;
328:
329: try {
330: roleInfo = new RoleInfo("RoleName", RelationSupport.class
331: .getName(), false, false, 23, 25, "Description");
332: } catch (Exception e) {
333: fail(e.toString());
334: }
335: assertEquals(true, roleInfo.checkMaxDegree(0));
336: assertEquals(true, roleInfo.checkMaxDegree(22));
337: assertEquals(true, roleInfo.checkMaxDegree(23));
338: assertEquals(true, roleInfo.checkMaxDegree(24));
339: assertEquals(true, roleInfo.checkMaxDegree(25));
340: assertEquals(false, roleInfo.checkMaxDegree(26));
341: assertEquals(false, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
342:
343: assertEquals(false, roleInfo.checkMinDegree(0));
344: assertEquals(false, roleInfo.checkMinDegree(22));
345: assertEquals(true, roleInfo.checkMinDegree(23));
346: assertEquals(true, roleInfo.checkMinDegree(24));
347: assertEquals(true, roleInfo.checkMinDegree(25));
348: assertEquals(true, roleInfo.checkMinDegree(26));
349: assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
350:
351: try {
352: roleInfo = new RoleInfo("RoleName", RelationSupport.class
353: .getName(), false, false, 25,
354: RoleInfo.ROLE_CARDINALITY_INFINITY, "Description");
355: } catch (Exception e) {
356: fail(e.toString());
357: }
358: assertEquals(true, roleInfo.checkMaxDegree(0));
359: assertEquals(true, roleInfo.checkMaxDegree(24));
360: assertEquals(true, roleInfo.checkMaxDegree(25));
361: assertEquals(true, roleInfo.checkMaxDegree(26));
362: assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
363:
364: assertEquals(false, roleInfo.checkMinDegree(0));
365: assertEquals(false, roleInfo.checkMinDegree(24));
366: assertEquals(true, roleInfo.checkMinDegree(25));
367: assertEquals(true, roleInfo.checkMinDegree(26));
368: assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
369:
370: try {
371: roleInfo = new RoleInfo("RoleName", RelationSupport.class
372: .getName(), false, false,
373: RoleInfo.ROLE_CARDINALITY_INFINITY,
374: RoleInfo.ROLE_CARDINALITY_INFINITY, "Description");
375: } catch (Exception e) {
376: fail(e.toString());
377: }
378: assertEquals(true, roleInfo.checkMaxDegree(0));
379: assertEquals(true, roleInfo.checkMaxDegree(26));
380: assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE));
381:
382: assertEquals(true, roleInfo.checkMinDegree(0));
383: assertEquals(true, roleInfo.checkMinDegree(24));
384: assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE));
385: }
386:
387: /**
388: * Test copy constructor.
389: */
390: public void testCopy() {
391: // Create the role info
392: RoleInfo roleInfo = null;
393: RoleInfo roleInfo2 = null;
394:
395: try {
396: roleInfo = new RoleInfo("RoleName", RelationSupport.class
397: .getName(), false, false, 23, 25, "Description");
398: roleInfo2 = new RoleInfo(roleInfo);
399: } catch (Exception e) {
400: fail(e.toString());
401: }
402:
403: // Did it work?
404: assertEquals(roleInfo.getName(), roleInfo2.getName());
405: assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2
406: .getRefMBeanClassName());
407: assertEquals(roleInfo.isReadable(), roleInfo2.isReadable());
408: assertEquals(roleInfo.isWritable(), roleInfo2.isWritable());
409: assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree());
410: assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree());
411: assertEquals(roleInfo.getDescription(), roleInfo2
412: .getDescription());
413: }
414:
415: /**
416: * Test serialization.
417: */
418: public void testSerialization() {
419: // Create the role info
420: RoleInfo roleInfo = null;
421: RoleInfo roleInfo2 = null;
422:
423: try {
424: roleInfo = new RoleInfo("RoleName", RelationSupport.class
425: .getName(), false, false, 23, 25, "Description");
426: // Serialize it
427: ByteArrayOutputStream baos = new ByteArrayOutputStream();
428: ObjectOutputStream oos = new ObjectOutputStream(baos);
429: oos.writeObject(roleInfo);
430:
431: // Deserialize it
432: ByteArrayInputStream bais = new ByteArrayInputStream(baos
433: .toByteArray());
434: ObjectInputStream ois = new ObjectInputStream(bais);
435: roleInfo2 = (RoleInfo) ois.readObject();
436: } catch (Exception e) {
437: fail(e.toString());
438: }
439:
440: // Did it work?
441: assertEquals(roleInfo.getName(), roleInfo2.getName());
442: assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2
443: .getRefMBeanClassName());
444: assertEquals(roleInfo.isReadable(), roleInfo2.isReadable());
445: assertEquals(roleInfo.isWritable(), roleInfo2.isWritable());
446: assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree());
447: assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree());
448: assertEquals(roleInfo.getDescription(), roleInfo2
449: .getDescription());
450: }
451: }
|