Search in sources :

Example 1 with GenericConstructor

use of org.evosuite.utils.GenericConstructor 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 2 with GenericConstructor

use of org.evosuite.utils.GenericConstructor 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

ConstructorStatement (org.evosuite.testcase.ConstructorStatement)2 MethodStatement (org.evosuite.testcase.MethodStatement)2 VariableReference (org.evosuite.testcase.VariableReference)2 GenericConstructor (org.evosuite.utils.GenericConstructor)2 GenericMethod (org.evosuite.utils.GenericMethod)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ArrayList (java.util.ArrayList)1 NullStatement (org.evosuite.testcase.NullStatement)1 PrimitiveStatement (org.evosuite.testcase.PrimitiveStatement)1