From e42d8e0d2dac515043be139f114832d80f57e1d0 Mon Sep 17 00:00:00 2001 From: qinnnyul Date: Thu, 3 May 2018 16:23:08 +0800 Subject: [PATCH] [Dubbo-1687]Enhance test coverage for dubbo filter (#1715) * use three different kinds of cache factory to increase test coverages * add unit test cases for dubbo-filter module * add copyright and made small refactor * make sure Jcache will exceed expired period --- dubbo-filter/dubbo-filter-cache/pom.xml | 6 ++ .../dubbo/cache/filter/CacheFilterTest.java | 60 +++++++++++++------ .../support/AbstractCacheFactoryTest.java | 33 ++++++++++ .../support/jcache/JCacheFactoryTest.java | 54 +++++++++++++++++ .../support/lru/LruCacheFactoryTest.java | 38 ++++++++++++ .../ThreadLocalCacheFactoryTest.java | 38 ++++++++++++ pom.xml | 1 + 7 files changed, 213 insertions(+), 17 deletions(-) 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 bd0791f325d..65164ba022f 100644 --- a/dubbo-filter/dubbo-filter-cache/pom.xml +++ b/dubbo-filter/dubbo-filter-cache/pom.xml @@ -39,5 +39,11 @@ javax.cache cache-api + + com.hazelcast + hazelcast + test + ${hazelcast_version} + \ 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"}); 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..6f0297fe8df --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/AbstractCacheFactoryTest.java @@ -0,0 +1,33 @@ +/* + * 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; +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=lru"); + 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..7a4aea84f8f --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/jcache/JCacheFactoryTest.java @@ -0,0 +1,54 @@ +/* + * 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 { + + @Test + public void testJCacheFactory() throws Exception { + Cache cache = super.constructCache(); + 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"); + Thread.sleep(10); + assertNull(cache.get("testKey")); + } + + @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..5f828820b05 --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/lru/LruCacheFactoryTest.java @@ -0,0 +1,38 @@ +/* + * 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; +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..eac77781e34 --- /dev/null +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java @@ -0,0 +1,38 @@ +/* + * 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; +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 diff --git a/pom.xml b/pom.xml index 2b2d37183f5..b5a04379f08 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ 4.12 + 3.9-EA 1.3 2.2 2.18.3