package com.jini.ch4;
import java.io.*;
import junit.framework.*;
public class FileReaderTester extends TestCase {
private FileReader _input;
private FileReader _empty;
private static final String FILE_NAME = "c:/Users/jini/Desktop/data.txt";
public static void main (String[] args) {
//1. 기본적인 Test 실행 방식.
junit.textui.TestRunner.run(suite());
//2. 해당 클래스에서 test로 시작하는 모든 메소드를 테스트 하도록 함.
junit.textui.TestRunner.run(new TestSuite(FileReaderTester.class));
//3. 여러 클래스의 Test Suite를 연결하기 위한 MasterTester 클래스 방식/
junit.textui.TestRunner.run( suite2());
}
public FileReaderTester(String name) {
super(name);
}
/**
* 메소드명을 파라미터로 하는 TestSuite
* @throws IOException
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new FileReaderTester("testRead"));
suite.addTest(new FileReaderTester("testReadAtEnd"));
return suite;
}
public static Test suite2() {
TestSuite result = new TestSuite();
result.addTest(new TestSuite(FileReaderTester.class));
result.addTest(new TestSuite(FileReaderTester.class));
return result;
}
public void testRead() throws IOException {
char ch = '&';
for(int x = 0; x < 4; x++) {
ch = (char)_input.read();
}
assertEquals('d', ch);
}
public void testReadAtEnd() throws IOException {
int ch = -1234;
for(int x = 0; x < 141; x++) {
ch = _input.read();
}
assertEquals("read at end", -1, _input.read());
}
public void testReadBoundaries() throws IOException {
assertEquals("read first char", 'B', _input.read());
int ch;
for(int x = 0; x < 140; x++) {
ch = _input.read();
}
assertEquals("read last char", '6', _input.read());
assertEquals("read at end", -1, _input.read());
assertEquals("read past end", -1, _input.read());
}
public void testEmptyRead() throws IOException {
assertEquals(-1, _empty.read());
}
public void testReadAfterClose() throws Exception {
_input.close();
try {
_input.read();
fail("read past end에 예외가 발생하지 않음");
} catch (IOException e) {
e.printStackTrace();
}
}
protected void setUp() {
try {
_input = new FileReader(FILE_NAME);
_empty = newEmptyFile();
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
}
protected void tearDown() {
try {
_input.close();
} catch (IOException e) {
throw new RuntimeException("테스트 파일을 닫는 중 에러 발생");
}
}
private FileReader newEmptyFile() throws IOException {
File empty = new File(FILE_NAME);
FileOutputStream out = new FileOutputStream(empty);
out.close();
return new FileReader(empty);
}
}
0 Comments:
댓글 쓰기