Search in sources :

Example 1 with UncompilableCodeException

use of org.evosuite.testcase.execution.UncompilableCodeException in project evosuite by EvoSuite.

the class FixedNumRandomTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    LoggingUtils.getEvoLogger().info("* Generating fixed number of random tests");
    RandomLengthTestFactory factory = new org.evosuite.testcase.factories.RandomLengthTestFactory();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    if (!canGenerateTestsForSUT()) {
        LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, 0);
        return suite;
    }
    for (int i = 0; i < Properties.NUM_RANDOM_TESTS; i++) {
        logger.info("Current test: " + i + "/" + Properties.NUM_RANDOM_TESTS);
        TestChromosome test = factory.getChromosome();
        ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
        Integer pos = result.getFirstPositionOfThrownException();
        if (pos != null) {
            if (result.getExceptionThrownAtPosition(pos) instanceof CodeUnderTestException || result.getExceptionThrownAtPosition(pos) instanceof UncompilableCodeException || result.getExceptionThrownAtPosition(pos) instanceof TestCaseExecutor.TimeoutExceeded) {
                // Filter invalid tests
                continue;
            } else {
                // Remove anything that follows an exception
                test.getTestCase().chop(pos + 1);
            }
            test.setChanged(true);
        } else {
            test.setLastExecutionResult(result);
        }
        suite.addTest(test);
    }
    // Search is finished, send statistics
    sendExecutionStatistics();
    return suite;
}
Also used : TestCaseExecutor(org.evosuite.testcase.execution.TestCaseExecutor) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) TestChromosome(org.evosuite.testcase.TestChromosome) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 2 with UncompilableCodeException

use of org.evosuite.testcase.execution.UncompilableCodeException in project evosuite by EvoSuite.

the class MethodStatement method execute.

/**
 * {@inheritDoc}
 */
@Override
public Throwable execute(final Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException {
    logger.trace("Executing method " + method.getName());
    final Object[] inputs = new Object[parameters.size()];
    Throwable exceptionThrown = null;
    try {
        return super.exceptionHandler(new Executer() {

            @Override
            public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
                Object callee_object;
                try {
                    java.lang.reflect.Type[] parameterTypes = method.getParameterTypes();
                    for (int i = 0; i < parameters.size(); i++) {
                        VariableReference parameterVar = parameters.get(i);
                        inputs[i] = parameterVar.getObject(scope);
                        if (inputs[i] == null && method.getMethod().getParameterTypes()[i].isPrimitive()) {
                            throw new CodeUnderTestException(new NullPointerException());
                        }
                        if (inputs[i] != null && !TypeUtils.isAssignable(inputs[i].getClass(), parameterTypes[i])) {
                            // !parameterVar.isAssignableTo(parameterTypes[i])) {
                            throw new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + parameterTypes[i]));
                        }
                    }
                    callee_object = method.isStatic() ? null : callee.getObject(scope);
                    if (!method.isStatic() && callee_object == null) {
                        throw new CodeUnderTestException(new NullPointerException());
                    }
                } catch (CodeUnderTestException e) {
                    throw e;
                // throw CodeUnderTestException.throwException(e.getCause());
                } catch (Throwable e) {
                    e.printStackTrace();
                    throw new EvosuiteError(e);
                }
                Object ret = method.getMethod().invoke(callee_object, inputs);
                /*
					 * TODO: Sometimes we do want to cast an Object to String etc...
					 */
                if (method.getReturnType() instanceof Class<?>) {
                    Class<?> returnClass = (Class<?>) method.getReturnType();
                    if (!returnClass.isPrimitive() && ret != null && !returnClass.isAssignableFrom(ret.getClass())) {
                        throw new CodeUnderTestException(new ClassCastException("Cannot assign " + method.getReturnType() + " to variable of type " + retval.getType()));
                    }
                }
                try {
                    retval.setObject(scope, ret);
                } catch (CodeUnderTestException e) {
                    throw e;
                // throw CodeUnderTestException.throwException(e);
                } catch (Throwable e) {
                    throw new EvosuiteError(e);
                }
            }

            @Override
            public Set<Class<? extends Throwable>> throwableExceptions() {
                Set<Class<? extends Throwable>> t = new LinkedHashSet<Class<? extends Throwable>>();
                t.add(InvocationTargetException.class);
                return t;
            }
        });
    } catch (InvocationTargetException e) {
        exceptionThrown = e.getCause();
        logger.debug("Exception thrown in method {}: {}", method.getName(), exceptionThrown);
    }
    return exceptionThrown;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 3 with UncompilableCodeException

use of org.evosuite.testcase.execution.UncompilableCodeException in project evosuite by EvoSuite.

the class FunctionalMockStatement method execute.

@Override
public Throwable execute(Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException {
    Throwable exceptionThrown = null;
    try {
        return super.exceptionHandler(new Executer() {

            @Override
            public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
                // First create the listener
                listener = createInvocationListener();
                // then create the mock
                Object ret;
                try {
                    logger.debug("Mockito: create mock for {}", targetClass);
                    ret = mock(targetClass, createMockSettings());
                    // ret = mockCreator.invoke(null,targetClass,withSettings().invocationListeners(listener));
                    // execute all "when" statements
                    int index = 0;
                    logger.debug("Mockito: going to mock {} different methods", mockedMethods.size());
                    for (MethodDescriptor md : mockedMethods) {
                        if (!md.shouldBeMocked()) {
                            // no need to mock a method that returns void
                            logger.debug("Mockito: method {} cannot be mocked", md.getMethodName());
                            continue;
                        }
                        // target method, eg foo.aMethod(...)
                        Method method = md.getMethod();
                        // this is needed if method is protected: it couldn't be called here, although fine in
                        // the generated JUnit tests
                        method.setAccessible(true);
                        // target inputs
                        Object[] targetInputs = new Object[md.getNumberOfInputParameters()];
                        for (int i = 0; i < targetInputs.length; i++) {
                            logger.debug("Mockito: executing matcher {}/{}", (1 + i), targetInputs.length);
                            targetInputs[i] = md.executeMatcher(i);
                        }
                        logger.debug("Mockito: going to invoke method {} with {} matchers", method.getName(), targetInputs.length);
                        if (!method.getDeclaringClass().isAssignableFrom(ret.getClass())) {
                            String msg = "Mismatch between callee's class " + ret.getClass() + " and method's class " + method.getDeclaringClass();
                            msg += "\nTarget class classloader " + targetClass.getClassLoader() + " vs method's classloader " + method.getDeclaringClass().getClassLoader();
                            throw new EvosuiteError(msg);
                        }
                        // actual call foo.aMethod(...)
                        Object targetMethodResult;
                        try {
                            if (targetInputs.length == 0) {
                                targetMethodResult = method.invoke(ret);
                            } else {
                                targetMethodResult = method.invoke(ret, targetInputs);
                            }
                        } catch (InvocationTargetException e) {
                            logger.error("Invocation of mocked {}.{}() threw an exception. " + "This means the method was not mocked", targetClass.getName(), method.getName());
                            throw e;
                        } catch (IllegalArgumentException | IllegalAccessError e) {
                            // FIXME: Happens for reasons I don't understand. By throwing a CodeUnderTestException EvoSuite
                            // will just ignore that mocking statement and continue, instead of crashing
                            logger.error("IAE on <" + method + "> when called with " + Arrays.toString(targetInputs));
                            throw new CodeUnderTestException(e);
                        }
                        // when(...)
                        logger.debug("Mockito: call 'when'");
                        OngoingStubbing<Object> retForThen = Mockito.when(targetMethodResult);
                        // thenReturn(...)
                        Object[] thenReturnInputs = null;
                        try {
                            int size = Math.min(md.getCounter(), Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT);
                            thenReturnInputs = new Object[size];
                            for (int i = 0; i < thenReturnInputs.length; i++) {
                                // the position in flat parameter list
                                int k = i + index;
                                if (k >= parameters.size()) {
                                    // throw new RuntimeException("EvoSuite ERROR: index " + k + " out of " + parameters.size());
                                    throw new CodeUnderTestException(new FalsePositiveException("EvoSuite ERROR: index " + k + " out of " + parameters.size()));
                                }
                                VariableReference parameterVar = parameters.get(i + index);
                                thenReturnInputs[i] = parameterVar.getObject(scope);
                                CodeUnderTestException codeUnderTestException = null;
                                if (thenReturnInputs[i] == null && method.getReturnType().isPrimitive()) {
                                    codeUnderTestException = new CodeUnderTestException(new NullPointerException());
                                } else if (thenReturnInputs[i] != null && !TypeUtils.isAssignable(thenReturnInputs[i].getClass(), method.getReturnType())) {
                                    codeUnderTestException = new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + method.getReturnType()));
                                }
                                if (codeUnderTestException != null) {
                                    throw codeUnderTestException;
                                }
                                thenReturnInputs[i] = fixBoxing(thenReturnInputs[i], method.getReturnType());
                            }
                        } catch (Exception e) {
                            // be sure "then" is always called after a "when", otherwise Mockito might end up in
                            // a inconsistent state
                            retForThen.thenThrow(new RuntimeException("Failed to setup mock: " + e.getMessage()));
                            throw e;
                        }
                        // final call when(...).thenReturn(...)
                        logger.debug("Mockito: executing 'thenReturn'");
                        if (thenReturnInputs == null || thenReturnInputs.length == 0) {
                            retForThen.thenThrow(new RuntimeException("No valid return value"));
                        } else if (thenReturnInputs.length == 1) {
                            retForThen.thenReturn(thenReturnInputs[0]);
                        } else {
                            Object[] values = Arrays.copyOfRange(thenReturnInputs, 1, thenReturnInputs.length);
                            retForThen.thenReturn(thenReturnInputs[0], values);
                        }
                        index += thenReturnInputs == null ? 0 : thenReturnInputs.length;
                    }
                } catch (CodeUnderTestException e) {
                    throw e;
                } catch (java.lang.NoClassDefFoundError e) {
                    AtMostOnceLogger.error(logger, "Cannot use Mockito on " + targetClass + " due to failed class initialization: " + e.getMessage());
                    // or should throw an exception?
                    return;
                } catch (MockitoException | IllegalAccessException | IllegalAccessError | IllegalArgumentException e) {
                    // FIXME: Happens for reasons I don't understand. By throwing a CodeUnderTestException EvoSuite
                    // will just ignore that mocking statement and continue, instead of crashing
                    AtMostOnceLogger.error(logger, "Cannot use Mockito on " + targetClass + " due to IAE: " + e.getMessage());
                    // or should throw an exception?
                    throw new CodeUnderTestException(e);
                } catch (Throwable t) {
                    AtMostOnceLogger.error(logger, "Failed to use Mockito on " + targetClass + ": " + t.getMessage());
                    throw new EvosuiteError(t);
                }
                // finally, activate the listener
                listener.activate();
                try {
                    retval.setObject(scope, ret);
                } catch (CodeUnderTestException e) {
                    throw e;
                } catch (Throwable e) {
                    throw new EvosuiteError(e);
                }
            }

            /**
             * a "char" can be used for a "int". But problem is that Mockito takes as input
             * Object, and so those get boxed. However, a Character cannot be used for a "int",
             * so we need to be sure to convert it here
             *
             * @param value
             * @param expectedType
             * @return
             */
            private Object fixBoxing(Object value, Class<?> expectedType) {
                if (!expectedType.isPrimitive()) {
                    return value;
                }
                Class<?> valuesClass = value.getClass();
                assert !valuesClass.isPrimitive();
                if (expectedType.equals(Integer.TYPE)) {
                    if (valuesClass.equals(Character.class)) {
                        value = (int) ((Character) value).charValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (int) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (int) ((Short) value).intValue();
                    }
                }
                if (expectedType.equals(Double.TYPE)) {
                    if (valuesClass.equals(Integer.class)) {
                        value = (double) ((Integer) value).intValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (double) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Character.class)) {
                        value = (double) ((Character) value).charValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (double) ((Short) value).intValue();
                    } else if (valuesClass.equals(Long.class)) {
                        value = (double) ((Long) value).longValue();
                    } else if (valuesClass.equals(Float.class)) {
                        value = (double) ((Float) value).floatValue();
                    }
                }
                if (expectedType.equals(Float.TYPE)) {
                    if (valuesClass.equals(Integer.class)) {
                        value = (float) ((Integer) value).intValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (float) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Character.class)) {
                        value = (float) ((Character) value).charValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (float) ((Short) value).intValue();
                    } else if (valuesClass.equals(Long.class)) {
                        value = (float) ((Long) value).longValue();
                    }
                }
                if (expectedType.equals(Long.TYPE)) {
                    if (valuesClass.equals(Integer.class)) {
                        value = (long) ((Integer) value).intValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (long) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Character.class)) {
                        value = (long) ((Character) value).charValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (long) ((Short) value).intValue();
                    }
                }
                if (expectedType.equals(Short.TYPE)) {
                    if (valuesClass.equals(Integer.class)) {
                        value = (short) ((Integer) value).intValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (short) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (short) ((Short) value).intValue();
                    } else if (valuesClass.equals(Character.class)) {
                        value = (short) ((Character) value).charValue();
                    } else if (valuesClass.equals(Long.class)) {
                        value = (short) ((Long) value).intValue();
                    }
                }
                if (expectedType.equals(Byte.TYPE)) {
                    if (valuesClass.equals(Integer.class)) {
                        value = (byte) ((Integer) value).intValue();
                    } else if (valuesClass.equals(Short.class)) {
                        value = (byte) ((Short) value).intValue();
                    } else if (valuesClass.equals(Byte.class)) {
                        value = (byte) ((Byte) value).intValue();
                    } else if (valuesClass.equals(Character.class)) {
                        value = (byte) ((Character) value).charValue();
                    } else if (valuesClass.equals(Long.class)) {
                        value = (byte) ((Long) value).intValue();
                    }
                }
                return value;
            }

            @Override
            public Set<Class<? extends Throwable>> throwableExceptions() {
                Set<Class<? extends Throwable>> t = new LinkedHashSet<>();
                t.add(InvocationTargetException.class);
                return t;
            }
        });
    } catch (InvocationTargetException e) {
        exceptionThrown = e.getCause();
    }
    return exceptionThrown;
}
Also used : EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method) MethodDescriptor(org.evosuite.testcase.fm.MethodDescriptor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) MockitoException(org.mockito.exceptions.base.MockitoException) FalsePositiveException(org.evosuite.runtime.FalsePositiveException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException) FalsePositiveException(org.evosuite.runtime.FalsePositiveException) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) InstrumentedClass(org.evosuite.runtime.instrumentation.InstrumentedClass) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 4 with UncompilableCodeException

use of org.evosuite.testcase.execution.UncompilableCodeException in project evosuite by EvoSuite.

the class ConstructorStatement method execute.

// TODO: Handle inner classes (need instance parameter for newInstance)
/**
 * {@inheritDoc}
 */
@Override
public Throwable execute(final Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, InstantiationException, IllegalAccessException {
    // PrintStream old_out = System.out;
    // PrintStream old_err = System.err;
    // System.setOut(out);
    // System.setErr(out);
    logger.trace("Executing constructor " + constructor.toString());
    final Object[] inputs = new Object[parameters.size()];
    Throwable exceptionThrown = null;
    try {
        return super.exceptionHandler(new Executer() {

            @Override
            public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
                java.lang.reflect.Type[] parameterTypes = constructor.getParameterTypes();
                for (int i = 0; i < parameters.size(); i++) {
                    VariableReference parameterVar = parameters.get(i);
                    try {
                        inputs[i] = parameterVar.getObject(scope);
                    } catch (CodeUnderTestException e) {
                        throw e;
                    // throw new CodeUnderTestException(e.getCause());
                    // throw CodeUnderTestException.throwException(e.getCause());
                    } catch (Throwable e) {
                        // FIXME: this does not seem to propagate to client root. Is this normal behavior?
                        logger.error("Class " + Properties.TARGET_CLASS + ". Error encountered: " + e);
                        assert (false);
                        throw new EvosuiteError(e);
                    }
                    if (inputs[i] != null && !TypeUtils.isAssignable(inputs[i].getClass(), parameterTypes[i])) {
                        // !parameterVar.isAssignableTo(parameterTypes[i])) {
                        throw new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + parameterTypes[i]));
                    }
                    if (inputs[i] == null && constructor.getConstructor().getParameterTypes()[i].isPrimitive()) {
                        throw new CodeUnderTestException(new NullPointerException());
                    }
                }
                // If this is a non-static member class, the first parameter must not be null
                if (constructor.getConstructor().getDeclaringClass().isMemberClass() && !Modifier.isStatic(constructor.getConstructor().getDeclaringClass().getModifiers())) {
                    if (inputs[0] == null) {
                        // throw new NullPointerException();
                        throw new CodeUnderTestException(new NullPointerException());
                    }
                }
                Object ret = constructor.getConstructor().newInstance(inputs);
                try {
                    // assert(retval.getVariableClass().isAssignableFrom(ret.getClass())) :"we want an " + retval.getVariableClass() + " but got an " + ret.getClass();
                    retval.setObject(scope, ret);
                } catch (CodeUnderTestException e) {
                    throw e;
                // throw CodeUnderTestException.throwException(e);
                } catch (Throwable e) {
                    throw new EvosuiteError(e);
                }
            }

            @Override
            public Set<Class<? extends Throwable>> throwableExceptions() {
                Set<Class<? extends Throwable>> t = new LinkedHashSet<Class<? extends Throwable>>();
                t.add(InvocationTargetException.class);
                return t;
            }
        });
    } catch (InvocationTargetException e) {
        VM.disableCallBacks();
        exceptionThrown = e.getCause();
        if (logger.isDebugEnabled()) {
            try {
                logger.debug("Exception thrown in constructor: " + e.getCause());
            }// this can happen if SUT throws exception on toString
             catch (Exception ex) {
                logger.debug("Exception thrown in constructor and SUT gives issue when calling e.getCause()", ex);
            }
        }
    }
    return exceptionThrown;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Aggregations

CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)4 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)3 VariableReference (org.evosuite.testcase.variable.VariableReference)3 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Method (java.lang.reflect.Method)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 FalsePositiveException (org.evosuite.runtime.FalsePositiveException)1 InstrumentedClass (org.evosuite.runtime.instrumentation.InstrumentedClass)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 TestCaseExecutor (org.evosuite.testcase.execution.TestCaseExecutor)1 RandomLengthTestFactory (org.evosuite.testcase.factories.RandomLengthTestFactory)1 MethodDescriptor (org.evosuite.testcase.fm.MethodDescriptor)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 MockitoException (org.mockito.exceptions.base.MockitoException)1