Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Migrate LogTest to JUnit5 #3939

Merged
merged 1 commit into from
May 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions src/test/java/spoon/test/logging/LogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,29 @@
*/
package spoon.test.logging;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.ParameterizedTest;
import spoon.Launcher;
import spoon.MavenLauncher;
import spoon.support.JavaOutputProcessor;
import spoon.support.Level;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@RunWith(Enclosed.class)
public class LogTest {

@RunWith(Parameterized.class)
public static class ParameterizedTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{Level.DEBUG, 6 },
{Level.INFO, 2 },
{Level.WARN, 0 },
{Level.ERROR, 0 },
{Level.OFF, 0}
});
}

@Parameterized.Parameter(0)
public Level level;

@Parameterized.Parameter(1)
public int nbLogMessagesMinimum;

@Test
public void testAllLevelsForLogs() {
@ParameterizedTest
@MethodSource("getLogLevelsAndExpectedCounts")
public void testAllLevelsForLogs(Pair<Level, Integer> levelAndExpectedCount) {
final Level level = levelAndExpectedCount.getLeft();
final int expectedCount = levelAndExpectedCount.getRight();
final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class);
final Launcher launcher = new Launcher();
logger.clear();
Expand All @@ -73,11 +55,24 @@ public void testAllLevelsForLogs() {
assertEquals(level, launcher.getFactory().getEnvironment().getLevel());

// contract: the number of messages increases with the log level
assertTrue(logger.getLoggingEvents().size() >= nbLogMessagesMinimum);
assertTrue(logger.getLoggingEvents().size() >= expectedCount);

}

/**
* @return log level and expected amount of logs for that level for the
* {@link LogTest::testAllLevelsForLogs} test.
*/
private static Stream<Pair<Level, Integer>> getLogLevelsAndExpectedCounts() {
return Stream.of(
Pair.of(Level.DEBUG, 6),
Pair.of(Level.INFO, 2),
Pair.of(Level.WARN, 0),
Pair.of(Level.ERROR, 0),
Pair.of(Level.OFF, 0)
);
}

public static class NonParameterizedTest {

@Test
public void testMavenLauncherLogs() {
Expand Down Expand Up @@ -109,6 +104,3 @@ public void testLoggingOff() {
assertEquals(0, logger.getLoggingEvents().size());
}
}


}