Skip to content

Commit

Permalink
Configure Jacoco for multimodule project in SonarQube
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Suszynski authored and cardil committed Jun 6, 2019
1 parent be8c902 commit 46084d9
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 77 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<sonar.host.url>https://sonar.wavesoftware.pl</sonar.host.url>
<sonar.jacoco.dataPath>${project.rootdir}/target/jacoco.exec</sonar.jacoco.dataPath>
<sonar.jacoco.itDataPath>${project.rootdir}/target/jacoco-it.exec</sonar.jacoco.itDataPath>
<sonar.jacoco.reportPaths>${project.rootdir}/target/jacoco.exec,${project.rootdir}/target/jacoco-it.exec</sonar.jacoco.reportPaths>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<java.source.version>8</java.source.version>
<sonar.java.source>${java.source.version}</sonar.java.source>
Expand Down
35 changes: 20 additions & 15 deletions tools/plugs-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@
<artifactId>maven-common-artifact-filters</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-wagon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>pl.wavesoftware</groupId>
Expand Down Expand Up @@ -151,6 +136,26 @@
<artifactId>maven-embedder</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-wagon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2019 Wave Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pl.wavesoftware.plugs.tools.maven.plugin;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import pl.wavesoftware.maven.testing.junit5.MavenInvoker;
import pl.wavesoftware.maven.testing.junit5.MavenInvokerExtension;
import pl.wavesoftware.maven.testing.junit5.MavenProjectCustomizer;
import pl.wavesoftware.maven.testing.junit5.MojoExtension;

import java.io.File;
import java.nio.file.Path;

import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute;
import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull;

@ExtendWith({
MojoExtension.class,
MavenInvokerExtension.class
})
abstract class BaseMavenTestCase {
private final Path pomDirectory;
private final MavenProjectCustomizer customizer;

BaseMavenTestCase(Path pomDirectory) {
this.pomDirectory = pomDirectory;
customizer = project -> {
File destination = fromTargetClasses(pomDirectory)
.resolve("target")
.resolve(project.getBuild().getFinalName() + "." + project.getPackaging())
.toFile();
project.getArtifact().setFile(destination);
};
}

@BeforeEach
void before(MavenInvoker invoker) {
invoker
.forDirectory(fromTargetClasses(pomDirectory))
.execute("clean", "package");
}

MavenProjectCustomizer getCustomizer() {
return customizer;
}

Path getPomDirectory() {
return pomDirectory;
}

private static Path fromTargetClasses(Path pomDirectory) {
Path testClasses = tryToExecute(() ->
new File(checkNotNull(
PackagePlugMojoIT.class.getClassLoader().getResource("."),
"20190117:004601"
).toURI()).toPath(),
"20190201:003826"
);
return testClasses.resolve(pomDirectory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,97 +20,157 @@
import org.apache.maven.monitor.logging.DefaultLog;
import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.LoggerFactory;
import pl.wavesoftware.maven.testing.junit5.MavenInvoker;
import pl.wavesoftware.maven.testing.junit5.MavenInvokerExtension;
import pl.wavesoftware.maven.testing.junit5.MavenProjectCustomizer;
import pl.wavesoftware.maven.testing.junit5.MojoExtension;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import pl.wavesoftware.maven.testing.junit5.MojoFactory;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute;
import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull;

/**
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
* @since 0.1.0
*/
@ExtendWith({
MojoExtension.class,
MavenInvokerExtension.class,
MockitoExtension.class
})
class PackagePlugMojoIT {

private final Path pomDirectory = Paths.get("simpliest");
private final MavenProjectCustomizer customizer = project -> {
File destination = fromTargetClasses(pomDirectory)
.resolve("target")
.resolve(project.getBuild().getFinalName() + "." + project.getPackaging())
.toFile();
project.getArtifact().setFile(destination);
};

@Spy
private Log log = new DefaultLog(new Slf4jLogger(
private static final Log DEFAULT_LOG = new DefaultLog(new Slf4jLogger(
LoggerFactory.getLogger(PackagePlugMojoIT.class)
));

@BeforeEach
void before(MavenInvoker invoker) {
invoker
.forDirectory(fromTargetClasses(pomDirectory))
.execute("clean", "compile", "jar:jar");
@Nested
@ExtendWith(MockitoExtension.class)
class Simpliest extends BaseMavenTestCase {
@Spy
private Log log = DEFAULT_LOG;

Simpliest() {
super(Paths.get("simpliest"));
}

@AfterEach
void after() {
Mockito.verifyNoMoreInteractions(log);
Mockito.validateMockitoUsage();
}

@Test
@DisplayName("Execute mojo on simpliest project")
void execute(MojoFactory factory) {
// given
PackagePlugMojo mojo = factory
.customizer(getCustomizer())
.builder(PackagePlugMojo.class)
.withPomDirectory(getPomDirectory())
.build(PackagePlugMojo.GOAL);
mojo.setLog(log);

// when & then
assertThatCode(mojo::execute).doesNotThrowAnyException();
verify(log, atLeastOnce()).isDebugEnabled();
verify(log).isInfoEnabled();
verify(log).info(contains("Building plug: "));
verify(log).debug(contains(
"simpliest-0.1.0-plg.jar was successful."
));
verify(log).debug(contains("Artifact attached to the build"));
Resource resource = new ClassPathResource(
"/simpliest/target/simpliest-0.1.0-plg.jar"
);
assertThat(resource.exists()).isTrue();
}
}

@AfterEach
void after() {
Mockito.verifyNoMoreInteractions(log);
Mockito.validateMockitoUsage();
@Nested
class Pom extends BaseMavenTestCase {

Pom() {
super(Paths.get("pom"));
}

@Test
@DisplayName("Execute mojo on pom project")
void execute(MojoFactory factory) {
// given
PackagePlugMojo mojo = factory
.customizer(getCustomizer())
.builder(PackagePlugMojo.class)
.withPomDirectory(getPomDirectory())
.build(PackagePlugMojo.GOAL);
mojo.setLog(DEFAULT_LOG);

// when & then
assertThatCode(mojo::execute).doesNotThrowAnyException();
Resource resource = new ClassPathResource(
"/pom/target/a-pom-0.1.0-plug.jar"
);
assertThat(resource.exists()).isFalse();
}
}

@Test
void execute(MojoFactory factory) {
// given
PackagePlugMojo mojo = factory
.customizer(customizer)
.builder(PackagePlugMojo.class)
.withPomDirectory(pomDirectory)
.build(PackagePlugMojo.GOAL);
mojo.setLog(log);

// when & then
assertThatCode(mojo::execute).doesNotThrowAnyException();
verify(log, atLeastOnce()).isDebugEnabled();
verify(log).isInfoEnabled();
verify(log).info(contains("Building plug: "));
verify(log).debug(contains(
"simpliest-0.1.0-plg.jar was successful."
));
verify(log).debug(contains("Artifact attached to the build"));
@Nested
class Skipped extends BaseMavenTestCase {

Skipped() {
super(Paths.get("skipped"));
}

@Test
@DisplayName("Execute mojo on skipped project")
void execute(MojoFactory factory) {
// given
PackagePlugMojo mojo = factory
.customizer(getCustomizer())
.builder(PackagePlugMojo.class)
.withPomDirectory(getPomDirectory())
.build(PackagePlugMojo.GOAL);
mojo.setLog(DEFAULT_LOG);

// when & then
assertThatCode(mojo::execute).doesNotThrowAnyException();
Resource resource = new ClassPathResource(
"/skipped/target/skipped-0.1.0-plug.jar"
);
assertThat(resource.exists()).isFalse();
}
}

private static Path fromTargetClasses(Path pomDirectory) {
Path testClasses = tryToExecute(() ->
new File(checkNotNull(
PackagePlugMojoIT.class.getClassLoader().getResource("."),
"20190117:004601"
).toURI()).toPath(),
"20190201:003826"
);
return testClasses.resolve(pomDirectory);
@Nested
class CodeWithDependencies extends BaseMavenTestCase {
CodeWithDependencies() {
super(Paths.get("code-with-deps"));
}

@Test
@DisplayName("Execute mojo on code-with-deps project")
void execute(MojoFactory factory) {
// given
PackagePlugMojo mojo = factory
.customizer(getCustomizer())
.builder(PackagePlugMojo.class)
.withPomDirectory(getPomDirectory())
.build(PackagePlugMojo.GOAL);
mojo.setLog(DEFAULT_LOG);

// when & then
assertThatCode(mojo::execute).doesNotThrowAnyException();
Resource resource = new ClassPathResource(
"/code-with-deps/target/code-with-deps-0.1.0-plug.jar"
);
assertThat(resource.exists()).isTrue();
}
}

}
37 changes: 37 additions & 0 deletions tools/plugs-maven-plugin/src/test/resources/code-with-deps/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!--
~ Copyright (c) 2019 Wave Software
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>code-with-deps</artifactId>
<version>0.1.0</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 46084d9

Please sign in to comment.