Search in sources :

Example 1 with MockRandomAccessFile

use of org.evosuite.runtime.mock.java.io.MockRandomAccessFile in project evosuite by EvoSuite.

the class MockRandomAccessFileTest method testNoWritePermission.

@Test
public void testNoWritePermission() {
    MockFramework.enable();
    VirtualFileSystem.getInstance().resetSingleton();
    VirtualFileSystem.getInstance().init();
    String fileName = "foo_random_access.txt";
    RandomAccessFile ra = null;
    try {
        ra = new MockRandomAccessFile(fileName, "r");
        Assert.fail();
    } catch (FileNotFoundException e1) {
    // expected as file does not exist
    }
    File file = new MockFile(fileName);
    try {
        file.createNewFile();
    } catch (IOException e1) {
        // we should be able to create it
        Assert.fail();
    }
    try {
        ra = new MockRandomAccessFile(fileName, "r");
    } catch (FileNotFoundException e1) {
        // we should be able to open the stream
        Assert.fail();
    }
    final int LENGTH = 10;
    try {
        ra.setLength(LENGTH);
        Assert.fail();
    } catch (IOException e) {
    // expected, as we do now have write permissions;
    }
    long size = -1;
    try {
        ra.close();
        ra = new MockRandomAccessFile(fileName, "rw");
        ra.setLength(LENGTH);
        size = ra.length();
        ra.close();
    } catch (IOException e) {
        Assert.fail();
    }
    Assert.assertEquals(LENGTH, size);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MockRandomAccessFile(org.evosuite.runtime.mock.java.io.MockRandomAccessFile) MockFile(org.evosuite.runtime.mock.java.io.MockFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MockRandomAccessFile(org.evosuite.runtime.mock.java.io.MockRandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) MockRandomAccessFile(org.evosuite.runtime.mock.java.io.MockRandomAccessFile) MockFile(org.evosuite.runtime.mock.java.io.MockFile) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MockFile (org.evosuite.runtime.mock.java.io.MockFile)1 MockRandomAccessFile (org.evosuite.runtime.mock.java.io.MockRandomAccessFile)1 Test (org.junit.Test)1