Permalink
Browse files

A protection in FileThumbnailCache against empty URLs

  • Loading branch information...
1 parent 5573ee1 commit 30bd3359acf6df28f3de11ae4a5c0f8f08761167 @tindandelion committed Oct 30, 2012
@@ -10,12 +10,12 @@
public static final byte[] THUMBNAIL = new byte[] {0x1, 0x2, 0x3};
private FileThumbnailCache cache;
- public void testLookupFailure() throws Exception {
+ public void testWhenNoThumbnailInCache_ReturnsNull() throws Exception {
final String url = "http://example.com/thumbnail.jpg";
assertNull(cache.lookup(url));
}
- public void testSaveAndLoadThumbnail() throws Exception {
+ public void testWhenHasThumbnailInCache_ReturnItsContent() throws Exception {
final String url = "http://example.com/thumbnail.jpg";
cache.update(url, THUMBNAIL);
@@ -24,8 +24,11 @@ public void testSaveAndLoadThumbnail() throws Exception {
assertTrue(Arrays.equals(THUMBNAIL, cached));
}
- public void testUnableToGetFilenameFromUrl() throws Exception {
+ public void testWhenUrlIsEmpty_NeitherSaveNorLookItUp() throws Exception {
+ final String url = "";
+ cache.update(url, THUMBNAIL);
+ assertNull(cache.lookup(url));
}
protected void setUp() throws Exception {
@@ -1,11 +0,0 @@
-package org.dandelion.radiot.integration;
-
-import android.net.Uri;
-import junit.framework.TestCase;
-
-public class UriTest extends TestCase {
- public void testGettingLastPathSegment() throws Exception {
- Uri uri = Uri.parse("http://example.com/filename.txt");
- assertEquals("filename.txt", uri.getLastPathSegment());
- }
-}
@@ -18,13 +18,15 @@ public FileThumbnailCache(File cacheDir) {
public void update(String url, byte[] thumbnail) {
makeCacheDir();
File cached = cachedFileForUrl(url);
- saveThumbnail(cached, thumbnail);
+ if (cached != null) {
+ saveThumbnail(cached, thumbnail);
+ }
}
@Override
public byte[] lookup(String url) {
File cached = cachedFileForUrl(url);
- if (cached.exists()) {
+ if (cached != null && cached.exists()) {
return loadThumbnail(cached);
} else {
return null;
@@ -62,7 +64,11 @@ private void saveThumbnail(File file, byte[] thumbnail) {
private File cachedFileForUrl(String url) {
Uri uri = Uri.parse(url);
- return new File(cacheDir, uri.getLastPathSegment());
+ String fname = uri.getLastPathSegment();
+ if (fname != null) {
+ return new File(cacheDir, fname);
+ } else {
+ return null;
+ }
}
-
}

0 comments on commit 30bd335

Please sign in to comment.