001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.terminal.report;
018
019import static org.apache.juneau.terminal.report.ReportText.*;
020
021import java.io.*;
022import java.nio.file.*;
023import java.util.*;
024
025/**
026 * Reads Maven Surefire (and Failsafe) <c>TEST-*.xml</c> reports.
027 *
028 * <p>
029 * The path may be a report directory, in which case every <c>TEST-*.xml</c> in it is read in name order, or a single file.
030 * The framework key is <c>surefire</c>.
031 */
032public final class SurefireReportReader implements ReportReader {
033
034   @Override /* ReportReader */
035   public String fw() {
036      return "surefire";
037   }
038
039   @Override /* ReportReader */
040   public ReportResult read(Path path, ReportLimits limits) {
041      var out = new ReportBuilder();
042      if (Files.isDirectory(path)) {
043         var files = new ArrayList<Path>();
044         try (var ds = Files.newDirectoryStream(path, "TEST-*.xml")) {
045            for (var p : ds)
046               files.add(p);
047         } catch (IOException e) {
048            out.warnings.add("report unreadable: cannot list " + fileName(path));
049            return out.build(fw());
050         }
051         if (files.isEmpty()) {
052            out.warnings.add("no TEST-*.xml files in " + fileName(path));
053            return out.build(fw());
054         }
055         files.sort(Comparator.comparing(p -> p.getFileName().toString()));
056         for (var p : files) {
057            if (out.truncated && out.tests.size() >= limits.maxTests())
058               break;
059            JUnitXmlParser.parse(p, limits, out);
060         }
061      } else {
062         JUnitXmlParser.parse(path, limits, out);
063      }
064      return out.build(fw());
065   }
066}