|
12 | 12 | import java.util.List; |
13 | 13 | import java.util.Map; |
14 | 14 | import java.util.Set; |
15 | | -import java.util.Map.Entry; |
| 15 | +//import java.util.Map.Entry; |
16 | 16 |
|
17 | 17 | import org.junit.runner.notification.Failure; |
18 | 18 |
|
@@ -48,7 +48,8 @@ public class Core { |
48 | 48 | private Map<String, List<String>> mutantsFolders; |
49 | 49 | private Exception error; |
50 | 50 | private MutationScore ms; |
51 | | - private int generation = -1 ; |
| 51 | + //private int generation = -1 ; |
| 52 | + private List<MutantInfo> lastGeneration = null; |
52 | 53 | public static boolean fullVerbose = false; |
53 | 54 | public static boolean showSurvivingMutants = false; |
54 | 55 | public static final int mujavappVersion = 20151011; |
@@ -111,7 +112,8 @@ public boolean generateMutants(String className, String[] methods, Mutant[] mutO |
111 | 112 | GenerationsInformation generationsInfo= generator.generate(false, true); |
112 | 113 | if (Core.fullVerbose) System.out.println(generationsInfo.showBasicInformation()); |
113 | 114 | this.mutantsFolders = generator.getMutantsFolderForGeneration(generation); |
114 | | - this.generation = generation; |
| 115 | + //this.generation = generation; |
| 116 | + this.lastGeneration = generationsInfo.getLastGeneration(); |
115 | 117 | } catch (Exception e) { |
116 | 118 | this.error = e; |
117 | 119 | } |
@@ -168,55 +170,110 @@ public String getOutputDir() { |
168 | 170 | } |
169 | 171 |
|
170 | 172 | public float calculateMutationScore(String[] testClasses, String className) { |
| 173 | + if (this.lastGeneration == null) { |
| 174 | + this.error = new IllegalStateException("There are no recorder mutants in the last generation"); |
| 175 | + return -1; |
| 176 | + } |
171 | 177 | List<String> survivingMutantsPaths = new LinkedList<>(); |
172 | 178 | int failedToCompile = 0; |
173 | 179 | int mutantsKilled = 0; |
174 | 180 | int mutants = 0; |
175 | 181 | int timedOut = 0; |
176 | | - for (Entry<String, List<String>> entry : lastMutantsFolder().entrySet()) { |
177 | | - for (String path : entry.getValue()) { |
178 | | - mutants++; |
179 | | - String currGen = "generation-" + this.generation; |
180 | | - String pathToFile = currGen + SEPARATOR + entry.getKey() + SEPARATOR + path; |
181 | | - String fullPathToJavaFile = pathToFile + className.replaceAll("\\.", SEPARATOR)+".java"; |
182 | | - if (!ms.compile(fullPathToJavaFile)){ |
183 | | - System.out.println("File : " + Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java" + " didn't compile\n"); |
184 | | - failedToCompile++; |
185 | | - continue; |
186 | | - } |
187 | | - boolean killed = false; |
188 | | - List<TestResult> results = ms.runTestsWithMutants(Arrays.asList(testClasses), pathToFile, className); |
189 | | - if (results == null) { |
190 | | - System.out.println("An error ocurred while running tests for mutants"); |
191 | | - System.out.println(ms.getLastError()!=null?ms.getLastError().toString():"no exception to display, contact your favorite mujava++ developer"); |
192 | | - return -1; |
193 | | - } |
194 | | - for (TestResult r : results) { |
195 | | - //System.out.println("Runned : " + r.getRunCount() + " tests (pass : " + (r.getRunCount()-r.getFailureCount()) + " | failed : " + r.getFailureCount() + ")\n"); |
196 | | - System.out.println(r.toString()+"\n"); |
197 | | - if (!r.wasSuccessful()) { |
198 | | - if (r.getTimedoutTests() > 0) timedOut++; |
199 | | - for (Failure f : r.getFailures()) { |
200 | | - if (Core.fullVerbose || toughnessAnalysis()) System.out.println("mutant : " + Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java"); |
201 | | - if (toughnessAnalysis()) { |
202 | | - float toughness = 1.0f - ((r.getTotalFailures() * 1.0f) / (r.getRunnedTestsCount() * 1.0f)); |
203 | | - this.addToughnessValue(toughness); |
204 | | - System.out.println("Toughness: " + toughness + " [failed : " + r.getTotalFailures() + " | total : " + r.getRunnedTestsCount() + "]"); |
205 | | - } |
206 | | - if (Core.fullVerbose) System.out.println("test : " + f.getTestHeader()); |
207 | | - if (Core.fullVerbose) System.out.println("failure description: " + f.getDescription()); |
208 | | - if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("exception: " + f.getException()); |
209 | | - if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("trace: " + f.getTrace()); |
210 | | - } |
| 182 | + for (MutantInfo mut : this.lastGeneration) { |
| 183 | + mutants++; |
| 184 | + String pathToFile = mut.getPath(); |
| 185 | + if (!ms.compile(pathToFile)){ |
| 186 | + System.out.println("File : " + pathToFile + " didn't compile\n"); //Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java" + " didn't compile\n"); |
| 187 | + failedToCompile++; |
| 188 | + continue; |
| 189 | + } |
| 190 | + boolean killed = false; |
| 191 | + List<TestResult> results = ms.runTestsWithMutants(Arrays.asList(testClasses), mut); |
| 192 | + if (results == null) { |
| 193 | + System.out.println("An error ocurred while running tests for mutants"); |
| 194 | + System.out.println(ms.getLastError()!=null?ms.getLastError().toString():"no exception to display, contact your favorite mujava++ developer"); |
| 195 | + return -1; |
| 196 | + } |
| 197 | + int runnedTestsCount = 0; |
| 198 | + int totalFailures = 0; |
| 199 | + for (TestResult r : results) { |
| 200 | + //System.out.println("Runned : " + r.getRunCount() + " tests (pass : " + (r.getRunCount()-r.getFailureCount()) + " | failed : " + r.getFailureCount() + ")\n"); |
| 201 | + System.out.println(r.toString()+"\n"); |
| 202 | + runnedTestsCount += r.getRunnedTestsCount(); |
| 203 | + totalFailures += r.getTotalFailures(); |
| 204 | + if (!r.wasSuccessful()) { |
| 205 | + if (r.getTimedoutTests() > 0) timedOut++; |
| 206 | + for (Failure f : r.getFailures()) { |
| 207 | +// if (Core.fullVerbose || toughnessAnalysis()) System.out.println("mutant : " + mut.getPath());//Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java"); |
| 208 | +// if (toughnessAnalysis()) { |
| 209 | +// float toughness = 1.0f - ((r.getTotalFailures() * 1.0f) / (r.getRunnedTestsCount() * 1.0f)); |
| 210 | +// this.addToughnessValue(toughness); |
| 211 | +// System.out.println("Toughness: " + toughness + " [failed : " + r.getTotalFailures() + " | total : " + r.getRunnedTestsCount() + "]"); |
| 212 | +// } |
| 213 | + if (Core.fullVerbose) System.out.println("test : " + f.getTestHeader()); |
| 214 | + if (Core.fullVerbose) System.out.println("failure description: " + f.getDescription()); |
| 215 | + if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("exception: " + f.getException()); |
| 216 | + if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("trace: " + f.getTrace()); |
211 | 217 | } |
212 | | - if (!killed && !r.wasSuccessful()) killed = true; |
213 | | - } |
214 | | - if (killed) mutantsKilled++; |
215 | | - if (!killed && Core.showSurvivingMutants) { |
216 | | - survivingMutantsPaths.add(fullPathToJavaFile); |
217 | 218 | } |
| 219 | + if (!killed && !r.wasSuccessful()) killed = true; |
| 220 | + } |
| 221 | + if (toughnessAnalysis()) { |
| 222 | + float toughness = 1.0f - ((totalFailures * 1.0f) / (runnedTestsCount * 1.0f)); |
| 223 | + this.addToughnessValue(toughness); |
| 224 | + System.out.println("Toughness: " + toughness + " [failed : " + totalFailures + " | total : " + runnedTestsCount + "]"); |
| 225 | + System.out.println(); |
| 226 | + } |
| 227 | + if (killed) mutantsKilled++; |
| 228 | + if (!killed && Core.showSurvivingMutants) { |
| 229 | + survivingMutantsPaths.add(pathToFile); |
218 | 230 | } |
| 231 | + |
219 | 232 | } |
| 233 | +// for (Entry<String, List<String>> entry : lastMutantsFolder().entrySet()) { |
| 234 | +// for (String path : entry.getValue()) { |
| 235 | +// mutants++; |
| 236 | +// String currGen = "generation-" + this.generation; |
| 237 | +// String pathToFile = currGen + SEPARATOR + entry.getKey() + SEPARATOR + path; |
| 238 | +// String fullPathToJavaFile = pathToFile + className.replaceAll("\\.", SEPARATOR)+".java"; |
| 239 | +// if (!ms.compile(fullPathToJavaFile)){ |
| 240 | +// System.out.println("File : " + Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java" + " didn't compile\n"); |
| 241 | +// failedToCompile++; |
| 242 | +// continue; |
| 243 | +// } |
| 244 | +// boolean killed = false; |
| 245 | +// List<TestResult> results = ms.runTestsWithMutants(Arrays.asList(testClasses), pathToFile, className); |
| 246 | +// if (results == null) { |
| 247 | +// System.out.println("An error ocurred while running tests for mutants"); |
| 248 | +// System.out.println(ms.getLastError()!=null?ms.getLastError().toString():"no exception to display, contact your favorite mujava++ developer"); |
| 249 | +// return -1; |
| 250 | +// } |
| 251 | +// for (TestResult r : results) { |
| 252 | +// //System.out.println("Runned : " + r.getRunCount() + " tests (pass : " + (r.getRunCount()-r.getFailureCount()) + " | failed : " + r.getFailureCount() + ")\n"); |
| 253 | +// System.out.println(r.toString()+"\n"); |
| 254 | +// if (!r.wasSuccessful()) { |
| 255 | +// if (r.getTimedoutTests() > 0) timedOut++; |
| 256 | +// for (Failure f : r.getFailures()) { |
| 257 | +// if (Core.fullVerbose || toughnessAnalysis()) System.out.println("mutant : " + Core.outputDir + pathToFile + className.replaceAll("\\.", SEPARATOR)+".java"); |
| 258 | +// if (toughnessAnalysis()) { |
| 259 | +// float toughness = 1.0f - ((r.getTotalFailures() * 1.0f) / (r.getRunnedTestsCount() * 1.0f)); |
| 260 | +// this.addToughnessValue(toughness); |
| 261 | +// System.out.println("Toughness: " + toughness + " [failed : " + r.getTotalFailures() + " | total : " + r.getRunnedTestsCount() + "]"); |
| 262 | +// } |
| 263 | +// if (Core.fullVerbose) System.out.println("test : " + f.getTestHeader()); |
| 264 | +// if (Core.fullVerbose) System.out.println("failure description: " + f.getDescription()); |
| 265 | +// if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("exception: " + f.getException()); |
| 266 | +// if (Core.fullVerbose && !(f.getException() instanceof java.lang.AssertionError)) System.out.println("trace: " + f.getTrace()); |
| 267 | +// } |
| 268 | +// } |
| 269 | +// if (!killed && !r.wasSuccessful()) killed = true; |
| 270 | +// } |
| 271 | +// if (killed) mutantsKilled++; |
| 272 | +// if (!killed && Core.showSurvivingMutants) { |
| 273 | +// survivingMutantsPaths.add(fullPathToJavaFile); |
| 274 | +// } |
| 275 | +// } |
| 276 | +// } |
220 | 277 | System.out.println("Mutants : "+ mutants + " | didn't compile : " + failedToCompile + " | mutants killed by tests : "+ mutantsKilled + " | surviving mutants : " + (mutants-failedToCompile-mutantsKilled) + " | total tests that timedout : " + timedOut + " | mutation score : "+((mutantsKilled+failedToCompile)*100.0)/mutants+ " | mutation score (only compiling mutants) : " + (mutantsKilled*100.0)/(mutants-failedToCompile) + '\n'); |
221 | 278 | if (Core.showSurvivingMutants) { |
222 | 279 | System.out.println("Surviving mutants paths:\n"); |
|
0 commit comments