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 java.util.Map.Entry; 029import java.util.concurrent.ConcurrentMap; 030import org.junit.Ignore; 031 032/** 033 * A generic JUnit test which tests {@code putIfAbsent} operations on a concurrent map. Can't be 034 * invoked directly; please see {@link 035 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. 036 * 037 * @author Louis Wasserman 038 */ 039@GwtCompatible 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 041public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> { 042 @Override 043 protected ConcurrentMap<K, V> getMap() { 044 return (ConcurrentMap<K, V>) super.getMap(); 045 } 046 047 @MapFeature.Require(SUPPORTS_PUT) 048 public void testPutIfAbsent_supportedAbsent() { 049 assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3())); 050 expectAdded(e3()); 051 } 052 053 @MapFeature.Require(SUPPORTS_PUT) 054 @CollectionSize.Require(absent = ZERO) 055 public void testPutIfAbsent_supportedPresent() { 056 assertEquals( 057 "putIfAbsent(present, value) should return existing value", 058 v0(), 059 getMap().putIfAbsent(k0(), v3())); 060 expectUnchanged(); 061 } 062 063 @MapFeature.Require(absent = SUPPORTS_PUT) 064 public void testPutIfAbsent_unsupportedAbsent() { 065 try { 066 putIfAbsent(e3()); 067 fail("putIfAbsent(notPresent, value) should throw"); 068 } catch (UnsupportedOperationException expected) { 069 } 070 expectUnchanged(); 071 expectMissing(e3()); 072 } 073 074 @MapFeature.Require(absent = SUPPORTS_PUT) 075 @CollectionSize.Require(absent = ZERO) 076 public void testPutIfAbsent_unsupportedPresentExistingValue() { 077 try { 078 assertEquals( 079 "putIfAbsent(present, existingValue) should return present or throw", 080 v0(), 081 putIfAbsent(e0())); 082 } catch (UnsupportedOperationException tolerated) { 083 } 084 expectUnchanged(); 085 } 086 087 @MapFeature.Require(absent = SUPPORTS_PUT) 088 @CollectionSize.Require(absent = ZERO) 089 public void testPutIfAbsent_unsupportedPresentDifferentValue() { 090 try { 091 getMap().putIfAbsent(k0(), v3()); 092 } catch (UnsupportedOperationException tolerated) { 093 } 094 expectUnchanged(); 095 } 096 097 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) 098 public void testPutIfAbsent_nullKeyUnsupported() { 099 try { 100 getMap().putIfAbsent(null, v3()); 101 fail("putIfAbsent(null, value) should throw"); 102 } catch (NullPointerException expected) { 103 } 104 expectUnchanged(); 105 expectNullKeyMissingWhenNullKeysUnsupported( 106 "Should not contain null key after unsupported putIfAbsent(null, value)"); 107 } 108 109 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 110 public void testPutIfAbsent_nullValueUnsupported() { 111 try { 112 getMap().putIfAbsent(k3(), null); 113 fail("putIfAbsent(key, null) should throw"); 114 } catch (NullPointerException expected) { 115 } 116 expectUnchanged(); 117 expectNullValueMissingWhenNullValuesUnsupported( 118 "Should not contain null value after unsupported put(key, null)"); 119 } 120 121 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 122 @CollectionSize.Require(absent = ZERO) 123 public void testPutIfAbsent_putWithNullValueUnsupported() { 124 try { 125 getMap().putIfAbsent(k0(), null); 126 } catch (NullPointerException tolerated) { 127 } 128 expectUnchanged(); 129 expectNullValueMissingWhenNullValuesUnsupported( 130 "Should not contain null after unsupported putIfAbsent(present, null)"); 131 } 132 133 private V putIfAbsent(Entry<K, V> entry) { 134 return getMap().putIfAbsent(entry.getKey(), entry.getValue()); 135 } 136}