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

Improve / fix equals and hashCode tests #316

Merged
merged 1 commit into from
Dec 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>${guava.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
27 changes: 6 additions & 21 deletions src/test/java/org/threeten/extra/TestDayOfMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RetryingTest;

import com.google.common.testing.EqualsTester;

/**
* Test DayOfMonth.
*/
Expand Down Expand Up @@ -731,29 +733,12 @@ public void test_compareTo_nullDayOfMonth() {
// equals() / hashCode()
//-----------------------------------------------------------------------
@Test
public void test_equals() {
public void test_equals_and_hashCode() {
EqualsTester equalsTester = new EqualsTester();
for (int i = 1; i <= MAX_LENGTH; i++) {
DayOfMonth a = DayOfMonth.of(i);
for (int j = 1; j <= MAX_LENGTH; j++) {
DayOfMonth b = DayOfMonth.of(j);
assertEquals(i == j, a.equals(b));
assertEquals(i == j, a.hashCode() == b.hashCode());
}
equalsTester.addEqualityGroup(DayOfMonth.of(i), DayOfMonth.of(i));
}
}

@Test
public void test_equals_nullDayOfMonth() {
DayOfMonth dom = null;
DayOfMonth test = DayOfMonth.of(1);
assertEquals(false, test.equals(dom));
}

@Test
public void test_equals_incorrectType() {
DayOfMonth test = DayOfMonth.of(1);
Object obj = "Incorrect type";
assertEquals(false, test.equals(obj));
equalsTester.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
27 changes: 6 additions & 21 deletions src/test/java/org/threeten/extra/TestDayOfYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RetryingTest;

import com.google.common.testing.EqualsTester;

/**
* Test DayOfYear.
*/
Expand Down Expand Up @@ -581,29 +583,12 @@ public void test_compareTo_nullDayOfYear() {
// equals() / hashCode()
//-----------------------------------------------------------------------
@Test
public void test_equals() {
public void test_equals_and_hashCode() {
EqualsTester equalsTester = new EqualsTester();
for (int i = 1; i <= LEAP_YEAR_LENGTH; i++) {
DayOfYear a = DayOfYear.of(i);
for (int j = 1; j <= LEAP_YEAR_LENGTH; j++) {
DayOfYear b = DayOfYear.of(j);
assertEquals(i == j, a.equals(b));
assertEquals(i == j, a.hashCode() == b.hashCode());
}
equalsTester.addEqualityGroup(DayOfYear.of(i), DayOfYear.of(i));
}
}

@Test
public void test_equals_nullDayOfYear() {
DayOfYear doy = null;
DayOfYear test = DayOfYear.of(1);
assertEquals(false, test.equals(doy));
}

@Test
public void test_equals_incorrectType() {
DayOfYear test = DayOfYear.of(1);
Object obj = "Incorrect type";
assertEquals(false, test.equals(obj));
equalsTester.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
35 changes: 7 additions & 28 deletions src/test/java/org/threeten/extra/TestDays.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -538,34 +540,11 @@ public void test_compareTo_null() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
Days test5 = Days.of(5);
Days test6 = Days.of(6);
assertEquals(true, test5.equals(test5));
assertEquals(false, test5.equals(test6));
assertEquals(false, test6.equals(test5));
}

@Test
public void test_equals_null() {
Days test5 = Days.of(5);
assertEquals(false, test5.equals(null));
}

@Test
public void test_equals_otherClass() {
Days test5 = Days.of(5);
Object obj = "";
assertEquals(false, test5.equals(obj));
}

//-----------------------------------------------------------------------
@Test
public void test_hashCode() {
Days test5 = Days.of(5);
Days test6 = Days.of(6);
assertEquals(true, test5.hashCode() == test5.hashCode());
assertEquals(false, test5.hashCode() == test6.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(Days.of(5), Days.of(5))
.addEqualityGroup(Days.of(6), Days.of(6))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
34 changes: 7 additions & 27 deletions src/test/java/org/threeten/extra/TestHours.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -425,33 +427,11 @@ public void test_compareTo_null() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
Hours test5 = Hours.of(5);
Hours test6 = Hours.of(6);
assertEquals(true, test5.equals(test5));
assertEquals(false, test5.equals(test6));
assertEquals(false, test6.equals(test5));
}

@Test
public void test_equals_null() {
Hours test5 = Hours.of(5);
assertEquals(false, test5.equals(null));
}

@Test
public void test_equals_otherClass() {
Hours test5 = Hours.of(5);
assertEquals(false, test5.equals((Object) ""));
}

//-----------------------------------------------------------------------
@Test
public void test_hashCode() {
Hours test5 = Hours.of(5);
Hours test6 = Hours.of(6);
assertEquals(true, test5.hashCode() == test5.hashCode());
assertEquals(false, test5.hashCode() == test6.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(Hours.of(5), Hours.of(5))
.addEqualityGroup(Hours.of(6), Hours.of(6))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
20 changes: 8 additions & 12 deletions src/test/java/org/threeten/extra/TestInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -916,18 +918,12 @@ public void test_toDuration() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
Interval a = Interval.of(NOW1, NOW2);
Interval a2 = Interval.of(NOW1, NOW2);
Interval b = Interval.of(NOW1, NOW3);
Interval c = Interval.of(NOW2, NOW2);
assertEquals(true, a.equals(a));
assertEquals(true, a.equals(a2));
assertEquals(false, a.equals(b));
assertEquals(false, a.equals(c));
assertEquals(false, a.equals(null));
assertEquals(false, a.equals((Object) ""));
assertEquals(true, a.hashCode() == a2.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(Interval.of(NOW1, NOW2), Interval.of(NOW1, NOW2))
.addEqualityGroup(Interval.of(NOW1, NOW3), Interval.of(NOW1, NOW3))
.addEqualityGroup(Interval.of(NOW2, NOW2), Interval.of(NOW2, NOW2))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
19 changes: 7 additions & 12 deletions src/test/java/org/threeten/extra/TestLocalDateRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.collect.Range;
import com.google.common.testing.EqualsTester;

/**
* Test date range.
Expand Down Expand Up @@ -1190,18 +1191,12 @@ public void test_toPeriod_unbounded_MAX() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
LocalDateRange a = LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_29);
LocalDateRange a2 = LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_29);
LocalDateRange b = LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_30);
LocalDateRange c = LocalDateRange.of(DATE_2012_07_28, DATE_2012_07_29);
assertEquals(true, a.equals(a));
assertEquals(true, a.equals(a2));
assertEquals(false, a.equals(b));
assertEquals(false, a.equals(c));
assertEquals(false, a.equals(null));
assertEquals(false, a.equals((Object) ""));
assertEquals(true, a.hashCode() == a2.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_29), LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_29))
.addEqualityGroup(LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_30), LocalDateRange.of(DATE_2012_07_27, DATE_2012_07_30))
.addEqualityGroup(LocalDateRange.of(DATE_2012_07_28, DATE_2012_07_29), LocalDateRange.of(DATE_2012_07_28, DATE_2012_07_29))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
34 changes: 7 additions & 27 deletions src/test/java/org/threeten/extra/TestMinutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -439,33 +441,11 @@ public void test_compareTo_null() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
Minutes test5 = Minutes.of(5);
Minutes test6 = Minutes.of(6);
assertEquals(true, test5.equals(test5));
assertEquals(false, test5.equals(test6));
assertEquals(false, test6.equals(test5));
}

@Test
public void test_equals_null() {
Minutes test5 = Minutes.of(5);
assertEquals(false, test5.equals(null));
}

@Test
public void test_equals_otherClass() {
Minutes test5 = Minutes.of(5);
assertEquals(false, test5.equals((Object) ""));
}

//-----------------------------------------------------------------------
@Test
public void test_hashCode() {
Minutes test5 = Minutes.of(5);
Minutes test6 = Minutes.of(6);
assertEquals(true, test5.hashCode() == test5.hashCode());
assertEquals(false, test5.hashCode() == test6.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(Minutes.of(5), Minutes.of(5))
.addEqualityGroup(Minutes.of(6), Minutes.of(6))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
35 changes: 7 additions & 28 deletions src/test/java/org/threeten/extra/TestMonths.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -531,34 +533,11 @@ public void test_compareTo_null() {

//-----------------------------------------------------------------------
@Test
public void test_equals() {
Months test5 = Months.of(5);
Months test6 = Months.of(6);
assertEquals(true, test5.equals(test5));
assertEquals(false, test5.equals(test6));
assertEquals(false, test6.equals(test5));
}

@Test
public void test_equals_null() {
Months test5 = Months.of(5);
assertEquals(false, test5.equals(null));
}

@Test
public void test_equals_otherClass() {
Months test5 = Months.of(5);
Object obj = "";
assertEquals(false, test5.equals(obj));
}

//-----------------------------------------------------------------------
@Test
public void test_hashCode() {
Months test5 = Months.of(5);
Months test6 = Months.of(6);
assertEquals(true, test5.hashCode() == test5.hashCode());
assertEquals(false, test5.hashCode() == test6.hashCode());
public void test_equals_and_hashCode() {
new EqualsTester()
.addEqualityGroup(Months.of(5), Months.of(5))
.addEqualityGroup(Months.of(6), Months.of(6))
.testEquals();
}

//-----------------------------------------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/org/threeten/extra/TestMutableClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

import org.junit.jupiter.api.Test;

import com.google.common.testing.EqualsTester;

/**
* Test class.
*/
Expand Down Expand Up @@ -262,12 +264,11 @@ public void test_equals() {
MutableClock withOtherZone = clock.withZone(ZoneOffset.MIN);
MutableClock withSameZone = withOtherZone.withZone(ZoneOffset.UTC);
MutableClock independent = MutableClock.epochUTC();
assertEquals(clock, clock);
assertNotEquals(null, clock);
assertNotEquals("", clock);
assertNotEquals(withOtherZone, clock);
assertEquals(clock, withSameZone);
assertNotEquals(clock, independent);
new EqualsTester()
.addEqualityGroup(clock, clock, withSameZone)
.addEqualityGroup(withOtherZone)
.addEqualityGroup(independent)
.testEquals();
}

@Test
Expand Down
Loading