modify cache api to match the one from shiro

This commit is contained in:
Sebastian Sdorra
2014-02-19 19:59:44 +01:00
parent ffc12f2fa1
commit 5715851ff1
6 changed files with 197 additions and 55 deletions

View File

@@ -30,17 +30,21 @@
*/
package sonia.scm.cache;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Predicate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sonia.scm.util.IOUtil;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
@@ -107,6 +111,21 @@ public abstract class CacheTestBase
assertFalse(cache.contains("test-2"));
}
/**
* Method description
*
*/
@Test
public void testOverride()
{
cache.put("test", "test123");
String previous = cache.put("test", "test456");
assertEquals("test123", previous);
assertEquals("test456", cache.get("test"));
}
/**
* Method description
*
@@ -127,7 +146,10 @@ public abstract class CacheTestBase
{
cache.put("test", "test123");
assertEquals("test123", cache.get("test"));
cache.remove("test");
String previous = cache.remove("test");
assertEquals("test123", previous);
assertNull(cache.get("test"));
}
@@ -139,10 +161,11 @@ public abstract class CacheTestBase
public void testRemoveAll()
{
cache.put("test-1", "test123");
cache.put("test-2", "test123");
cache.put("test-2", "test456");
cache.put("a-1", "test123");
cache.put("a-2", "test123");
cache.removeAll(new Predicate<String>()
Iterable<String> previous = cache.removeAll(new Predicate<String>()
{
@Override
public boolean apply(String item)
@@ -150,12 +173,32 @@ public abstract class CacheTestBase
return item.startsWith("test");
}
});
assertThat(previous, containsInAnyOrder("test123", "test456"));
assertNull(cache.get("test-1"));
assertNull(cache.get("test-2"));
assertNotNull(cache.get("a-1"));
assertNotNull(cache.get("a-2"));
}
/**
* Method description
*
*/
@Test
public void testSize()
{
assertEquals(0, cache.size());
cache.put("test", "test123");
assertEquals(1, cache.size());
cache.put("test-1", "test123");
assertEquals(2, cache.size());
cache.remove("test");
assertEquals(1, cache.size());
cache.clear();
assertEquals(0, cache.size());
}
//~--- fields ---------------------------------------------------------------
/** Field description */