From af9612c3333f6e8e6a20bf64ac687dbac80a88d9 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 16 Aug 2023 16:21:23 +0300 Subject: [PATCH] feat(#2392): Add tests for ForeignTojos.find method --- .../org/eolang/maven/tojos/ForeignTojo.java | 20 ++++++++++ .../org/eolang/maven/tojos/ForeignTojos.java | 3 -- .../eolang/maven/tojos/ForeignTojosTest.java | 37 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) 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 ddd4bd3472..4059aea906 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 @@ -27,6 +27,7 @@ import com.yegor256.tojos.Tojo; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Objects; import org.eolang.maven.Coordinates; import org.eolang.maven.hash.CommitHash; import org.eolang.maven.util.Rel; @@ -307,4 +308,23 @@ public ForeignTojo withVer(final String ver) { public String ver() { return this.delegate.get(ForeignTojos.Attribute.VER.key()); } + + @Override + public boolean equals(final Object other) { + final boolean result; + if (this == other) { + result = true; + } else if (other == null || this.getClass() != other.getClass()) { + result = false; + } else { + final ForeignTojo tojo = (ForeignTojo) other; + result = Objects.equals(this.delegate, tojo.delegate); + } + return result; + } + + @Override + public int hashCode() { + return Objects.hash(this.delegate); + } } 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 37fa771d15..6100d5f39b 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 @@ -128,9 +128,6 @@ public ForeignTojo add(final ObjectName name) { * 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( diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/tojos/ForeignTojosTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/tojos/ForeignTojosTest.java index 41b83eccaf..90b385a768 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/tojos/ForeignTojosTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/tojos/ForeignTojosTest.java @@ -24,10 +24,14 @@ package org.eolang.maven.tojos; import java.io.IOException; +import java.util.Arrays; +import java.util.List; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -79,6 +83,39 @@ void doesNotContain(final String existing, final String considered) { ); } + @Test + void findsLookingTojoCorrectly() { + final String looking = "looking"; + MatcherAssert.assertThat( + "Found tojo should be the same as added", + this.tojos.add(looking), + Matchers.equalTo(this.tojos.find(looking)) + ); + } + + @Test + void throwsExceptionIfTojoWasNotFound() { + final String id = "absent"; + Assertions.assertThrows( + IllegalArgumentException.class, + () -> this.tojos.find(id), + String.format("Should throw an exception if tojo with id='%s' was not found", id) + ); + } + + @Test + void findsAnyTojoIfSeveralTojosWithTheSameIdWereAdded() { + final String same = "same"; + final ForeignTojo first = this.tojos.add(same); + final ForeignTojo second = this.tojos.add(same); + final List expected = Arrays.asList(first, second); + MatcherAssert.assertThat( + "We don't care which tojo will be returned, but it should be one of the added tojos", + expected, + Matchers.hasItem(this.tojos.find(same)) + ); + } + @AfterEach void tearDown() throws IOException { this.tojos.close();