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.sonar.api.batch.SensorContext;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Metric;
28 import org.sonar.api.resources.*;
29 import org.sonar.api.resources.File;
30 import org.sonar.api.test.IsMeasure;
31 import org.sonar.api.test.IsResource;
33 import java.net.URISyntaxException;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Matchers.anyDouble;
37 import static org.mockito.Matchers.argThat;
38 import static org.mockito.Matchers.eq;
39 import static org.mockito.Mockito.*;
41 public class AbstractSurefireParserTest {
44 public void shouldAggregateReports() throws URISyntaxException {
45 AbstractSurefireParser parser = newParser();
46 SensorContext context = mockContext();
48 parser.collect(new Project("foo"), context, getDir("multipleReports"));
50 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TESTS), anyDouble());
51 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), eq(CoreMetrics.TEST_ERRORS), anyDouble());
52 verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA)));
56 public void shouldNotFailIfNoReports() throws URISyntaxException {
57 AbstractSurefireParser parser = newParser();
58 SensorContext context = mockContext();
60 parser.collect(new Project("foo"), context, getDir("noReports"));
62 verifyZeroInteractions(context);
66 public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
67 AbstractSurefireParser parser = newParser();
68 SensorContext context = mockContext();
70 parser.collect(new Project("foo"), context, getDir("noTests"));
72 verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble());
76 public void shouldNotInsertMeasuresOnPomProjects() throws URISyntaxException {
77 AbstractSurefireParser parser = newParser();
78 SensorContext context = mockContext();
80 parser.collect(new Project("foo").setPackaging("pom"), context, getDir("noReports"));
82 verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble());
86 public void shouldMergeInnerClasses() throws URISyntaxException {
87 AbstractSurefireParser parser = newParser();
89 SensorContext context = mock(SensorContext.class);
90 when(context.isIndexed(argThat(new BaseMatcher<Resource>(){
91 public boolean matches(Object o) {
92 return !((Resource)o).getName().contains("$");
94 public void describeTo(Description description) {
96 }), eq(false))).thenReturn(true);
98 parser.collect(new Project("foo"), context, getDir("innerClasses"));
100 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TESTS), eq(7.0));
101 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TEST_ERRORS), eq(1.0));
102 verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")), any(Metric.class), anyDouble());
106 private AbstractSurefireParser newParser() {
107 return new AbstractSurefireParser() {
109 protected Resource<?> getUnitTestResource(String classKey) {
110 return new File(classKey);
115 private java.io.File getDir(String dirname) throws URISyntaxException {
116 return new java.io.File(getClass().getResource("/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/" + dirname).toURI());
119 private SensorContext mockContext() {
120 SensorContext context = mock(SensorContext.class);
121 when(context.isIndexed(any(Resource.class), eq(false))).thenReturn(true);