Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- /**
- * Demo of how to test that an IOException is being caught correctly.
- * Low level read operations can throw IOException.
- * If you use scanner, this won't happen.
- */
- public class TestIOException extends junit.framework.TestCase
- {
- // Return false if write attempt fails
- private boolean write(FileOutputStream out)
- {
- boolean result = true;
- try
- {
- out.write (new byte[3]);
- }
- catch (IOException ex)
- {
- result = false;
- }
- return result;
- }
- public void testWrite() throws IOException
- {
- // Fake stream always throws IOException
- MyFakeStream out = new MyFakeStream("XYZ");
- assertFalse(write(out));
- }
- }
- import java.io.*;
- /** A mock output stream that will ALWAYS throw IOException when writing.
- * @author jdalbey
- * @version 0.1
- */
- public class MyFakeStream extends FileOutputStream
- {
- public MyFakeStream (String filename) throws IOException
- {
- super (getFDOf (filename));
- }
- @Override
- public void write(byte[] b) throws IOException
- {
- throw new IOException();
- }
- protected static FileDescriptor getFDOf (String filename) throws IOException
- {
- RandomAccessFile file = new RandomAccessFile (filename, "rw");
- return file.getFD ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement