Browse Source

Unit test for Downloader

Change-Id: I3b054eb2144f51bc0b5935ee24525616dc042a4e
Reviewed-on: http://gerrit.dmdirc.com/4047
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/47/4047/4
Greg Holmes 9 years ago
parent
commit
aad1578811
3 changed files with 184 additions and 14 deletions
  1. 1
    0
      build.gradle
  2. 45
    14
      src/com/dmdirc/util/io/Downloader.java
  3. 138
    0
      test/com/dmdirc/util/io/DownloaderTest.java

+ 1
- 0
build.gradle View File

@@ -29,6 +29,7 @@ repositories.mavenCentral()
29 29
 dependencies {
30 30
     testCompile group: 'junit', name: 'junit', version: '4.11'
31 31
     testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
32
+    testCompile group: 'com.google.jimfs', name: 'jimfs', version: '1.0'
32 33
 }
33 34
 
34 35
 publishing {

+ 45
- 14
src/com/dmdirc/util/io/Downloader.java View File

@@ -24,8 +24,6 @@ package com.dmdirc.util.io;
24 24
 
25 25
 import java.io.BufferedReader;
26 26
 import java.io.DataOutputStream;
27
-import java.io.File;
28
-import java.io.FileOutputStream;
29 27
 import java.io.IOException;
30 28
 import java.io.InputStream;
31 29
 import java.io.InputStreamReader;
@@ -33,6 +31,9 @@ import java.io.OutputStream;
33 31
 import java.net.URL;
34 32
 import java.net.URLConnection;
35 33
 import java.net.URLEncoder;
34
+import java.nio.file.Files;
35
+import java.nio.file.Path;
36
+import java.nio.file.Paths;
36 37
 import java.util.ArrayList;
37 38
 import java.util.List;
38 39
 import java.util.Map;
@@ -47,7 +48,7 @@ public class Downloader {
47 48
      *
48 49
      * @param url The URL to retrieve
49 50
      * @return A list of lines received from the server
50
-     * @throws java.io.IOException If there's an I/O error while downloading
51
+     * @throws IOException If there's an I/O error while downloading
51 52
      */
52 53
     public List<String> getPage(final String url) throws IOException {
53 54
 
@@ -60,7 +61,7 @@ public class Downloader {
60 61
      * @param url The URL to retrieve
61 62
      * @param postData The raw POST data to send
62 63
      * @return A list of lines received from the server
63
-     * @throws java.io.IOException If there's an I/O error while downloading
64
+     * @throws IOException If there's an I/O error while downloading
64 65
      */
65 66
     public List<String> getPage(final String url, final String postData)
66 67
             throws IOException {
@@ -90,7 +91,7 @@ public class Downloader {
90 91
      * @param url The URL to retrieve
91 92
      * @param postData A map of post data that should be sent
92 93
      * @return A list of lines received from the server
93
-     * @throws java.io.IOException If there's an I/O error while downloading
94
+     * @throws IOException If there's an I/O error while downloading
94 95
      */
95 96
     public List<String> getPage(final String url,
96 97
             final Map<String, String> postData) throws IOException {
@@ -111,31 +112,56 @@ public class Downloader {
111 112
      *
112 113
      * @param url The URL to retrieve
113 114
      * @param file The file to save the page to
114
-     * @throws java.io.IOException If there's an I/O error while downloading
115
+     * @throws IOException If there's an I/O error while downloading
115 116
      */
116
-    public void downloadPage(final String url, final String file)
117
+    public void downloadPage(final String url, final Path file)
117 118
             throws IOException {
118 119
         downloadPage(url, file, null);
119 120
     }
120 121
 
122
+    /**
123
+     * Downloads the specified page to disk.
124
+     *
125
+     * @param url The URL to retrieve
126
+     * @param file The file to save the page to
127
+     * @throws IOException If there's an I/O error while downloading
128
+     */
129
+    @Deprecated
130
+    public void downloadPage(final String url, final String file)
131
+            throws IOException {
132
+        downloadPage(url, Paths.get(file), null);
133
+    }
134
+
121 135
     /**
122 136
      * Downloads the specified page to disk.
123 137
      *
124 138
      * @param url The URL to retrieve
125 139
      * @param file The file to save the page to
126 140
      * @param listener The progress listener for this download
127
-     * @throws java.io.IOException If there's an I/O error while downloading
141
+     * @throws IOException If there's an I/O error while downloading
128 142
      */
143
+    @Deprecated
129 144
     public void downloadPage(final String url, final String file,
130 145
             final DownloadListener listener) throws IOException {
146
+        downloadPage(url, Paths.get(file), listener);
147
+    }
148
+
149
+    /**
150
+     * Downloads the specified page to disk.
151
+     *
152
+     * @param url The URL to retrieve
153
+     * @param file The file to save the page to
154
+     * @param listener The progress listener for this download
155
+     * @throws IOException If there's an I/O error while downloading
156
+     */
157
+    public void downloadPage(final String url, final Path file,
158
+            final DownloadListener listener) throws IOException {
131 159
 
132 160
         final URLConnection urlConn = getConnection(url, "");
133
-        final File myFile = new File(file);
134 161
 
135
-        try (OutputStream output = new FileOutputStream(myFile);
162
+        try (OutputStream output = Files.newOutputStream(file);
136 163
                 InputStream input = urlConn.getInputStream()) {
137 164
             final int length = urlConn.getContentLength();
138
-            int current = 0;
139 165
 
140 166
             if (listener != null) {
141 167
                 listener.setIndeterminate(length == -1);
@@ -144,6 +170,7 @@ public class Downloader {
144 170
             final byte[] buffer = new byte[512];
145 171
             int count;
146 172
 
173
+            int current = 0;
147 174
             do {
148 175
                 count = input.read(buffer);
149 176
 
@@ -166,13 +193,12 @@ public class Downloader {
166 193
      * @param url The URL to connect to
167 194
      * @param postData The POST data to pass to the URL
168 195
      * @return An URLConnection for the specified URL/data
169
-     * @throws java.io.IOException If an I/O exception occurs while connecting
196
+     * @throws IOException If an I/O exception occurs while connecting
170 197
      */
171 198
     private URLConnection getConnection(final String url,
172 199
             final String postData)
173 200
             throws IOException {
174
-        final URL myUrl = new URL(url);
175
-        final URLConnection urlConn = myUrl.openConnection();
201
+        final URLConnection urlConn = getURLConnection(url);
176 202
 
177 203
         urlConn.setUseCaches(false);
178 204
         urlConn.setDoInput(true);
@@ -192,4 +218,9 @@ public class Downloader {
192 218
         return urlConn;
193 219
     }
194 220
 
221
+    protected URLConnection getURLConnection(final String url) throws IOException {
222
+        final URL myUrl = new URL(url);
223
+        return myUrl.openConnection();
224
+    }
225
+
195 226
 }

+ 138
- 0
test/com/dmdirc/util/io/DownloaderTest.java View File

@@ -0,0 +1,138 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.io;
24
+
25
+import com.google.common.collect.Lists;
26
+import com.google.common.collect.Maps;
27
+import com.google.common.jimfs.Configuration;
28
+import com.google.common.jimfs.Jimfs;
29
+
30
+import java.io.ByteArrayInputStream;
31
+import java.io.IOException;
32
+import java.io.OutputStream;
33
+import java.net.URLConnection;
34
+import java.nio.file.FileSystem;
35
+import java.nio.file.Files;
36
+import java.nio.file.Path;
37
+import java.util.Map;
38
+
39
+import org.junit.Before;
40
+import org.junit.Test;
41
+import org.junit.runner.RunWith;
42
+import org.mockito.InOrder;
43
+import org.mockito.Mock;
44
+import org.mockito.runners.MockitoJUnitRunner;
45
+
46
+import static org.junit.Assert.assertEquals;
47
+import static org.junit.Assert.assertFalse;
48
+import static org.junit.Assert.assertTrue;
49
+import static org.mockito.Matchers.anyBoolean;
50
+import static org.mockito.Matchers.anyByte;
51
+import static org.mockito.Matchers.anyInt;
52
+import static org.mockito.Mockito.atLeastOnce;
53
+import static org.mockito.Mockito.inOrder;
54
+import static org.mockito.Mockito.never;
55
+import static org.mockito.Mockito.verify;
56
+import static org.mockito.Mockito.when;
57
+
58
+@RunWith(MockitoJUnitRunner.class)
59
+public class DownloaderTest {
60
+
61
+    @Mock private URLConnection mockedConnection;
62
+    @Mock private OutputStream os;
63
+    @Mock private DownloadListener listener;
64
+    private FileSystem fakeFS;
65
+
66
+    @Before
67
+    public void setup() throws IOException {
68
+        fakeFS = Jimfs.newFileSystem(Configuration.unix());
69
+        ByteArrayInputStream is = new ByteArrayInputStream(
70
+                "OMG IM A FAKE DOWNLOAD".getBytes("UTF-8"));
71
+        when(mockedConnection.getInputStream()).thenReturn(is);
72
+        when(mockedConnection.getOutputStream()).thenReturn(os);
73
+        when(mockedConnection.getLastModified()).thenReturn((Long)10L, (Long)11L);
74
+    }
75
+
76
+    @Test
77
+    public void testGetPage() throws IOException {
78
+        final String postData = "sdfgsdfgsdfg";
79
+        new TestableDownloader().getPage("rar");
80
+        verify(mockedConnection, never()).setRequestProperty("Content-Type",
81
+                "application/x-www-form-urlencoded");
82
+        verify(os, never()).write(anyByte());
83
+    }
84
+
85
+    @Test
86
+    public void testGetPageString() throws IOException {
87
+        final String postData = "sdfgsdfgsdfg";
88
+        new TestableDownloader().getPage("rar", postData);
89
+        verify(mockedConnection).setRequestProperty("Content-Type",
90
+                "application/x-www-form-urlencoded");
91
+        final InOrder order = inOrder(os);
92
+        for (int i=0; i < postData.length(); i++) {
93
+            order.verify(os).write((byte) postData.charAt(i));
94
+        }
95
+    }
96
+
97
+    @Test
98
+    public void testGetPageMap() throws IOException {
99
+        final Map<String, String> postData = Maps.newHashMap();
100
+        postData.put("key1", "value1");
101
+        postData.put("key2", "value2");
102
+        final String postDataString = "key1=value1&key2=value2";
103
+        new TestableDownloader().getPage("rar", postData);
104
+        verify(mockedConnection).setRequestProperty("Content-Type",
105
+                "application/x-www-form-urlencoded");
106
+        final InOrder order = inOrder(os);
107
+        for (int i=0; i < postDataString.length(); i++) {
108
+            order.verify(os).write(postDataString.charAt(i));
109
+        }
110
+    }
111
+
112
+    @Test
113
+    public void testDownloadPage() throws IOException {
114
+        final Path file = fakeFS.getPath("test.txt");
115
+        assertFalse(Files.exists(file));
116
+        new TestableDownloader().downloadPage("rar", file);
117
+        assertTrue(Files.exists(file));
118
+        assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file));
119
+    }
120
+
121
+    @Test
122
+    public void testListener() throws IOException {
123
+        final Path file = fakeFS.getPath("test.txt");
124
+        assertFalse(Files.exists(file));
125
+        new TestableDownloader().downloadPage("rar", file, listener);
126
+        assertTrue(Files.exists(file));
127
+        assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file));
128
+        verify(listener).setIndeterminate(anyBoolean());
129
+        verify(listener, atLeastOnce()).downloadProgress(anyInt());
130
+    }
131
+
132
+    private class TestableDownloader extends Downloader {
133
+        protected URLConnection getURLConnection(final String url) throws IOException {
134
+            return mockedConnection;
135
+        }
136
+    }
137
+
138
+}

Loading…
Cancel
Save