1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.bidimap;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.Serializable;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.TreeMap;
33
34 import org.apache.commons.collections4.SortedBidiMap;
35 import org.apache.commons.collections4.comparators.ComparableComparator;
36 import org.apache.commons.collections4.comparators.ReverseComparator;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 @SuppressWarnings("boxing")
43 public class DualTreeBidiMap2Test<K extends Comparable<K>, V extends Comparable<V>> extends AbstractSortedBidiMapTest<K, V> {
44
45 private static final class IntegerComparator implements Comparator<Integer>, Serializable{
46 private static final long serialVersionUID = 1L;
47 @Override
48 public int compare(final Integer o1, final Integer o2) {
49 return o1.compareTo(o2);
50 }
51 }
52
53 public DualTreeBidiMap2Test() {
54 super(DualTreeBidiMap2Test.class.getSimpleName());
55 }
56
57 @Override
58 public String getCompatibilityVersion() {
59 return "4.Test2";
60 }
61
62
63
64
65 @Override
66 public String[] ignoredTests() {
67 final String recursiveTest = "DualTreeBidiMap2Test.bulkTestInverseMap.bulkTestInverseMap";
68 return new String[] { recursiveTest };
69 }
70
71 @Override
72 public TreeMap<K, V> makeConfirmedMap() {
73 return new TreeMap<>(new ReverseComparator<>(ComparableComparator.<K>comparableComparator()));
74 }
75
76 @Override
77 public DualTreeBidiMap<K, V> makeObject() {
78 return new DualTreeBidiMap<>(
79 new ReverseComparator<>(ComparableComparator.<K>comparableComparator()),
80 new ReverseComparator<>(ComparableComparator.<V>comparableComparator()));
81 }
82
83 @Test
84 public void testCollections364() throws Exception {
85 final DualTreeBidiMap<String, Integer> original = new DualTreeBidiMap<>(
86 String.CASE_INSENSITIVE_ORDER, new IntegerComparator());
87 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
88 final ObjectOutputStream out = new ObjectOutputStream(buffer);
89 out.writeObject(original);
90 out.close();
91
92 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
93 @SuppressWarnings("unchecked")
94 final DualTreeBidiMap<String, Integer> deserialized = (DualTreeBidiMap<String, Integer>) in.readObject();
95 in.close();
96
97 assertNotNull(original.comparator());
98 assertNotNull(deserialized.comparator());
99 assertEquals(original.comparator().getClass(), deserialized.comparator().getClass());
100 assertEquals(original.valueComparator().getClass(), deserialized.valueComparator().getClass());
101 }
102
103 @Test
104 public void testComparator() {
105 resetEmpty();
106 final SortedBidiMap<K, V> bidi = (SortedBidiMap<K, V>) map;
107 assertNotNull(bidi.comparator());
108 assertTrue(bidi.comparator() instanceof ReverseComparator);
109 }
110
111 @Test
112 public void testComparator2() {
113 final DualTreeBidiMap<String, Integer> dtbm = new DualTreeBidiMap<>(
114 String.CASE_INSENSITIVE_ORDER, null);
115 dtbm.put("two", 0);
116 dtbm.put("one", 1);
117 assertEquals("one", dtbm.firstKey());
118 assertEquals("two", dtbm.lastKey());
119
120 }
121
122 @Test
123 public void testSerializeDeserializeCheckComparator() throws Exception {
124 final SortedBidiMap<?, ?> obj = makeObject();
125 if (obj instanceof Serializable && isTestSerialization()) {
126 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
127 final ObjectOutputStream out = new ObjectOutputStream(buffer);
128 out.writeObject(obj);
129 out.close();
130
131 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
132 final Object dest = in.readObject();
133 in.close();
134
135 final SortedBidiMap<?, ?> bidi = (SortedBidiMap<?, ?>) dest;
136 assertNotNull(obj.comparator());
137 assertNotNull(bidi.comparator());
138 assertTrue(bidi.comparator() instanceof ReverseComparator);
139 }
140 }
141
142 @Test
143 public void testSortOrder() throws Exception {
144 final SortedBidiMap<K, V> sm = makeFullMap();
145
146
147 List<K> newSortedKeys = getAsList(getSampleKeys());
148 newSortedKeys.sort(new ReverseComparator<>(ComparableComparator.<K>comparableComparator()));
149 newSortedKeys = Collections.unmodifiableList(newSortedKeys);
150
151 final Iterator<K> mapIter = sm.keySet().iterator();
152 for (final K expectedKey : newSortedKeys) {
153 final K mapKey = mapIter.next();
154 assertNotNull(expectedKey, "key in sorted list may not be null");
155 assertNotNull(mapKey, "key in map may not be null");
156 assertEquals(expectedKey, mapKey, "key from sorted list and map must be equal");
157 }
158 }
159
160
161
162
163
164
165
166 }