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.collection;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.collections.Transformer;
027: import org.apache.commons.collections.TransformerUtils;
028:
029: /**
030: * Extension of {@link TestCollection} for exercising the {@link TransformedCollection}
031: * implementation.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestTransformedCollection extends AbstractTestCollection {
039:
040: private static class StringToInteger implements Transformer {
041: public Object transform(Object input) {
042: return new Integer((String) input);
043: }
044: }
045:
046: public static final Transformer NOOP_TRANSFORMER = TransformerUtils
047: .nopTransformer();
048: public static final Transformer STRING_TO_INTEGER_TRANSFORMER = new StringToInteger();
049:
050: public TestTransformedCollection(String testName) {
051: super (testName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestTransformedCollection.class);
056: }
057:
058: public static void main(String args[]) {
059: String[] testCaseName = { TestTransformedCollection.class
060: .getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: //-----------------------------------------------------------------------
065: public Collection makeConfirmedCollection() {
066: return new ArrayList();
067: }
068:
069: public Collection makeConfirmedFullCollection() {
070: List list = new ArrayList();
071: list.addAll(Arrays.asList(getFullElements()));
072: return list;
073: }
074:
075: public Collection makeCollection() {
076: return TransformedCollection.decorate(new ArrayList(),
077: NOOP_TRANSFORMER);
078: }
079:
080: public Collection makeFullCollection() {
081: List list = new ArrayList();
082: list.addAll(Arrays.asList(getFullElements()));
083: return TransformedCollection.decorate(list, NOOP_TRANSFORMER);
084: }
085:
086: //-----------------------------------------------------------------------
087: public Object[] getFullElements() {
088: return new Object[] { "1", "3", "5", "7", "2", "4", "6" };
089: }
090:
091: public Object[] getOtherElements() {
092: return new Object[] { "9", "88", "678", "87", "98", "78", "99" };
093: }
094:
095: //-----------------------------------------------------------------------
096: public void testTransformedCollection() {
097: Collection coll = TransformedCollection.decorate(
098: new ArrayList(), STRING_TO_INTEGER_TRANSFORMER);
099: assertEquals(0, coll.size());
100: Object[] els = getFullElements();
101: for (int i = 0; i < els.length; i++) {
102: coll.add(els[i]);
103: assertEquals(i + 1, coll.size());
104: assertEquals(true, coll.contains(new Integer(
105: (String) els[i])));
106: assertEquals(false, coll.contains(els[i]));
107: }
108:
109: assertEquals(true, coll.remove(new Integer((String) els[0])));
110: }
111:
112: public String getCompatibilityVersion() {
113: return "3.1";
114: }
115:
116: // public void testCreate() throws Exception {
117: // resetEmpty();
118: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedCollection.emptyCollection.version3.1.obj");
119: // resetFull();
120: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedCollection.fullCollection.version3.1.obj");
121: // }
122:
123: }
|