JUnit test with Memcached

如果想写 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/


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值