diff options
author | jonmv <venstad@gmail.com> | 2022-10-26 11:25:14 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-10-26 11:25:14 +0200 |
commit | 73ca78767d08828859f112bc6f23997c113617cf (patch) | |
tree | ea6c69808e46b7db17ecdb71bdd13ae46e466e0f /vespajlib | |
parent | e97359f2fc892f72fae912ee3535b1cee50ae8e1 (diff) |
Add unit test for method cache
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java | 71 | ||||
-rw-r--r-- | vespajlib/src/test/resources/dummy | 1 |
2 files changed, 72 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java new file mode 100644 index 00000000000..7b3d3e98cec --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java @@ -0,0 +1,71 @@ +package com.yahoo.collections; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MethodCacheTest { + + @Test + void testCache() throws Exception { + + URL url = MethodCacheTest.class.getClassLoader().getResource("dummy").toURI().resolve(".").toURL(); + + class MyLoader extends URLClassLoader { + MyLoader() { super(new URL[] { url }, MethodCacheTest.class.getClassLoader()); } + public Class<?> loadClass(String name) throws ClassNotFoundException { + if (name.equals(Dummy.class.getName())) synchronized (getClassLoadingLock(name)) { return findClass(name); } + else return super.loadClass(name); + } + } + + try (MyLoader myLoader = new MyLoader()) { + Class<?> applicationClass = Dummy.class; + Class<?> customClass = myLoader.loadClass(Dummy.class.getName()); + + assertNotSame(applicationClass, customClass); + assertSame(applicationClass.getName(), customClass.getName()); + + MethodCache methods = new MethodCache("clone"); + AtomicBoolean updatedCache = new AtomicBoolean(); + Object applicationDummy = applicationClass.getConstructor().newInstance(); + Object customDummy = customClass.getConstructor().newInstance(); + + Method applicationMethod = methods.get(applicationDummy, __ -> updatedCache.set(true)); + assertTrue(updatedCache.getAndSet(false), "cache was updated"); + + Method cachedApplicationMethod = methods.get(applicationDummy, __ -> updatedCache.set(true)); + assertFalse(updatedCache.getAndSet(false), "cache was updated"); + + Method customMethod = methods.get(customDummy, __ -> updatedCache.set(true)); + assertTrue(updatedCache.getAndSet(false), "cache was updated"); + + Method cachedCustomMethod = methods.get(customDummy, __ -> updatedCache.set(true)); + assertFalse(updatedCache.getAndSet(false), "cache was updated"); + + assertSame(applicationMethod, cachedApplicationMethod); + assertNotSame(applicationMethod, customMethod); + assertSame(customMethod, cachedCustomMethod); + + cachedApplicationMethod.invoke(applicationDummy); + cachedCustomMethod.invoke(customDummy); + assertThrows(IllegalArgumentException.class, () -> applicationMethod.invoke(customDummy)); + assertThrows(IllegalArgumentException.class, () -> customMethod.invoke(applicationDummy)); + } + } + + public static class Dummy implements Cloneable { + public Dummy() { } + public Object clone() throws CloneNotSupportedException { return super.clone(); } + } + +} diff --git a/vespajlib/src/test/resources/dummy b/vespajlib/src/test/resources/dummy new file mode 100644 index 00000000000..2995a4d0e74 --- /dev/null +++ b/vespajlib/src/test/resources/dummy @@ -0,0 +1 @@ +dummy
\ No newline at end of file |