Search in sources :

Example 1 with InspectorAssertion

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

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

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

Assertion (org.evosuite.assertion.Assertion)3 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 TypeToken (com.googlecode.gentyref.TypeToken)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Inspector (org.evosuite.assertion.Inspector)1 PrimitiveFieldAssertion (org.evosuite.assertion.PrimitiveFieldAssertion)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 Statement (org.evosuite.testcase.statements.Statement)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)1 GenericMethod (org.evosuite.utils.generic.GenericMethod)1