Redis 一款服务器数据结构服务器(多用于内存数据库) Junit 测试 Code Speech

package com.redis.test;

import java.util.Set;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;

/**
 * @author zhaoming23@gmail.com
 *         2012-5-17 下午04:02:09
 */
public class JedisTest {

    /** 
     * 该安全认证密码通过 redis 服务器端 conf 中 requirepass 来指定
     * 配置文件中的描述如下
     * since redis is pretty fast an outside user can try up to
     * 150k passwords per second against a good box. This means
     * that you should use a very strong password otherwise it
     * will be very easy to break
     * 不太安全的,可以爆破 。当然这是在外网情况下。
     * 内网在服务前做上白名单
     * */
    public static final String REDIS_REQUIRE_PASS = "12345678";
    
    /** 如果服务器是多网卡的情保定到固定ip上 服务端配置项 bind */
    public static final String REDIS_HOST = "127.0.0.1";
    
    /** redis 2.4 的默认端口是 6379 如果需要修改请关注port配置项 */
    public static final int REDIS_PORT = 7766;
    
    private Jedis jedis;
    
    @Before
    public void auth() {
        jedis = new Jedis(REDIS_HOST, REDIS_PORT);
        jedis.auth(REDIS_REQUIRE_PASS);
    }
    
    @Test
    public void test_jedis_set_alias() {
        //TODO 别名
    }
    
    @Test
    @Ignore
    public void test_jedis_shutdown() {
        //关闭服务 一次性操作 生产环境小心使用
        jedis.shutdown();
    }
    
    @Test
    public void test_jedis_setbit_1() {
        String ret = jedis.set("123", "value123");
        Assert.assertEquals("OK", ret);
        boolean setbit = jedis.setbit("123", 0, false);
        System.out.println(setbit);
    }
    
    @Test(timeout = 2000)
    public void test_jedis_setex_persist() throws InterruptedException {
        String test_setex_key = "timeOutData";
        String test_setex_value = "timeOutDataValue";
        jedis.setex(test_setex_key, 1, test_setex_value);
        String ret = jedis.get(test_setex_key);
        Assert.assertEquals(test_setex_value, ret);
        //在数据超时之前设置转持久类型
        jedis.persist(test_setex_key);
        Thread.sleep(1500);
        ret = jedis.get(test_setex_key);
        Assert.assertEquals(test_setex_value, ret);
        //再转回自超时数据
        jedis.setex(test_setex_key, 1, test_setex_value);
        ret = jedis.get(test_setex_key);
        Assert.assertEquals(test_setex_value, ret);
        //超时之前设置不转持久
        Thread.sleep(2000);
        ret = jedis.get(test_setex_key);
        Assert.assertEquals(null, ret);
    }
    
    /** 
     * 7.数量查询 异常问题
     * 7.scard 
     * */
    @Test(expected = JedisDataException.class)
    public void test_jedis_scard_for_exception() {
        String test_for_exception_key = "strKey";
        jedis.set(test_for_exception_key, "strKeyValue");
        //这里会抛redis data exception
        //原因你放进去的是string,但是用set的 方式去访问数量。
        //个人感觉 jedis的api很难用,语意不好理解,编程时需注意。
        jedis.scard(test_for_exception_key);
    }
    
    /** 
     * 7.数量查询
     * 7.scard 
     * */
    @Test
    public void test_jedis_scard() {
        String test_key = "online";
        //不是什么卡片 是单词 cardinality 数量的意思,
        //也就是查询 某个key下的元素数量
        //不要错误访问了set的内容否则会抛错
        Long ret = jedis.scard("RandomStringForNil");
        Assert.assertEquals(0, ret.intValue());
        ret = jedis.scard(test_key);
        Assert.assertEquals(3, ret.intValue());
    }
    
    /**
     * 6.自删除数据 
     * 6.setex 
     * */
    @Test(timeout = 2000)
    public void test_jedis_cli_setex() throws InterruptedException {
        //自动超时元素
        String test_timeout_key = "timeOutData";
        String test_timeout_value = "timeOutDataValue";
        //1秒超时
        jedis.setex(test_timeout_key, 1, test_timeout_value);
        String ret = jedis.get(test_timeout_key);
        Assert.assertEquals("自超时数据失败!", test_timeout_value, ret);
        //等待
        Thread.sleep(1500);
        ret = jedis.get(test_timeout_key);
        Assert.assertEquals(null, ret);
    }
    
    /**
     * 5.集合操作 
     * 5.sadd/srem/smembers 
     * */
    @Test
    public void test_jedis_cli_sadd() {
        //sadd 理解成 set add 不重复元素
        String test_key = "online";
        String delete_will_1 = "111";
        String delete_will_2 = "222";
        jedis.sadd(test_key, delete_will_1);
        jedis.sadd(test_key, delete_will_2);
        jedis.sadd(test_key, "my");
        jedis.sadd(test_key, "my");
        jedis.sadd(test_key, "Jackson");
        jedis.sadd(test_key, "Linus");
        //删除222
        Long srem = jedis.srem(test_key, delete_will_2);
        Assert.assertEquals(1, srem.intValue());
        //删除111
        jedis.srem(test_key, delete_will_1);
        Assert.assertEquals(1, srem.intValue());
        //查询
        Set<String> smembers = jedis.smembers(test_key);
        Assert.assertEquals("[Linus, my, Jackson]", smembers.toString());
    }
    
    /**
     * 4.字节数组操作 
     * 4.set(byte[] etc...)/get(byte[] etc...) 
     * */
    @Test
    public void test_jedis_cli_set_bytes() {
        String test_key = "helloKey";
        String test_value = "helloValue";
        jedis.set(test_key.getBytes(), test_value.getBytes());
        byte[] bs = jedis.get(test_key.getBytes());
        //从调用的角度来看和set String key 没什么区别
        //仅仅是支持bytes的key的api重载罢了
        //但是服务端的实现需要参考相关代码
        Assert.assertEquals(test_value, new String(bs));
        String ret = jedis.get(test_key);
        //用非bytes的方式也可以访问到
        Assert.assertEquals(test_value, ret);
    }
    
    /**
     * 3.访问控制 
     * 3.auth 
     * */
    @Test
    public void test_jedis_cli_auth() {
        if(null == jedis)
            jedis = new Jedis(REDIS_HOST, REDIS_PORT);
        //密码设置 服务器端设置了 requirepass 值后使用,
        //当调用命令之前请先auth验证权限
        String auth = jedis.auth(REDIS_REQUIRE_PASS);
        Assert.assertEquals("OK", auth);
    }
    
    /** 
     * 2.字符累积
     * 2.append 
     * */
    @Test
    public void test_jedis_cli_append_string() {
        String test_key = "appendKey";
        String test_value = "appendValue";
        String test_context = "-AppendContext";
        String ret = jedis.set(test_key, "");
        Assert.assertEquals("OK", ret);
        ret = jedis.set(test_key, test_value);
        Assert.assertEquals("OK", ret);
        //很容易理解了 append 就是追加内容到 key中返回值是长度
        Long append = jedis.append(test_key, test_context);
        String returnValue = jedis.get(test_key);
        Assert.assertEquals(test_value + test_context, returnValue);
        Assert.assertEquals(
                test_value.length() + test_context.length(),
                append.longValue());
    }
    
    /** 
     * 字符key/value
     * 1.set/get 
     * */
    @Test
    public void test_jedis_set_string_key() {
        String test_key = "demo";
        String test_value = "demoValue";
        //保存
        String result = jedis.set(test_key, test_value);
        //保存结果
        Assert.assertEquals("OK", result);
        //再次获取
        String returnValue = jedis.get(test_key);
        //获取结果
        Assert.assertEquals(test_value, returnValue);
    }
    
    @Test
    public void test() {
        Assert.assertEquals(1, 1);
    }
    

}
posted @ 2012-05-18 17:09  迷离雅琪  阅读(1459)  评论(0编辑  收藏  举报