From 4e91361f6f1a23cf874766a74177493f6e5813c2 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 22 Dec 2023 12:13:19 +0300 Subject: [PATCH 1/5] feat(#2713): add explanaiton messages for tests --- .../eolang/maven/optimization/OptCachedTest.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index 1c3e009e2d..44b1b5693a 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -45,10 +45,11 @@ */ class OptCachedTest { @Test - void optimizesIfXmlAlreadyInCache(final @TempDir Path tmp) throws IOException { + void returnsFromCacheIfXmlAlreadyInCache(final @TempDir Path tmp) throws IOException { final XML program = OptCachedTest.program(); OptCachedTest.save(tmp, program); MatcherAssert.assertThat( + "We expected that the program will be returned from the cache", new OptCached(path -> program, tmp).apply(program), Matchers.equalTo(program) ); @@ -58,19 +59,16 @@ void optimizesIfXmlAlreadyInCache(final @TempDir Path tmp) throws IOException { void optimizesIfXmlIsAbsentInCache(final @TempDir Path tmp) { final XML program = OptCachedTest.program(); final Path cache = tmp.resolve("cache"); - final XML res = new OptCached(path -> program, cache) - .apply(program); MatcherAssert.assertThat( - res, - Matchers.equalTo(program) + "We expect that the program will be created and returned as is (same instance)", + new OptCached(path -> program, cache).apply(program), + Matchers.sameInstance(program) ); MatcherAssert.assertThat( + "We expect that the cache saved the program after the first run", cache.resolve("main.xmir").toFile(), FileMatchers.anExistingFile() ); - MatcherAssert.assertThat( - res, Matchers.equalTo(program) - ); } /** From 7cd9d7549a240e0930ce39f3aad759b67d23b6bf Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 22 Dec 2023 12:43:34 +0300 Subject: [PATCH 2/5] feat(#2713): add simple implementation of time checking --- .../eolang/maven/optimization/OptCached.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java index 56d7719443..e97aace7f6 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java @@ -28,6 +28,11 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.time.Instant; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Optional; import org.eolang.maven.AssembleMojo; import org.eolang.maven.Place; import org.eolang.maven.footprint.FtDefault; @@ -67,11 +72,9 @@ public OptCached( @Override public XML apply(final XML xml) { try { - final Path path = new Place(xml.xpath("/program/@name").get(0)) - .make(this.folder, AssembleMojo.IR_EXTENSION); final XML optimized; - if (Files.exists(path)) { - optimized = new XMLDocument(path); + if (this.contains(xml)) { + optimized = new XMLDocument(this.cached(xml)); } else { optimized = this.delegate.apply(xml); new FtDefault(this.folder).save( @@ -85,4 +88,39 @@ public XML apply(final XML xml) { throw new IllegalStateException(String.format("Can't optimize '%s'", xml), ex); } } + + /** + * Returns the path to the cached program. + * Pay attention that the path is not checked for existence. + * @param xml Eo program. + * @return Path to the cached program. + */ + private Path cached(final XML xml) { + return new Place(xml.xpath("/program/@name").get(0)) + .make(this.folder, AssembleMojo.IR_EXTENSION); + } + + /** + * Checks if the cache contains the program. + * @param xml Eo program. + * @return True if the cache contains the program. + * @throws IOException If fails. + */ + private boolean contains(final XML xml) throws IOException { + final Path path = this.cached(xml); + final Optional time = xml.xpath("/program/@time").stream().findFirst(); + final boolean res; + if (Files.exists(path) && time.isPresent()) { + res = Files.readAttributes(path, BasicFileAttributes.class) + .creationTime() + .toInstant() + .truncatedTo(ChronoUnit.SECONDS) + .equals( + ZonedDateTime.parse(time.get()).toInstant().truncatedTo(ChronoUnit.SECONDS) + ); + } else { + res = false; + } + return res; + } } From 2173fd30d587ba12807facd91ead3a9b41b7117c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 22 Dec 2023 14:51:55 +0300 Subject: [PATCH 3/5] feat(#2713): add one more test that checks outdated cache --- .../maven/optimization/OptCachedTest.java | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index 44b1b5693a..ea9c2c0973 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -29,6 +29,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; import org.cactoos.bytes.BytesOf; import org.cactoos.bytes.UncheckedBytes; import org.cactoos.io.ResourceOf; @@ -38,6 +41,9 @@ import org.hamcrest.io.FileMatchers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.xembly.Directive; +import org.xembly.Directives; +import org.xembly.Xembler; /** * Test case for {@link org.eolang.maven.optimization.OptCached}. @@ -45,18 +51,20 @@ */ class OptCachedTest { @Test - void returnsFromCacheIfXmlAlreadyInCache(final @TempDir Path tmp) throws IOException { + void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOException { final XML program = OptCachedTest.program(); OptCachedTest.save(tmp, program); MatcherAssert.assertThat( "We expected that the program will be returned from the cache", - new OptCached(path -> program, tmp).apply(program), + new OptCached(path -> { + throw new IllegalStateException("This code shouldn't be executed"); + }, tmp).apply(program), Matchers.equalTo(program) ); } @Test - void optimizesIfXmlIsAbsentInCache(final @TempDir Path tmp) { + void optimizesIfXmlIsAbsentInCache(@TempDir final Path tmp) { final XML program = OptCachedTest.program(); final Path cache = tmp.resolve("cache"); MatcherAssert.assertThat( @@ -71,6 +79,18 @@ void optimizesIfXmlIsAbsentInCache(final @TempDir Path tmp) { ); } + @Test + void optimizesBecauseChacheIsExpired(@TempDir final Path tmp) throws IOException { + final XML outdated = OptCachedTest.program(ZonedDateTime.now().minusMinutes(1)); + final XML updated = OptCachedTest.program(ZonedDateTime.now()); + OptCachedTest.save(tmp, outdated); + MatcherAssert.assertThat( + "We expected that the program will be optimized because the cache is expired", + new OptCached(path -> updated, tmp).apply(outdated), + Matchers.equalTo(updated) + ); + } + /** * Save XML program with hardcoded name to a temp directory. * @param tmp Temporary test directory. @@ -85,16 +105,27 @@ private static Path save(final Path tmp, final XML xml) throws IOException { } /** - * Get parsed program from resources. + * Generates EO program for tests. * @return XML representation of program. */ private static XML program() { + return OptCachedTest.program(ZonedDateTime.now()); + } + + /** + * Generates EO program for tests with specified time. + * @param time Time. + * @return XML representation of program. + */ + private static XML program(final ZonedDateTime time) { return new XMLDocument( - new UncheckedBytes( - new BytesOf( - new ResourceOf("org/eolang/maven/optimize/main.xml") - ) - ).asBytes() + new Xembler( + new Directives() + .add("program") + .attr("name", "main") + .attr("time", time.format(DateTimeFormatter.ISO_INSTANT)) + .up() + ).xmlQuietly() ); } } From cebabf2160532850654d15e98640b2201220100c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 22 Dec 2023 14:57:51 +0300 Subject: [PATCH 4/5] feat(#2713): add more tests --- .../maven/optimization/OptCachedTest.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index ea9c2c0973..6514a0d040 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -52,7 +52,7 @@ class OptCachedTest { @Test void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOException { - final XML program = OptCachedTest.program(); + final XML program = OptCachedTest.program(ZonedDateTime.now()); OptCachedTest.save(tmp, program); MatcherAssert.assertThat( "We expected that the program will be returned from the cache", @@ -91,6 +91,18 @@ void optimizesBecauseChacheIsExpired(@TempDir final Path tmp) throws IOException ); } + @Test + void optimizesIfTimeIsNotSet(@TempDir final Path tmp) throws IOException { + final XML without = OptCachedTest.program(); + final XML with = OptCachedTest.program(ZonedDateTime.now()); + OptCachedTest.save(tmp, without); + MatcherAssert.assertThat( + "We expected that the program will be optimized because the cache doesn't have time", + new OptCached(path -> with, tmp).apply(without), + Matchers.equalTo(with) + ); + } + /** * Save XML program with hardcoded name to a temp directory. * @param tmp Temporary test directory. @@ -109,7 +121,14 @@ private static Path save(final Path tmp, final XML xml) throws IOException { * @return XML representation of program. */ private static XML program() { - return OptCachedTest.program(ZonedDateTime.now()); + return new XMLDocument( + new Xembler( + new Directives() + .add("program") + .attr("name", "main") + .up() + ).xmlQuietly() + ); } /** From 2970797f81930e235d4282533e9791ddb7b76b7b Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 22 Dec 2023 15:00:17 +0300 Subject: [PATCH 5/5] feat(#2713): fix all qulice suggestions --- .../org/eolang/maven/optimization/OptCached.java | 1 - .../eolang/maven/optimization/OptCachedTest.java | 14 ++++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java index e97aace7f6..45a2c89123 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java @@ -29,7 +29,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; -import java.time.Instant; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Optional; diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index 6514a0d040..97d40790ca 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -31,17 +31,12 @@ import java.nio.file.Paths; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.Arrays; -import org.cactoos.bytes.BytesOf; -import org.cactoos.bytes.UncheckedBytes; -import org.cactoos.io.ResourceOf; import org.eolang.maven.util.HmBase; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.io.FileMatchers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.xembly.Directive; import org.xembly.Directives; import org.xembly.Xembler; @@ -56,9 +51,12 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOExcep OptCachedTest.save(tmp, program); MatcherAssert.assertThat( "We expected that the program will be returned from the cache", - new OptCached(path -> { - throw new IllegalStateException("This code shouldn't be executed"); - }, tmp).apply(program), + new OptCached( + path -> { + throw new IllegalStateException("This code shouldn't be executed"); + }, + tmp + ).apply(program), Matchers.equalTo(program) ); }