001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.util.Comparator;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.collections.Bag;
024: import org.apache.commons.collections.Predicate;
025: import org.apache.commons.collections.PredicateUtils;
026: import org.apache.commons.collections.SortedBag;
027:
028: /**
029: * Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag}
030: * implementation.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Phil Steitz
036: */
037: public class TestPredicatedSortedBag extends AbstractTestSortedBag {
038:
039: private SortedBag nullBag = null;
040:
041: public TestPredicatedSortedBag(String testName) {
042: super (testName);
043: }
044:
045: public static Test suite() {
046: return new TestSuite(TestPredicatedSortedBag.class);
047: }
048:
049: public static void main(String args[]) {
050: String[] testCaseName = { TestPredicatedSortedBag.class
051: .getName() };
052: junit.textui.TestRunner.main(testCaseName);
053: }
054:
055: //--------------------------------------------------------------------------
056:
057: protected Predicate stringPredicate() {
058: return new Predicate() {
059: public boolean evaluate(Object o) {
060: return o instanceof String;
061: }
062: };
063: }
064:
065: protected Predicate truePredicate = PredicateUtils.truePredicate();
066:
067: protected SortedBag decorateBag(SortedBag bag, Predicate predicate) {
068: return PredicatedSortedBag.decorate(bag, predicate);
069: }
070:
071: public Bag makeBag() {
072: return decorateBag(new TreeBag(), truePredicate);
073: }
074:
075: protected Bag makeTestBag() {
076: return decorateBag(new TreeBag(), stringPredicate());
077: }
078:
079: //--------------------------------------------------------------------------
080:
081: public void testDecorate() {
082: SortedBag bag = decorateBag(new TreeBag(), stringPredicate());
083: SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag();
084: try {
085: SortedBag bag3 = decorateBag(new TreeBag(), null);
086: fail("Expecting IllegalArgumentException for null predicate");
087: } catch (IllegalArgumentException e) {
088: }
089: try {
090: SortedBag bag4 = decorateBag(nullBag, stringPredicate());
091: fail("Expecting IllegalArgumentException for null bag");
092: } catch (IllegalArgumentException e) {
093: }
094: }
095:
096: public void testSortOrder() {
097: SortedBag bag = decorateBag(new TreeBag(), stringPredicate());
098: String one = "one";
099: String two = "two";
100: String three = "three";
101: bag.add(one);
102: bag.add(two);
103: bag.add(three);
104: assertEquals("first element", bag.first(), one);
105: assertEquals("last element", bag.last(), two);
106: Comparator c = bag.comparator();
107: assertTrue("natural order, so comparator should be null",
108: c == null);
109: }
110:
111: public String getCompatibilityVersion() {
112: return "3.1";
113: }
114:
115: // public void testCreate() throws Exception {
116: // Bag bag = makeBag();
117: // writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedSortedBag.emptyCollection.version3.1.obj");
118: // bag = makeBag();
119: // bag.add("A");
120: // bag.add("A");
121: // bag.add("B");
122: // bag.add("B");
123: // bag.add("C");
124: // writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedSortedBag.fullCollection.version3.1.obj");
125: // }
126:
127: }
|