From 7438b379ef04cf3700671bfcc5cabacee5ad4e1e Mon Sep 17 00:00:00 2001 From: Frederik Boster Date: Fri, 26 Feb 2021 18:36:01 +0100 Subject: [PATCH] [MGPG-80] implement GpgVersion equality in adherence to comparibility --- .../apache/maven/plugins/gpg/GpgVersion.java | 25 +++++++++++++++++++ .../plugins/gpg/GpgVersionConsumerTest.java | 2 +- .../maven/plugins/gpg/GpgVersionTest.java | 23 +++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java b/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java index a6dc14b..eee73c9 100644 --- a/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java +++ b/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java @@ -19,6 +19,7 @@ * under the License. */ +import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -123,4 +124,28 @@ public String toString() return versionStringBuilder.toString(); } + @Override + public boolean equals( final Object other ) + { + if ( this == other ) + { + return true; + } + + if ( !( other instanceof GpgVersion ) ) + { + return false; + } + + final GpgVersion that = (GpgVersion) other; + + return compareTo( that ) == 0; + } + + @Override + public int hashCode() + { + return Arrays.hashCode( versionSegments ); + } + } diff --git a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java index 15f30cd..3bcd912 100644 --- a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java +++ b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java @@ -34,7 +34,7 @@ public void test() GpgVersionConsumer consumer = new GpgVersionConsumer(); consumer.consumeLine( "gpg (GnuPG/MacGPG2) 2.2.10" ); - assertThat( consumer.getGpgVersion().toString(), is( GpgVersion.parse( "2.2.10" ).toString() ) ); + assertThat( consumer.getGpgVersion(), is( GpgVersion.parse( "2.2.10" ) ) ); } } diff --git a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionTest.java b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionTest.java index 504f5a5..659952a 100644 --- a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionTest.java +++ b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionTest.java @@ -21,7 +21,9 @@ import org.junit.Test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; /** @@ -48,4 +50,25 @@ public void testOpposite() assertFalse( GpgVersion.parse( "gpg (GnuPG) 2.0.26 (Gpg4win 2.2.3)" ) .isBefore( GpgVersion.parse( "2.0.26" ) ) ); } + + @Test + public void testEquality() + { + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ), GpgVersion.parse( "gpg (GnuPG) 2.2.1" ) ); + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ), GpgVersion.parse( "2.2.1" ) ); + assertEquals( GpgVersion.parse( "gpg (GnuPG/MacGPG2) 2.2.10" ), GpgVersion.parse( "2.2.10" ) ); + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.0.26 (Gpg4win 2.2.3)" ), GpgVersion.parse( "2.0.26" ) ); + + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ).hashCode(), GpgVersion.parse( "gpg (GnuPG) 2.2.1" ).hashCode() ); + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ).hashCode(), GpgVersion.parse( "2.2.1" ).hashCode() ); + assertEquals( GpgVersion.parse( "gpg (GnuPG/MacGPG2) 2.2.10" ).hashCode(), GpgVersion.parse( "2.2.10" ).hashCode() ); + assertEquals( GpgVersion.parse( "gpg (GnuPG) 2.0.26 (Gpg4win 2.2.3)" ).hashCode(), GpgVersion.parse( "2.0.26" ).hashCode() ); + + assertNotEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ), GpgVersion.parse( "2.2.0" ) ); + assertNotEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ), GpgVersion.parse( "2.2" ) ); + + assertNotEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ).hashCode(), GpgVersion.parse( "2.2.0" ).hashCode() ); + assertNotEquals( GpgVersion.parse( "gpg (GnuPG) 2.2.1" ).hashCode(), GpgVersion.parse( "2.2" ).hashCode() ); + } + }