1/*
2 * Copyright (C) 2009 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.testing;
18
19import com.google.common.annotations.GwtCompatible;
20
21import java.util.Iterator;
22import java.util.Map.Entry;
23import java.util.SortedMap;
24
25/**
26 * Tests representing the contract of {@link SortedMap}. Concrete subclasses of
27 * this base class test conformance of concrete {@link SortedMap} subclasses to
28 * that contract.
29 *
30 * @author Jared Levy
31 */
32// TODO: Use this class to test classes besides ImmutableSortedMap.
33@GwtCompatible
34public abstract class SortedMapInterfaceTest<K, V>
35    extends MapInterfaceTest<K, V> {
36
37  protected SortedMapInterfaceTest(boolean allowsNullKeys,
38      boolean allowsNullValues, boolean supportsPut, boolean supportsRemove,
39      boolean supportsClear) {
40    super(allowsNullKeys, allowsNullValues, supportsPut, supportsRemove,
41        supportsClear);
42  }
43
44  @Override protected abstract SortedMap<K, V> makeEmptyMap()
45      throws UnsupportedOperationException;
46
47  @Override protected abstract SortedMap<K, V> makePopulatedMap()
48      throws UnsupportedOperationException;
49
50  @Override protected SortedMap<K, V> makeEitherMap() {
51    try {
52      return makePopulatedMap();
53    } catch (UnsupportedOperationException e) {
54      return makeEmptyMap();
55    }
56  }
57
58  public void testTailMapWriteThrough() {
59    final SortedMap<K, V> map;
60    try {
61      map = makePopulatedMap();
62    } catch (UnsupportedOperationException e) {
63      return;
64    }
65    if (map.size() < 2 || !supportsPut) {
66      return;
67    }
68    Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
69    Entry<K, V> firstEntry = iterator.next();
70    Entry<K, V> secondEntry = iterator.next();
71    K key = secondEntry.getKey();
72    SortedMap<K, V> subMap = map.tailMap(key);
73    V value = getValueNotInPopulatedMap();
74    subMap.put(key, value);
75    assertEquals(secondEntry.getValue(), value);
76    assertEquals(map.get(key), value);
77    try {
78      subMap.put(firstEntry.getKey(), value);
79      fail("Expected IllegalArgumentException");
80    } catch (IllegalArgumentException expected) {
81    }
82  }
83
84  public void testTailMapRemoveThrough() {
85    final SortedMap<K, V> map;
86    try {
87      map = makePopulatedMap();
88    } catch (UnsupportedOperationException e) {
89      return;
90    }
91    int oldSize = map.size();
92    if (map.size() < 2 || !supportsRemove) {
93      return;
94    }
95    Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
96    Entry<K, V> firstEntry = iterator.next();
97    Entry<K, V> secondEntry = iterator.next();
98    K key = secondEntry.getKey();
99    SortedMap<K, V> subMap = map.tailMap(key);
100    subMap.remove(key);
101    assertNull(subMap.remove(firstEntry.getKey()));
102    assertEquals(map.size(), oldSize - 1);
103    assertFalse(map.containsKey(key));
104    assertEquals(subMap.size(), oldSize - 2);
105  }
106
107  public void testTailMapClearThrough() {
108    final SortedMap<K, V> map;
109    try {
110      map = makePopulatedMap();
111    } catch (UnsupportedOperationException e) {
112      return;
113    }
114    int oldSize = map.size();
115    if (map.size() < 2 || !supportsClear) {
116      return;
117    }
118    Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
119    iterator.next(); // advance
120    Entry<K, V> secondEntry = iterator.next();
121    K key = secondEntry.getKey();
122    SortedMap<K, V> subMap = map.tailMap(key);
123    int subMapSize = subMap.size();
124    subMap.clear();
125    assertEquals(map.size(), oldSize - subMapSize);
126    assertTrue(subMap.isEmpty());
127  }
128}
129