private void mergeTestHistoryResult()

in maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java [243:307]


    private void mergeTestHistoryResult() {
        globalStats = new RunStatistics();
        flakyTests = new TreeMap<>();
        failedTests = new TreeMap<>();
        errorTests = new TreeMap<>();

        Map<String, List<TestMethodStats>> mergedTestHistoryResult = new HashMap<>();
        // Merge all the stats for tests from listeners
        for (TestSetRunListener listener : listeners) {
            for (TestMethodStats methodStats : listener.getTestMethodStats()) {
                List<TestMethodStats> currentMethodStats =
                        mergedTestHistoryResult.get(methodStats.getTestClassMethodName());
                if (currentMethodStats == null) {
                    currentMethodStats = new ArrayList<>();
                    currentMethodStats.add(methodStats);
                    mergedTestHistoryResult.put(methodStats.getTestClassMethodName(), currentMethodStats);
                } else {
                    currentMethodStats.add(methodStats);
                }
            }
        }

        // Update globalStatistics by iterating through mergedTestHistoryResult
        int completedCount = 0, skipped = 0;

        for (Map.Entry<String, List<TestMethodStats>> entry : mergedTestHistoryResult.entrySet()) {
            List<TestMethodStats> testMethodStats = entry.getValue();
            String testClassMethodName = entry.getKey();
            completedCount++;

            List<ReportEntryType> resultTypes = new ArrayList<>();
            for (TestMethodStats methodStats : testMethodStats) {
                resultTypes.add(methodStats.getResultType());
            }

            switch (getTestResultType(resultTypes, reportConfiguration.getRerunFailingTestsCount())) {
                case success:
                    // If there are multiple successful runs of the same test, count all of them
                    int successCount = 0;
                    for (ReportEntryType type : resultTypes) {
                        if (type == SUCCESS) {
                            successCount++;
                        }
                    }
                    completedCount += successCount - 1;
                    break;
                case skipped:
                    skipped++;
                    break;
                case flake:
                    flakyTests.put(testClassMethodName, testMethodStats);
                    break;
                case failure:
                    failedTests.put(testClassMethodName, testMethodStats);
                    break;
                case error:
                    errorTests.put(testClassMethodName, testMethodStats);
                    break;
                default:
                    throw new IllegalStateException("Get unknown test result type");
            }
        }

        globalStats.set(completedCount, errorTests.size(), failedTests.size(), skipped, flakyTests.size());
    }