Search in sources :

Example 1 with EvoSuiteFile

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

use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.

the class ConcolicExecutionEnvironmentTest method buildTestCaseWithFile.

private static DefaultTestCase buildTestCaseWithFile() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder tc = new TestCaseBuilder();
    // int int0 = 10;
    VariableReference int0 = tc.appendIntPrimitive(10);
    // MockFile mockFile0 = (MockFile)MockFile.createTempFile("tmp",
    // "foo.txt");
    VariableReference prefixVarRef = tc.appendStringPrimitive("temp");
    VariableReference sufixVarRef = tc.appendStringPrimitive(".txt");
    Method createTempFileMethod = MockFile.class.getMethod("createTempFile", String.class, String.class);
    VariableReference mockFileVarRef = tc.appendMethod(null, createTempFileMethod, prefixVarRef, sufixVarRef);
    // String path = mockFile0.getPath;
    Method getPathMethod = MockFile.class.getMethod("getPath");
    VariableReference pathVarRef = tc.appendMethod(mockFileVarRef, getPathMethod);
    // EvoSuiteFile evosuiteFile = new EvoSuiteFile();
    Constructor<EvoSuiteFile> evoSuiteFileCtor = EvoSuiteFile.class.getConstructor(String.class);
    VariableReference evosuiteFile = tc.appendConstructor(evoSuiteFileCtor, pathVarRef);
    // String string0 = "<<FILE CONTENT>>"
    VariableReference fileContentVarRef = tc.appendStringPrimitive("<<FILE CONTENT>>");
    // FileSystemHandling.appendStringToFile(evosuiteFile, string0)
    Method appendStringToFileMethod = FileSystemHandling.class.getMethod("appendStringToFile", EvoSuiteFile.class, String.class);
    tc.appendMethod(null, appendStringToFileMethod, evosuiteFile, fileContentVarRef);
    // DseWithFile dseWithFile0 = new DseWithFile();
    Constructor<TestCaseWithFile> ctor = TestCaseWithFile.class.getConstructor();
    VariableReference dseWithFileVarRef = tc.appendConstructor(ctor);
    // String ret_val = dseWithFile0.test((File) mockFile0);
    Method testMethod = TestCaseWithFile.class.getMethod("test", File.class);
    tc.appendMethod(dseWithFileVarRef, testMethod, mockFileVarRef);
    // DseWithFile.isiZero(int0)
    Method isZeroMethod = TestCaseWithFile.class.getMethod("isZero", int.class);
    tc.appendMethod(null, isZeroMethod, int0);
    return tc.getDefaultTestCase();
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCaseWithFile(com.examples.with.different.packagename.concolic.TestCaseWithFile) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) Method(java.lang.reflect.Method)

Example 3 with EvoSuiteFile

use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.

the class FileNamePrimitiveStatement method randomize.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.PrimitiveStatement#randomize()
	 */
/**
 * {@inheritDoc}
 */
@Override
public void randomize() {
    String path = Randomness.choice(tc.getAccessedEnvironment().getViewOfAccessedFiles());
    if (path != null) {
        setValue(new EvoSuiteFile(path));
    } else {
        // FIXME find out why this case can actually happen! (I don't think we want this?)
        setValue(null);
    }
    logger.debug("Randomized filename: " + value);
}
Also used : EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile)

Example 4 with EvoSuiteFile

use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(FileNamePrimitiveStatement statement, Scope scope) {
    EvoSuiteFile conc_evosuite_file = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    ReferenceExpression fileRef;
    if (conc_evosuite_file == null) {
        fileRef = env.heap.getReference(null);
        if (fileRef.getConcreteValue() != null) {
            throw new IllegalStateException("Expected null concrete object");
        }
    } else {
        fileRef = (ReferenceConstant) env.heap.getReference(conc_evosuite_file);
        if (fileRef.getConcreteValue() == null) {
            throw new IllegalStateException("Expected non-null concrete object");
        }
    }
    symb_references.put(varRefName, fileRef);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression)

Example 5 with EvoSuiteFile

use of org.evosuite.runtime.testdata.EvoSuiteFile in project evosuite by EvoSuite.

the class EnvironmentDataSystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    // String string0 = "baz5";
    VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
    // DseFoo dseFoo0 = new DseFoo();
    GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
    ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
    VariableReference fooVar = test.addStatement(fooConstructorStatement);
    // String fileName = new String("/home/galeotti/README.txt")
    String path = "/home/galeotti/README.txt";
    EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
    FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
    test.addStatement(fileNameStmt);
    Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
    GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    // DseBar dseBar0 = new DseBar(string0);
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
    VariableReference callee = test.addStatement(constructorStatement);
    // dseBar0.coverMe(dseFoo0);
    Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
    test.addStatement(ms);
    System.out.println(test);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    BranchCoverageMap.getInstance().searchStarted(null);
    assertEquals(4.0, fitness.getFitness(suite), 0.1F);
    suite.addTest(test);
    assertEquals(1.0, fitness.getFitness(suite), 0.1F);
    System.out.println("Test suite: " + suite);
    Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
    TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
    LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
    localObjective.addFitnessFunction(fitness);
    localSearch.doSearch(suite, localObjective);
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
    BranchCoverageMap.getInstance().searchFinished(null);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) FileNamePrimitiveStatement(org.evosuite.testcase.statements.environment.FileNamePrimitiveStatement) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) DseBar(com.examples.with.different.packagename.localsearch.DseBar) TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) GenericClass(org.evosuite.utils.generic.GenericClass) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) 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