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.test.IsMeasure;
30 import org.sonar.api.test.IsResource;
32 import java.net.URISyntaxException;
33 import java.util.Arrays;
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 * See http://jira.codehaus.org/browse/SONAR-2371
59 public void shouldInsertZeroWhenNoReports() throws URISyntaxException {
60 AbstractSurefireParser parser = newParser();
61 SensorContext context = mockContext();
62 Project project = mock(Project.class);
64 parser.collect(project, context, getDir("noReports"));
66 verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
70 * See http://jira.codehaus.org/browse/SONAR-2371
73 public void shouldNotInsertZeroWhenNoReports() throws URISyntaxException {
74 AbstractSurefireParser parser = newParser();
75 SensorContext context = mockContext();
76 Project project = mock(Project.class);
77 when(project.getModules()).thenReturn(Arrays.asList(new Project("foo")));
79 parser.collect(project, context, getDir("noReports"));
81 verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0);
85 public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
86 AbstractSurefireParser parser = newParser();
87 SensorContext context = mockContext();
89 parser.collect(new Project("foo"), context, getDir("noTests"));
91 verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble());
95 public void shouldMergeInnerClasses() throws URISyntaxException {
96 AbstractSurefireParser parser = newParser();
98 SensorContext context = mock(SensorContext.class);
99 when(context.isIndexed(argThat(new BaseMatcher<Resource>(){
100 public boolean matches(Object o) {
101 return !((Resource)o).getName().contains("$");
103 public void describeTo(Description description) {
105 }), eq(false))).thenReturn(true);
107 parser.collect(new Project("foo"), context, getDir("innerClasses"));
109 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TESTS), eq(7.0));
110 verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap")), eq(CoreMetrics.TEST_ERRORS), eq(1.0));
111 verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")), any(Metric.class), anyDouble());
115 private AbstractSurefireParser newParser() {
116 return new AbstractSurefireParser() {
118 protected Resource<?> getUnitTestResource(String classKey) {
119 return new File(classKey);
124 private java.io.File getDir(String dirname) throws URISyntaxException {
125 return new java.io.File(getClass().getResource("/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/" + dirname).toURI());
128 private SensorContext mockContext() {
129 SensorContext context = mock(SensorContext.class);
130 when(context.isIndexed(any(Resource.class), eq(false))).thenReturn(true);