@Nonnull Cache.Key getCacheKey(@Nonnull String key) { final int keyType = getCacheKeyType(); return new Cache.Key(keyType, key); }
@Nonnull protected final Cache.Key newKey() { return new Cache.Key(mCounter.getAndIncrement() % RequestType.values().length, "test"); }
@Test public void testShouldRemoveAllEntriesOfType() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key k1 = newKey(); cache.put(k1, newEntry()); final Cache.Key k11 = new Cache.Key(k1.type, k1.key + "test"); cache.put(k11, newEntry()); final Cache.Key k2 = new Cache.Key(k1.type + 1, "test"); cache.put(k2, newEntry()); final Cache.Key k3 = new Cache.Key(k1.type + 2, "test"); cache.put(k3, newEntry()); assertNotNull(cache.get(k1)); assertNotNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); cache.removeAll(k1.type); assertNull(cache.get(k1)); assertNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); }
@Test public void testShouldRemoveEntriesByType() throws Exception { final MapCache cache = new MapCache(); final Cache.Key key = new Cache.Key(1, "test"); final Cache.Key key1 = new Cache.Key(1, "test1"); final Cache.Key key2 = new Cache.Key(1, "test2"); final Cache.Key key3 = new Cache.Key(2, "test2"); cache.put(key, newEntry()); cache.put(key1, newEntry()); cache.put(key2, newEntry()); cache.put(key3, newEntry()); Assert.assertNotNull(cache.get(key)); Assert.assertNotNull(cache.get(key1)); Assert.assertNotNull(cache.get(key2)); Assert.assertNotNull(cache.get(key3)); cache.removeAll(1); Assert.assertNull(cache.get(key)); Assert.assertNull(cache.get(key1)); Assert.assertNull(cache.get(key2)); Assert.assertNotNull(cache.get(key3)); } }
@Test public void testHashCodeAndEquals() throws Exception { final Cache.Key k1 = new Cache.Key(1, "test"); final Cache.Key k2 = new Cache.Key(2, "test"); final Cache.Key k3 = new Cache.Key(1, "test1"); final Cache.Key k4 = new Cache.Key(3, "test1"); final Cache.Key k5 = new Cache.Key(1, "test"); assertNotEquals(k1.hashCode(), k2.hashCode()); assertNotEquals(k1.hashCode(), k3.hashCode()); assertNotEquals(k1.hashCode(), k4.hashCode()); assertEquals(k1.hashCode(), k5.hashCode()); assertNotEquals(k1, k2); assertNotEquals(k1, k3); assertNotEquals(k1, k4); assertEquals(k1, k5); assertNotEquals(k1.toString(), k2.toString()); assertNotEquals(k1.toString(), k3.toString()); assertNotEquals(k1.toString(), k4.toString()); assertEquals(k1.toString(), k5.toString()); } }