From f1faeca71f2876931f48f134f4682d0902a0f8a9 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 24 Sep 2018 11:19:48 -0300 Subject: [PATCH 1/2] For #942: Fixed `TextEnvelope.equals()` misbehavior. --- .../java/org/cactoos/text/TextEnvelope.java | 17 +++++++++++++- .../org/cactoos/text/TextEnvelopeTest.java | 23 ++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index c92c6a0e54..ae2985c80d 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -73,8 +73,23 @@ public final int hashCode() { return new UncheckedScalar<>(this.origin).value().hashCode(); } + // @todo #942:30min Refactor TextEnvelope.equals(). Current implementation + // of TextEnvelope.equals() uses some things that we must avoid, like more + // than one return on method, instance of usage and typecasting. Refactor + // this method so we can get rid of these smelly things. + // @Override + @SuppressWarnings("PMD.OnlyOneReturn") public final boolean equals(final Object obj) { - return new UncheckedScalar<>(this.origin).value().equals(obj); + if (this == obj) { + return true; + } + if (!(obj instanceof TextEnvelope)) { + return false; + } + final TextEnvelope that = (TextEnvelope) obj; + return new UncheckedText(this).asString().equals( + new UncheckedText(that).asString() + ); } } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index 63a9214554..4370112d48 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -51,15 +51,32 @@ public void testAsString() throws Exception { } /** * Test for {@link TextEnvelope#equals(Object)} method. Must assert - * that the envelope value is equal to its string value. + * that the envelope value is equal another text representing the same + * value. */ @Test public void testEquals() { final String text = "equals"; MatcherAssert.assertThat( - "Envelope value does not match its represented String value", + "Envelope does not match text representing the same value", new TextEnvelopeDummy(text), - new IsEqual<>(text) + new IsEqual<>(new TextOf(text)) + ); + } + + /** + * Test for {@link TextEnvelope#equals(Object)} method. Must assert + * that the envelope value is equal another text representing the same + * value (in this case a {@link JoinedText}). + */ + @Test + public void testEqualsOtherText() { + MatcherAssert.assertThat( + "Envelope does not match another text representing the same value", + new TextEnvelopeDummy("isequaltoanothertext"), + new IsEqual<>( + new JoinedText("", "is", "equal", "to", "another", "text") + ) ); } /** From d38aa54dc346f49c01d7235dab4ba4684df7f092 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 26 Sep 2018 13:36:26 -0300 Subject: [PATCH 2/2] For #942: Changed `equals` to match agains `Text` interface, fixed todo. --- src/main/java/org/cactoos/text/TextEnvelope.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index ae2985c80d..830896d799 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -74,9 +74,9 @@ public final int hashCode() { } // @todo #942:30min Refactor TextEnvelope.equals(). Current implementation - // of TextEnvelope.equals() uses some things that we must avoid, like more - // than one return on method, instance of usage and typecasting. Refactor - // this method so we can get rid of these smelly things. + // of TextEnvelope.equals() uses some things that we must avoid, like more + // than one return on method, instance of usage and typecasting. Refactor + // this method so we can get rid of these smelly things. // @Override @SuppressWarnings("PMD.OnlyOneReturn") @@ -84,10 +84,10 @@ public final boolean equals(final Object obj) { if (this == obj) { return true; } - if (!(obj instanceof TextEnvelope)) { + if (!(obj instanceof Text)) { return false; } - final TextEnvelope that = (TextEnvelope) obj; + final Text that = (Text) obj; return new UncheckedText(this).asString().equals( new UncheckedText(that).asString() );