Search in sources :

Example 6 with EvoSuiteFile

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

EvoSuiteFile (org.evosuite.runtime.testdata.EvoSuiteFile)6 VariableReference (org.evosuite.testcase.variable.VariableReference)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 MockFile (org.evosuite.runtime.mock.java.io.MockFile)2 MockFileInputStream (org.evosuite.runtime.mock.java.io.MockFileInputStream)2 TestCaseWithFile (com.examples.with.different.packagename.concolic.TestCaseWithFile)1 DseBar (com.examples.with.different.packagename.localsearch.DseBar)1 DseFoo (com.examples.with.different.packagename.localsearch.DseFoo)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Scanner (java.util.Scanner)1 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)1 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)1 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)1 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)1 FileNamePrimitiveStatement (org.evosuite.testcase.statements.environment.FileNamePrimitiveStatement)1