01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.util.Collection;
20: import java.util.List;
21:
22: /**
23: * Tests TypedCollection.
24: *
25: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
26: *
27: * @author Stephen Colebourne
28: */
29: public abstract class TestTypedCollection extends BulkTest {
30:
31: public TestTypedCollection(String name) {
32: super (name);
33: }
34:
35: protected abstract Collection typedCollection();
36:
37: protected Class getType() {
38: return String.class;
39: }
40:
41: public void testIllegalAdd() {
42: Collection c = typedCollection();
43: Integer i = new Integer(3);
44: try {
45: c.add(i);
46: fail("Integer should fail string predicate.");
47: } catch (IllegalArgumentException e) {
48: // expected
49: }
50: assertTrue("Collection shouldn't contain illegal element", !c
51: .contains(i));
52: }
53:
54: public void testIllegalAddAll() {
55: Collection c = typedCollection();
56: List elements = new ArrayList();
57: elements.add("one");
58: elements.add("two");
59: elements.add(new Integer(3));
60: elements.add("four");
61: try {
62: c.addAll(elements);
63: fail("Integer should fail string predicate.");
64: } catch (IllegalArgumentException e) {
65: // expected
66: }
67: assertTrue("Collection shouldn't contain illegal element", !c
68: .contains("one"));
69: assertTrue("Collection shouldn't contain illegal element", !c
70: .contains("two"));
71: assertTrue("Collection shouldn't contain illegal element", !c
72: .contains(new Integer(3)));
73: assertTrue("Collection shouldn't contain illegal element", !c
74: .contains("four"));
75: }
76:
77: }
|