Skip to content

Commit

Permalink
Added a test case for the cache view
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Jul 8, 2023
1 parent 373361c commit f3d72df
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <KB> The backend key type
* @param <V> The value type
*/
public class CacheView<KF,KB, V>
public class CacheView<KF, KB, V>
implements Cache<KF, V>
{
/** The delegate cache*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.aksw.commons.util.cache;

import java.util.Map;
import java.util.Map.Entry;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.base.Converter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class TestCacheView {
@Test
public void testCacheView() {
Cache<Entry<String, Integer>, String> sharedCache = CacheBuilder.newBuilder().build();

Converter<Integer, Entry<String, Integer>> convA = Converter.from(i -> Map.entry("a", i), Entry::getValue);
Converter<Integer, Entry<String, Integer>> convB = Converter.from(i -> Map.entry("b", i), Entry::getValue);

Cache<Integer, String> cacheA = new CacheView<>(sharedCache, convA, e -> "a".equals(e.getKey()));
Cache<Integer, String> cacheB = new CacheView<>(sharedCache, convB, e -> "b".equals(e.getKey()));

for (int i = 0; i < 3; ++i) {
int v = i;
CacheUtils.get(cacheA, v, () -> "a" + v);
}

for (int j = 0; j < 3; ++j) {
int v = j;
CacheUtils.get(cacheB, v, () -> "b" + v);
}

Assert.assertEquals("a1", cacheA.getIfPresent(1));
Assert.assertEquals("b2", cacheB.getIfPresent(2));

// System.out.println(sharedCache.asMap());
// System.out.println(CacheUtils.getIfPresent(cacheA, 2));
// System.out.println(CacheUtils.getIfPresent(cacheB, 2));
}
}

0 comments on commit f3d72df

Please sign in to comment.