From 9aee125089100198f025b5af6f6925eb0050f9b6 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Wed, 4 Jun 2014 08:58:18 -0700 Subject: [PATCH] Cleanup data in LocalUriFetcher --- .../load/resource/LocalUriFetcherTest.java | 66 +++++++++++++++++++ .../glide/load/resource/LocalUriFetcher.java | 22 +++++-- 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java diff --git a/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java b/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java new file mode 100644 index 000000000..2b76f2229 --- /dev/null +++ b/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java @@ -0,0 +1,66 @@ +package com.bumptech.glide.load.resource; + +import android.content.ContentResolver; +import android.content.Context; +import android.net.Uri; +import com.bumptech.glide.Priority; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; + +import java.io.Closeable; +import java.io.FileNotFoundException; +import java.io.IOException; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +@RunWith(RobolectricTestRunner.class) +public class LocalUriFetcherTest { + private TestLocalUriFetcher fetcher; + + @Before + public void setUp() { + fetcher = new TestLocalUriFetcher(Robolectric.application, Uri.parse("content://empty")); + } + + @Test + public void testClosesDataOnCleanup() throws Exception { + Closeable closeable = fetcher.loadResource(Priority.NORMAL); + fetcher.cleanup(); + + verify(closeable).close(); + } + + @Test + public void testDoesNotCLoseNullData() throws IOException { + fetcher.cleanup(); + + verify(fetcher.closeable, never()).close(); + } + + @Test + public void testHandlesExceptionOnClose() throws Exception { + Closeable closeable = fetcher.loadResource(Priority.NORMAL); + + doThrow(new IOException("Test")).when(closeable).close(); + fetcher.cleanup(); + verify(closeable).close(); + } + + private static class TestLocalUriFetcher extends LocalUriFetcher { + public Closeable closeable = mock(Closeable.class); + public TestLocalUriFetcher(Context context, Uri uri) { + super(context, uri); + } + + @Override + protected Closeable loadResource(Uri uri, ContentResolver contentResolver) throws FileNotFoundException { + return closeable; + } + } +} diff --git a/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java b/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java index 459e3da7b..078e23cbd 100644 --- a/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java +++ b/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java @@ -3,17 +3,22 @@ package com.bumptech.glide.load.resource; import android.content.ContentResolver; import android.content.Context; import android.net.Uri; +import android.util.Log; import com.bumptech.glide.Priority; +import java.io.Closeable; import java.io.FileNotFoundException; +import java.io.IOException; import java.lang.ref.WeakReference; /** * */ -public abstract class LocalUriFetcher implements ResourceFetcher { +public abstract class LocalUriFetcher implements ResourceFetcher { + private static final String TAG = "LocalUriFetcher"; private final WeakReference contextRef; private final Uri uri; + private T data; /** * Opens an input stream for a uri pointing to a local asset. Only certain uris are supported @@ -37,14 +42,23 @@ public abstract class LocalUriFetcher implements ResourceFetcher { throw new NullPointerException("Context has been cleared in LocalUriFetcher uri: " + uri); } ContentResolver contentResolver = context.getContentResolver(); - return loadResource(uri, contentResolver); + data = loadResource(uri, contentResolver); + return data; } @Override public void cleanup() { - // Do nothing. - } + if (data != null) { + try { + data.close(); + } catch (IOException e) { + if (Log.isLoggable(TAG, Log.VERBOSE)) { + Log.v(TAG, "failed to close data", e); + } + } + } + } @Override public void cancel() { -- GitLab