1 /* 2 * Copyright (C) 2011 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; 18 19 import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.base.Function; 24 import com.google.common.testing.NullPointerTester; 25 import java.util.Map; 26 import java.util.concurrent.ConcurrentHashMap; 27 import java.util.concurrent.CountDownLatch; 28 import junit.framework.TestCase; 29 30 /** @author Charles Fry */ 31 @GwtCompatible(emulated = true) 32 public class MapMakerTest extends TestCase { 33 34 @GwtIncompatible // NullPointerTester testNullParameters()35 public void testNullParameters() throws Exception { 36 NullPointerTester tester = new NullPointerTester(); 37 tester.testAllPublicInstanceMethods(new MapMaker()); 38 } 39 40 @GwtIncompatible // threads 41 static final class DelayingIdentityLoader<T> implements Function<T, T> { 42 private final CountDownLatch delayLatch; 43 DelayingIdentityLoader(CountDownLatch delayLatch)44 DelayingIdentityLoader(CountDownLatch delayLatch) { 45 this.delayLatch = delayLatch; 46 } 47 48 @Override apply(T key)49 public T apply(T key) { 50 awaitUninterruptibly(delayLatch); 51 return key; 52 } 53 } 54 55 /* 56 * TODO(cpovirk): eliminate duplication between these tests and those in LegacyMapMakerTests and 57 * anywhere else 58 */ 59 60 /** Tests for the builder. */ 61 public static class MakerTest extends TestCase { testInitialCapacity_negative()62 public void testInitialCapacity_negative() { 63 MapMaker maker = new MapMaker(); 64 try { 65 maker.initialCapacity(-1); 66 fail(); 67 } catch (IllegalArgumentException expected) { 68 } 69 } 70 71 // TODO(cpovirk): enable when ready xtestInitialCapacity_setTwice()72 public void xtestInitialCapacity_setTwice() { 73 MapMaker maker = new MapMaker().initialCapacity(16); 74 try { 75 // even to the same value is not allowed 76 maker.initialCapacity(16); 77 fail(); 78 } catch (IllegalArgumentException expected) { 79 } 80 } 81 testReturnsPlainConcurrentHashMapWhenPossible()82 public void testReturnsPlainConcurrentHashMapWhenPossible() { 83 Map<?, ?> map = new MapMaker().initialCapacity(5).makeMap(); 84 assertTrue(map instanceof ConcurrentHashMap); 85 } 86 } 87 } 88