~ubuntu-branches/debian/sid/libcommons-collections-java/sid

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/collections/TestPredicatedCollection.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2004-08-07 00:02:50 UTC
  • Revision ID: james.westby@ubuntu.com-20040807000250-hcnqvrdpxg95nmzr
Tags: upstream-2.1.1
ImportĀ upstreamĀ versionĀ 2.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999-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;
 
19
import java.util.Collection;
 
20
import java.util.List;
 
21
 
 
22
 
 
23
public abstract class TestPredicatedCollection extends BulkTest {
 
24
 
 
25
    public TestPredicatedCollection(String name) {
 
26
        super(name);
 
27
    }
 
28
 
 
29
 
 
30
    protected abstract Collection predicatedCollection();
 
31
 
 
32
    protected Predicate getPredicate() {
 
33
        return new Predicate() {
 
34
            public boolean evaluate(Object o) {
 
35
                return o instanceof String;
 
36
            }
 
37
        };
 
38
    }
 
39
 
 
40
 
 
41
    public void testIllegalAdd() {
 
42
        Collection c = predicatedCollection();
 
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", 
 
51
         !c.contains(i));   
 
52
    }
 
53
 
 
54
 
 
55
    public void testIllegalAddAll() {
 
56
        Collection c = predicatedCollection();
 
57
        List elements = new ArrayList();
 
58
        elements.add("one");
 
59
        elements.add("two");
 
60
        elements.add(new Integer(3));
 
61
        elements.add("four");
 
62
        try {
 
63
            c.addAll(elements);
 
64
            fail("Integer should fail string predicate.");
 
65
        } catch (IllegalArgumentException e) {
 
66
            // expected
 
67
        }
 
68
        assertTrue("Collection shouldn't contain illegal element", 
 
69
         !c.contains("one"));   
 
70
        assertTrue("Collection shouldn't contain illegal element", 
 
71
         !c.contains("two"));   
 
72
        assertTrue("Collection shouldn't contain illegal element", 
 
73
         !c.contains(new Integer(3)));   
 
74
        assertTrue("Collection shouldn't contain illegal element", 
 
75
         !c.contains("four"));   
 
76
    }
 
77
 
 
78
}