Search in sources :

Example 1 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class DefaultTestCase method assertionsNeedDownCast.

private boolean assertionsNeedDownCast(Statement s, VariableReference var, Class<?> abstractClass) {
    for (Assertion assertion : s.getAssertions()) {
        if (assertion instanceof InspectorAssertion && assertion.getSource().equals(var)) {
            InspectorAssertion inspectorAssertion = (InspectorAssertion) assertion;
            Method inspectorMethod = inspectorAssertion.getInspector().getMethod();
            if (!ClassUtils.hasMethod(abstractClass, inspectorMethod.getName(), inspectorMethod.getParameterTypes())) {
                return true;
            }
        } else if (assertion instanceof PrimitiveFieldAssertion && assertion.getSource().equals(var)) {
            PrimitiveFieldAssertion fieldAssertion = (PrimitiveFieldAssertion) assertion;
            if (!fieldAssertion.getField().getDeclaringClass().isAssignableFrom(abstractClass)) {
                return true;
            }
        }
    }
    return false;
}
Also used : InspectorAssertion(org.evosuite.assertion.InspectorAssertion) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) PrimitiveFieldAssertion(org.evosuite.assertion.PrimitiveFieldAssertion) PrimitiveFieldAssertion(org.evosuite.assertion.PrimitiveFieldAssertion) Method(java.lang.reflect.Method)

Example 2 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testClassLoaderChange.

@Test
public void testClassLoaderChange() throws NoSuchMethodException, SecurityException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassTwoParameters.class;
    Method creatorMethod = targetClass.getMethod("create", new Class<?>[] {});
    Method targetMethod = targetClass.getMethod("get", new Class<?>[] { Object.class });
    Method inspectorMethod = targetClass.getMethod("testMe", new Class<?>[] {});
    Constructor<?> intConst = Integer.class.getConstructor(new Class<?>[] { int.class });
    GenericClass listOfInteger = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassTwoParameters<Integer, Integer>>() {
    }.getType());
    GenericMethod genericCreatorMethod = new GenericMethod(creatorMethod, targetClass).getGenericInstantiationFromReturnValue(listOfInteger);
    System.out.println(genericCreatorMethod.getGeneratedClass().toString());
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass).copyWithNewOwner(genericCreatorMethod.getGeneratedClass());
    System.out.println(genericMethod.getGeneratedClass().toString());
    DefaultTestCase test = new DefaultTestCase();
    MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>());
    test.addStatement(ms1);
    IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class);
    test.addStatement(ps1);
    GenericConstructor intConstructor = new GenericConstructor(intConst, Integer.class);
    List<VariableReference> constParam = new ArrayList<VariableReference>();
    constParam.add(ps1.getReturnValue());
    ConstructorStatement cs1 = new ConstructorStatement(test, intConstructor, constParam);
    // test.addStatement(cs1);
    List<VariableReference> callParam = new ArrayList<VariableReference>();
    callParam.add(ps1.getReturnValue());
    MethodStatement ms2 = new MethodStatement(test, genericMethod, ms1.getReturnValue(), callParam);
    test.addStatement(ms2);
    Inspector inspector = new Inspector(targetClass, inspectorMethod);
    Assertion assertion = new InspectorAssertion(inspector, ms2, ms1.getReturnValue(), 0);
    ms2.addAssertion(assertion);
    String code = test.toCode();
    ClassLoader loader = new InstrumentingClassLoader();
    Properties.TARGET_CLASS = targetClass.getCanonicalName();
    Properties.CRITERION = new Criterion[1];
    Properties.CRITERION[0] = Criterion.MUTATION;
    DefaultTestCase testCopy = test.clone();
    testCopy.changeClassLoader(loader);
    String code2 = testCopy.toCode();
    Assert.assertEquals(code, code2);
    Assert.assertEquals(code, test.toCode());
    testCopy.removeAssertion(assertion);
    Assert.assertEquals(code, test.toCode());
    // test.removeAssertion(assertion);
    test.removeAssertions();
    System.out.println(test.toCode());
}
Also used : InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) Inspector(org.evosuite.assertion.Inspector) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 3 with Assertion

use of org.evosuite.assertion.Assertion 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)

Example 4 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class AbstractStatement method copyAssertions.

/**
 * {@inheritDoc}
 *
 * Create copies of all attached assertions
 */
@Override
public Set<Assertion> copyAssertions(TestCase newTestCase, int offset) {
    Set<Assertion> copy = new LinkedHashSet<Assertion>();
    for (Assertion a : assertions) {
        if (a == null) {
            logger.info("Assertion is null!");
            logger.info("Statement has assertions: " + assertions.size());
        } else
            copy.add(a.copy(newTestCase, offset));
    }
    return copy;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assertion(org.evosuite.assertion.Assertion)

Example 5 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class RegressionAssertionCounter method checkForAssertions.

private static int checkForAssertions(Boolean removeAssertions, Boolean noExecution, RegressionAssertionGenerator assertionGenerator, RegressionTestChromosome regressionTest) {
    int totalCount = 0;
    if (!noExecution) {
        ExecutionResult result1 = assertionGenerator.runTest(regressionTest.getTheTest().getTestCase());
        ExecutionResult result2 = assertionGenerator.runTest(regressionTest.getTheSameTestForTheOtherClassLoader().getTestCase());
        if (result1.test == null || result2.test == null || result1.hasTimeout() || result2.hasTimeout()) {
            logger.warn("=============================== HAD TIMEOUT ===============================");
        } else {
            int exceptionDiffs = RegressionExceptionHelper.compareExceptionDiffs(result1.getCopyOfExceptionMapping(), result2.getCopyOfExceptionMapping());
            if (exceptionDiffs > 0) {
                logger.debug("Had {} different exceptions! ({})", exceptionDiffs, totalCount);
            }
            totalCount += exceptionDiffs;
            for (Class<?> observerClass : RegressionAssertionGenerator.observerClasses) {
                if (result1.getTrace(observerClass) != null) {
                    result1.getTrace(observerClass).getAssertions(regressionTest.getTheTest().getTestCase(), result2.getTrace(observerClass));
                }
            }
        }
    }
    int assertionCount = regressionTest.getTheTest().getTestCase().getAssertions().size();
    totalCount += assertionCount;
    // Store assertion comments for later flakiness check
    if (assertionCount > 0) {
        List<Assertion> assertions = regressionTest.getTheTest().getTestCase().getAssertions();
        List<String> assertionComments = new ArrayList<>();
        for (Assertion assertion : assertions) {
            logger.warn("+++++ Assertion: {} {}", assertion.getCode(), assertion.getComment());
            assertionComments.add(assertion.getComment());
        }
        RegressionAssertionCounter.assertionComments.put(regressionTest.getTheTest().getTestCase().toCode().hashCode(), assertionComments);
        if (assertions.size() == 0) {
            logger.warn("=========> NO ASSERTIONS!!!");
        } else {
            logger.warn("Assertions ^^^^^^^^^");
        }
    }
    if (removeAssertions) {
        regressionTest.getTheTest().getTestCase().removeAssertions();
    }
    return totalCount;
}
Also used : Assertion(org.evosuite.assertion.Assertion) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Aggregations

Assertion (org.evosuite.assertion.Assertion)7 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)4 ArrayList (java.util.ArrayList)3 Method (java.lang.reflect.Method)2 LinkedHashSet (java.util.LinkedHashSet)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 TypeToken (com.googlecode.gentyref.TypeToken)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Inspector (org.evosuite.assertion.Inspector)1 PrimitiveFieldAssertion (org.evosuite.assertion.PrimitiveFieldAssertion)1 ContractViolation (org.evosuite.contracts.ContractViolation)1 Branch (org.evosuite.coverage.branch.Branch)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)1