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.CharArrayReader;
021: import java.io.FilterReader;
022: import java.io.IOException;
023: import java.io.PushbackReader;
024: import java.io.Reader;
025: import java.io.StringReader;
026:
027: public class PushbackReaderTest extends junit.framework.TestCase {
028:
029: PushbackReader pbr;
030:
031: String pbString = "Hello World";
032:
033: /**
034: * @tests java.io.PushbackReader#PushbackReader(java.io.Reader)
035: */
036: public void test_ConstructorLjava_io_Reader() {
037: // Test for method java.io.PushbackReader(java.io.Reader)
038: try {
039: pbr.close();
040: pbr = new PushbackReader(new StringReader(pbString));
041: char buf[] = new char[5];
042: pbr.read(buf, 0, 5);
043: pbr.unread(buf);
044: } catch (IOException e) {
045: // Correct
046: return;
047: }
048: fail("Created reader with buffer larger than 1");
049: }
050:
051: /**
052: * @tests java.io.PushbackReader#PushbackReader(java.io.Reader, int)
053: */
054: public void test_ConstructorLjava_io_ReaderI() {
055: // Test for method java.io.PushbackReader(java.io.Reader, int)
056: assertTrue("Used to test", true);
057: }
058:
059: /**
060: * @tests java.io.PushbackReader#close()
061: */
062: public void test_close() {
063: // Test for method void java.io.PushbackReader.close()
064: try {
065: pbr.close();
066: pbr.read();
067: } catch (Exception e) {
068: return;
069: }
070: fail("Failed to throw exception reading from closed reader");
071: }
072:
073: /**
074: * @tests java.io.PushbackReader#mark(int)
075: */
076: public void test_markI() {
077: try {
078: pbr.mark(3);
079: } catch (IOException e) {
080: // correct
081: return;
082: }
083: fail("mark failed to throw expected IOException");
084: }
085:
086: /**
087: * @tests java.io.PushbackReader#markSupported()
088: */
089: public void test_markSupported() {
090: // Test for method boolean java.io.PushbackReader.markSupported()
091: assertTrue("markSupported returned true", !pbr.markSupported());
092: }
093:
094: /**
095: * @tests java.io.PushbackReader#read()
096: */
097: public void test_read() {
098: // Test for method int java.io.PushbackReader.read()
099: try {
100: char c;
101: pbr.read();
102: c = (char) pbr.read();
103: assertTrue("Failed to read char: " + c, c == pbString
104: .charAt(1));
105: Reader reader = new PushbackReader(new CharArrayReader(
106: new char[] { '\u8765' }));
107: assertTrue("Wrong double byte character",
108: reader.read() == '\u8765');
109: } catch (IOException e) {
110: fail("IOException during read test : " + e.getMessage());
111: }
112: }
113:
114: /**
115: * @tests java.io.PushbackReader#read(char[], int, int)
116: */
117: public void test_read$CII() {
118: // Test for method int java.io.PushbackReader.read(char [], int, int)
119: try {
120: char[] c = new char[5];
121: pbr.read(c, 0, 5);
122: assertTrue("Failed to read chars", new String(c)
123: .equals(pbString.substring(0, 5)));
124: } catch (IOException e) {
125: fail("IOException during read test : " + e.getMessage());
126: }
127: }
128:
129: /**
130: * @tests java.io.PushbackReader#read(char[], int, int)
131: */
132: public void test_read_$CII_Exception() throws IOException {
133: pbr = new PushbackReader(new StringReader(pbString), 10);
134:
135: char[] nullCharArray = null;
136: char[] charArray = new char[10];
137:
138: try {
139: pbr.read(nullCharArray, -1, -1);
140: fail("should throw IndexOutOfBoundsException");
141: } catch (IndexOutOfBoundsException e) {
142: // expected
143: }
144:
145: try {
146: pbr.read(nullCharArray, 1, 0);
147: fail("should throw NullPointerException");
148: } catch (NullPointerException e) {
149: // expected
150: }
151:
152: try {
153: pbr.read(charArray, -1, -1);
154: fail("should throw IndexOutOfBoundsException");
155: } catch (IndexOutOfBoundsException e) {
156: // expected
157: }
158:
159: pbr.read(charArray, 0, 0);
160: pbr.read(charArray, 0, charArray.length);
161: pbr.read(charArray, charArray.length, 0);
162:
163: try {
164: pbr.read(charArray, charArray.length + 1, 0);
165: fail("should throw IndexOutOfBoundsException");
166: } catch (IndexOutOfBoundsException e) {
167: //expected
168: }
169:
170: try {
171: pbr.read(charArray, charArray.length + 1, 1);
172: fail("should throw IndexOutOfBoundsException");
173: } catch (IndexOutOfBoundsException e) {
174: //expected
175: }
176:
177: pbr.close();
178:
179: try {
180: pbr.read(nullCharArray, -1, -1);
181: fail("should throw IOException");
182: } catch (IOException e) {
183: // expected
184: }
185:
186: try {
187: pbr.read(charArray, -1, -1);
188: fail("should throw IOException");
189: } catch (IOException e) {
190: // expected
191: }
192: }
193:
194: /**
195: * @tests java.io.PushbackReader#ready()
196: */
197: public void test_ready() {
198: // Test for method boolean java.io.PushbackReader.ready()
199: try {
200: char[] c = new char[11];
201: if (c.length > 0)
202: ;// use c to avoid warning msg
203: assertTrue("Ready stream returned false to ready()", pbr
204: .ready());
205: } catch (IOException e) {
206: fail("IOException during ready() test : " + e.getMessage());
207: }
208: }
209:
210: /**
211: * @tests java.io.PushbackReader#reset()
212: */
213: public void test_reset() {
214: try {
215: pbr.reset();
216: } catch (IOException e) {
217: // correct
218: return;
219: }
220: fail("mark failed to throw expected IOException");
221: }
222:
223: /**
224: * @tests java.io.PushbackReader#unread(char[])
225: */
226: public void test_unread$C() {
227: // Test for method void java.io.PushbackReader.unread(char [])
228: try {
229: char[] c = new char[5];
230: pbr.read(c, 0, 5);
231: pbr.unread(c);
232: pbr.read(c, 0, 5);
233: assertTrue("Failed to unread chars", new String(c)
234: .equals(pbString.substring(0, 5)));
235: } catch (IOException e) {
236: fail("IOException during read test : " + e.getMessage());
237: }
238: }
239:
240: /**
241: * @tests java.io.PushbackReader#skip(long)
242: */
243: public void test_skip$J() {
244: char chars[] = new char[] { 'h', 'e', 'l', 'l', 'o' };
245: for (int i = 0; i < 3; i++) {
246: Reader reader, reader2;
247: switch (i) {
248: case 0:
249: reader = new StringReader(new String(chars));
250: reader2 = new StringReader(new String(chars));
251: break;
252: case 1:
253: reader = new FilterReader(new StringReader(new String(
254: chars))) {
255: };
256: reader2 = new FilterReader(new StringReader(new String(
257: chars))) {
258: };
259: break;
260: default:
261: reader = new CharArrayReader(chars);
262: reader2 = new CharArrayReader(chars);
263: }
264: PushbackReader pReader = new PushbackReader(reader, 2);
265: PushbackReader pReader2 = new PushbackReader(reader2, 2);
266: boolean skipped = false;
267: long numSkipped = 0;
268: try {
269: numSkipped = pReader2.skip(3);
270: pReader2.unread('a');
271: pReader2.unread('b');
272: numSkipped += pReader2.skip(10);
273: numSkipped += pReader2.skip(10);
274: numSkipped += pReader2.skip(10);
275: numSkipped += pReader2.skip(10);
276: numSkipped += pReader2.skip(10);
277: numSkipped += pReader2.skip(10);
278: assertEquals(
279: "Did not skip correct number of characters", 7,
280: numSkipped);
281: numSkipped = 0;
282: numSkipped += pReader.skip(2);
283: pReader.unread('i');
284: numSkipped += pReader.skip(2);
285: numSkipped += pReader.skip(0);
286: skipped = true;
287: numSkipped += pReader.skip(-1);
288: fail("Failed to throw "
289: + new IllegalArgumentException().getClass()
290: .getName());
291: } catch (IllegalArgumentException e) {
292: assertTrue("Failed to skip characters" + e, skipped);
293: } catch (IOException e) {
294: fail("Failed to skip characters" + e);
295: }
296: try {
297: numSkipped += pReader.skip(1);
298: numSkipped += pReader.skip(1);
299: numSkipped += pReader.skip(1);
300: assertEquals("Failed to skip all characters", 6,
301: numSkipped);
302: long nextSkipped = pReader.skip(1);
303: assertEquals("skipped empty reader", 0, nextSkipped);
304: } catch (IOException e) {
305: fail("Failed to skip more characters" + e);
306: }
307: }
308: }
309:
310: /**
311: * @tests java.io.PushbackReader#unread(char[], int, int)
312: */
313: public void test_unread$CII() {
314: // Test for method void java.io.PushbackReader.unread(char [], int, int)
315: try {
316: char[] c = new char[5];
317: pbr.read(c, 0, 5);
318: pbr.unread(c, 0, 2);
319: pbr.read(c, 0, 5);
320: assertTrue("Failed to unread chars", new String(c)
321: .equals(pbString.substring(0, 2)
322: + pbString.substring(5, 8)));
323: } catch (IOException e) {
324: fail("IOException during unread test : " + e.getMessage());
325: }
326: }
327:
328: /**
329: * @tests java.io.PushbackReader#unread(char[], int, int)
330: */
331: public void test_unread_$CII_NullPointerException()
332: throws IOException {
333: //a pushback reader with one character buffer
334: pbr = new PushbackReader(new StringReader(pbString));
335:
336: try {
337: pbr.unread(null, 0, 1);
338: fail("should throw NullPointerException");
339: } catch (NullPointerException e) {
340: // expected
341: }
342: }
343:
344: /**
345: * @tests java.io.PushbackReader#unread(char[], int, int)
346: */
347: public void test_unread_$CII_Exception_InsufficientBuffer()
348: throws IOException {
349: //a pushback reader with one character buffer
350: pbr = new PushbackReader(new StringReader(pbString));
351:
352: //if count > buffer's size , should throw IOException
353: try {
354: pbr.unread(new char[pbString.length()], 0, 2);
355: fail("should throw IOException");
356: } catch (IOException e) {
357: // expected
358: }
359: }
360:
361: /**
362: * @tests java.io.PushbackReader#unread(char[], int, int)
363: */
364: public void test_unread_$CII_ArrayIndexOutOfBoundsException()
365: throws IOException {
366: //a pushback reader with one character buffer
367: pbr = new PushbackReader(new StringReader(pbString));
368:
369: try {
370: pbr.unread(new char[pbString.length()], -1, -1);
371: fail("should throw ArrayIndexOutOfBoundsException");
372: } catch (ArrayIndexOutOfBoundsException e) {
373: // expected
374: }
375: }
376:
377: /**
378: * @tests java.io.PushbackReader#unread(int)
379: */
380: public void test_unreadI() {
381: // Test for method void java.io.PushbackReader.unread(int)
382:
383: try {
384: int c;
385: pbr.read();
386: c = pbr.read();
387: pbr.unread(c);
388: assertTrue("Failed to unread char", pbr.read() == c);
389: } catch (IOException e) {
390: fail("IOException during unread test : " + e.getMessage());
391: }
392: }
393:
394: /**
395: * Sets up the fixture, for example, open a network connection. This method
396: * is called before a test is executed.
397: */
398: protected void setUp() {
399: pbr = new PushbackReader(new StringReader(pbString), 10);
400: }
401:
402: /**
403: * Tears down the fixture, for example, close a network connection. This
404: * method is called after a test is executed.
405: */
406: protected void tearDown() {
407: try {
408: pbr.close();
409: } catch (IOException e) {
410: }
411: }
412: }
|