001: /*
002: * Copyright 2001-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;
017:
018: import junit.framework.Test;
019:
020: import org.apache.commons.collections.bag.HashBag;
021: import org.apache.commons.collections.bag.PredicatedBag;
022: import org.apache.commons.collections.bag.PredicatedSortedBag;
023: import org.apache.commons.collections.bag.SynchronizedBag;
024: import org.apache.commons.collections.bag.SynchronizedSortedBag;
025: import org.apache.commons.collections.bag.TransformedBag;
026: import org.apache.commons.collections.bag.TransformedSortedBag;
027: import org.apache.commons.collections.bag.TreeBag;
028: import org.apache.commons.collections.bag.UnmodifiableBag;
029: import org.apache.commons.collections.bag.UnmodifiableSortedBag;
030:
031: /**
032: * Tests for BagUtils factory methods.
033: *
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Phil Steitz
037: */
038: public class TestBagUtils extends BulkTest {
039:
040: public TestBagUtils(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: return BulkTest.makeSuite(TestBagUtils.class);
046: }
047:
048: //----------------------------------------------------------------------
049:
050: protected Class stringClass = this .getName().getClass();
051: protected Predicate truePredicate = PredicateUtils.truePredicate();
052: protected Transformer nopTransformer = TransformerUtils
053: .nopTransformer();
054:
055: //----------------------------------------------------------------------
056:
057: public void testSynchronizedBag() {
058: Bag bag = BagUtils.synchronizedBag(new HashBag());
059: assertTrue("Returned object should be a SynchronizedBag.",
060: bag instanceof SynchronizedBag);
061: try {
062: bag = BagUtils.synchronizedBag(null);
063: fail("Expecting IllegalArgumentException for null bag.");
064: } catch (IllegalArgumentException ex) {
065: // expected
066: }
067: }
068:
069: public void testUnmodifiableBag() {
070: Bag bag = BagUtils.unmodifiableBag(new HashBag());
071: assertTrue("Returned object should be an UnmodifiableBag.",
072: bag instanceof UnmodifiableBag);
073: try {
074: bag = BagUtils.unmodifiableBag(null);
075: fail("Expecting IllegalArgumentException for null bag.");
076: } catch (IllegalArgumentException ex) {
077: // expected
078: }
079: }
080:
081: public void testPredicatedBag() {
082: Bag bag = BagUtils.predicatedBag(new HashBag(), truePredicate);
083: assertTrue("Returned object should be a PredicatedBag.",
084: bag instanceof PredicatedBag);
085: try {
086: bag = BagUtils.predicatedBag(null, truePredicate);
087: fail("Expecting IllegalArgumentException for null bag.");
088: } catch (IllegalArgumentException ex) {
089: // expected
090: }
091: try {
092: bag = BagUtils.predicatedBag(new HashBag(), null);
093: fail("Expecting IllegalArgumentException for null predicate.");
094: } catch (IllegalArgumentException ex) {
095: // expected
096: }
097: }
098:
099: public void testTypedBag() {
100: Bag bag = BagUtils.typedBag(new HashBag(), stringClass);
101: assertTrue("Returned object should be a TypedBag.",
102: bag instanceof PredicatedBag);
103: try {
104: bag = BagUtils.typedBag(null, stringClass);
105: fail("Expecting IllegalArgumentException for null bag.");
106: } catch (IllegalArgumentException ex) {
107: // expected
108: }
109: try {
110: bag = BagUtils.typedBag(new HashBag(), null);
111: fail("Expecting IllegalArgumentException for null type.");
112: } catch (IllegalArgumentException ex) {
113: // expected
114: }
115: }
116:
117: public void testTransformedBag() {
118: Bag bag = BagUtils
119: .transformedBag(new HashBag(), nopTransformer);
120: assertTrue("Returned object should be an TransformedBag.",
121: bag instanceof TransformedBag);
122: try {
123: bag = BagUtils.transformedBag(null, nopTransformer);
124: fail("Expecting IllegalArgumentException for null bag.");
125: } catch (IllegalArgumentException ex) {
126: // expected
127: }
128: try {
129: bag = BagUtils.transformedBag(new HashBag(), null);
130: fail("Expecting IllegalArgumentException for null transformer.");
131: } catch (IllegalArgumentException ex) {
132: // expected
133: }
134: }
135:
136: public void testSynchronizedSortedBag() {
137: Bag bag = BagUtils.synchronizedSortedBag(new TreeBag());
138: assertTrue(
139: "Returned object should be a SynchronizedSortedBag.",
140: bag instanceof SynchronizedSortedBag);
141: try {
142: bag = BagUtils.synchronizedSortedBag(null);
143: fail("Expecting IllegalArgumentException for null bag.");
144: } catch (IllegalArgumentException ex) {
145: // expected
146: }
147: }
148:
149: public void testUnmodifiableSortedBag() {
150: Bag bag = BagUtils.unmodifiableSortedBag(new TreeBag());
151: assertTrue(
152: "Returned object should be an UnmodifiableSortedBag.",
153: bag instanceof UnmodifiableSortedBag);
154: try {
155: bag = BagUtils.unmodifiableSortedBag(null);
156: fail("Expecting IllegalArgumentException for null bag.");
157: } catch (IllegalArgumentException ex) {
158: // expected
159: }
160: }
161:
162: public void testPredicatedSortedBag() {
163: Bag bag = BagUtils.predicatedSortedBag(new TreeBag(),
164: truePredicate);
165: assertTrue("Returned object should be a PredicatedSortedBag.",
166: bag instanceof PredicatedSortedBag);
167: try {
168: bag = BagUtils.predicatedSortedBag(null, truePredicate);
169: fail("Expecting IllegalArgumentException for null bag.");
170: } catch (IllegalArgumentException ex) {
171: // expected
172: }
173: try {
174: bag = BagUtils.predicatedSortedBag(new TreeBag(), null);
175: fail("Expecting IllegalArgumentException for null predicate.");
176: } catch (IllegalArgumentException ex) {
177: // expected
178: }
179: }
180:
181: public void testTypedSortedBag() {
182: Bag bag = BagUtils.typedSortedBag(new TreeBag(), stringClass);
183: assertTrue("Returned object should be a TypedSortedBag.",
184: bag instanceof PredicatedSortedBag);
185: try {
186: bag = BagUtils.typedSortedBag(null, stringClass);
187: fail("Expecting IllegalArgumentException for null bag.");
188: } catch (IllegalArgumentException ex) {
189: // expected
190: }
191: try {
192: bag = BagUtils.typedSortedBag(new TreeBag(), null);
193: fail("Expecting IllegalArgumentException for null type.");
194: } catch (IllegalArgumentException ex) {
195: // expected
196: }
197: }
198:
199: public void testTransformedSortedBag() {
200: Bag bag = BagUtils.transformedSortedBag(new TreeBag(),
201: nopTransformer);
202: assertTrue("Returned object should be an TransformedSortedBag",
203: bag instanceof TransformedSortedBag);
204: try {
205: bag = BagUtils.transformedSortedBag(null, nopTransformer);
206: fail("Expecting IllegalArgumentException for null bag.");
207: } catch (IllegalArgumentException ex) {
208: // expected
209: }
210: try {
211: bag = BagUtils.transformedSortedBag(new TreeBag(), null);
212: fail("Expecting IllegalArgumentException for null transformer.");
213: } catch (IllegalArgumentException ex) {
214: // expected
215: }
216: }
217: }
|