[BAEL-3981] Moved inner classes to upper level
This commit is contained in:
@@ -35,34 +35,34 @@ class ComparableInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndWrongComparable joe = new Person.PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
Person.PersonWithEqualsAndWrongComparable richard = new Person.PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndWrongComparable mike = new Person.PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
|
||||
SortedSet<Person.PersonWithEqualsAndWrongComparable> people = new TreeSet<>();
|
||||
SortedSet<PersonWithEqualsAndWrongComparable> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
@@ -71,34 +71,34 @@ class ComparableInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparable joe = new Person.PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
Person.PersonWithEqualsAndComparable richard = new Person.PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparable mike = new Person.PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
|
||||
SortedSet<Person.PersonWithEqualsAndComparable> people = new TreeSet<>();
|
||||
SortedSet<PersonWithEqualsAndComparable> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
|
||||
@@ -10,16 +10,16 @@ class ComparatorInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenListOfTwoPersonWithEqualsAndComparatorByFirstName_whenSort_thenSortedByFirstNames() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals allan = new PersonWithEquals("Allan", "Dale");
|
||||
|
||||
List<Person.PersonWithEquals> people = new ArrayList<>();
|
||||
List<PersonWithEquals> people = new ArrayList<>();
|
||||
people.add(joe);
|
||||
people.add(allan);
|
||||
|
||||
Comparator<Person.PersonWithEquals> compareByFirstNames = new Comparator<Person.PersonWithEquals>() {
|
||||
Comparator<PersonWithEquals> compareByFirstNames = new Comparator<PersonWithEquals>() {
|
||||
@Override
|
||||
public int compare(Person.PersonWithEquals o1, Person.PersonWithEquals o2) {
|
||||
public int compare(PersonWithEquals o1, PersonWithEquals o2) {
|
||||
return o1.firstName().compareTo(o2.firstName());
|
||||
}
|
||||
};
|
||||
@@ -30,14 +30,14 @@ class ComparatorInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenListOfTwoPersonWithEqualsAndComparatorByFirstNameFunctionalStyle_whenSort_thenSortedByFirstNames() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals allan = new Person.PersonWithEquals("Allan", "Dale");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals allan = new PersonWithEquals("Allan", "Dale");
|
||||
|
||||
List<Person.PersonWithEquals> people = new ArrayList<>();
|
||||
List<PersonWithEquals> people = new ArrayList<>();
|
||||
people.add(joe);
|
||||
people.add(allan);
|
||||
|
||||
Comparator<Person.PersonWithEquals> compareByFirstNames = Comparator.comparing(Person.PersonWithEquals::firstName);
|
||||
Comparator<PersonWithEquals> compareByFirstNames = Comparator.comparing(PersonWithEquals::firstName);
|
||||
people.sort(compareByFirstNames);
|
||||
|
||||
assertThat(people).containsExactly(allan, joe);
|
||||
@@ -45,34 +45,34 @@ class ComparatorInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparableUsingComparator joe = new Person.PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
Person.PersonWithEqualsAndComparableUsingComparator richard = new Person.PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
Person.PersonWithEqualsAndComparableUsingComparator mike = new Person.PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
|
||||
SortedSet<Person.PersonWithEqualsAndComparableUsingComparator> people = new TreeSet<>();
|
||||
SortedSet<PersonWithEqualsAndComparableUsingComparator> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
|
||||
@@ -49,24 +49,24 @@ class EqualsMethodUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithoutEqualsWithSameNames_whenEquals_thenFalse() {
|
||||
Person.PersonWithoutEquals joe = new Person.PersonWithoutEquals("Joe", "Portman");
|
||||
Person.PersonWithoutEquals joeAgain = new Person.PersonWithoutEquals("Joe", "Portman");
|
||||
PersonWithoutEquals joe = new PersonWithoutEquals("Joe", "Portman");
|
||||
PersonWithoutEquals joeAgain = new PersonWithoutEquals("Joe", "Portman");
|
||||
|
||||
assertThat(joe.equals(joeAgain)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsWithSameNames_whenEquals_thenTrue() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
assertThat(joe.equals(joeAgain)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWittEqualsWithDifferentNames_whenEquals_thenFalse() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman");
|
||||
|
||||
assertThat(joe.equals(nathalie)).isFalse();
|
||||
}
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
package com.baeldung.comparing;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.primitives.Booleans;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.google.common.primitives.Shorts;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class GuavaUnitTest {
|
||||
@@ -66,8 +59,8 @@ class GuavaUnitTest {
|
||||
class ComparisonChainClass {
|
||||
@Test
|
||||
void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNathalieSecond() {
|
||||
Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman");
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
int comparisonResult = ComparisonChain.start()
|
||||
.compare(nathalie.lastName(), joe.lastName())
|
||||
|
||||
@@ -10,40 +10,40 @@ class ObjectsEqualsStaticMethodUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsWithSameNames_whenObjectsEquals_thenTrue() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals joeAgain = new Person.PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
assertThat(Objects.equals(joe, joeAgain)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsWithDifferentNames_whenObjectsEquals_thenFalse() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals nathalie = new Person.PersonWithEquals("Nathalie", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals nathalie = new PersonWithEquals("Nathalie", "Portman");
|
||||
|
||||
assertThat(Objects.equals(joe, nathalie)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsFirstNull_whenObjectsEquals_thenFalse() {
|
||||
Person.PersonWithEquals nobody = null;
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals nobody = null;
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
assertThat(Objects.equals(nobody, joe)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoObjectsSecondtNull_whenObjectsEquals_thenFalse() {
|
||||
Person.PersonWithEquals joe = new Person.PersonWithEquals("Joe", "Portman");
|
||||
Person.PersonWithEquals nobody = null;
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals nobody = null;
|
||||
|
||||
assertThat(Objects.equals(joe, nobody)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoObjectsNull_whenObjectsEquals_thenTrue() {
|
||||
Person.PersonWithEquals nobody = null;
|
||||
Person.PersonWithEquals nobodyAgain = null;
|
||||
PersonWithEquals nobody = null;
|
||||
PersonWithEquals nobodyAgain = null;
|
||||
|
||||
assertThat(Objects.equals(nobody, nobodyAgain)).isTrue();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user