Home | History | Annotate | Download | only in cache
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.common.cache;
     16 
     17 import static java.util.Arrays.asList;
     18 
     19 import com.google.common.collect.ImmutableList;
     20 import com.google.common.collect.ImmutableMap;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * @author Charles Fry
     26  */
     27 public class CacheManualTest extends TestCase {
     28 
     29   public void testGetIfPresent() {
     30     Cache<Object, Object> cache = CacheBuilder.newBuilder().build();
     31     CacheStats stats = cache.stats();
     32     assertEquals(0, stats.missCount());
     33     assertEquals(0, stats.loadSuccessCount());
     34     assertEquals(0, stats.loadExceptionCount());
     35     assertEquals(0, stats.hitCount());
     36 
     37     Object one = new Object();
     38     Object two = new Object();
     39 
     40     assertNull(cache.getIfPresent(one));
     41     stats = cache.stats();
     42     assertEquals(1, stats.missCount());
     43     assertEquals(0, stats.loadSuccessCount());
     44     assertEquals(0, stats.loadExceptionCount());
     45     assertEquals(0, stats.hitCount());
     46     assertNull(cache.asMap().get(one));
     47     assertFalse(cache.asMap().containsKey(one));
     48     assertFalse(cache.asMap().containsValue(two));
     49 
     50     assertNull(cache.getIfPresent(two));
     51     stats = cache.stats();
     52     assertEquals(2, stats.missCount());
     53     assertEquals(0, stats.loadSuccessCount());
     54     assertEquals(0, stats.loadExceptionCount());
     55     assertEquals(0, stats.hitCount());
     56     assertNull(cache.asMap().get(two));
     57     assertFalse(cache.asMap().containsKey(two));
     58     assertFalse(cache.asMap().containsValue(one));
     59 
     60     cache.put(one, two);
     61 
     62     assertSame(two, cache.getIfPresent(one));
     63     stats = cache.stats();
     64     assertEquals(2, stats.missCount());
     65     assertEquals(0, stats.loadSuccessCount());
     66     assertEquals(0, stats.loadExceptionCount());
     67     assertEquals(1, stats.hitCount());
     68     assertSame(two, cache.asMap().get(one));
     69     assertTrue(cache.asMap().containsKey(one));
     70     assertTrue(cache.asMap().containsValue(two));
     71 
     72     assertNull(cache.getIfPresent(two));
     73     stats = cache.stats();
     74     assertEquals(3, stats.missCount());
     75     assertEquals(0, stats.loadSuccessCount());
     76     assertEquals(0, stats.loadExceptionCount());
     77     assertEquals(1, stats.hitCount());
     78     assertNull(cache.asMap().get(two));
     79     assertFalse(cache.asMap().containsKey(two));
     80     assertFalse(cache.asMap().containsValue(one));
     81 
     82     cache.put(two, one);
     83 
     84     assertSame(two, cache.getIfPresent(one));
     85     stats = cache.stats();
     86     assertEquals(3, stats.missCount());
     87     assertEquals(0, stats.loadSuccessCount());
     88     assertEquals(0, stats.loadExceptionCount());
     89     assertEquals(2, stats.hitCount());
     90     assertSame(two, cache.asMap().get(one));
     91     assertTrue(cache.asMap().containsKey(one));
     92     assertTrue(cache.asMap().containsValue(two));
     93 
     94     assertSame(one, cache.getIfPresent(two));
     95     stats = cache.stats();
     96     assertEquals(3, stats.missCount());
     97     assertEquals(0, stats.loadSuccessCount());
     98     assertEquals(0, stats.loadExceptionCount());
     99     assertEquals(3, stats.hitCount());
    100     assertSame(one, cache.asMap().get(two));
    101     assertTrue(cache.asMap().containsKey(two));
    102     assertTrue(cache.asMap().containsValue(one));
    103   }
    104 
    105   public void testGetAllPresent() {
    106     Cache<Integer, Integer> cache = CacheBuilder.newBuilder().build();
    107     CacheStats stats = cache.stats();
    108     assertEquals(0, stats.missCount());
    109     assertEquals(0, stats.loadSuccessCount());
    110     assertEquals(0, stats.loadExceptionCount());
    111     assertEquals(0, stats.hitCount());
    112 
    113     assertEquals(ImmutableMap.of(), cache.getAllPresent(ImmutableList.<Integer>of()));
    114     stats = cache.stats();
    115     assertEquals(0, stats.missCount());
    116     assertEquals(0, stats.loadSuccessCount());
    117     assertEquals(0, stats.loadExceptionCount());
    118     assertEquals(0, stats.hitCount());
    119 
    120     assertEquals(ImmutableMap.of(), cache.getAllPresent(asList(1, 2, 3)));
    121     stats = cache.stats();
    122     assertEquals(3, stats.missCount());
    123     assertEquals(0, stats.loadSuccessCount());
    124     assertEquals(0, stats.loadExceptionCount());
    125     assertEquals(0, stats.hitCount());
    126 
    127     cache.put(2, 22);
    128 
    129     assertEquals(ImmutableMap.of(2, 22), cache.getAllPresent(asList(1, 2, 3)));
    130     stats = cache.stats();
    131     assertEquals(5, stats.missCount());
    132     assertEquals(0, stats.loadSuccessCount());
    133     assertEquals(0, stats.loadExceptionCount());
    134     assertEquals(1, stats.hitCount());
    135 
    136     cache.put(3, 33);
    137 
    138     assertEquals(ImmutableMap.of(2, 22, 3, 33), cache.getAllPresent(asList(1, 2, 3)));
    139     stats = cache.stats();
    140     assertEquals(6, stats.missCount());
    141     assertEquals(0, stats.loadSuccessCount());
    142     assertEquals(0, stats.loadExceptionCount());
    143     assertEquals(3, stats.hitCount());
    144 
    145     cache.put(1, 11);
    146 
    147     assertEquals(ImmutableMap.of(1, 11, 2, 22, 3, 33), cache.getAllPresent(asList(1, 2, 3)));
    148     stats = cache.stats();
    149     assertEquals(6, stats.missCount());
    150     assertEquals(0, stats.loadSuccessCount());
    151     assertEquals(0, stats.loadExceptionCount());
    152     assertEquals(6, stats.hitCount());
    153   }
    154 
    155 }
    156