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.SortedBag;
025:
026: /**
027: * Extension of {@link TestBag} for exercising the {@link TypedSortedBag}
028: * implementation.
029: *
030: * @since Commons Collections 3.0
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Phil Steitz
034: */
035: public class TestTypedSortedBag extends AbstractTestSortedBag {
036:
037: public TestTypedSortedBag(String testName) {
038: super (testName);
039: }
040:
041: public static Test suite() {
042: return new TestSuite(TestTypedSortedBag.class);
043: }
044:
045: public static void main(String args[]) {
046: String[] testCaseName = { TestTypedSortedBag.class.getName() };
047: junit.textui.TestRunner.main(testCaseName);
048: }
049:
050: //--------------------------------------------------------------------------
051:
052: protected Class stringClass = this .getName().getClass();
053: private Object obj = new Object();
054: protected Class objectClass = obj.getClass();
055: protected SortedBag nullBag = null;
056:
057: protected SortedBag decorateBag(SortedBag bag, Class claz) {
058: return TypedSortedBag.decorate(bag, claz);
059: }
060:
061: public Bag makeBag() {
062: return decorateBag(new TreeBag(), objectClass);
063: }
064:
065: protected Bag makeTestBag() {
066: return decorateBag(new TreeBag(), stringClass);
067: }
068:
069: //--------------------------------------------------------------------------
070:
071: public void testDecorate() {
072: SortedBag bag = decorateBag(new TreeBag(), stringClass);
073: try {
074: SortedBag bag3 = decorateBag(new TreeBag(), null);
075: fail("Expecting IllegalArgumentException for null predicate");
076: } catch (IllegalArgumentException e) {
077: }
078: try {
079: SortedBag bag4 = decorateBag(nullBag, stringClass);
080: fail("Expecting IllegalArgumentException for null bag");
081: } catch (IllegalArgumentException e) {
082: }
083: }
084:
085: public void testSortOrder() {
086: SortedBag bag = decorateBag(new TreeBag(), stringClass);
087: String one = "one";
088: String two = "two";
089: String three = "three";
090: bag.add(one);
091: bag.add(two);
092: bag.add(three);
093: assertEquals("first element", bag.first(), one);
094: assertEquals("last element", bag.last(), two);
095: Comparator c = bag.comparator();
096: assertTrue("natural order, so comparator should be null",
097: c == null);
098: }
099:
100: protected boolean skipSerializedCanonicalTests() {
101: return true;
102: }
103:
104: }
|