From 17802d5128939341a14a04191c2f7cf4e6965d4f Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 09:28:51 -0300 Subject: [PATCH 1/7] 1st refactor: complex method in invoke method --- .../anno/aop/JetCacheInterceptor.java | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) 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()); From 6eb1050617ee2e3fcc4931fb81226772893a1565 Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 10:12:22 -0300 Subject: [PATCH 2/7] 2nd refactor: Long Parameter List method in foo3 method --- .../jetcache/anno/method/ClassUtilTest.java | 105 +++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) 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..e3b8a478 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,123 @@ 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; + + // Constructor and getters/setters for all parameters + public Foo3Params(byte p2, short p3, char p4, int p5, long p6, float p7, double p8, boolean p9) { + this.p2 = p2; + this.p3 = p3; + this.p4 = p4; + this.p5 = p5; + this.p6 = p6; + this.p7 = p7; + this.p8 = p8; + this.p9 = 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 { From 7bc91015b368ef115d36efa4c9a3c21c3494d97b Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 10:27:36 -0300 Subject: [PATCH 3/7] 3rd refactor: Long identifier method in stopRefreshAfterLastAccess method --- .../jetcache/anno/method/CacheConfigUtil.java | 2 +- .../main/java/com/alicp/jetcache/RefreshCache.java | 2 +- .../java/com/alicp/jetcache/RefreshPolicy.java | 14 +++++++------- .../java/com/alicp/jetcache/RefreshCacheTest.java | 8 ++++---- .../com/alicp/jetcache/SimpleCacheManagerTest.java | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) 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/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; From 99a63a9c532e3d3cfa74082621e3beee9666ce4a Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 11:40:46 -0300 Subject: [PATCH 4/7] 4th refactor: Insufficient Modularization smell in ProxyUtilTest class. --- jetcache-test/pom.xml | 8 + .../jetcache/anno/method/ProxyUtilTest.java | 729 ++++++++---------- .../jetcache/anno/method/interfaces/C11.java | 26 + .../jetcache/anno/method/interfaces/C12.java | 10 + .../jetcache/anno/method/interfaces/C4.java | 13 + .../jetcache/anno/method/interfaces/I1.java | 7 + .../jetcache/anno/method/interfaces/I10.java | 13 + .../jetcache/anno/method/interfaces/I11.java | 15 + .../jetcache/anno/method/interfaces/I12.java | 10 + .../jetcache/anno/method/interfaces/I2.java | 11 + .../jetcache/anno/method/interfaces/I3_1.java | 8 + .../jetcache/anno/method/interfaces/I3_2.java | 7 + .../jetcache/anno/method/interfaces/I4_1.java | 7 + .../jetcache/anno/method/interfaces/I4_2.java | 8 + .../jetcache/anno/method/interfaces/I5.java | 7 + .../jetcache/anno/method/interfaces/I6.java | 7 + .../jetcache/anno/method/interfaces/I7_1.java | 7 + .../jetcache/anno/method/interfaces/I7_2.java | 7 + .../jetcache/anno/method/interfaces/I8.java | 28 + .../jetcache/anno/method/interfaces/I9.java | 17 + 20 files changed, 544 insertions(+), 401 deletions(-) create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C11.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C4.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I10.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I11.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I12.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I3_2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I4_2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I5.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I6.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I7_2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I8.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/I9.java 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 + 16 + + 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 index b5aa7a42..c901d50e 100644 --- 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 @@ -1,22 +1,17 @@ -/** - * Created on 13-09-23 17:35 - */ package com.alicp.jetcache.anno.method; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 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.*; +import com.alicp.jetcache.anno.method.interfaces.*; 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 java.util.HashMap; @@ -26,13 +21,8 @@ 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; +import static org.junit.jupiter.api.Assertions.*; -/** - * @author huangli - */ public class ProxyUtilTest { private ConfigProvider configProvider; @@ -50,496 +40,433 @@ public void stop() { configProvider.shutdown(); } - public interface I1 { - int count(); + // Test class for annotation on class + @Nested + class AnnotationOnClassTest { - int countWithoutCache(); - } - public class C1 implements I1 { - int count; + public class C1 implements I1 { + int count; - @Cached - public int count() { - return count++; - } + @Cached + public int count() { + return count++; + } - public int countWithoutCache() { - 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()); + @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()); + } } - public interface I2 { - @Cached - int count(); - - int countWithoutCache(); - } + // Test class for annotation on interface + @Nested + class AnnotationOnInterfaceTest { - public class C2 implements I2 { - int count; + public class C2 implements I2 { + int count; - public int count() { - return count++; - } + public int count() { + return count++; + } - public int countWithoutCache() { - return count++; + public int countWithoutCache() { + return count++; + } } - } - public class C22 implements I2 { - int count; + public class C22 implements I2 { + int count; - public int count() { - return count++; - } + public int count() { + return count++; + } - public int countWithoutCache() { - 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(); - } + @Test + public void testGetProxyByAnnotation2() { + I2 c1 = new C2(); + I2 c2 = ProxyUtil.getProxyByAnnotation(c1, configProvider, cacheManager); - public class C3 implements I3_2 { - int count; - - public int count() { - return count++; - } + assertNotEquals(c1.count(), c1.count()); + assertNotEquals(c1.countWithoutCache(), c1.countWithoutCache()); + assertEquals(c2.count(), c2.count()); + assertNotEquals(c2.countWithoutCache(), c2.countWithoutCache()); - public int countWithoutCache() { - return count++; + I2 c3 = new C22(); + I2 c4 = ProxyUtil.getProxyByAnnotation(c3, configProvider, cacheManager); + assertEquals(c2.count(), c4.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(); - } + @Nested + class AnnotationOnSuperInterfaceTest { - public interface I4_2 extends I4_1 { - @Cached - int count(); - } + public class C3 implements I3_2 { + int count; - public class C4 implements I4_2 { - int count; + public int count() { + return count++; + } - public int count() { - return count++; + public int countWithoutCache() { + return count++; + } } - public int countWithoutCache() { - return count++; + @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()); } } - @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()); - } + // Test class for annotation on sub interface + @Nested + class AnnotationOnSubInterfaceTest { - public interface I5 { - int count(); + public class C4 implements I4_2 { + int count; - int countWithoutCache(); - } - - public class C5 implements I5 { - int count; - - @Cached(enabled = false) - public int count() { - return count++; - } + public int count() { + return count++; + } - public int countWithoutCache() { - 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(() -> { + @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()); - return null; - }); + } } - public interface I6 { - int count(); + // Test class for annotation with enabled=false + @Nested + class AnnotationWithEnabledFalseTest { + public class C5 implements I5 { + int count; - int countWithoutCache(); - } - - public class C6 implements I6 { - int count; + @Cached(enabled = false) + public int count() { + return count++; + } - @EnableCache - @Cached(enabled = false) - public int count() { - return count++; + public int countWithoutCache() { + return count++; + } } - public int countWithoutCache() { - return count++; + @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; + }); } } - @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()); - } + // Test class for annotation with enabled=false + EnableCache + @Nested + class AnnotationWithEnabledFalseAndEnableCacheTest { - public interface I7_1 { - int count(); + public class C6 implements I6 { + int count; - int countWithoutCache(); - } - - public class C7_1 implements I7_1 { - int count; + @EnableCache + @Cached(enabled = false) + public int count() { + return count++; + } - @Cached(enabled = false) - public int count() { - return count++; + public int countWithoutCache() { + return count++; + } } - @Override - public int countWithoutCache() { - return count++; + @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()); } } - public interface I7_2 { - int count(); + // Test class for annotation with enabled=false + EnableCache (enable in caller) + @Nested + class AnnotationWithEnabledFalseAndEnableCacheInCallerTest { - int countWithoutCache(); - } - - public class C7_2 implements I7_2 { - I7_1 service; + public class C7_1 implements I7_1 { + int count; - @EnableCache - public int count() { - return service.count(); - } + @Cached(enabled = false) + public int count() { + return count++; + } - @EnableCache - public int countWithoutCache() { - return service.countWithoutCache(); + @Override + public int countWithoutCache() { + return count++; + } } - } - @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 class C7_2 implements I7_2 { + I7_1 service; - public interface I8 { - @Cached(name = "c1", key = "args[0]") - int count(String id); + @EnableCache + public int count() { + return service.count(); + } - @CacheUpdate(name = "c1", key = "#id", value = "args[1]") - void update(String id, int value); + @EnableCache + public int countWithoutCache() { + return service.countWithoutCache(); + } + } - @CacheUpdate(name = "c2", key = "args[0]", value = "args[1]") - void update2(String id, int value); + @Test + public void testGetProxyByAnnotation7() { + I7_1 c1_1 = new C7_1(); + I7_1 c1_2 = ProxyUtil.getProxyByAnnotation(c1_1, configProvider, cacheManager); - @CacheInvalidate(name = "c1", key = "#id") - void delete(String id); + 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()); + } + } - @CacheInvalidate(name = "c2", key = "args[0]") - void delete2(String id); + // Test class for @CacheUpdate and @CacheInvalidate test + @Nested + class CacheUpdateInvalidateTest { - @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<>(); + 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++; + @Override + public int count(String id) { + Integer v = m.get(id); + if (v == null) { + v = count++; + } + v++; + m.put(id, v); + return v; } - v++; - m.put(id, v); - return v; - } - @Override - public void update(String theId, int value) { - m.put(theId, value); - } + @Override + public void update(String theId, int value) { + m.put(theId, value); + } - @Override - public void delete(String theId) { - m.remove(theId); - } + @Override + public void delete(String theId) { + m.remove(theId); + } - @Override - public void update2(String theId, int value) { - m.put(theId, value); - } + @Override + public void update2(String theId, int value) { + m.put(theId, value); + } - @Override - public void delete2(String theId) { - m.remove(theId); - } + @Override + public void delete2(String theId) { + m.remove(theId); + } - @Override - public int randomUpdate(String id) { - return new Random().nextInt(); - } + @Override + public int randomUpdate(String id) { + return new Random().nextInt(); + } - @Override - public int randomUpdate2(String id) { - return new Random().nextInt(); + @Override + public int randomUpdate2(String id) { + return new Random().nextInt(); + } } - } + @Test + public void testGetProxyByAnnotation8() { + I8 i8 = new C8(); + I8 i8_proxy = ProxyUtil.getProxyByAnnotation(i8, configProvider, cacheManager); - @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")); - 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.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")); - 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.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")); + 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); - } + // Test class for @CacheRefresh test + @Nested + class CacheRefreshTest { - public class C9 implements I9 { - int count1; - int count2; + public class C9 implements I9 { + int count1; + int count2; - public int count() { - return count1++; - } + public int count() { + return count1++; + } - @Override - public int count(int a, int b) { - return a + b + count2++; + @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; + @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)); + { + 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); - } + // Test class for @CachePenetrationProtect test + @Nested + class CachePenetrationProtectTest { + public class C10 implements I10 { + AtomicInteger count1 = new AtomicInteger(0); + AtomicInteger count2 = new AtomicInteger(0); - 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 count1(int p) { - try { - Thread.sleep(50); - } catch (InterruptedException e) { - e.printStackTrace(); + @Override + public int count2(int p) { + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return count2.incrementAndGet(); } - return count1.incrementAndGet(); } - @Override - public int count2(int p) { - try { - Thread.sleep(50); - } catch (InterruptedException e) { - e.printStackTrace(); + @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(); } - return count2.incrementAndGet(); + latch.await(); } } - @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]); + + // 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/interfaces/C11.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C11.java new file mode 100644 index 00000000..c7aae4cc --- /dev/null +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C11.java @@ -0,0 +1,26 @@ +package com.alicp.jetcache.anno.method.interfaces; + +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/interfaces/C12.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java new file mode 100644 index 00000000..579d7476 --- /dev/null +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java @@ -0,0 +1,10 @@ +package com.alicp.jetcache.anno.method.interfaces; + +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/interfaces/C4.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C4.java new file mode 100644 index 00000000..f4cb49df --- /dev/null +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C4.java @@ -0,0 +1,13 @@ +package com.alicp.jetcache.anno.method.interfaces; + +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/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); +} From 7b022cce39d48575f285ce47848f14e8152970e3 Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 12:42:15 -0300 Subject: [PATCH 5/7] 5th refactor: Multifaceted Abstraction in CreateCacheTest --- .../jetcache/anno/filed/CreateCacheTest.java | 108 +++++++----------- 1 file changed, 41 insertions(+), 67 deletions(-) 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()); + } + } } - } From a38a613dc2ff185555ee5d0964034b06bbe3b939 Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 20:35:19 -0300 Subject: [PATCH 6/7] Updated 4th Refactor: fixed in order to remove the code smell flag from csv --- .../jetcache/anno/method/ProxyUtilTest.java | 472 ------------------ .../method/proxyutiltest/AnnotationTest.java | 158 ++++++ .../method/proxyutiltest/ProxyUtilTest.java | 146 ++++++ .../proxyutiltest/annotationimpl/C1.java | 17 + .../proxyutiltest/annotationimpl/C10.java | 30 ++ .../annotationimpl}/C11.java | 4 +- .../annotationimpl}/C12.java | 4 +- .../proxyutiltest/annotationimpl/C2.java | 15 + .../proxyutiltest/annotationimpl/C22.java | 15 + .../proxyutiltest/annotationimpl/C3.java | 15 + .../annotationimpl}/C4.java | 4 +- .../proxyutiltest/annotationimpl/C5.java | 17 + .../proxyutiltest/annotationimpl/C6.java | 19 + .../proxyutiltest/annotationimpl/C7_1.java | 18 + .../proxyutiltest/annotationimpl/C7_2.java | 19 + .../proxyutiltest/annotationimpl/C8.java | 53 ++ .../proxyutiltest/annotationimpl/C9.java | 17 + 17 files changed, 548 insertions(+), 475 deletions(-) delete mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/AnnotationTest.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/ProxyUtilTest.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C10.java rename jetcache-test/src/test/java/com/alicp/jetcache/anno/method/{interfaces => proxyutiltest/annotationimpl}/C11.java (79%) rename jetcache-test/src/test/java/com/alicp/jetcache/anno/method/{interfaces => proxyutiltest/annotationimpl}/C12.java (50%) create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C22.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C3.java rename jetcache-test/src/test/java/com/alicp/jetcache/anno/method/{interfaces => proxyutiltest/annotationimpl}/C4.java (58%) create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C5.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C6.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_1.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C7_2.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C8.java create mode 100644 jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C9.java 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 c901d50e..00000000 --- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/ProxyUtilTest.java +++ /dev/null @@ -1,472 +0,0 @@ -package com.alicp.jetcache.anno.method; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -import com.alicp.jetcache.CacheManager; -import com.alicp.jetcache.anno.*; -import com.alicp.jetcache.anno.method.interfaces.*; -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 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.*; - -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 annotation on class - @Nested - class AnnotationOnClassTest { - - - public class C1 implements I1 { - int count; - - @Cached - public int count() { - return count++; - } - - public int countWithoutCache() { - return count++; - } - } - - @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 class for annotation on interface - @Nested - class AnnotationOnInterfaceTest { - - 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 - 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 { - - public class C3 implements I3_2 { - int count; - - public int count() { - return count++; - } - - public int countWithoutCache() { - return count++; - } - } - - @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()); - } - } - - // Test class for annotation on sub interface - @Nested - class AnnotationOnSubInterfaceTest { - - public class C4 implements I4_2 { - int count; - - public int count() { - return count++; - } - - public int countWithoutCache() { - return count++; - } - } - - @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()); - } - } - - // Test class for annotation with enabled=false - @Nested - class AnnotationWithEnabledFalseTest { - public class C5 implements I5 { - int count; - - @Cached(enabled = false) - public int count() { - return count++; - } - - public int countWithoutCache() { - return count++; - } - } - - @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; - }); - } - } - - // Test class for annotation with enabled=false + EnableCache - @Nested - class AnnotationWithEnabledFalseAndEnableCacheTest { - - public class C6 implements I6 { - int count; - - @EnableCache - @Cached(enabled = false) - public int count() { - return count++; - } - - public int countWithoutCache() { - return count++; - } - } - - @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()); - } - } - - // Test class for annotation with enabled=false + EnableCache (enable in caller) - @Nested - class AnnotationWithEnabledFalseAndEnableCacheInCallerTest { - - public class C7_1 implements I7_1 { - int count; - - @Cached(enabled = false) - public int count() { - return count++; - } - - @Override - public int countWithoutCache() { - return count++; - } - } - - 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 - 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 class for @CacheUpdate and @CacheInvalidate test - @Nested - class CacheUpdateInvalidateTest { - - - - 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 - 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 { - - 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 - 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 { - 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 - 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/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/interfaces/C11.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java similarity index 79% rename from jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C11.java rename to jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java index c7aae4cc..b8eaf42f 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C11.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C11.java @@ -1,4 +1,6 @@ -package com.alicp.jetcache.anno.method.interfaces; +package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl; + +import com.alicp.jetcache.anno.method.interfaces.I11; public class C11 implements I11 { int count; diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java similarity index 50% rename from jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java rename to jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java index 579d7476..0c7c993c 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C12.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C12.java @@ -1,4 +1,6 @@ -package com.alicp.jetcache.anno.method.interfaces; +package com.alicp.jetcache.anno.method.proxyutiltest.annotationimpl; + +import com.alicp.jetcache.anno.method.interfaces.I12; public class C12 implements I12 { int count; 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/interfaces/C4.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java similarity index 58% rename from jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C4.java rename to jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java index f4cb49df..98f806ca 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/interfaces/C4.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/method/proxyutiltest/annotationimpl/C4.java @@ -1,4 +1,6 @@ -package com.alicp.jetcache.anno.method.interfaces; +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; 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++; + } +} From c16d39e759502dfc1136eed71dc1446b28b32e47 Mon Sep 17 00:00:00 2001 From: Tung Ngo Date: Thu, 11 Jul 2024 20:59:29 -0300 Subject: [PATCH 7/7] Updated 1st refactor: complex method in invoke method --- .../alicp/jetcache/anno/method/ClassUtilTest.java | 12 ------------ 1 file changed, 12 deletions(-) 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 e3b8a478..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 @@ -64,18 +64,6 @@ class Foo3Params { private double p8; private boolean p9; - // Constructor and getters/setters for all parameters - public Foo3Params(byte p2, short p3, char p4, int p5, long p6, float p7, double p8, boolean p9) { - this.p2 = p2; - this.p3 = p3; - this.p4 = p4; - this.p5 = p5; - this.p6 = p6; - this.p7 = p7; - this.p8 = p8; - this.p9 = p9; - } - public byte getP2() { return p2; }