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: package org.apache.commons.io.input;
018:
019: import java.io.EOFException;
020: import java.io.IOException;
021: import java.io.Reader;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * JUnit Test Case for {@link NullReader}.
027: *
028: * @version $Id: NullReaderTest.java 463529 2006-10-13 00:37:09Z niallp $
029: */
030: public class NullReaderTest extends TestCase {
031:
032: /** Constructor */
033: public NullReaderTest(String name) {
034: super (name);
035: }
036:
037: /** Set up */
038: protected void setUp() throws Exception {
039: super .setUp();
040: }
041:
042: /** Tear Down */
043: protected void tearDown() throws Exception {
044: super .tearDown();
045: }
046:
047: /**
048: * Test <code>available()</code> method.
049: */
050: public void testRead() throws Exception {
051: int size = 5;
052: TestNullReader reader = new TestNullReader(size);
053: for (int i = 0; i < size; i++) {
054: assertEquals("Check Value [" + i + "]", i, reader.read());
055: }
056:
057: // Check End of File
058: assertEquals("End of File", -1, reader.read());
059:
060: // Test reading after the end of file
061: try {
062: int result = reader.read();
063: fail("Should have thrown an IOException, value=[" + result
064: + "]");
065: } catch (IOException e) {
066: assertEquals("Read after end of file", e.getMessage());
067: }
068:
069: // Close - should reset
070: reader.close();
071: assertEquals("Available after close", 0, reader.getPosition());
072: }
073:
074: /**
075: * Test <code>read(char[])</code> method.
076: */
077: public void testReadCharArray() throws Exception {
078: char[] chars = new char[10];
079: Reader reader = new TestNullReader(15);
080:
081: // Read into array
082: int count1 = reader.read(chars);
083: assertEquals("Read 1", chars.length, count1);
084: for (int i = 0; i < count1; i++) {
085: assertEquals("Check Chars 1", i, chars[i]);
086: }
087:
088: // Read into array
089: int count2 = reader.read(chars);
090: assertEquals("Read 2", 5, count2);
091: for (int i = 0; i < count2; i++) {
092: assertEquals("Check Chars 2", count1 + i, chars[i]);
093: }
094:
095: // End of File
096: int count3 = reader.read(chars);
097: assertEquals("Read 3 (EOF)", -1, count3);
098:
099: // Test reading after the end of file
100: try {
101: int count4 = reader.read(chars);
102: fail("Should have thrown an IOException, value=[" + count4
103: + "]");
104: } catch (IOException e) {
105: assertEquals("Read after end of file", e.getMessage());
106: }
107:
108: // reset by closing
109: reader.close();
110:
111: // Read into array using offset & length
112: int offset = 2;
113: int lth = 4;
114: int count5 = reader.read(chars, offset, lth);
115: assertEquals("Read 5", lth, count5);
116: for (int i = offset; i < lth; i++) {
117: assertEquals("Check Chars 3", i, chars[i]);
118: }
119: }
120:
121: /**
122: * Test when configured to throw an EOFException at the end of file
123: * (rather than return -1).
124: */
125: public void testEOFException() throws Exception {
126: Reader reader = new TestNullReader(2, false, true);
127: assertEquals("Read 1", 0, reader.read());
128: assertEquals("Read 2", 1, reader.read());
129: try {
130: int result = reader.read();
131: fail("Should have thrown an EOFException, value=[" + result
132: + "]");
133: } catch (EOFException e) {
134: // expected
135: }
136: }
137:
138: /**
139: * Test <code>mark()</code> and <code>reset()</code> methods.
140: */
141: public void testMarkAndReset() throws Exception {
142: int position = 0;
143: int readlimit = 10;
144: Reader reader = new TestNullReader(100, true, false);
145:
146: assertTrue("Mark Should be Supported", reader.markSupported());
147:
148: // No Mark
149: try {
150: reader.reset();
151: fail("Read limit exceeded, expected IOException ");
152: } catch (Exception e) {
153: assertEquals("No Mark IOException message",
154: "No position has been marked", e.getMessage());
155: }
156:
157: for (; position < 3; position++) {
158: assertEquals("Read Before Mark [" + position + "]",
159: position, reader.read());
160: }
161:
162: // Mark
163: reader.mark(readlimit);
164:
165: // Read further
166: for (int i = 0; i < 3; i++) {
167: assertEquals("Read After Mark [" + i + "]", (position + i),
168: reader.read());
169: }
170:
171: // Reset
172: reader.reset();
173:
174: // Read From marked position
175: for (int i = 0; i < readlimit + 1; i++) {
176: assertEquals("Read After Reset [" + i + "]",
177: (position + i), reader.read());
178: }
179:
180: // Reset after read limit passed
181: try {
182: reader.reset();
183: fail("Read limit exceeded, expected IOException ");
184: } catch (Exception e) {
185: assertEquals(
186: "Read limit IOException message",
187: "Marked position ["
188: + position
189: + "] is no longer valid - passed the read limit ["
190: + readlimit + "]", e.getMessage());
191: }
192: }
193:
194: /**
195: * Test <code>mark()</code> not supported.
196: */
197: public void testMarkNotSupported() throws Exception {
198: Reader reader = new TestNullReader(100, false, true);
199: assertFalse("Mark Should NOT be Supported", reader
200: .markSupported());
201:
202: try {
203: reader.mark(5);
204: fail("mark() should throw UnsupportedOperationException");
205: } catch (UnsupportedOperationException e) {
206: assertEquals("mark() error message", "Mark not supported",
207: e.getMessage());
208: }
209:
210: try {
211: reader.reset();
212: fail("reset() should throw UnsupportedOperationException");
213: } catch (UnsupportedOperationException e) {
214: assertEquals("reset() error message", "Mark not supported",
215: e.getMessage());
216: }
217: }
218:
219: /**
220: * Test <code>skip()</code> method.
221: */
222: public void testSkip() throws Exception {
223: Reader reader = new TestNullReader(10, true, false);
224: assertEquals("Read 1", 0, reader.read());
225: assertEquals("Read 2", 1, reader.read());
226: assertEquals("Skip 1", 5, reader.skip(5));
227: assertEquals("Read 3", 7, reader.read());
228: assertEquals("Skip 2", 2, reader.skip(5)); // only 2 left to skip
229: assertEquals("Skip 3 (EOF)", -1, reader.skip(5)); // End of file
230: try {
231: reader.skip(5); //
232: fail("Expected IOException for skipping after end of file");
233: } catch (Exception e) {
234: assertEquals("Skip after EOF IOException message",
235: "Skip after end of file", e.getMessage());
236: }
237: }
238:
239: // ------------- Test NullReader implementation -------------
240:
241: private static final class TestNullReader extends NullReader {
242: public TestNullReader(int size) {
243: super (size);
244: }
245:
246: public TestNullReader(int size, boolean markSupported,
247: boolean throwEofException) {
248: super (size, markSupported, throwEofException);
249: }
250:
251: protected int processChar() {
252: return ((int) getPosition() - 1);
253: }
254:
255: protected void processChars(char[] chars, int offset, int length) {
256: int startPos = (int) getPosition() - length;
257: for (int i = offset; i < length; i++) {
258: chars[i] = (char) (startPos + i);
259: }
260: }
261:
262: }
263: }
|