BUG-994: improve object cache
[yangtools.git] / common / object-cache-api / src / test / java / org / opendaylight / yangtools / objcache / spi / SoftKeyTest.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.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15
16 import org.junit.After;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.objcache.spi.AbstractObjectCache.SoftKey;
20
21 import com.google.common.base.FinalizableReferenceQueue;
22
23 public class SoftKeyTest {
24     private FinalizableReferenceQueue queue;
25
26
27     @Before
28     public void setUp() {
29         queue = new FinalizableReferenceQueue();
30     }
31
32     @After
33     public void tearDown() {
34         queue.close();
35     }
36
37     @Test
38     public void testEquals() {
39         final String str = "foo";
40
41         final SoftKey<?> key = new SoftKey<String>(str, queue) {
42             @Override
43             public void finalizeReferent() {
44
45             }
46         };
47
48         assertSame(str, key.get());
49         assertEquals(str.hashCode(), key.hashCode());
50         assertTrue(key.equals(str));
51         key.clear();
52         assertNull(key.get());
53         assertEquals(str.hashCode(), key.hashCode());
54         assertFalse(key.equals(str));
55     }
56 }