Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect.testing.testers;
 
 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
 import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
 
 
 import java.util.Map;
A generic JUnit test which tests put operations on a map. Can't be invoked directly; please see com.google.common.collect.testing.MapTestSuiteBuilder.

Author(s):
Chris Povirk
Kevin Bourrillion
 
 @SuppressWarnings("unchecked"// too many "unchecked generic array creations"
 @GwtCompatible(emulated = true)
 public class MapPutTester<K, V> extends AbstractMapTester<K, V> {
   private Entry<K, V> nullKeyEntry;
   private Entry<K, V> nullValueEntry;
   private Entry<K, V> nullKeyValueEntry;
   private Entry<K, V> presentKeyNullValueEntry;
 
   @Override public void setUp() throws Exception {
     super.setUp();
      = entry(null..getValue());
      = entry(..getKey(), null);
      = entry(nullnull);
   }
 
   public void testPut_supportedNotPresent() {
     assertNull("put(notPresent, value) should return null"put(.));
   }
 
   @CollectionSize.Require(absent = )
     try {
       Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
       put(.);
       iterator.next();
       fail("Expected ConcurrentModificationException");
     } catch (ConcurrentModificationException expected) {
       // success
     }
   }
 
   @CollectionSize.Require(absent = )
     try {
       Iterator<K> iterator = getMap().keySet().iterator();
       put(.);
       iterator.next();
       fail("Expected ConcurrentModificationException");
     } catch (ConcurrentModificationException expected) {
       // success
     }
   }
 
   @CollectionSize.Require(absent = )
     try {
       Iterator<V> iterator = getMap().values().iterator();
       put(.);
      iterator.next();
      fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException expected) {
      // success
    }
  }
  public void testPut_unsupportedNotPresent() {
    try {
      put(.);
      fail("put(notPresent, value) should throw");
    } catch (UnsupportedOperationException expected) {
    }
  }
  @CollectionSize.Require(absent = )
    try {
      assertEquals("put(present, existingValue) should return present or throw",
          ..getValue(), put(.));
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  @CollectionSize.Require(absent = )
    try {
      fail("put(present, differentValue) should throw");
    } catch (UnsupportedOperationException expected) {
    }
  }
    assertNull("put(null, value) should return null"put());
  }
  @CollectionSize.Require(absent = )
  public void testPut_nullKeySupportedPresent() {
    Entry<K, V> newEntry = entry(null..getValue());
    assertEquals("put(present, value) should return the associated value",
        getValueForNullKey(), put(newEntry));
    Entry<K, V>[] expected = createArrayWithNullKey();
    expected[getNullLocation()] = newEntry;
    expectContents(expected);
  }
  @MapFeature.Require(value = , absent = )
  public void testPut_nullKeyUnsupported() {
    try {
      put();
      fail("put(null, value) should throw");
    } catch (NullPointerException expected) {
    }
        "Should not contain null key after unsupported put(null, value)");
  }
  public void testPut_nullValueSupported() {
    assertNull("put(key, null) should return null"put());
  }
  public void testPut_nullValueUnsupported() {
    try {
      put();
      fail("put(key, null) should throw");
    } catch (NullPointerException expected) {
    }
        "Should not contain null value after unsupported put(key, null)");
  }
  @CollectionSize.Require(absent = )
    assertEquals("put(present, null) should return the associated value",
  }
  @CollectionSize.Require(absent = )
    try {
      fail("put(present, null) should throw");
    } catch (NullPointerException expected) {
    }
        "Should not contain null after unsupported put(present, null)");
  }
  @CollectionSize.Require(absent = )
    assertNull("put(present, null) should return the associated value (null)",
        getMap().put(getKeyForNullValue(), null));
  }
  @CollectionSize.Require(absent = )
    Entry<K, V> newEntry = entry(getKeyForNullValue(), ..getValue());
    assertNull("put(present, value) should return the associated value (null)",
        put(newEntry));
    Entry<K, V>[] expected = createArrayWithNullValue();
    expected[getNullLocation()] = newEntry;
    expectContents(expected);
  }
    assertNull("put(null, null) should return null"put());
  }
  private V put(Map.Entry<K, V> entry) {
    return getMap().put(entry.getKey(), entry.getValue());
  }

  
Returns the java.lang.reflect.Method instance for testPut_nullKeyUnsupported() so that tests of java.util.TreeMap can suppress it with FeatureSpecificTestSuiteBuilder.suppressing() until Sun bug 5045147 is fixed.
  @GwtIncompatible("reflection")
  public static Method getPutNullKeyUnsupportedMethod() {
    return Helpers.getMethod(MapPutTester.class"testPut_nullKeyUnsupported");
  }
New to GrepCode? Check out our FAQ X