From 8e82e95d3791c4e2ecbc5589266172b827abf399 Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Mon, 6 May 2024 19:20:41 +0300 Subject: [PATCH] #3160 fix code duplication for DataizedTest and resolve some new PMD violations --- .../test/java/org/eolang/DataizedTest.java | 61 ++++++------------- .../test/java/org/eolang/SnippetTestCase.java | 54 ++++++++++------ .../test/java/org/eolang/XmirObjectTest.java | 6 +- 3 files changed, 57 insertions(+), 64 deletions(-) diff --git a/eo-runtime/src/test/java/org/eolang/DataizedTest.java b/eo-runtime/src/test/java/org/eolang/DataizedTest.java index c52013b11b..409775c188 100644 --- a/eo-runtime/src/test/java/org/eolang/DataizedTest.java +++ b/eo-runtime/src/test/java/org/eolang/DataizedTest.java @@ -35,6 +35,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Test case for {@link Dataized}. @@ -101,42 +103,9 @@ void logsWhenException() { ); } - @Test - void printsShortLogs() throws InterruptedException { - final Logger log = Logger.getLogger("printsShortLogs"); - final Level before = log.getLevel(); - log.setLevel(Level.ALL); - final List logs = new LinkedList<>(); - final Handler hnd = new Hnd(logs); - log.addHandler(hnd); - final Thread thread = new Thread( - () -> { - final String property = System.getProperty(DataizedTest.DATAIZATION_LOG); - System.getProperties().setProperty( - DataizedTest.DATAIZATION_LOG, - String.valueOf(1) - ); - final Phi phi = new PhiDec(Phi.Φ); - new Dataized(phi, log).take(); - if (property != null) { - System.getProperties().setProperty(DataizedTest.DATAIZATION_LOG, property); - } else { - System.clearProperty(DataizedTest.DATAIZATION_LOG); - } - }); - thread.start(); - thread.join(); - log.setLevel(before); - log.removeHandler(hnd); - MatcherAssert.assertThat( - AtCompositeTest.TO_ADD_MESSAGE, - logs.size(), - Matchers.equalTo(1) - ); - } - - @Test - void printsLongLogs() throws InterruptedException { + @ParameterizedTest + @ValueSource(ints = {1, 2}) + void printsShortLogs(final int level) throws InterruptedException { final Logger log = Logger.getLogger(Dataized.class.getName()); final Level before = log.getLevel(); log.setLevel(Level.ALL); @@ -148,7 +117,7 @@ void printsLongLogs() throws InterruptedException { final String property = System.getProperty(DataizedTest.DATAIZATION_LOG); System.getProperties().setProperty( DataizedTest.DATAIZATION_LOG, - String.valueOf(2) + String.valueOf(level) ); final Phi phi = new PhiDec(Phi.Φ); new Dataized(phi, log).take(); @@ -162,11 +131,19 @@ void printsLongLogs() throws InterruptedException { thread.join(); log.setLevel(before); log.removeHandler(hnd); - MatcherAssert.assertThat( - AtCompositeTest.TO_ADD_MESSAGE, - logs.size(), - Matchers.greaterThan(1) - ); + if (level == 1) { + MatcherAssert.assertThat( + "Number of log records should be 1 in case of short logs", + logs.size(), + Matchers.equalTo(1) + ); + } else if (level == 2) { + MatcherAssert.assertThat( + "Number of log records should be greater than 1 in case of long logs", + logs.size(), + Matchers.greaterThan(1) + ); + } } /** diff --git a/eo-runtime/src/test/java/org/eolang/SnippetTestCase.java b/eo-runtime/src/test/java/org/eolang/SnippetTestCase.java index 28465fc0ef..300ddaf96d 100755 --- a/eo-runtime/src/test/java/org/eolang/SnippetTestCase.java +++ b/eo-runtime/src/test/java/org/eolang/SnippetTestCase.java @@ -67,6 +67,22 @@ @ExtendWith(WeAreOnline.class) @SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleNotContainsTestWord"}) final class SnippetTestCase { + + /** + * True. + */ + private static final String TRUE = "true"; + + /** + * False. + */ + private static final String FALSE = "false"; + + /** + * UTF-8. + */ + private static final String UTF_8 = "UTF-8"; + @ParameterizedTest @Tag("slow") @ExtendWith(WeAreOnline.class) @@ -79,8 +95,8 @@ void runsAllSnippets(final String yml, final @TempDir Path temp) throws IOExcept new Farea(temp).together( f -> { f.properties() - .set("project.build.sourceEncoding", "UTF-8") - .set("project.reporting.outputEncoding", "UTF-8"); + .set("project.build.sourceEncoding", SnippetTestCase.UTF_8) + .set("project.reporting.outputEncoding", SnippetTestCase.UTF_8); SnippetTestCase.copySources(f, "src/main/eo"); f.files() .file(String.format("src/main/eo/%s", file)) @@ -98,7 +114,7 @@ void runsAllSnippets(final String yml, final @TempDir Path temp) throws IOExcept .phase("generate-sources") .goals("register", "assemble", "verify", "transpile") .configuration() - .set("failOnWarnings", "true"); + .set("failOnWarnings", SnippetTestCase.TRUE); f.build() .plugins() .append("org.codehaus.mojo", "exec-maven-plugin", "3.1.1") @@ -136,8 +152,8 @@ void runsTestsAfterPhiAndUnphi(final @TempDir Path temp) throws IOException { SnippetTestCase.copySources(f, "src/main"); SnippetTestCase.copySources(f, "src/test/eo"); f.properties() - .set("project.build.sourceEncoding", "UTF-8") - .set("project.reporting.outputEncoding", "UTF-8"); + .set("project.build.sourceEncoding", SnippetTestCase.UTF_8) + .set("project.reporting.outputEncoding", SnippetTestCase.UTF_8); f.dependencies().append( "net.sf.saxon", "Saxon-HE", @@ -170,7 +186,7 @@ void runsTestsAfterPhiAndUnphi(final @TempDir Path temp) throws IOException { .set("unphiMetas", new String[]{"+tests"}) .set("printSourcesDir", "${project.build.directory}/generated-eo-test/1-parse") .set("printOutputDir", "${project.basedir}/src/test/generated-eo") - .set("printReversed", "true"); + .set("printReversed", SnippetTestCase.TRUE); f.exec("clean", "compile"); final String phi = f.log(); MatcherAssert.assertThat( @@ -180,8 +196,8 @@ void runsTestsAfterPhiAndUnphi(final @TempDir Path temp) throws IOException { ); f.files().file("pom.xml").delete(); f.properties() - .set("project.build.sourceEncoding", "UTF-8") - .set("project.reporting.outputEncoding", "UTF-8"); + .set("project.build.sourceEncoding", SnippetTestCase.UTF_8) + .set("project.reporting.outputEncoding", SnippetTestCase.UTF_8); f.dependencies().append( "org.junit.jupiter", "junit-jupiter-api", @@ -207,10 +223,10 @@ void runsTestsAfterPhiAndUnphi(final @TempDir Path temp) throws IOException { .configuration() .set("foreign", "${project.basedir}/target/eo-foreign.csv") .set("foreignFormat", "csv") - .set("failOnWarning", "false") - .set("offline", "true") - .set("withRuntimeDependency", "false") - .set("placeBinariesThatHaveSources", "true"); + .set("failOnWarning", SnippetTestCase.FALSE) + .set("offline", SnippetTestCase.TRUE) + .set("withRuntimeDependency", SnippetTestCase.FALSE) + .set("placeBinariesThatHaveSources", SnippetTestCase.TRUE); f.build() .plugins() .append( @@ -230,17 +246,17 @@ void runsTestsAfterPhiAndUnphi(final @TempDir Path temp) throws IOException { .configuration() .set("foreign", "${project.basedir}/target/eo-foreign.csv") .set("foreignFormat", "csv") - .set("failOnWarning", "false") - .set("offline", "true") + .set("failOnWarning", SnippetTestCase.FALSE) + .set("offline", SnippetTestCase.TRUE) .set("scope", "test") .set("sourcesDir", "${project.basedir}/src/test/generated-eo") .set("targetDir", "${project.basedir}/target/eo-test") - .set("addSourcesRoot", "false") - .set("addTestSourcesRoot", "true") - .set("failOnWarning", "false") + .set("addSourcesRoot", SnippetTestCase.FALSE) + .set("addTestSourcesRoot", SnippetTestCase.TRUE) + .set("failOnWarning", SnippetTestCase.FALSE) .set("generatedDir", "${project.basedir}/target/generated-test-sources") - .set("withRuntimeDependency", "false") - .set("placeBinariesThatHaveSources", "true"); + .set("withRuntimeDependency", SnippetTestCase.FALSE) + .set("placeBinariesThatHaveSources", SnippetTestCase.TRUE); f.exec("clean", "test"); final String log = f.log(); Logger.debug(this, log); diff --git a/eo-runtime/src/test/java/org/eolang/XmirObjectTest.java b/eo-runtime/src/test/java/org/eolang/XmirObjectTest.java index b6e85faa58..d11f492a39 100644 --- a/eo-runtime/src/test/java/org/eolang/XmirObjectTest.java +++ b/eo-runtime/src/test/java/org/eolang/XmirObjectTest.java @@ -40,14 +40,14 @@ * * @since 0.38 */ -public class XmirObjectTest { +final class XmirObjectTest { @Test - public void annotatesOnlyPublicClasses() throws IOException { + void annotatesOnlyPublicClasses() throws IOException { final Set> clazzes = ClassPath.from(ClassLoader.getSystemClassLoader()) .getAllClasses() .stream() - .filter(clazz -> clazz.getPackageName().equals("EOorg.EOeolang")) + .filter(clazz -> "EOorg.EOeolang".equals(clazz.getPackageName())) .map(ClassPath.ClassInfo::load) .filter( clazz -> clazz.getSimpleName().startsWith("EO")