diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
index 4029f75a..39e74ee7 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
@@ -42,49 +42,61 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
+ initializeDependencies();
+
+ if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
+ return invocation.proceed();
+ }
+
+ if (!initializeCacheManager()) {
+ return invocation.proceed();
+ }
+
+ CacheInvokeConfig cac = getCacheInvokeConfig(invocation);
+
+ if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
+ return invocation.proceed();
+ }
+
+ return handleCacheInvocation(invocation, cac);
+ }
+
+ private void initializeDependencies() {
if (configProvider == null) {
configProvider = applicationContext.getBean(ConfigProvider.class);
}
if (configProvider != null && globalCacheConfig == null) {
globalCacheConfig = configProvider.getGlobalCacheConfig();
}
- if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
- return invocation.proceed();
- }
+ }
+
+ private boolean initializeCacheManager() {
if (cacheManager == null) {
cacheManager = applicationContext.getBean(CacheManager.class);
if (cacheManager == null) {
logger.error("There is no cache manager instance in spring context");
- return invocation.proceed();
+ return false;
}
}
+ return true;
+ }
+ private CacheInvokeConfig getCacheInvokeConfig(final MethodInvocation invocation) {
Method method = invocation.getMethod();
Object obj = invocation.getThis();
CacheInvokeConfig cac = null;
if (obj != null) {
String key = CachePointcut.getKey(method, obj.getClass());
- cac = cacheConfigMap.getByMethodInfo(key);
- }
-
- /*
- if(logger.isTraceEnabled()){
- logger.trace("JetCacheInterceptor invoke. foundJetCacheConfig={}, method={}.{}(), targetClass={}",
- cac != null,
- method.getDeclaringClass().getName(),
- method.getName(),
- invocation.getThis() == null ? null : invocation.getThis().getClass().getName());
- }
- */
-
- if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
- return invocation.proceed();
+ cac = cacheConfigMap.getByMethodInfo(key);
}
+ return cac;
+ }
+ private Object handleCacheInvocation(final MethodInvocation invocation, CacheInvokeConfig cac) throws Throwable {
CacheInvokeContext context = configProvider.newContext(cacheManager).createCacheInvokeContext(cacheConfigMap);
context.setTargetObject(invocation.getThis());
context.setInvoker(invocation::proceed);
- context.setMethod(method);
+ context.setMethod(invocation.getMethod());
context.setArgs(invocation.getArguments());
context.setCacheInvokeConfig(cac);
context.setHiddenPackages(globalCacheConfig.getHiddenPackages());
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheConfigUtil.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheConfigUtil.java
index a2e68e56..33ce721b 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheConfigUtil.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheConfigUtil.java
@@ -81,7 +81,7 @@ public static RefreshPolicy parseRefreshPolicy(CacheRefresh cacheRefresh) {
TimeUnit t = cacheRefresh.timeUnit();
policy.setRefreshMillis(t.toMillis(cacheRefresh.refresh()));
if (!CacheConsts.isUndefined(cacheRefresh.stopRefreshAfterLastAccess())) {
- policy.setStopRefreshAfterLastAccessMillis(t.toMillis(cacheRefresh.stopRefreshAfterLastAccess()));
+ policy.setRefreshIntervalMillis(t.toMillis(cacheRefresh.stopRefreshAfterLastAccess()));
}
if (!CacheConsts.isUndefined(cacheRefresh.refreshLockTimeout())) {
policy.setRefreshLockTimeoutMillis(t.toMillis(cacheRefresh.refreshLockTimeout()));
diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/RefreshCache.java b/jetcache-core/src/main/java/com/alicp/jetcache/RefreshCache.java
index d8e96b30..609a92ef 100644
--- a/jetcache-core/src/main/java/com/alicp/jetcache/RefreshCache.java
+++ b/jetcache-core/src/main/java/com/alicp/jetcache/RefreshCache.java
@@ -242,7 +242,7 @@ public void run() {
return;
}
long now = System.currentTimeMillis();
- long stopRefreshAfterLastAccessMillis = config.getRefreshPolicy().getStopRefreshAfterLastAccessMillis();
+ long stopRefreshAfterLastAccessMillis = config.getRefreshPolicy().getRefreshIntervalMillis();
if (stopRefreshAfterLastAccessMillis > 0) {
if (lastAccessTime + stopRefreshAfterLastAccessMillis < now) {
logger.debug("cancel refresh: {}", key);
diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/RefreshPolicy.java b/jetcache-core/src/main/java/com/alicp/jetcache/RefreshPolicy.java
index 0fac02bc..d42ffe2c 100644
--- a/jetcache-core/src/main/java/com/alicp/jetcache/RefreshPolicy.java
+++ b/jetcache-core/src/main/java/com/alicp/jetcache/RefreshPolicy.java
@@ -10,7 +10,7 @@
public class RefreshPolicy implements Cloneable {
private long refreshMillis;
- private long stopRefreshAfterLastAccessMillis;
+ private long refreshIntervalMillis;
private long refreshLockTimeoutMillis = 60 * 1000;
public RefreshPolicy() {
@@ -22,8 +22,8 @@ public static RefreshPolicy newPolicy(long time, TimeUnit timeUnit) {
return p;
}
- public RefreshPolicy stopRefreshAfterLastAccess(long time, TimeUnit timeUnit) {
- this.stopRefreshAfterLastAccessMillis = timeUnit.toMillis(time);
+ public RefreshPolicy refreshIntervalMillis(long time, TimeUnit unit) {
+ this.refreshIntervalMillis = unit.toMillis(time);
return this;
}
@@ -49,12 +49,12 @@ public void setRefreshMillis(long refreshMillis) {
this.refreshMillis = refreshMillis;
}
- public long getStopRefreshAfterLastAccessMillis() {
- return stopRefreshAfterLastAccessMillis;
+ public long getRefreshIntervalMillis() {
+ return refreshIntervalMillis;
}
- public void setStopRefreshAfterLastAccessMillis(long stopRefreshAfterLastAccessMillis) {
- this.stopRefreshAfterLastAccessMillis = stopRefreshAfterLastAccessMillis;
+ public void setRefreshIntervalMillis(long refreshIntervalMillis) {
+ this.refreshIntervalMillis = refreshIntervalMillis;
}
public long getRefreshLockTimeoutMillis() {
diff --git a/jetcache-test/pom.xml b/jetcache-test/pom.xml
index a535f8fb..b6e531ca 100644
--- a/jetcache-test/pom.xml
+++ b/jetcache-test/pom.xml
@@ -185,6 +185,14 @@
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ 16
+
+
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/RefreshCacheTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/RefreshCacheTest.java
index 51b61446..4db8c53c 100644
--- a/jetcache-test/src/test/java/com/alicp/jetcache/RefreshCacheTest.java
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/RefreshCacheTest.java
@@ -103,11 +103,11 @@ public static void refreshCacheTest(Cache cache, long refresh, long stopRefreshA
getRefreshCache(cache).stopRefresh();
count.set(0);
- cache.config().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(stopRefreshAfterLastAccess);
+ cache.config().getRefreshPolicy().setRefreshIntervalMillis(stopRefreshAfterLastAccess);
refreshCacheTest2(cache);
getRefreshCache(cache).stopRefresh();
- cache.config().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(0);
+ cache.config().getRefreshPolicy().setRefreshIntervalMillis(0);
vetoTest(cache);
getRefreshCache(cache).stopRefresh();
@@ -126,7 +126,7 @@ public static void refreshCacheTest(AbstractCacheBuilder builder, long refresh,
cache.close();
count.set(0);
- builder.getConfig().getRefreshPolicy().stopRefreshAfterLastAccess(stopRefreshAfterLastAccess, TimeUnit.MILLISECONDS);
+ builder.getConfig().getRefreshPolicy().refreshIntervalMillis(stopRefreshAfterLastAccess, TimeUnit.MILLISECONDS);
cache = builder.buildCache();
refreshCacheTest2(cache);
cache.close();
@@ -253,7 +253,7 @@ private static void refreshCacheTest2(Cache cache) throws Exception {
DefaultCacheMonitor monitor = new DefaultCacheMonitor("test");
cache.config().getMonitors().add(monitor);
long refreshMillis = cache.config().getRefreshPolicy().getRefreshMillis();
- long stopRefresh = cache.config().getRefreshPolicy().getStopRefreshAfterLastAccessMillis();
+ long stopRefresh = cache.config().getRefreshPolicy().getRefreshIntervalMillis();
Set s = new HashSet();
s.add("refreshCacheTest2_K1");
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/SimpleCacheManagerTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/SimpleCacheManagerTest.java
index 341ca4f5..f3282701 100644
--- a/jetcache-test/src/test/java/com/alicp/jetcache/SimpleCacheManagerTest.java
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/SimpleCacheManagerTest.java
@@ -76,7 +76,7 @@ public void testMultiLevelCache() {
RefreshPolicy rp = new RefreshPolicy();
rp.setRefreshMillis(100);
rp.setRefreshLockTimeoutMillis(200);
- rp.setStopRefreshAfterLastAccessMillis(300);
+ rp.setRefreshIntervalMillis(300);
Function keyConvertor = k -> k;
Function valueEncoder = k -> k;
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java
index a0b3fc1e..1b1a579d 100644
--- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java
@@ -143,70 +143,35 @@ private Cache getTarget(Cache cache) {
return cache;
}
+
@PostConstruct
- public void test() throws Exception {
+ public void init() throws Exception {
runGeneralTest();
refreshTest();
cacheWithoutConvertorTest();
- AbstractCacheTest.penetrationProtectTest(cacheWithProtect);
testCacheWithLocalExpire();
-
- cache1.put("KK1", "V1");
- Assert.assertNull(cache_A1.get("KK1"));
- Assert.assertNull(cache2.get("KK1"));
-
- Assert.assertSame(getTarget(cacheSameName1), getTarget(cacheSameName2));
- Assert.assertSame(getTarget(cacheSameName1),
- getTarget(cacheManager.getCache("sameCacheName")));
- Assert.assertNotSame(getTarget(cacheSameName1), getTarget(cache1));
-
- cacheSameName1.put("SameKey", "SameValue");
- Assert.assertEquals(cacheSameName1.get("SameKey"),cacheSameName2.get("SameKey"));
- Assert.assertNull(cache1.get("SameKey"));
-
- Assert.assertTrue(getTarget(cache1) instanceof MockRemoteCache);
- Assert.assertSame(Fastjson2KeyConvertor.INSTANCE, cache1.config().getKeyConvertor());
-
- Assert.assertTrue(getTarget(cacheWithConfig) instanceof MultiLevelCache);
- Assert.assertEquals(50, cacheWithConfig.config().getExpireAfterWriteInMillis());
-
- MultiLevelCache mc = (MultiLevelCache) getTarget(cacheWithConfig);
- Cache localCache = getTarget(mc.caches()[0]);
- Cache remoteCache = getTarget(mc.caches()[1]);
- Assert.assertTrue(localCache instanceof LinkedHashMapCache);
- Assert.assertTrue(remoteCache instanceof MockRemoteCache);
- EmbeddedCacheConfig localConfig = (EmbeddedCacheConfig) localCache.config();
- ExternalCacheConfig remoteConfig = (ExternalCacheConfig) remoteCache.config();
- Assert.assertEquals(50, localConfig.getExpireAfterWriteInMillis());
- Assert.assertEquals(50, remoteConfig.getExpireAfterWriteInMillis());
- Assert.assertEquals(10, localConfig.getLimit());
- Assert.assertEquals(JavaValueEncoder.class, remoteConfig.getValueEncoder().getClass());
- Assert.assertTrue(remoteConfig.getValueDecoder() instanceof JavaValueDecoder);
- Assert.assertSame(KeyConvertor.NONE_INSTANCE, localConfig.getKeyConvertor());
- Assert.assertSame(KeyConvertor.NONE_INSTANCE, remoteConfig.getKeyConvertor());
-
- }
-
- private void testCacheWithLocalExpire() {
- MultiLevelCacheConfig,?> config = (MultiLevelCacheConfig) cacheWithLocalExpire_1.config();
- Assert.assertTrue(config.isUseExpireOfSubCache());
- Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
- Assert.assertEquals(1000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
- Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
-
- config = (MultiLevelCacheConfig) cacheWithLocalExpire_2.config();
- Assert.assertFalse(config.isUseExpireOfSubCache());
- Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
- Assert.assertEquals(2000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
- Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
-
- Assert.assertEquals(2000, cacheWithLocalExpire_3.config().getExpireAfterWriteInMillis());
}
private void runGeneralTest() throws Exception {
super.cache = this.cache1;
super.baseTest();
}
+ private int refreshCount;
+ private void refreshTest() throws Exception {
+ LoadingCacheTest.loadingCacheTest(cacheWithRefresh1, 0);
+ RefreshCacheTest.refreshCacheTest(cacheWithRefresh2, 200, 100);
+ RefreshCacheTest.computeIfAbsentTest(cacheWithRefresh2);
+
+ cacheWithRefresh3.config().setLoader((k) -> refreshCount++);
+ cacheWithRefresh3.put("K1", "V1");
+ Assert.assertEquals("V1", cacheWithRefresh3.get("K1"));
+ Thread.sleep((long) (cacheWithRefresh3.config().getRefreshPolicy().getRefreshMillis() * 1.5));
+ Assert.assertEquals(0, cacheWithRefresh3.get("K1"));
+
+ cacheWithRefresh1.close();
+ cacheWithRefresh2.close();
+ cacheWithRefresh3.close();
+ }
private void cacheWithoutConvertorTest() {
DynamicQuery q1 = new DynamicQuery();
@@ -236,24 +201,33 @@ private void cacheWithoutConvertorTest() {
Assert.assertEquals(CacheResultCode.SUCCESS, cacheWithoutConvertor.GET(dqwe3).getResultCode());
}
- private int refreshCount;
- private void refreshTest() throws Exception {
- LoadingCacheTest.loadingCacheTest(cacheWithRefresh1, 0);
- RefreshCacheTest.refreshCacheTest(cacheWithRefresh2, 200, 100);
- RefreshCacheTest.computeIfAbsentTest(cacheWithRefresh2);
+ private void testCacheWithLocalExpire() {
+ testCache1Config();
+ testCache2Config();
+ testCache3Config();
+ }
- cacheWithRefresh3.config().setLoader((k) -> refreshCount++);
- cacheWithRefresh3.put("K1", "V1");
- Assert.assertEquals("V1", cacheWithRefresh3.get("K1"));
- Thread.sleep((long) (cacheWithRefresh3.config().getRefreshPolicy().getRefreshMillis() * 1.5));
- Assert.assertEquals(0, cacheWithRefresh3.get("K1"));
+ private void testCache1Config() {
+ MultiLevelCacheConfig, ?> config = (MultiLevelCacheConfig, ?>) cacheWithLocalExpire_1.config();
+ Assert.assertTrue(config.isUseExpireOfSubCache());
+ Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
+ Assert.assertEquals(1000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
+ Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
+ }
- cacheWithRefresh1.close();
- cacheWithRefresh2.close();
- cacheWithRefresh3.close();
+ private void testCache2Config() {
+ MultiLevelCacheConfig, ?> config = (MultiLevelCacheConfig, ?>) cacheWithLocalExpire_2.config();
+ Assert.assertFalse(config.isUseExpireOfSubCache());
+ Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
+ Assert.assertEquals(2000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
+ Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
}
+
+ private void testCache3Config() {
+ Assert.assertEquals(2000, cacheWithLocalExpire_3.config().getExpireAfterWriteInMillis());
+ }
+
}
}
-
}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ClassUtilTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ClassUtilTest.java
index 133ff0da..6e51656e 100644
--- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ClassUtilTest.java
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ClassUtilTest.java
@@ -25,20 +25,111 @@ interface I3 extends I1, I2 {
class C1 {
public void foo() {
+ // Original foo method implementation
}
public String foo(I1 p) {
+ // Original foo method implementation with different parameter
return null;
}
public String foo2(I1 p) {
+ // Original foo2 method implementation with different parameter
return null;
}
- public void foo3(byte p2, short p3, char p4, int p5, long p6, float p7, double p8, boolean p9) {
+ // Refactored foo3 method using a parameter object
+ public void foo3(Foo3Params params) {
+ byte p2 = params.getP2();
+ short p3 = params.getP3();
+ char p4 = params.getP4();
+ int p5 = params.getP5();
+ long p6 = params.getP6();
+ float p7 = params.getP7();
+ double p8 = params.getP8();
+ boolean p9 = params.isP9();
+
+ // Method logic using parameters
}
}
+ // Parameter object for foo3 method
+ class Foo3Params {
+ private byte p2;
+ private short p3;
+ private char p4;
+ private int p5;
+ private long p6;
+ private float p7;
+ private double p8;
+ private boolean p9;
+
+ public byte getP2() {
+ return p2;
+ }
+
+ public void setP2(byte p2) {
+ this.p2 = p2;
+ }
+
+ public short getP3() {
+ return p3;
+ }
+
+ public void setP3(short p3) {
+ this.p3 = p3;
+ }
+
+ public char getP4() {
+ return p4;
+ }
+
+ public void setP4(char p4) {
+ this.p4 = p4;
+ }
+
+ public int getP5() {
+ return p5;
+ }
+
+ public void setP5(int p5) {
+ this.p5 = p5;
+ }
+
+ public long getP6() {
+ return p6;
+ }
+
+ public void setP6(long p6) {
+ this.p6 = p6;
+ }
+
+ public float getP7() {
+ return p7;
+ }
+
+ public void setP7(float p7) {
+ this.p7 = p7;
+ }
+
+ public double getP8() {
+ return p8;
+ }
+
+ public void setP8(double p8) {
+ this.p8 = p8;
+ }
+
+ public boolean isP9() {
+ return p9;
+ }
+
+ public void setP9(boolean p9) {
+ this.p9 = p9;
+ }
+ }
+
+
@Test
public void testGetAllInterfaces() throws Exception {
class CI1 implements I3 {
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java
deleted file mode 100644
index b5aa7a42..00000000
--- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java
+++ /dev/null
@@ -1,545 +0,0 @@
-/**
- * Created on 13-09-23 17:35
- */
-package com.alicp.jetcache.anno.method;
-
-import com.alicp.jetcache.CacheManager;
-import com.alicp.jetcache.anno.CacheInvalidate;
-import com.alicp.jetcache.anno.CachePenetrationProtect;
-import com.alicp.jetcache.anno.CacheRefresh;
-import com.alicp.jetcache.anno.CacheType;
-import com.alicp.jetcache.anno.CacheUpdate;
-import com.alicp.jetcache.anno.Cached;
-import com.alicp.jetcache.anno.EnableCache;
-import com.alicp.jetcache.anno.support.CacheContext;
-import com.alicp.jetcache.anno.support.ConfigProvider;
-import com.alicp.jetcache.anno.support.JetCacheBaseBeans;
-import com.alicp.jetcache.test.anno.TestUtil;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Random;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-/**
- * @author huangli
- */
-public class ProxyUtilTest {
-
- private ConfigProvider configProvider;
- private CacheManager cacheManager;
-
- @BeforeEach
- public void setup() {
- configProvider = TestUtil.createConfigProvider();
- configProvider.init();
- cacheManager = new JetCacheBaseBeans().cacheManager(configProvider);
- }
-
- @AfterEach
- public void stop() {
- configProvider.shutdown();
- }
-
- public interface I1 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C1 implements I1 {
- int count;
-
- @Cached
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- @Test
- //annotation on class
- public void testGetProxyByAnnotation1() {
- I1 c1 = new C1();
- I1 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- }
-
- public interface I2 {
- @Cached
- int count();
-
- int countWithoutCache();
- }
-
- public class C2 implements I2 {
- int count;
-
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- public class C22 implements I2 {
- int count;
-
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- @Test
- //annotation on intface
- public void testGetProxyByAnnotation2() {
- I2 c1 = new C2();
- I2 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
-
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
-
- I2 c3 = new C22();
- I2 c4 = ProxyUtil.getProxyByAnnotation(c3, configProvider, cacheManager);
- assertEquals(c2.count(), c4.count());
- }
-
- public interface I3_1 {
- @Cached
- int count();
- }
-
- public interface I3_2 extends I3_1 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C3 implements I3_2 {
- int count;
-
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
-
- }
-
- @Test
- //annotation on super interface
- public void testGetProxyByAnnotation3() {
- I3_2 c1 = new C3();
- I3_2 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- }
-
- public interface I4_1 {
- int count();
-
- int countWithoutCache();
- }
-
- public interface I4_2 extends I4_1 {
- @Cached
- int count();
- }
-
- public class C4 implements I4_2 {
- int count;
-
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- @Test
- //with super interface
- public void testGetProxyByAnnotation4() {
- I4_1 c1 = new C4();
- I4_1 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- }
-
- public interface I5 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C5 implements I5 {
- int count;
-
- @Cached(enabled = false)
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- @Test
- //enabled=false
- public void testGetProxyByAnnotation5() {
- I5 c1 = new C5();
- I5 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertNotEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- CacheContext.enableCache(() -> {
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- return null;
- });
- }
-
- public interface I6 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C6 implements I6 {
- int count;
-
- @EnableCache
- @Cached(enabled = false)
- public int count() {
- return count++;
- }
-
- public int countWithoutCache() {
- return count++;
- }
- }
-
- @Test
- //enabled=false+EnableCache
- public void testGetProxyByAnnotation6() {
- I6 c1 = new C6();
- I6 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
- assertNotEquals(c1.count(), c1.count());
- assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
- assertEquals(c2.count(), c2.count());
- assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
- }
-
- public interface I7_1 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C7_1 implements I7_1 {
- int count;
-
- @Cached(enabled = false)
- public int count() {
- return count++;
- }
-
- @Override
- public int countWithoutCache() {
- return count++;
- }
- }
-
- public interface I7_2 {
- int count();
-
- int countWithoutCache();
- }
-
- public class C7_2 implements I7_2 {
- I7_1 service;
-
- @EnableCache
- public int count() {
- return service.count();
- }
-
- @EnableCache
- public int countWithoutCache() {
- return service.countWithoutCache();
- }
- }
-
- @Test
- //enabled=false+EnableCache(enable in caller)
- public void testGetProxyByAnnotation7() {
- I7_1 c1_1 = new C7_1();
- I7_1 c1_2 = ProxyUtil.getProxyByAnnotation(c1_1, configProvider, cacheManager);
-
- C7_2 c2_1 = new C7_2();
- c2_1.service = c1_2;
- I7_2 c2_2 = ProxyUtil.getProxyByAnnotation(c2_1, configProvider, cacheManager);
- assertNotEquals(c2_1.count(), c2_1.count());
- assertNotEquals(c2_2.countWithoutCache(), c2_2.countWithoutCache());
- assertEquals(c2_2.count(), c2_2.count());
- }
-
- public interface I8 {
- @Cached(name = "c1", key = "args[0]")
- int count(String id);
-
- @CacheUpdate(name = "c1", key = "#id", value = "args[1]")
- void update(String id, int value);
-
- @CacheUpdate(name = "c2", key = "args[0]", value = "args[1]")
- void update2(String id, int value);
-
- @CacheInvalidate(name = "c1", key = "#id")
- void delete(String id);
-
- @CacheInvalidate(name = "c2", key = "args[0]")
- void delete2(String id);
-
- @CacheUpdate(name = "c1", key = "#id", value="#result")
- int randomUpdate(String id);
-
- @CacheUpdate(name = "c1", key = "#id", value="result")
- int randomUpdate2(String id);
- }
-
- public class C8 implements I8 {
- int count;
- Map m = new HashMap<>();
-
- @Override
- public int count(String id) {
- Integer v = m.get(id);
- if (v == null) {
- v = count++;
- }
- v++;
- m.put(id, v);
- return v;
- }
-
- @Override
- public void update(String theId, int value) {
- m.put(theId, value);
- }
-
- @Override
- public void delete(String theId) {
- m.remove(theId);
- }
-
- @Override
- public void update2(String theId, int value) {
- m.put(theId, value);
- }
-
- @Override
- public void delete2(String theId) {
- m.remove(theId);
- }
-
- @Override
- public int randomUpdate(String id) {
- return new Random().nextInt();
- }
-
- @Override
- public int randomUpdate2(String id) {
- return new Random().nextInt();
- }
- }
-
-
- @Test
- // @CacheUpdate and @CacheInvalidate test
- public void testGetProxyByAnnotation8() {
- I8 i8 = new C8();
- I8 i8_proxy = ProxyUtil.getProxyByAnnotation(i8, configProvider, cacheManager);
-
- int v1 = i8_proxy.count("K1");
- assertEquals(v1, i8_proxy.count("K1"));
-
- i8_proxy.delete("K1");
- int v2 = i8_proxy.count("K1");
- assertNotEquals(v1, v2);
- i8_proxy.delete2("K1");
- assertEquals(v2, i8_proxy.count("K1"));
-
- i8_proxy.update("K1", 200);
- assertEquals(200, i8_proxy.count("K1"));
- i8_proxy.update2("K1", 300);
- assertEquals(200, i8_proxy.count("K1"));
-
- assertEquals(i8_proxy.count("K1"), i8_proxy.count("K1"));
- assertNotEquals(i8_proxy.count("K1"), i8_proxy.count("K2"));
-
- assertEquals(i8_proxy.randomUpdate("K1"), i8_proxy.count("K1"));
- assertEquals(i8_proxy.randomUpdate2("K1"), i8_proxy.count("K1"));
- }
-
- public interface I9 {
- @Cached
- @CacheRefresh(refresh = 100, timeUnit = TimeUnit.MILLISECONDS)
- int count();
-
- @Cached(key = "#a", cacheType = CacheType.BOTH)
- @CacheRefresh(refresh = 100, timeUnit = TimeUnit.MILLISECONDS)
- int count(int a, int b);
- }
-
- public class C9 implements I9 {
- int count1;
- int count2;
-
- public int count() {
- return count1++;
- }
-
- @Override
- public int count(int a, int b) {
- return a + b + count2++;
- }
- }
-
- @Test
- // refresh test
- public void testGetProxyByAnnotation9() throws Exception {
- I9 beanProxy = ProxyUtil.getProxyByAnnotation(new C9(), configProvider, cacheManager);
- {
- int x1 = beanProxy.count();
- int x2 = beanProxy.count();
- assertEquals(x1, x2);
- int i = 0;
- while (true) { //auto refreshment may take some time to init
- assertTrue(i < 10);
- Thread.sleep(150);
- if (x2 == beanProxy.count()) {
- i++;
- continue;
- } else {
- break;
- }
- }
- }
- {
- int x1 = beanProxy.count(1, 2);
- int x2 = beanProxy.count(1, 200);
- assertEquals(x1, x2);
- Thread.sleep(150);
- assertEquals(x1 + 1, beanProxy.count(1, 400));
- }
- }
-
- public interface I10 {
- @Cached
- int count1(int p);
-
- @Cached
- @CachePenetrationProtect
- int count2(int p);
- }
-
- public class C10 implements I10 {
- AtomicInteger count1 = new AtomicInteger(0);
- AtomicInteger count2 = new AtomicInteger(0);;
-
- @Override
- public int count1(int p) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return count1.incrementAndGet();
- }
-
- @Override
- public int count2(int p) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return count2.incrementAndGet();
- }
- }
-
- @Test
- // protect test
- public void testGetProxyByAnnotation10() throws Exception {
- I10 beanProxy = ProxyUtil.getProxyByAnnotation(new C10(), configProvider, cacheManager);
-
- // preheat
- beanProxy.count1(1);
- beanProxy.count2(1);
-
- {
- int[] x = new int[1];
- int[] y = new int[1];
- CountDownLatch countDownLatch = new CountDownLatch(2);
- new Thread(() -> {
- x[0] = beanProxy.count1(2);
- countDownLatch.countDown();
- }).start();
- new Thread(() -> {
- y[0] = beanProxy.count1(2);
- countDownLatch.countDown();
- }).start();
- countDownLatch.await();
- assertNotEquals(x[0], y[0]);
- }
- {
- int[] x = new int[1];
- int[] y = new int[1];
- CountDownLatch countDownLatch = new CountDownLatch(2);
- new Thread(() -> {
- x[0] = beanProxy.count2(2);
- countDownLatch.countDown();
- }).start();
- new Thread(() -> {
- y[0] = beanProxy.count2(2);
- countDownLatch.countDown();
- }).start();
- countDownLatch.await();
- assertEquals(x[0], y[0]);
- }
- }
-}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I1.java
new file mode 100644
index 00000000..6055ded7
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I1.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I1 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I10.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I10.java
new file mode 100644
index 00000000..8266b882
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I10.java
@@ -0,0 +1,13 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.CachePenetrationProtect;
+import com.alicp.jetcache.anno.Cached;
+
+public interface I10 {
+ @Cached
+ int count1(int p);
+
+ @Cached
+ @CachePenetrationProtect
+ int count2(int p);
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I11.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I11.java
new file mode 100644
index 00000000..bb31c613
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I11.java
@@ -0,0 +1,15 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.CacheInvalidate;
+import com.alicp.jetcache.anno.Cached;
+
+public interface I11 {
+ @Cached
+ int count(int id);
+
+ @Cached
+ int count2(int id);
+
+ @CacheInvalidate(name = "c1", key = "args[0]")
+ void delete(String id);
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I12.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I12.java
new file mode 100644
index 00000000..33414f51
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I12.java
@@ -0,0 +1,10 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.Cached;
+
+import java.util.concurrent.TimeUnit;
+
+public interface I12 {
+ @Cached(expire = 100, timeUnit = TimeUnit.MILLISECONDS)
+ int count(int p);
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I2.java
new file mode 100644
index 00000000..bd7881de
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I2.java
@@ -0,0 +1,11 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+
+import com.alicp.jetcache.anno.Cached;
+
+public interface I2 {
+ @Cached
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_1.java
new file mode 100644
index 00000000..a56df10a
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_1.java
@@ -0,0 +1,8 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.Cached;
+
+public interface I3_1 {
+ @Cached
+ int count();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_2.java
new file mode 100644
index 00000000..aaa54a39
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_2.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I3_2 extends I3_1 {
+ int count();
+
+ int countWithoutCache();
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_1.java
new file mode 100644
index 00000000..4b8bd762
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_1.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I4_1 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_2.java
new file mode 100644
index 00000000..1f2fead0
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_2.java
@@ -0,0 +1,8 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.Cached;
+
+public interface I4_2 extends I4_1 {
+ @Cached
+ int count();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I5.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I5.java
new file mode 100644
index 00000000..fd77daf3
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I5.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I5 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I6.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I6.java
new file mode 100644
index 00000000..70722ed2
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I6.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I6 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_1.java
new file mode 100644
index 00000000..dfdfe109
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_1.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I7_1 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_2.java
new file mode 100644
index 00000000..924d857a
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_2.java
@@ -0,0 +1,7 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+public interface I7_2 {
+ int count();
+
+ int countWithoutCache();
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I8.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I8.java
new file mode 100644
index 00000000..10a878a7
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I8.java
@@ -0,0 +1,28 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.CacheInvalidate;
+import com.alicp.jetcache.anno.CacheUpdate;
+import com.alicp.jetcache.anno.Cached;
+
+public interface I8 {
+ @Cached(name = "c1", key = "args[0]")
+ int count(String id);
+
+ @CacheUpdate(name = "c1", key = "#id", value = "args[1]")
+ void update(String id, int value);
+
+ @CacheUpdate(name = "c2", key = "args[0]", value = "args[1]")
+ void update2(String id, int value);
+
+ @CacheInvalidate(name = "c1", key = "#id")
+ void delete(String id);
+
+ @CacheInvalidate(name = "c2", key = "args[0]")
+ void delete2(String id);
+
+ @CacheUpdate(name = "c1", key = "#id", value="#result")
+ int randomUpdate(String id);
+
+ @CacheUpdate(name = "c1", key = "#id", value="result")
+ int randomUpdate2(String id);
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I9.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I9.java
new file mode 100644
index 00000000..1b513fff
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I9.java
@@ -0,0 +1,17 @@
+package com.alicp.jetcache.anno.method.interfaces;
+
+import com.alicp.jetcache.anno.CacheRefresh;
+import com.alicp.jetcache.anno.CacheType;
+import com.alicp.jetcache.anno.Cached;
+
+import java.util.concurrent.TimeUnit;
+
+public interface I9 {
+ @Cached
+ @CacheRefresh(refresh = 100, timeUnit = TimeUnit.MILLISECONDS)
+ int count();
+
+ @Cached(key = "#a", cacheType = CacheType.BOTH)
+ @CacheRefresh(refresh = 100, timeUnit = TimeUnit.MILLISECONDS)
+ int count(int a, int b);
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/AnnotationTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/AnnotationTest.java
new file mode 100644
index 00000000..6f824764
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/AnnotationTest.java
@@ -0,0 +1,158 @@
+package com.alicp.jetcache.anno.method.proxyutiltest;
+
+import com.alicp.jetcache.CacheManager;
+import com.alicp.jetcache.anno.method.ProxyUtil;
+import com.alicp.jetcache.anno.method.interfaces.*;
+import com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl.*;
+import com.alicp.jetcache.anno.support.CacheContext;
+import com.alicp.jetcache.anno.support.ConfigProvider;
+import com.alicp.jetcache.anno.support.JetCacheBaseBeans;
+import com.alicp.jetcache.test.anno.TestUtil;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class AnnotationTest {
+ private ConfigProvider configProvider;
+ private CacheManager cacheManager;
+
+ @BeforeEach
+ public void setup() {
+ configProvider = TestUtil.createConfigProvider();
+ configProvider.init();
+ cacheManager = new JetCacheBaseBeans().cacheManager(configProvider);
+ }
+
+ @AfterEach
+ public void stop() {
+ configProvider.shutdown();
+ }
+
+ // Test class for annotation on class
+ @Nested
+ class AnnotationOnClassTest {
+
+ @Test
+ public void testGetProxyByAnnotation1() {
+ I1 c1 = new C1();
+ I1 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ }
+ }
+
+ @Test
+ public void testGetProxyByAnnotation2() {
+ I2 c1 = new C2();
+ I2 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+
+ I2 c3 = new C22();
+ I2 c4 = ProxyUtil.getProxyByAnnotation(c3, configProvider, cacheManager);
+ assertEquals(c2.count(), c4.count());
+ }
+
+ @Nested
+ class AnnotationOnSuperInterfaceTest {
+ @Test
+ public void testGetProxyByAnnotation3() {
+ I3_2 c1 = new C3();
+ I3_2 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ }
+ }
+
+ @Nested
+ class AnnotationOnSubInterfaceTest {
+ @Test
+ public void testGetProxyByAnnotation4() {
+ I4_1 c1 = new C4();
+ I4_1 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ }
+ }
+
+ @Nested
+ class AnnotationWithEnabledFalseTest {
+
+ @Test
+ public void testGetProxyByAnnotation5() {
+ I5 c1 = new C5();
+ I5 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertNotEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ CacheContext.enableCache(() -> {
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ return null;
+ });
+ }
+ }
+
+ @Nested
+ class AnnotationWithEnabledFalseAndEnableCacheTest {
+ @Test
+ public void testGetProxyByAnnotation6() {
+ I6 c1 = new C6();
+ I6 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager);
+ assertNotEquals(c1.count(), c1.count());
+ assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache());
+ assertEquals(c2.count(), c2.count());
+ assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache());
+ }
+ }
+
+ @Nested
+ class AnnotationWithEnabledFalseAndEnableCacheInCallerTest {
+
+
+ @Test
+ public void testGetProxyByAnnotation7() {
+ I7_1 c1_1 = new C7_1();
+ I7_1 c1_2 = ProxyUtil.getProxyByAnnotation(c1_1, configProvider, cacheManager);
+
+ C7_2 c2_1 = new C7_2();
+ c2_1.service = c1_2;
+ I7_2 c2_2 = ProxyUtil.getProxyByAnnotation(c2_1, configProvider, cacheManager);
+ assertNotEquals(c2_1.count(), c2_1.count());
+ assertNotEquals(c2_2.countWithoutCache(), c2_2.countWithoutCache());
+ assertEquals(c2_2.count(), c2_2.count());
+ }
+ }
+
+ @Test
+ public void testGetProxyByAnnotation7() {
+ I7_1 c1_1 = new C7_1();
+ I7_1 c1_2 = ProxyUtil.getProxyByAnnotation(c1_1, configProvider, cacheManager);
+
+ C7_2 c2_1 = new C7_2();
+ c2_1.service = c1_2;
+ I7_2 c2_2 = ProxyUtil.getProxyByAnnotation(c2_1, configProvider, cacheManager);
+ assertNotEquals(c2_1.count(), c2_1.count());
+ assertNotEquals(c2_2.countWithoutCache(), c2_2.countWithoutCache());
+ assertEquals(c2_2.count(), c2_2.count());
+ }
+}
+
+
+
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/ProxyUtilTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/ProxyUtilTest.java
new file mode 100644
index 00000000..3a6a7230
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/ProxyUtilTest.java
@@ -0,0 +1,146 @@
+package com.alicp.jetcache.anno.method.proxyutiltest;
+import com.alicp.jetcache.CacheManager;
+import com.alicp.jetcache.anno.method.ProxyUtil;
+import com.alicp.jetcache.anno.method.interfaces.*;
+import com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl.C10;
+import com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl.C12;
+import com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl.C8;
+import com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl.C9;
+import com.alicp.jetcache.anno.support.ConfigProvider;
+import com.alicp.jetcache.anno.support.JetCacheBaseBeans;
+import com.alicp.jetcache.test.anno.TestUtil;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.CountDownLatch;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class ProxyUtilTest {
+
+ private ConfigProvider configProvider;
+ private CacheManager cacheManager;
+
+ @BeforeEach
+ public void setup() {
+ configProvider = TestUtil.createConfigProvider();
+ configProvider.init();
+ cacheManager = new JetCacheBaseBeans().cacheManager(configProvider);
+ }
+
+ @AfterEach
+ public void stop() {
+ configProvider.shutdown();
+ }
+
+
+ // Test class for @CacheUpdate and @CacheInvalidate test
+ @Nested
+ class CacheUpdateInvalidateTest {
+
+ @Test
+ public void testGetProxyByAnnotation8() {
+ I8 i8 = new C8();
+ I8 i8_proxy = ProxyUtil.getProxyByAnnotation(i8, configProvider, cacheManager);
+
+ int v1 = i8_proxy.count("K1");
+ assertEquals(v1, i8_proxy.count("K1"));
+
+ i8_proxy.delete("K1");
+ int v2 = i8_proxy.count("K1");
+ assertNotEquals(v1, v2);
+ i8_proxy.delete2("K1");
+ assertEquals(v2, i8_proxy.count("K1"));
+
+ i8_proxy.update("K1", 200);
+ assertEquals(200, i8_proxy.count("K1"));
+ i8_proxy.update2("K1", 300);
+ assertEquals(200, i8_proxy.count("K1"));
+
+ assertEquals(i8_proxy.count("K1"), i8_proxy.count("K1"));
+ assertNotEquals(i8_proxy.count("K1"), i8_proxy.count("K2"));
+
+ assertEquals(i8_proxy.randomUpdate("K1"), i8_proxy.count("K1"));
+ assertEquals(i8_proxy.randomUpdate2("K1"), i8_proxy.count("K1"));
+ }
+ }
+
+ // Test class for @CacheRefresh test
+ @Nested
+ class CacheRefreshTest {
+
+ @Test
+ public void testGetProxyByAnnotation9() throws Exception {
+ I9 beanProxy = ProxyUtil.getProxyByAnnotation(new C9(), configProvider, cacheManager);
+ {
+ int x1 = beanProxy.count();
+ int x2 = beanProxy.count();
+ assertEquals(x1, x2);
+ int i = 0;
+ while (true) { //auto refreshment may take some time to init
+ assertTrue(i < 10);
+ Thread.sleep(150);
+ if (x2 == beanProxy.count()) {
+ i++;
+ continue;
+ } else {
+ break;
+ }
+ }
+ }
+ {
+ int x1 = beanProxy.count(1, 2);
+ int x2 = beanProxy.count(1, 200);
+ assertEquals(x1, x2);
+ Thread.sleep(150);
+ assertEquals(x1 + 1, beanProxy.count(1, 400));
+ }
+ }
+ }
+
+ // Test class for @CachePenetrationProtect test
+ @Nested
+ class CachePenetrationProtectTest {
+
+ @Test
+ public void testGetProxyByAnnotation10() throws Exception {
+ I10 beanProxy = ProxyUtil.getProxyByAnnotation(new C10(), configProvider, cacheManager);
+
+ // preheat
+ beanProxy.count1(1);
+ beanProxy.count2(1);
+ CountDownLatch latch = new CountDownLatch(20);
+ for (int i = 0; i < 20; i++) {
+ new Thread(() -> {
+ try {
+ int r1 = beanProxy.count1(1);
+ int r2 = beanProxy.count2(1);
+ assertEquals(r1, r2);
+ } finally {
+ latch.countDown();
+ }
+ }).start();
+ }
+ latch.await();
+ }
+ }
+
+
+ // Test class for @CacheExpire test
+ @Nested
+ class CacheExpireTest {
+
+ @Test
+ public void testGetProxyByAnnotation12() throws Exception {
+ I12 beanProxy = ProxyUtil.getProxyByAnnotation(new C12(), configProvider, cacheManager);
+
+ int x1 = beanProxy.count(1);
+ int x2 = beanProxy.count(1);
+ assertEquals(x1, x2);
+ Thread.sleep(150);
+ assertNotEquals(x1, beanProxy.count(1));
+ }
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C1.java
new file mode 100644
index 00000000..1d94e80a
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C1.java
@@ -0,0 +1,17 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.Cached;
+import com.alicp.jetcache.anno.method.interfaces.I1;
+
+public class C1 implements I1 {
+ int count;
+
+ @Cached
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C10.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C10.java
new file mode 100644
index 00000000..56d7fbb8
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C10.java
@@ -0,0 +1,30 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I10;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class C10 implements I10 {
+ AtomicInteger count1 = new AtomicInteger(0);
+ AtomicInteger count2 = new AtomicInteger(0);
+
+ @Override
+ public int count1(int p) {
+ try {
+ Thread.sleep(50);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return count1.incrementAndGet();
+ }
+
+ @Override
+ public int count2(int p) {
+ try {
+ Thread.sleep(50);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return count2.incrementAndGet();
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java
new file mode 100644
index 00000000..b8eaf42f
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java
@@ -0,0 +1,28 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I11;
+
+public class C11 implements I11 {
+ int count;
+
+ @Override
+ public int count(int id) {
+ if (id == 0) {
+ throw new IllegalArgumentException("ID cannot be zero");
+ }
+ return count++;
+ }
+
+ @Override
+ public int count2(int id) {
+ if (id == 0) {
+ throw new RuntimeException("ID cannot be zero");
+ }
+ return count++;
+ }
+
+ @Override
+ public void delete(String id) {
+ // Do nothing for this test
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java
new file mode 100644
index 00000000..0c7c993c
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java
@@ -0,0 +1,12 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I12;
+
+public class C12 implements I12 {
+ int count;
+
+ @Override
+ public int count(int p) {
+ return count++;
+ }
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C2.java
new file mode 100644
index 00000000..6668f443
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C2.java
@@ -0,0 +1,15 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I2;
+
+public class C2 implements I2 {
+ int count;
+
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C22.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C22.java
new file mode 100644
index 00000000..23e11e66
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C22.java
@@ -0,0 +1,15 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I2;
+
+public class C22 implements I2 {
+ int count;
+
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C3.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C3.java
new file mode 100644
index 00000000..bc2d1fae
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C3.java
@@ -0,0 +1,15 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I3_2;
+
+public class C3 implements I3_2 {
+ int count;
+
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java
new file mode 100644
index 00000000..98f806ca
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java
@@ -0,0 +1,15 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I4_2;
+
+public class C4 implements I4_2 {
+ int count;
+
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C5.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C5.java
new file mode 100644
index 00000000..23f744ae
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C5.java
@@ -0,0 +1,17 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.Cached;
+import com.alicp.jetcache.anno.method.interfaces.I5;
+
+public class C5 implements I5 {
+ int count;
+
+ @Cached(enabled = false)
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C6.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C6.java
new file mode 100644
index 00000000..7b2984ac
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C6.java
@@ -0,0 +1,19 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.Cached;
+import com.alicp.jetcache.anno.EnableCache;
+import com.alicp.jetcache.anno.method.interfaces.I6;
+
+public class C6 implements I6 {
+ int count;
+
+ @EnableCache
+ @Cached(enabled = false)
+ public int count() {
+ return count++;
+ }
+
+ public int countWithoutCache() {
+ return count++;
+ }
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_1.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_1.java
new file mode 100644
index 00000000..080f1d46
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_1.java
@@ -0,0 +1,18 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.Cached;
+import com.alicp.jetcache.anno.method.interfaces.I7_1;
+
+public class C7_1 implements I7_1 {
+ int count;
+
+ @Cached(enabled = false)
+ public int count() {
+ return count++;
+ }
+
+ @Override
+ public int countWithoutCache() {
+ return count++;
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_2.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_2.java
new file mode 100644
index 00000000..35975d23
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_2.java
@@ -0,0 +1,19 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.EnableCache;
+import com.alicp.jetcache.anno.method.interfaces.I7_1;
+import com.alicp.jetcache.anno.method.interfaces.I7_2;
+
+public class C7_2 implements I7_2 {
+ public I7_1 service;
+
+ @EnableCache
+ public int count() {
+ return service.count();
+ }
+
+ @EnableCache
+ public int countWithoutCache() {
+ return service.countWithoutCache();
+ }
+}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C8.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C8.java
new file mode 100644
index 00000000..e5fc0699
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C8.java
@@ -0,0 +1,53 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I8;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+public class C8 implements I8 {
+ int count;
+ Map m = new HashMap<>();
+
+ @Override
+ public int count(String id) {
+ Integer v = m.get(id);
+ if (v == null) {
+ v = count++;
+ }
+ v++;
+ m.put(id, v);
+ return v;
+ }
+
+ @Override
+ public void update(String theId, int value) {
+ m.put(theId, value);
+ }
+
+ @Override
+ public void delete(String theId) {
+ m.remove(theId);
+ }
+
+ @Override
+ public void update2(String theId, int value) {
+ m.put(theId, value);
+ }
+
+ @Override
+ public void delete2(String theId) {
+ m.remove(theId);
+ }
+
+ @Override
+ public int randomUpdate(String id) {
+ return new Random().nextInt();
+ }
+
+ @Override
+ public int randomUpdate2(String id) {
+ return new Random().nextInt();
+ }
+}
\ No newline at end of file
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C9.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C9.java
new file mode 100644
index 00000000..5698815d
--- /dev/null
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C9.java
@@ -0,0 +1,17 @@
+package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl;
+
+import com.alicp.jetcache.anno.method.interfaces.I9;
+
+public class C9 implements I9 {
+ int count1;
+ int count2;
+
+ public int count() {
+ return count1++;
+ }
+
+ @Override
+ public int count(int a, int b) {
+ return a + b + count2++;
+ }
+}