From 7b1d8e7368fbcb463ba7ecca6cf2176d211e73ae Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 2 Jul 2021 07:39:33 +0100 Subject: [PATCH] Migrate to Junit 5 syntax (#421) --- .../builder/BuilderConfigurationTest.java | 6 +--- .../CheckstyleClassNotFoundExceptionTest.java | 2 +- .../CheckstyleSourceLoadExceptionTest.java | 2 +- .../ruleset/builder/CheckstyleWriterTest.java | 25 +++++++------- .../builder/ClassGraphPackageScannerTest.java | 2 +- .../DefaultReadmeIndexBuilderTest.java | 6 ++-- .../builder/DefaultRuleClassLocatorTest.java | 6 ++-- .../builder/DefaultRuleReadmeLoaderTest.java | 33 +++++++++---------- .../ruleset/builder/OutputPropertiesTest.java | 6 ++-- .../ruleset/builder/ReadmeBuilderTest.java | 8 ++--- .../ReadmeFragmentNotFoundExceptionTest.java | 2 +- .../ruleset/builder/ReadmeWriterTest.java | 6 ++-- .../ruleset/builder/RuleLevelTest.java | 2 +- .../ruleset/builder/RuleLoaderTest.java | 6 ++-- .../ruleset/builder/RuleParentTest.java | 2 +- .../ruleset/builder/RuleSourceTest.java | 2 +- .../checkstyle/ruleset/builder/RuleTest.java | 7 ++-- .../ruleset/builder/RulesPropertiesTest.java | 6 ++-- .../builder/SourcesPropertiesTest.java | 6 ++-- .../builder/TemplatePropertiesTest.java | 6 ++-- 20 files changed, 64 insertions(+), 77 deletions(-) diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java index 59ee2e2..efb5c11 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java @@ -1,14 +1,10 @@ package net.kemitix.checkstyle.ruleset.builder; -import com.google.common.reflect.ClassPath; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import java.io.IOException; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.stream.Stream; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java index eadf0a5..09e7bb6 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleClassNotFoundExceptionTest.java @@ -1,6 +1,6 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java index cb3d1b1..7c92881 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleSourceLoadExceptionTest.java @@ -1,6 +1,6 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java index 0eab043..d5a6815 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/CheckstyleWriterTest.java @@ -3,11 +3,11 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; import me.andrz.builder.map.MapBuilder; import org.assertj.core.api.ThrowableAssert; -import org.junit.Before; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -36,11 +36,8 @@ public class CheckstyleWriterTest { private static final String FILE_SEPARATOR = System.getProperty("file.separator"); - @org.junit.Rule - public ExpectedException exception = ExpectedException.none(); - - @org.junit.Rule - public TemporaryFolder folder = new TemporaryFolder(); + @TempDir + public File folder; private CheckstyleWriter checkstyleWriter; @@ -61,7 +58,7 @@ public class CheckstyleWriterTest { private RuleClassLocator ruleClassLocator = mock(RuleClassLocator.class); private SourcesProperties sourceProperties = new SourcesProperties(); - @Before + @BeforeEach public void setUp() throws Exception { ruleName = "RegexpOnFilename"; ruleClassname = "com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck"; @@ -74,12 +71,12 @@ public class CheckstyleWriterTest { .put(getOutputFile(RuleLevel.COMPLEXITY)) .build(); outputProperties.setRulesetFiles(outputFiles); - outputDirectory = folder.newFolder() - .toPath(); + outputDirectory = folder.toPath(); + Files.createDirectories(outputDirectory); outputProperties.setDirectory(outputDirectory); templateProperties = new TemplateProperties(); - val checkstyleTemplate = folder.newFile("checkstyle-template.xml") - .toPath(); + val checkstyleTemplate = folder.toPath().resolve("checkstyle-template.xml"); + checkstyleTemplate.toFile().createNewFile(); Files.write( checkstyleTemplate, TEMPLATE.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING); templateProperties.setCheckstyleXml(checkstyleTemplate); diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ClassGraphPackageScannerTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ClassGraphPackageScannerTest.java index 2197293..d7ea5f3 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ClassGraphPackageScannerTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ClassGraphPackageScannerTest.java @@ -1,6 +1,6 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.List; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilderTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilderTest.java index 1356f34..e87d381 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilderTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilderTest.java @@ -1,8 +1,8 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; @@ -20,7 +20,7 @@ public class DefaultReadmeIndexBuilderTest { private RulesProperties rulesProperties; - @Before + @BeforeEach public void setUp() throws Exception { rulesProperties = new RulesProperties(); indexBuilder = new DefaultReadmeIndexBuilder(rulesProperties); diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java index 4715f9f..10f6595 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleClassLocatorTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.*; @@ -27,7 +27,7 @@ public class DefaultRuleClassLocatorTest { private final RuleSource checkstyleRuleSource = RuleSourceMother.checkstyle.get(); private final RuleSource sevntuRuleSource = RuleSourceMother.sevntu.get(); - @Before + @BeforeEach public void setUp() { sourceProperties.setSources(Arrays.asList( checkstyleRuleSource, sevntuRuleSource diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoaderTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoaderTest.java index f7a05f2..3f1d3e9 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoaderTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoaderTest.java @@ -1,25 +1,25 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; -import org.junit.Before; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; -import static org.assertj.core.api.Assertions.assertThat; - /** * Tests for {@link DefaultRuleReadmeLoader}. * * @author Paul Campbell (pcampbell@kemitix.net) */ -public class DefaultRuleReadmeLoaderTest { +public class DefaultRuleReadmeLoaderTest + implements WithAssertions { private RuleReadmeLoader loader; @@ -27,17 +27,14 @@ public class DefaultRuleReadmeLoaderTest { private Path fragments; - @org.junit.Rule - public TemporaryFolder folder = new TemporaryFolder(); + @TempDir + public File folder; - @org.junit.Rule - public ExpectedException exception = ExpectedException.none(); - - @Before + @BeforeEach public void setUp() throws Exception { final TemplateProperties templateProperties = new TemplateProperties(); - fragments = folder.newFolder("fragments") - .toPath(); + fragments = folder.toPath().resolve("fragments"); + Files.createDirectories(fragments); templateProperties.setReadmeFragments(fragments); loader = new DefaultRuleReadmeLoader(templateProperties); rule = new Rule(); @@ -61,10 +58,10 @@ public class DefaultRuleReadmeLoaderTest { public void loadEnabledWithMissingFragment() { //given rule.setEnabled(true); - exception.expect(ReadmeFragmentNotFoundException.class); - exception.expectMessage("name"); //when - loader.load(rule); + assertThatExceptionOfType(ReadmeFragmentNotFoundException.class) + .isThrownBy(() -> loader.load(rule)) + .withMessage("name"); } @Test diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/OutputPropertiesTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/OutputPropertiesTest.java index b01098d..9860928 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/OutputPropertiesTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/OutputPropertiesTest.java @@ -2,8 +2,8 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.nio.file.Paths; import java.util.HashMap; @@ -17,7 +17,7 @@ public class OutputPropertiesTest { private OutputProperties outputProperties; - @Before + @BeforeEach public void setUp() throws Exception { outputProperties = new OutputProperties(); } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeBuilderTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeBuilderTest.java index 68d4f2c..c19eb99 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeBuilderTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeBuilderTest.java @@ -1,12 +1,10 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.util.UUID; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -19,7 +17,7 @@ public class ReadmeBuilderTest { private ReadmeBuilder readmeBuilder; private String template = "i:%s,ec:%s,es:%s,dc:%s,ds:%s"; - @Before + @BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeFragmentNotFoundExceptionTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeFragmentNotFoundExceptionTest.java index 7abfda9..a8ee6da 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeFragmentNotFoundExceptionTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeFragmentNotFoundExceptionTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java index a4edf52..fb71455 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java @@ -1,8 +1,8 @@ package net.kemitix.checkstyle.ruleset.builder; import net.kemitix.files.FileReaderWriter; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -40,7 +40,7 @@ public class ReadmeWriterTest { private String templateBody = UUID.randomUUID().toString(); private String formattedOutput = UUID.randomUUID().toString(); - @Before + @BeforeEach public void setUp() throws IOException { MockitoAnnotations.initMocks(this); diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLevelTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLevelTest.java index fce27f6..252d4c7 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLevelTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLevelTest.java @@ -1,6 +1,6 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.stream.Stream; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLoaderTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLoaderTest.java index 4afeb7e..30ba060 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLoaderTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleLoaderTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -44,7 +44,7 @@ public class RuleLoaderTest { private String enabledRuleName = "enabled-" + UUID.randomUUID().toString(); private String disabledRuleName = "disabled-" + UUID.randomUUID().toString(); - @Before + @BeforeEach public void setUp() { MockitoAnnotations.initMocks(this); diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleParentTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleParentTest.java index 3684c85..639cf10 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleParentTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleParentTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java index 4c0df94..b6e43d8 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleSourceTest.java @@ -1,6 +1,6 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Random; import java.util.UUID; diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleTest.java index 92241b6..04be25c 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RuleTest.java @@ -2,9 +2,8 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.net.URI; @@ -19,7 +18,7 @@ public class RuleTest { private RuleSource ruleSource = RuleSourceMother.checkstyle.get(); - @Before + @BeforeEach public void setUp() throws Exception { rule = new Rule(); } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RulesPropertiesTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RulesPropertiesTest.java index 1dd4136..a10811d 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RulesPropertiesTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/RulesPropertiesTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -14,7 +14,7 @@ public class RulesPropertiesTest { private RulesProperties rulesProperties; - @Before + @BeforeEach public void setUp() { rulesProperties = new RulesProperties(); } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/SourcesPropertiesTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/SourcesPropertiesTest.java index b403591..2eb5873 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/SourcesPropertiesTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/SourcesPropertiesTest.java @@ -1,7 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -9,7 +9,7 @@ public class SourcesPropertiesTest { private SourcesProperties sourcesProperties; - @Before + @BeforeEach public void setUp() { sourcesProperties = new SourcesProperties(); } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java index db3da8e..35e6e4a 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java @@ -1,8 +1,8 @@ package net.kemitix.checkstyle.ruleset.builder; import org.assertj.core.api.SoftAssertions; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.File; import java.nio.file.Path; @@ -17,7 +17,7 @@ public class TemplatePropertiesTest { private TemplateProperties templateProperties; - @Before + @BeforeEach public void setUp() throws Exception { templateProperties = new TemplateProperties(); }