Skip to content

Commit

Permalink
Adding test for cache verification
Browse files Browse the repository at this point in the history
Signed-off-by: shikharj05 <[email protected]>
  • Loading branch information
shikharj05 committed Dec 16, 2024
1 parent 50be83b commit 31e037f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public final class SafeSerializationUtils {
);

private static final Set<String> SAFE_CLASS_NAMES = Collections.singleton("org.ldaptive.LdapAttribute$LdapAttributeValues");
private static final Map<Class<?>, Boolean> safeClassCache = new ConcurrentHashMap<>();
static final Map<Class<?>, Boolean> safeClassCache = new ConcurrentHashMap<>();

static boolean isSafeClass(Class<?> cls) {
return safeClassCache.computeIfAbsent(cls, SafeSerializationUtils::computeIsSafeClass);
}

private static boolean computeIsSafeClass(Class<?> cls) {
static boolean computeIsSafeClass(Class<?> cls) {
return cls.isArray() || SAFE_CLASSES.contains(cls) || SAFE_CLASS_NAMES.contains(cls.getName()) || isAssignableFromSafeClass(cls);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ class CustomArrayList extends ArrayList<String> {}
class CustomMap extends HashMap<String, Integer> {}
assertTrue(SafeSerializationUtils.isSafeClass(CustomMap.class));
}

@Test
public void testCaching() {
// First call should compute the result
boolean result1 = SafeSerializationUtils.isSafeClass(String.class);
assertTrue(result1);

// Second call should use cached result
boolean result2 = SafeSerializationUtils.isSafeClass(String.class);
assertTrue(result2);

// Verify that the cache was used (size should be 1)
assertEquals(1, SafeSerializationUtils.safeClassCache.size());

// Third call for a different class
boolean result3 = SafeSerializationUtils.isSafeClass(Integer.class);
assertTrue(result3);
// Verify that the cache was updated
assertEquals(2, SafeSerializationUtils.safeClassCache.size());
}
}

0 comments on commit 31e037f

Please sign in to comment.