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

Use EqualsTester for equals/hashcode relationships #144

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading