Search in sources :

Example 91 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ObjectFields method getObjectVariables.

/*
	 * private boolean breakRecursion(Object p) { Integer hashCode =
	 * getHasCode(p); Integer recursionCnt = hashRecursionCntMap.get(hashCode);
	 * if (recursionCnt == null) { recursionCnt = 0; } if (recursionCnt >=
	 * MAX_RECURSION) { return true; } recursionCnt++;
	 * hashRecursionCntMap.put(hashCode, recursionCnt); return false; }
	 */
/*
	 * private double getCompositeObjectValue(Object p) { Double cachedDistance
	 * = resultCache.get(getHasCode(p)); if (cachedDistance != null) { return
	 * cachedDistance; } if (breakRecursion(p)) { return 0.0; } Class<?>
	 * commonAncestor = getCommonAncestor(p); double distance =
	 * getTypeDistance(commonAncestor, p); //distance +=
	 * getFieldDistance(commonAncestor, p); resultCache.put(getHasCode(p),
	 * distance); return distance; }
	 */
public Map<Integer, Map<String, Map<String, Object>>> getObjectVariables() {
    // List<Map<Integer, Map<String, Object>>> ov = new ArrayList<Map<Integer, Map<String, Object>>>();
    // Map<Integer, Map<String, Object>> variable_field = new HashMap<Integer, Map<String, Object>>();
    Map<Integer, Map<String, Map<String, Object>>> variable_ref_field = new HashMap<Integer, Map<String, Map<String, Object>>>();
    for (VariableReference vref : scope.getVariables()) {
        Object scope_object = scope.getObject(vref);
        if (scope_object == null)
            continue;
        String vref_class = vref.getClassName();
        // logger.warn(x);
        int vref_string = vref.getStPosition();
        Map<String, Object> objectMap = getObjectMap(scope_object);
        Map<String, Map<String, Object>> vrefObjectMap = new HashMap<String, Map<String, Object>>();
        vrefObjectMap.put(vref_class, objectMap);
        // variable_field.put(vref_string, objectMap);
        variable_ref_field.put(vref_string, vrefObjectMap);
    /*
			 * 
			 * Class<?> so_class = scope_object.getClass();
			 * 
			 * int vref_string = vref.getStPosition();
			 * 
			 * if (ClassUtils.isPrimitiveOrWrapper(scope_object.getClass())) {
			 * Map<String, Object> all_vars = new HashMap<String, Object>();
			 * variable_field.put(vref_string, (Map<String, Object>) (all_vars
			 * .put("fake_var", scope_object))); } else { Map<String, Object>
			 * all_vars = getAllVars(scope_object, 0,"");
			 * variable_field.put(vref_string, all_vars); }
			 */
    // boolean is_prim = ClassUtils.isPrimitiveOrWrapper(so_class);
    // assert is_prim != false : so_class.toString();
    /*
			 * logger.warn("scope_ob: " + scope_object.toString() + ", so_class"
			 * + so_class.toString() + ", vref:" + vref_string +
			 * ",primitve? "+is_prim);
			 */
    }
    return variable_ref_field;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 92 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ClassReInitializer method getMoreClassesToReset.

/*
	 * TODO: I think this would be nicer if each type of statement registered
	 * the classes to reset as part of their execute() method
	 */
private static HashSet<String> getMoreClassesToReset(TestCase tc, ExecutionResult result) {
    HashSet<String> moreClassesForStaticReset = new HashSet<String>();
    for (int position = 0; position < result.getExecutedStatements(); position++) {
        Statement statement = tc.getStatement(position);
        // If we reset also after reads, get all fields
        if (Properties.RESET_STATIC_FIELD_GETS) {
            for (VariableReference var : statement.getVariableReferences()) {
                if (var.isFieldReference()) {
                    FieldReference fieldReference = (FieldReference) var;
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        }
        // Check for explicit assignments to static fields
        if (statement.isAssignmentStatement()) {
            if (statement.getReturnValue() instanceof FieldReference) {
                FieldReference fieldReference = (FieldReference) statement.getReturnValue();
                if (fieldReference.getField().isStatic()) {
                    moreClassesForStaticReset.add(fieldReference.getField().getOwnerClass().getClassName());
                }
            }
        } else if (statement instanceof FieldStatement) {
            // Check if we are invoking a non-pure method on a static field
            // variable
            FieldStatement fieldStatement = (FieldStatement) statement;
            if (fieldStatement.getField().isStatic()) {
                VariableReference fieldReference = fieldStatement.getReturnValue();
                if (Properties.RESET_STATIC_FIELD_GETS) {
                    moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                } else {
                    // Check if the field was passed to a non-pure method
                    for (int i = fieldStatement.getPosition() + 1; i < result.getExecutedStatements(); i++) {
                        Statement invokedStatement = tc.getStatement(i);
                        if (invokedStatement.references(fieldReference)) {
                            if (invokedStatement instanceof MethodStatement) {
                                if (fieldReference.equals(((MethodStatement) invokedStatement).getCallee())) {
                                    if (!CheapPurityAnalyzer.getInstance().isPure(((MethodStatement) invokedStatement).getMethod().getMethod())) {
                                        moreClassesForStaticReset.add(fieldStatement.getField().getOwnerClass().getClassName());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (statement instanceof PrivateFieldStatement) {
            PrivateFieldStatement fieldStatement = (PrivateFieldStatement) statement;
            if (fieldStatement.isStaticField()) {
                moreClassesForStaticReset.add(fieldStatement.getOwnerClassName());
            }
        }
    }
    return moreClassesForStaticReset;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) HashSet(java.util.HashSet)

Example 93 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class FunctionalMockStatement method fillWithNullRefs.

public void fillWithNullRefs() {
    for (int i = 0; i < parameters.size(); i++) {
        VariableReference ref = parameters.get(i);
        if (ref == null) {
            Class<?> expected = getExpectedParameterType(i);
            Object value = null;
            if (expected.isPrimitive()) {
                // can't fill a primitive with null
                if (expected.equals(Integer.TYPE)) {
                    value = 0;
                } else if (expected.equals(Float.TYPE)) {
                    value = 0f;
                } else if (expected.equals(Double.TYPE)) {
                    value = 0d;
                } else if (expected.equals(Long.TYPE)) {
                    value = 0L;
                } else if (expected.equals(Boolean.TYPE)) {
                    value = false;
                } else if (expected.equals(Short.TYPE)) {
                    value = Short.valueOf("0");
                } else if (expected.equals(Character.TYPE)) {
                    value = 'a';
                }
            }
            parameters.set(i, new ConstantValue(tc, new GenericClass(expected), value));
        }
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 94 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class FunctionalMockStatement method copy.

@Override
public Statement copy(TestCase newTestCase, int offset) {
    FunctionalMockStatement copy = new FunctionalMockStatement(newTestCase, retval.getType(), targetClass);
    for (VariableReference r : this.parameters) {
        copy.parameters.add(r.copy(newTestCase, offset));
    }
    // no need to clone, as only read, and created new instance at each new execution
    copy.listener = this.listener;
    for (MethodDescriptor md : this.mockedMethods) {
        copy.mockedMethods.add(md.getCopy());
    }
    for (Map.Entry<String, int[]> entry : methodParameters.entrySet()) {
        int[] array = entry.getValue();
        int[] copiedArray = array == null ? null : new int[] { array[0], array[1] };
        copy.methodParameters.put(entry.getKey(), copiedArray);
    }
    return copy;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) MethodDescriptor(org.evosuite.testcase.fm.MethodDescriptor)

Example 95 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class FunctionalMockStatement method addMissingInputs.

public void addMissingInputs(List<VariableReference> inputs) throws IllegalArgumentException {
    Inputs.checkNull(inputs);
    logger.debug("Adding {} missing values", inputs.size());
    if (!inputs.isEmpty()) {
        if (inputs.size() > parameters.size()) {
            // first quick check
            throw new IllegalArgumentException("Not enough parameter place holders");
        }
        int index = 0;
        for (VariableReference ref : inputs) {
            while (parameters.get(index) != null) {
                index++;
                if (index >= parameters.size()) {
                    throw new IllegalArgumentException("Not enough parameter place holders");
                }
            }
            logger.debug("Current input: " + ref + " for expected type " + getExpectedParameterType(index));
            assert ref.isAssignableTo(getExpectedParameterType(index));
            parameters.set(index, ref);
        }
    }
    // check if all "holes" have been filled
    for (VariableReference ref : parameters) {
        if (ref == null) {
            throw new IllegalArgumentException("Functional mock not fully set with all needed missing inputs");
        }
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14