View Javadoc
1   /*
2    * Copyright (C) 2015 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.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.AbstractMapTester;
26  import com.google.common.collect.testing.features.CollectionSize;
27  import com.google.common.collect.testing.features.MapFeature;
28  import java.util.Map;
29  
30  /**
31   * A generic JUnit test which tests {@link Map#putIfAbsent}. Can't be
32   * invoked directly; please see
33   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
34   * 
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class MapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
39  
40    @MapFeature.Require(SUPPORTS_PUT)
41    public void testPutIfAbsent_supportedAbsent() {
42      assertNull("putIfAbsent(notPresent, value) should return null", 
43          getMap().putIfAbsent(k3(), v3()));
44      expectAdded(e3());
45    }
46    
47    @MapFeature.Require(SUPPORTS_PUT)
48    @CollectionSize.Require(absent = ZERO)
49    public void testPutIfAbsent_supportedPresent() {
50      assertEquals("putIfAbsent(present, value) should return existing value", 
51          v0(), getMap().putIfAbsent(k0(), v3()));
52      expectUnchanged();
53    }
54  
55    @MapFeature.Require(absent = SUPPORTS_PUT)
56    public void testPutIfAbsent_unsupportedAbsent() {
57      try {
58        getMap().putIfAbsent(k3(), v3());
59        fail("putIfAbsent(notPresent, value) should throw");
60      } catch (UnsupportedOperationException expected) {
61      }
62      expectUnchanged();
63      expectMissing(e3());
64    }
65  
66    @MapFeature.Require(absent = SUPPORTS_PUT)
67    @CollectionSize.Require(absent = ZERO)
68    public void testPutIfAbsent_unsupportedPresentExistingValue() {
69      try {
70        assertEquals("putIfAbsent(present, existingValue) should return present or throw",
71            v0(), getMap().putIfAbsent(k0(), v0()));
72      } catch (UnsupportedOperationException tolerated) {
73      }
74      expectUnchanged();
75    }
76  
77    @MapFeature.Require(absent = SUPPORTS_PUT)
78    @CollectionSize.Require(absent = ZERO)
79    public void testPutIfAbsent_unsupportedPresentDifferentValue() {
80      try {
81        getMap().putIfAbsent(k0(), v3());
82      } catch (UnsupportedOperationException tolerated) {
83      }
84      expectUnchanged();
85    }
86  
87    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
88    public void testPutIfAbsent_nullKeyUnsupported() {
89      try {
90        getMap().putIfAbsent(null, v3());
91        fail("putIfAbsent(null, value) should throw");
92      } catch (NullPointerException expected) {
93      }
94      expectUnchanged();
95      expectNullKeyMissingWhenNullKeysUnsupported(
96          "Should not contain null key after unsupported putIfAbsent(null, value)");
97    }
98  
99    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
100   public void testPutIfAbsent_nullValueUnsupported() {
101     try {
102       getMap().putIfAbsent(k3(), null);
103       fail("putIfAbsent(key, null) should throw");
104     } catch (NullPointerException expected) {
105     }
106     expectUnchanged();
107     expectNullValueMissingWhenNullValuesUnsupported(
108         "Should not contain null value after unsupported put(key, null)");
109   }
110 
111   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
112   @CollectionSize.Require(absent = ZERO)
113   public void testPutIfAbsent_putWithNullValueUnsupported() {
114     try {
115       getMap().putIfAbsent(k0(), null);
116     } catch (NullPointerException tolerated) {
117     }
118     expectUnchanged();
119     expectNullValueMissingWhenNullValuesUnsupported(
120         "Should not contain null after unsupported putIfAbsent(present, null)");
121   }
122 }