|
| 1 | +/* |
| 2 | + * SonarSource :: .NET :: Core |
| 3 | + * Copyright (C) 2014-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.plugins.dotnet.tests; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.text.DateFormat; |
| 22 | +import java.text.ParseException; |
| 23 | +import java.text.SimpleDateFormat; |
| 24 | +import java.util.Date; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +public class VisualStudioTestResultParser implements UnitTestResultParser { |
| 33 | + |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(VisualStudioTestResultParser.class); |
| 35 | + private final Map<String, String> methodFileMap; |
| 36 | + |
| 37 | + VisualStudioTestResultParser(Map<String, String> methodFileMap) { |
| 38 | + this.methodFileMap = methodFileMap; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void accept(File file, Map<String, UnitTestResults> unitTestResults) { |
| 43 | + LOG.info("Parsing the Visual Studio Test Results file '{}'.", file.getAbsolutePath()); |
| 44 | + new Parser(file, unitTestResults, this.methodFileMap).parse(); |
| 45 | + } |
| 46 | + |
| 47 | + private static class Parser { |
| 48 | + private final File file; |
| 49 | + private final Map<String, UnitTestResults> testIdTestResultMap; |
| 50 | + private final Map<String, UnitTestResults> unitTestResults; |
| 51 | + // Date Format: // https://github.com/microsoft/vstest/blob/7d34b30433259fb914aaaf276fde663a47b6ef2f/src/Microsoft.TestPlatform.Extensions.TrxLogger/XML/XmlPersistence.cs#L557-L572 |
| 52 | + private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); |
| 53 | + private final Pattern millisecondsPattern = Pattern.compile("(\\.(\\d{0,3}))\\d*+"); |
| 54 | + private final Map<String, String> methodFileMap; |
| 55 | + |
| 56 | + Parser(File file, Map<String, UnitTestResults> unitTestResults, Map<String, String> methodFileMap) { |
| 57 | + this.file = file; |
| 58 | + this.unitTestResults = unitTestResults; |
| 59 | + this.testIdTestResultMap = new HashMap<>(); |
| 60 | + this.methodFileMap = methodFileMap; |
| 61 | + } |
| 62 | + |
| 63 | + public void parse() { |
| 64 | + try (XmlParserHelper xmlParserHelper = new XmlParserHelper(file)) { |
| 65 | + checkRootTag(xmlParserHelper); |
| 66 | + dispatchTags(xmlParserHelper); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new IllegalStateException("Unable to close report", e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void dispatchTags(XmlParserHelper xmlParserHelper) { |
| 73 | + String tagName; |
| 74 | + while ((tagName = xmlParserHelper.nextStartTag()) != null) { |
| 75 | + if ("UnitTestResult".equals(tagName)) { |
| 76 | + handleUnitTestResultTag(xmlParserHelper); |
| 77 | + } else if ("UnitTest".equals(tagName)) { |
| 78 | + handleUnitTestTag(xmlParserHelper); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void handleUnitTestResultTag(XmlParserHelper xmlParserHelper) { |
| 84 | + String testId = xmlParserHelper.getRequiredAttribute("testId"); |
| 85 | + String outcome = xmlParserHelper.getRequiredAttribute("outcome"); |
| 86 | + Date start = getRequiredDateAttribute(xmlParserHelper, "startTime"); |
| 87 | + Date finish = getRequiredDateAttribute(xmlParserHelper, "endTime"); |
| 88 | + long duration = finish.getTime() - start.getTime(); |
| 89 | + |
| 90 | + var testResult = new VisualStudioTestResults(outcome, duration); |
| 91 | + testIdTestResultMap.put(testId, testResult); |
| 92 | + LOG.debug("Parsed Visual Studio Unit Test - testId: {} outcome: {}, startTime: {}, endTime: {}", |
| 93 | + testId, outcome, start, finish); |
| 94 | + } |
| 95 | + |
| 96 | + private void handleUnitTestTag(XmlParserHelper xmlParserHelper) { |
| 97 | + String testId = xmlParserHelper.getRequiredAttribute("id"); |
| 98 | + |
| 99 | + String tagName; |
| 100 | + while ((tagName = xmlParserHelper.nextStartTag()) != null) { |
| 101 | + if ("TestMethod".equals(tagName)) { |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + if (tagName == null){ |
| 106 | + throw new ParseErrorException("No TestMethod attribute found on UnitTest tag"); |
| 107 | + } |
| 108 | + |
| 109 | + String methodName = xmlParserHelper.getRequiredAttribute("name"); |
| 110 | + String className = xmlParserHelper.getRequiredAttribute("className"); |
| 111 | + String codeBase = xmlParserHelper.getRequiredAttribute("codeBase"); |
| 112 | + |
| 113 | + String dllName = codeBase.substring(codeBase.lastIndexOf(File.separator) + 1, codeBase.lastIndexOf('.')); |
| 114 | + |
| 115 | + String fullyQualifiedName = dllName + "." + className + "." + methodName; |
| 116 | + |
| 117 | + associateTestIdTestResultWithFile(testId, fullyQualifiedName); |
| 118 | + } |
| 119 | + |
| 120 | + public void associateTestIdTestResultWithFile(String testId, String methodFullName) { |
| 121 | + if(!methodFileMap.containsKey(methodFullName)) { |
| 122 | + throw new IllegalStateException(String.format("Test method %s with testId %s cannot be mapped to the test source file", methodFullName, testId)); |
| 123 | + } |
| 124 | + |
| 125 | + String fileName = methodFileMap.get(methodFullName); |
| 126 | + |
| 127 | + if (unitTestResults.containsKey(fileName)) { |
| 128 | + var fileTestResult = unitTestResults.get(fileName); |
| 129 | + var testIdTestResult = testIdTestResultMap.get(testId); |
| 130 | + fileTestResult.add(testIdTestResult); |
| 131 | + } else { |
| 132 | + unitTestResults.put(fileName, testIdTestResultMap.get(testId)); |
| 133 | + } |
| 134 | + |
| 135 | + LOG.debug("Associated Visual Studio Unit Test to File - file: {}, TestId: {} MethodFullName: {}", |
| 136 | + fileName, testId, methodFullName); |
| 137 | + } |
| 138 | + |
| 139 | + private Date getRequiredDateAttribute(XmlParserHelper xmlParserHelper, String name) { |
| 140 | + String value = xmlParserHelper.getRequiredAttribute(name); |
| 141 | + try { |
| 142 | + value = keepOnlyMilliseconds(value); |
| 143 | + return dateFormat.parse(value); |
| 144 | + } catch (ParseException e) { |
| 145 | + throw xmlParserHelper.parseError("Expected a valid date and time instead of \"" + value + "\" for the attribute \"" + name + "\". " + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private String keepOnlyMilliseconds(String value) { |
| 150 | + StringBuilder sb = new StringBuilder(); |
| 151 | + |
| 152 | + Matcher matcher = millisecondsPattern.matcher(value); |
| 153 | + StringBuilder trailingZeros = new StringBuilder(); |
| 154 | + while (matcher.find()) { |
| 155 | + String milliseconds = matcher.group(2); |
| 156 | + trailingZeros.setLength(0); |
| 157 | + trailingZeros.append("0".repeat(Math.max(0, 3 - milliseconds.length()))); |
| 158 | + matcher.appendReplacement(sb, "$1" + trailingZeros); |
| 159 | + } |
| 160 | + matcher.appendTail(sb); |
| 161 | + |
| 162 | + return sb.toString(); |
| 163 | + } |
| 164 | + |
| 165 | + private static void checkRootTag(XmlParserHelper xmlParserHelper) { |
| 166 | + xmlParserHelper.checkRootTag("TestRun"); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments