2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 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 org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.junit.Test;
25 import org.mockito.internal.matchers.StartsWith;
26 import org.sonar.api.batch.SensorContext;
27 import org.sonar.api.measures.CoreMetrics;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.resources.*;
30 import org.sonar.api.resources.File;
31 import org.sonar.api.test.IsMeasure;
32 import org.sonar.api.test.IsResource;
34 import java.net.URISyntaxException;
36 import static org.mockito.Matchers.any;
37 import static org.mockito.Matchers.anyDouble;
38 import static org.mockito.Matchers.argThat;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.*;
42 public class AbstractSurefireParserTest {
45 public void shouldAggregateReports() throws URISyntaxException {
46 AbstractSurefireParser parser = newParser();
47 SensorContext context = mockContext();
49 parser.collect(new Project("foo"), context, getDir("multipleReports"));
51 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TESTS), anyDouble());
52 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TEST_ERRORS), anyDouble());
53 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA)));
57 public void shouldNotFailIfNoReports() throws URISyntaxException {
58 AbstractSurefireParser parser = newParser();
59 SensorContext context = mockContext();
61 parser.collect(new Project("foo"), context, getDir("noReports"));
63 verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
67 public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
68 AbstractSurefireParser parser = newParser();
69 SensorContext context = mockContext();
71 parser.collect(new Project("foo"), context, getDir("noTests"));
73 verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble());
77 public void shouldNotInsertMeasuresOnPomProjects() throws URISyntaxException {
78 AbstractSurefireParser parser = newParser();
79 SensorContext context = mockContext();
81 parser.collect(new Project("foo").setPackaging("pom"), context, getDir("noReports"));
83 verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble());
87 public void shouldMergeInnerClasses() throws URISyntaxException {
88 AbstractSurefireParser parser = newParser();
90 SensorContext context = mock(SensorContext.class);
91 when(context.isIndexed(argThat(new BaseMatcher<Resource>(){
92 public boolean matches(Object o) {
93 return !((Resource)o).getName().contains("$");
95 public void describeTo(Description description) {
97 }), eq(false))).thenReturn(true);
99 parser.collect(new Project("foo"), context, getDir("innerClasses"));
101 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TESTS), eq(7.0));
102 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TEST_ERRORS), eq(1.0));
103 verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")), any(Metric.class), anyDouble());
107 private AbstractSurefireParser newParser() {
108 return new AbstractSurefireParser() {
110 protected Resource<?> getUnitTestResource(String classKey) {
111 return new File(classKey);
116 private java.io.File getDir(String dirname) throws URISyntaxException {
117 return new java.io.File(getClass().getResource("/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/" + dirname).toURI());
120 private SensorContext mockContext() {
121 SensorContext context = mock(SensorContext.class);
122 when(context.isIndexed(any(Resource.class), eq(false))).thenReturn(true);