Skip to content

Commit

Permalink
unit test supplement.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Jan 30, 2018
1 parent 786e4bb commit f69a252
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public LoadingCache(Cache<K, V> cache) {
while (cache instanceof ProxyCache) {
cache = ((ProxyCache) cache).getTargetCache();
}
final Cache c = cache;
if (cache instanceof AbstractCache) {
eventConsumer = ((AbstractCache) c)::notify;
eventConsumer = ((AbstractCache) cache)::notify;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -25,6 +27,7 @@ public void test() throws Exception {
cache = new LoadingCache<>(cache);
baseTest();
loadingCacheTest(cache, 0);
errorTest();
}

public static void loadingCacheTest(Cache cache, long waitMillis) throws Exception {
Expand Down Expand Up @@ -114,4 +117,22 @@ private static void loadingCacheTestImpl(Cache cache, long waitMillis) throws Ex

cache.config().getMonitors().remove(monitor);
}

private void errorTest() {
cache.config().setLoader((key) -> {
throw new SQLException();
});
try {
cache.get("K1");
Assert.fail();
} catch (CacheInvokeException e) {
}
try {
Set s = new HashSet();
s.add("K1");
cache.getAll(s);
Assert.fail();
} catch (CacheInvokeException e) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static void refreshCacheTest(Cache cache, long refresh, long stopRefreshA
RefreshPolicy oldPolicy = cache.config().getRefreshPolicy();

cache.config().setLoader((key) -> key + "_V" + count.getAndIncrement());
RefreshPolicy policy = RefreshPolicy.newPolicy(refresh, TimeUnit.MILLISECONDS);
RefreshPolicy policy = RefreshPolicy.newPolicy(refresh, TimeUnit.MILLISECONDS)
.refreshLockTimeout(10, TimeUnit.SECONDS);
cache.config().setRefreshPolicy(policy);
refreshCacheTest1(cache);
getRefreshCache(cache).stopRefresh();
Expand All @@ -52,13 +53,14 @@ public static void refreshCacheTest(AbstractCacheBuilder builder, long refresh,
AtomicInteger count = new AtomicInteger(0);
builder.loader((key) -> key + "_V" + count.getAndIncrement());
RefreshPolicy policy = RefreshPolicy.newPolicy(refresh, TimeUnit.MILLISECONDS);
policy.setRefreshLockTimeoutMillis(10000);
builder.refreshPolicy(policy);
Cache cache = builder.buildCache();
refreshCacheTest1(cache);
cache.close();

count.set(0);
builder.getConfig().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(stopRefreshAfterLastAccess);
builder.getConfig().getRefreshPolicy().stopRefreshAfterLastAccess(stopRefreshAfterLastAccess, TimeUnit.MILLISECONDS);
cache = builder.buildCache();
refreshCacheTest2(cache);
cache.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
package com.alicp.jetcache.anno.method;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import java.io.Serializable;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -36,6 +36,9 @@ public String foo(I1 p) {
public String foo2(I1 p) {
return null;
}

public void foo3(byte p2, short p3, char p4, int p5, long p6, float p7, double p8, boolean p9) {
}
}

@Test
Expand All @@ -47,42 +50,48 @@ class CI2 extends CI1 implements Cloneable, I1 {
}
Object obj = new CI2();
Class<?>[] is = ClassUtil.getAllInterfaces(obj);
Assert.assertEquals(3, is.length);
assertEquals(3, is.length);
}

@Test
public void testGenerateCacheName() throws Exception {
Method m1 = C1.class.getMethod("foo");
Method m2 = C1.class.getMethod("foo", I1.class);
Method m3 = C1.class.getMethod("foo2", I1.class);
Method m4 = C1.class.getMethod("foo3", byte.class, short.class, char.class, int.class, long.class, float.class, double.class, boolean.class);

String s1 = "m.ClassUtilTest$C1." + m1.getName() + "()";
String s2 = ClassUtil.generateCacheName(m1, hidePack);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);

s1 = "m.ClassUtilTest$C1." + m2.getName() + "(Lm.ClassUtilTest$I1;)";
s2 = ClassUtil.generateCacheName(m2, hidePack);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);

s1 = "c.a.j.a.m.ClassUtilTest$C1." + m3.getName() + "(Lc.a.j.a.m.ClassUtilTest$I1;)";
s2 = ClassUtil.generateCacheName(m3, null);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);

s1 = "m.ClassUtilTest$C1." + m4.getName() + "(BSCIJFDZ)";
s2 = ClassUtil.generateCacheName(m4, hidePack);
assertEquals(s1, s2);
}

@Test
public void removeHiddenPackageTest() {
String[] hs = {"com.foo", "com.bar."};
Assert.assertEquals("Foo", ClassUtil.removeHiddenPackage(hs, "com.foo.Foo"));
Assert.assertEquals("foo.Bar", ClassUtil.removeHiddenPackage(hs, "com.bar.foo.Bar"));
Assert.assertEquals("", ClassUtil.removeHiddenPackage(hs, "com.foo"));
Assert.assertEquals("com.bar.foo.Bar", ClassUtil.removeHiddenPackage(null, "com.bar.foo.Bar"));
Assert.assertEquals(null, ClassUtil.removeHiddenPackage(hs, null));
assertEquals("Foo", ClassUtil.removeHiddenPackage(hs, "com.foo.Foo"));
assertEquals("foo.Bar", ClassUtil.removeHiddenPackage(hs, "com.bar.foo.Bar"));
assertEquals("", ClassUtil.removeHiddenPackage(hs, "com.foo"));
assertEquals("com.bar.foo.Bar", ClassUtil.removeHiddenPackage(null, "com.bar.foo.Bar"));
assertEquals(null, ClassUtil.removeHiddenPackage(hs, null));
}

@Test
public void getShortClassNameTest() {
Assert.assertEquals("j.l.String",ClassUtil.getShortClassName("java.lang.String"));
Assert.assertEquals("String",ClassUtil.getShortClassName("String"));
assertNull(ClassUtil.getShortClassName(null));
assertEquals("j.l.String", ClassUtil.getShortClassName("java.lang.String"));
assertEquals("String", ClassUtil.getShortClassName("String"));
}

@Test
Expand All @@ -93,15 +102,15 @@ public void testGetMethodSig() throws Exception {

String s1 = m1.getName() + "()V";
String s2 = ClassUtil.getMethodSig(m1);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);

s1 = m2.getName() + "(L" + I1.class.getName().replace('.', '/') + ";)Ljava/lang/String;";
s2 = ClassUtil.getMethodSig(m2);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);

s1 = m3.getName() + "(L" + I1.class.getName().replace('.', '/') + ";)Ljava/lang/String;";
s2 = ClassUtil.getMethodSig(m3);
Assert.assertEquals(s1, s2);
assertEquals(s1, s2);
}

}

0 comments on commit f69a252

Please sign in to comment.