001/* 002 * Copyright (C) 2015 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionSize.ZERO; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.testing.AbstractMapTester; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.MapFeature; 028import com.google.errorprone.annotations.CanIgnoreReturnValue; 029import java.util.Map.Entry; 030import java.util.concurrent.ConcurrentMap; 031import org.junit.Ignore; 032 033/** 034 * A generic JUnit test which tests {@code putIfAbsent} operations on a concurrent map. Can't be 035 * invoked directly; please see {@link 036 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. 037 * 038 * @author Louis Wasserman 039 */ 040@GwtCompatible 041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 042@SuppressWarnings("JUnit4ClassUsedInJUnit3") 043@ElementTypesAreNonnullByDefault 044public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> { 045 @Override 046 protected ConcurrentMap<K, V> getMap() { 047 return (ConcurrentMap<K, V>) super.getMap(); 048 } 049 050 @MapFeature.Require(SUPPORTS_PUT) 051 public void testPutIfAbsent_supportedAbsent() { 052 assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3())); 053 expectAdded(e3()); 054 } 055 056 @MapFeature.Require(SUPPORTS_PUT) 057 @CollectionSize.Require(absent = ZERO) 058 public void testPutIfAbsent_supportedPresent() { 059 assertEquals( 060 "putIfAbsent(present, value) should return existing value", 061 v0(), 062 getMap().putIfAbsent(k0(), v3())); 063 expectUnchanged(); 064 } 065 066 @MapFeature.Require(absent = SUPPORTS_PUT) 067 public void testPutIfAbsent_unsupportedAbsent() { 068 try { 069 putIfAbsent(e3()); 070 fail("putIfAbsent(notPresent, value) should throw"); 071 } catch (UnsupportedOperationException expected) { 072 } 073 expectUnchanged(); 074 expectMissing(e3()); 075 } 076 077 @MapFeature.Require(absent = SUPPORTS_PUT) 078 @CollectionSize.Require(absent = ZERO) 079 public void testPutIfAbsent_unsupportedPresentExistingValue() { 080 try { 081 assertEquals( 082 "putIfAbsent(present, existingValue) should return present or throw", 083 v0(), 084 putIfAbsent(e0())); 085 } catch (UnsupportedOperationException tolerated) { 086 } 087 expectUnchanged(); 088 } 089 090 @MapFeature.Require(absent = SUPPORTS_PUT) 091 @CollectionSize.Require(absent = ZERO) 092 public void testPutIfAbsent_unsupportedPresentDifferentValue() { 093 try { 094 getMap().putIfAbsent(k0(), v3()); 095 } catch (UnsupportedOperationException tolerated) { 096 } 097 expectUnchanged(); 098 } 099 100 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) 101 public void testPutIfAbsent_nullKeyUnsupported() { 102 try { 103 getMap().putIfAbsent(null, v3()); 104 fail("putIfAbsent(null, value) should throw"); 105 } catch (NullPointerException expected) { 106 } 107 expectUnchanged(); 108 expectNullKeyMissingWhenNullKeysUnsupported( 109 "Should not contain null key after unsupported putIfAbsent(null, value)"); 110 } 111 112 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 113 public void testPutIfAbsent_nullValueUnsupported() { 114 try { 115 getMap().putIfAbsent(k3(), null); 116 fail("putIfAbsent(key, null) should throw"); 117 } catch (NullPointerException expected) { 118 } 119 expectUnchanged(); 120 expectNullValueMissingWhenNullValuesUnsupported( 121 "Should not contain null value after unsupported put(key, null)"); 122 } 123 124 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 125 @CollectionSize.Require(absent = ZERO) 126 public void testPutIfAbsent_putWithNullValueUnsupported() { 127 try { 128 getMap().putIfAbsent(k0(), null); 129 } catch (NullPointerException tolerated) { 130 } 131 expectUnchanged(); 132 expectNullValueMissingWhenNullValuesUnsupported( 133 "Should not contain null after unsupported putIfAbsent(present, null)"); 134 } 135 136 @CanIgnoreReturnValue 137 private V putIfAbsent(Entry<K, V> entry) { 138 return getMap().putIfAbsent(entry.getKey(), entry.getValue()); 139 } 140}