From 023ba8d6b8d3e115fd59a7122749c4677404c45a Mon Sep 17 00:00:00 2001 From: Yulin Qin Date: Fri, 27 Apr 2018 21:00:00 +0800 Subject: [PATCH 1/4] use three different kinds of cache factory to increase test coverages --- dubbo-filter/dubbo-filter-cache/pom.xml | 5 ++ .../dubbo/cache/filter/CacheFilterTest.java | 60 +++++++++++++------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/dubbo-filter/dubbo-filter-cache/pom.xml b/dubbo-filter/dubbo-filter-cache/pom.xml index bd0791f325d..4eacb50e606 100644 --- a/dubbo-filter/dubbo-filter-cache/pom.xml +++ b/dubbo-filter/dubbo-filter-cache/pom.xml @@ -39,5 +39,10 @@ javax.cache cache-api + + com.hazelcast + hazelcast + 3.9-EA + \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/filter/CacheFilterTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/filter/CacheFilterTest.java index a9e8d041cb3..0fa6d698c07 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/filter/CacheFilterTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/filter/CacheFilterTest.java @@ -16,45 +16,71 @@ */ package com.alibaba.dubbo.cache.filter; +import com.alibaba.dubbo.cache.CacheFactory; +import com.alibaba.dubbo.cache.support.jcache.JCacheFactory; import com.alibaba.dubbo.cache.support.lru.LruCacheFactory; +import com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCacheFactory; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.rpc.Invoker; import com.alibaba.dubbo.rpc.RpcInvocation; import com.alibaba.dubbo.rpc.RpcResult; - import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.List; +import static org.junit.runners.Parameterized.*; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +@RunWith(Parameterized.class) public class CacheFilterTest { - private static RpcInvocation invocation; - static CacheFilter cacheFilter = new CacheFilter(); - static Invoker invoker = mock(Invoker.class); - static Invoker invoker1 = mock(Invoker.class); - static Invoker invoker2 = mock(Invoker.class); - - @BeforeClass - public static void setUp() { + private RpcInvocation invocation; + private CacheFilter cacheFilter = new CacheFilter(); + private Invoker invoker = mock(Invoker.class); + private Invoker invoker1 = mock(Invoker.class); + private Invoker invoker2 = mock(Invoker.class); + private String cacheType; + private CacheFactory cacheFactory; + + public CacheFilterTest(String cacheType, CacheFactory cacheFactory) { + this.cacheType = cacheType; + this.cacheFactory = cacheFactory; + } + + @Parameters + public static List cacheFactories() { + return Arrays.asList(new Object[][]{ + {"lru", new LruCacheFactory()}, + {"jcache", new JCacheFactory()}, + {"threadlocal", new ThreadLocalCacheFactory()} + }); + } + + @Before + public void setUp() throws Exception { invocation = new RpcInvocation(); - cacheFilter.setCacheFactory(new LruCacheFactory()); + cacheFilter.setCacheFactory(this.cacheFactory); - URL url = URL.valueOf("test://test:11/test?cache=lru"); + URL url = URL.valueOf("test://test:11/test?cache=" + this.cacheType); - given(invoker.invoke(invocation)).willReturn(new RpcResult(new String("value"))); + given(invoker.invoke(invocation)).willReturn(new RpcResult("value")); given(invoker.getUrl()).willReturn(url); - given(invoker1.invoke(invocation)).willReturn(new RpcResult(new String("value1"))); + given(invoker1.invoke(invocation)).willReturn(new RpcResult("value1")); given(invoker1.getUrl()).willReturn(url); - given(invoker2.invoke(invocation)).willReturn(new RpcResult(new String("value2"))); + given(invoker2.invoke(invocation)).willReturn(new RpcResult("value2")); given(invoker2.getUrl()).willReturn(url); + } @Test - public void test_No_Arg_Method() { + public void testNonArgsMethod() { invocation.setMethodName("echo"); invocation.setParameterTypes(new Class[]{}); invocation.setArguments(new Object[]{}); @@ -66,7 +92,7 @@ public void test_No_Arg_Method() { } @Test - public void test_Args_Method() { + public void testMethodWithArgs() { invocation.setMethodName("echo1"); invocation.setParameterTypes(new Class[]{String.class}); invocation.setArguments(new Object[]{"arg1"}); From 11f80c114a9bcb5c2c6f50cfe4ecc38edd531b14 Mon Sep 17 00:00:00 2001 From: Yulin Qin Date: Sat, 28 Apr 2018 07:29:53 +0800 Subject: [PATCH 2/4] add unit test cases for dubbo-filter module --- dubbo-filter/dubbo-filter-cache/pom.xml | 3 ++- .../support/AbstractCacheFactoryTest.java | 17 ++++++++++++++ .../support/jcache/JCacheFactoryTest.java | 23 +++++++++++++++++++ .../support/lru/LruCacheFactoryTest.java | 22 ++++++++++++++++++ .../ThreadLocalCacheFactoryTest.java | 22 ++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java create mode 100644 dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java create mode 100644 dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java create mode 100644 dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java diff --git a/dubbo-filter/dubbo-filter-cache/pom.xml b/dubbo-filter/dubbo-filter-cache/pom.xml index 4eacb50e606..d2b2be3b947 100644 --- a/dubbo-filter/dubbo-filter-cache/pom.xml +++ b/dubbo-filter/dubbo-filter-cache/pom.xml @@ -28,6 +28,7 @@ The cache module of dubbo project false + 3.9-EA @@ -42,7 +43,7 @@ com.hazelcast hazelcast - 3.9-EA + ${hazelcast_version} \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java new file mode 100644 index 00000000000..3a40bb5ed9c --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java @@ -0,0 +1,17 @@ +package com.alibaba.dubbo.cache.support; + +import com.alibaba.dubbo.cache.Cache; +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.Invocation; +import com.alibaba.dubbo.rpc.RpcInvocation; + +public abstract class AbstractCacheFactoryTest { + + protected Cache constructCache() { + URL url = URL.valueOf("test://test:11/test?cache=jcache"); + Invocation invocation = new RpcInvocation(); + return getCacheFactory().getCache(url, invocation); + } + + protected abstract AbstractCacheFactory getCacheFactory(); +} \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java new file mode 100644 index 00000000000..13f965e7989 --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java @@ -0,0 +1,23 @@ +package com.alibaba.dubbo.cache.support.jcache; + +import com.alibaba.dubbo.cache.Cache; +import com.alibaba.dubbo.cache.support.AbstractCacheFactory; +import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class JCacheFactoryTest extends AbstractCacheFactoryTest { + + @Test + public void testJCacheFactory() throws Exception { + Cache cache = super.constructCache(); + assertThat(cache instanceof JCache, is(true)); + } + + @Override + protected AbstractCacheFactory getCacheFactory() { + return new JCacheFactory(); + } +} \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java new file mode 100644 index 00000000000..68c114c4813 --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java @@ -0,0 +1,22 @@ +package com.alibaba.dubbo.cache.support.lru; + +import com.alibaba.dubbo.cache.Cache; +import com.alibaba.dubbo.cache.support.AbstractCacheFactory; +import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class LruCacheFactoryTest extends AbstractCacheFactoryTest{ + @Test + public void testLruCacheFactory() throws Exception { + Cache cache = super.constructCache(); + assertThat(cache instanceof LruCache, is(true)); + } + + @Override + protected AbstractCacheFactory getCacheFactory() { + return new LruCacheFactory(); + } +} \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java new file mode 100644 index 00000000000..b9b32dcbc9e --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java @@ -0,0 +1,22 @@ +package com.alibaba.dubbo.cache.support.threadlocal; + +import com.alibaba.dubbo.cache.Cache; +import com.alibaba.dubbo.cache.support.AbstractCacheFactory; +import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ThreadLocalCacheFactoryTest extends AbstractCacheFactoryTest { + @Test + public void testThreadLocalCacheFactory() throws Exception { + Cache cache = super.constructCache(); + assertThat(cache instanceof ThreadLocalCache, is(true)); + } + + @Override + protected AbstractCacheFactory getCacheFactory() { + return new ThreadLocalCacheFactory(); + } +} \ No newline at end of file From 7692c2910c7f5ef4660ecda01f26dfaaa6a4a8d4 Mon Sep 17 00:00:00 2001 From: Yulin Qin Date: Sat, 28 Apr 2018 07:50:00 +0800 Subject: [PATCH 3/4] add copyright and made small refactor --- dubbo-filter/dubbo-filter-cache/pom.xml | 2 +- .../support/AbstractCacheFactoryTest.java | 18 ++++++++++- .../support/jcache/JCacheFactoryTest.java | 30 +++++++++++++++++++ .../support/lru/LruCacheFactoryTest.java | 16 ++++++++++ .../ThreadLocalCacheFactoryTest.java | 16 ++++++++++ pom.xml | 1 + 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/dubbo-filter/dubbo-filter-cache/pom.xml b/dubbo-filter/dubbo-filter-cache/pom.xml index d2b2be3b947..65164ba022f 100644 --- a/dubbo-filter/dubbo-filter-cache/pom.xml +++ b/dubbo-filter/dubbo-filter-cache/pom.xml @@ -28,7 +28,6 @@ The cache module of dubbo project false - 3.9-EA @@ -43,6 +42,7 @@ com.hazelcast hazelcast + test ${hazelcast_version} diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java index 3a40bb5ed9c..6f0297fe8df 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.dubbo.cache.support; import com.alibaba.dubbo.cache.Cache; @@ -8,7 +24,7 @@ public abstract class AbstractCacheFactoryTest { protected Cache constructCache() { - URL url = URL.valueOf("test://test:11/test?cache=jcache"); + URL url = URL.valueOf("test://test:11/test?cache=lru"); Invocation invocation = new RpcInvocation(); return getCacheFactory().getCache(url, invocation); } diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java index 13f965e7989..d5c3a743054 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java @@ -1,11 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.dubbo.cache.support.jcache; import com.alibaba.dubbo.cache.Cache; import com.alibaba.dubbo.cache.support.AbstractCacheFactory; import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest; +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.Invocation; +import com.alibaba.dubbo.rpc.RpcInvocation; import org.junit.Test; import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; public class JCacheFactoryTest extends AbstractCacheFactoryTest { @@ -16,6 +36,16 @@ public void testJCacheFactory() throws Exception { assertThat(cache instanceof JCache, is(true)); } + @Test + public void testJCacheGetExpired() throws Exception { + URL url = URL.valueOf("test://test:11/test?cache=jacache&.cache.write.expire=1"); + AbstractCacheFactory cacheFactory = getCacheFactory(); + Invocation invocation = new RpcInvocation(); + Cache cache = cacheFactory.getCache(url, invocation); + cache.put("testKey", "testValue"); + assertNull(cache.get("testKey")); + } + @Override protected AbstractCacheFactory getCacheFactory() { return new JCacheFactory(); diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java index 68c114c4813..5f828820b05 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.dubbo.cache.support.lru; import com.alibaba.dubbo.cache.Cache; diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java index b9b32dcbc9e..eac77781e34 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.dubbo.cache.support.threadlocal; import com.alibaba.dubbo.cache.Cache; diff --git a/pom.xml b/pom.xml index e3f024281ea..2a164e41fc8 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ 4.12 + 3.9-EA 2.2 2.18.3 From 12249b7aa428260ac7dc7b6cac9afaf333123e4f Mon Sep 17 00:00:00 2001 From: Yulin Qin Date: Sat, 28 Apr 2018 10:13:01 +0800 Subject: [PATCH 4/4] make sure Jcache will exceed expired period --- .../alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java index d5c3a743054..7a4aea84f8f 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java @@ -43,6 +43,7 @@ public void testJCacheGetExpired() throws Exception { Invocation invocation = new RpcInvocation(); Cache cache = cacheFactory.getCache(url, invocation); cache.put("testKey", "testValue"); + Thread.sleep(10); assertNull(cache.get("testKey")); }