JUnit test with Memcached

来源:互联网 发布:江西电子软件学校 编辑:程序博客网 时间:2024/05/16 13:15

如果想写 Memcached 的单元测试,但又不依赖于现有的 Memcached 服务器,可以使用 jmemcached-core 框架。

1、在项目中添加 jmemcached-core 的依赖

<dependency><groupId>com.thimbleware.jmemcached</groupId><artifactId>jmemcached-core</artifactId><version>1.0.0</version><scope>test</scope></dependency>

 

2、编写 Memcached 客户端

package com.example.common;import java.io.IOException;import java.util.concurrent.TimeoutException;import net.rubyeye.xmemcached.MemcachedClient;import net.rubyeye.xmemcached.MemcachedClientBuilder;import net.rubyeye.xmemcached.XMemcachedClientBuilder;import net.rubyeye.xmemcached.exception.MemcachedException;import net.rubyeye.xmemcached.utils.AddrUtil;public class XMemcacheClient {    protected static MemcachedClient connectMem() throws IOException {        String memURL = SysProperties.MEMCACHED_SERVER + ":" + SysProperties.MEMCACHED_PORT;        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(memURL));        MemcachedClient memcachedClient = builder.build();        return memcachedClient;    }    protected static void close(MemcachedClient memcachedClient) throws IOException {        if (memcachedClient != null) {            memcachedClient.shutdown();        }    }    public static boolean set(String key, int exp, Object o) throws IOException, TimeoutException, InterruptedException, MemcachedException {        MemcachedClient memcachedClient = null;        try {            memcachedClient = connectMem();            return memcachedClient.set(key, exp, o);        } finally {            close(memcachedClient);        }    }    public static Object get(String key) throws IOException, TimeoutException, InterruptedException, MemcachedException {        MemcachedClient memcachedClient = null;        try {            memcachedClient = connectMem();            return memcachedClient.get(key);        } finally {            close(memcachedClient);        }    }    public static boolean delete(String key) throws IOException, TimeoutException, InterruptedException, MemcachedException {        MemcachedClient memcachedClient = null;        try {            memcachedClient = connectMem();            return memcachedClient.delete(key);        } finally {            close(memcachedClient);        }    }    }


 

3、JUnit 测试类

package com.example.common;import java.io.IOException;import java.util.concurrent.TimeoutException;import net.rubyeye.xmemcached.MemcachedClient;import net.rubyeye.xmemcached.exception.MemcachedException;import net.rubyeye.xmemcached.utils.AddrUtil;import org.easymock.EasyMockSupport;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import com.thimbleware.jmemcached.CacheImpl;import com.thimbleware.jmemcached.Key;import com.thimbleware.jmemcached.LocalCacheElement;import com.thimbleware.jmemcached.MemCacheDaemon;import com.thimbleware.jmemcached.storage.CacheStorage;import com.thimbleware.jmemcached.storage.hash.ConcurrentLinkedHashMap;public class XMemcacheClientTest extends EasyMockSupport {    final MemCacheDaemon<LocalCacheElement> daemon = new MemCacheDaemon<LocalCacheElement>();    @Before    public void init() {        CacheStorage<Key, LocalCacheElement> storage = ConcurrentLinkedHashMap.create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO, 100, 20480);        daemon.setCache(new CacheImpl(storage));        daemon.setBinary(false);        daemon.setAddr(AddrUtil.getAddresses(SysProperties.MEMCACHED_SERVER + ":" + SysProperties.MEMCACHED_PORT).get(0));        daemon.setIdleTime(200);        daemon.setVerbose(true);        daemon.start();    }    @Test    public void test() throws IOException, TimeoutException, InterruptedException, MemcachedException {        // set        Assert.assertTrue(XMemcacheClient.set("hello", 5, "hello world"));        // get        Assert.assertTrue("hello world".equals(XMemcacheClient.get("hello")));        // delete        Assert.assertTrue(XMemcacheClient.delete("hello"));    }    @Test    public void testClose() throws IOException, TimeoutException, InterruptedException, MemcachedException {        MemcachedClient memcachedClient = null;        // memcachedClient is null        XMemcacheClient.close(memcachedClient);        // memcachedClient not null        memcachedClient = XMemcacheClient.connectMem();        XMemcacheClient.close(memcachedClient);    }    @After    public void end() {        daemon.stop();    }}


 

jmemecache的用法说明:http://code.google.com/p/jmemcache-daemon/


 

原创粉丝点击