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);
}
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);
}
Aggregations