From 1359b743a19c251e70961541906a1a71dbb46b5b Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 3 Oct 2012 15:06:39 +0200 Subject: [PATCH] SONAR-2804 Add test --- .../plugins/jacoco/JaCoCoAllTestsSensor.java | 3 +- .../jacoco/JaCoCoAllTestsSensorTest.java | 96 ++++++++++++++++++ .../test/resources/HelloWorld.class.toCopy | Bin 0 -> 666 bytes .../jacoco/JaCoCoAllTestsSensorTest/it.exec | Bin 0 -> 5862 bytes .../jacoco/JaCoCoAllTestsSensorTest/ut.exec | Bin 0 -> 5850 bytes 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java create mode 100644 plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy create mode 100644 plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec create mode 100644 plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java index 4a9b53fdf8f..79a24239cb4 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensor.java @@ -43,7 +43,7 @@ import java.io.InputStream; import java.util.Collection; public class JaCoCoAllTestsSensor implements Sensor { - private static final String MERGED_EXEC = "target/merged.exec"; + private static final String MERGED_EXEC = "target/sonar/merged.exec"; private final JacocoConfiguration configuration; @@ -66,6 +66,7 @@ public class JaCoCoAllTestsSensor implements Sensor { File reportUTs = project.getFileSystem().resolvePath(configuration.getReportPath()); File reportITs = project.getFileSystem().resolvePath(configuration.getItReportPath()); File reportAllTests = project.getFileSystem().resolvePath(MERGED_EXEC); + reportAllTests.getParentFile().mkdirs(); SessionInfoStore infoStore = new SessionInfoStore(); ExecutionDataStore dataStore = new ExecutionDataStore(); diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java new file mode 100644 index 00000000000..bc318e66b6f --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java @@ -0,0 +1,96 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.jacoco; + +import com.google.common.io.Files; +import org.junit.Test; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.JavaFile; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.ProjectFileSystem; +import org.sonar.api.resources.Resource; +import org.sonar.test.TestUtils; + +import java.io.File; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +public class JaCoCoAllTestsSensorTest { + private final JacocoConfiguration configuration= mock(JacocoConfiguration.class); + private final SensorContext context = mock(SensorContext.class); + private final ProjectFileSystem pfs = mock(ProjectFileSystem.class); + private final Project project = mock(Project.class); + private final JaCoCoAllTestsSensor sensor = new JaCoCoAllTestsSensor(configuration); + + @Test + public void should_execute_on_project() { + Project project = mock(Project.class); + when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec"); + when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC).thenReturn(Project.AnalysisType.REUSE_REPORTS); + + assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); + } + + @Test + public void do_not_execute_when_report_path_not_specified() { + Project project = mock(Project.class); + when(configuration.getItReportPath()).thenReturn(""); + + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); + } + + @Test + public void should_save_measures() throws IOException { + File outputDir = TestUtils.getResource(JaCoCoAllTestsSensorTest.class, "."); + Files.copy(TestUtils.getResource("HelloWorld.class.toCopy"), new File(outputDir, "HelloWorld.class")); + + JavaFile resource = new JavaFile("com.sonar.coverages.HelloWorld"); + + when(project.getFileSystem()).thenReturn(pfs); + when(context.getResource(any(Resource.class))).thenReturn(resource); + when(configuration.getReportPath()).thenReturn("ut.exec"); + when(configuration.getItReportPath()).thenReturn("it.exec"); + when(pfs.getBuildOutputDir()).thenReturn(outputDir); + when(pfs.resolvePath("ut.exec")).thenReturn(new File(outputDir, "ut.exec")); + when(pfs.resolvePath("it.exec")).thenReturn(new File(outputDir, "it.exec")); + when(pfs.resolvePath("target/sonar/merged.exec")).thenReturn(new File("target/sonar/merged.exec")); + + sensor.analyse(project, context); + + verify(context).getResource(resource); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_LINES_TO_COVER, 5.0)); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, 0.0)); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_COVERAGE_LINE_HITS_DATA, "3=1;6=1;7=1;10=1;11=1")); + verifyNoMoreInteractions(context); + } + + @Test + public void testSensorDefinition() { + assertThat(sensor.toString()).isEqualTo("JaCoCoAllTestsSensor"); + } +} diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy new file mode 100644 index 0000000000000000000000000000000000000000..e9d34004571d88f34d608f68ea33b6bfbdc7d84a GIT binary patch literal 666 zcmaKpOHTqZ6opR#85qVFiYUI&mAY`^$_2)aiP40p35e@5Hdr&Xrc+G(S-Q}~g+IU_ zWxO2>XhNJtALrilooV~|_5K0i0NXx1EEKVbN&!ngs#q>y#Y4@*DntHADrt`yvOBvS zhTMt16%4aYsl>&g-xa3KyD=%{rjB^r;YPaskC?M}GGVAUBi#=Zt+)vz{U{9Yi6lG| zajb8&iEkSW3kiQpg|@z?ytYUz8Ea$pTv^dG+)Ax}ihfHEOe9XFi=Q3S_V4*44^TkS z!&-oKY%t8F<+?-HQ@e-F0Cj9JREMQs{9y;f#{Yj!RF@RST=l}s?!AcYAF1_}Skb48 z(*r70nrXAt;gylfwyY7n?+!jeX7C@6hGc=|t<>}Sjmj!cYrO~Hd zNHe2(YVQr~nHCw8Xw0W#76Gk;VFRWyLp!>uGCSYoAsN)cQPji;uNZrLL>1tgCEox3y{&>syYmb=BR2?43tUcx0e^mOqk{!`wS} zzTfxzzVA1>`g&8K3B;t+6-==ZmrL7IdA>Z(iun;NZ>QO~;*rHkqb&&u0O*oqw_Wz{dg;5EE5rvVH$0C(4e<7Spb95=f8MIyG zv3uyQZ6_K>!54Zn+p|B2aw^Br+$W_-5HVTo+0WcuJw7rR{<-fO>*N@`yClzVZPkHOK>+IaSmy`W*4BcNU2Y_yVMC%#q(z48W*1C2+X%4v;@sRaF@_d&9jzVDVnop~l$7B7w>$ zpYo}u(kxRc?MN_OSF68v?1ZJ{%HQk-zFS;2t49F zhk^{n$(*ti9C?l$g3BRzBEr339X!71sxUQe<4O=@CVZ}R$45Y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yPMzZ|G)KBA!E=cR)n1!o81$c&Z6Rq*8~7fB941hIGY+1aKe z-Yy%F<0uId1(&{@Ls)aeE~tAduHtWFH}LJp7)X zoE5VvfAepNSQ2n4CtnZ!;rQT?E>TV(gH_9d`k@P3&%Ee)Bb3+=nuFoduLVj((|^#) z_;R7G7>T0Wib-H3c}W>I;?7Rjx3&U!{eff8H_g7a$iFp86S*u)B=qhr+`iSin%=2} zxmQhm-);2Ru7h7iWhS>o#J>gxYBTGJ)@`lVp8}P^uyn~k=KeTv)7&mm8c87iFn&gj zX?CwqK#KpGc78as&ul?du+kk61Pdl+PoFjTg-Jku1JJq7w_&y?3CG;BmzU|_!w0U? ZtLJ@kygRwY_qB)F#=Ec`2~Y=g{{=U^to;B0 literal 0 HcmV?d00001 diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec new file mode 100644 index 0000000000000000000000000000000000000000..09d843736410891e1cf489c50d672ee5f7389cad GIT binary patch literal 5850 zcmb_gdsGuw9==HkAPDA)4{Sxz#z$>&z@R8vEe|PBkw~h@TB}32z{q4K-kBh&4_vEt zTTfftTKlwEpw{QHTVHEMMc36;)>V$yZ56Fzt+ke8t=m<153+Y2G2xMc?pgmJC&%17 z_kO?c_kG`QbhWjnKof{grAwItAtsBqC-Zzsj1}`ES>8^wF{AAX`C|(#0O%5j?$21E zd}da_9W(Gdvv1D*9K`Xsh??i(7?EN)5n+yIDeU4ngayjM7a*3JOxuf}LxMP!7nwo^ z;ZU6cesW;E<>N(DzXb`>a@t9Lf+z=Fia1JeVN}RqL}BFQu}I~}9|&jB99@KP8f_PO z>>hSt_r>}#@P(etp3L{6oXRmY_eshZL`)WY_A@tCO^OVLf9bu!dL^ko9*pkvBv~$2 zWbCvcGF*{om&Edx79F@81fc$~wLHMKaR(UH=|&P7HYD0aT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