use of org.evosuite.runtime.testdata.EvoSuiteFile 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);
}
Aggregations