Search in sources :

Example 11 with MockFile

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

the class VirtualFileSystemTest method testWorkingDirectoryExists.

@Test
public void testWorkingDirectoryExists() {
    MockFile workingDir = new MockFile(java.lang.System.getProperty("user.dir"));
    Assert.assertTrue(workingDir.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) Test(org.junit.Test)

Example 12 with MockFile

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

the class FileSystemHandlingTest method createNewFileByAddingLine.

@Test
public void createNewFileByAddingLine() throws IOException {
    RuntimeSettings.useVFS = true;
    Runtime.getInstance().resetRuntime();
    String data = "A new line to be added";
    EvoSuiteFile file = new EvoSuiteFile("foo");
    MockFile mf = new MockFile(file.getPath());
    Assert.assertFalse(mf.exists());
    FileSystemHandling.appendStringToFile(file, data);
    Assert.assertTrue(mf.exists());
    // try read bytes directly
    MockFileInputStream in = new MockFileInputStream(file.getPath());
    byte[] buffer = new byte[1024];
    in.read(buffer);
    in.close();
    String byteString = new String(buffer);
    Assert.assertTrue("Read: " + byteString, byteString.startsWith(data));
    // try with InputStreamReader
    InputStreamReader reader = new InputStreamReader(new MockFileInputStream(file.getPath()));
    char[] cbuf = new char[1024];
    reader.read(cbuf);
    reader.close();
    String charString = new String(cbuf);
    Assert.assertTrue("Read: " + charString, charString.startsWith(data));
    // try BufferedReader
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new MockFileInputStream(file.getPath())));
    cbuf = new char[1024];
    bufferedReader.read(cbuf);
    bufferedReader.close();
    charString = new String(cbuf);
    Assert.assertTrue("Read: " + charString, charString.startsWith(data));
    // try with Scanner
    Scanner fromFile = new Scanner(new MockFileInputStream(file.getPath()));
    String fileContent = fromFile.nextLine();
    fromFile.close();
    Assert.assertEquals(data, fileContent);
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) MockFile(org.evosuite.runtime.mock.java.io.MockFile) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) BufferedReader(java.io.BufferedReader) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 13 with MockFile

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

the class InstrumentingAgent_IntTest method testMockFramework_noAgent.

@Test
public void testMockFramework_noAgent() {
    /*
		 * OverrideMocks should default even if called
		 * directly. 
		 */
    MockFramework.enable();
    MockFile file = new MockFile("bar/foo");
    File parent = file.getParentFile();
    Assert.assertTrue(parent instanceof MockFile);
    // now, disable
    MockFramework.disable();
    parent = file.getParentFile();
    // should rollback to original behavior
    Assert.assertFalse(parent instanceof MockFile);
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile)

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