### Question:
InMemoryCacheManager { @Produces @CacheProducer public <K, V> Cache<K, V> acquireCache(InjectionPoint injectionPoint) { Annotated annotated = injectionPoint.getAnnotated(); CacheType cacheTypeAnnotation = annotated.getAnnotation(CacheType.class); return acquireCache(cacheTypeAnnotation.value()); } @Inject  InMemoryCacheManager(InMemoryCacheConfigurator configurator); static MemoryUsageCalculator newCalc(); @Produces @CacheProducer Cache<K, V> acquireCache(InjectionPoint injectionPoint); @SuppressWarnings("unchecked") Cache<K, V> acquireCache(String cacheName); List<String> getAllocatedCacheNames(); CacheStats getStats(String cacheName);  }### Answer:
@Test void createCache() { setupManager(); Cache<String, byte[]> cache1024 = manager.acquireCache("SIMPLE_CACHE_NAME_1024"); assertNotNull(cache1024); cache1024 = manager.acquireCache("SIMPLE_CACHE_NAME_2048"); assertNull(cache1024); }
@Test void testCacheManipulations() { setupManager(); byte[] bytes1 = "1234567890".getBytes(); byte[] bytes2 = "ABCDEF1234567890".getBytes(); Cache<String, byte[]> cache1024 = manager.acquireCache("SIMPLE_CACHE_NAME_1024"); assertNotNull(cache1024); byte[] rez = cache1024.getIfPresent("key1"); assertNull(rez); cache1024.put("key1", bytes1); cache1024.put("key2", bytes2); assertEquals(2, cache1024.size()); assertEquals(bytes1, cache1024.getIfPresent("key1")); assertEquals(bytes2, cache1024.getIfPresent("key2")); }
@Test void testCacheManipulationsWithCacheLoader() throws ExecutionException { String cacheName = "SIMPLE_CACHE_NAME"; doReturn(64 * 1024 * 1024L).when(configurator).getAvailableMemory(); CacheConfiguration<String, byte[]> cacheCfg = new CacheConfigurator<>( cacheName, 1024, 10, new CacheLoader<>() { @Override public byte[] load(String key) throws Exception { return key.getBytes(); } }); doReturn(List.of(cacheCfg)).when(configurator).getConfiguredCaches(); manager = new InMemoryCacheManager(configurator); Cache<String, byte[]> cache = manager.acquireCache(cacheName); assertEquals(0L, cache.size()); LoadingCache<String, byte[]> loadingCache = (LoadingCache<String, byte[]>) cache; String key1 = "firstKey"; String key2 = "secondKey"; assertArrayEquals(key1.getBytes(), loadingCache.get(key1)); assertEquals(1L, cache.size()); assertArrayEquals(key2.getBytes(), loadingCache.get(key2)); assertEquals(2L, loadingCache.size()); }
@Test void testCacheEvictions() { String cacheName = "SIMPLE_CACHE_NAME"; doReturn(64 * 1024 * 1024L).when(configurator).getAvailableMemory(); CacheConfiguration cacheCfg = new CacheConfigurator(cacheName, 1024 * 1024, 1); doReturn(List.of(cacheCfg)).when(configurator).getConfiguredCaches(); manager = new InMemoryCacheManager(configurator); Cache<Integer, byte[]> cache = manager.acquireCache(cacheName); assertEquals(64, cacheCfg.getMaxSize()); int i = 0; for (; i < cacheCfg.getMaxSize() * 2; i++) { cache.put(i, Long.toUnsignedString(i * 1000L).getBytes()); } assertEquals(64 * 2, i); assertEquals(64, cache.size()); i--; assertNotNull(cache.getIfPresent(i)); assertArrayEquals(Long.toUnsignedString(i * 1000L).getBytes(), cache.getIfPresent(i)); }