|
| 1 | +package eu.stamp.botsing.commons; |
| 2 | + |
| 3 | +import org.evosuite.Properties; |
| 4 | +import org.evosuite.TestSuiteGeneratorHelper; |
| 5 | +import org.evosuite.TimeController; |
| 6 | +import org.evosuite.contracts.FailingTestSet; |
| 7 | +import org.evosuite.coverage.TestFitnessFactory; |
| 8 | +import org.evosuite.junit.JUnitAnalyzer; |
| 9 | +import org.evosuite.junit.writer.TestSuiteWriter; |
| 10 | +import org.evosuite.result.TestGenerationResult; |
| 11 | +import org.evosuite.result.TestGenerationResultBuilder; |
| 12 | +import org.evosuite.rmi.ClientServices; |
| 13 | +import org.evosuite.rmi.service.ClientState; |
| 14 | +import org.evosuite.statistics.RuntimeVariable; |
| 15 | +import org.evosuite.testcase.ConstantInliner; |
| 16 | +import org.evosuite.testcase.TestCase; |
| 17 | +import org.evosuite.testcase.TestFitnessFunction; |
| 18 | +import org.evosuite.testsuite.TestSuiteChromosome; |
| 19 | +import org.evosuite.testsuite.TestSuiteMinimizer; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +public class PostProcessUtility { |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(PostProcessUtility.class); |
| 30 | + |
| 31 | + public static void postProcessTests(TestSuiteChromosome testSuite, List<TestFitnessFactory<? extends TestFitnessFunction>> fitnessFactories) { |
| 32 | + |
| 33 | + if (Properties.INLINE) { |
| 34 | + ConstantInliner inliner = new ConstantInliner(); |
| 35 | + inliner.inline(testSuite); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + if (Properties.MINIMIZE) { |
| 40 | + double before = testSuite.getFitness(); |
| 41 | + |
| 42 | + TestSuiteMinimizer minimizer = new TestSuiteMinimizer(fitnessFactories); |
| 43 | + |
| 44 | + LOG.info("* Minimizing test suite"); |
| 45 | + minimizer.minimize(testSuite, true); |
| 46 | + |
| 47 | + double after = testSuite.getFitness(); |
| 48 | + if (after > before + 0.01d) { // assume minimization |
| 49 | + throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (Properties.ASSERTIONS) { |
| 54 | + LOG.info("Generating assertions"); |
| 55 | + TestSuiteGeneratorHelper.addAssertions(testSuite); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + compileAndCheckTests(testSuite); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + private static void compileAndCheckTests(TestSuiteChromosome chromosome) { |
| 64 | + LOG.info("* Compiling and checking tests"); |
| 65 | + |
| 66 | + if (!JUnitAnalyzer.isJavaCompilerAvailable()) { |
| 67 | + String msg = "No Java compiler is available. Make sure to run EvoSuite with the JDK and not the JRE." |
| 68 | + + "You can try to setup the JAVA_HOME system variable to point to it, as well as to make sure that the PATH " |
| 69 | + + "variable points to the JDK before any JRE."; |
| 70 | + LOG.error(msg); |
| 71 | + throw new RuntimeException(msg); |
| 72 | + } |
| 73 | + |
| 74 | + ClientServices.getInstance().getClientNode().changeState(ClientState.JUNIT_CHECK); |
| 75 | + |
| 76 | + // Store this value; if this option is true then the JUnit check |
| 77 | + // would not succeed, as the JUnit classloader wouldn't find the class |
| 78 | + boolean junitSeparateClassLoader = Properties.USE_SEPARATE_CLASSLOADER; |
| 79 | + Properties.USE_SEPARATE_CLASSLOADER = false; |
| 80 | + |
| 81 | + int numUnstable = 0; |
| 82 | + |
| 83 | + // note: compiling and running JUnit tests can be very time consuming |
| 84 | + if (!TimeController.getInstance().isThereStillTimeInThisPhase()) { |
| 85 | + Properties.USE_SEPARATE_CLASSLOADER = junitSeparateClassLoader; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + List<TestCase> testCases = chromosome.getTests(); // make copy of |
| 90 | + // current tests |
| 91 | + |
| 92 | + // first, let's just get rid of all the tests that do not compile |
| 93 | + JUnitAnalyzer.removeTestsThatDoNotCompile(testCases); |
| 94 | + |
| 95 | + // compile and run each test one at a time. and keep track of total time |
| 96 | + long start = java.lang.System.currentTimeMillis(); |
| 97 | + Iterator<TestCase> iter = testCases.iterator(); |
| 98 | + while (iter.hasNext()) { |
| 99 | + if (!TimeController.getInstance().hasTimeToExecuteATestCase()) { |
| 100 | + break; |
| 101 | + } |
| 102 | + TestCase tc = iter.next(); |
| 103 | + List<TestCase> list = new ArrayList<>(); |
| 104 | + list.add(tc); |
| 105 | + numUnstable += JUnitAnalyzer.handleTestsThatAreUnstable(list); |
| 106 | + if (list.isEmpty()) { |
| 107 | + // if the test was unstable and deleted, need to remove it from |
| 108 | + // final testSuite |
| 109 | + iter.remove(); |
| 110 | + } |
| 111 | + } |
| 112 | + /* |
| 113 | + * compiling and running each single test individually will take more |
| 114 | + * than compiling/running everything in on single suite. so it can be |
| 115 | + * used as an upper bound |
| 116 | + */ |
| 117 | + long delta = java.lang.System.currentTimeMillis() - start; |
| 118 | + |
| 119 | + numUnstable += checkAllTestsIfTime(testCases, delta); |
| 120 | + |
| 121 | + // second passage on reverse order, this is to spot dependencies among |
| 122 | + // tests |
| 123 | + if (testCases.size() > 1) { |
| 124 | + Collections.reverse(testCases); |
| 125 | + numUnstable += checkAllTestsIfTime(testCases, delta); |
| 126 | + } |
| 127 | + |
| 128 | + chromosome.clearTests(); // remove all tests |
| 129 | + for (TestCase testCase : testCases) { |
| 130 | + chromosome.addTest(testCase); // add back the filtered tests |
| 131 | + } |
| 132 | + |
| 133 | + boolean unstable = (numUnstable > 0); |
| 134 | + |
| 135 | + if (!TimeController.getInstance().isThereStillTimeInThisPhase()) { |
| 136 | + LOG.warn("JUnit checking timed out"); |
| 137 | + } |
| 138 | + |
| 139 | + ClientServices.track(RuntimeVariable.HadUnstableTests, unstable); |
| 140 | + ClientServices.track(RuntimeVariable.NumUnstableTests, numUnstable); |
| 141 | + Properties.USE_SEPARATE_CLASSLOADER = junitSeparateClassLoader; |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + private static int checkAllTestsIfTime(List<TestCase> testCases, long delta) { |
| 147 | + if (TimeController.getInstance().hasTimeToExecuteATestCase() |
| 148 | + && TimeController.getInstance().isThereStillTimeInThisPhase(delta)) { |
| 149 | + return JUnitAnalyzer.handleTestsThatAreUnstable(testCases); |
| 150 | + } |
| 151 | + return 0; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + public static TestGenerationResult writeJUnitTestsAndCreateResult(TestSuiteChromosome testSuite, String suffix) { |
| 156 | + List<TestCase> tests = testSuite.getTests(); |
| 157 | + if (Properties.JUNIT_TESTS) { |
| 158 | + ClientServices.getInstance().getClientNode().changeState(ClientState.WRITING_TESTS); |
| 159 | + |
| 160 | + TestSuiteWriter suiteWriter = new TestSuiteWriter(); |
| 161 | + suiteWriter.insertTests(tests); |
| 162 | + |
| 163 | + String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1); |
| 164 | + String testDir = Properties.TEST_DIR; |
| 165 | + |
| 166 | + LOG.info("* Writing JUnit test case '" + (name + suffix) + "' to " + testDir); |
| 167 | + suiteWriter.writeTestSuite(name + suffix, testDir, testSuite.getLastExecutionResults()); |
| 168 | + } |
| 169 | + return TestGenerationResultBuilder.buildSuccessResult(); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + public static void writeJUnitFailingTests() { |
| 175 | + if (!Properties.CHECK_CONTRACTS) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + FailingTestSet.sendStatistics(); |
| 180 | + |
| 181 | + if (Properties.JUNIT_TESTS) { |
| 182 | + |
| 183 | + TestSuiteWriter suiteWriter = new TestSuiteWriter(); |
| 184 | + //suiteWriter.insertTests(FailingTestSet.getFailingTests()); |
| 185 | + |
| 186 | + TestSuiteChromosome suite = new TestSuiteChromosome(); |
| 187 | + for(TestCase test : FailingTestSet.getFailingTests()) { |
| 188 | + test.setFailing(); |
| 189 | + suite.addTest(test); |
| 190 | + } |
| 191 | + |
| 192 | + String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1); |
| 193 | + String testDir = Properties.TEST_DIR; |
| 194 | + LOG.info("* Writing failing test cases '" + (name + Properties.JUNIT_SUFFIX) + "' to " + testDir); |
| 195 | + suiteWriter.insertAllTests(suite.getTests()); |
| 196 | + FailingTestSet.writeJUnitTestSuite(suiteWriter); |
| 197 | + |
| 198 | + suiteWriter.writeTestSuite(name + Properties.JUNIT_FAILED_SUFFIX, testDir, suite.getLastExecutionResults()); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments