Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

improving tests issue #25,and fix issue #47 #49

Merged
merged 1 commit into from

1 participant

@luigi-agosti

No description provided.

@luigi-agosti luigi-agosti reopened this
@luigi-agosti luigi-agosti merged commit 3056ba6 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 27, 2012
  1. @luigi-agosti
This page is out of date. Refresh to see the latest.
Showing with 636 additions and 237 deletions.
  1. +5 −0 core/pom.xml
  2. +9 −5 core/src/main/java/com/novoda/imageloader/core/ImageManager.java
  3. +7 −1 core/src/main/java/com/novoda/imageloader/core/LoaderSettings.java
  4. +12 −9 core/src/main/java/com/novoda/imageloader/core/file/BasicFileManager.java
  5. +54 −0 core/src/main/java/com/novoda/imageloader/core/file/util/AndroidFileContext.java
  6. +22 −34 core/src/main/java/com/novoda/imageloader/core/file/util/FileUtil.java
  7. +7 −7 core/src/main/java/com/novoda/imageloader/core/loader/util/BitmapDisplayer.java
  8. +23 −0 core/src/main/java/com/novoda/imageloader/core/util/Log.java
  9. +63 −0 core/src/test/java/com/novoda/imageloader/core/ImageManagerTest.java
  10. +0 −21 core/src/test/java/com/novoda/imageloader/core/Util.java
  11. +31 −24 core/src/test/java/com/novoda/imageloader/core/file/BasicFileManagerTest.java
  12. +42 −0 core/src/test/java/com/novoda/imageloader/core/file/FileTestCase.java
  13. +0 −94 core/src/test/java/com/novoda/imageloader/core/file/FileUtilTest.java
  14. +182 −0 core/src/test/java/com/novoda/imageloader/core/file/util/FileUtilTest.java
  15. +74 −0 core/src/test/java/com/novoda/imageloader/core/loader/util/BitmapDisplayerTest.java
  16. +17 −2 core/src/test/java/com/novoda/imageloader/core/model/ImageTagFactoryTest.java
  17. +20 −4 core/src/test/java/com/novoda/imageloader/core/model/ImageWrapperTest.java
  18. +29 −12 core/src/test/java/com/novoda/imageloader/core/network/UrlNetworlManagerTest.java
  19. +17 −17 core/src/test/java/com/novoda/imageloader/core/network/UrlUtilTest.java
  20. +20 −7 core/src/test/java/com/novoda/imageloader/core/util/DirectLoaderTest.java
  21. +2 −0  core/src/test/resources/testFile1
View
5 core/pom.xml
@@ -66,6 +66,11 @@
<version>1.4.12</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>junit-addons</groupId>
+ <artifactId>junit-addons</artifactId>
+ <version>1.4</version>
+ </dependency>
</dependencies>
<build>
View
14 core/src/main/java/com/novoda/imageloader/core/ImageManager.java
@@ -56,11 +56,7 @@ public ImageManager(Context context, LoaderSettings settings) {
cacheManager = new SoftMapCache();
}
loaderContext.setCache(cacheManager);
- if (settings.isUseAsyncTasks()) {
- this.loader = new ConcurrentLoader(loaderContext);
- } else {
- this.loader = new SimpleLoader(loaderContext);
- }
+ setLoader(settings);
verifyPermissions(context);
}
@@ -83,6 +79,14 @@ public CacheManager getCacheManager() {
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
+
+ protected void setLoader(LoaderSettings settings) {
+ if (settings.isUseAsyncTasks()) {
+ this.loader = new ConcurrentLoader(loaderContext);
+ } else {
+ this.loader = new SimpleLoader(loaderContext);
+ }
+ }
private void verifyPermissions(Context context) {
verifyPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
View
8 core/src/main/java/com/novoda/imageloader/core/LoaderSettings.java
@@ -21,6 +21,7 @@
import android.os.Build;
import com.novoda.imageloader.core.cache.CacheManager;
+import com.novoda.imageloader.core.file.util.AndroidFileContext;
import com.novoda.imageloader.core.file.util.FileUtil;
/**
@@ -127,6 +128,10 @@ public boolean isUseAsyncTasks() {
public void setUseAsyncTasks(boolean useAsyncTasks) {
this.useAsyncTasks = useAsyncTasks;
}
+
+ public boolean isCleanOnSetup() {
+ return true;
+ }
/**
* Builder for the LoaderSettings.
@@ -175,7 +180,8 @@ public SettingsBuilder withCacheDir(File file) {
}
public LoaderSettings build(Context context) {
- settings.setCacheDir(new FileUtil().prepareCacheDirectory(context));
+ File dir = new FileUtil().prepareCacheDirectory(new AndroidFileContext(context));
+ settings.setCacheDir(dir);
settings.setSdkVersion(Build.VERSION.SDK);
return settings;
}
View
21 core/src/main/java/com/novoda/imageloader/core/file/BasicFileManager.java
@@ -23,6 +23,7 @@
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.file.util.FileUtil;
import com.novoda.imageloader.core.network.UrlUtil;
+import com.novoda.imageloader.core.util.Log;
/**
* This is a basic implementation for the file manager.
@@ -31,11 +32,13 @@
*/
public class BasicFileManager implements FileManager {
- private LoaderSettings settings;
+ private LoaderSettings loaderSettings;
public BasicFileManager(LoaderSettings settings) {
- this.settings = settings;
- cleanOldFiles();
+ this.loaderSettings = settings;
+ if(settings.isCleanOnSetup()) {
+ cleanOldFiles();
+ }
}
/**
@@ -52,7 +55,7 @@ public void clean() {
*/
@Override
public void cleanOldFiles() {
- deleteOldFiles(settings.getExpirationPeriod());
+ deleteOldFiles(loaderSettings.getExpirationPeriod());
}
@Override
@@ -70,7 +73,7 @@ public void saveBitmap(String fileName, Bitmap b, int width, int height) {
FileOutputStream out = new FileOutputStream(fileName + "-" + width + "x" + height);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
- e.printStackTrace();
+ Log.warning("" + e.getMessage());
}
}
@@ -78,25 +81,25 @@ public void saveBitmap(String fileName, Bitmap b, int width, int height) {
public File getFile(String url) {
url = processUrl(url);
String filename = String.valueOf(url.hashCode());
- return new File(settings.getCacheDir(), filename);
+ return new File(loaderSettings.getCacheDir(), filename);
}
@Override
public File getFile(String url, int width, int height) {
url = processUrl(url);
String filename = String.valueOf(url.hashCode()) + "-" + width + "x" + height;
- return new File(settings.getCacheDir(), filename);
+ return new File(loaderSettings.getCacheDir(), filename);
}
private String processUrl(String url) {
- if (settings.isQueryIncludedInHash()) {
+ if (loaderSettings.isQueryIncludedInHash()) {
return url;
}
return new UrlUtil().removeQuery(url);
}
private void deleteOldFiles(final long expirationPeriod) {
- final String cacheDir = settings.getCacheDir().getAbsolutePath();
+ final String cacheDir = loaderSettings.getCacheDir().getAbsolutePath();
Thread cleaner = new Thread(new Runnable() {
@Override
public void run() {
View
54 core/src/main/java/com/novoda/imageloader/core/file/util/AndroidFileContext.java
@@ -0,0 +1,54 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.novoda.imageloader.core.file.util;
+
+import java.io.File;
+
+import android.content.Context;
+
+/**
+ * Internal class to abstract the dependency to specific implementation
+ * of android functionalities.
+ * This class is for internal usage of the imageLoader.
+ */
+public class AndroidFileContext {
+
+ private Context context;
+
+ public AndroidFileContext(Context context) {
+ this.context = context;
+ }
+
+ protected boolean isMounted() {
+ if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
+ return true;
+ }
+ return false;
+ }
+
+ protected String getPackageName() {
+ return context.getPackageName();
+ }
+
+ protected File getExternalStorageDirectory() {
+ return android.os.Environment.getExternalStorageDirectory();
+ }
+
+ protected File preparePhoneCacheDir() {
+ return context.getCacheDir();
+ }
+
+}
View
56 core/src/main/java/com/novoda/imageloader/core/file/util/FileUtil.java
@@ -23,20 +23,19 @@
import java.io.InputStream;
import java.io.OutputStream;
-import android.content.Context;
-import android.util.Log;
-
import com.novoda.imageloader.core.exception.ImageCopyException;
+import com.novoda.imageloader.core.util.Log;
+/**
+ * This class is internal to the imageLoader.
+ * If you want to used it make sure to prepare for changes to the interface.
+ */
public class FileUtil {
private static final String ANDROID_ROOT = "/Android/data/";
private static final String NOMEDIA_FILE_NAME = ".nomedia";
private static final String DEFAULT_IMAGE_FOLDER_NAME = "/cache/images";
- private static final String TAG = "ImageLoader";
-
private static final int BUFFER_SIZE = 60 * 1024;
- private static final byte[] BUFFER = new byte[BUFFER_SIZE];
public void closeSilently(Closeable c) {
try {
@@ -44,26 +43,26 @@ public void closeSilently(Closeable c) {
c.close();
}
} catch (Exception e) {
- Log.e(TAG, "Problem closing stream " + e.getMessage());
+ Log.warning("Problem closing stream " + e.getMessage());
}
}
public void copyStream(InputStream is, OutputStream os) {
try {
+ byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
- synchronized (BUFFER) {
- int amountRead = is.read(BUFFER);
- if (amountRead == -1) {
- break;
- }
- os.write(BUFFER, 0, amountRead);
+ int amountRead = is.read(buffer);
+ if (amountRead == -1) {
+ break;
}
+ os.write(buffer, 0, amountRead);
}
- } catch (Exception ex) {
- Log.e(TAG, "Exception : ", ex);
+ } catch (Exception e) {
+ Log.warning("Exception : " + e.getMessage());
}
}
+
public boolean deleteFileCache(String cacheDirFullPath) {
return reduceFileCache(cacheDirFullPath, -1);
}
@@ -101,42 +100,31 @@ public void copy(File src, File dst) throws ImageCopyException {
}
}
- public File prepareCacheDirectory(Context context) {
+ public File prepareCacheDirectory(AndroidFileContext fileContext) {
File dir = null;
- if (!isMounted()) {
- dir = preparePhoneCacheDir(context);
+ if (fileContext.isMounted()) {
+ dir = prepareExternalCacheDir(fileContext);
} else {
- dir = prepareExternalCacheDir(context);
+ dir = fileContext.preparePhoneCacheDir();
}
addNomediaFile(dir);
return dir;
}
- public boolean isMounted() {
- if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
- return true;
- }
- return false;
- }
-
- private File prepareExternalCacheDir(Context context) {
- String relativepath = ANDROID_ROOT + context.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
- File file = new File(android.os.Environment.getExternalStorageDirectory(), relativepath);
+ private File prepareExternalCacheDir(AndroidFileContext fileContext) {
+ String relativepath = ANDROID_ROOT + fileContext.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
+ File file = new File(fileContext.getExternalStorageDirectory(), relativepath);
if (!file.isDirectory()) {
file.mkdirs();
}
return file;
}
- private File preparePhoneCacheDir(Context context) {
- return context.getCacheDir();
- }
-
private void addNomediaFile(File dir) {
try {
new File(dir, NOMEDIA_FILE_NAME).createNewFile();
} catch (Exception e) {
- // Don't care if doesn't save the file
+ Log.warning("Problem creating .nomedia file : " + e.getMessage());
}
}
View
14 core/src/main/java/com/novoda/imageloader/core/loader/util/BitmapDisplayer.java
@@ -21,15 +21,15 @@
public class BitmapDisplayer implements Runnable {
private Bitmap bitmap;
- private ImageWrapper imageView;
+ private ImageWrapper imageWrapper;
- public BitmapDisplayer(Bitmap b, ImageWrapper i) {
- bitmap = b;
- imageView = i;
+ public BitmapDisplayer(Bitmap bitmap, ImageWrapper imageWrapper) {
+ this.bitmap = bitmap;
+ this.imageWrapper = imageWrapper;
}
public void runOnUiThread() {
- imageView.runOnUiThread(this);
+ imageWrapper.runOnUiThread(this);
}
@Override
@@ -37,9 +37,9 @@ public void run() {
if (bitmap == null) {
return;
}
- if(imageView.isUrlChanged()) {
+ if(imageWrapper.isUrlChanged()) {
return;
}
- imageView.setBitmap(bitmap);
+ imageWrapper.setBitmap(bitmap);
}
}
View
23 core/src/main/java/com/novoda/imageloader/core/util/Log.java
@@ -0,0 +1,23 @@
+package com.novoda.imageloader.core.util;
+
+public class Log {
+
+ private static final String TAG = "ImageLoader";
+
+ public static final void info(String message) {
+ try {
+ android.util.Log.i(TAG, message);
+ } catch(Exception e) {
+ System.out.println(TAG + " " + message);
+ }
+ }
+
+ public static final void warning(String message) {
+ try {
+ android.util.Log.w(TAG, message);
+ } catch(Exception e) {
+ System.out.println(TAG + " " + message);
+ }
+ }
+
+}
View
63 core/src/test/java/com/novoda/imageloader/core/ImageManagerTest.java
@@ -0,0 +1,63 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.novoda.imageloader.core;
+
+import static org.mockito.Mockito.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import android.Manifest;
+import android.content.Context;
+import android.content.pm.PackageManager;
+
+public class ImageManagerTest {
+
+ private LoaderSettings loaderSettings;
+ private Context context;
+
+ @Before
+ public void beforeEveryTest() {
+ loaderSettings = mock(LoaderSettings.class);
+ when(loaderSettings.isCleanOnSetup()).thenReturn(false);
+ context = mock(Context.class);
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void shouldComplaingIfInternetPermissionIsNotSet() {
+ PackageManager pm = mock(PackageManager.class);
+ when(pm.checkPermission(Manifest.permission.INTERNET, null))
+ .thenReturn(PackageManager.PERMISSION_DENIED);
+ when(context.getPackageManager()).thenReturn(pm);
+
+ new ImageManager(context, loaderSettings) {
+ protected void setLoader(LoaderSettings settings) {};
+ };
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void shouldComplaingIfWriteExternalStoragePermissionIsNotSet() {
+ PackageManager pm = mock(PackageManager.class);
+ when(pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, null))
+ .thenReturn(PackageManager.PERMISSION_DENIED);
+ when(context.getPackageManager()).thenReturn(pm);
+
+ new ImageManager(context, loaderSettings) {
+ protected void setLoader(LoaderSettings settings) {};
+ };
+ }
+
+}
View
21 core/src/test/java/com/novoda/imageloader/core/Util.java
@@ -1,21 +0,0 @@
-/**
- * Copyright 2012 Novoda Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.novoda.imageloader.core;
-
-public class Util {
-
- public static final String FOLDER_FOR_TEST_TMP_FILES = "target/unit-test/tmp";
-}
View
55 core/src/test/java/com/novoda/imageloader/core/file/BasicFileManagerTest.java
@@ -1,49 +1,54 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.novoda.imageloader.core.file;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import java.io.File;
-import java.io.IOException;
-
-import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.Mockito;
import com.novoda.imageloader.core.LoaderSettings;
-import com.novoda.imageloader.core.Util;
-public class BasicFileManagerTest {
+public class BasicFileManagerTest extends FileTestCase {
private BasicFileManager basicFileManager;
private LoaderSettings settings;
- private File cacheDir;
@Before
public void beforeEachTest() {
- settings = Mockito.mock(LoaderSettings.class);
- basicFileManager = new BasicFileManager(settings) {
- @Override
- public void cleanOldFiles() {
- //overriding to tests
- }
- };
- cacheDir = new File(Util.FOLDER_FOR_TEST_TMP_FILES);
- cacheDir.mkdirs();
+ settings = mock(LoaderSettings.class);
+ createCacheDir();
}
@After
- public void afterEachTest() throws IOException {
- FileUtils.deleteDirectory(cacheDir);
- }
+ public void afterEachTest() {
+ deleteCacheDir();
+ }
@Test
public void shouldDistinguishBetweenUrlWithQueryIfIsQueryIncludedInHashIsTrue() {
- Mockito.when(settings.getCacheDir()).thenReturn(cacheDir);
- Mockito.when(settings.isQueryIncludedInHash()).thenReturn(true);
+ when(settings.isCleanOnSetup()).thenReturn(false);
+ when(settings.getCacheDir()).thenReturn(cacheDir);
+ when(settings.isQueryIncludedInHash()).thenReturn(true);
+ basicFileManager = new BasicFileManager(settings);
String filePath1 = basicFileManager.getFile("http://googl.com?param=1").getAbsolutePath();
String filePath2 = basicFileManager.getFile("http://googl.com?param=2").getAbsolutePath();
@@ -52,9 +57,11 @@ public void shouldDistinguishBetweenUrlWithQueryIfIsQueryIncludedInHashIsTrue()
@Test
public void shouldNotDistinguishBetweenUrlWithQueryIfIsQueryIncludedInHashIsFalse() {
- Mockito.when(settings.getCacheDir()).thenReturn(cacheDir);
- Mockito.when(settings.isQueryIncludedInHash()).thenReturn(false);
+ when(settings.isCleanOnSetup()).thenReturn(false);
+ when(settings.getCacheDir()).thenReturn(cacheDir);
+ when(settings.isQueryIncludedInHash()).thenReturn(false);
+ basicFileManager = new BasicFileManager(settings);
String filePath1 = basicFileManager.getFile("http://googl.com?param=1").getAbsolutePath();
String filePath2 = basicFileManager.getFile("http://googl.com?param=2").getAbsolutePath();
View
42 core/src/test/java/com/novoda/imageloader/core/file/FileTestCase.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.novoda.imageloader.core.file;
+
+import java.io.File;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Assert;
+
+public class FileTestCase {
+
+ private static final String FOLDER_FOR_TEST_TMP_FILES = "target/unit-test/tmp";
+
+ protected File cacheDir;
+
+ protected void createCacheDir() {
+ cacheDir = new File(FOLDER_FOR_TEST_TMP_FILES);
+ cacheDir.mkdirs();
+ }
+
+ protected void deleteCacheDir() {
+ try {
+ FileUtils.deleteDirectory(cacheDir);
+ } catch (Exception e) {
+ Assert.fail("Problem deleting the cachedir : " + e.getMessage());
+ }
+ }
+
+}
View
94 core/src/test/java/com/novoda/imageloader/core/file/FileUtilTest.java
@@ -1,94 +0,0 @@
-/**
- * Copyright 2012 Novoda Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.novoda.imageloader.core.file;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.io.IOException;
-
-import junit.framework.Assert;
-
-import org.apache.commons.io.FileUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.novoda.imageloader.core.Util;
-import com.novoda.imageloader.core.file.util.FileUtil;
-
-public class FileUtilTest {
-
- private FileUtil fileUtil = new FileUtil();
- private File cacheDir;
-
- @Before
- public void beforeEachTest() {
- cacheDir = new File(Util.FOLDER_FOR_TEST_TMP_FILES);
- cacheDir.mkdirs();
- }
-
- @After
- public void afterEachTest() throws IOException {
- FileUtils.deleteDirectory(cacheDir);
- }
-
- @Test
- public void shouldDeleteAllFiles() throws IOException {
- File f1 = createFile("1");
- File f2 = createFile("2");
-
- assertTrue(fileUtil.deleteFileCache(cacheDir.getAbsolutePath()));
-
- assertFalse(f1.exists());
- assertFalse(f2.exists());
- }
-
- @Test
- public void shouldReduceFiles() throws IOException {
- File f1 = createFile("1", 10000);
- File f2 = createFile("2");
- File f3 = createFile("3", 105000);
- File f4 = createFile("4", System.currentTimeMillis());
-
- assertTrue(fileUtil.reduceFileCache(cacheDir.getAbsolutePath(), 24*3600*1000));
-
- assertFalse(f1.exists());
- assertTrue(f2.exists());
- assertFalse(f3.exists());
- assertTrue(f4.exists());
- }
-
- private File createFile(String name) {
- return createFile(name, -1);
- }
-
- private File createFile(String name, long lastModified) {
- try {
- File f1 = new File(cacheDir, name + ".tmp");
- FileUtils.write(f1, name);
- if(lastModified != -1) {
- f1.setLastModified(lastModified);
- }
- return f1;
- } catch (Exception e) {
- Assert.fail("Can't crete file for the test" + e.getMessage());
- throw new RuntimeException("Can't crete file for the test " + e.getMessage());
- }
- }
-
-}
View
182 core/src/test/java/com/novoda/imageloader/core/file/util/FileUtilTest.java
@@ -0,0 +1,182 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.novoda.imageloader.core.file.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.Assert;
+import junitx.framework.FileAssert;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.novoda.imageloader.core.exception.ImageCopyException;
+import com.novoda.imageloader.core.file.FileTestCase;
+
+public class FileUtilTest extends FileTestCase {
+
+ private FileUtil fileUtil = new FileUtil();
+
+ @Before
+ public void beforeEachTest() {
+ createCacheDir();
+ }
+
+ @After
+ public void afterEachTest() {
+ deleteCacheDir();
+ }
+
+ @Test
+ public void shouldDeleteAllFiles() throws IOException {
+ File f1 = createFile("1");
+ File f2 = createFile("2");
+
+ assertTrue(fileUtil.deleteFileCache(cacheDir.getAbsolutePath()));
+
+ assertFalse(f1.exists());
+ assertFalse(f2.exists());
+ }
+
+ @Test
+ public void shouldReduceFiles() throws IOException {
+ File f1 = createFile("1", 10000);
+ File f2 = createFile("2");
+ File f3 = createFile("3", 105000);
+ File f4 = createFile("4", System.currentTimeMillis());
+
+ assertTrue(fileUtil.reduceFileCache(cacheDir.getAbsolutePath(), 24*3600*1000));
+
+ assertFalse(f1.exists());
+ assertTrue(f2.exists());
+ assertFalse(f3.exists());
+ assertTrue(f4.exists());
+ }
+
+ @Test
+ public void shouldCloseNonNullCloseable() throws IOException {
+ Closeable closeable = mock(Closeable.class);
+
+ fileUtil.closeSilently(closeable);
+
+ verify(closeable, atLeastOnce()).close();
+ }
+
+ @Test
+ public void shouldCloseAvoidNullPointers() {
+ fileUtil.closeSilently(null);
+ }
+
+ @Test
+ public void shouldCloseFailSilently() throws IOException {
+ Closeable closeable = mock(Closeable.class);
+ doThrow(new IOException()).when(closeable).close();
+ fileUtil.closeSilently(closeable);
+ }
+
+ @Test
+ public void shouldCopyStream() throws ImageCopyException {
+ File from = new File("src/test/resources/testFile1");
+ assertTrue(from.exists());
+ File to = new File(cacheDir.getAbsolutePath() + "testFile1");
+
+ fileUtil.copy(from, to);
+
+ FileAssert.assertBinaryEquals(from, to);
+ }
+
+ @Test(expected = ImageCopyException.class)
+ public void shouldCopyStreamFailWithImageCopyException() throws ImageCopyException {
+ File from = new File("src/test/resources/testFileThatDoesNotExists");
+ File to = new File(cacheDir.getAbsolutePath() + "testFile2");
+
+ fileUtil.copy(from, to);
+ }
+
+ @Test
+ public void shouldCopyStreams() throws FileNotFoundException {
+ File from = new File("src/test/resources/testFile1");
+ assertTrue(from.exists());
+ File to = new File(cacheDir.getAbsolutePath() + "testFile3");
+ InputStream in = new FileInputStream(from);
+ OutputStream out = new FileOutputStream(to);
+
+ fileUtil.copyStream(in, out);
+
+ FileAssert.assertBinaryEquals(from, to);
+ }
+
+ @Test
+ public void shouldCopyStreamsFailSilently() throws IOException {
+ InputStream from = mock(InputStream.class);
+ when(from.read(any(byte[].class))).thenThrow(new IOException());
+ File to = new File(cacheDir.getAbsolutePath() + "testFile3");
+ OutputStream out = new FileOutputStream(to);
+
+ fileUtil.copyStream(from, out);
+ }
+
+ @Test
+ public void shouldPrepareCacheDirectoryOnFileSystemIfIsMounted() {
+ AndroidFileContext fileContext = mock(AndroidFileContext.class);
+ when(fileContext.isMounted()).thenReturn(true);
+ when(fileContext.getPackageName()).thenReturn("com.something");
+ when(fileContext.getExternalStorageDirectory()).thenReturn(cacheDir);
+
+ fileUtil.prepareCacheDirectory(fileContext);
+
+ File expected = new File(cacheDir.getAbsoluteFile() + "/Android/data/com.something/cache/images");
+ assertTrue(expected.exists());
+ assertTrue(expected.isDirectory());
+ }
+
+ private File createFile(String name) {
+ return createFile(name, -1);
+ }
+
+ private File createFile(String name, long lastModified) {
+ try {
+ File f1 = new File(cacheDir, name + ".tmp");
+ FileUtils.write(f1, name);
+ if(lastModified != -1) {
+ f1.setLastModified(lastModified);
+ }
+ return f1;
+ } catch (Exception e) {
+ Assert.fail("Can't crete file for the test" + e.getMessage());
+ throw new RuntimeException("Can't crete file for the test " + e.getMessage());
+ }
+ }
+
+}
View
74 core/src/test/java/com/novoda/imageloader/core/loader/util/BitmapDisplayerTest.java
@@ -0,0 +1,74 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.novoda.imageloader.core.loader.util;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import android.graphics.Bitmap;
+
+import com.novoda.imageloader.core.model.ImageWrapper;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ Bitmap.class })
+public class BitmapDisplayerTest {
+
+ private BitmapDisplayer bitmapDisplayer;
+ private Bitmap bitmap;
+ private ImageWrapper imageWrapper;
+
+ @Before
+ public void beforeEachTest() {
+ bitmap = PowerMockito.mock(Bitmap.class);
+ imageWrapper = mock(ImageWrapper.class);
+ }
+
+ @Test
+ public void shouldNotSetNullBitmap() {
+ bitmapDisplayer = new BitmapDisplayer(null, imageWrapper);
+ bitmapDisplayer.run();
+
+ verify(imageWrapper, never()).setBitmap(null);
+ }
+
+ @Test
+ public void shouldNotSetBitmapIfTheUrlHasChanged() {
+ when(imageWrapper.isUrlChanged()).thenReturn(true);
+ bitmapDisplayer = new BitmapDisplayer(bitmap, imageWrapper);
+ bitmapDisplayer.run();
+
+ verify(imageWrapper, never()).setBitmap(null);
+ }
+
+ @Test
+ public void shouldSetBitmap() {
+ when(imageWrapper.isUrlChanged()).thenReturn(false);
+ bitmapDisplayer = new BitmapDisplayer(bitmap, imageWrapper);
+ bitmapDisplayer.run();
+
+ verify(imageWrapper).setBitmap(bitmap);
+ }
+
+}
View
19 core/src/test/java/com/novoda/imageloader/core/model/ImageTagFactoryTest.java
@@ -1,15 +1,30 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.novoda.imageloader.core.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
-import static org.mockito.Mockito.*;
import android.content.Context;
import android.view.Display;
-import android.view.WindowManager;
public class ImageTagFactoryTest {
View
24 core/src/test/java/com/novoda/imageloader/core/model/ImageWrapperTest.java
@@ -1,12 +1,28 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.novoda.imageloader.core.model;
-import org.junit.Before;
-import org.junit.Test;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
import android.widget.ImageView;
View
41 core/src/test/java/com/novoda/imageloader/core/network/UrlNetworlManagerTest.java
@@ -1,5 +1,29 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.novoda.imageloader.core.network;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
@@ -9,27 +33,21 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
-import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.mockito.Mockito.*;
-
import com.novoda.imageloader.core.LoaderSettings;
-import com.novoda.imageloader.core.Util;
import com.novoda.imageloader.core.exception.ImageNotFoundException;
+import com.novoda.imageloader.core.file.FileTestCase;
import com.novoda.imageloader.core.file.util.FileUtil;
-public class UrlNetworlManagerTest {
+public class UrlNetworlManagerTest extends FileTestCase {
private UrlNetworkManager urlNetworkManager;
private LoaderSettings loaderSettings;
private HttpURLConnection httpURLConnection;
private FileUtil fileUtil;
- private File cacheDir;
private File imageFile;
@Before
@@ -44,15 +62,14 @@ protected HttpURLConnection openConnection(String url) throws IOException, Malfo
return httpURLConnection;
}
};
- cacheDir = new File(Util.FOLDER_FOR_TEST_TMP_FILES);
- cacheDir.mkdirs();
+ createCacheDir();
imageFile = new File(cacheDir.getAbsolutePath() + "/test.jpg");
imageFile.createNewFile();
}
@After
- public void afterEachTest() throws IOException {
- FileUtils.deleteDirectory(cacheDir);
+ public void afterEachTest() {
+ deleteCacheDir();
}
@Test(expected = ImageNotFoundException.class)
View
34 core/src/test/java/com/novoda/imageloader/core/network/UrlUtilTest.java
@@ -22,22 +22,22 @@
import com.novoda.imageloader.core.network.UrlUtil;
public class UrlUtilTest {
-
- private UrlUtil urlUtil = new UrlUtil();
-
- @Test
- public void SHOULD_removeQueryFromUrlReturnNul_WHEN_urlIsNull() {
- assertEquals(null, urlUtil.removeQuery(null));
- }
-
- @Test
- public void SHOULD_removeQueryFromUrlReturnUnmodifiedUrl_WHEN_qeryNotPresent() {
- assertEquals("http://www.google.com", urlUtil.removeQuery("http://www.google.com"));
- }
-
- @Test
- public void SHOULD_removeQueryFromUrl() {
- assertEquals("http://www.google.com", urlUtil.removeQuery("http://www.google.com?q=test"));
- }
+
+ private UrlUtil urlUtil = new UrlUtil();
+
+ @Test
+ public void SHOULD_removeQueryFromUrlReturnNul_WHEN_urlIsNull() {
+ assertEquals(null, urlUtil.removeQuery(null));
+ }
+
+ @Test
+ public void SHOULD_removeQueryFromUrlReturnUnmodifiedUrl_WHEN_qeryNotPresent() {
+ assertEquals("http://www.google.com", urlUtil.removeQuery("http://www.google.com"));
+ }
+
+ @Test
+ public void SHOULD_removeQueryFromUrl() {
+ assertEquals("http://www.google.com", urlUtil.removeQuery("http://www.google.com?q=test"));
+ }
}
View
27 core/src/test/java/com/novoda/imageloader/core/util/DirectLoaderTest.java
@@ -1,3 +1,18 @@
+/**
+ * Copyright 2012 Novoda Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.novoda.imageloader.core.util;
import static org.junit.Assert.assertEquals;
@@ -6,26 +21,26 @@
import java.io.InputStream;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.rule.PowerMockRule;
+import org.powermock.modules.junit4.PowerMockRunner;
import android.graphics.Bitmap;
import com.novoda.imageloader.core.bitmap.BitmapUtil;
import com.novoda.imageloader.core.network.NetworkManager;
+@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bitmap.class })
public class DirectLoaderTest {
- @Rule public PowerMockRule rule = new PowerMockRule();
-
private DirectLoader directLoader;
private NetworkManager networkManager;
private BitmapUtil bitmapUtil;
+ private String url = "http://www.google.com";
@Before
public void beforeEachTest() {
@@ -46,14 +61,12 @@ public void shouldReturnNullIfUrlIsEmpty() {
@Test
public void shouldReturnNullIfIsNotPossibleToGetAnInputStreamFromNetwrokResource() {
- String url = "http://www.google.com";
Mockito.when(networkManager.retrieveInputStream(url)).thenReturn(null);
assertNull(directLoader.download(url));
}
-
+
@Test
public void shouldReturnBitmapFromHttpResource() {
- String url = "http://www.google.com";
InputStream is = Mockito.mock(InputStream.class);
Mockito.when(networkManager.retrieveInputStream(url)).thenReturn(is);
Bitmap expectedBitmap = PowerMockito.mock(Bitmap.class);
View
2  core/src/test/resources/testFile1
@@ -0,0 +1,2 @@
+this file is used by testing
+the content should be copied over to another file
Something went wrong with that request. Please try again.