Переглянути джерело

Merge pull request #24 from greboid/test

Add a test for ListenerInputStream.
pull/25/head
Chris Smith 9 роки тому
джерело
коміт
5b027c8d0f
1 змінених файлів з 166 додано та 0 видалено
  1. 166
    0
      test/com/dmdirc/util/io/ListenerInputStreamTest.java

+ 166
- 0
test/com/dmdirc/util/io/ListenerInputStreamTest.java Переглянути файл

@@ -0,0 +1,166 @@
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 java.io.IOException;
26
+import java.io.InputStream;
27
+
28
+import javax.annotation.Nonnull;
29
+
30
+import org.junit.Before;
31
+import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
35
+
36
+import static junit.framework.TestCase.assertEquals;
37
+import static junit.framework.TestCase.assertTrue;
38
+import static org.mockito.Mockito.never;
39
+import static org.mockito.Mockito.verify;
40
+
41
+@RunWith(MockitoJUnitRunner.class)
42
+public class ListenerInputStreamTest {
43
+
44
+    @Mock private DownloadListener listener;
45
+
46
+    private ListenerInputStream instance;
47
+
48
+    @Before
49
+    public void setUp() throws Exception {
50
+        instance = new ListenerInputStream(new MockedInputStream(), listener, 100);
51
+    }
52
+
53
+    @Test
54
+    public void testInit_Indeterminate() throws Exception {
55
+        instance = new ListenerInputStream(new MockedInputStream(), listener, -1);
56
+        assertEquals(2, instance.read());
57
+        verify(listener, never()).downloadProgress(2);
58
+        verify(listener).setIndeterminate(true);
59
+    }
60
+
61
+    @Test
62
+    public void testInit_Determinate() throws Exception {
63
+        assertEquals(2, instance.read());
64
+        verify(listener).downloadProgress(2);
65
+        verify(listener).setIndeterminate(false);
66
+    }
67
+
68
+    @Test
69
+    public void testRead() throws Exception {
70
+        assertEquals(2, instance.read());
71
+        verify(listener).downloadProgress(2);
72
+    }
73
+
74
+    @Test
75
+    public void testRead1() throws Exception {
76
+        assertEquals(4, instance.read(new byte[6]));
77
+        verify(listener).downloadProgress(4);
78
+    }
79
+
80
+    @Test
81
+    public void testRead2() throws Exception {
82
+        assertEquals(6, instance.read(new byte[8], 0, 8));
83
+        verify(listener).downloadProgress(6);
84
+    }
85
+
86
+    @Test
87
+    public void testSkip() throws Exception {
88
+        assertEquals(8, instance.skip(4));
89
+        verify(listener).downloadProgress(8);
90
+    }
91
+
92
+    @Test
93
+    public void testAvailable() throws Exception {
94
+        assertEquals(10, instance.available());
95
+    }
96
+
97
+    @Test(expected = IOException.class)
98
+    public void testClose() throws Exception {
99
+        instance.close();
100
+    }
101
+
102
+    @Test(expected = IOException.class)
103
+    public void testMarkAndReset_Unsupported() throws Exception {
104
+        instance.mark(0);
105
+        instance.reset();
106
+    }
107
+
108
+    @Test
109
+    public void testMarkSupported() throws Exception {
110
+        assertTrue(instance.markSupported());
111
+    }
112
+
113
+    private static class MockedInputStream extends InputStream {
114
+
115
+        public MockedInputStream() {
116
+        }
117
+
118
+        @Override
119
+        public int read() throws IOException {
120
+            return 2;
121
+        }
122
+
123
+        @Override
124
+        public int read(@Nonnull final byte[] b) throws IOException {
125
+            return 4;
126
+        }
127
+
128
+        @Override
129
+        public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
130
+            if (off == 0 && len == 6) {
131
+                return 4;
132
+            } else {
133
+                return 6;
134
+            }
135
+        }
136
+
137
+        @Override
138
+        public long skip(final long n) throws IOException {
139
+            return 8;
140
+        }
141
+
142
+        @Override
143
+        public int available() throws IOException {
144
+            return 10;
145
+        }
146
+
147
+        @Override
148
+        public void close() throws IOException {
149
+            throw new IOException("Test");
150
+        }
151
+
152
+        @Override
153
+        public synchronized void mark(final int readlimit) {
154
+        }
155
+
156
+        @Override
157
+        public synchronized void reset() throws IOException {
158
+            throw new IOException("Test");
159
+        }
160
+
161
+        @Override
162
+        public boolean markSupported() {
163
+            return true;
164
+        }
165
+    }
166
+}

Завантаження…
Відмінити
Зберегти