class SavingStreamProvider implements BitmapProvider {

    @Override
    public Bitmap load() throws IOException {
        Bitmap bitmap;
        try {
            bitmap = loadBitmap();
        } catch (OutOfMemoryError e) {
            if (request.memoryCache() != null) request.memoryCache().clear();
            System.gc();
            try {
                bitmap = loadBitmap();
            } catch (OutOfMemoryError e2) {
                throw e2;
            }
        }
        return bitmap;
    }

}

class SavingStreamProviderTest  {

    @Test
    public void testLoads_doesNotSaveSource() throws IOException {

        MirageRequest request = mock(MirageRequest.class);
        UrlFactory factory = mock(UrlFactory.class);
        InputStream inputStream = mock(InputStream.class);
        when(factory.getStream(any(Uri.class))).thenReturn(inputStream);
        when(request.urlFactory()).thenReturn(factory);
        when(request.uri()).thenReturn(Uri.parse("http://www.sample.com/"));

        DiskCache diskCache = mock(DiskCache.class);
        MemoryCache<String, Bitmap> memCache = mock(MemoryCache.class);
        when(request.diskCache()).thenReturn(diskCache);
        when(request.memoryCache()).thenReturn(memCache);

        SavingStreamProvider provider = new SavingStreamProvider(request) {
            @Override
            protected InputStream stream() throws IOException {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Bitmap bitmap = Bitmap.createBitmap(400, 600, Bitmap.Config.ARGB_8888);
                bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
                byte[] bitmapdata = bos.toByteArray();
                ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
                return bs;
            }
        };
        when(request.provider()).thenReturn(provider);

        Bitmap bitmap = provider.load();
        Assert.assertNotNull(bitmap);

        verify(diskCache, times(0)).put(anyString(), any(DiskCache.Writer.class));
    }

}