Search in sources :

Example 16 with VariableReference

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

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(InfixExpression infixExpr, Class<?> exprType) {
    if (exprType == null) {
        exprType = retrieveTypeClass(infixExpr);
    }
    VariableReference ref = new VariableReferenceImpl(testCase.getReference(), exprType);
    VariableReference leftOperand = retrieveVariableReference(infixExpr.getLeftOperand(), null);
    leftOperand.setOriginalCode(infixExpr.getLeftOperand().toString());
    Operator operator = Operator.toOperator(infixExpr.getOperator().toString());
    VariableReference rightOperand = retrieveVariableReference(infixExpr.getRightOperand(), null);
    rightOperand.setOriginalCode(infixExpr.getRightOperand().toString());
    PrimitiveExpression expr = new PrimitiveExpression(testCase.getReference(), ref, leftOperand, operator, rightOperand);
    testCase.addStatement(expr);
    return ref;
}
Also used : Operator(org.evosuite.testcase.PrimitiveExpression.Operator) VariableReference(org.evosuite.testcase.VariableReference) VariableReferenceImpl(org.evosuite.testcase.VariableReferenceImpl) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression)

Example 17 with VariableReference

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

the class TestCaseCodeGenerator method createPlainInitStmt.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createPlainInitStmt(final int logRecNo, final TestCase testCase) {
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = this.log.objectIds.get(logRecNo);
    if (this.oidToVarRefMap.containsKey(oid)) {
        // TODO this might happen because of Integer.valueOf(), for example. . Is this approach ok?
        return;
    }
    final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value = this.log.params.get(logRecNo)[0];
    if (// Class is a plain type according to log
    value instanceof Class) {
    // FIXME this code needs to get working
    // try
    // {
    // final VariableReference varRef   = new VariableReferenceImpl(testCase, Class.class);
    // final VariableReference    valueRef = new VariableReferenceImpl(testCase, getClassForName(type));
    // 
    // final AssignmentStatement assign = new AssignmentStatement(testCase, varRef, valueRef);
    // this.oidToVarRefMap.put(oid, testCase.addStatement(assign));
    // }
    // catch(final Exception e)
    // {
    // throw new RuntimeException(e);
    // }
    } else {
        final PrimitiveStatement primitiveValue = PrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
        primitiveValue.setValue(value);
        final VariableReference varRef = testCase.addStatement(primitiveValue);
        this.oidToVarRefMap.put(oid, varRef);
    }
}
Also used : PrimitiveStatement(org.evosuite.testcase.PrimitiveStatement) VariableReference(org.evosuite.testcase.VariableReference)

Example 18 with VariableReference

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

the class TestCaseCodeGenerator method createUnobservedInitStmt.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createUnobservedInitStmt(final int logRecNo, final TestCase testCase) {
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = this.log.objectIds.get(logRecNo);
    final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    try {
        // Class.forName("com.thoughtworks.xstream.XStream", true, StaticTestCluster.classLoader);
        final Class<?> xStreamType = getClassForName("com.thoughtworks.xstream.XStream");
        // Class.forName(type, true, StaticTestCluster.classLoader);
        final Class<?> typeClass = getClassForName(type);
        final Object value = this.log.params.get(logRecNo)[0];
        if (xStreamRef == null) {
            final ConstructorStatement constr = new ConstructorStatement(testCase, new GenericConstructor(xStreamType.getConstructor(new Class<?>[0]), xStreamType), Collections.EMPTY_LIST);
            xStreamRef = testCase.addStatement(constr);
        }
        // Class.forName("java.lang.String", true, StaticTestCluster.classLoader);
        final Class<?> stringType = getClassForName("java.lang.String");
        final PrimitiveStatement stringRep = PrimitiveStatement.getPrimitiveStatement(testCase, stringType);
        stringRep.setValue(value);
        final VariableReference stringRepRef = testCase.addStatement(stringRep);
        final MethodStatement m = new MethodStatement(testCase, new GenericMethod(xStreamType.getMethod("fromXML", stringType), typeClass), xStreamRef, Arrays.asList(stringRepRef));
        this.oidToVarRefMap.put(oid, testCase.addStatement(m));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.ConstructorStatement) MethodStatement(org.evosuite.testcase.MethodStatement) PrimitiveStatement(org.evosuite.testcase.PrimitiveStatement) VariableReference(org.evosuite.testcase.VariableReference) GenericConstructor(org.evosuite.utils.GenericConstructor) GenericMethod(org.evosuite.utils.GenericMethod)

Example 19 with VariableReference

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

the class TestCaseCodeGenerator method createFieldWriteAccessStmt.

private void createFieldWriteAccessStmt(final int logRecNo, final TestCase testCase) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final Object[] methodArgs = this.log.params.get(logRecNo);
    final int oid = this.log.objectIds.get(logRecNo);
    final int captureId = this.log.captureIds.get(logRecNo);
    final String fieldName = this.log.namesOfAccessedFields.get(captureId);
    final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = this.log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type));
        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);
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FieldReference(org.evosuite.testcase.FieldReference) VariableReference(org.evosuite.testcase.VariableReference) NullStatement(org.evosuite.testcase.NullStatement) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) GenericField(org.evosuite.utils.GenericField)

Example 20 with VariableReference

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

the class TestCaseCodeGenerator method createMethodCallStmt.

private void createMethodCallStmt(final int logRecNo, final TestCase testCase) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final int oid = this.log.objectIds.get(logRecNo);
    final Object[] methodArgs = this.log.params.get(logRecNo);
    final String methodName = this.log.methodNames.get(logRecNo);
    final String methodDesc = this.log.descList.get(logRecNo);
    final org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
    final Class<?>[] methodParamTypeClasses = new Class[methodParamTypes.length];
    for (int i = 0; i < methodParamTypes.length; i++) {
        methodParamTypeClasses[i] = getClassFromType(methodParamTypes[i]);
    }
    final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    Class<?> type;
    try {
        // Class.forName(typeName, true, StaticTestCluster.classLoader);
        type = getClassForName(typeName);
        final ArrayList<VariableReference> args = new ArrayList<VariableReference>();
        // is either an oid or null
        Integer argOID;
        for (int i = 0; i < methodArgs.length; i++) {
            argOID = (Integer) methodArgs[i];
            if (argOID == null) {
                args.add(testCase.addStatement(new NullStatement(testCase, methodParamTypeClasses[i])));
            } else {
                args.add(this.oidToVarRefMap.get(argOID));
            }
        }
        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 = this.log.returnValues.get(logRecNo);
            if (CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
                final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type.getMethod(methodName, methodParamTypeClasses).getReturnType()), this.oidToVarRefMap.get(oid), args);
                testCase.addStatement(m);
            } else {
                final org.objectweb.asm.Type returnType = org.objectweb.asm.Type.getReturnType(methodDesc);
                // Person var0 = var.getPerson();
                final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), getClassFromType(returnType)), this.oidToVarRefMap.get(oid), args);
                final Integer returnValueOID = (Integer) returnValue;
                this.oidToVarRefMap.put(returnValueOID, testCase.addStatement(m));
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.ConstructorStatement) MethodStatement(org.evosuite.testcase.MethodStatement) VariableReference(org.evosuite.testcase.VariableReference) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.GenericConstructor) GenericMethod(org.evosuite.utils.GenericMethod) NullStatement(org.evosuite.testcase.NullStatement)

Aggregations

VariableReference (org.evosuite.testcase.VariableReference)27 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)6 ArrayList (java.util.ArrayList)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 ArrayIndex (org.evosuite.testcase.ArrayIndex)4 ArrayReference (org.evosuite.testcase.ArrayReference)4 GenericMethod (org.evosuite.utils.GenericMethod)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3