### Question:
CachedAreaProvider implements AreaProvider { @Nullable @Override public Box getSourceArea(@Nonnull RenderImage renderImage) { if (renderImage != m_lastProcessedImage) { m_lastProcessedImage = renderImage; m_cachedArea = m_cachedProvider.getSourceArea(renderImage); } return m_cachedArea; } CachedAreaProvider(AreaProvider cachedProvider); @Override void setParameters(String parameters); @Nullable @Override Box getSourceArea(@Nonnull RenderImage renderImage); @Nonnull @Override Size getNativeSize(@Nonnull NiftyImage image);  }### Answer:
@Test public void testGetSourceAreaReturnsSameAreaAsCachedProvider() { RenderImage image = createMock(RenderImage.class); AreaProvider areaProvider = createMock(AreaProvider.class); expect(areaProvider.getSourceArea(image)).andReturn(new Box(1, 2, 3, 4)).anyTimes(); replay(areaProvider); CachedAreaProvider cachedAreaProvider = new CachedAreaProvider(areaProvider); assertEquals(cachedAreaProvider.getSourceArea(image), areaProvider.getSourceArea(image)); verify(areaProvider); }
@Test public void testGetSourceAreaCachesReturnedValueForASingleImage() { RenderImage image = createMock(RenderImage.class); AreaProvider areaProvider = createMock(AreaProvider.class); expect(areaProvider.getSourceArea(image)).andReturn(new Box(1, 2, 3, 4)); replay(areaProvider); CachedAreaProvider cachedAreaProvider = new CachedAreaProvider(areaProvider); assertSame(cachedAreaProvider.getSourceArea(image), cachedAreaProvider.getSourceArea(image)); verify(areaProvider); }
@Test public void testGetSourceAreaInvalidatesCachedAreaForADifferentImage() { RenderImage firstImage = createMock(RenderImage.class); RenderImage secondImage = createMock(RenderImage.class); AreaProvider areaProvider = createMock(AreaProvider.class); expect(areaProvider.getSourceArea(firstImage)).andReturn(new Box(1, 2, 3, 4)); expect(areaProvider.getSourceArea(secondImage)).andReturn(new Box(1, 2, 3, 4)); replay(areaProvider); CachedAreaProvider cachedAreaProvider = new CachedAreaProvider(areaProvider); assertNotSame(cachedAreaProvider.getSourceArea(firstImage), cachedAreaProvider.getSourceArea(secondImage)); verify(areaProvider); }