Merge branch 'master' of https://github.com/AlexFlorinHriscu/tutorials
This commit is contained in:
commit
956445cf37
|
@ -1,31 +1,31 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>core-java-arrays-3</artifactId>
|
<artifactId>core-java-arrays-3</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-arrays-3</name>
|
<name>core-java-arrays-3</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>parent-java</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-java</relativePath>
|
<relativePath>../../parent-java</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<assertj.version>3.14.0</assertj.version>
|
<assertj.version>3.14.0</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -9,92 +9,88 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ArraysDeepEqualsUnitTest {
|
public class ArraysDeepEqualsUnitTest {
|
||||||
|
|
||||||
class Person {
|
class Person {
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
Person(int id, String name, int age) {
|
Person(int id, String name, int age) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof Person))
|
if (!(obj instanceof Person))
|
||||||
return false;
|
return false;
|
||||||
Person person = (Person) obj;
|
Person person = (Person) obj;
|
||||||
return id == person.id && name.equals(person.name) && age == person.age;
|
return id == person.id && name.equals(person.name) && age == person.age;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, name, age);
|
return Objects.hash(id, name, age);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoUnidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
void givenTwoUnidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
||||||
Object[] anArray = new Object[] { "string1", "string2", "string3" };
|
Object[] anArray = new Object[] { "string1", "string2", "string3" };
|
||||||
Object[] anotherArray = new Object[] { "string1", "string2", "string3" };
|
Object[] anotherArray = new Object[] { "string1", "string2", "string3" };
|
||||||
|
|
||||||
assertTrue(Arrays.equals(anArray, anotherArray));
|
assertTrue(Arrays.equals(anArray, anotherArray));
|
||||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoUnidimensionalObjectTypeArraysWithNullElements_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
void givenTwoUnidimensionalObjectTypeArraysWithNullElements_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
||||||
Object[] anArray = new Object[] { "string1", null, "string3" };
|
Object[] anArray = new Object[] { "string1", null, "string3" };
|
||||||
Object[] anotherArray = new Object[] { "string1", null, "string3" };
|
Object[] anotherArray = new Object[] { "string1", null, "string3" };
|
||||||
|
|
||||||
assertTrue(Arrays.equals(anArray, anotherArray));
|
assertTrue(Arrays.equals(anArray, anotherArray));
|
||||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoUnidimensionalObjectTypeArraysWithNestedElements_whenUsingEqualsAndDeepEquals_thenShouldReturnDifferently() {
|
void givenTwoUnidimensionalObjectTypeArraysWithNestedElements_whenUsingEqualsAndDeepEquals_thenShouldReturnDifferently() {
|
||||||
Object[] anArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
Object[] anArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
||||||
Object[] anotherArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
Object[] anotherArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
||||||
|
|
||||||
assertFalse(Arrays.equals(anArray, anotherArray));
|
assertFalse(Arrays.equals(anArray, anotherArray));
|
||||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoMultidimensionalPrimitiveTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
void givenTwoMultidimensionalPrimitiveTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
||||||
int[][] anArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
int[][] anArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
||||||
int[][] anotherArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
int[][] anotherArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
||||||
|
|
||||||
assertFalse(Arrays.equals(anArray, anotherArray));
|
assertFalse(Arrays.equals(anArray, anotherArray));
|
||||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoMultidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
void givenTwoMultidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
||||||
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) },
|
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||||
{ new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||||
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) },
|
|
||||||
{ new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
|
||||||
|
|
||||||
assertFalse(Arrays.equals(personArray1, personArray2));
|
assertFalse(Arrays.equals(personArray1, personArray2));
|
||||||
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenTwoMultidimensionalObjectTypeArrays_whenUsingDeepEqualsFromObjectsAndArraysClasses_thenBothShouldReturnTrue() {
|
void givenTwoMultidimensionalObjectTypeArrays_whenUsingDeepEqualsFromObjectsAndArraysClasses_thenBothShouldReturnTrue() {
|
||||||
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) },
|
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||||
{ new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||||
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) },
|
|
||||||
{ new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
|
||||||
|
|
||||||
assertTrue(Objects.deepEquals(personArray1, personArray2));
|
assertTrue(Objects.deepEquals(personArray1, personArray2));
|
||||||
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
<module>core-java-annotations</module>
|
<module>core-java-annotations</module>
|
||||||
<module>core-java-arrays</module>
|
<module>core-java-arrays</module>
|
||||||
<module>core-java-arrays-2</module>
|
<module>core-java-arrays-2</module>
|
||||||
<module>core-java-arrays-3</module>
|
<module>core-java-arrays-3</module>
|
||||||
|
|
||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
<module>core-java-collections-2</module>
|
<module>core-java-collections-2</module>
|
||||||
|
|
Loading…
Reference in New Issue