### Question:
ContextHolderCache { private void updateCacheSize() { final int desiredMaxSize = contextCacheSize.getDesiredCacheSize(); final int actualMaxSize = cache.getMaxSize(); if (desiredMaxSize != actualMaxSize) { final LeastRecentlyUsed<String, ContextHolder> newCache = new LeastRecentlyUsed<String, ContextHolder>( desiredMaxSize); if (desiredMaxSize > cache.size()) { for (final Entry<String, ContextHolder> entry : cache.entrySet()) { newCache.put(entry.getKey(), entry.getValue()); } } else { logger.warning("Shrinking ContextHolderCache in response to change of system setting that determines " + "the maximum number of allowable concurrent sessions. Some cache entries will."); final LinkedList<Entry<String, ContextHolder>> entriesAsList = new LinkedList<Entry<String, ContextHolder>>(cache.entrySet()); final Iterator<Entry<String, ContextHolder>> itr = entriesAsList.descendingIterator(); final LinkedList<Entry<String, ContextHolder>> entriesToCopy = new LinkedList<Entry<String, ContextHolder>>(); int numberCopied = 0; while (itr.hasNext() && numberCopied++ < desiredMaxSize) { final Entry<String, ContextHolder> entry = itr.next(); entriesToCopy.add(entry); } final Iterator<Entry<String, ContextHolder>> toCopyRevItr = entriesToCopy.descendingIterator(); while (toCopyRevItr.hasNext()) { final Entry<String, ContextHolder> toCopy = toCopyRevItr.next(); newCache.put(toCopy.getKey(), toCopy.getValue()); } } cache = newCache; } } @Inject  ContextHolderCache(ContextCacheSize contextCacheSize); synchronized ContextHolder get(String key); synchronized ContextHolder remove(String key); synchronized ContextHolder createCachedContextHolder(); synchronized ContextHolder put(String key, ContextHolder contextHolder); synchronized int size(); synchronized int getMaxSize();  }### Answer:
@Test public void updateCacheSize() { final int initialCacheSize = 5; final int newCacheSize = 3; final ContextCacheSize cacheSize = mock(ContextCacheSize.class); when(cacheSize.getDesiredCacheSize()).thenReturn(initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, initialCacheSize, newCacheSize); final ContextHolderCache cache = new ContextHolderCache(cacheSize); for (Integer i = 0; i < initialCacheSize * 4; ++i) { final String idx = i.toString(); cache.put(idx, new ContextHolder(idx)); } assertThat(cache.size()).isEqualTo(initialCacheSize); for (Integer i = 0; i < 15; ++i) { assertThat(cache.get(i.toString())).isNull(); } for (Integer i = 15; i < initialCacheSize * 4; ++i) { assertThat(cache.get(i.toString())).isNotNull(); } cache.put("20", new ContextHolder("20")); assertThat(cache.size()).isEqualTo(newCacheSize); for (Integer i = 18; i <= 20; ++i) { final ContextHolder hldr = cache.get(i.toString()); assertThat(cache.get(i.toString())).isNotNull(); } }