1 16 package org.apache.commons.collections.bidimap; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.TreeMap ; 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 48 public class TestDualTreeBidiMap2 extends AbstractTestSortedBidiMap { 49 50 public static void main(String [] args) { 51 TestRunner.run(suite()); 52 } 53 54 public static Test suite() { 55 return BulkTest.makeSuite(TestDualTreeBidiMap2.class); 56 } 57 58 public TestDualTreeBidiMap2(String testName) { 59 super(testName); 60 } 61 62 public BidiMap makeEmptyBidiMap() { 63 return new DualTreeBidiMap(new ReverseComparator(ComparableComparator.getInstance())); 64 } 65 66 public Map makeConfirmedMap() { 67 return new TreeMap (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 { 78 SortedBidiMap obj = (SortedBidiMap) makeObject(); 79 if (obj instanceof Serializable && isTestSerialization()) { 80 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 81 ObjectOutputStream out = new ObjectOutputStream (buffer); 82 out.writeObject(obj); 83 out.close(); 84 85 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 86 Object 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 { 97 SortedBidiMap sm = (SortedBidiMap) makeFullMap(); 98 99 List newSortedKeys = Arrays.asList(getSampleKeys()); 101 Collections.sort(newSortedKeys, new ReverseComparator(ComparableComparator.getInstance())); 102 newSortedKeys = Collections.unmodifiableList(newSortedKeys); 103 104 Iterator mapIter = sm.keySet().iterator(); 105 Iterator expectedIter = newSortedKeys.iterator(); 106 while (expectedIter.hasNext()) { 107 Object expectedKey = expectedIter.next(); 108 Object 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 getCompatibilityVersion() { 116 return "3.Test2"; 117 } 118 119 122 public String [] ignoredTests() { 123 return new String [] {"TestDualTreeBidiMap2.bulkTestInverseMap.bulkTestInverseMap"}; 124 } 125 126 } 133 | Popular Tags |