001: /*
002: * Copyright 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.bidimap;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.Serializable;
023: import java.util.Arrays;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.TreeMap;
029:
030: import junit.framework.Test;
031: import junit.textui.TestRunner;
032:
033: import org.apache.commons.collections.BidiMap;
034: import org.apache.commons.collections.BulkTest;
035: import org.apache.commons.collections.SortedBidiMap;
036: import org.apache.commons.collections.comparators.ComparableComparator;
037: import org.apache.commons.collections.comparators.ReverseComparator;
038:
039: /**
040: * JUnit tests.
041: *
042: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
043: *
044: * @author Matthew Hawthorne
045: * @author Stephen Colebourne
046: * @author Jonas Van Poucke
047: */
048: public class TestDualTreeBidiMap2 extends AbstractTestSortedBidiMap {
049:
050: public static void main(String[] args) {
051: TestRunner.run(suite());
052: }
053:
054: public static Test suite() {
055: return BulkTest.makeSuite(TestDualTreeBidiMap2.class);
056: }
057:
058: public TestDualTreeBidiMap2(String testName) {
059: super (testName);
060: }
061:
062: public BidiMap makeEmptyBidiMap() {
063: return new DualTreeBidiMap(new ReverseComparator(
064: ComparableComparator.getInstance()));
065: }
066:
067: public Map makeConfirmedMap() {
068: return new TreeMap(new ReverseComparator(ComparableComparator
069: .getInstance()));
070: }
071:
072: public void testComparator() {
073: resetEmpty();
074: SortedBidiMap bidi = (SortedBidiMap) map;
075: assertNotNull(bidi.comparator());
076: assertTrue(bidi.comparator() instanceof ReverseComparator);
077: }
078:
079: public void testSerializeDeserializeCheckComparator()
080: throws Exception {
081: SortedBidiMap obj = (SortedBidiMap) makeObject();
082: if (obj instanceof Serializable && isTestSerialization()) {
083: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
084: ObjectOutputStream out = new ObjectOutputStream(buffer);
085: out.writeObject(obj);
086: out.close();
087:
088: ObjectInputStream in = new ObjectInputStream(
089: new ByteArrayInputStream(buffer.toByteArray()));
090: Object dest = in.readObject();
091: in.close();
092:
093: SortedBidiMap bidi = (SortedBidiMap) dest;
094: assertNotNull(obj.comparator());
095: assertNotNull(bidi.comparator());
096: assertTrue(bidi.comparator() instanceof ReverseComparator);
097: }
098: }
099:
100: public void testSortOrder() throws Exception {
101: SortedBidiMap sm = (SortedBidiMap) makeFullMap();
102:
103: // Sort by the comparator used in the makeEmptyBidiMap() method
104: List newSortedKeys = Arrays.asList(getSampleKeys());
105: Collections.sort(newSortedKeys, new ReverseComparator(
106: ComparableComparator.getInstance()));
107: newSortedKeys = Collections.unmodifiableList(newSortedKeys);
108:
109: Iterator mapIter = sm.keySet().iterator();
110: Iterator expectedIter = newSortedKeys.iterator();
111: while (expectedIter.hasNext()) {
112: Object expectedKey = expectedIter.next();
113: Object mapKey = mapIter.next();
114: assertNotNull("key in sorted list may not be null",
115: expectedKey);
116: assertNotNull("key in map may not be null", mapKey);
117: assertEquals("key from sorted list and map must be equal",
118: expectedKey, mapKey);
119: }
120: }
121:
122: public String getCompatibilityVersion() {
123: return "3.Test2";
124: }
125:
126: /**
127: * Override to prevent infinite recursion of tests.
128: */
129: public String[] ignoredTests() {
130: return new String[] { "TestDualTreeBidiMap2.bulkTestInverseMap.bulkTestInverseMap" };
131: }
132:
133: // public void testCreate() throws Exception {
134: // resetEmpty();
135: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/DualTreeBidiMap.emptyCollection.version3.Test2.obj");
136: // resetFull();
137: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/DualTreeBidiMap.fullCollection.version3.Test2.obj");
138: // }
139: }
|