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.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032:
033: import javax.management.ObjectName;
034: import javax.management.MalformedObjectNameException;
035: import javax.management.relation.RoleStatus;
036: import javax.management.relation.RoleUnresolved;
037: import javax.management.relation.RoleUnresolvedList;
038:
039: import junit.framework.TestCase;
040:
041: /**
042: * Role Unresolved lists tests.<p>
043: *
044: * Test it to death.<p>
045: *
046: * NOTE: The tests use String literals to ensure the comparisons are
047: * not performed on object references.
048: *
049: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
050: */
051: public class RoleUnresolvedListTestCase extends TestCase {
052: // Attributes ----------------------------------------------------------------
053:
054: // Role Unresolveds used in testing
055: boolean setUpDone = false;
056: RoleUnresolved roleUnresolved1;
057: RoleUnresolved roleUnresolved2;
058:
059: // Constructor ---------------------------------------------------------------
060:
061: /**
062: * Construct the test
063: */
064: public RoleUnresolvedListTestCase(String s) {
065: super (s);
066: }
067:
068: // Tests ---------------------------------------------------------------------
069:
070: /**
071: * Empty Constructors.
072: */
073: public void testEmptyConstructors() {
074: setUpRoleUnresolveds();
075:
076: // Empty lists
077: RoleUnresolvedList empty = new RoleUnresolvedList();
078: assertEquals(0, empty.size());
079: empty = new RoleUnresolvedList(100);
080: assertEquals(0, empty.size());
081: }
082:
083: /**
084: * Basic constructor test.
085: */
086: public void testBasicConstructor() {
087: setUpRoleUnresolveds();
088:
089: ArrayList roleUnresolveds = new ArrayList();
090: roleUnresolveds.add(roleUnresolved1);
091: roleUnresolveds.add(roleUnresolved2);
092: RoleUnresolvedList full = new RoleUnresolvedList(
093: roleUnresolveds);
094: assertEquals(2, full.size());
095: assertEquals(roleUnresolved1, full.get(0));
096: assertEquals(roleUnresolved2, full.get(1));
097: Iterator iterator = full.iterator();
098: assertEquals(roleUnresolved1, iterator.next());
099: assertEquals(roleUnresolved2, iterator.next());
100: }
101:
102: /**
103: * Basic constructor test, ordering. Do it backwards
104: */
105: public void testBasicConstructorOrdering() {
106: setUpRoleUnresolveds();
107:
108: ArrayList roleUnresolveds = new ArrayList();
109: roleUnresolveds.add(roleUnresolved2);
110: roleUnresolveds.add(roleUnresolved1);
111: RoleUnresolvedList full = new RoleUnresolvedList(
112: roleUnresolveds);
113: assertEquals(2, full.size());
114: assertEquals(roleUnresolved2, full.get(0));
115: assertEquals(roleUnresolved1, full.get(1));
116: Iterator iterator = full.iterator();
117: assertEquals(roleUnresolved2, iterator.next());
118: assertEquals(roleUnresolved1, iterator.next());
119: }
120:
121: /**
122: * Basic constructor test, allows duplicates
123: */
124: public void testBasicConstructorDuplicates() {
125: setUpRoleUnresolveds();
126:
127: // Check duplicates allowed
128: ArrayList roleUnresolveds = new ArrayList();
129: roleUnresolveds.add(roleUnresolved1);
130: roleUnresolveds.add(roleUnresolved1);
131: RoleUnresolvedList full = new RoleUnresolvedList(
132: roleUnresolveds);
133: assertEquals(2, full.size());
134: assertEquals(roleUnresolved1, full.get(0));
135: assertEquals(roleUnresolved1, full.get(1));
136: Iterator iterator = full.iterator();
137: assertEquals(roleUnresolved1, iterator.next());
138: assertEquals(roleUnresolved1, iterator.next());
139: }
140:
141: /**
142: * Test Error Handling.
143: */
144: public void testErrorHandling() {
145: setUpRoleUnresolveds();
146:
147: // Shouldn't allow new roleUnresolveds
148: ArrayList roleUnresolveds = new ArrayList();
149: roleUnresolveds.add(roleUnresolved1);
150: roleUnresolveds.add(null);
151:
152: // Shouldn't allow null for the name in constructor
153: boolean caught = false;
154: try {
155: new RoleUnresolvedList(roleUnresolveds);
156: } catch (IllegalArgumentException e) {
157: caught = true;
158: }
159: if (caught == false)
160: fail("Constructor accepts null roleUnresolveds");
161:
162: // Should only allow roleUnresolveds
163: roleUnresolveds = new ArrayList();
164: roleUnresolveds.add(roleUnresolved1);
165: roleUnresolveds.add(new Object());
166: caught = false;
167: try {
168: new RoleUnresolvedList(roleUnresolveds);
169: } catch (IllegalArgumentException e) {
170: caught = true;
171: }
172: if (caught == false)
173: fail("Constructor accepts non roleUnresolveds");
174: }
175:
176: /**
177: * Single Append tests.
178: */
179: public void testSingleAppend() {
180: setUpRoleUnresolveds();
181:
182: // Simple add
183: RoleUnresolvedList list = new RoleUnresolvedList();
184: list.add(roleUnresolved1);
185: assertEquals(1, list.size());
186: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
187: Iterator iterator = list.iterator();
188: assertEquals(roleUnresolved1.toString(), iterator.next()
189: .toString());
190:
191: // Once more for luck, should append
192: list.add(roleUnresolved2);
193: assertEquals(2, list.size());
194: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
195: assertEquals(roleUnresolved2.toString(), list.get(1).toString());
196: iterator = list.iterator();
197: assertEquals(roleUnresolved1.toString(), iterator.next()
198: .toString());
199: assertEquals(roleUnresolved2.toString(), iterator.next()
200: .toString());
201:
202: // Add a null, shouldn't work
203: boolean caught = false;
204: try {
205: list.add(null);
206: } catch (IllegalArgumentException e) {
207: caught = true;
208: }
209: if (caught == false)
210: fail("RoleUnresolved add(null) shouldn't work");
211: }
212:
213: /**
214: * Add single
215: */
216: public void testSingleAdd() {
217: setUpRoleUnresolveds();
218:
219: RoleUnresolvedList list = new RoleUnresolvedList();
220: list.add(roleUnresolved1);
221: list.add(roleUnresolved2);
222:
223: // Add one
224: list.add(1, roleUnresolved1);
225: assertEquals(3, list.size());
226: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
227: assertEquals(roleUnresolved1.toString(), list.get(1).toString());
228: assertEquals(roleUnresolved2.toString(), list.get(2).toString());
229: Iterator iterator = list.iterator();
230: assertEquals(roleUnresolved1.toString(), iterator.next()
231: .toString());
232: assertEquals(roleUnresolved1.toString(), iterator.next()
233: .toString());
234: assertEquals(roleUnresolved2.toString(), iterator.next()
235: .toString());
236:
237: // Add a roleUnresolved in the wrong place
238: boolean caught = false;
239: try {
240: list.add(4, roleUnresolved1);
241: } catch (IndexOutOfBoundsException e) {
242: caught = true;
243: }
244: if (caught == false)
245: fail("Shouldn't be able to add a roleUnresolved outside of valid range");
246:
247: // Add a null should not work
248: caught = false;
249: try {
250: list.add(1, null);
251: } catch (IllegalArgumentException e) {
252: caught = true;
253: }
254: if (caught == false)
255: fail("Shouldn't be able to add a null at an index");
256: }
257:
258: /**
259: * Set single
260: */
261: public void testSingleSet() {
262: setUpRoleUnresolveds();
263:
264: RoleUnresolvedList list = new RoleUnresolvedList();
265: list.add(roleUnresolved1);
266: list.add(roleUnresolved2);
267:
268: // Add one
269: list.set(1, roleUnresolved1);
270: assertEquals(2, list.size());
271: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
272: assertEquals(roleUnresolved1.toString(), list.get(1).toString());
273: Iterator iterator = list.iterator();
274: assertEquals(roleUnresolved1.toString(), iterator.next()
275: .toString());
276: assertEquals(roleUnresolved1.toString(), iterator.next()
277: .toString());
278:
279: // Add a role Unresolved in the wrong place
280: boolean caught = false;
281: try {
282: list.set(4, roleUnresolved1);
283: } catch (IndexOutOfBoundsException e) {
284: caught = true;
285: }
286: if (caught == false)
287: fail("Shouldn't be able to set a roleUnresolved outside of valid range");
288:
289: // set a null should not work
290: caught = false;
291: try {
292: list.add(1, null);
293: } catch (IllegalArgumentException e) {
294: caught = true;
295: }
296: if (caught == false)
297: fail("Shouldn't be able to set a null at an index");
298: }
299:
300: /**
301: * Add multiple
302: */
303: public void testMultipleAdd() {
304: setUpRoleUnresolveds();
305:
306: RoleUnresolvedList list = new RoleUnresolvedList();
307: list.add(roleUnresolved1);
308: list.add(roleUnresolved1);
309: RoleUnresolvedList listToAdd = new RoleUnresolvedList();
310: listToAdd.add(roleUnresolved2);
311: listToAdd.add(roleUnresolved2);
312:
313: // Add all
314: list.addAll(listToAdd);
315: assertEquals(4, list.size());
316: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
317: assertEquals(roleUnresolved1.toString(), list.get(1).toString());
318: assertEquals(roleUnresolved2.toString(), list.get(2).toString());
319: assertEquals(roleUnresolved2.toString(), list.get(3).toString());
320: Iterator iterator = list.iterator();
321: assertEquals(roleUnresolved1.toString(), iterator.next()
322: .toString());
323: assertEquals(roleUnresolved1.toString(), iterator.next()
324: .toString());
325: assertEquals(roleUnresolved2.toString(), iterator.next()
326: .toString());
327: assertEquals(roleUnresolved2.toString(), iterator.next()
328: .toString());
329:
330: // Add a null should work (not very standard)
331: boolean caught = false;
332: try {
333: list.addAll(null);
334: } catch (Exception e) {
335: caught = true;
336: }
337: if (caught == true)
338: fail("Should be able to addAll a null");
339: }
340:
341: /**
342: * Add multiple at a location
343: */
344: public void testMultipleLocationAdd() {
345: setUpRoleUnresolveds();
346:
347: RoleUnresolvedList list = new RoleUnresolvedList();
348: list.add(roleUnresolved1);
349: list.add(roleUnresolved1);
350: RoleUnresolvedList listToAdd = new RoleUnresolvedList();
351: listToAdd.add(roleUnresolved2);
352: listToAdd.add(roleUnresolved2);
353:
354: // Add all
355: list.addAll(1, listToAdd);
356: assertEquals(4, list.size());
357: assertEquals(roleUnresolved1.toString(), list.get(0).toString());
358: assertEquals(roleUnresolved2.toString(), list.get(1).toString());
359: assertEquals(roleUnresolved2.toString(), list.get(2).toString());
360: assertEquals(roleUnresolved1.toString(), list.get(3).toString());
361: Iterator iterator = list.iterator();
362: assertEquals(roleUnresolved1.toString(), iterator.next()
363: .toString());
364: assertEquals(roleUnresolved2.toString(), iterator.next()
365: .toString());
366: assertEquals(roleUnresolved2.toString(), iterator.next()
367: .toString());
368: assertEquals(roleUnresolved1.toString(), iterator.next()
369: .toString());
370:
371: // Add a role Unresolved in the wrong place
372: boolean caught = false;
373: try {
374: list.addAll(6, listToAdd);
375: } catch (IndexOutOfBoundsException e) {
376: caught = true;
377: }
378: if (caught == false)
379: fail("Shouldn't be able to addAll a roleUnresolved outside of valid range");
380:
381: // Add a null should not work
382: caught = false;
383: try {
384: list.addAll(1, null);
385: } catch (IllegalArgumentException e) {
386: caught = true;
387: }
388: if (caught == false)
389: fail("Shouldn't be able to addAll a null at an index");
390: }
391:
392: /**
393: * Test clone.
394: */
395: public void testClone() {
396: setUpRoleUnresolveds();
397:
398: try {
399: ArrayList roleUnresolveds = new ArrayList();
400: roleUnresolveds.add(roleUnresolved1);
401: roleUnresolveds.add(roleUnresolved2);
402: RoleUnresolvedList full = new RoleUnresolvedList(
403: roleUnresolveds);
404: RoleUnresolvedList clone = (RoleUnresolvedList) full
405: .clone();
406: assertEquals(2, clone.size());
407: assertEquals(roleUnresolved1.toString(), clone.get(0)
408: .toString());
409: assertEquals(roleUnresolved2.toString(), clone.get(1)
410: .toString());
411: Iterator iterator = clone.iterator();
412: assertEquals(roleUnresolved1.toString(), iterator.next()
413: .toString());
414: assertEquals(roleUnresolved2.toString(), iterator.next()
415: .toString());
416: } catch (IllegalArgumentException e) {
417: fail("FAILS IN RI: roleUnresolvedList -> RoleList?");
418: }
419: }
420:
421: /**
422: * Test serialization.
423: */
424: public void testSerialization() {
425: setUpRoleUnresolveds();
426:
427: ArrayList roleUnresolveds = new ArrayList();
428: roleUnresolveds.add(roleUnresolved1);
429: roleUnresolveds.add(roleUnresolved2);
430: RoleUnresolvedList full = new RoleUnresolvedList(
431: roleUnresolveds);
432: RoleUnresolvedList copy = null;
433:
434: try {
435: // Serialize it
436: ByteArrayOutputStream baos = new ByteArrayOutputStream();
437: ObjectOutputStream oos = new ObjectOutputStream(baos);
438: oos.writeObject(full);
439:
440: // Deserialize it
441: ByteArrayInputStream bais = new ByteArrayInputStream(baos
442: .toByteArray());
443: ObjectInputStream ois = new ObjectInputStream(bais);
444: copy = (RoleUnresolvedList) ois.readObject();
445: } catch (IOException ioe) {
446: fail(ioe.toString());
447: } catch (ClassNotFoundException cnfe) {
448: fail(cnfe.toString());
449: }
450:
451: // Did it work?
452: assertEquals(2, copy.size());
453: assertEquals(roleUnresolved1.toString(), copy.get(0).toString());
454: assertEquals(roleUnresolved2.toString(), copy.get(1).toString());
455: Iterator iterator = copy.iterator();
456: assertEquals(roleUnresolved1.toString(), iterator.next()
457: .toString());
458: assertEquals(roleUnresolved2.toString(), iterator.next()
459: .toString());
460: }
461:
462: // Tests ---------------------------------------------------------------------
463:
464: private void setUpRoleUnresolveds() {
465: if (setUpDone == true)
466: return;
467: try {
468: // Create the roleUnresolveds
469: ArrayList roleValue1 = new ArrayList();
470: ObjectName a = new ObjectName(":a=a");
471: ObjectName b = new ObjectName(":b=b");
472: roleValue1.add(a);
473: roleValue1.add(b);
474: roleUnresolved1 = new RoleUnresolved("RoleName1",
475: roleValue1, RoleStatus.ROLE_NOT_READABLE);
476:
477: ArrayList roleValue2 = new ArrayList();
478: ObjectName c = new ObjectName(":c=c");
479: ObjectName d = new ObjectName(":d=d");
480: roleValue2.add(c);
481: roleValue2.add(d);
482: roleUnresolved2 = new RoleUnresolved("RoleName2",
483: roleValue2, RoleStatus.ROLE_NOT_READABLE);
484: } catch (MalformedObjectNameException mfone) {
485: fail(mfone.toString());
486: }
487: setUpDone = true;
488: }
489: }
|