View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link DualTreeBidiMap}.
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  
48          @Override
49          public int compare(final Integer o1, final Integer o2) {
50              return o1.compareTo(o2);
51          }
52      }
53  
54      @Override
55      public String getCompatibilityVersion() {
56          return "4.Test2";
57      }
58  
59      @Override
60      public boolean isAllowNullValueGet() {
61          // TODO Is this a bug or a feature?
62          return true;
63      }
64  
65      @Override
66      public TreeMap<K, V> makeConfirmedMap() {
67          return new TreeMap<>(new ReverseComparator<>(ComparableComparator.<K>comparableComparator()));
68      }
69  
70      @Override
71      public DualTreeBidiMap<K, V> makeObject() {
72          return new DualTreeBidiMap<>(
73                  new ReverseComparator<>(ComparableComparator.<K>comparableComparator()),
74                  new ReverseComparator<>(ComparableComparator.<V>comparableComparator()));
75      }
76  
77      @Test
78      public void testCollections364() throws Exception {
79          final DualTreeBidiMap<String, Integer> original = new DualTreeBidiMap<>(
80                  String.CASE_INSENSITIVE_ORDER, new IntegerComparator());
81          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
82          final ObjectOutputStream out = new ObjectOutputStream(buffer);
83          out.writeObject(original);
84          out.close();
85  
86          final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
87          @SuppressWarnings("unchecked")
88          final DualTreeBidiMap<String, Integer> deserialized = (DualTreeBidiMap<String, Integer>) in.readObject();
89          in.close();
90  
91          assertNotNull(original.comparator());
92          assertNotNull(deserialized.comparator());
93          assertEquals(original.comparator().getClass(), deserialized.comparator().getClass());
94          assertEquals(original.valueComparator().getClass(), deserialized.valueComparator().getClass());
95      }
96  
97      @Test
98      public void testComparator() {
99          resetEmpty();
100         final SortedBidiMap<K, V> bidi = (SortedBidiMap<K, V>) map;
101         assertNotNull(bidi.comparator());
102         assertTrue(bidi.comparator() instanceof ReverseComparator);
103     }
104 
105     @Test
106     public void testComparator2() {
107         final DualTreeBidiMap<String, Integer> dtbm = new DualTreeBidiMap<>(
108                 String.CASE_INSENSITIVE_ORDER, null);
109         dtbm.put("two", 0);
110         dtbm.put("one", 1);
111         assertEquals("one", dtbm.firstKey());
112         assertEquals("two", dtbm.lastKey());
113 
114     }
115 
116     @Test
117     public void testSerializeDeserializeCheckComparator() throws Exception {
118         final SortedBidiMap<?, ?> obj = makeObject();
119         if (obj instanceof Serializable && isTestSerialization()) {
120             final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
121             final ObjectOutputStream out = new ObjectOutputStream(buffer);
122             out.writeObject(obj);
123             out.close();
124 
125             final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
126             final Object dest = in.readObject();
127             in.close();
128 
129             final SortedBidiMap<?, ?> bidi = (SortedBidiMap<?, ?>) dest;
130             assertNotNull(obj.comparator());
131             assertNotNull(bidi.comparator());
132             assertTrue(bidi.comparator() instanceof ReverseComparator);
133         }
134     }
135 
136     @Test
137     public void testSortOrder() throws Exception {
138         final SortedBidiMap<K, V> sm = makeFullMap();
139 
140         // Sort by the comparator used in the makeEmptyBidiMap() method
141         List<K> newSortedKeys = getAsList(getSampleKeys());
142         newSortedKeys.sort(new ReverseComparator<>(ComparableComparator.<K>comparableComparator()));
143         newSortedKeys = Collections.unmodifiableList(newSortedKeys);
144 
145         final Iterator<K> mapIter = sm.keySet().iterator();
146         for (final K expectedKey : newSortedKeys) {
147             final K mapKey = mapIter.next();
148             assertNotNull(expectedKey, "key in sorted list may not be null");
149             assertNotNull(mapKey, "key in map may not be null");
150             assertEquals(expectedKey, mapKey, "key from sorted list and map must be equal");
151         }
152     }
153 
154 //    public void testCreate() throws Exception {
155 //        resetEmpty();
156 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/DualTreeBidiMap.emptyCollection.version4.Test2.obj");
157 //        resetFull();
158 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/DualTreeBidiMap.fullCollection.version4.Test2.obj");
159 //    }
160 }