FileDocCategorySizeDatePackage
FileReaderTest.javaAPI DocAndroid 1.5 API5484Wed May 06 22:41:04 BST 2009tests.api.java.io

FileReaderTest

public class FileReaderTest extends TestCase

Fields Summary
FileReader
br
BufferedWriter
bw
FileInputStream
fis
File
f
Constructors Summary
Methods Summary
protected voidsetUp()
Sets up the fixture, for example, open a network connection. This method is called before a test is executed.


        f = new File(System.getProperty("java.io.tmpdir"), "reader.tst");

        if (f.exists()) {
            if (!f.delete()) {
                fail("Unable to delete test file");
            }
        }
    
protected voidtearDown()
Tears down the fixture, for example, close a network connection. This method is called after a test is executed.


        try {
            bw.close();
            br.close();
        } catch (Exception e) {
        }

        try {
            if (fis != null)
                fis.close();
        } catch (Exception e) {
        }
        f.delete();
    
public voidtest_ConstructorLjava_io_File()

tests
java.io.FileReader#FileReader(java.io.File)

        // Test for method java.io.FileReader(java.io.File)
        try {
            bw = new BufferedWriter(new FileWriter(f.getPath()));
            bw.write(" After test string", 0, 18);
            bw.close();
            br = new FileReader(f);
            char[] buf = new char[100];
            int r = br.read(buf);
            br.close();
            assertEquals("Test 1: Failed to read correct chars", 
                    " After test string", new String(buf, 0, r));
        } catch (Exception e) {
            fail("Exception during Constructor test " + e.toString());
        }
        
        File noFile = new File(System.getProperty("java.io.tmpdir"), "noreader.tst");
        try {
            br = new FileReader(noFile);
            fail("Test 2: FileNotFoundException expected.");
        } catch (FileNotFoundException e) {
            // Expected.
        }
    
public voidtest_ConstructorLjava_io_FileDescriptor()

tests
java.io.FileReader#FileReader(java.io.FileDescriptor)

        // Test for method java.io.FileReader(java.io.FileDescriptor)
        try {
            bw = new BufferedWriter(new FileWriter(f.getPath()));
            bw.write(" After test string", 0, 18);
            bw.close();
            FileInputStream fis = new FileInputStream(f.getPath());
            br = new FileReader(fis.getFD());
            char[] buf = new char[100];
            int r = br.read(buf);
            br.close();
            fis.close();
            assertEquals("Failed to read correct chars", 
                    " After test string", new String(buf, 0, r));
        } catch (Exception e) {
            fail("Exception during Constructor test " + e.toString());
        }
    
public voidtest_ConstructorLjava_lang_String()

tests
java.io.FileReader#FileReader(java.lang.String)

        // Test for method java.io.FileReader(java.lang.String)
        try {
            bw = new BufferedWriter(new FileWriter(f.getPath()));
            bw.write(" After test string", 0, 18);
            bw.close();
            br = new FileReader(f.getPath());
            char[] buf = new char[100];
            int r = br.read(buf);
            br.close();
            assertEquals("Test 1: Failed to read correct chars", 
                    " After test string", new String(buf, 0, r));
        } catch (Exception e) {
            fail("Exception during Constructor test " + e.toString());
        }
 
        try {
            br = new FileReader(System.getProperty("java.io.tmpdir") + "/noreader.tst");
            fail("Test 2: FileNotFoundException expected.");
        } catch (FileNotFoundException e) {
            // Expected.
        }