Skip to content

Commit

Permalink
Merge pull request #144 from RIPE-NCC/feature/equalstester-for-equals…
Browse files Browse the repository at this point in the history
…-hashcode

Use EqualsTester for equals/hashcode relationships
  • Loading branch information
ties authored Feb 23, 2024
2 parents 1c3f053 + 3e9db63 commit 60cf1ca
Showing 1 changed file with 13 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
package net.ripe.rpki.commons.crypto.cms.roa;

import com.google.common.collect.Sets;
import com.google.common.testing.EqualsTester;
import net.ripe.ipresource.IpRange;
import org.junit.Test;

import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.fail;


public class RoaPrefixTest {
@Test
public void shouldEqualWhenSemanticallyEqual() {
var s1 = new RoaPrefix(IpRange.parse("10.0.0.0/8"));
var s1_null = new RoaPrefix(IpRange.parse("10.0.0.0/8"), null);
var s1_8 = new RoaPrefix(IpRange.parse("10.0.0.0/8"), 8);

var s1_32 = new RoaPrefix(IpRange.parse("10.0.0.0/8"), 32);

var s2 = new RoaPrefix(IpRange.parse("11.0.0.0/8"));
var s2_8 = new RoaPrefix(IpRange.parse("11.0.0.0/8"), 8);

// hashcode contract
assertThat(s1.hashCode()).isEqualTo(s1_null.hashCode());
assertThat(s1.hashCode()).isEqualTo(s1_8.hashCode());

// not equal when differing prefix or differing maxlength
assertThat(s1).isNotEqualTo(s1_32);
assertThat(s1).isNotEqualTo(s2);
assertThat(s1).isNotEqualTo(s2_8);
// or whatever
assertThat(s1).isNotEqualTo("🤷‍♂️");

// reflexive
assertThat(s1).isEqualTo(s1);
assertThat(s1).isEqualTo(s1_null);
assertThat(s1).isEqualTo(s1_8);

// symmetric
assertThat(s1_8).isEqualTo(s1);
assertThat(s1_null).isEqualTo(s1);

// transitive
assertThat(s1).isEqualTo(s1_null);
assertThat(s1_null).isEqualTo(s1_8);
// =>
assertThat(s1).isEqualTo(s1_8);
// recall: EqualsTester includes a test against an artibtrary object of another class
new EqualsTester()
.addEqualityGroup(
new RoaPrefix(IpRange.parse("10.0.0.0/8")),
new RoaPrefix(IpRange.parse("10.0.0.0/8"), null),
new RoaPrefix(IpRange.parse("10.0.0.0/8"), 8)
).addEqualityGroup(
new RoaPrefix(IpRange.parse("10.0.0.0/8"), 32)
).addEqualityGroup(
new RoaPrefix(IpRange.parse("11.0.0.0/8")),
new RoaPrefix(IpRange.parse("11.0.0.0/8"), 8)
);
}

@Test
Expand Down

0 comments on commit 60cf1ca

Please sign in to comment.