Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MGPG-80] implement GpgVersion equality in adherence to comparibility #12

Merged
merged 1 commit into from Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -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 );
}

}
Expand Up @@ -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" ) ) );
}

}
23 changes: 23 additions & 0 deletions src/test/java/org/apache/maven/plugins/gpg/GpgVersionTest.java
Expand Up @@ -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;

/**
Expand All @@ -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" ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also test that hashcodes are not equal here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While objects which are equal must produce the same hash code it is neither required nor expected that unequal objects always produce different hash codes.
Arrays.hashCode should already provide a reasonable implementation to minimize hash collisions.

Object#hashCode JavaDoc:

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
[ ... ]

  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed expected to a very high degree of probability, specifically one in 4.2 billion. It is also expected that hashcodes are deterministic and not random so if this case is not equal now it should not be equal on the next run. Finally it's expected that a good hash algorithm move nearby values far apart so this one should definitely be different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test statements to assert that the hash codes are different.

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() );
}

}