Skip to content

Commit e42d8e0

Browse files
qinnnyulbeiwei30
authored andcommitted
[Dubbo-1687]Enhance test coverage for dubbo filter (apache#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
1 parent 299f78a commit e42d8e0

File tree

7 files changed

+213
-17
lines changed

7 files changed

+213
-17
lines changed

dubbo-filter/dubbo-filter-cache/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@
3939
<groupId>javax.cache</groupId>
4040
<artifactId>cache-api</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.hazelcast</groupId>
44+
<artifactId>hazelcast</artifactId>
45+
<scope>test</scope>
46+
<version>${hazelcast_version}</version>
47+
</dependency>
4248
</dependencies>
4349
</project>

dubbo-filter/dubbo-filter-cache/src/test/java/com/alibaba/dubbo/cache/filter/CacheFilterTest.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,71 @@
1616
*/
1717
package com.alibaba.dubbo.cache.filter;
1818

19+
import com.alibaba.dubbo.cache.CacheFactory;
20+
import com.alibaba.dubbo.cache.support.jcache.JCacheFactory;
1921
import com.alibaba.dubbo.cache.support.lru.LruCacheFactory;
22+
import com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCacheFactory;
2023
import com.alibaba.dubbo.common.URL;
2124
import com.alibaba.dubbo.rpc.Invoker;
2225
import com.alibaba.dubbo.rpc.RpcInvocation;
2326
import com.alibaba.dubbo.rpc.RpcResult;
24-
2527
import org.junit.Assert;
26-
import org.junit.BeforeClass;
28+
import org.junit.Before;
2729
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.Parameterized;
32+
33+
import java.util.Arrays;
34+
import java.util.List;
2835

36+
import static org.junit.runners.Parameterized.*;
2937
import static org.mockito.BDDMockito.given;
3038
import static org.mockito.Mockito.mock;
3139

40+
@RunWith(Parameterized.class)
3241
public class CacheFilterTest {
33-
private static RpcInvocation invocation;
34-
static CacheFilter cacheFilter = new CacheFilter();
35-
static Invoker<?> invoker = mock(Invoker.class);
36-
static Invoker<?> invoker1 = mock(Invoker.class);
37-
static Invoker<?> invoker2 = mock(Invoker.class);
38-
39-
@BeforeClass
40-
public static void setUp() {
42+
private RpcInvocation invocation;
43+
private CacheFilter cacheFilter = new CacheFilter();
44+
private Invoker<?> invoker = mock(Invoker.class);
45+
private Invoker<?> invoker1 = mock(Invoker.class);
46+
private Invoker<?> invoker2 = mock(Invoker.class);
47+
private String cacheType;
48+
private CacheFactory cacheFactory;
49+
50+
public CacheFilterTest(String cacheType, CacheFactory cacheFactory) {
51+
this.cacheType = cacheType;
52+
this.cacheFactory = cacheFactory;
53+
}
54+
55+
@Parameters
56+
public static List<Object[]> cacheFactories() {
57+
return Arrays.asList(new Object[][]{
58+
{"lru", new LruCacheFactory()},
59+
{"jcache", new JCacheFactory()},
60+
{"threadlocal", new ThreadLocalCacheFactory()}
61+
});
62+
}
63+
64+
@Before
65+
public void setUp() throws Exception {
4166
invocation = new RpcInvocation();
42-
cacheFilter.setCacheFactory(new LruCacheFactory());
67+
cacheFilter.setCacheFactory(this.cacheFactory);
4368

44-
URL url = URL.valueOf("test://test:11/test?cache=lru");
69+
URL url = URL.valueOf("test://test:11/test?cache=" + this.cacheType);
4570

46-
given(invoker.invoke(invocation)).willReturn(new RpcResult(new String("value")));
71+
given(invoker.invoke(invocation)).willReturn(new RpcResult("value"));
4772
given(invoker.getUrl()).willReturn(url);
4873

49-
given(invoker1.invoke(invocation)).willReturn(new RpcResult(new String("value1")));
74+
given(invoker1.invoke(invocation)).willReturn(new RpcResult("value1"));
5075
given(invoker1.getUrl()).willReturn(url);
5176

52-
given(invoker2.invoke(invocation)).willReturn(new RpcResult(new String("value2")));
77+
given(invoker2.invoke(invocation)).willReturn(new RpcResult("value2"));
5378
given(invoker2.getUrl()).willReturn(url);
79+
5480
}
5581

5682
@Test
57-
public void test_No_Arg_Method() {
83+
public void testNonArgsMethod() {
5884
invocation.setMethodName("echo");
5985
invocation.setParameterTypes(new Class<?>[]{});
6086
invocation.setArguments(new Object[]{});
@@ -66,7 +92,7 @@ public void test_No_Arg_Method() {
6692
}
6793

6894
@Test
69-
public void test_Args_Method() {
95+
public void testMethodWithArgs() {
7096
invocation.setMethodName("echo1");
7197
invocation.setParameterTypes(new Class<?>[]{String.class});
7298
invocation.setArguments(new Object[]{"arg1"});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.cache.support;
18+
19+
import com.alibaba.dubbo.cache.Cache;
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.rpc.Invocation;
22+
import com.alibaba.dubbo.rpc.RpcInvocation;
23+
24+
public abstract class AbstractCacheFactoryTest {
25+
26+
protected Cache constructCache() {
27+
URL url = URL.valueOf("test://test:11/test?cache=lru");
28+
Invocation invocation = new RpcInvocation();
29+
return getCacheFactory().getCache(url, invocation);
30+
}
31+
32+
protected abstract AbstractCacheFactory getCacheFactory();
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.cache.support.jcache;
18+
19+
import com.alibaba.dubbo.cache.Cache;
20+
import com.alibaba.dubbo.cache.support.AbstractCacheFactory;
21+
import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest;
22+
import com.alibaba.dubbo.common.URL;
23+
import com.alibaba.dubbo.rpc.Invocation;
24+
import com.alibaba.dubbo.rpc.RpcInvocation;
25+
import org.junit.Test;
26+
27+
import static org.hamcrest.core.Is.is;
28+
import static org.junit.Assert.assertNull;
29+
import static org.junit.Assert.assertThat;
30+
31+
public class JCacheFactoryTest extends AbstractCacheFactoryTest {
32+
33+
@Test
34+
public void testJCacheFactory() throws Exception {
35+
Cache cache = super.constructCache();
36+
assertThat(cache instanceof JCache, is(true));
37+
}
38+
39+
@Test
40+
public void testJCacheGetExpired() throws Exception {
41+
URL url = URL.valueOf("test://test:11/test?cache=jacache&.cache.write.expire=1");
42+
AbstractCacheFactory cacheFactory = getCacheFactory();
43+
Invocation invocation = new RpcInvocation();
44+
Cache cache = cacheFactory.getCache(url, invocation);
45+
cache.put("testKey", "testValue");
46+
Thread.sleep(10);
47+
assertNull(cache.get("testKey"));
48+
}
49+
50+
@Override
51+
protected AbstractCacheFactory getCacheFactory() {
52+
return new JCacheFactory();
53+
}
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.cache.support.lru;
18+
19+
import com.alibaba.dubbo.cache.Cache;
20+
import com.alibaba.dubbo.cache.support.AbstractCacheFactory;
21+
import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest;
22+
import org.junit.Test;
23+
24+
import static org.hamcrest.core.Is.is;
25+
import static org.junit.Assert.assertThat;
26+
27+
public class LruCacheFactoryTest extends AbstractCacheFactoryTest{
28+
@Test
29+
public void testLruCacheFactory() throws Exception {
30+
Cache cache = super.constructCache();
31+
assertThat(cache instanceof LruCache, is(true));
32+
}
33+
34+
@Override
35+
protected AbstractCacheFactory getCacheFactory() {
36+
return new LruCacheFactory();
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.cache.support.threadlocal;
18+
19+
import com.alibaba.dubbo.cache.Cache;
20+
import com.alibaba.dubbo.cache.support.AbstractCacheFactory;
21+
import com.alibaba.dubbo.cache.support.AbstractCacheFactoryTest;
22+
import org.junit.Test;
23+
24+
import static org.hamcrest.core.Is.is;
25+
import static org.junit.Assert.assertThat;
26+
27+
public class ThreadLocalCacheFactoryTest extends AbstractCacheFactoryTest {
28+
@Test
29+
public void testThreadLocalCacheFactory() throws Exception {
30+
Cache cache = super.constructCache();
31+
assertThat(cache instanceof ThreadLocalCache, is(true));
32+
}
33+
34+
@Override
35+
protected AbstractCacheFactory getCacheFactory() {
36+
return new ThreadLocalCacheFactory();
37+
}
38+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<properties>
9090
<!-- Test libs -->
9191
<junit_version>4.12</junit_version>
92+
<hazelcast_version>3.9-EA</hazelcast_version>
9293
<hamcrest_version>1.3</hamcrest_version>
9394
<cglib_version>2.2</cglib_version>
9495
<mockito_version>2.18.3</mockito_version>

0 commit comments

Comments
 (0)