001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Sergey A. Krivenko
020: * @version $Revision: 1.4.4.2 $
021: */package java.beans.beancontext;
022:
023: import java.util.Locale;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: /**
030: * Test class for java.beans.beancontext.BeanContextSupport.
031: * <p>
032: */
033:
034: public class BeanContextSupportTest extends TestCase {
035:
036: /** STANDARD BEGINNING * */
037:
038: /**
039: * No arguments constructor to enable serialization.
040: * <p>
041: */
042: public BeanContextSupportTest() {
043: super ();
044: }
045:
046: /**
047: * Constructs this test case with the given name.
048: * <p>
049: *
050: * @param name -
051: * The name for this test case.
052: * <p>
053: */
054: public BeanContextSupportTest(String name) {
055: super (name);
056: }
057:
058: /** TEST CONSTRUCTORS * */
059:
060: /**
061: * * Test constructor with BeanContext, Locale, boolean, boolean parameters.
062: * <p>
063: *
064: * @see BeanContextSupport#BeanContextSupport(BeanContext, Locale, boolean,
065: * boolean)
066: */
067: public void testConstructorBeanContextLocalebooleanboolean() {
068: new BeanContextSupport(null, null, true, true);
069: }
070:
071: /**
072: * * Test constructor with BeanContext, Locale, boolean parameters.
073: * <p>
074: *
075: * @see BeanContextSupport#BeanContextSupport(BeanContext, Locale, boolean)
076: */
077: public void testConstructorBeanContextLocaleboolean() {
078: new BeanContextSupport(null, null, true);
079: }
080:
081: /**
082: * * Test constructor with BeanContext, Locale parameters.
083: * <p>
084: *
085: * @see BeanContextSupport#BeanContextSupport(BeanContext, Locale)
086: */
087: public void testConstructorBeanContextLocale() {
088: new BeanContextSupport(null, null);
089: }
090:
091: /**
092: * * Test constructor with BeanContext parameter.
093: * <p>
094: *
095: * @see BeanContextSupport#BeanContextSupport(BeanContext)
096: */
097: public void testConstructorBeanContext() {
098: new BeanContextSupport(null);
099: }
100:
101: /**
102: * * Test constructor with no parameters.
103: * <p>
104: *
105: * @see BeanContextSupport#BeanContextSupport()
106: */
107: public void testConstructor() {
108: new BeanContextSupport();
109: }
110:
111: /** TEST METHODS * */
112:
113: /**
114: * Test method createBCSChild() with Object, Object parameters.
115: * <p>
116: */
117: public void testCreateBCSChildObjectObject() {
118: BeanContextSupport sup = new BeanContextSupport();
119: sup.createBCSChild(new BeanContextSupport(),
120: new BeanContextSupport());
121: }
122:
123: /**
124: * Test method setLocale() with Locale parameter.
125: * <p>
126: */
127: public void testSetLocaleLocale() throws Exception {
128: BeanContextSupport sup = new BeanContextSupport();
129: sup.setLocale(null);
130:
131: assertEquals("BeanContext should have default locale",
132: java.util.Locale.getDefault(), sup.getLocale());
133: }
134:
135: /**
136: * Test method bcsChildren() with no parameters.
137: * <p>
138: */
139: @SuppressWarnings("unchecked")
140: public void testBcsChildren() {
141: BeanContextSupport sup = new BeanContextSupport();
142: sup.add(new BeanContextChildSupport());
143:
144: for (java.util.Iterator it = sup.bcsChildren(); it.hasNext();) {
145: Object next = it.next();
146:
147: assertTrue("Children must be instances of "
148: + "BeanContextSupport.BCSChild class "
149: + "but at least one of them: " + next.getClass(),
150: next instanceof BeanContextSupport.BCSChild);
151: }
152: }
153:
154: /**
155: * Test method retainAll() with Collection parameter.
156: * <p>
157: */
158: public void testRetainAllCollection() {
159: /*
160: * // Create an instance and add one child BeanContextSupport sup = new
161: * BeanContextSupport(); BeanContextChildSupport ch = new
162: * BeanContextChildSupport(); sup.add(ch); // Create collection with an
163: * instance of the child that was added java.util.Collection col = new
164: * java.util.ArrayList(); col.add(ch); // Remove all children that are
165: * not in the collection // The collection must remain unchanged if
166: * (sup.retainAll(col)) { fail("False should be returned"); } // Just
167: * one child must be present if (sup.size() != 1) { fail("The size of
168: * the collection must be 1"); } // Add a new child in the collection
169: * and remove the old one col.clear(); col.add(new Object()); // Remove
170: * all children that are not in the collection // The collection must
171: * have 0 elements after that if (!sup.retainAll(col)) { fail("True
172: * should be returned"); } // No children must be present if (sup.size() !=
173: * 0) { fail("The size of the collection must be 0"); }
174: */
175: }
176:
177: /**
178: * Test method removeAll() with Collection parameter.
179: * <p>
180: */
181: public void testRemoveAllCollection() {
182: /*
183: * // Create an instance and add one child BeanContextSupport sup = new
184: * BeanContextSupport(); BeanContextChildSupport ch = new
185: * BeanContextChildSupport(); sup.add(ch); // Create collection with an
186: * instance of an arbitrary child java.util.Collection col = new
187: * java.util.ArrayList(); col.add(new Object()); // Remove all children
188: * that are in the collection // The collection should not change after
189: * that if (sup.removeAll(col)) { fail("False should be returned"); } //
190: * Add a child that is a member of the BeanContext col.add(ch); //
191: * Remove all children that are in the collection // The collection
192: * should change after that if (!sup.removeAll(col)) { fail("True should
193: * be returned"); } // No children must be present if (sup.size() != 0) {
194: * fail("The size of the collection must be 0 but is " + sup.size()); }
195: */
196: }
197:
198: /**
199: * Test method containsAll() with Collection parameter.
200: * <p>
201: */
202: public void testContainsAllCollection() {
203: /*
204: * // Create an instance and add two children BeanContextSupport sup =
205: * new BeanContextSupport(); BeanContextChildSupport ch = new
206: * BeanContextChildSupport(); Object obj = new Object(); sup.add(ch);
207: * sup.add(obj); // Create collection with BCS children that just were
208: * added java.util.Collection col = new java.util.ArrayList();
209: *
210: * for (java.util.Iterator it = sup.bcsChildren(); it.hasNext(); ) {
211: * col.add(it.next()); } // Two collections have the same elements if
212: * (!sup.containsAll(col)) { fail("True should be returned"); }
213: *
214: * sup.remove(obj); // Now they are different if (sup.containsAll(col)) {
215: * fail("False should be returned"); }
216: */
217: }
218:
219: /**
220: * Test method addAll() with Collection parameter.
221: * <p>
222: */
223: public void testAddAllCollection() {
224: /*
225: * // Create an instance and add two children BeanContextSupport sup =
226: * new BeanContextSupport(); // Create collection with two elements
227: * java.util.Collection col = new java.util.ArrayList(); col.add(new
228: * BeanContextChildSupport()); col.add(new Object()); // Place two
229: * children into the BeanContext if (!sup.addAll(col)) { fail("True
230: * should be returned"); } // Two children must be present if
231: * (sup.size() != 2) { fail("The size of the collection must be 2 but is " +
232: * sup.size()); }
233: */
234: }
235:
236: /**
237: * Test method remove() with Object, boolean parameters.
238: * <p>
239: */
240: public void testRemoveObjectboolean() {
241: // Create an instance and add one child
242: BeanContextSupport sup = new BeanContextSupport();
243: BeanContextChildSupport ch = new BeanContextChildSupport();
244: sup.add(ch);
245:
246: // Remove non-existent child
247: assertFalse(sup.remove(new Object(), true));
248:
249: // Remove it
250: assertTrue(sup.remove(ch, true));
251:
252: // No children must be present
253: assertEquals("The size of the collection must be 0", 0, sup
254: .size());
255: }
256:
257: /**
258: * Test method remove() with Object parameter.
259: * <p>
260: */
261: public void testRemoveObject() {
262: // Create an instance and add one child
263: BeanContextSupport sup = new BeanContextSupport();
264: BeanContextChildSupport ch = new BeanContextChildSupport();
265: sup.add(ch);
266:
267: // Remove non-existent child
268: assertFalse(sup.remove(new Object()));
269:
270: // Remove it
271: assertTrue(sup.remove(ch));
272:
273: // No children must be present
274: assertEquals("The size of the collection must be 0", 0, sup
275: .size());
276: }
277:
278: /**
279: * Test method containsKey() with Object parameter.
280: * <p>
281: */
282: public void testContainsKeyObject() {
283:
284: // Create an instance and add a child
285: BeanContextSupport sup = new BeanContextSupport();
286: BeanContextChildSupport ch = new BeanContextChildSupport();
287: sup.add(ch);
288:
289: // We should find the child now
290: assertTrue(sup.containsKey(ch));
291: }
292:
293: /**
294: * Test method contains() with Object parameter.
295: * <p>
296: */
297: public void testContainsObject() {
298: // Create an instance and add a child
299: BeanContextSupport sup = new BeanContextSupport();
300: BeanContextChildSupport ch = new BeanContextChildSupport();
301: sup.add(ch);
302:
303: BeanContextSupport.BCSChild bcs = (BeanContextSupport.BCSChild) sup
304: .bcsChildren().next();
305:
306: // We should find the child now
307: if (!sup.contains(bcs)) {
308: // fail("True should be returned");
309: }
310: }
311:
312: /**
313: * Test method add() with Object parameter.
314: * <p>
315: */
316: public void testAddObject() {
317: // Create an instance and add a child
318: BeanContextSupport sup = new BeanContextSupport();
319: sup.add(new Object());
320:
321: // Just one child must be present
322: assertEquals("The size of the collection must be 1", 1, sup
323: .size());
324: }
325:
326: /**
327: * Test method toArray() with no parameters.
328: * <p>
329: */
330: public void testToArray() {
331: // Create an instance and add two children
332: BeanContextSupport sup = new BeanContextSupport();
333: sup.add("obj1");
334: sup.add("obj2");
335:
336: // Convert to array
337: Object[] array = sup.toArray();
338:
339: // Check length
340: assertEquals("The size of the collection must be 2", 2,
341: array.length);
342: }
343:
344: /**
345: * Test method copyChildren() with no parameters.
346: * <p>
347: */
348: public void testCopyChildren() {
349: // Create an instance and add two children
350: BeanContextSupport sup = new BeanContextSupport();
351: sup.add("obj1");
352: sup.add("obj2");
353:
354: // Convert to array
355: Object[] array = sup.copyChildren();
356:
357: // Check length
358: assertEquals("The size of the collection must be 2", 2,
359: array.length);
360: }
361:
362: /**
363: * Test method removeBeanContextMembershipListener() with
364: * BeanContextMembershipListener parameter.
365: * <p>
366: */
367: public void testRemoveBeanContextMembershipListenerBeanContextMembershipListener() {
368: // Create BeanContext and BeanContextMembershipListener instances
369: BeanContextSupport sup = new BeanContextSupport();
370: BeanContextMembershipListener l = getBeanContextMembershipListener();
371: sup.addBeanContextMembershipListener(l);
372: sup.removeBeanContextMembershipListener(l);
373:
374: assertFalse("Listener should not be present", sup.bcmListeners
375: .contains(l));
376: }
377:
378: /**
379: * Test method addBeanContextMembershipListener() with
380: * BeanContextMembershipListener parameter.
381: * <p>
382: */
383: public void testAddBeanContextMembershipListenerBeanContextMembershipListener() {
384: // Create BeanContext and BeanContextMembershipListener instances
385: BeanContextSupport sup = new BeanContextSupport();
386: BeanContextMembershipListener l = getBeanContextMembershipListener();
387: sup.addBeanContextMembershipListener(l);
388:
389: assertTrue("Listener should be present", sup.bcmListeners
390: .contains(l));
391: }
392:
393: /**
394: * Test method getBeanContextPeer() with no parameters.
395: * <p>
396: */
397: public void testGetBeanContextPeer() {
398: // Create BeanContext instance
399: BeanContextSupport sup = new BeanContextSupport();
400:
401: // The peer and this context should be equal
402: assertEquals("The peer and the BeanContext should be equal",
403: sup, sup.getBeanContextPeer());
404: }
405:
406: /**
407: * Test method vetoableChange() with PropertyChangeEvent parameter.
408: * <p>
409: */
410: // public void testVetoableChangePropertyChangeEvent() {
411: // /** @todo: not implemented yet in the class * */
412: // Create BeanContext instance
413: // BeanContextSupport sup = new BeanContextSupport();
414: // sup.vetoableChange(null);
415: // }
416: /**
417: * Test method propertyChange() with PropertyChangeEvent parameter.
418: * <p>
419: */
420: // public void testPropertyChangePropertyChangeEvent() {
421: // /** @todo: not implemented yet in the class * */
422: // // Create BeanContext instance
423: // BeanContextSupport sup = new BeanContextSupport();
424: // // sup.propertyChange(null);
425: // }
426: /**
427: * Test method isEmpty() with no parameters.
428: * <p>
429: */
430: public void testIsEmpty() {
431: // Create BeanContext instance
432: BeanContextSupport sup = new BeanContextSupport();
433:
434: assertTrue("The collection of children should be empty", sup
435: .isEmpty());
436:
437: sup.add(new Object());
438:
439: assertFalse("The collection of children should not be empty",
440: sup.isEmpty());
441: }
442:
443: /**
444: * Test method clear() with no parameters.
445: * <p>
446: */
447: public void testClear() {
448: /*
449: * // Create BeanContext instance BeanContextSupport sup = new
450: * BeanContextSupport(); // Add a child and then clear sup.add(new
451: * Object()); sup.clear();
452: *
453: * if (!sup.isEmpty()) { fail("The collection of children should be
454: * empty"); }
455: */
456: }
457:
458: /**
459: * Test method size() with no parameters.
460: * <p>
461: */
462: public void testSize() {
463: // Create BeanContext instance
464: BeanContextSupport sup = new BeanContextSupport();
465:
466: assertEquals("The size of the collection should be equal to 0",
467: 0, sup.size());
468:
469: sup.add(new Object());
470:
471: assertEquals("The collection of children should not be empty",
472: 1, sup.size());
473: }
474:
475: /**
476: * Test method getResourceAsStream() with String, BeanContextChild=null
477: * parameters.
478: * <p>
479: */
480: public void test_getResourceAsStreamLlava_lang_StringLjava_beans_beancontext_BeanContextChild() {
481: BeanContextSupport obj = new BeanContextSupport();
482: try {
483: obj.getResourceAsStream(new String(), null);
484: fail("NullPointerException expected");
485: } catch (NullPointerException t) {
486: }
487: }
488:
489: /**
490: * Test method getResourceAsStream() with String=null, BeanContextChild=null
491: * parameters.
492: * <p>
493: */
494: public void test_getResourceAsStreamLlava_lang_StringLjava_beans_beancontext_BeanContextChild2() {
495: BeanContextSupport obj = new BeanContextSupport();
496: try {
497: obj.getResourceAsStream(null, null);
498: fail("NullPointerException expected");
499: } catch (NullPointerException t) {
500: }
501: }
502:
503: /**
504: * Test method vetoableChange() with PropertyChangeEvent=null parameter.
505: * <p>
506: *
507: * @throws Exception
508: */
509: public void test_vetoableChangeLjava_beans_PropertyChangeEvent()
510: throws Exception {
511: BeanContextSupport obj = new BeanContextSupport();
512: try {
513: obj.vetoableChange(null);
514: fail("NullPointerException expected");
515: } catch (NullPointerException t) {
516: }
517: }
518:
519: /**
520: * Test method getResource() with String!=null, BeanContextChild=null
521: * parameters.
522: * <p>
523: */
524: public void test_getResourceLjava_lang_StringLjava_beans_beancontext_BeanContextChild() {
525: BeanContextSupport obj = new BeanContextSupport();
526: try {
527: obj.getResource("", null);
528: fail("NullPointerException expected");
529: } catch (NullPointerException t) {
530: }
531: }
532:
533: /**
534: * Test method getResource() with String=null, BeanContextChild=null
535: * parameters.
536: * <p>
537: */
538: public void test_getResourceLjava_lang_StringLjava_beans_beancontext_BeanContextChild2() {
539: BeanContextSupport obj = new BeanContextSupport();
540: try {
541: obj.getResource(null, null);
542: fail("NullPointerException expected");
543: } catch (NullPointerException t) {
544: }
545: }
546:
547: /** UTILITY METHODS * */
548:
549: /**
550: * Create BeanContextMembershipListener instance
551: */
552: private BeanContextMembershipListener getBeanContextMembershipListener() {
553: return new BeanContextMembershipListener() {
554:
555: public void childrenAdded(BeanContextMembershipEvent bcme) {
556: }
557:
558: public void childrenRemoved(BeanContextMembershipEvent bcme) {
559: }
560: };
561: }
562:
563: /** STANDARD ENDING * */
564:
565: /**
566: * Start testing from the command line.
567: * <p>
568: */
569: public static Test suite() {
570: return new TestSuite(BeanContextSupportTest.class);
571: }
572:
573: /**
574: * Start testing from the command line.
575: * <p>
576: *
577: * @param args -
578: * Command line parameters.
579: * <p>
580: */
581: public static void main(String args[]) {
582: junit.textui.TestRunner.run(suite());
583: }
584: }
|