2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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
20 package org.sonar.plugins.surefire.api;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.anyDouble;
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Matchers.argThat;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.net.URISyntaxException;
34 import java.util.Arrays;
36 import org.hamcrest.BaseMatcher;
37 import org.hamcrest.Description;
38 import org.junit.Test;
39 import org.sonar.api.batch.SensorContext;
40 import org.sonar.api.measures.CoreMetrics;
41 import org.sonar.api.measures.Metric;
42 import org.sonar.api.resources.File;
43 import org.sonar.api.resources.Project;
44 import org.sonar.api.resources.Qualifiers;
45 import org.sonar.api.resources.Resource;
46 import org.sonar.api.resources.Scopes;
47 import org.sonar.api.test.IsMeasure;
48 import org.sonar.api.test.IsResource;
50 public class AbstractSurefireParserTest {
53 public void shouldAggregateReports() throws URISyntaxException {
54 AbstractSurefireParser parser = newParser();
55 SensorContext context = mockContext();
57 parser.collect(new Project("foo"), context, getDir("multipleReports"));
59 // Only 6 tests measures should be stored, no more: the TESTS-AllTests.xml must not be read as there's 1 file result per unit test
61 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TESTS), anyDouble());
62 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TEST_ERRORS), anyDouble());
63 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA)));
66 // SONAR-2841: if there's only a test suite report, then it should be read.
68 public void shouldUseTestSuiteReportIfAlone() throws URISyntaxException {
69 AbstractSurefireParser parser = newParser();
70 SensorContext context = mockContext();
72 parser.collect(new Project("foo"), context, getDir("onlyTestSuiteReport"));
74 verify(context, times(2)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TESTS), anyDouble());
75 verify(context, times(2)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TEST_ERRORS), anyDouble());
76 verify(context, times(2)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA)));
80 * See http://jira.codehaus.org/browse/SONAR-2371
83 public void shouldInsertZeroWhenNoReports() throws URISyntaxException {
84 AbstractSurefireParser parser = newParser();
85 SensorContext context = mockContext();
86 Project project = mock(Project.class);
88 parser.collect(project, context, getDir("noReports"));
90 verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
94 * See http://jira.codehaus.org/browse/SONAR-2371
97 public void shouldNotInsertZeroWhenNoReports() throws URISyntaxException {
98 AbstractSurefireParser parser = newParser();
99 SensorContext context = mockContext();
100 Project project = mock(Project.class);
101 when(project.getModules()).thenReturn(Arrays.asList(new Project("foo")));
103 parser.collect(project, context, getDir("noReports"));
105 verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0);
109 public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
110 AbstractSurefireParser parser = newParser();
111 SensorContext context = mockContext();
113 parser.collect(new Project("foo"), context, getDir("noTests"));
115 verify(context, never()).saveMeasure(any(Resource.class), (Metric) anyObject(), anyDouble());
119 public void shouldMergeInnerClasses() throws URISyntaxException {
120 AbstractSurefireParser parser = newParser();
122 SensorContext context = mock(SensorContext.class);
123 when(context.isIndexed(argThat(new BaseMatcher<Resource>() {
124 public boolean matches(Object o) {
125 return !((Resource) o).getName().contains("$");
128 public void describeTo(Description description) {
130 }), eq(false))).thenReturn(true);
132 parser.collect(new Project("foo"), context, getDir("innerClasses"));
135 .saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TESTS), eq(7.0));
136 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TEST_ERRORS),
138 verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")),
139 any(Metric.class), anyDouble());
143 public void shouldMergeNestedInnerClasses() throws URISyntaxException {
144 AbstractSurefireParser parser = newParser();
146 SensorContext context = mockContext();
147 parser.collect(new Project("foo"), context, getDir("nestedInnerClasses"));
149 verify(context).saveMeasure(
150 argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.plugins.surefire.NestedInnerTest")),
151 eq(CoreMetrics.TESTS),
155 private AbstractSurefireParser newParser() {
156 return new AbstractSurefireParser() {
158 protected Resource<?> getUnitTestResource(String classKey) {
159 return new File(classKey);
164 private java.io.File getDir(String dirname) throws URISyntaxException {
165 return new java.io.File(getClass().getResource("/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/" + dirname).toURI());
168 private SensorContext mockContext() {
169 SensorContext context = mock(SensorContext.class);
170 when(context.isIndexed(any(Resource.class), eq(false))).thenReturn(true);