KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestTypedCollection


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Tests TypedCollection.
24  *
25  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:20:35 $
26  *
27  * @author Stephen Colebourne
28  */

29 public abstract class TestTypedCollection extends BulkTest {
30
31     public TestTypedCollection(String JavaDoc name) {
32         super(name);
33     }
34
35
36     protected abstract Collection JavaDoc typedCollection();
37
38     protected Class JavaDoc getType() {
39         return String JavaDoc.class;
40     }
41
42
43     public void testIllegalAdd() {
44         Collection JavaDoc c = typedCollection();
45         Integer JavaDoc i = new Integer JavaDoc(3);
46         try {
47             c.add(i);
48             fail("Integer should fail string predicate.");
49         } catch (IllegalArgumentException JavaDoc e) {
50             // expected
51
}
52         assertTrue("Collection shouldn't contain illegal element",
53          !c.contains(i));
54     }
55
56
57     public void testIllegalAddAll() {
58         Collection JavaDoc c = typedCollection();
59         List JavaDoc elements = new ArrayList JavaDoc();
60         elements.add("one");
61         elements.add("two");
62         elements.add(new Integer JavaDoc(3));
63         elements.add("four");
64         try {
65             c.addAll(elements);
66             fail("Integer should fail string predicate.");
67         } catch (IllegalArgumentException JavaDoc e) {
68             // expected
69
}
70         assertTrue("Collection shouldn't contain illegal element",
71          !c.contains("one"));
72         assertTrue("Collection shouldn't contain illegal element",
73          !c.contains("two"));
74         assertTrue("Collection shouldn't contain illegal element",
75          !c.contains(new Integer JavaDoc(3)));
76         assertTrue("Collection shouldn't contain illegal element",
77          !c.contains("four"));
78     }
79
80 }
81
Popular Tags