Question
Need to write JUnit test class for the code below: ************************************************************************ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class
Need to write JUnit test class for the code below:
************************************************************************
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BiblioFileIO {
public static final int READER = 0;
public static final int WRITER = 1;
private BufferedReader reader;
private BufferedWriter writer;
private File file;
public BiblioFileIO() {
file = new File( "bibliography.txt" );
}
public BiblioFileIO( String fileName ) {
file = new File( fileName );
}
public void close( int which ) {
try {
switch ( which ) {
case READER:
if ( reader != null ) {
reader.close();
}
break;
case WRITER:
if ( writer != null ) {
writer.close();
}
break;
}
} catch ( IOException e ) {
System.out.print( e.getMessage() );
}
}
public boolean open( int which ) {
boolean success = false;
try {
if ( which == READER ) {
if ( file.exists() && file.canRead() ) {
reader = new BufferedReader( new FileReader( file ) );
success = true;
}
} else if ( which == WRITER ) {
writer = new BufferedWriter( new FileWriter( file ) );
success = true;
}
} catch ( IOException e ) {
success = false;
}
return success;
}
public String readLine() {
String line;
try {
line = reader.readLine();
} catch ( IOException | NullPointerException e ) {
line = null;
}
return line;
}
public void write( String line ) {
try {
if ( writer != null ) {
writer.write( line );
writer.newLine();
}
} catch ( IOException e ) {
System.out.print( e.getMessage() );
}
}
public void deleteFile() {
file.delete();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started