BUG-994: improve object cache
[yangtools.git] / common / object-cache-api / src / test / java / org / opendaylight / yangtools / objcache / spi / CacheTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.objcache.spi;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
13
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.objcache.ObjectCache;
18 import org.opendaylight.yangtools.objcache.spi.AbstractObjectCache.SoftKey;
19
20 import com.google.common.base.FinalizableReferenceQueue;
21 import com.google.common.cache.CacheBuilder;
22
23 public class CacheTest {
24     private FinalizableReferenceQueue queue;
25     private ObjectCache oc;
26
27     @Before
28     public void setUp() {
29         queue = new FinalizableReferenceQueue();
30         oc = new AbstractObjectCache(CacheBuilder.newBuilder().softValues().<SoftKey<?>, Object>build(), queue) {
31         };
32     }
33
34     @After
35     public void tearDown() {
36         queue.close();
37     }
38
39     @Test
40     public void testMissingKey() {
41         final String key1 = "abcd";
42         final String key2 = "efgh";
43
44         assertSame(key1, oc.getReference(key1));
45         assertSame(key2, oc.getReference(key2));
46     }
47
48     @Test
49     public void testPresentKey() {
50         final String key1 = new String("abcd");
51         final String key2 = new String("abcd");
52
53         assertSame(key1, oc.getReference(key1));
54
55         final String key3 = oc.getReference(key2);
56         assertEquals(key2, key3);
57         assertNotSame(key2, key3);
58         assertSame(key1, key3);
59     }
60 }