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/
- JUnit test with Memcached
- JUnit integration test with Spring
- Mike Clark的Test-Driven Development with JUnit Workshop
- JUnit Test in Spring Context with JNDI Data Source
- JUnit Test in Spring Context with JNDI Data Source
- junit test
- junit test
- junit-test
- junit @Test
- 【Junit】The import org.junit.Test conflicts with a type defined in the same file报错
- JUnit的报错“No tests found with test runner JUnit4”
- The import org.junit.Test conflicts with a type defined in the same file
- The import org.junit.Test conflicts with a type defined in the same file报错
- Junit 4 test suit
- android junit test问题
- Junit test EJB3
- android junit test问题
- junit test application代码
- Qt技巧【学习】
- js判断为空Null与字符串为空实例
- 使用 SELECT 语句从表中取数据 创建新表 字段属性 向表中添加数据 删除
- ActiveMQ学习(三)——MQ的通讯模式
- error LNK2019: unresolved external symbol _BIO_new referenced in function _main异常
- JUnit test with Memcached
- hdu2824 The Euler function 欧拉函数
- JQuery中对option的添加、删除、取值
- 自旋锁(spin lock) API
- CodeIgniter源码分析之index.php
- hosts 文件编码不对,也不能访问本
- jquery禁用右键、文本选择功能、复制按键的实现
- 什么是超链接?超链接是什么意思?
- ActiveMQ学习(四)——应用程序接口