Search in sources :

Example 6 with MockFileInputStream

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

the class CharByteReadWriteTest method testReadWriteByte.

@Test
public void testReadWriteByte() throws Throwable {
    String file = "FileOutputStream_file.tmp";
    String expected = "testReadWriteByte";
    byte[] data = expected.getBytes();
    MockFileOutputStream out = new MockFileOutputStream(file);
    out.write(data, 0, data.length);
    out.flush();
    out.close();
    byte[] buffer = new byte[1024];
    MockFileInputStream in = new MockFileInputStream(file);
    int read = in.read(buffer);
    in.close();
    String result = new String(buffer, 0, read);
    Assert.assertEquals(expected, result);
}
Also used : MockFileOutputStream(org.evosuite.runtime.mock.java.io.MockFileOutputStream) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 7 with MockFileInputStream

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

Aggregations

MockFileInputStream (org.evosuite.runtime.mock.java.io.MockFileInputStream)7 Test (org.junit.Test)7 MockFile (org.evosuite.runtime.mock.java.io.MockFile)4 Scanner (java.util.Scanner)3 File (java.io.File)2 MockFileOutputStream (org.evosuite.runtime.mock.java.io.MockFileOutputStream)2 EvoSuiteFile (org.evosuite.runtime.testdata.EvoSuiteFile)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 MockPrintStream (org.evosuite.runtime.mock.java.io.MockPrintStream)1 MockPrintWriter (org.evosuite.runtime.mock.java.io.MockPrintWriter)1