Search in sources :

Example 1 with NonInstrumentingClassLoader

use of org.evosuite.instrumentation.NonInstrumentingClassLoader in project evosuite by EvoSuite.

the class JUnitAnalyzer method handleTestsThatAreUnstable.

/**
 * Compile and run all the test cases, and mark as "unstable" all the ones
 * that fail during execution (ie, unstable assertions).
 *
 * <p>
 * If a test fail due to an exception not related to a JUnit assertion, then
 * remove such test from the input list
 *
 * @param tests
 * @return the number of unstable tests
 */
public static int handleTestsThatAreUnstable(List<TestCase> tests) {
    int numUnstable = 0;
    logger.info("Going to execute: handleTestsThatAreUnstable");
    if (tests == null || tests.isEmpty()) {
        // nothing to do
        return numUnstable;
    }
    File dir = createNewTmpDir();
    if (dir == null) {
        logger.error("Failed to create tmp dir");
        return numUnstable;
    }
    logger.debug("Created tmp folder: " + dir.getAbsolutePath());
    try {
        List<File> generated = compileTests(tests, dir);
        if (generated == null) {
            /*
				 * Note: in theory this shouldn't really happen, as check for compilation
				 * is done before calling this method
				 */
            logger.warn("Failed to compile the test cases ");
            return numUnstable;
        }
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            logger.error("Ran out of time while checking tests");
            return numUnstable;
        }
        // Create a new classloader so that each test gets freshly loaded classes
        loader = new NonInstrumentingClassLoader();
        Class<?>[] testClasses = loadTests(generated);
        if (testClasses == null) {
            logger.error("Found no classes for compiled tests");
            return numUnstable;
        }
        JUnitResult result = runTests(testClasses, dir);
        if (result.wasSuccessful()) {
            // everything is OK
            return numUnstable;
        }
        failure_loop: for (JUnitFailure failure : result.getFailures()) {
            // TODO check if correct
            String testName = failure.getDescriptionMethodName();
            for (int i = 0; i < tests.size(); i++) {
                if (TestSuiteWriterUtils.getNameOfTest(tests, i).equals(testName)) {
                    if (tests.get(i).isFailing()) {
                        logger.info("Failure is expected, continuing...");
                        continue failure_loop;
                    }
                }
            }
            if (testName == null) {
                /*
					 * this can happen if there is a failure in the scaffolding (eg @AfterClass/@BeforeClass).
					 * in such case, everything need to be deleted
					 */
                StringBuilder sb = new StringBuilder();
                sb.append("Issue in scaffolding of the test suite: " + failure.getMessage() + "\n");
                sb.append("Stack trace:\n");
                for (String elem : failure.getExceptionStackTrace()) {
                    sb.append(elem + "\n");
                }
                logger.error(sb.toString());
                numUnstable = tests.size();
                tests.clear();
                return numUnstable;
            }
            // so it might be best to ignore it for now.
            if (testName.equals("initializationError") && failure.getMessage().contains("Failed to attach Java Agent")) {
                logger.warn("Likely error with EvoSuite instrumentation, ignoring failure in test execution");
                continue failure_loop;
            }
            logger.warn("Found unstable test named " + testName + " -> " + failure.getExceptionClassName() + ": " + failure.getMessage());
            for (String elem : failure.getExceptionStackTrace()) {
                logger.info(elem);
            }
            boolean toRemove = !(failure.isAssertionError());
            for (int i = 0; i < tests.size(); i++) {
                if (TestSuiteWriterUtils.getNameOfTest(tests, i).equals(testName)) {
                    logger.warn("Failing test:\n " + tests.get(i).toCode());
                    numUnstable++;
                    /*
						 * we have a match. should we remove it or mark as unstable?
						 * When we have an Assert.* failing, we can just comment out
						 * all the assertions in the test case. If it is an "assert"
						 * in the SUT that fails, we do want to have the JUnit test fail.
						 * On the other hand, if a test fail due to an uncaught exception,
						 * we should delete it, as it would either represent a bug in EvoSuite
						 * or something we cannot (easily) fix here 
						 */
                    if (!toRemove) {
                        logger.debug("Going to mark test as unstable: " + testName);
                        tests.get(i).setUnstable(true);
                    } else {
                        logger.debug("Going to remove unstable test: " + testName);
                        tests.remove(i);
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        logger.error("" + e, e);
        return numUnstable;
    } finally {
        if (dir != null) {
            try {
                FileUtils.deleteDirectory(dir);
            } catch (Exception e) {
                logger.warn("Cannot delete tmp dir: " + dir.getName(), e);
            }
        }
    }
    // if we arrive here, then it means at least one test was unstable
    return numUnstable;
}
Also used : NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) File(java.io.File) IOException(java.io.IOException)

Example 2 with NonInstrumentingClassLoader

use of org.evosuite.instrumentation.NonInstrumentingClassLoader in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_nonInstrumentation_package.

@Test
public void testPackageLevel_differentPackage_nonInstrumentation_package() throws Exception {
    TestCase tc = new DefaultTestCase();
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    NonInstrumentingClassLoader loader = new NonInstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

NonInstrumentingClassLoader (org.evosuite.instrumentation.NonInstrumentingClassLoader)2 File (java.io.File)1 IOException (java.io.IOException)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)1 Test (org.junit.Test)1