|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.utils; |
|
20 |
|
21 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
22 import com.nokia.mj.impl.utils.BufferedReader; |
|
23 |
|
24 import java.io.ByteArrayInputStream; |
|
25 import java.io.InputStreamReader; |
|
26 import java.io.IOException; |
|
27 |
|
28 import j2meunit.framework.Test; |
|
29 import j2meunit.framework.TestCase; |
|
30 import j2meunit.framework.TestMethod; |
|
31 import j2meunit.framework.TestSuite; |
|
32 |
|
33 /** |
|
34 * BufferedReader unit tests. |
|
35 */ |
|
36 public class BufferedReaderTests extends TestCase implements InstallerMain |
|
37 { |
|
38 |
|
39 private static final String TEST_DATA = "one\ntwo\nthree"; |
|
40 private static final int MAX_BUF_SIZE = 16; |
|
41 |
|
42 // Begin j2meunit test framework setup |
|
43 public void installerMain(String[] args) |
|
44 { |
|
45 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
46 |
|
47 suite.addTest(new BufferedReaderTests("testMark", new TestMethod() |
|
48 { |
|
49 public void run(TestCase tc) |
|
50 { |
|
51 ((BufferedReaderTests)tc).testMark(); |
|
52 } |
|
53 })); |
|
54 suite.addTest(new BufferedReaderTests("testRead", new TestMethod() |
|
55 { |
|
56 public void run(TestCase tc) |
|
57 { |
|
58 ((BufferedReaderTests)tc).testRead(); |
|
59 } |
|
60 })); |
|
61 suite.addTest(new BufferedReaderTests("testRead2", new TestMethod() |
|
62 { |
|
63 public void run(TestCase tc) |
|
64 { |
|
65 ((BufferedReaderTests)tc).testRead2(); |
|
66 } |
|
67 })); |
|
68 suite.addTest(new BufferedReaderTests("testReadLine", new TestMethod() |
|
69 { |
|
70 public void run(TestCase tc) |
|
71 { |
|
72 ((BufferedReaderTests)tc).testReadLine(); |
|
73 } |
|
74 })); |
|
75 suite.addTest(new BufferedReaderTests("testSkip", new TestMethod() |
|
76 { |
|
77 public void run(TestCase tc) |
|
78 { |
|
79 ((BufferedReaderTests)tc).testSkip(); |
|
80 } |
|
81 })); |
|
82 |
|
83 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
84 } |
|
85 |
|
86 public BufferedReaderTests() |
|
87 { |
|
88 } |
|
89 |
|
90 public BufferedReaderTests(String aTestName, TestMethod aTestMethod) |
|
91 { |
|
92 super(aTestName, aTestMethod); |
|
93 } |
|
94 |
|
95 public void assertFalse(String aMsg, boolean aCondition) |
|
96 { |
|
97 assertTrue(aMsg, !aCondition); |
|
98 } |
|
99 |
|
100 // End j2meunit test framework setup |
|
101 |
|
102 protected void setUp() |
|
103 { |
|
104 } |
|
105 |
|
106 protected void tearDown() |
|
107 { |
|
108 } |
|
109 |
|
110 public void testMark() |
|
111 { |
|
112 BufferedReader r = null; |
|
113 try |
|
114 { |
|
115 r = new BufferedReader( |
|
116 new InputStreamReader( |
|
117 new ByteArrayInputStream(TEST_DATA.getBytes()))); |
|
118 assertTrue("r.markSupported() != false", |
|
119 r.markSupported() == false); |
|
120 try |
|
121 { |
|
122 r.mark(10); |
|
123 assertTrue("mark didn't throw exception", false); |
|
124 } |
|
125 catch (IOException ioe) |
|
126 { |
|
127 } |
|
128 catch (Throwable t) |
|
129 { |
|
130 assertTrue("mark threw wrong exception: " + t.toString(), |
|
131 false); |
|
132 } |
|
133 try |
|
134 { |
|
135 r.reset(); |
|
136 assertTrue("reset didn't throw exception", false); |
|
137 } |
|
138 catch (IOException ioe) |
|
139 { |
|
140 } |
|
141 catch (Throwable t) |
|
142 { |
|
143 assertTrue("reset threw wrong exception: " + t.toString(), |
|
144 false); |
|
145 } |
|
146 } |
|
147 catch (Throwable t) |
|
148 { |
|
149 assertTrue(t.toString(), false); |
|
150 t.printStackTrace(); |
|
151 } |
|
152 finally |
|
153 { |
|
154 if (r != null) |
|
155 { |
|
156 try |
|
157 { |
|
158 r.close(); |
|
159 } |
|
160 catch (IOException ioe) |
|
161 { |
|
162 } |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 public void testRead() |
|
168 { |
|
169 for (int i = 1; i < MAX_BUF_SIZE; i++) |
|
170 { |
|
171 testRead(i); |
|
172 } |
|
173 } |
|
174 |
|
175 private void testRead(int aBufSize) |
|
176 { |
|
177 BufferedReader r = null; |
|
178 try |
|
179 { |
|
180 r = new BufferedReader( |
|
181 new InputStreamReader( |
|
182 new ByteArrayInputStream(TEST_DATA.getBytes()))); |
|
183 try |
|
184 { |
|
185 for (int i = 0; r.ready(); i++) |
|
186 { |
|
187 char c = (char)r.read(); |
|
188 assertTrue("incorrect char '" + c + "' at index " + i + |
|
189 ", bufSize=" + aBufSize, |
|
190 TEST_DATA.charAt(i) == c); |
|
191 } |
|
192 int eos = r.read(); |
|
193 assertTrue("read: incorrect end of stream result: " + eos + |
|
194 ", bufSize=" + aBufSize, eos == -1); |
|
195 } |
|
196 catch (IOException ioe) |
|
197 { |
|
198 assertTrue("read threw exception: " + ioe.toString(), |
|
199 false); |
|
200 } |
|
201 } |
|
202 catch (Throwable t) |
|
203 { |
|
204 assertTrue(t.toString(), false); |
|
205 t.printStackTrace(); |
|
206 } |
|
207 finally |
|
208 { |
|
209 if (r != null) |
|
210 { |
|
211 try |
|
212 { |
|
213 r.close(); |
|
214 } |
|
215 catch (IOException ioe) |
|
216 { |
|
217 } |
|
218 } |
|
219 } |
|
220 } |
|
221 |
|
222 public void testRead2() |
|
223 { |
|
224 for (int i = 1; i < MAX_BUF_SIZE; i++) |
|
225 { |
|
226 testRead2(i); |
|
227 } |
|
228 } |
|
229 |
|
230 private void testRead2(int aBufSize) |
|
231 { |
|
232 BufferedReader r = null; |
|
233 try |
|
234 { |
|
235 r = new BufferedReader( |
|
236 new InputStreamReader( |
|
237 new ByteArrayInputStream(TEST_DATA.getBytes()))); |
|
238 try |
|
239 { |
|
240 char[] buf = new char[5]; |
|
241 for (int i = 0; r.ready(); i++) |
|
242 { |
|
243 int count = r.read(buf); |
|
244 String line = new String(buf); |
|
245 String msg = "incorrect chars '" + line + |
|
246 "', i=" + i + ", bufSize=" + aBufSize; |
|
247 switch (i) |
|
248 { |
|
249 case 0: |
|
250 assertTrue(msg, count == 5 && line.equals("one\nt")); |
|
251 break; |
|
252 case 1: |
|
253 assertTrue(msg, count == 5 && line.equals("wo\nth")); |
|
254 break; |
|
255 case 2: |
|
256 assertTrue(msg, count == 3 && line.equals("reeth")); |
|
257 break; |
|
258 default: |
|
259 assertTrue("incorrect number of reads" + |
|
260 ", bufSize=" + aBufSize, false); |
|
261 } |
|
262 } |
|
263 int eos = r.read(); |
|
264 assertTrue("read: incorrect end of stream result: " + eos + |
|
265 ", bufSize=" + aBufSize, eos == -1); |
|
266 } |
|
267 catch (IOException ioe) |
|
268 { |
|
269 assertTrue("read threw exception: " + ioe.toString(), |
|
270 false); |
|
271 } |
|
272 } |
|
273 catch (Throwable t) |
|
274 { |
|
275 assertTrue(t.toString(), false); |
|
276 t.printStackTrace(); |
|
277 } |
|
278 finally |
|
279 { |
|
280 if (r != null) |
|
281 { |
|
282 try |
|
283 { |
|
284 r.close(); |
|
285 } |
|
286 catch (IOException ioe) |
|
287 { |
|
288 } |
|
289 } |
|
290 } |
|
291 } |
|
292 |
|
293 public void testReadLine() |
|
294 { |
|
295 for (int i = 1; i < MAX_BUF_SIZE; i++) |
|
296 { |
|
297 testReadLine(i); |
|
298 } |
|
299 } |
|
300 |
|
301 private void testReadLine(int aBufSize) |
|
302 { |
|
303 BufferedReader r = null; |
|
304 try |
|
305 { |
|
306 r = new BufferedReader( |
|
307 new InputStreamReader( |
|
308 new ByteArrayInputStream(TEST_DATA.getBytes()))); |
|
309 try |
|
310 { |
|
311 for (int i = 0; r.ready(); i++) |
|
312 { |
|
313 String line = r.readLine(); |
|
314 String msg = "incorrect line " + i + ": '" + line + |
|
315 "', bufSize=" + aBufSize; |
|
316 switch (i) |
|
317 { |
|
318 case 0: |
|
319 assertTrue(msg, line.equals("one")); |
|
320 break; |
|
321 case 1: |
|
322 assertTrue(msg, line.equals("two")); |
|
323 break; |
|
324 case 2: |
|
325 assertTrue(msg, line.equals("three")); |
|
326 break; |
|
327 default: |
|
328 assertTrue("incorrect number of lines" + |
|
329 ", bufSize=" + aBufSize, false); |
|
330 } |
|
331 } |
|
332 String eos = r.readLine(); |
|
333 assertTrue("readline: incorrect end of stream result: '" + eos + |
|
334 "', bufSize=" + aBufSize, eos == null); |
|
335 } |
|
336 catch (IOException ioe) |
|
337 { |
|
338 assertTrue("readLine threw exception: " + ioe.toString(), |
|
339 false); |
|
340 } |
|
341 } |
|
342 catch (Throwable t) |
|
343 { |
|
344 assertTrue(t.toString(), false); |
|
345 t.printStackTrace(); |
|
346 } |
|
347 finally |
|
348 { |
|
349 if (r != null) |
|
350 { |
|
351 try |
|
352 { |
|
353 r.close(); |
|
354 } |
|
355 catch (IOException ioe) |
|
356 { |
|
357 } |
|
358 } |
|
359 } |
|
360 } |
|
361 |
|
362 public void testSkip() |
|
363 { |
|
364 for (int bufSize=1; bufSize<MAX_BUF_SIZE; bufSize++) |
|
365 { |
|
366 for (int skipAmount=0; skipAmount<MAX_BUF_SIZE+2; skipAmount++) |
|
367 { |
|
368 testSkip(bufSize, skipAmount); |
|
369 } |
|
370 } |
|
371 } |
|
372 |
|
373 private void testSkip(int aBufSize, int aSkipAmount) |
|
374 { |
|
375 BufferedReader r = null; |
|
376 try |
|
377 { |
|
378 r = new BufferedReader( |
|
379 new InputStreamReader( |
|
380 new ByteArrayInputStream(TEST_DATA.getBytes()))); |
|
381 try |
|
382 { |
|
383 for (int i = 0; r.ready(); i++) |
|
384 { |
|
385 char c = (char)r.read(); |
|
386 assertTrue("incorrect char '" + c + "' at index " + i + |
|
387 ", bufSize=" + aBufSize + |
|
388 ", skipAmount=" + aSkipAmount, |
|
389 TEST_DATA.charAt(i) == c); |
|
390 r.skip(aSkipAmount); |
|
391 i += aSkipAmount; |
|
392 } |
|
393 long eos = r.skip(1); |
|
394 assertTrue("skip: incorrect end of stream result: " + eos + |
|
395 ", bufSize=" + aBufSize, eos == 0); |
|
396 } |
|
397 catch (IOException ioe) |
|
398 { |
|
399 assertTrue("Unexpected exception: " + ioe.toString(), |
|
400 false); |
|
401 } |
|
402 } |
|
403 catch (Throwable t) |
|
404 { |
|
405 assertTrue(t.toString(), false); |
|
406 t.printStackTrace(); |
|
407 } |
|
408 finally |
|
409 { |
|
410 if (r != null) |
|
411 { |
|
412 try |
|
413 { |
|
414 r.close(); |
|
415 } |
|
416 catch (IOException ioe) |
|
417 { |
|
418 } |
|
419 } |
|
420 } |
|
421 } |
|
422 } |