選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SurefireStaxHandler.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.surefire.data;
  21. import java.text.ParseException;
  22. import java.util.Locale;
  23. import javax.xml.stream.XMLStreamException;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.codehaus.staxmate.in.ElementFilter;
  26. import org.codehaus.staxmate.in.SMEvent;
  27. import org.codehaus.staxmate.in.SMHierarchicCursor;
  28. import org.codehaus.staxmate.in.SMInputCursor;
  29. import org.sonar.api.utils.ParsingUtils;
  30. import org.sonar.api.utils.StaxParser.XmlStreamHandler;
  31. public class SurefireStaxHandler implements XmlStreamHandler {
  32. private UnitTestIndex index;
  33. public SurefireStaxHandler(UnitTestIndex index) {
  34. this.index = index;
  35. }
  36. public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
  37. SMInputCursor testSuite = rootCursor.constructDescendantCursor(new ElementFilter("testsuite"));
  38. SMEvent testSuiteEvent;
  39. while ((testSuiteEvent = testSuite.getNext()) != null) {
  40. if (testSuiteEvent.compareTo(SMEvent.START_ELEMENT) == 0) {
  41. String testSuiteClassName = testSuite.getAttrValue("name");
  42. if (StringUtils.contains(testSuiteClassName, "$")) {
  43. // test suites for inner classes are ignored
  44. return;
  45. }
  46. SMInputCursor testCase = testSuite.childCursor(new ElementFilter("testcase"));
  47. SMEvent event;
  48. while ((event = testCase.getNext()) != null) {
  49. if (event.compareTo(SMEvent.START_ELEMENT) == 0) {
  50. String testClassName = getClassname(testCase, testSuiteClassName);
  51. UnitTestClassReport classReport = index.index(testClassName);
  52. parseTestCase(testCase, classReport);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. private String getClassname(SMInputCursor testCaseCursor, String defaultClassname) throws XMLStreamException {
  59. String testClassName = testCaseCursor.getAttrValue("classname");
  60. return StringUtils.defaultIfBlank(testClassName, defaultClassname);
  61. }
  62. private void parseTestCase(SMInputCursor testCaseCursor, UnitTestClassReport report) throws XMLStreamException {
  63. report.add(parseTestResult(testCaseCursor));
  64. }
  65. private void setStackAndMessage(UnitTestResult result, SMInputCursor stackAndMessageCursor) throws XMLStreamException {
  66. result.setMessage(stackAndMessageCursor.getAttrValue("message"));
  67. String stack = stackAndMessageCursor.collectDescendantText();
  68. result.setStackTrace(stack);
  69. }
  70. private UnitTestResult parseTestResult(SMInputCursor testCaseCursor) throws XMLStreamException {
  71. UnitTestResult detail = new UnitTestResult();
  72. String name = getTestCaseName(testCaseCursor);
  73. detail.setName(name);
  74. String status = UnitTestResult.STATUS_OK;
  75. long duration = getTimeAttributeInMS(testCaseCursor);
  76. SMInputCursor childNode = testCaseCursor.descendantElementCursor();
  77. if (childNode.getNext() != null) {
  78. String elementName = childNode.getLocalName();
  79. if ("skipped".equals(elementName)) {
  80. status = UnitTestResult.STATUS_SKIPPED;
  81. // bug with surefire reporting wrong time for skipped tests
  82. duration = 0L;
  83. } else if ("failure".equals(elementName)) {
  84. status = UnitTestResult.STATUS_FAILURE;
  85. setStackAndMessage(detail, childNode);
  86. } else if ("error".equals(elementName)) {
  87. status = UnitTestResult.STATUS_ERROR;
  88. setStackAndMessage(detail, childNode);
  89. }
  90. }
  91. while (childNode.getNext() != null) {
  92. // make sure we loop till the end of the elements cursor
  93. }
  94. detail.setDurationMilliseconds(duration);
  95. detail.setStatus(status);
  96. return detail;
  97. }
  98. private long getTimeAttributeInMS(SMInputCursor testCaseCursor) throws XMLStreamException {
  99. // hardcoded to Locale.ENGLISH see http://jira.codehaus.org/browse/SONAR-602
  100. try {
  101. Double time = ParsingUtils.parseNumber(testCaseCursor.getAttrValue("time"), Locale.ENGLISH);
  102. return !Double.isNaN(time) ? new Double(ParsingUtils.scaleValue(time * 1000, 3)).longValue() : 0L;
  103. } catch (ParseException e) {
  104. throw new XMLStreamException(e);
  105. }
  106. }
  107. private String getTestCaseName(SMInputCursor testCaseCursor) throws XMLStreamException {
  108. String classname = testCaseCursor.getAttrValue("classname");
  109. String name = testCaseCursor.getAttrValue("name");
  110. if (StringUtils.contains(classname, "$")) {
  111. return StringUtils.substringAfter(classname, "$") + "/" + name;
  112. }
  113. return name;
  114. }
  115. }