|
Commons IO example source code file (NullReaderTest.java)
The Commons IO NullReaderTest.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io.input;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import junit.framework.TestCase;
/**
* JUnit Test Case for {@link NullReader}.
*
* @version $Id: NullReaderTest.java 1003571 2010-10-01 16:26:35Z sebb $
*/
public class NullReaderTest extends TestCase {
/** Constructor */
public NullReaderTest(String name) {
super(name);
}
/** Set up */
@Override
protected void setUp() throws Exception {
super.setUp();
}
/** Tear Down */
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test <code>available() method.
*/
public void testRead() throws Exception {
int size = 5;
TestNullReader reader = new TestNullReader(size);
for (int i = 0; i < size; i++) {
assertEquals("Check Value [" + i + "]", i, reader.read());
}
// Check End of File
assertEquals("End of File", -1, reader.read());
// Test reading after the end of file
try {
int result = reader.read();
fail("Should have thrown an IOException, value=[" + result + "]");
} catch (IOException e) {
assertEquals("Read after end of file", e.getMessage());
}
// Close - should reset
reader.close();
assertEquals("Available after close", 0, reader.getPosition());
}
/**
* Test <code>read(char[]) method.
*/
public void testReadCharArray() throws Exception {
char[] chars = new char[10];
Reader reader = new TestNullReader(15);
// Read into array
int count1 = reader.read(chars);
assertEquals("Read 1", chars.length, count1);
for (int i = 0; i < count1; i++) {
assertEquals("Check Chars 1", i, chars[i]);
}
// Read into array
int count2 = reader.read(chars);
assertEquals("Read 2", 5, count2);
for (int i = 0; i < count2; i++) {
assertEquals("Check Chars 2", count1 + i, chars[i]);
}
// End of File
int count3 = reader.read(chars);
assertEquals("Read 3 (EOF)", -1, count3);
// Test reading after the end of file
try {
int count4 = reader.read(chars);
fail("Should have thrown an IOException, value=[" + count4 + "]");
} catch (IOException e) {
assertEquals("Read after end of file", e.getMessage());
}
// reset by closing
reader.close();
// Read into array using offset & length
int offset = 2;
int lth = 4;
int count5 = reader.read(chars, offset, lth);
assertEquals("Read 5", lth, count5);
for (int i = offset; i < lth; i++) {
assertEquals("Check Chars 3", i, chars[i]);
}
}
/**
* Test when configured to throw an EOFException at the end of file
* (rather than return -1).
*/
public void testEOFException() throws Exception {
Reader reader = new TestNullReader(2, false, true);
assertEquals("Read 1", 0, reader.read());
assertEquals("Read 2", 1, reader.read());
try {
int result = reader.read();
fail("Should have thrown an EOFException, value=[" + result + "]");
} catch (EOFException e) {
// expected
}
}
/**
* Test <code>mark() and
Other Commons IO examples (source code examples)Here is a short list of links related to this Commons IO NullReaderTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.