소스 검색

Add unit test for StreamReaderUtil

Change-Id: I333d7a498862cc1dfb6dfe467f97cdaa3372b273
Reviewed-on: http://gerrit.dmdirc.com/4064
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/64/4064/3
Greg Holmes 9 년 전
부모
커밋
cc70fb7c1c
1개의 변경된 파일80개의 추가작업 그리고 0개의 파일을 삭제
  1. 80
    0
      test/com/dmdirc/util/io/StreamReaderTest.java

+ 80
- 0
test/com/dmdirc/util/io/StreamReaderTest.java 파일 보기

@@ -0,0 +1,80 @@
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.InputStream;
26
+import java.util.ArrayList;
27
+import java.util.List;
28
+
29
+import org.junit.Test;
30
+import org.junit.runner.RunWith;
31
+import org.mockito.runners.MockitoJUnitRunner;
32
+
33
+import static org.junit.Assert.assertEquals;
34
+import static org.junit.Assert.assertNull;
35
+import static org.junit.Assert.assertTrue;
36
+import static org.mockito.Matchers.any;
37
+import static org.mockito.Matchers.anyInt;
38
+import static org.mockito.Mockito.atLeastOnce;
39
+import static org.mockito.Mockito.spy;
40
+import static org.mockito.Mockito.verify;
41
+
42
+@RunWith(MockitoJUnitRunner.class)
43
+public class StreamReaderTest {
44
+
45
+    @Test
46
+    public void testGetListDiscarding() throws Exception {
47
+        final InputStream inputStream = spy(getClass().getResource("test5.txt").openStream());
48
+        final StreamReader streamReader = new StreamReader(inputStream);
49
+        assertNull(streamReader.getList());
50
+        streamReader.run();
51
+        assertNull(streamReader.getList());
52
+        verify(inputStream, atLeastOnce()).read(any(byte[].class), anyInt(), anyInt());
53
+    }
54
+
55
+    @Test
56
+    public void testGetListBuffer() throws Exception {
57
+        final StringBuffer stringBuffer = new StringBuffer();
58
+        final StreamReader streamReader = new StreamReader(getClass().getResource("test5.txt")
59
+                .openStream(),
60
+                stringBuffer);
61
+        assertNull(streamReader.getList());
62
+        streamReader.run();
63
+        assertNull(streamReader.getList());
64
+        assertEquals("Line 1\nLine 2\nLine 3", stringBuffer.toString());
65
+    }
66
+
67
+    @Test
68
+    public void testGetListList() throws Exception {
69
+        final List<String> list = new ArrayList<>();
70
+        final StreamReader streamReader = new StreamReader(getClass().getResource("test5.txt")
71
+                .openStream(), list);
72
+        assertTrue(streamReader.getList().isEmpty());
73
+        streamReader.run();
74
+        assertEquals(4, list.size());
75
+        assertEquals("", list.get(0));
76
+        assertEquals("Line 1", list.get(1));
77
+        assertEquals("Line 2", list.get(2));
78
+        assertEquals("Line 3", list.get(3));
79
+    }
80
+}

Loading…
취소
저장