001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.BufferedReader;
021: import java.io.ByteArrayInputStream;
022: import java.io.CharArrayReader;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.PipedReader;
026: import java.io.Reader;
027: import java.io.StringReader;
028:
029: import tests.support.Support_StringReader;
030:
031: public class BufferedReaderTest extends junit.framework.TestCase {
032:
033: BufferedReader br;
034:
035: String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
036:
037: /**
038: * @tests java.io.BufferedReader#BufferedReader(java.io.Reader)
039: */
040: public void test_ConstructorLjava_io_Reader() {
041: // Test for method java.io.BufferedReader(java.io.Reader)
042: assertTrue("Used in tests", true);
043: }
044:
045: /**
046: * @tests java.io.BufferedReader#BufferedReader(java.io.Reader, int)
047: */
048: public void test_ConstructorLjava_io_ReaderI() {
049: // Test for method java.io.BufferedReader(java.io.Reader, int)
050: assertTrue("Used in tests", true);
051: }
052:
053: /**
054: * @tests java.io.BufferedReader#close()
055: */
056: public void test_close() {
057: // Test for method void java.io.BufferedReader.close()
058: try {
059: br = new BufferedReader(
060: new Support_StringReader(testString));
061: br.close();
062: br.read();
063: fail("Read on closed stream");
064: } catch (IOException x) {
065: return;
066: }
067: }
068:
069: /**
070: * @tests java.io.BufferedReader#mark(int)
071: */
072: public void test_markI() throws IOException {
073: // Test for method void java.io.BufferedReader.mark(int)
074: char[] buf = null;
075: br = new BufferedReader(new Support_StringReader(testString));
076: br.skip(500);
077: br.mark(1000);
078: br.skip(250);
079: br.reset();
080: buf = new char[testString.length()];
081: br.read(buf, 0, 500);
082: assertTrue("Failed to set mark properly", testString.substring(
083: 500, 1000).equals(new String(buf, 0, 500)));
084:
085: try {
086: br = new BufferedReader(
087: new Support_StringReader(testString), 800);
088: br.skip(500);
089: br.mark(250);
090: br.read(buf, 0, 1000);
091: br.reset();
092: fail("Failed to invalidate mark properly");
093: } catch (IOException x) {
094: // Expected
095: }
096:
097: char[] chars = new char[256];
098: for (int i = 0; i < 256; i++)
099: chars[i] = (char) i;
100: Reader in = new BufferedReader(new Support_StringReader(
101: new String(chars)), 12);
102:
103: in.skip(6);
104: in.mark(14);
105: in.read(new char[14], 0, 14);
106: in.reset();
107: assertTrue("Wrong chars", in.read() == (char) 6
108: && in.read() == (char) 7);
109:
110: in = new BufferedReader(new Support_StringReader(new String(
111: chars)), 12);
112: in.skip(6);
113: in.mark(8);
114: in.skip(7);
115: in.reset();
116: assertTrue("Wrong chars 2", in.read() == (char) 6
117: && in.read() == (char) 7);
118:
119: BufferedReader br = new BufferedReader(
120: new StringReader("01234"), 2);
121: br.mark(3);
122: char[] carray = new char[3];
123: int result = br.read(carray);
124: assertEquals(3, result);
125: assertEquals("Assert 0:", '0', carray[0]);
126: assertEquals("Assert 1:", '1', carray[1]);
127: assertEquals("Assert 2:", '2', carray[2]);
128: assertEquals("Assert 3:", '3', br.read());
129:
130: br = new BufferedReader(new StringReader("01234"), 2);
131: br.mark(3);
132: carray = new char[4];
133: result = br.read(carray);
134: assertEquals("Assert 4:", 4, result);
135: assertEquals("Assert 5:", '0', carray[0]);
136: assertEquals("Assert 6:", '1', carray[1]);
137: assertEquals("Assert 7:", '2', carray[2]);
138: assertEquals("Assert 8:", '3', carray[3]);
139: assertEquals("Assert 9:", '4', br.read());
140: assertEquals("Assert 10:", -1, br.read());
141:
142: BufferedReader reader = new BufferedReader(new StringReader(
143: "01234"));
144: reader.mark(Integer.MAX_VALUE);
145: reader.read();
146: reader.close();
147: }
148:
149: /**
150: * @tests java.io.BufferedReader#markSupported()
151: */
152: public void test_markSupported() {
153: // Test for method boolean java.io.BufferedReader.markSupported()
154: br = new BufferedReader(new Support_StringReader(testString));
155: assertTrue("markSupported returned false", br.markSupported());
156: }
157:
158: /**
159: * @tests java.io.BufferedReader#read()
160: */
161: public void test_read() throws IOException {
162: // Test for method int java.io.BufferedReader.read()
163: try {
164: br = new BufferedReader(
165: new Support_StringReader(testString));
166: int r = br.read();
167: assertTrue("Char read improperly",
168: testString.charAt(0) == r);
169: br = new BufferedReader(new Support_StringReader(
170: new String(new char[] { '\u8765' })));
171: assertTrue("Wrong double byte character",
172: br.read() == '\u8765');
173: } catch (java.io.IOException e) {
174: fail("Exception during read test");
175: }
176:
177: char[] chars = new char[256];
178: for (int i = 0; i < 256; i++)
179: chars[i] = (char) i;
180: Reader in = new BufferedReader(new Support_StringReader(
181: new String(chars)), 12);
182: try {
183: assertEquals("Wrong initial char", 0, in.read()); // Fill the
184: // buffer
185: char[] buf = new char[14];
186: in.read(buf, 0, 14); // Read greater than the buffer
187: assertTrue("Wrong block read data", new String(buf)
188: .equals(new String(chars, 1, 14)));
189: assertEquals("Wrong chars", 15, in.read()); // Check next byte
190: } catch (IOException e) {
191: fail("Exception during read test 2:" + e);
192: }
193:
194: // regression test for HARMONY-841
195: assertTrue(new BufferedReader(new CharArrayReader(new char[5],
196: 1, 0), 2).read() == -1);
197: }
198:
199: /**
200: * @tests java.io.BufferedReader#read(char[], int, int)
201: */
202: public void test_read$CII() throws Exception {
203: char[] ca = new char[2];
204: BufferedReader toRet = new BufferedReader(
205: new InputStreamReader(new ByteArrayInputStream(
206: new byte[0])));
207: try {
208: toRet.close();
209: } catch (IOException e) {
210: fail("unexpected 1: " + e);
211: }
212:
213: /* Closed reader should throw IOException reading zero bytes */
214: try {
215: toRet.read(ca, 0, 0);
216: fail("Reading zero bytes on a closed reader should not work");
217: } catch (IOException e) {
218: // expected
219: }
220:
221: /*
222: * Closed reader should throw IOException in preference to index out of
223: * bounds
224: */
225: try {
226: // Read should throw IOException before
227: // ArrayIndexOutOfBoundException
228: toRet.read(ca, 1, 5);
229: fail("IOException should have been thrown");
230: } catch (IOException e) {
231: // expected
232: }
233:
234: // Test to ensure that a drained stream returns 0 at EOF
235: toRet = new BufferedReader(new InputStreamReader(
236: new ByteArrayInputStream(new byte[2])));
237: try {
238: assertEquals("Emptying the reader should return two bytes",
239: 2, toRet.read(ca, 0, 2));
240: assertEquals("EOF on a reader should be -1", -1, toRet
241: .read(ca, 0, 2));
242: assertEquals("Reading zero bytes at EOF should work", 0,
243: toRet.read(ca, 0, 0));
244: } catch (IOException ex) {
245: fail("Unexpected IOException : " + ex.getLocalizedMessage());
246: }
247:
248: // Test for method int java.io.BufferedReader.read(char [], int, int)
249: try {
250: char[] buf = new char[testString.length()];
251: br = new BufferedReader(
252: new Support_StringReader(testString));
253: br.read(buf, 50, 500);
254: assertTrue("Chars read improperly",
255: new String(buf, 50, 500).equals(testString
256: .substring(0, 500)));
257: } catch (java.io.IOException e) {
258: fail("Exception during read test");
259: }
260:
261: BufferedReader bufin = new BufferedReader(new Reader() {
262: int size = 2, pos = 0;
263:
264: char[] contents = new char[size];
265:
266: public int read() throws IOException {
267: if (pos >= size)
268: throw new IOException("Read past end of data");
269: return contents[pos++];
270: }
271:
272: public int read(char[] buf, int off, int len)
273: throws IOException {
274: if (pos >= size)
275: throw new IOException("Read past end of data");
276: int toRead = len;
277: if (toRead > (size - pos))
278: toRead = size - pos;
279: System.arraycopy(contents, pos, buf, off, toRead);
280: pos += toRead;
281: return toRead;
282: }
283:
284: public boolean ready() throws IOException {
285: return size - pos > 0;
286: }
287:
288: public void close() throws IOException {
289: }
290: });
291: try {
292: bufin.read();
293: int result = bufin.read(new char[2], 0, 2);
294: assertTrue("Incorrect result: " + result, result == 1);
295: } catch (IOException e) {
296: fail("Unexpected: " + e);
297: }
298:
299: //regression for HARMONY-831
300: try {
301: new BufferedReader(new PipedReader(), 9).read(
302: new char[] {}, 7, 0);
303: fail("should throw IndexOutOfBoundsException");
304: } catch (IndexOutOfBoundsException e) {
305: }
306:
307: // Regression for HARMONY-54
308: char[] ch = {};
309: BufferedReader reader = new BufferedReader(new CharArrayReader(
310: ch));
311: try {
312: // Check exception thrown when the reader is open.
313: reader.read(null, 1, 0);
314: fail("Assert 0: NullPointerException expected");
315: } catch (NullPointerException e) {
316: // Expected
317: }
318:
319: // Now check IOException is thrown in preference to
320: // NullPointerexception when the reader is closed.
321: reader.close();
322: try {
323: reader.read(null, 1, 0);
324: fail("Assert 1: IOException expected");
325: } catch (IOException e) {
326: // Expected
327: }
328:
329: try {
330: // And check that the IOException is thrown before
331: // ArrayIndexOutOfBoundException
332: reader.read(ch, 0, 42);
333: fail("Assert 2: IOException expected");
334: } catch (IOException e) {
335: // expected
336: }
337: }
338:
339: /**
340: * @tests java.io.BufferedReader#read(char[], int, int)
341: */
342: public void test_read_$CII_Exception() throws IOException {
343: br = new BufferedReader(new Support_StringReader(testString));
344: char[] nullCharArray = null;
345: char[] charArray = testString.toCharArray();
346:
347: try {
348: br.read(nullCharArray, -1, -1);
349: fail("should throw IndexOutOfBoundsException");
350: } catch (IndexOutOfBoundsException e) {
351: // expected
352: }
353:
354: try {
355: br.read(nullCharArray, -1, 0);
356: fail("should throw IndexOutOfBoundsException");
357: } catch (IndexOutOfBoundsException e) {
358: // expected
359: }
360:
361: try {
362: br.read(nullCharArray, 0, -1);
363: fail("should throw NullPointerException");
364: } catch (NullPointerException e) {
365: // expected
366: }
367:
368: try {
369: br.read(nullCharArray, 0, 0);
370: fail("should throw NullPointerException");
371: } catch (NullPointerException e) {
372: // expected
373: }
374:
375: try {
376: br.read(nullCharArray, 0, 1);
377: fail("should throw NullPointerException");
378: } catch (NullPointerException e) {
379: // expected
380: }
381:
382: try {
383: br.read(charArray, -1, -1);
384: fail("should throw IndexOutOfBoundsException");
385: } catch (IndexOutOfBoundsException e) {
386: // expected
387: }
388:
389: try {
390: br.read(charArray, -1, 0);
391: fail("should throw IndexOutOfBoundsException");
392: } catch (IndexOutOfBoundsException e) {
393: // expected
394: }
395:
396: br.read(charArray, 0, 0);
397: br.read(charArray, 0, charArray.length);
398: br.read(charArray, charArray.length, 0);
399:
400: try {
401: br.read(charArray, charArray.length + 1, 0);
402: fail("should throw IndexOutOfBoundsException");
403: } catch (IndexOutOfBoundsException e) {
404: //expected
405: }
406:
407: try {
408: br.read(charArray, charArray.length + 1, 1);
409: fail("should throw IndexOutOfBoundsException");
410: } catch (IndexOutOfBoundsException e) {
411: //expected
412: }
413:
414: br.close();
415:
416: try {
417: br.read(nullCharArray, -1, -1);
418: fail("should throw IOException");
419: } catch (IOException e) {
420: // expected
421: }
422:
423: try {
424: br.read(charArray, -1, 0);
425: fail("should throw IOException");
426: } catch (IOException e) {
427: // expected
428: }
429:
430: try {
431: br.read(charArray, 0, -1);
432: fail("should throw IOException");
433: } catch (IOException e) {
434: // expected
435: }
436: }
437:
438: /**
439: * @tests java.io.BufferedReader#readLine()
440: */
441: public void test_readLine() {
442: // Test for method java.lang.String java.io.BufferedReader.readLine()
443: try {
444: br = new BufferedReader(
445: new Support_StringReader(testString));
446: String r = br.readLine();
447: assertEquals("readLine returned incorrect string",
448: "Test_All_Tests", r);
449: } catch (java.io.IOException e) {
450: fail("Exception during readLine test");
451: }
452: }
453:
454: /**
455: * @tests java.io.BufferedReader#ready()
456: */
457: public void test_ready() {
458: // Test for method boolean java.io.BufferedReader.ready()
459: try {
460: br = new BufferedReader(
461: new Support_StringReader(testString));
462: assertTrue("ready returned false", br.ready());
463: } catch (java.io.IOException e) {
464: fail("Exception during ready test" + e.toString());
465: }
466: }
467:
468: /**
469: * @tests java.io.BufferedReader#reset()
470: */
471: public void test_reset() {
472: // Test for method void java.io.BufferedReader.reset()
473: try {
474: br = new BufferedReader(
475: new Support_StringReader(testString));
476: br.skip(500);
477: br.mark(900);
478: br.skip(500);
479: br.reset();
480: char[] buf = new char[testString.length()];
481: br.read(buf, 0, 500);
482: assertTrue("Failed to reset properly", testString
483: .substring(500, 1000).equals(
484: new String(buf, 0, 500)));
485: } catch (java.io.IOException e) {
486: fail("Exception during reset test");
487: }
488: try {
489: br = new BufferedReader(
490: new Support_StringReader(testString));
491: br.skip(500);
492: br.reset();
493: fail("Reset succeeded on unmarked stream");
494: } catch (IOException x) {
495: return;
496:
497: }
498:
499: }
500:
501: /**
502: * @tests java.io.BufferedReader#skip(long)
503: */
504: public void test_skipJ() {
505: // Test for method long java.io.BufferedReader.skip(long)
506: try {
507: br = new BufferedReader(
508: new Support_StringReader(testString));
509: br.skip(500);
510: char[] buf = new char[testString.length()];
511: br.read(buf, 0, 500);
512: assertTrue("Failed to set skip properly", testString
513: .substring(500, 1000).equals(
514: new String(buf, 0, 500)));
515: } catch (java.io.IOException e) {
516: fail("Exception during skip test");
517: }
518:
519: }
520:
521: /**
522: * Sets up the fixture, for example, open a network connection. This method
523: * is called before a test is executed.
524: */
525: protected void setUp() {
526: }
527:
528: /**
529: * Tears down the fixture, for example, close a network connection. This
530: * method is called after a test is executed.
531: */
532: protected void tearDown() {
533: try {
534: br.close();
535: } catch (Exception e) {
536: }
537: }
538: }
|