Search in sources :

Example 31 with VariableReference

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

the class EvoTestCaseCodeGenerator method createMethodCallStmt.

@Override
public void createMethodCallStmt(final CaptureLog log, final int logRecNo) {
    if (log == null)
        throw new IllegalArgumentException("captured log must not be null");
    if (logRecNo <= -1)
        throw new IllegalArgumentException("log record number is invalid: " + logRecNo);
    if (isMaximumLengthReached())
        return;
    // assumption: all necessary statements are created and there is one variable for each referenced object
    final int oid = log.objectIds.get(logRecNo);
    final Object[] methodArgs = log.params.get(logRecNo);
    final String methodName = log.methodNames.get(logRecNo);
    Class<?> type;
    try {
        final String typeName = log.getTypeName(oid);
        type = getClassForName(typeName);
        logger.debug("Creating method call statement for call to method {}.{}", typeName, methodName);
        final Class<?>[] methodParamTypeClasses = getMethodParamTypeClasses(log, logRecNo);
        final ArrayList<VariableReference> args = getArguments(methodArgs, methodParamTypeClasses);
        if (CaptureLog.OBSERVED_INIT.equals(methodName)) {
            // Person var0 = new Person();
            final ConstructorStatement constStmt = new ConstructorStatement(testCase, new GenericConstructor(type.getDeclaredConstructor(methodParamTypeClasses), type), args);
            this.oidToVarRefMap.put(oid, testCase.addStatement(constStmt));
        } else {
            // ------------------ handling for ordinary method calls e.g. var1 = var0.doSth();
            final Object returnValue = log.returnValues.get(logRecNo);
            if (CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
                GenericMethod genericMethod = new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type);
                MethodStatement m = new MethodStatement(testCase, genericMethod, this.oidToVarRefMap.get(oid), args);
                testCase.addStatement(m);
            } else {
                // final org.objectweb.asm.Type returnType = org.objectweb.asm.Type.getReturnType(methodDesc);
                logger.debug("Callee: {} ({})", this.oidToVarRefMap.get(oid), this.oidToVarRefMap.keySet());
                // Person var0 = var.getPerson();
                final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type), this.oidToVarRefMap.get(oid), args);
                final Integer returnValueOID = (Integer) returnValue;
                this.oidToVarRefMap.put(returnValueOID, testCase.addStatement(m));
            }
        }
    } catch (NoSuchMethodException e) {
        logger.info("Method not found; this may happen e.g. if an exception is thrown in the constructor");
        return;
    } catch (final Exception e) {
        logger.info("Error at log record number {}: {}", logRecNo, e.toString());
        logger.info("Test case so far: " + testCase.toCode());
        logger.info(log.toString());
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating method call stmt for %s.", logRecNo, methodName);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 32 with VariableReference

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

the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.

@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
    // assumption: all necessary statements are created and there is one variable for each referenced object
    final Object[] methodArgs = log.params.get(logRecNo);
    final int oid = log.objectIds.get(logRecNo);
    final int captureId = log.captureIds.get(logRecNo);
    final String fieldName = log.getNameOfAccessedFields(captureId);
    final String typeName = log.getTypeName(oid);
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
        final AssignmentStatement assignment;
        final Integer arg = (Integer) methodArgs[0];
        if (arg == null) {
            final NullStatement nullStmt = new NullStatement(testCase, fieldType);
            final VariableReference nullReference = testCase.addStatement(nullStmt);
            assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
        } else {
            assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
        }
        final VariableReference varRef = testCase.addStatement(assignment);
        logger.debug("Adding assignment statement: " + assignment.getCode());
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) NullStatement(org.evosuite.testcase.statements.NullStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 33 with VariableReference

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

the class EvoTestCaseCodeGenerator method createMapInitStmt.

@Override
public void createMapInitStmt(final CaptureLog log, final int logRecNo) {
    try {
        final int oid = log.objectIds.get(logRecNo);
        final Object[] params = log.params.get(logRecNo);
        String collTypeName = log.getTypeName(oid);
        Class<?> collType = getClassForName(collTypeName);
        // -- determine if an alternative collection must be used for code generation
        final boolean isPublic = java.lang.reflect.Modifier.isPublic(collType.getModifiers());
        if (!isPublic || !hasDefaultConstructor(collType)) {
            collType = HashMap.class;
        }
        // -- create code for instantiating collection
        final List<VariableReference> noParams = Collections.emptyList();
        final ConstructorStatement constrStmt = new ConstructorStatement(testCase, new GenericConstructor(collType.getConstructor(new Class<?>[0]), collType), noParams);
        final VariableReference collRef = testCase.addStatement(constrStmt);
        this.oidToVarRefMap.put(oid, collRef);
        // --- fill collection
        MethodStatement methodStmt;
        // is either an oid or null
        Integer argOID;
        ArrayList<VariableReference> paramList = new ArrayList<VariableReference>();
        for (int i = 0; i < params.length; i++) {
            argOID = (Integer) params[i];
            if (argOID == null) {
                paramList.add(testCase.addStatement(new NullStatement(testCase, Object.class)));
            } else {
                paramList.add(this.oidToVarRefMap.get(argOID));
            }
            if (i % 2 == 1) {
                final Method method = collType.getMethod("put", Object.class, Object.class);
                replaceNullWithNullReferences(paramList, Object.class, Object.class);
                methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
                testCase.addStatement(methodStmt);
                paramList = new ArrayList<VariableReference>(2);
            }
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating map init stmt", logRecNo);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) NullStatement(org.evosuite.testcase.statements.NullStatement)

Example 34 with VariableReference

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

the class TestCaseExpander method visitConstructorStatement.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.TestVisitor#visitConstructorStatement(org.evosuite
	 * .testcase.ConstructorStatement)
	 */
public void visitConstructorStatement(TestCase test, ConstructorStatement statement) {
    int i = 0;
    for (VariableReference var : statement.getParameterReferences()) {
        if (var.isPrimitive() || var.isString()) {
            if (usedVariables.contains(var) && test.getStatement(var.getStPosition()) instanceof PrimitiveStatement<?>) {
                // Duplicate and replace
                VariableReference varCopy = duplicateStatement(test, var);
                statement.replaceParameterReference(varCopy, i);
                usedVariables.add(varCopy);
            }
            usedVariables.add(var);
        }
        i++;
    }
    addUnchangedMapping(test, statement.getReturnValue());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference)

Example 35 with VariableReference

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

the class TestCaseExpander method duplicateStatement.

private VariableReference duplicateStatement(TestCase test, VariableReference owner) {
    Statement statement = test.getStatement(owner.getStPosition());
    currentPosition++;
    VariableReference copy = test.addStatement(statement.clone(test), owner.getStPosition() + 1);
    if (!variableMapping.containsKey(owner.getStPosition())) {
        variableMapping.put(owner.getStPosition(), new HashSet<VariableReference>());
    // variableMapping.get(owner.getStPosition()).add(owner);
    }
    variableMapping.get(owner.getStPosition()).add(copy);
    return copy;
}
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