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