Search in sources :

Example 1 with Inspector

use of org.evosuite.assertion.Inspector 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 2 with Inspector

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

the class OutputCoverageGoal method createGoalsFromObject.

public static Set<OutputCoverageGoal> createGoalsFromObject(String className, String methodName, String methodDesc, Object returnValue) {
    Set<OutputCoverageGoal> goals = new LinkedHashSet<>();
    if (!DependencyAnalysis.isTargetClassName(className))
        return goals;
    if (methodName.equals("hashCode"))
        return goals;
    String methodNameWithDesc = methodName + methodDesc;
    Type returnType = Type.getReturnType(methodDesc);
    switch(returnType.getSort()) {
        case Type.BOOLEAN:
            String desc = ((boolean) returnValue) ? BOOL_TRUE : BOOL_FALSE;
            goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, desc));
            break;
        case Type.CHAR:
            char c = (char) returnValue;
            if (Character.isAlphabetic(c))
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, CHAR_ALPHA));
            else if (Character.isDigit(c))
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, CHAR_DIGIT));
            else
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, CHAR_OTHER));
            break;
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
        case Type.FLOAT:
        case Type.LONG:
        case Type.DOUBLE:
            assert (returnValue instanceof Number);
            if (isJavaNumber(returnValue)) {
                double value = ((Number) returnValue).doubleValue();
                String numDesc = (value < 0) ? NUM_NEGATIVE : (value == 0) ? NUM_ZERO : NUM_POSITIVE;
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, numDesc, (Number) returnValue));
            }
            break;
        case Type.ARRAY:
            if (returnValue == null)
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, REF_NULL));
            else {
                String arrDesc = (Array.getLength(returnValue) == 0) ? ARRAY_EMPTY : ARRAY_NONEMPTY;
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, arrDesc));
            }
            break;
        case Type.OBJECT:
            if (returnValue == null)
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, REF_NULL));
            else {
                goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, REF_NONNULL));
                if (returnType.getClassName().equals("java.lang.String")) {
                    String valDesc = ((String) returnValue).isEmpty() ? STRING_EMPTY : STRING_NONEMPTY;
                    goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, valDesc));
                    break;
                }
                for (Inspector inspector : InspectorManager.getInstance().getInspectors(returnValue.getClass())) {
                    String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod());
                    try {
                        Object val = inspector.getValue(returnValue);
                        if (val instanceof Boolean) {
                            String valDesc = ((boolean) val) ? BOOL_TRUE : BOOL_FALSE;
                            goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + valDesc));
                        } else if (isJavaNumber(val)) {
                            double dv = ((Number) val).doubleValue();
                            String valDesc = (dv < 0) ? NUM_NEGATIVE : (dv == 0) ? NUM_ZERO : NUM_POSITIVE;
                            goals.add(new OutputCoverageGoal(className, methodNameWithDesc, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + valDesc));
                        }
                    } catch (InvocationTargetException e) {
                        // Exceptions in inspectors can happen
                        logger.debug(e.getMessage(), e);
                    } catch (IllegalAccessException e) {
                        logger.warn(e.getMessage(), e);
                    }
                }
            }
            break;
        default:
            // TODO: what to do with the sort for METHOD?
            break;
    }
    return goals;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(org.objectweb.asm.Type) Inspector(org.evosuite.assertion.Inspector)

Example 3 with Inspector

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

the class OutputCoverageFactory method getCoverageGoals.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.coverage.TestCoverageFactory#getCoverageGoals()
	 */
/**
 * {@inheritDoc}
 */
@Override
public List<OutputCoverageTestFitness> getCoverageGoals() {
    List<OutputCoverageTestFitness> goals = new ArrayList<OutputCoverageTestFitness>();
    long start = System.currentTimeMillis();
    String targetClass = Properties.TARGET_CLASS;
    final MethodNameMatcher matcher = new MethodNameMatcher();
    for (String className : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownClasses()) {
        if (!(targetClass.equals("") || className.endsWith(targetClass)))
            continue;
        for (Method method : TestClusterUtils.getClass(className).getDeclaredMethods()) {
            String methodName = method.getName() + Type.getMethodDescriptor(method);
            if (!TestUsageChecker.canUse(method) || !matcher.methodMatches(methodName) || methodName.equals("hashCode()I"))
                continue;
            logger.info("Adding goals for method " + className + "." + methodName);
            Type returnType = Type.getReturnType(method);
            switch(returnType.getSort()) {
                case Type.BOOLEAN:
                    goals.add(createGoal(className, methodName, returnType, BOOL_TRUE));
                    goals.add(createGoal(className, methodName, returnType, BOOL_FALSE));
                    break;
                case Type.CHAR:
                    goals.add(createGoal(className, methodName, returnType, CHAR_ALPHA));
                    goals.add(createGoal(className, methodName, returnType, CHAR_DIGIT));
                    goals.add(createGoal(className, methodName, returnType, CHAR_OTHER));
                    break;
                case Type.BYTE:
                case Type.SHORT:
                case Type.INT:
                case Type.FLOAT:
                case Type.LONG:
                case Type.DOUBLE:
                    goals.add(createGoal(className, methodName, returnType, NUM_NEGATIVE));
                    goals.add(createGoal(className, methodName, returnType, NUM_ZERO));
                    goals.add(createGoal(className, methodName, returnType, NUM_POSITIVE));
                    break;
                case Type.ARRAY:
                    goals.add(createGoal(className, methodName, returnType, REF_NULL));
                    goals.add(createGoal(className, methodName, returnType, ARRAY_EMPTY));
                    goals.add(createGoal(className, methodName, returnType, ARRAY_NONEMPTY));
                    break;
                case Type.OBJECT:
                    goals.add(createGoal(className, methodName, returnType, REF_NULL));
                    // goals.add(new OutputCoverageTestFitness(new OutputCoverageGoal(className, methodName, returnType.toString(), REF_NONNULL)));
                    if (returnType.getClassName().equals("java.lang.String")) {
                        goals.add(createGoal(className, methodName, returnType, STRING_EMPTY));
                        goals.add(createGoal(className, methodName, returnType, STRING_NONEMPTY));
                        break;
                    }
                    boolean observerGoalsAdded = false;
                    Class<?> returnClazz = method.getReturnType();
                    for (Inspector inspector : InspectorManager.getInstance().getInspectors(returnClazz)) {
                        String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod());
                        Type t = Type.getReturnType(inspector.getMethod());
                        if (t.getSort() == Type.BOOLEAN) {
                            goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + BOOL_TRUE));
                            goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + BOOL_FALSE));
                            observerGoalsAdded = true;
                        } else if (Arrays.asList(new Integer[] { Type.BYTE, Type.SHORT, Type.INT, Type.FLOAT, Type.LONG, Type.DOUBLE }).contains(t.getSort())) {
                            goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + NUM_NEGATIVE));
                            goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + NUM_ZERO));
                            goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + NUM_POSITIVE));
                            observerGoalsAdded = true;
                        }
                    }
                    if (!observerGoalsAdded)
                        goals.add(createGoal(className, methodName, returnType, REF_NONNULL));
                    break;
                default:
                    // TODO: what to do with the sort for METHOD?
                    break;
            }
        }
    }
    goalComputationTime = System.currentTimeMillis() - start;
    return goals;
}
Also used : Type(org.objectweb.asm.Type) MethodNameMatcher(org.evosuite.coverage.MethodNameMatcher) ArrayList(java.util.ArrayList) Inspector(org.evosuite.assertion.Inspector) Method(java.lang.reflect.Method)

Aggregations

Inspector (org.evosuite.assertion.Inspector)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Type (org.objectweb.asm.Type)2 TypeToken (com.googlecode.gentyref.TypeToken)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Assertion (org.evosuite.assertion.Assertion)1 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)1 MethodNameMatcher (org.evosuite.coverage.MethodNameMatcher)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)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 Test (org.junit.Test)1