Search in sources :

Example 1 with MockFile

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

the class FileSystemHandlingTest method createNewFileByAddingData.

@Test
public void createNewFileByAddingData() throws IOException {
    RuntimeSettings.useVFS = true;
    Runtime.getInstance().resetRuntime();
    byte[] data = new byte[] { 42, 66 };
    EvoSuiteFile file = new EvoSuiteFile("foo");
    MockFile mf = new MockFile(file.getPath());
    Assert.assertFalse(mf.exists());
    FileSystemHandling.appendDataToFile(file, data);
    Assert.assertTrue(mf.exists());
    MockFileInputStream in = new MockFileInputStream(file.getPath());
    byte[] buffer = new byte[4];
    int count = in.read(buffer);
    in.close();
    Assert.assertEquals(data.length, count);
    Assert.assertEquals(data[0], buffer[0]);
    Assert.assertEquals(data[1], buffer[1]);
    Assert.assertEquals(0, buffer[2]);
    Assert.assertEquals(0, buffer[3]);
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 2 with MockFile

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

the class VirtualFileSystemTest method testCreateDeleteFileDirectly.

@Test
public void testCreateDeleteFileDirectly() throws IOException {
    MockFile file = new MockFile("foo");
    Assert.assertFalse(file.exists());
    boolean created = file.createNewFile();
    Assert.assertTrue(created);
    Assert.assertTrue(file.exists());
    boolean deleted = file.delete();
    Assert.assertTrue(deleted);
    Assert.assertFalse(file.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) Test(org.junit.Test)

Example 3 with MockFile

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

the class VirtualFileSystemTest method testRename.

@Test
public void testRename() throws IOException {
    File bla = new MockFile("bla");
    File doh = new MockFile("doh");
    Assert.assertFalse(bla.exists());
    Assert.assertFalse(doh.exists());
    boolean created = bla.createNewFile();
    Assert.assertTrue(created);
    Assert.assertTrue(bla.exists());
    Assert.assertFalse(doh.exists());
    boolean renamed = bla.renameTo(doh);
    Assert.assertTrue(renamed);
    Assert.assertFalse(bla.exists());
    Assert.assertTrue(doh.exists());
    File inAnotherFolder = new MockFile("foo/hei/hello.tmp");
    Assert.assertFalse(inAnotherFolder.exists());
    renamed = doh.renameTo(inAnotherFolder);
    Assert.assertFalse(renamed);
    Assert.assertFalse(inAnotherFolder.exists());
    Assert.assertTrue(doh.exists());
    File de = new MockFile("deeee");
    File blup = new MockFile("blup");
    Assert.assertFalse(de.exists());
    Assert.assertFalse(blup.exists());
    renamed = de.renameTo(blup);
    Assert.assertFalse(renamed);
    Assert.assertFalse(de.exists());
    Assert.assertFalse(blup.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile) Test(org.junit.Test)

Example 4 with MockFile

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

the class VirtualFileSystemTest method testCreateDeleteFolderDirectly.

@Test
public void testCreateDeleteFolderDirectly() throws IOException {
    MockFile folder = new MockFile("foo" + File.separator + "hello");
    Assert.assertFalse(folder.exists());
    // parent doesn't exist, so should fail
    boolean created = folder.mkdir();
    Assert.assertFalse(created);
    Assert.assertFalse(folder.exists());
    created = folder.mkdirs();
    Assert.assertTrue(created);
    Assert.assertTrue(folder.exists());
    MockFile file = new MockFile(folder.getAbsoluteFile() + File.separator + "evo");
    created = file.createNewFile();
    Assert.assertTrue(created);
    Assert.assertTrue(file.exists());
    // deleting non-empty folder should fail
    boolean deleted = folder.delete();
    Assert.assertFalse(deleted);
    Assert.assertTrue(folder.exists());
    deleted = file.delete();
    Assert.assertTrue(deleted);
    Assert.assertFalse(file.exists());
    // now we can delete the folder
    deleted = folder.delete();
    Assert.assertTrue(deleted);
    Assert.assertFalse(folder.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) Test(org.junit.Test)

Example 5 with MockFile

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

the class MockJFileChooserTest method testGetCurrentDirectory.

@Test
public void testGetCurrentDirectory() {
    JFileChooser chooser = new MockJFileChooser();
    File dir = chooser.getCurrentDirectory();
    Assert.assertTrue(dir.exists());
    Assert.assertTrue(dir instanceof MockFile);
}
Also used : MockJFileChooser(org.evosuite.runtime.mock.javax.swing.MockJFileChooser) MockJFileChooser(org.evosuite.runtime.mock.javax.swing.MockJFileChooser) JFileChooser(javax.swing.JFileChooser) MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFile(org.evosuite.runtime.mock.java.io.MockFile) File(java.io.File) 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