View Javadoc
1   /*
2    * Copyright (C) 2009 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.cache;
18  
19  import static com.google.common.cache.TestingCacheLoaders.constantLoader;
20  import static com.google.common.cache.TestingCacheLoaders.identityLoader;
21  import static com.google.common.cache.TestingRemovalListeners.countingRemovalListener;
22  import static com.google.common.cache.TestingRemovalListeners.nullRemovalListener;
23  import static java.util.concurrent.TimeUnit.NANOSECONDS;
24  import static java.util.concurrent.TimeUnit.SECONDS;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.base.Ticker;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * Unit tests for CacheBuilder.
33   */
34  @GwtCompatible(emulated = true)
35  public class CacheBuilderTest extends TestCase {
36  
37    public void testNewBuilder() {
38      CacheLoader<Object, Integer> loader = constantLoader(1);
39  
40      LoadingCache<String, Integer> cache = CacheBuilder.newBuilder()
41          .removalListener(countingRemovalListener())
42          .build(loader);
43  
44      assertEquals(Integer.valueOf(1), cache.getUnchecked("one"));
45      assertEquals(1, cache.size());
46    }
47  
48    public void testInitialCapacity_negative() {
49      CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
50      try {
51        builder.initialCapacity(-1);
52        fail();
53      } catch (IllegalArgumentException expected) {}
54    }
55  
56    public void testInitialCapacity_setTwice() {
57      CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>().initialCapacity(16);
58      try {
59        // even to the same value is not allowed
60        builder.initialCapacity(16);
61        fail();
62      } catch (IllegalStateException expected) {}
63    }
64  
65    public void testInitialCapacity_large() {
66      CacheBuilder.newBuilder().initialCapacity(Integer.MAX_VALUE);
67      // that the builder didn't blow up is enough;
68      // don't actually create this monster!
69    }
70  
71    public void testConcurrencyLevel_zero() {
72      CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
73      try {
74        builder.concurrencyLevel(0);
75        fail();
76      } catch (IllegalArgumentException expected) {}
77    }
78  
79    public void testConcurrencyLevel_setTwice() {
80      CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>().concurrencyLevel(16);
81      try {
82        // even to the same value is not allowed
83        builder.concurrencyLevel(16);
84        fail();
85      } catch (IllegalStateException expected) {}
86    }
87  
88    public void testConcurrencyLevel_large() {
89      CacheBuilder.newBuilder().concurrencyLevel(Integer.MAX_VALUE);
90      // don't actually build this beast
91    }
92  
93    public void testMaximumSize_negative() {
94      CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
95      try {
96        builder.maximumSize(-1);
97        fail();
98      } catch (IllegalArgumentException expected) {}
99    }
100 
101   public void testMaximumSize_setTwice() {
102     CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>().maximumSize(16);
103     try {
104       // even to the same value is not allowed
105       builder.maximumSize(16);
106       fail();
107     } catch (IllegalStateException expected) {}
108   }
109 
110   public void testTimeToLive_negative() {
111     CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
112     try {
113       builder.expireAfterWrite(-1, SECONDS);
114       fail();
115     } catch (IllegalArgumentException expected) {}
116   }
117 
118   public void testTimeToLive_small() {
119     CacheBuilder.newBuilder()
120         .expireAfterWrite(1, NANOSECONDS)
121         .build(identityLoader());
122     // well, it didn't blow up.
123   }
124 
125   public void testTimeToLive_setTwice() {
126     CacheBuilder<Object, Object> builder =
127         new CacheBuilder<Object, Object>().expireAfterWrite(3600, SECONDS);
128     try {
129       // even to the same value is not allowed
130       builder.expireAfterWrite(3600, SECONDS);
131       fail();
132     } catch (IllegalStateException expected) {}
133   }
134 
135   public void testTimeToIdle_negative() {
136     CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
137     try {
138       builder.expireAfterAccess(-1, SECONDS);
139       fail();
140     } catch (IllegalArgumentException expected) {}
141   }
142 
143   public void testTimeToIdle_small() {
144     CacheBuilder.newBuilder()
145         .expireAfterAccess(1, NANOSECONDS)
146         .build(identityLoader());
147     // well, it didn't blow up.
148   }
149 
150   public void testTimeToIdle_setTwice() {
151     CacheBuilder<Object, Object> builder =
152         new CacheBuilder<Object, Object>().expireAfterAccess(3600, SECONDS);
153     try {
154       // even to the same value is not allowed
155       builder.expireAfterAccess(3600, SECONDS);
156       fail();
157     } catch (IllegalStateException expected) {}
158   }
159 
160   public void testTimeToIdleAndToLive() {
161     CacheBuilder.newBuilder()
162         .expireAfterWrite(1, NANOSECONDS)
163         .expireAfterAccess(1, NANOSECONDS)
164         .build(identityLoader());
165     // well, it didn't blow up.
166   }
167 
168   public void testTicker_setTwice() {
169     Ticker testTicker = Ticker.systemTicker();
170     CacheBuilder<Object, Object> builder =
171         new CacheBuilder<Object, Object>().ticker(testTicker);
172     try {
173       // even to the same instance is not allowed
174       builder.ticker(testTicker);
175       fail();
176     } catch (IllegalStateException expected) {}
177   }
178 
179   public void testRemovalListener_setTwice() {
180     RemovalListener<Object, Object> testListener = nullRemovalListener();
181     CacheBuilder<Object, Object> builder =
182         new CacheBuilder<Object, Object>().removalListener(testListener);
183     try {
184       // even to the same instance is not allowed
185       builder = builder.removalListener(testListener);
186       fail();
187     } catch (IllegalStateException expected) {}
188   }
189 
190   // "Basher tests", where we throw a bunch of stuff at a LoadingCache and check basic invariants.
191 }
192