Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Upgrade Mockito; remove PowerMock
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored and centic9 committed Mar 7, 2023
1 parent 21458c7 commit 6cd38c0
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 167 deletions.
7 changes: 7 additions & 0 deletions .mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>1.6</version>
</extension>
</extensions>
2 changes: 2 additions & 0 deletions .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-Pconsume-incrementals
-Pmight-produce-incrementals
5 changes: 4 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env groovy

/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin(jdkVersions: [8, 11])
buildPlugin(useContainerAgent: true, configurations: [
[platform: 'linux', jdk: 17],
[platform: 'windows', jdk: 11],
])

// More complex Jenkinsfile sample: https://github.com/jenkinsci/graphql-server-plugin/blob/master/Jenkinsfile
65 changes: 28 additions & 37 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ THE SOFTWARE.
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.19</version>
<version>4.55</version>
<relativePath />
</parent>

<artifactId>jacoco</artifactId>
<version>3.3.3-SNAPSHOT</version>
<version>${revision}${changelist}</version>
<packaging>hpi</packaging>

<name>Jenkins JaCoCo plugin</name>
<url>https://github.com/jenkinsci/jacoco-plugin</url>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>

<properties>
<java.level>8</java.level>
<revision>3.3.3</revision>
<changelist>-SNAPSHOT</changelist>
<jacoco.version>0.8.8</jacoco.version>
<!-- see https://www.jenkins.io/changelog-stable/ for changelog of the LTS releases -->
<jenkins.version>2.277.1</jenkins.version>
<!-- until the plugin-pom picks up a newer version -->
<jenkins-test-harness.version>1589.vc23fca066d5c</jenkins-test-harness.version>
<jenkins.version>2.361.4</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<!-- Do not fail if tests are run without argLine -->
<argLine />
</properties>
Expand Down Expand Up @@ -104,10 +105,10 @@ THE SOFTWARE.
</developers>

<scm>
<connection>scm:git:https://github.com/jenkinsci/jacoco-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/jacoco-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/jacoco-plugin</url>
<tag>HEAD</tag>
<connection>scm:git:https://github.com/${gitHubRepo}.git</connection>
<developerConnection>scm:git:git@github.com:${gitHubRepo}.git</developerConnection>
<url>https://github.com/${gitHubRepo}</url>
<tag>${scmTag}</tag>
</scm>

<distributionManagement>
Expand All @@ -117,11 +118,22 @@ THE SOFTWARE.
</repository>
</distributionManagement>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.361.x</artifactId>
<version>1836.vfe602c266c05</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>dashboard-view</artifactId>
<version>2.18.1</version>
<optional>true</optional>
</dependency>
<dependency>
Expand All @@ -132,25 +144,19 @@ THE SOFTWARE.
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.2.1</version>
<version>3.5.1</version>
</dependency>

</dependencies>
Expand Down Expand Up @@ -324,21 +330,6 @@ THE SOFTWARE.
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>hpi</goal>
</goals>
</execution>
</executions>
<configuration>
<maskClasses>org.objectweb.asm.</maskClasses>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/jacoco/model/Coverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.kohsuke.stapler.export.ExportedBean;

/**
* Represents <tt>x/y</tt> where x={@link #missed} and y={@link #covered}.
* Represents {@code x/y} where x={@link #missed} and y={@link #covered}.
*
* @author Kohsuke Kawaguchi
* @author Jonathan Fuerth
Expand Down
91 changes: 37 additions & 54 deletions src/test/java/hudson/plugins/jacoco/BuildOverBuildTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,21 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.MockedStatic;

import java.io.PrintStream;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;

@RunWith(PowerMockRunner.class)
@PrepareForTest(JacocoDeltaCoverageResultSummary.class)
// See e.g. https://issues.jenkins-ci.org/browse/JENKINS-55179
@org.powermock.core.classloader.annotations.PowerMockIgnore({
"com.sun.org.apache.xerces.*",
"javax.xml.*",
"org.xml.*",
"javax.management.*"})
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

public class BuildOverBuildTest {

private JacocoDeltaCoverageResultSummary jacocoDeltaCoverageResultSummary_1, jacocoDeltaCoverageResultSummary_2;
private JacocoHealthReportDeltaThresholds deltaHealthThresholds;
//private JacocoHealthReportThresholds healthThresholds;

private Run run = PowerMock.createNiceMock(Run.class);
private Run run = mock(Run.class);
private final PrintStream logger = System.out;

@Before
Expand Down Expand Up @@ -64,44 +54,39 @@ public void setUp(){
@Test
public void shouldPassIfNegativeMetricIsWithinThresholdAndOtherMetricesArePositiveAndAboveThreshold(){

PowerMock.mockStatic(JacocoDeltaCoverageResultSummary.class);
//noinspection ConstantConditions
expect(JacocoDeltaCoverageResultSummary.build(anyObject(Run.class))).andReturn(jacocoDeltaCoverageResultSummary_1);

PowerMock.replay(JacocoDeltaCoverageResultSummary.class);
try (MockedStatic<JacocoDeltaCoverageResultSummary> staticJacocoDeltaCoverageResultSummary =
mockStatic(JacocoDeltaCoverageResultSummary.class)) {
staticJacocoDeltaCoverageResultSummary
.when(() -> JacocoDeltaCoverageResultSummary.build(any()))
.thenReturn(jacocoDeltaCoverageResultSummary_1);

JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger);
JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger);

PowerMock.verify(JacocoDeltaCoverageResultSummary.class);

Assert.assertEquals("Delta coverage drop is lesser than delta health threshold values", Result.SUCCESS, result);
Assert.assertEquals("Delta coverage drop is lesser than delta health threshold values", Result.SUCCESS, result);
}

}

// Test if the build with delta coverage < delta threshold will pass
@Test
public void checkBuildOverBuildSuccessTest(){

PowerMock.mockStatic(JacocoDeltaCoverageResultSummary.class);
//noinspection ConstantConditions
expect(JacocoDeltaCoverageResultSummary.build(anyObject(Run.class))).andReturn(jacocoDeltaCoverageResultSummary_2);
//noinspection ConstantConditions
expect(JacocoDeltaCoverageResultSummary.build(anyObject(Run.class))).andReturn(jacocoDeltaCoverageResultSummary_1);

PowerMock.replay(JacocoDeltaCoverageResultSummary.class);
try (MockedStatic<JacocoDeltaCoverageResultSummary> staticJacocoDeltaCoverageResultSummary = mockStatic(JacocoDeltaCoverageResultSummary.class)) {
staticJacocoDeltaCoverageResultSummary
.when(() -> JacocoDeltaCoverageResultSummary.build(any()))
.thenReturn(jacocoDeltaCoverageResultSummary_2)
.thenReturn(jacocoDeltaCoverageResultSummary_1);
JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger); // check for first test case: delta coverage < delta threshold

JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger); // check for first test case: delta coverage < delta threshold
Assert.assertEquals("Delta coverage is lesser than delta health threshold values", Result.SUCCESS, result);

Assert.assertEquals("Delta coverage is lesser than delta health threshold values", Result.SUCCESS, result);

result = jacocoPublisher.checkBuildOverBuildResult(run, logger); // check for second test case: delta coverage > delta threshold but overall coverage better than last successful build
Assert.assertEquals("Delta coverage is greater than delta health threshold values but overall coverage is better than last successful build's coverage", Result.SUCCESS, result);

PowerMock.verify(JacocoDeltaCoverageResultSummary.class);
result = jacocoPublisher.checkBuildOverBuildResult(run, logger); // check for second test case: delta coverage > delta threshold but overall coverage better than last successful build
Assert.assertEquals("Delta coverage is greater than delta health threshold values but overall coverage is better than last successful build's coverage", Result.SUCCESS, result);
}

}

Expand All @@ -120,18 +105,16 @@ public void shouldFailIfNegativeMetricIsAboveThresholdAndOtherMetricesArePositiv
jacocoDeltaCoverageResultSummary.setBranchCoverage(0f);
jacocoDeltaCoverageResultSummary.setComplexityCoverage(-2.678f);

PowerMock.mockStatic(JacocoDeltaCoverageResultSummary.class);
//noinspection ConstantConditions
expect(JacocoDeltaCoverageResultSummary.build(anyObject(Run.class))).andReturn(jacocoDeltaCoverageResultSummary);

PowerMock.replay(JacocoDeltaCoverageResultSummary.class);

JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger);
try (MockedStatic<JacocoDeltaCoverageResultSummary> staticJacocoDeltaCoverageResultSummary = mockStatic(JacocoDeltaCoverageResultSummary.class)) {
staticJacocoDeltaCoverageResultSummary
.when(() -> JacocoDeltaCoverageResultSummary.build(any()))
.thenReturn(jacocoDeltaCoverageResultSummary);

PowerMock.verify(JacocoDeltaCoverageResultSummary.class);
Assert.assertEquals("Delta coverage drop is greater than delta health threshold values", Result.FAILURE, result);
JacocoPublisher jacocoPublisher = new JacocoPublisher();
jacocoPublisher.deltaHealthReport = deltaHealthThresholds;
Result result = jacocoPublisher.checkBuildOverBuildResult(run, logger);

Assert.assertEquals("Delta coverage drop is greater than delta health threshold values", Result.FAILURE, result);
}
}
}
20 changes: 4 additions & 16 deletions src/test/java/hudson/plugins/jacoco/JacocoPublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -49,14 +45,6 @@
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;

@RunWith(PowerMockRunner.class)
@PrepareForTest(JacocoPublisher.class)
// See e.g. https://issues.jenkins-ci.org/browse/JENKINS-55179
@org.powermock.core.classloader.annotations.PowerMockIgnore({
"com.sun.org.apache.xerces.*",
"javax.xml.*",
"org.xml.*",
"javax.management.*"})
public class JacocoPublisherTest extends AbstractJacocoTestBase {
private final TaskListener taskListener = niceMock(TaskListener.class);
private final Launcher launcher = niceMock(Launcher.class);
Expand Down Expand Up @@ -496,8 +484,8 @@ public void testCopyClass_Wrong() throws IOException, InterruptedException {
public void testPerformWithBuildOverBuild() throws IOException, InterruptedException {

// expect
final Run run = PowerMock.createNiceMock(Run.class);
final Job job = PowerMock.createNiceMock(Job.class);
final Run run = niceMock(Run.class);
final Job job = niceMock(Job.class);

expect(run.getResult()).andReturn(Result.SUCCESS).anyTimes();
expect(run.getEnvironment(taskListener)).andReturn(new EnvVars()).anyTimes();
Expand All @@ -522,7 +510,7 @@ public void testPerformWithBuildOverBuild() throws IOException, InterruptedExcep
expect(run.getParent()).andReturn(job).anyTimes();
expect(job.getLastSuccessfulBuild()).andReturn(run).anyTimes();

PowerMock.replay(taskListener, run, job);
replay(taskListener, run, job);

// execute
//noinspection deprecation
Expand All @@ -540,6 +528,6 @@ public void testPerformWithBuildOverBuild() throws IOException, InterruptedExcep
}

// verify
PowerMock.verify(taskListener, run, job);
verify(taskListener, run, job);
}
}
Loading

0 comments on commit 6cd38c0

Please sign in to comment.