This commit is contained in:
Looly 2025-10-23 11:16:50 +08:00
parent c9c4cf5ae3
commit 47acd87e58

View File

@ -22,6 +22,11 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class SimpleCacheTest {
@BeforeEach
@ -82,4 +87,36 @@ public class SimpleCacheTest {
Assertions.assertTrue(tester.getInterval() > 0);
Assertions.assertEquals("aaaValue", cache.get("aaa"));
}
@Test
void removeTest(){
final SimpleCache<String, String> cache = new SimpleCache<>();
cache.put("key1", "value1");
cache.get("key1");
cache.put("key2", "value2");
cache.get("key2");
cache.put("key3", "value3");
cache.get("key3");
cache.put("key4", "value4");
cache.get("key4");
cache.get("key5", ()->"value5");
String key = null;
for (Map.Entry<String, String> entry : cache) {
if ("value3".equals(entry.getValue())) {
key = entry.getKey();
break;
}
}
if(null != key){
cache.remove(key);
}
assertEquals("value1", cache.get("key1"));
assertEquals("value2", cache.get("key2"));
assertEquals("value4", cache.get("key4"));
assertEquals("value5", cache.get("key5"));
assertNull(cache.get("key3"));
}
}