improve test coverage partially fixing issue #25 #44

Merged
merged 1 commit into from Jun 26, 2012
View
12 core/pom.xml
@@ -42,6 +42,18 @@
<version>2.0.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>1.4.12</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>1.4.12</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
View
4 core/src/main/java/com/novoda/imageloader/core/ImageManager.java
@@ -27,7 +27,7 @@
import com.novoda.imageloader.core.loader.Loader;
import com.novoda.imageloader.core.loader.SimpleLoader;
import com.novoda.imageloader.core.network.NetworkManager;
-import com.novoda.imageloader.core.network.UrlNetworkLoader;
+import com.novoda.imageloader.core.network.UrlNetworkManager;
/**
* ImageManager has the responsibility to provide a
@@ -49,7 +49,7 @@ public ImageManager(Context context, LoaderSettings settings) {
this.loaderContext = new LoaderContext();
loaderContext.setSettings(settings);
loaderContext.setFileManager(new BasicFileManager(settings));
- loaderContext.setNetworkManager(new UrlNetworkLoader(settings));
+ loaderContext.setNetworkManager(new UrlNetworkManager(settings));
loaderContext.setResBitmapCache(new SoftMapCache());
cacheManager = settings.getCacheManager();
if (cacheManager == null) {
View
11 core/src/main/java/com/novoda/imageloader/core/model/ImageTagFactory.java
@@ -40,9 +40,7 @@ public ImageTagFactory(int width, int height, int defaultImageResId) {
}
public ImageTagFactory(Context context, int defaultImageResId) {
- Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- DisplayMetrics dm = new DisplayMetrics();
- d.getMetrics(dm);
+ Display d = prepareDisplay(context);
this.width = d.getWidth();
this.height = d.getHeight();
this.defaultImageResId = defaultImageResId;
@@ -78,5 +76,12 @@ public ImageTag build(String url) {
it.setPreviewWidth(previewImageWidth);
return it;
}
+
+ protected Display prepareDisplay(Context context) {
+ Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
+ DisplayMetrics dm = new DisplayMetrics();
+ d.getMetrics(dm);
+ return d;
+ }
}
View
6 core/src/main/java/com/novoda/imageloader/core/network/NetworkManager.java
@@ -18,6 +18,12 @@
import java.io.File;
import java.io.InputStream;
+/**
+ * A network manager is responsible of downloading images
+ * given an http resource of url string.
+ * At the moment requested resources can be retrieved as InputStream
+ * or saved directly to a file.
+ */
public interface NetworkManager {
void retrieveImage(String url, File f);
View
26 ...loader/core/network/UrlNetworkLoader.java → ...oader/core/network/UrlNetworkManager.java
@@ -18,22 +18,32 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
import java.net.URL;
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.exception.ImageNotFoundException;
import com.novoda.imageloader.core.file.util.FileUtil;
-public class UrlNetworkLoader implements NetworkManager {
+/**
+ * Basic implementation of the NetworkManager using URL connection.
+ */
+public class UrlNetworkManager implements NetworkManager {
- private FileUtil fileUtil = new FileUtil();
+ private FileUtil fileUtil;
private LoaderSettings settings;
- public UrlNetworkLoader(LoaderSettings settings) {
+ public UrlNetworkManager(LoaderSettings settings) {
+ this(settings, new FileUtil());
+ }
+
+ public UrlNetworkManager(LoaderSettings settings, FileUtil fileUtil) {
this.settings = settings;
+ this.fileUtil = fileUtil;
}
@Override
@@ -43,7 +53,7 @@ public void retrieveImage(String url, File f) {
HttpURLConnection conn = null;
applyChangeonSdkVersion(settings.getSdkVersion());
try {
- conn = (HttpURLConnection) new URL(url).openConnection();
+ conn = openConnection(url);
conn.setConnectTimeout(settings.getConnectionTimeout());
conn.setReadTimeout(settings.getReadTimeout());
is = conn.getInputStream();
@@ -61,12 +71,12 @@ public void retrieveImage(String url, File f) {
fileUtil.closeSilently(os);
}
}
-
+
@Override
public InputStream retrieveInputStream(String url) {
HttpURLConnection conn = null;
try {
- conn = (HttpURLConnection) new URL(url).openConnection();
+ conn = openConnection(url);
conn.setConnectTimeout(settings.getConnectionTimeout());
conn.setReadTimeout(settings.getReadTimeout());
return conn.getInputStream();
@@ -77,6 +87,10 @@ public InputStream retrieveInputStream(String url) {
return null;
}
}
+
+ protected HttpURLConnection openConnection(String url) throws IOException, MalformedURLException {
+ return (HttpURLConnection) new URL(url).openConnection();
+ }
private void applyChangeonSdkVersion(String sdkVersion) {
if (Integer.parseInt(sdkVersion) < 8) {
View
10 core/src/main/java/com/novoda/imageloader/core/util/DirectLoader.java
@@ -7,7 +7,7 @@
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.bitmap.BitmapUtil;
import com.novoda.imageloader.core.network.NetworkManager;
-import com.novoda.imageloader.core.network.UrlNetworkLoader;
+import com.novoda.imageloader.core.network.UrlNetworkManager;
/**
* Direct loader make use of the NetworkManager and the BitmapUtil
@@ -19,8 +19,12 @@
private BitmapUtil bitmapUtil;
public DirectLoader() {
- networkManager = new UrlNetworkLoader(new LoaderSettings());
- bitmapUtil = new BitmapUtil();
+ this(new UrlNetworkManager(new LoaderSettings()), new BitmapUtil());
+ }
+
+ public DirectLoader(NetworkManager networkManager, BitmapUtil bitmapUtil) {
+ this.networkManager = networkManager;
+ this.bitmapUtil = bitmapUtil;
}
public Bitmap download(String url) {
View
7 core/src/test/java/com/novoda/imageloader/core/file/util/FileUtilTest.java
@@ -0,0 +1,7 @@
+package com.novoda.imageloader.core.file.util;
+
+public class FileUtilTest {
+
+
+
+}
View
81 core/src/test/java/com/novoda/imageloader/core/model/ImageTagFactoryTest.java
@@ -0,0 +1,81 @@
+package com.novoda.imageloader.core.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+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 {
+
+ private ImageTagFactory imageTagFactory;
+ private int height = 12;
+ private int width = 9;
+ private int defaultResourceId = 1;
+ private String url = "google.com";
+
+ @Before
+ public void beforeEachTest() {
+ imageTagFactory = new ImageTagFactory(9, 12, 1);
+ }
+
+ @Test
+ public void shouldSetNormalPropertiesOnTheImageTag() {
+ ImageTag imageTag = imageTagFactory.build(url);
+
+ assertEquals(defaultResourceId, imageTag.getLoadingResourceId());
+ assertEquals(defaultResourceId, imageTag.getNotFoundResourceId());
+ assertEquals(height, imageTag.getHeight());
+ assertEquals(width, imageTag.getWidth());
+ assertEquals(url, imageTag.getUrl());
+ }
+
+ @Test
+ public void shouldSetPreviewProperties() {
+ int previewHeight = 1;
+ int previewWidth = 2;
+ imageTagFactory.usePreviewImage(previewWidth, previewHeight, true);
+ ImageTag imageTag = imageTagFactory.build(url);
+
+ assertEquals(previewHeight, imageTag.getPreviewHeight());
+ assertEquals(previewWidth, imageTag.getPreviewWidth());
+ }
+
+ @Test
+ public void shouldUseTheSameUrlForPreview() {
+ imageTagFactory.usePreviewImage(1, 1, true);
+ ImageTag imageTag = imageTagFactory.build(url);
+
+ assertEquals(url, imageTag.getPreviewUrl());
+ }
+
+ @Test
+ public void shouldNotUseTheSameUrlForPreview() {
+ imageTagFactory.usePreviewImage(1, 1, false);
+ ImageTag imageTag = imageTagFactory.build(url);
+
+ assertNull(imageTag.getPreviewUrl());
+ }
+
+ @Test
+ public void shouldUseDisplaySizes() {
+ final Display display = mock(Display.class);
+ when(display.getHeight()).thenReturn(21);
+ when(display.getWidth()).thenReturn(12);
+ imageTagFactory = new ImageTagFactory(null, 1) {
+ @Override
+ protected Display prepareDisplay(Context context) {
+ return display;
+ }
+ };
+ ImageTag imageTag = imageTagFactory.build(url);
+ assertEquals(21, imageTag.getHeight());
+ assertEquals(12, imageTag.getWidth());
+ }
+
+}
View
72 core/src/test/java/com/novoda/imageloader/core/model/ImageWrapperTest.java
@@ -0,0 +1,72 @@
+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 android.widget.ImageView;
+
+
+public class ImageWrapperTest {
+
+ private ImageWrapper imageWrapper;
+ private ImageView imageView;
+ private ImageTag imageTag;
+
+ @Before
+ public void beforeEachTest() {
+ imageView = mock(ImageView.class);
+ imageTag = mock(ImageTag.class);
+
+ }
+
+ @Test
+ public void shouldNotFailIfImageTagIsNull() {
+ imageWrapper = new ImageWrapper(imageView);
+ }
+
+ @Test
+ public void shouldSetLoadingResourceId() {
+ when(imageTag.getLoadingResourceId()).thenReturn(1);
+ when(imageView.getTag()).thenReturn(imageTag);
+
+ imageWrapper = new ImageWrapper(imageView);
+ assertEquals(1, imageWrapper.getLoadingResourceId());
+ }
+
+ @Test
+ public void shouldSetNotFoundResourceIdIfDefined() {
+ when(imageTag.getNotFoundResourceId()).thenReturn(2);
+ when(imageView.getTag()).thenReturn(imageTag);
+
+ imageWrapper = new ImageWrapper(imageView);
+ assertEquals(2, imageWrapper.getNotFoundResourceId());
+ }
+
+ @Test
+ public void shouldReturnTrueIfCurrentUrlHasChanged() {
+ when(imageTag.getUrl()).thenReturn("url1");
+ when(imageView.getTag()).thenReturn(imageTag);
+
+ imageWrapper = new ImageWrapper(imageView);
+ when(imageTag.getUrl()).thenReturn("url2");
+
+ assertTrue(imageWrapper.isUrlChanged());
+ }
+
+ @Test
+ public void shouldReturnFalseIfCurrentUrlHasNotChanged() {
+ when(imageTag.getUrl()).thenReturn("url1");
+ when(imageView.getTag()).thenReturn(imageTag);
+
+ imageWrapper = new ImageWrapper(imageView);
+ when(imageTag.getUrl()).thenReturn("url1");
+
+ assertFalse(imageWrapper.isUrlChanged());
+ }
+
+}
View
128 core/src/test/java/com/novoda/imageloader/core/network/UrlNetworlManagerTest.java
@@ -0,0 +1,128 @@
+package com.novoda.imageloader.core.network;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+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.util.FileUtil;
+
+public class UrlNetworlManagerTest {
+
+ private UrlNetworkManager urlNetworkManager;
+ private LoaderSettings loaderSettings;
+ private HttpURLConnection httpURLConnection;
+ private FileUtil fileUtil;
+ private File cacheDir;
+ private File imageFile;
+
+ @Before
+ public void beforeEachTest() throws IOException {
+ loaderSettings = mock(LoaderSettings.class);
+ fileUtil = mock(FileUtil.class);
+ when(loaderSettings.getSdkVersion()).thenReturn("8");
+ httpURLConnection = mock(HttpURLConnection.class);
+ urlNetworkManager = new UrlNetworkManager(loaderSettings, fileUtil) {
+ @Override
+ protected HttpURLConnection openConnection(String url) throws IOException, MalformedURLException {
+ return httpURLConnection;
+ }
+ };
+ cacheDir = new File(Util.FOLDER_FOR_TEST_TMP_FILES);
+ cacheDir.mkdirs();
+ imageFile = new File(cacheDir.getAbsolutePath() + "/test.jpg");
+ imageFile.createNewFile();
+ }
+
+ @After
+ public void afterEachTest() throws IOException {
+ FileUtils.deleteDirectory(cacheDir);
+ }
+
+ @Test(expected = ImageNotFoundException.class)
+ public void shouldThrowFileNotFoundExecptionIfFileDoesNotExists() throws IOException {
+ File imageFile = new File(cacheDir.getAbsolutePath() + "/");
+ imageFile.createNewFile();
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ }
+
+ @Test
+ public void shouldExecuteCopyStream() {
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ verify(fileUtil).copyStream(any(InputStream.class), any(FileOutputStream.class));
+ }
+
+ @Test
+ public void shouldAlwaysCallStreams() {
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ verify(fileUtil, atLeast(2)).closeSilently(any(Closeable.class));
+ }
+
+ @Test
+ public void shouldCallDisconnectIfDefinedInSettings() {
+ when(loaderSettings.getDisconnectOnEveryCall()).thenReturn(true);
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ verify(httpURLConnection).disconnect();
+ }
+
+ @Test
+ public void shouldAvoidToDisconnectIfNotDefinedInSettings() {
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ verify(httpURLConnection, never()).disconnect();
+ }
+
+ @Test
+ public void shouldFailGracefullyForUnknownExceptions() throws IOException {
+ when(httpURLConnection.getInputStream()).thenThrow(new IOException());
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ verify(httpURLConnection, never()).disconnect();
+ }
+
+ @Test
+ public void shouldSetKeepAliveSystemPropertyForApiLevelOlderThan8() {
+ System.setProperty("http.keepAlive", "true");
+ assertEquals("true", System.getProperty("http.keepAlive"));
+ when(loaderSettings.getSdkVersion()).thenReturn("4");
+
+ urlNetworkManager.retrieveImage("http://king.com", imageFile);
+ assertEquals("false", System.getProperty("http.keepAlive"));
+ }
+
+ @Test
+ public void shouldRetrieveInputStream() throws IOException {
+ InputStream expected = mock(InputStream.class);
+ when(httpURLConnection.getInputStream()).thenReturn(expected);
+ InputStream actual = urlNetworkManager.retrieveInputStream("http://king.com");
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void shouldRetrieveNullInputStreamForUnknownExceptions() throws IOException {
+ when(httpURLConnection.getInputStream()).thenThrow(new IOException());
+ InputStream actual = urlNetworkManager.retrieveInputStream("http://king.com");
+ assertNull(actual);
+ }
+
+ @Test(expected = ImageNotFoundException.class)
+ public void shouldThrowImageNotFoundExceptionForFileNotFoundException() throws IOException {
+ when(httpURLConnection.getInputStream()).thenThrow(new FileNotFoundException());
+ urlNetworkManager.retrieveInputStream("http://king.com");
+ }
+
+}
View
2 ...da/imageloader/core/util/UrlUtilTest.java → ...imageloader/core/network/UrlUtilTest.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.novoda.imageloader.core.util;
+package com.novoda.imageloader.core.network;
import static org.junit.Assert.assertEquals;
View
64 core/src/test/java/com/novoda/imageloader/core/util/DirectLoaderTest.java
@@ -0,0 +1,64 @@
+package com.novoda.imageloader.core.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.io.InputStream;
+
+import org.junit.Before;
+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.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 {
+
+ private DirectLoader directLoader;
+ private NetworkManager networkManager;
+ private BitmapUtil bitmapUtil;
+
+ @Before
+ public void beforeEachTest() {
+ networkManager = Mockito.mock(NetworkManager.class);
+ bitmapUtil = Mockito.mock(BitmapUtil.class);
+ directLoader = new DirectLoader(networkManager, bitmapUtil);
+ }
+
+ @Test
+ public void shouldReturnNullIfUrlIsNull() {
+ assertNull(directLoader.download(null));
+ }
+
+ @Test
+ public void shouldReturnNullIfUrlIsEmpty() {
+ assertNull(directLoader.download(""));
+ }
+
+ @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);
+ PowerMockito.when(bitmapUtil.decodeInputStream(is)).thenReturn(expectedBitmap);
+ Bitmap actualBitmap = directLoader.download(url);
+ assertEquals(expectedBitmap, actualBitmap);
+ }
+
+}