### Question:
MemoryLimitedCacheLoader implements BootstrapCacheLoader, Cloneable { protected boolean isInMemoryLimitReached(final Ehcache cache, final int loadedElements) { long maxBytesInMem; long maxElementsInMem; final boolean overflowToOffHeap = cache.getCacheConfiguration().isOverflowToOffHeap(); maxElementsInMem = cache.getCacheConfiguration().getMaxEntriesLocalHeap() == 0 ? Integer.MAX_VALUE : cache.getCacheConfiguration().getMaxEntriesLocalHeap(); if (overflowToOffHeap) { maxBytesInMem = cache.getCacheConfiguration().getMaxBytesLocalOffHeap(); } else { maxBytesInMem = cache.getCacheConfiguration().getMaxBytesLocalHeap(); } if (maxBytesInMem != 0) { final long inMemoryCount = overflowToOffHeap ? cache.getStatistics().getLocalOffHeapSize() : cache.getStatistics().getLocalHeapSize(); if (inMemoryCount == 0L) { return false; } else { final long inMemorySizeInBytes = overflowToOffHeap ? cache.getStatistics().getLocalOffHeapSizeInBytes() : cache.getStatistics() .getLocalHeapSizeInBytes(); final long avgSize = inMemorySizeInBytes / inMemoryCount; return inMemorySizeInBytes + (avgSize * 2) >= maxBytesInMem; } } else { return loadedElements >= maxElementsInMem; } }  @Override Object clone();  }### Answer:
@Test public void testCountBasedLimitSetToNoLimit() { configuration.setOverflowToOffHeap(false); configuration.setMaxEntriesLocalHeap(0); assertThat(loader.isInMemoryLimitReached(cache, 0), is(false)); assertThat(loader.isInMemoryLimitReached(cache, Integer.MAX_VALUE), is(true)); }
@Test public void testCountBasedLimitSetToSomeLimit() { configuration.setOverflowToOffHeap(false); int maxEntriesLocalHeap = 10; configuration.setMaxEntriesLocalHeap(maxEntriesLocalHeap); assertThat(loader.isInMemoryLimitReached(cache, maxEntriesLocalHeap - 2), is(false)); assertThat(loader.isInMemoryLimitReached(cache, maxEntriesLocalHeap), is(true)); assertThat(loader.isInMemoryLimitReached(cache, maxEntriesLocalHeap + 2), is(true)); }
@Test public void testLocalHeapSizeBased() { configuration.setOverflowToOffHeap(false); configuration.setMaxBytesLocalHeap(1024L); when(statisticsGateway.getLocalHeapSize()).thenReturn(0L, 1L, 4L); when(statisticsGateway.getLocalHeapSizeInBytes()).thenReturn(250L, 1000L); assertThat(loader.isInMemoryLimitReached(cache, 0), is(false)); assertThat(loader.isInMemoryLimitReached(cache, 1), is(false)); assertThat(loader.isInMemoryLimitReached(cache, 4), is(true)); }
@Test public void testOffHeapSizeBased() { configuration.setOverflowToOffHeap(true); configuration.setMaxBytesLocalHeap(1024L); configuration.setMaxBytesLocalOffHeap(2048L); when(statisticsGateway.getLocalOffHeapSize()).thenReturn(0L, 1L, 4L); when(statisticsGateway.getLocalOffHeapSizeInBytes()).thenReturn(500L, 2000L); assertThat(loader.isInMemoryLimitReached(cache, 0), is(false)); assertThat(loader.isInMemoryLimitReached(cache, 1), is(false)); assertThat(loader.isInMemoryLimitReached(cache, 4), is(true)); }
@Test public void testPooledOffHeapSizeBased() { configuration.setOverflowToOffHeap(true); assertThat(loader.isInMemoryLimitReached(cache, 0), is(false)); assertThat(loader.isInMemoryLimitReached(cache, Integer.MAX_VALUE), is(true)); }
@Test public void testPooledHeapSizeBased() { assertThat(loader.isInMemoryLimitReached(cache, 0), is(false)); assertThat(loader.isInMemoryLimitReached(cache, Integer.MAX_VALUE), is(true)); }