Search in sources :

Example 1 with MockFileInputStream

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

the class FileSystemHandlingTest method createNewFileByAddingData.

@Test
public void createNewFileByAddingData() throws IOException {
    RuntimeSettings.useVFS = true;
    Runtime.getInstance().resetRuntime();
    byte[] data = new byte[] { 42, 66 };
    EvoSuiteFile file = new EvoSuiteFile("foo");
    MockFile mf = new MockFile(file.getPath());
    Assert.assertFalse(mf.exists());
    FileSystemHandling.appendDataToFile(file, data);
    Assert.assertTrue(mf.exists());
    MockFileInputStream in = new MockFileInputStream(file.getPath());
    byte[] buffer = new byte[4];
    int count = in.read(buffer);
    in.close();
    Assert.assertEquals(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 : MockFile(org.evosuite.runtime.mock.java.io.MockFile) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 2 with MockFileInputStream

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

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

the class CharByteReadWriteTest method testPrintWriter.

@Test
public void testPrintWriter() throws Throwable {
    String file = "PrintWriter_file.tmp";
    String expected = "testPrintWriter";
    MockPrintWriter out = new MockPrintWriter(file);
    out.println(expected);
    out.close();
    Scanner in = new Scanner(new MockFileInputStream(file));
    String result = in.nextLine();
    in.close();
    Assert.assertEquals(expected, result);
}
Also used : Scanner(java.util.Scanner) MockPrintWriter(org.evosuite.runtime.mock.java.io.MockPrintWriter) MockFileInputStream(org.evosuite.runtime.mock.java.io.MockFileInputStream) Test(org.junit.Test)

Example 4 with MockFileInputStream

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

the class VirtualFileSystemTest method testReadingNonExistingFile.

@Test
public void testReadingNonExistingFile() throws IOException {
    String fileName = "this_file_should_not_exist";
    File realFile = new File(fileName);
    Assert.assertFalse(realFile.exists());
    try {
        MockFileInputStream in = new MockFileInputStream(realFile);
        // real file does not exist
        Assert.fail();
    } catch (FileNotFoundException e) {
    }
    File mockFile = new MockFile(fileName);
    Assert.assertFalse(mockFile.exists());
    try {
        MockFileInputStream in = new MockFileInputStream(mockFile);
        // also the mock file does not exist (yet)
        Assert.fail();
    } catch (FileNotFoundException e) {
    }
    boolean created = mockFile.createNewFile();
    Assert.assertTrue(created);
    Assert.assertTrue(mockFile.exists());
    // real file shouldn's have been created
    Assert.assertFalse(realFile.exists());
    // following should work even if real file does not exist
    MockFileInputStream in = new MockFileInputStream(mockFile);
}
Also used : MockFile(org.evosuite.runtime.mock.java.io.MockFile) FileNotFoundException(java.io.FileNotFoundException) 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 5 with MockFileInputStream

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

the class CharByteReadWriteTest method testPrintStream.

@Test
public void testPrintStream() throws Throwable {
    String file = "PrintStream_file.tmp";
    String expected = "testPrintStream";
    MockPrintStream out = new MockPrintStream(file);
    out.println(expected);
    out.close();
    Scanner in = new Scanner(new MockFileInputStream(file));
    String result = in.nextLine();
    in.close();
    Assert.assertEquals(expected, result);
}
Also used : Scanner(java.util.Scanner) MockPrintStream(org.evosuite.runtime.mock.java.io.MockPrintStream) 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