KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bidimap > TestDualTreeBidiMap2


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bidimap;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.TreeMap JavaDoc;
29
30 import junit.framework.Test;
31 import junit.textui.TestRunner;
32
33 import org.apache.commons.collections.BidiMap;
34 import org.apache.commons.collections.BulkTest;
35 import org.apache.commons.collections.SortedBidiMap;
36 import org.apache.commons.collections.comparators.ComparableComparator;
37 import org.apache.commons.collections.comparators.ReverseComparator;
38
39 /**
40  * JUnit tests.
41  *
42  * @version $Revision: 1.2 $ $Date: 2004/06/21 22:30:05 $
43  *
44  * @author Matthew Hawthorne
45  * @author Stephen Colebourne
46  * @author Jonas Van Poucke
47  */

48 public class TestDualTreeBidiMap2 extends AbstractTestSortedBidiMap {
49
50     public static void main(String JavaDoc[] args) {
51         TestRunner.run(suite());
52     }
53     
54     public static Test suite() {
55         return BulkTest.makeSuite(TestDualTreeBidiMap2.class);
56     }
57
58     public TestDualTreeBidiMap2(String JavaDoc testName) {
59         super(testName);
60     }
61
62     public BidiMap makeEmptyBidiMap() {
63         return new DualTreeBidiMap(new ReverseComparator(ComparableComparator.getInstance()));
64     }
65
66     public Map JavaDoc makeConfirmedMap() {
67         return new TreeMap JavaDoc(new ReverseComparator(ComparableComparator.getInstance()));
68     }
69
70     public void testComparator() {
71         resetEmpty();
72         SortedBidiMap bidi = (SortedBidiMap) map;
73         assertNotNull(bidi.comparator());
74         assertTrue(bidi.comparator() instanceof ReverseComparator);
75     }
76
77     public void testSerializeDeserializeCheckComparator() throws Exception JavaDoc {
78         SortedBidiMap obj = (SortedBidiMap) makeObject();
79         if (obj instanceof Serializable JavaDoc && isTestSerialization()) {
80             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
81             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
82             out.writeObject(obj);
83             out.close();
84
85             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
86             Object JavaDoc dest = in.readObject();
87             in.close();
88             
89             SortedBidiMap bidi = (SortedBidiMap) dest;
90             assertNotNull(obj.comparator());
91             assertNotNull(bidi.comparator());
92             assertTrue(bidi.comparator() instanceof ReverseComparator);
93         }
94     }
95
96     public void testSortOrder() throws Exception JavaDoc {
97         SortedBidiMap sm = (SortedBidiMap) makeFullMap();
98
99         // Sort by the comparator used in the makeEmptyBidiMap() method
100
List JavaDoc newSortedKeys = Arrays.asList(getSampleKeys());
101         Collections.sort(newSortedKeys, new ReverseComparator(ComparableComparator.getInstance()));
102         newSortedKeys = Collections.unmodifiableList(newSortedKeys);
103
104         Iterator JavaDoc mapIter = sm.keySet().iterator();
105         Iterator JavaDoc expectedIter = newSortedKeys.iterator();
106         while (expectedIter.hasNext()) {
107             Object JavaDoc expectedKey = expectedIter.next();
108             Object JavaDoc mapKey = mapIter.next();
109             assertNotNull("key in sorted list may not be null", expectedKey);
110             assertNotNull("key in map may not be null", mapKey);
111             assertEquals("key from sorted list and map must be equal", expectedKey, mapKey);
112         }
113     }
114
115     public String JavaDoc getCompatibilityVersion() {
116         return "3.Test2";
117     }
118
119     /**
120      * Override to prevent infinite recursion of tests.
121      */

122     public String JavaDoc[] ignoredTests() {
123         return new String JavaDoc[] {"TestDualTreeBidiMap2.bulkTestInverseMap.bulkTestInverseMap"};
124     }
125     
126 // public void testCreate() throws Exception {
127
// resetEmpty();
128
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/DualTreeBidiMap.emptyCollection.version3.Test2.obj");
129
// resetFull();
130
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/DualTreeBidiMap.fullCollection.version3.Test2.obj");
131
// }
132
}
133
Popular Tags