1/*
2 * Copyright (C) 2007 The Guava Authors
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
17package com.google.common.collect;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.testing.SerializableTester;
24
25import junit.framework.TestCase;
26
27import java.util.Arrays;
28import java.util.Collection;
29import java.util.Comparator;
30import java.util.Iterator;
31import java.util.Map;
32import java.util.SortedSet;
33
34/**
35 * Unit tests for {@code TreeMultimap} with explicit comparators.
36 *
37 * @author Jared Levy
38 */
39@GwtCompatible(emulated = true)
40public class TreeMultimapExplicitTest extends TestCase {
41
42  /**
43   * Compare strings lengths, and if the lengths are equal compare the strings.
44   * A {@code null} is less than any non-null value.
45   */
46  private enum StringLength implements Comparator<String> {
47    COMPARATOR;
48
49    @Override
50    public int compare(String first, String second) {
51      if (first == second) {
52        return 0;
53      } else if (first == null) {
54        return -1;
55      } else if (second == null) {
56        return 1;
57      } else if (first.length() != second.length()) {
58        return first.length() - second.length();
59      } else {
60        return first.compareTo(second);
61      }
62    }
63  }
64
65  /**
66   * Decreasing integer values. A {@code null} comes before any non-null value.
67   */
68  private static final Comparator<Integer> DECREASING_INT_COMPARATOR =
69      Ordering.<Integer>natural().reverse().nullsFirst();
70
71  private SetMultimap<String, Integer> create() {
72    return TreeMultimap.create(
73        StringLength.COMPARATOR, DECREASING_INT_COMPARATOR);
74  }
75
76  /**
77   * Create and populate a {@code TreeMultimap} with explicit comparators.
78   */
79  private TreeMultimap<String, Integer> createPopulate() {
80    TreeMultimap<String, Integer> multimap = TreeMultimap.create(
81        StringLength.COMPARATOR, DECREASING_INT_COMPARATOR);
82    multimap.put("google", 2);
83    multimap.put("google", 6);
84    multimap.put(null, 3);
85    multimap.put(null, 1);
86    multimap.put(null, 7);
87    multimap.put("tree", 0);
88    multimap.put("tree", null);
89    return multimap;
90  }
91
92  /**
93   * Test that a TreeMultimap created from another uses the natural ordering.
94   */
95  public void testMultimapCreateFromTreeMultimap() {
96    TreeMultimap<String, Integer> tree = TreeMultimap.create(
97        StringLength.COMPARATOR, DECREASING_INT_COMPARATOR);
98    tree.put("google", 2);
99    tree.put("google", 6);
100    tree.put("tree", 0);
101    tree.put("tree", 3);
102    assertThat(tree.keySet()).has().exactly("tree", "google").inOrder();
103    assertThat(tree.get("google")).has().exactly(6, 2).inOrder();
104
105    TreeMultimap<String, Integer> copy = TreeMultimap.create(tree);
106    assertEquals(tree, copy);
107    assertThat(copy.keySet()).has().exactly("google", "tree").inOrder();
108    assertThat(copy.get("google")).has().exactly(2, 6).inOrder();
109    assertEquals(Ordering.natural(), copy.keyComparator());
110    assertEquals(Ordering.natural(), copy.valueComparator());
111    assertEquals(Ordering.natural(), copy.get("google").comparator());
112  }
113
114  public void testToString() {
115    Multimap<String, Integer> multimap = create();
116    multimap.put("foo", 3);
117    multimap.put("bar", 1);
118    multimap.putAll("foo", Arrays.asList(-1, 2, 4));
119    multimap.putAll("bar", Arrays.asList(2, 3));
120    multimap.put("foo", 1);
121    assertEquals("{bar=[3, 2, 1], foo=[4, 3, 2, 1, -1]}",
122        multimap.toString());
123  }
124
125  public void testGetComparator() {
126    TreeMultimap<String, Integer> multimap = createPopulate();
127    assertEquals(StringLength.COMPARATOR, multimap.keyComparator());
128    assertEquals(DECREASING_INT_COMPARATOR, multimap.valueComparator());
129  }
130
131  public void testOrderedGet() {
132    TreeMultimap<String, Integer> multimap = createPopulate();
133    assertThat(multimap.get(null)).has().exactly(7, 3, 1).inOrder();
134    assertThat(multimap.get("google")).has().exactly(6, 2).inOrder();
135    assertThat(multimap.get("tree")).has().exactly(null, 0).inOrder();
136  }
137
138  public void testOrderedKeySet() {
139    TreeMultimap<String, Integer> multimap = createPopulate();
140    assertThat(multimap.keySet()).has().exactly(null, "tree", "google").inOrder();
141  }
142
143  public void testOrderedAsMapEntries() {
144    TreeMultimap<String, Integer> multimap = createPopulate();
145    Iterator<Map.Entry<String, Collection<Integer>>> iterator =
146        multimap.asMap().entrySet().iterator();
147    Map.Entry<String, Collection<Integer>> entry = iterator.next();
148    assertEquals(null, entry.getKey());
149    assertThat(entry.getValue()).has().exactly(7, 3, 1);
150    entry = iterator.next();
151    assertEquals("tree", entry.getKey());
152    assertThat(entry.getValue()).has().exactly(null, 0);
153    entry = iterator.next();
154    assertEquals("google", entry.getKey());
155    assertThat(entry.getValue()).has().exactly(6, 2);
156  }
157
158  public void testOrderedEntries() {
159    TreeMultimap<String, Integer> multimap = createPopulate();
160    assertThat(multimap.entries()).has().exactly(
161        Maps.immutableEntry((String) null, 7),
162        Maps.immutableEntry((String) null, 3),
163        Maps.immutableEntry((String) null, 1),
164        Maps.immutableEntry("tree", (Integer) null),
165        Maps.immutableEntry("tree", 0),
166        Maps.immutableEntry("google", 6),
167        Maps.immutableEntry("google", 2)).inOrder();
168  }
169
170  public void testOrderedValues() {
171    TreeMultimap<String, Integer> multimap = createPopulate();
172    assertThat(multimap.values()).has().exactly(7, 3, 1, null, 0, 6, 2).inOrder();
173  }
174
175  public void testComparator() {
176    TreeMultimap<String, Integer> multimap = createPopulate();
177    assertEquals(DECREASING_INT_COMPARATOR, multimap.get("foo").comparator());
178    assertEquals(DECREASING_INT_COMPARATOR,
179        multimap.get("missing").comparator());
180  }
181
182  public void testMultimapComparators() {
183    Multimap<String, Integer> multimap = create();
184    multimap.put("foo", 3);
185    multimap.put("bar", 1);
186    multimap.putAll("foo", Arrays.asList(-1, 2, 4));
187    multimap.putAll("bar", Arrays.asList(2, 3));
188    multimap.put("foo", 1);
189    TreeMultimap<String, Integer> copy =
190        TreeMultimap.create(StringLength.COMPARATOR, DECREASING_INT_COMPARATOR);
191    copy.putAll(multimap);
192    assertEquals(multimap, copy);
193    assertEquals(StringLength.COMPARATOR, copy.keyComparator());
194    assertEquals(DECREASING_INT_COMPARATOR, copy.valueComparator());
195  }
196
197  public void testSortedKeySet() {
198    TreeMultimap<String, Integer> multimap = createPopulate();
199    SortedSet<String> keySet = multimap.keySet();
200
201    assertEquals(null, keySet.first());
202    assertEquals("google", keySet.last());
203    assertEquals(StringLength.COMPARATOR, keySet.comparator());
204    assertEquals(Sets.newHashSet(null, "tree"), keySet.headSet("yahoo"));
205    assertEquals(Sets.newHashSet("google"), keySet.tailSet("yahoo"));
206    assertEquals(Sets.newHashSet("tree"), keySet.subSet("ask", "yahoo"));
207  }
208
209  @GwtIncompatible("SerializableTester")
210  public void testExplicitComparatorSerialization() {
211    TreeMultimap<String, Integer> multimap = createPopulate();
212    TreeMultimap<String, Integer> copy
213        = SerializableTester.reserializeAndAssert(multimap);
214    assertThat(copy.values()).has().exactly(7, 3, 1, null, 0, 6, 2).inOrder();
215    assertThat(copy.keySet()).has().exactly(null, "tree", "google").inOrder();
216    assertEquals(multimap.keyComparator(), copy.keyComparator());
217    assertEquals(multimap.valueComparator(), copy.valueComparator());
218  }
219}
220