Search in sources :

Example 1 with MockFileOutputStream

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

the class VirtualFileSystemTest method testReadAfterWriteToFile.

@Test
public void testReadAfterWriteToFile() throws IOException {
    File file = MockFile.createTempFile("foo", ".tmp");
    Assert.assertTrue(file.exists());
    byte[] data = new byte[] { 42, 66 };
    MockFileOutputStream out = new MockFileOutputStream(file);
    out.write(data);
    out.close();
    MockFileInputStream in = new MockFileInputStream(file);
    byte[] buffer = new byte[4];
    int count = in.read(buffer);
    in.close();
    Assert.assertEquals("End of stream should had been reached", 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 : MockFileOutputStream(org.evosuite.runtime.mock.java.io.MockFileOutputStream) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 2 with MockFileOutputStream

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

the class VirtualFileSystemTest method testWriteToFile.

@Test
public void testWriteToFile() throws IOException {
    String fileName = "foo_written_with_FOS";
    File realFile = new File(fileName);
    // be sure to get it deleted in case we accidently create it
    realFile.deleteOnExit();
    Assert.assertFalse(realFile.exists());
    File file = new MockFile(fileName);
    Assert.assertFalse(file.exists());
    byte[] data = new byte[] { 42 };
    MockFileOutputStream out = new MockFileOutputStream(file);
    out.write(data);
    // writing to such file should create it
    Assert.assertTrue(file.exists());
    out.close();
    try {
        out.write(data);
        Assert.fail();
    } catch (Exception e) {
    // this is expected, as the stream is closed
    }
    // be sure that no real file was created
    Assert.assertFalse(realFile.exists());
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) MockFileOutputStream(org.evosuite.runtime.mock.java.io.MockFileOutputStream) File(java.io.File) MockFile(org.evosuite.runtime.mock.java.io.MockFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with MockFileOutputStream

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

Aggregations

MockFileOutputStream (org.evosuite.runtime.mock.java.io.MockFileOutputStream)3 Test (org.junit.Test)3 File (java.io.File)2 MockFile (org.evosuite.runtime.mock.java.io.MockFile)2 MockFileInputStream (org.evosuite.runtime.mock.java.io.MockFileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1