Skip to content

Commit

Permalink
[Dubbo-1687]Enhance test coverage for dubbo filter (#1715)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
qinnnyul authored and beiwei30 committed May 3, 2018
1 parent 903db35 commit 9f7306f
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 17 deletions.
6 changes: 6 additions & 0 deletions dubbo-filter/dubbo-filter-cache/pom.xml
Expand Up @@ -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>
Expand Up @@ -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[]{});
Expand All @@ -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"});
Expand Down
@@ -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();
}
@@ -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();
}
}
@@ -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();
}
}
@@ -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();
}
}
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down

0 comments on commit 9f7306f

Please sign in to comment.