Search in sources :

Example 6 with StringPrimitiveStatement

use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.

the class AbstractMOSA method removeUnusedVariables.

/**
 * When a test case is changed via crossover and/or mutation, it can contains some
 * primitive variables that are not used as input (or to store the output) of method calls.
 * Thus, this method removes all these "trash" statements.
 * @param chromosome
 * @return true or false depending on whether "unused variables" are removed
 */
public boolean removeUnusedVariables(T chromosome) {
    int sizeBefore = chromosome.size();
    TestCase t = ((TestChromosome) chromosome).getTestCase();
    List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
    boolean has_deleted = false;
    int num = 0;
    for (Statement s : t) {
        VariableReference var = s.getReturnValue();
        boolean delete = false;
        delete = delete || s instanceof PrimitiveStatement;
        delete = delete || s instanceof ArrayStatement;
        delete = delete || s instanceof StringPrimitiveStatement;
        if (!t.hasReferences(var) && delete) {
            to_delete.add(num);
            has_deleted = true;
        }
        num++;
    }
    Collections.sort(to_delete, Collections.reverseOrder());
    for (Integer position : to_delete) {
        t.remove(position);
    }
    int sizeAfter = chromosome.size();
    if (has_deleted)
        logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
    return has_deleted;
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) ArrayList(java.util.ArrayList) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 7 with StringPrimitiveStatement

use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.

the class StringLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    StringPrimitiveStatement p = (StringPrimitiveStatement) test.getTestCase().getStatement(statement);
    backup(test, p);
    // TODO: First apply 10 random mutations to determine if string influences _uncovered_ branch
    boolean affected = false;
    String oldValue = p.getValue();
    for (int i = 0; i < Properties.LOCAL_SEARCH_PROBES; i++) {
        if (Randomness.nextDouble() > 0.5)
            p.increment();
        else
            p.randomize();
        logger.info("Probing string " + oldValue + " ->" + p.getCode());
        int result = objective.hasChanged(test);
        if (result < 0) {
            backup(test, p);
        } else {
            restore(test, p);
        }
        if (result != 0) {
            affected = true;
            logger.info("String affects fitness");
            break;
        }
    }
    if (affected) {
        boolean hasImproved = false;
        logger.info("Applying local search to string " + p.getCode());
        // First try to remove each of the characters
        logger.info("Removing characters");
        if (removeCharacters(objective, test, p, statement))
            hasImproved = true;
        logger.info("Statement: " + p.getCode());
        // Second, try to replace each of the characters with each of the 64 possible characters
        logger.info("Replacing characters");
        if (replaceCharacters(objective, test, p, statement))
            hasImproved = true;
        logger.info("Statement: " + p.getCode());
        // Third, try to add characters
        logger.info("Adding characters");
        if (addCharacters(objective, test, p, statement))
            hasImproved = true;
        logger.info("Statement: " + p.getCode());
        logger.info("Resulting string: " + p.getValue());
        return hasImproved;
    // } else {
    // logger.info("Not applying local search to string as it does not improve fitness");
    }
    return false;
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement)

Example 8 with StringPrimitiveStatement

use of org.evosuite.testcase.statements.StringPrimitiveStatement 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)

Example 9 with StringPrimitiveStatement

use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.

the class ConstraintVerifierTest method testNoNullInputs_notNull.

@Test
public void testNoNullInputs_notNull() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo");
    tc.getTestCase().addStatement(foo);
    VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
    factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
    Assert.assertEquals(5, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) EvoServletConfig(org.evosuite.runtime.javaee.javax.servlet.EvoServletConfig) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 10 with StringPrimitiveStatement

use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.

the class ConstraintVerifierTest method testNoNullInputs_nullString.

@Test
public void testNoNullInputs_nullString() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    // shouldn't be able to pass it to createDispatcher
    StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), null);
    tc.getTestCase().addStatement(foo);
    VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
    factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
    /*
            even if "foo" is null and we have the P of object reuse to 1, still "foo"
            will not be used, as "null" constraint is always enforced
         */
    Assert.assertEquals(tc.getTestCase().toCode(), 6, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) EvoServletConfig(org.evosuite.runtime.javaee.javax.servlet.EvoServletConfig) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Aggregations

StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)11 VariableReference (org.evosuite.testcase.variable.VariableReference)7 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)5 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 Method (java.lang.reflect.Method)4 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)4 Test (org.junit.Test)4 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)3 DseBar (com.examples.with.different.packagename.localsearch.DseBar)2 DseFoo (com.examples.with.different.packagename.localsearch.DseFoo)2 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)2 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)2 EvoServletConfig (org.evosuite.runtime.javaee.javax.servlet.EvoServletConfig)2 EvoServletState (org.evosuite.runtime.javaee.javax.servlet.EvoServletState)2 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)2 TestSuiteLocalSearch (org.evosuite.testsuite.localsearch.TestSuiteLocalSearch)2 GenericClass (org.evosuite.utils.generic.GenericClass)2