### Question:
TypeCache extends ReferenceQueue<ClassLoader> { public Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy) { Class<?> type = find(classLoader, key); if (type != null) { return type; } else { try { return insert(classLoader, key, lazy.call()); } catch (Throwable throwable) { throw new IllegalArgumentException("Could not create type", throwable); } } } TypeCache(Sort sort); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> find(ClassLoader classLoader, T key); @SuppressFBWarnings(value = "GC_UNRELATED_TYPES", justification = "Cross-comparison is intended") Class<?> insert(ClassLoader classLoader, T key, Class<?> type); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy); Class<?> findOrInsert(ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor); void expungeStaleEntries(); void clear();  }### Answer:
@Test @SuppressWarnings("unchecked") public void testFindOrInsert() throws Exception { TypeCache<Object> typeCache = new TypeCache<Object>(TypeCache.Sort.WEAK); Object key = new Object(); assertThat(typeCache.find(ClassLoader.getSystemClassLoader(), key), nullValue(Class.class)); Callable<Class<?>> callable = mock(Callable.class); when(callable.call()).thenReturn((Class) Void.class); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable, new Object()), is((Object) Void.class)); verify(callable).call(); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable), is((Object) Void.class)); assertThat(typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), key, callable, new Object()), is((Object) Void.class)); verifyNoMoreInteractions(callable); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void testCreationException() throws Exception { TypeCache<Object> typeCache = new TypeCache<Object>(TypeCache.Sort.WEAK); Callable<Class<?>> callable = mock(Callable.class); when(callable.call()).thenThrow(RuntimeException.class); typeCache.findOrInsert(ClassLoader.getSystemClassLoader(), new Object(), callable, new Object()); }