This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f7306f  [Dubbo-1687]Enhance test coverage for dubbo filter (#1715)
9f7306f is described below

commit 9f7306f98e725ff6b1a5956813a68842bc9e9089
Author: qinnnyul <yulin.qin....@gmail.com>
AuthorDate: Thu May 3 16:23:08 2018 +0800

    [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 ++++++++++++++++------
 .../cache/support/AbstractCacheFactoryTest.java    | 33 ++++++++++++
 .../cache/support/jcache/JCacheFactoryTest.java    | 54 +++++++++++++++++++
 .../cache/support/lru/LruCacheFactoryTest.java     | 38 ++++++++++++++
 .../threadlocal/ThreadLocalCacheFactoryTest.java   | 38 ++++++++++++++
 pom.xml                                            |  1 +
 7 files changed, 213 insertions(+), 17 deletions(-)

diff --git a/dubbo-filter/dubbo-filter-cache/pom.xml 
b/dubbo-filter/dubbo-filter-cache/pom.xml
index bd0791f..65164ba 100644
--- a/dubbo-filter/dubbo-filter-cache/pom.xml
+++ b/dubbo-filter/dubbo-filter-cache/pom.xml
@@ -39,5 +39,11 @@
             <groupId>javax.cache</groupId>
             <artifactId>cache-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.hazelcast</groupId>
+            <artifactId>hazelcast</artifactId>
+            <scope>test</scope>
+            <version>${hazelcast_version}</version>
+        </dependency>
     </dependencies>
 </project>
\ 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 a9e8d04..0fa6d69 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<Object[]> 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 class CacheFilterTest {
     }
 
     @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 0000000..6f0297f
--- /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 0000000..7a4aea8
--- /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 0000000..5f82882
--- /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 0000000..eac7778
--- /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 2b2d371..b5a0437 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,6 +89,7 @@
     <properties>
         <!-- Test libs -->
         <junit_version>4.12</junit_version>
+        <hazelcast_version>3.9-EA</hazelcast_version>
         <hamcrest_version>1.3</hamcrest_version>
         <cglib_version>2.2</cglib_version>
         <mockito_version>2.18.3</mockito_version>

-- 
To stop receiving notification emails like this one, please contact
i...@apache.org.

Reply via email to