Skip to content

Commit

Permalink
Update dependency org.mockito:mockito-core to v5 (#4950)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tibor Blenessy <saberduck@users.noreply.github.com>
renovate[bot] and saberduck authored Dec 3, 2024
1 parent 534f134 commit 70428c3
Showing 3 changed files with 26 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@
<assertj.version>3.26.3</assertj.version>
<jgit.version>7.0.0.202409031743-r</jgit.version>
<junit.version>5.11.3</junit.version>
<mockito.version>3.12.4</mockito.version>
<mockito.version>5.14.2</mockito.version>
<slf4j.version>2.0.16</slf4j.version>
<sonar.version>24.12.0.100206</sonar.version>
<sonar.api.version>10.14.0.2599</sonar.api.version>
Original file line number Diff line number Diff line change
@@ -17,13 +17,14 @@
package org.sonar.plugins.javascript.utils;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProgressReport implements Runnable {

private final long period;
private final Logger logger;
private final Consumer<String> logger;
private long count;
private long currentFileNumber = -1;
private String currentFilename;
@@ -41,7 +42,7 @@ public class ProgressReport implements Runnable {
*/
private final AtomicBoolean interrupted = new AtomicBoolean();

public ProgressReport(String threadName, long period, Logger logger, String adjective) {
public ProgressReport(String threadName, long period, Consumer<String> logger, String adjective) {
interrupted.set(false);
this.period = period;
this.logger = logger;
@@ -51,6 +52,10 @@ public ProgressReport(String threadName, long period, Logger logger, String adje
thread.setDaemon(true);
}

public ProgressReport(String threadName, long period, Logger logger, String adjective) {
this(threadName, period, logger::info, adjective);
}

public ProgressReport(String threadName, long period, String adjective) {
this(threadName, period, LoggerFactory.getLogger(ProgressReport.class), adjective);
}
@@ -144,9 +149,6 @@ private void join() {
}

private void log(String message) {
synchronized (logger) {
logger.info(message);
logger.notifyAll();
}
logger.accept(message);
}
}
Original file line number Diff line number Diff line change
@@ -17,16 +17,13 @@
package org.sonar.plugins.javascript.utils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.awaitility.Awaitility.await;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.slf4j.Logger;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

@@ -47,29 +44,20 @@ void testSonarLogger() throws Exception {

@Test
void testPlural() throws Exception {
Logger logger = mock(Logger.class);
List<String> messages = new ArrayList<>();

ProgressReport report = new ProgressReport(
ProgressReportTest.class.getName(),
100,
logger,
messages::add,
"analyzed"
);

report.start(2, "foo1.java");

// Wait for start message
waitForMessage(logger);

// Wait for at least one progress message
waitForMessage(logger);
await().until(() -> messages.size() >= 3);

report.stop();

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(logger, atLeast(3)).info(captor.capture());

List<String> messages = captor.getAllValues();
assertThat(messages).hasSizeGreaterThanOrEqualTo(3);
assertThat(messages.get(0)).isEqualTo("2 source files to be analyzed");
for (int i = 1; i < messages.size() - 1; i++) {
@@ -81,29 +69,19 @@ void testPlural() throws Exception {

@Test
void testSingular() throws Exception {
Logger logger = mock(Logger.class);
List<String> messages = new ArrayList<>();

ProgressReport report = new ProgressReport(
ProgressReportTest.class.getName(),
100,
logger,
messages::add,
"analyzed"
);

report.start(1, "foo.java");

// Wait for start message
waitForMessage(logger);

// Wait for at least one progress message
waitForMessage(logger);

await().until(() -> messages.size() >= 3);
report.stop();

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(logger, atLeast(3)).info(captor.capture());

List<String> messages = captor.getAllValues();
assertThat(messages).hasSizeGreaterThanOrEqualTo(3);
assertThat(messages.get(0)).isEqualTo("1 source file to be analyzed");
for (int i = 1; i < messages.size() - 1; i++) {
@@ -115,36 +93,36 @@ void testSingular() throws Exception {

@Test
void testCancel() throws InterruptedException {
Logger logger = mock(Logger.class);
List<String> messages = new ArrayList<>();

ProgressReport report = new ProgressReport(
ProgressReport.class.getName(),
100,
logger,
messages::add,
"analyzed"
);
report.start(1, "foo.java");

// Wait for start message
waitForMessage(logger);
await().until(() -> !messages.isEmpty());

report.cancel();
}

@Test
void testStopPreserveTheInterruptedFlag() throws InterruptedException {
Logger logger = mock(Logger.class);
List<String> messages = new ArrayList<>();

ProgressReport report = new ProgressReport(
ProgressReport.class.getName(),
100,
logger,
messages::add,
"analyzed"
);
report.start(1, "foo.java");

// Wait for start message
waitForMessage(logger);
await().until(() -> !messages.isEmpty());

AtomicBoolean interruptFlagPreserved = new AtomicBoolean(false);

@@ -166,11 +144,9 @@ void testStopPreserveTheInterruptedFlag() throws InterruptedException {
t.join(1000);
assertThat(interruptFlagPreserved.get()).isTrue();

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(logger, atLeast(1)).info(captor.capture());

List<String> messages = captor.getAllValues();
assertThat(messages).contains("1/1" + " source file has been analyzed");
assertThat(messages)
.isNotEmpty()
.contains("1/1" + " source file has been analyzed");
}

@Test
@@ -207,9 +183,4 @@ void interrupted_thread_should_exit_immediately() throws InterruptedException {
assertThat(time.get()).isLessThan(300);
}

private static void waitForMessage(Logger logger) throws InterruptedException {
synchronized (logger) {
logger.wait();
}
}
}

0 comments on commit 70428c3

Please sign in to comment.