package info.noconfuse.modules.cache;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:context-cache.xml"})
public class MemcachedCacheTest {

    @Autowired
    private MemcachedClientBuilder clientBuilder;

    private MemcachedClient memcachedClient;
    private MemcachedCache memcachedCache;
    private String key;

    @Before
    public void setUp() throws IOException {
        memcachedClient = clientBuilder.build();
        memcachedCache = new MemcachedCache("test", memcachedClient);
        key = Long.toString(System.currentTimeMillis());
    }

    @After
    public void tearDown() throws Exception {
        memcachedClient.flushAll();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCreateCacheWithoutName() {
        new MemcachedCache(null, memcachedClient);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCreateCacheWithoutClient() {
        new MemcachedCache("hello", null);
    }

    @Test
    public void testPut() throws Exception {
        memcachedCache.put(key, this.getClass().getName());
        String target = memcachedClient.get("test_" + key);
        Assert.assertEquals(MemcachedCacheTest.class.getName(), target);
    }

    @Test
    public void testPutIfAbsent() throws Exception {
        Cache.ValueWrapper vw = memcachedCache.putIfAbsent(key, this.getClass().getName());
        Assert.assertNull(vw);

        vw = memcachedCache.putIfAbsent(key, this.getClass().getName() + "_new");
        Assert.assertEquals(MemcachedCacheTest.class.getName(), vw.get());
        String target = memcachedClient.get("test_" + key);
        Assert.assertNotEquals(MemcachedCacheTest.class.getName() + "_new", target);

        memcachedCache.putIfAbsent(key + "_new", this.getClass().getName() + "_new");
        target = memcachedClient.get("test_" + key + "_new");
        Assert.assertEquals(MemcachedCacheTest.class.getName() + "_new", target);
    }

    @Test
    public void testGet() throws Exception {
        memcachedClient.set("test_" + key, 0, this.getClass().getName());
        Cache.ValueWrapper vw = memcachedCache.get(key);
        Assert.assertEquals(MemcachedCacheTest.class.getName(), vw.get());
    }

    @Test
    public void testGetWithType() throws Exception {
        memcachedClient.set("test_" + key, 0, this.getClass().getName());
        String value = memcachedCache.get(key, String.class);
        Assert.assertEquals(MemcachedCacheTest.class.getName(), value);
    }

    @Test
    public void testEvict() throws Exception {
        memcachedClient.set("test_" + key, 0, this.getClass().getName());
        memcachedCache.evict(key);
        String target = memcachedClient.get("test_" + key);
        Assert.assertNull(target);
    }

    @Test
    public void testClear() throws Exception {
        memcachedClient.set("test_" + key, 0, this.getClass().getName());
        memcachedClient.set("test_" + key + "_new", 0, this.getClass().getName());
        memcachedCache.clear();
        String target = memcachedClient.get("test_" + key);
        Assert.assertNull(target);
        target = memcachedClient.get("test_" + key + "_new");
        Assert.assertNull(target);
    }
}

@@ -3740,11 +3740,415 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testExpire() throws Exception %7B%0A        MemcachedCache cache = new MemcachedCache(%22short-lived%22, memcachedClient, 2);%0A        cache.put(%22die%22, %22young%22);%0A        String val = memcachedClient.get(%22short-lived_die%22);%0A        Assert.assertEquals(%22young%22, val);%0A        Thread.sleep(2000);%0A        val = memcachedClient.get(%22short-lived_die%22);%0A        Assert.assertNull(val);%0A    %7D
 %0A%7D%0A

Test cached data expired