• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   @GwtIncompatible // NullPointerTester
testNullParameters()34   public void testNullParameters() throws Exception {
35     NullPointerTester tester = new NullPointerTester();
36     tester.testAllPublicInstanceMethods(new MapMaker());
37   }
38 
39   @GwtIncompatible // threads
40   static final class DelayingIdentityLoader<T> implements Function<T, T> {
41     private final CountDownLatch delayLatch;
42 
DelayingIdentityLoader(CountDownLatch delayLatch)43     DelayingIdentityLoader(CountDownLatch delayLatch) {
44       this.delayLatch = delayLatch;
45     }
46 
47     @Override
apply(T key)48     public T apply(T key) {
49       awaitUninterruptibly(delayLatch);
50       return key;
51     }
52   }
53 
54   /*
55    * TODO(cpovirk): eliminate duplication between these tests and those in LegacyMapMakerTests and
56    * anywhere else
57    */
58 
testInitialCapacity_negative()59   public void testInitialCapacity_negative() {
60     MapMaker maker = new MapMaker();
61     try {
62       maker.initialCapacity(-1);
63       fail();
64     } catch (IllegalArgumentException expected) {
65     }
66   }
67 
68   // TODO(cpovirk): enable when ready (apparently after a change to our GWT emulation)
xtestInitialCapacity_setTwice()69   public void xtestInitialCapacity_setTwice() {
70     MapMaker maker = new MapMaker().initialCapacity(16);
71     try {
72       // even to the same value is not allowed
73       maker.initialCapacity(16);
74       fail();
75     } catch (IllegalStateException expected) {
76     }
77   }
78 
testReturnsPlainConcurrentHashMapWhenPossible()79   public void testReturnsPlainConcurrentHashMapWhenPossible() {
80     Map<?, ?> map = new MapMaker().initialCapacity(5).makeMap();
81     assertTrue(map instanceof ConcurrentHashMap);
82   }
83 }
84