Search in sources :

Example 6 with Statement

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

the class TestChromosome method mockChange.

private boolean mockChange() {
    /*
			Be sure to update the mocked values if there has been any change in
			behavior in the last execution.

			Note: mock "expansion" cannot be done after a test has been mutated and executed,
			as the expansion itself might have side effects. Therefore, it has to be done
			before a test is evaluated.
		 */
    boolean changed = false;
    for (int i = 0; i < test.size(); i++) {
        Statement st = test.getStatement(i);
        if (!(st instanceof FunctionalMockStatement)) {
            continue;
        }
        FunctionalMockStatement fms = (FunctionalMockStatement) st;
        if (!fms.doesNeedToUpdateInputs()) {
            continue;
        }
        int preLength = test.size();
        try {
            List<Type> missing = fms.updateMockedMethods();
            int pos = st.getPosition();
            logger.debug("Generating parameters for mock call");
            // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
            List<VariableReference> refs = TestFactory.getInstance().satisfyParameters(test, null, missing, null, pos, 0, true, false, true);
            fms.addMissingInputs(refs);
        } catch (Exception e) {
            // shouldn't really happen because, in the worst case, we could create mocks for missing parameters
            String msg = "Functional mock problem: " + e.toString();
            AtMostOnceLogger.warn(logger, msg);
            fms.fillWithNullRefs();
            return changed;
        }
        changed = true;
        int increase = test.size() - preLength;
        i += increase;
    }
    return changed;
}
Also used : Type(java.lang.reflect.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with Statement

use of org.evosuite.testcase.statements.Statement 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 8 with Statement

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

the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassTwoParameter.

@Test
public void testGenericClassTwoParameter() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameterTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameter.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    Assert.assertEquals("", 8, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    String code = carved.toString();
    String setLong = "GenericObjectWrapperTwoParameter<String, String>";
    Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Example 9 with Statement

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

the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassSequence.

@Test
public void testGenericClassSequence() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSequenceTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    Assert.assertEquals("", 6, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    String code = carved.toString();
    String setLong = "GenericObjectWrapper<GenericObjectWrapperSequenceTest.Foo>";
    Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
    code = carved.toString();
    setLong = "(Object)";
    Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong));
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Example 10 with Statement

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

the class RegressionSuiteMinimizer method removeDuplicateAssertions.

private void removeDuplicateAssertions(RegressionTestSuiteChromosome suite) {
    Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
    Map<String, List<String>> uniqueAssertions = new HashMap<String, List<String>>();
    // int i = -1;
    while (it.hasNext()) {
        // i++;
        RegressionTestChromosome test = (RegressionTestChromosome) it.next();
        boolean changed = false;
        boolean hadAssertion = false;
        // keep track of new unique assertions, and if not unique, remove the assertion
        for (Assertion a : test.getTheTest().getTestCase().getAssertions()) {
            String aClass = a.getClass().getSimpleName();
            List<String> aTypes = uniqueAssertions.get(aClass);
            if (aTypes == null) {
                aTypes = new ArrayList<String>();
            }
            String aType = "";
            if (a instanceof InspectorAssertion) {
                InspectorAssertion ia = (InspectorAssertion) a;
                try {
                    aType = ia.getInspector().getMethod().getName();
                } catch (NullPointerException e) {
                    // technically this should not happen
                    Statement s = ia.getStatement();
                    if (s instanceof MethodStatement) {
                        aType = ((MethodStatement) s).getMethod().getName();
                    }
                }
            }
            if (aTypes.contains(aType)) {
                // logger.warn("removing non-unique assertion: {}-{}", aClass, aType);
                changed = true;
                a.getStatement().getPosition();
                test.getTheTest().getTestCase().removeAssertion(a);
                continue;
            }
            aTypes.add(aType);
            uniqueAssertions.put(aClass, aTypes);
            hadAssertion = true;
        }
        if (changed) {
            test.updateClassloader();
        }
    }
    if (uniqueAssertions.size() > 0) {
        logger.warn("unique assertions: {}", uniqueAssertions);
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) HashMap(java.util.HashMap) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) List(java.util.List) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

Statement (org.evosuite.testcase.statements.Statement)48 MethodStatement (org.evosuite.testcase.statements.MethodStatement)25 VariableReference (org.evosuite.testcase.variable.VariableReference)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)16 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)11 TestCase (org.evosuite.testcase.TestCase)8 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 FieldStatement (org.evosuite.testcase.statements.FieldStatement)7 Method (java.lang.reflect.Method)6 TestChromosome (org.evosuite.testcase.TestChromosome)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)5 NullStatement (org.evosuite.testcase.statements.NullStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)4 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)4 TestFactory (org.evosuite.testcase.TestFactory)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3