Skip to content

Commit

Permalink
objectionary#3160 fix code duplication for DataizedTest and resolve s…
Browse files Browse the repository at this point in the history
…ome new PMD violations
  • Loading branch information
c71n93 committed May 6, 2024
1 parent 8bf5e84 commit 8e82e95
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 64 deletions.
61 changes: 19 additions & 42 deletions eo-runtime/src/test/java/org/eolang/DataizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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<LogRecord> 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);
Expand 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();
Expand All @@ -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)
);
}
}

/**
Expand Down
54 changes: 35 additions & 19 deletions eo-runtime/src/test/java/org/eolang/SnippetTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand All @@ -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(
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions eo-runtime/src/test/java/org/eolang/XmirObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>> 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")
Expand Down

0 comments on commit 8e82e95

Please sign in to comment.