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 com.google.common.annotations.GwtCompatible;
20import com.google.common.collect.testing.MapInterfaceTest;
21import com.google.common.collect.testing.SortedMapInterfaceTest;
22
23import java.util.SortedMap;
24import java.util.TreeMap;
25
26/**
27 * Tests for {@link ForwardingSortedMap} using {@link MapInterfaceTest}.
28 *
29 * @author George van den Driessche
30 */
31@GwtCompatible
32public class ForwardingSortedMapImplementsMapTest
33    extends SortedMapInterfaceTest<String, Integer> {
34
35  private static class SimpleForwardingSortedMap<K, V>
36      extends ForwardingSortedMap<K, V> {
37    final SortedMap<K, V> delegate;
38    SimpleForwardingSortedMap(SortedMap<K, V> delegate) {
39      this.delegate = delegate;
40    }
41    @Override protected SortedMap<K, V> delegate() {
42      return delegate;
43    }
44  }
45
46  public ForwardingSortedMapImplementsMapTest() {
47    super(true, true, true, true, true);
48  }
49
50  @Override protected SortedMap<String, Integer> makeEmptyMap() {
51    return new SimpleForwardingSortedMap<String, Integer>(
52        new TreeMap<String, Integer>(Ordering.natural().nullsFirst()));
53  }
54
55  @Override protected SortedMap<String, Integer> makePopulatedMap() {
56    final SortedMap<String, Integer> sortedMap = makeEmptyMap();
57    sortedMap.put("one", 1);
58    sortedMap.put("two", 2);
59    sortedMap.put("three", 3);
60    return sortedMap;
61  }
62
63  @Override protected String getKeyNotInPopulatedMap()
64      throws UnsupportedOperationException {
65    return "minus one";
66  }
67
68  @Override protected Integer getValueNotInPopulatedMap()
69      throws UnsupportedOperationException {
70    return -1;
71  }
72
73  @Override public void testContainsKey() {
74    try {
75      super.testContainsKey();
76    } catch (ClassCastException tolerated) {
77    }
78  }
79
80  @Override public void testEntrySetContainsEntryIncompatibleKey() {
81    try {
82      super.testEntrySetContainsEntryIncompatibleKey();
83    } catch (ClassCastException tolerated) {
84    }
85  }
86
87  @Override public void testEntrySetContainsEntryIncompatibleComparableKey() {
88    try {
89      super.testEntrySetContainsEntryIncompatibleComparableKey();
90    } catch (ClassCastException tolerated) {
91    }
92  }
93
94  @Override public void testEntrySetRemoveAllNullFromEmpty() {
95    try {
96      super.testEntrySetRemoveAllNullFromEmpty();
97    } catch (RuntimeException tolerated) {
98      // GWT's TreeMap.entrySet().removeAll(null) doesn't throws NPE.
99    }
100  }
101
102  @Override public void testEntrySetRetainAllNullFromEmpty() {
103    try {
104      super.testEntrySetRetainAllNullFromEmpty();
105    } catch (RuntimeException tolerated) {
106      // GWT's TreeMap.entrySet().retainAll(null) doesn't throws NPE.
107    }
108  }
109
110  @Override public void testKeySetRemoveAllNullFromEmpty() {
111    try {
112      super.testKeySetRemoveAllNullFromEmpty();
113    } catch (RuntimeException tolerated) {
114      // GWT's TreeMap.keySet().removeAll(null) doesn't throws NPE.
115    }
116  }
117
118  @Override public void testKeySetRetainAllNullFromEmpty() {
119    try {
120      super.testKeySetRetainAllNullFromEmpty();
121    } catch (RuntimeException tolerated) {
122      // GWT's TreeMap.keySet().retainAll(null) doesn't throws NPE.
123    }
124  }
125
126  @Override public void testValuesRemoveAllNullFromEmpty() {
127    try {
128      super.testValuesRemoveAllNullFromEmpty();
129    } catch (RuntimeException tolerated) {
130      // GWT's TreeMap.values().removeAll(null) doesn't throws NPE.
131    }
132  }
133
134  @Override public void testValuesRetainAllNullFromEmpty() {
135    try {
136      super.testValuesRemoveAllNullFromEmpty();
137    } catch (RuntimeException tolerated) {
138      // GWT's TreeMap.values().retainAll(null) doesn't throws NPE.
139    }
140  }
141}
142