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