Search in sources :

Example 6 with MockFile

use of org.evosuite.runtime.mock.java.io.MockFile 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)

Example 7 with MockFile

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

the class MockFileTest method testAssumptionOnConstructors_Mock.

@Test
public void testAssumptionOnConstructors_Mock() {
    String name = "base";
    MockFile base = new MockFile(name);
    MockFile emptyParent = new MockFile("", name);
    MockFile nullParent = new MockFile((String) null, name);
    assertEquals(base.getAbsolutePath(), nullParent.getAbsolutePath());
    assertTrue(base.getAbsolutePath().length() > (name.length() + 1));
    if (emptyParent.getAbsolutePath().startsWith("/")) {
        // Mac/Linux
        assertEquals("/" + name, emptyParent.getAbsolutePath());
    }
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) Test(org.junit.Test)

Example 8 with MockFile

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

the class MockFileTest method testSamePath.

@Test
public void testSamePath() {
    String name = "foo.txt";
    File real = new File(name);
    MockFile mock = new MockFile(name);
    assertEquals(real.toString(), mock.toString());
    assertEquals(real.getPath(), mock.getPath());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFile(org.evosuite.runtime.mock.java.io.MockFile) File(java.io.File) Test(org.junit.Test)

Example 9 with MockFile

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

the class VirtualFileSystemTest method testReadingNonExistingFile.

@Test
public void testReadingNonExistingFile() throws IOException {
    String fileName = "this_file_should_not_exist";
    File realFile = new File(fileName);
    Assert.assertFalse(realFile.exists());
    try {
        MockFileInputStream in = new MockFileInputStream(realFile);
        // real file does not exist
        Assert.fail();
    } catch (FileNotFoundException e) {
    }
    File mockFile = new MockFile(fileName);
    Assert.assertFalse(mockFile.exists());
    try {
        MockFileInputStream in = new MockFileInputStream(mockFile);
        // also the mock file does not exist (yet)
        Assert.fail();
    } catch (FileNotFoundException e) {
    }
    boolean created = mockFile.createNewFile();
    Assert.assertTrue(created);
    Assert.assertTrue(mockFile.exists());
    // real file shouldn's have been created
    Assert.assertFalse(realFile.exists());
    // following should work even if real file does not exist
    MockFileInputStream in = new MockFileInputStream(mockFile);
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 10 with MockFile

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

the class VirtualFileSystemTest method testWriteToFile.

@Test
public void testWriteToFile() throws IOException {
    String fileName = "foo_written_with_FOS";
    File realFile = new File(fileName);
    // be sure to get it deleted in case we accidently create it
    realFile.deleteOnExit();
    Assert.assertFalse(realFile.exists());
    File file = new MockFile(fileName);
    Assert.assertFalse(file.exists());
    byte[] data = new byte[] { 42 };
    MockFileOutputStream out = new MockFileOutputStream(file);
    out.write(data);
    // writing to such file should create it
    Assert.assertTrue(file.exists());
    out.close();
    try {
        out.write(data);
        Assert.fail();
    } catch (Exception e) {
    // this is expected, as the stream is closed
    }
    // be sure that no real file was created
    Assert.assertFalse(realFile.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFileOutputStream(org.evosuite.runtime.mock.java.io.MockFileOutputStream) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

MockFile (org.evosuite.runtime.mock.java.io.MockFile)13 Test (org.junit.Test)12 File (java.io.File)7 FileNotFoundException (java.io.FileNotFoundException)3 MockFileInputStream (org.evosuite.runtime.mock.java.io.MockFileInputStream)3 IOException (java.io.IOException)2 EvoSuiteFile (org.evosuite.runtime.testdata.EvoSuiteFile)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 RandomAccessFile (java.io.RandomAccessFile)1 Scanner (java.util.Scanner)1 JFileChooser (javax.swing.JFileChooser)1 MockFileOutputStream (org.evosuite.runtime.mock.java.io.MockFileOutputStream)1 MockRandomAccessFile (org.evosuite.runtime.mock.java.io.MockRandomAccessFile)1 MockJFileChooser (org.evosuite.runtime.mock.javax.swing.MockJFileChooser)1