[BAEL-4281] moves example module

This commit is contained in:
Joao Esperancinha 2020-07-21 06:37:09 +02:00
parent 289276f8ff
commit ad1d9b4b84
6 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.arraycompare;
import java.util.Objects;
public class Plane {
private final String name;
private final String model;
public Plane(String name, String model) {
this.name = name;
this.model = model;
}
public String getName() {
return name;
}
public String getModel() {
return model;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Plane plane = (Plane) o;
return Objects.equals(name, plane.name) && Objects.equals(model, plane.model);
}
@Override
public int hashCode() {
return Objects.hash(name, model);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class DeepEqualsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameContent_thenDeepEquals() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
boolean result = Arrays.deepEquals(planes1, planes2);
assertTrue("Result is not true", result);
}
@Test
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") },
new Plane[] { new Plane("Plane 1", "A320") } };
boolean result = Arrays.deepEquals(planes1, planes2);
assertFalse("Result is true", result);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class EqualsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameContent_thenEquals() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
boolean result = Arrays.equals(planes1, planes2);
assertTrue("Result is not true", result);
}
@Test
public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" };
boolean result = Arrays.equals(planes1, planes2);
assertFalse("Result is true", result);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.junit.Assert.assertThat;
public class LengthsCompareUnitTest {
@Test
public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 };
assertThat(planes1, arrayWithSize(8));
assertThat(quantities, arrayWithSize(8));
assertThat(planes1.length, is(8));
assertThat(quantities.length, is(8));
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Comparator;
import static org.junit.Assert.assertTrue;
public class OrderCompareUnitTest {
@Test
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
final Plane[][] planes1 = new Plane[][] {
new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] {
new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } };
Comparator<Plane> planeComparator = (o1, o2) -> {
if (o1.getName()
.equals(o2.getName())) {
return o2.getModel()
.compareTo(o1.getModel());
}
return o2.getName()
.compareTo(o1.getName());
};
Arrays.sort(planes1[0], planeComparator);
Arrays.sort(planes2[0], planeComparator);
boolean result = Arrays.deepEquals(planes1, planes2);
assertTrue("Result is false", result);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class ReferenceCompareUnitTest {
@Test
public void givenArray1andArray2_whenEquals_thenEqual() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = planes1;
assertSame("Objects are not equal!", planes1, planes2);
planes2[0] = "747";
assertSame("Objects are not same!", planes1, planes2);
assertEquals("Objects are not equal!", "747", planes2[0]);
assertEquals("Objects are not equal!", "747", planes1[0]);
}
@Test
public void givenArray1andArray2_whenDifferentValues_thenNotEqual() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
assertNotSame("Objects are the same!", planes1, planes2);
assertNotEquals("Objects are equal!", planes1, planes2);
}
}