Search in sources :

Example 1 with StringPrimitiveStatement

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

the class TestSuiteGenerator method buildLoadTargetClassTestCase.

/**
 * Creates a single Test Case that only loads the target class.
 * <code>
 * Thread currentThread = Thread.currentThread();
 * ClassLoader classLoader = currentThread.getClassLoader();
 * classLoader.load(className);
 * </code>
 * @param className the class to be loaded
 * @return
 * @throws EvosuiteError if a reflection error happens while creating the test case
 */
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        // Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        // Statement loadClassStmt = new MethodStatement(test,
        // new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar,
        // Collections.singletonList(string0));
        // test.addStatement(loadClassStmt);
        BooleanPrimitiveStatement stmt1 = new BooleanPrimitiveStatement(test, true);
        VariableReference boolean0 = test.addStatement(stmt1);
        Method forNameMethod = Class.class.getMethod("forName", String.class, boolean.class, ClassLoader.class);
        Statement forNameStmt = new MethodStatement(test, new GenericMethod(forNameMethod, forNameMethod.getDeclaringClass()), null, Arrays.<VariableReference>asList(string0, boolean0, contextClassLoaderVar));
        test.addStatement(forNameStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 2 with StringPrimitiveStatement

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

the class TestClassInitialization method buildLoadTargetClassTestCase.

private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        Statement loadClassStmt = new MethodStatement(test, new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar, Collections.singletonList(string0));
        test.addStatement(loadClassStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 3 with StringPrimitiveStatement

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

the class StringAVMLocalSearch 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 4 with StringPrimitiveStatement

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

the class TestCaseBuilder method appendStringPrimitive.

public VariableReference appendStringPrimitive(String string) {
    StringPrimitiveStatement primitiveStmt = new StringPrimitiveStatement(tc, string);
    tc.addStatement(primitiveStmt);
    return primitiveStmt.getReturnValue();
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement)

Example 5 with StringPrimitiveStatement

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

the class Issre13SystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    // force using DSE
    Properties.DSE_PROBABILITY = 1.0;
    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);
    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);
    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(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) 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) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) 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