diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java index 0ef5e04c31..ddd4bd3472 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java @@ -112,6 +112,14 @@ public String hash() { return this.delegate.get(ForeignTojos.Attribute.HASH.key()); } + /** + * The tojo probed. + * @return The probed. + */ + public String probed() { + return this.delegate.get(ForeignTojos.Attribute.PROBED.key()); + } + /** * Checks if tojo was not already optimized. * diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java index a91310ac6c..37fa771d15 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java @@ -124,6 +124,28 @@ public ForeignTojo add(final ObjectName name) { return this.add(name.toString()); } + /** + * Find tojo by tojo id. + * @param id The id of the tojo. + * @return The tojo. + * @todo #2331:90min Add unit test for ForeignTojos.find method. + * The test should check that the method returns the tojo with the + * specified id. When the test is ready, remove that puzzle. + */ + public ForeignTojo find(final String id) { + return new ForeignTojo( + this.tojos.value() + .select(tojo -> tojo.get(Attribute.ID.key()).equals(id)) + .stream() + .findFirst() + .orElseThrow( + () -> new IllegalArgumentException( + String.format("Tojo '%s' not found", id) + ) + ) + ); + } + /** * Get the tojos that are not discovered yet. * @return The tojos. diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java index b9e2a8350f..2313c32ab9 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java @@ -55,6 +55,7 @@ import org.cactoos.text.UncheckedText; import org.eolang.maven.hash.CommitHash; import org.eolang.maven.objectionary.Objectionaries; +import org.eolang.maven.tojos.ForeignTojo; import org.eolang.maven.tojos.ForeignTojos; import org.eolang.maven.tojos.PlacedTojos; import org.eolang.maven.util.Home; @@ -445,6 +446,22 @@ Map result() throws IOException { ); } + /** + * Retrieve the entry of the last program in the eo-foreign.csv file. + * @return Tojo entry. + */ + ForeignTojo programTojo() { + return this.foreignTojos().find(this.tojoId(this.current.get() - 1)); + } + + /** + * Same as {@link FakeMaven#programTojo()} but for external tojos. + * @return Tojo entry. + */ + ForeignTojo programExternalTojo() { + return this.externalTojos().find(this.tojoId(this.current.get() - 1)); + } + /** * The version of eo-maven-plugin for tests. * @return Version. @@ -494,7 +511,7 @@ private FakeMaven withProgram(final String content) throws IOException { String.format("foo/x/main%s.eo", FakeMaven.suffix(this.current.get())) ); this.workspace.save(content, path); - final String object = String.format("foo.x.main%s", FakeMaven.suffix(this.current.get())); + final String object = this.tojoId(this.current.get()); final String scope = this.scope(); final String version = "0.25.0"; final Path source = this.workspace.absolute(path); @@ -537,6 +554,15 @@ private String scope() { return String.valueOf(this.params.getOrDefault("scope", "compile")); } + /** + * The id of the program in tojos file. + * @param id Number of the program. + * @return String id. + */ + private static String tojoId(final int id) { + return String.format("foo.x.main%s", FakeMaven.suffix(id)); + } + /** * Looks for all declared fields for mojo and its parents. * diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java index 0d3b983957..f3278eef2a 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java @@ -23,11 +23,9 @@ */ package org.eolang.maven; -import com.yegor256.tojos.MnCsv; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.LinkedList; import java.util.Map; import org.cactoos.io.ResourceOf; import org.cactoos.map.MapEntry; @@ -57,13 +55,7 @@ * @since 0.28.11 * @todo #2302:30min Refactor tests in the class. Looks like there is a lot of * code duplication among all tests in the class. Need to reduce it somehow. - * @todo #2302:30min Refactor firstEntity method. The "first entity of the - * foreign tojos" looks strange. It looks like we are tying to scan an - * intermediate state which we are trying to read in the middle of the process. - * It lead to a fragile implementation. Could we check the result somehow else? - * Or to skip that check at all? */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") @ExtendWith(OnlineCondition.class) final class ProbeMojoTest { /** @@ -75,14 +67,12 @@ final class ProbeMojoTest { @ExtendWith(OnlineCondition.class) void findsProbes(@TempDir final Path temp) throws Exception { MatcherAssert.assertThat( - ProbeMojoTest.firstEntry( - new FakeMaven(temp) - .with("foreignFormat", "json") - .withProgram(ProbeMojoTest.program()) - .execute(new FakeMaven.Probe()) - .foreignPath(), - "probed" - ), + new FakeMaven(temp) + .with("foreignFormat", "json") + .withProgram(ProbeMojoTest.program()) + .execute(new FakeMaven.Probe()) + .programTojo() + .probed(), Matchers.equalTo("5") ); } @@ -93,36 +83,31 @@ void findsProbesViaOfflineHashFile(@TempDir final Path temp) throws IOException new ResourceOf("org/eolang/maven/commits/tags.txt"), Paths.get("tags.txt") ); - final CommitHash hash = new ChCached( - new ChText(temp.resolve("tags.txt"), "master") - ); MatcherAssert.assertThat( - ProbeMojoTest.firstEntry( - new FakeMaven(temp) - .with("hsh", hash) - .withProgram(ProbeMojoTest.program()) - .execute(new FakeMaven.Probe()) - .foreignPath(), - "hash" - ), + new FakeMaven(temp) + .with( + "hsh", + new ChCached( + new ChText(temp.resolve("tags.txt"), "master") + ) + ) + .withProgram(ProbeMojoTest.program()) + .execute(new FakeMaven.Probe()) + .programTojo() + .hash(), Matchers.equalTo("mmmmmmm") ); } @Test void findsProbesViaOfflineHash(@TempDir final Path temp) throws IOException { - final CommitHash hash = new ChCached( - new ChPattern("*.*.*:abcdefg", "1.0.0") - ); MatcherAssert.assertThat( - ProbeMojoTest.firstEntry( - new FakeMaven(temp) - .with("hsh", hash) - .withProgram(ProbeMojoTest.program()) - .execute(new FakeMaven.Probe()) - .foreignPath(), - "hash" - ), + new FakeMaven(temp) + .with("hsh", new ChPattern("*.*.*:abcdefg", "1.0.0")) + .withProgram(ProbeMojoTest.program()) + .execute(new FakeMaven.Probe()) + .programTojo() + .hash(), Matchers.equalTo("abcdefg") ); } @@ -131,17 +116,17 @@ void findsProbesViaOfflineHash(@TempDir final Path temp) throws IOException { @ExtendWith(OnlineCondition.class) void findsProbesInOyRemote(@TempDir final Path temp) throws IOException { final String tag = "0.28.10"; - final CommitHash hash = new ChRemote(tag); MatcherAssert.assertThat( - ProbeMojoTest.firstEntry( - new FakeMaven(temp) - .with("tag", tag) - .with("objectionaries", new Objectionaries.Fake(new OyRemote(hash))) - .withProgram(ProbeMojoTest.program()) - .execute(new FakeMaven.Probe()) - .foreignPath(), - "probed" - ), + new FakeMaven(temp) + .with("tag", tag) + .with( + "objectionaries", + new Objectionaries.Fake(new OyRemote(new ChRemote(tag))) + ) + .withProgram(ProbeMojoTest.program()) + .execute(new FakeMaven.Probe()) + .programTojo() + .probed(), Matchers.equalTo("2") ); } @@ -165,19 +150,13 @@ void findsProbesWithVersionsInOneObjectionary(@TempDir final Path temp) throws I Matchers.is(true) ); MatcherAssert.assertThat( - "First entry of tojos after probing should have contained one probed object, but it didn't", - ProbeMojoTest.firstEntry( - maven.externalPath(), - "probed" - ), + "Program entry in tojos after probing should contain one probed object", + maven.programExternalTojo().probed(), Matchers.equalTo("1") ); MatcherAssert.assertThat( - "First entry of tojos after probing should have contained given hash, but it didn't", - ProbeMojoTest.firstEntry( - maven.externalPath(), - "hash" - ), + "Program entry in tojos after probing should contain given hash", + maven.programExternalTojo().hash(), Matchers.equalTo(hash.value()) ); } @@ -221,18 +200,12 @@ void findsProbesWithVersionsInDifferentObjectionaries(@TempDir final Path temp) ); MatcherAssert.assertThat( "First entry of tojos after probing should have contained two probed objects, but it didn't", - ProbeMojoTest.firstEntry( - maven.externalPath(), - "probed" - ), + maven.programExternalTojo().probed(), Matchers.equalTo("2") ); MatcherAssert.assertThat( "First entry of tojos after probing should have contained given hash, but it didn't", - ProbeMojoTest.firstEntry( - maven.externalPath(), - "hash" - ), + maven.programExternalTojo().hash(), Matchers.equalTo(first.value()) ); } @@ -244,8 +217,4 @@ private static String program() { ) ).asString(); } - - private static String firstEntry(final Path foreign, final String field) { - return new LinkedList<>(new MnCsv(foreign.toFile()).read()).getFirst().get(field); - } }