OSDN Git Service

1c551b6e7b0849f31dcaaff18ca969325ce83bdb
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / parser / SpottedInputStreamTest.java
1 /*
2  */
3 package jp.sourceforge.mikutoga.parser;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.FilterInputStream;
7 import java.io.IOException;
8 import org.junit.After;
9 import org.junit.AfterClass;
10 import org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import static org.junit.Assert.*;
14
15 /**
16  *
17  */
18 public class SpottedInputStreamTest {
19
20     public SpottedInputStreamTest() {
21     }
22
23     @BeforeClass
24     public static void setUpClass() {
25     }
26
27     @AfterClass
28     public static void tearDownClass() {
29     }
30
31     @Before
32     public void setUp() {
33     }
34
35     @After
36     public void tearDown() {
37     }
38
39     /**
40      * Test of read method, of class SpottedInputStream.
41      */
42     @Test
43     public void testRead_0args() throws Exception {
44         System.out.println("read");
45
46         SpottedInputStream sis;
47         ByteArrayInputStream bis;
48
49         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02});
50         sis = new SpottedInputStream(bis);
51
52         int result;
53
54         result = sis.read();
55         assertEquals(0x01, result);
56
57         result = sis.read();
58         assertEquals(0x02, result);
59
60         result = sis.read();
61         assertEquals(-1, result);
62
63         return;
64     }
65
66     /**
67      * Test of read method, of class SpottedInputStream.
68      */
69     @Test
70     public void testRead_byteArr() throws Exception {
71         System.out.println("read");
72
73         SpottedInputStream sis;
74         ByteArrayInputStream bis;
75
76         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02, 0x03});
77         sis = new SpottedInputStream(bis);
78
79         byte[] buf = new byte[2];
80         int result;
81
82         result = sis.read(buf);
83         assertEquals(2, result);
84         assertEquals((byte)0x01, buf[0]);
85         assertEquals((byte)0x02, buf[1]);
86
87         result = sis.read(buf);
88         assertEquals(1, result);
89         assertEquals((byte)0x03, buf[0]);
90
91         result = sis.read(buf);
92         assertEquals(-1, result);
93
94         return;
95     }
96
97     /**
98      * Test of read method, of class SpottedInputStream.
99      */
100     @Test
101     public void testRead_3args() throws Exception {
102         System.out.println("read");
103
104
105         SpottedInputStream sis;
106         ByteArrayInputStream bis;
107
108         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02, 0x03});
109         sis = new SpottedInputStream(bis);
110
111         byte[] buf = new byte[3];
112         buf[0] = (byte)0xf1;
113         buf[1] = (byte)0xf2;
114         buf[2] = (byte)0xf3;
115         int result;
116
117         result = sis.read(buf, 1, 2);
118         assertEquals(2, result);
119         assertEquals((byte)0xf1, buf[0]);
120         assertEquals((byte)0x01, buf[1]);
121         assertEquals((byte)0x02, buf[2]);
122
123         result = sis.read(buf, 0, 1);
124         assertEquals(1, result);
125         assertEquals((byte)0x03, buf[0]);
126         assertEquals((byte)0x01, buf[1]);
127         assertEquals((byte)0x02, buf[2]);
128
129         result = sis.read(buf, 0, 1);
130         assertEquals(-1, result);
131
132         return;
133     }
134
135     /**
136      * Test of skip method, of class SpottedInputStream.
137      */
138     @Test
139     public void testSkip() throws Exception {
140         System.out.println("skip");
141
142         SpottedInputStream sis;
143         ByteArrayInputStream bis;
144
145         bis = new ByteArrayInputStream(new byte[]{0x11, 0x12, 0x13});
146         sis = new SpottedInputStream(bis);
147
148         int result;
149         long skipped;
150
151         result = sis.read();
152         assertEquals(0x11, result);
153
154         skipped = sis.skip(1L);
155         assertEquals(1L, skipped);
156
157         result = sis.read();
158         assertEquals(0x13, result);
159
160         skipped = sis.skip(1L);
161         assertEquals(0L, skipped);
162
163         return;
164     }
165
166     /**
167      * Test of close method, of class SpottedInputStream.
168      */
169     @Test
170     public void testClose() throws Exception {
171         System.out.println("close");
172
173         SpottedInputStream sis;
174         TestInputStream tis;
175
176         tis = new TestInputStream();
177         sis = new SpottedInputStream(tis);
178
179         assertFalse(tis.closed);
180         sis.close();
181         assertTrue(tis.closed);
182
183         return;
184     }
185
186     /**
187      * Test of getPosition method, of class SpottedInputStream.
188      */
189     @Test
190     public void testGetPosition() throws Exception{
191         System.out.println("getPosition");
192
193         SpottedInputStream sis;
194         ByteArrayInputStream bis;
195
196         bis = new ByteArrayInputStream(new byte[]{0x01,0x02});
197         sis = new SpottedInputStream(bis);
198
199         assertEquals(0L, sis.getPosition());
200
201         sis.read();
202         assertEquals(1L, sis.getPosition());
203
204         sis.read();
205         assertEquals(2L, sis.getPosition());
206
207         sis.read();
208         assertEquals(2L, sis.getPosition());
209
210         return;
211     }
212
213     /**
214      * Test of hasMore method, of class SpottedInputStream.
215      */
216     @Test
217     public void testHasMore() throws Exception {
218         System.out.println("hasMore");
219
220         SpottedInputStream sis;
221         ByteArrayInputStream bis;
222
223         bis = new ByteArrayInputStream(new byte[]{0x01,0x02});
224         sis = new SpottedInputStream(bis);
225
226         assertTrue(sis.hasMore());
227         sis.read();
228         assertTrue(sis.hasMore());
229         sis.read();
230         assertFalse(sis.hasMore());
231
232         return;
233     }
234
235     private static class TestInputStream extends FilterInputStream{
236         public boolean closed = false;
237         public boolean flushed = false;
238
239         TestInputStream(){
240             super(new ByteArrayInputStream(new byte[]{}));
241             return;
242         }
243
244         @Override
245         public void close() throws IOException {
246             super.close();
247             this.closed = true;
248             return;
249         }
250
251     }
252
253 }