BAEL-3951: Corrected misspelling.
This commit is contained in:
parent
69c50660de
commit
48d1c94dc0
|
@ -1,22 +1,22 @@
|
||||||
package com.baeldung.java14.record;
|
package com.baeldung.java14.record;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public record Person (String name, String address) {
|
public record Person (String name, String address) {
|
||||||
|
|
||||||
public static String UNKWOWN_ADDRESS = "Unknown";
|
public static String UNKNOWN_ADDRESS = "Unknown";
|
||||||
public static String UNNAMED = "Unnamed";
|
public static String UNNAMED = "Unnamed";
|
||||||
|
|
||||||
public Person {
|
public Person {
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
Objects.requireNonNull(address);
|
Objects.requireNonNull(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(String name) {
|
public Person(String name) {
|
||||||
this(name, UNKWOWN_ADDRESS);
|
this(name, UNKNOWN_ADDRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Person unnamed(String address) {
|
public static Person unnamed(String address) {
|
||||||
return new Person(UNNAMED, address);
|
return new Person(UNNAMED, address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,150 +1,150 @@
|
||||||
package com.baeldung.java14.record;
|
package com.baeldung.java14.record;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class PersonTest {
|
public class PersonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSameNameAndAddress_whenEquals_thenPersonsEqual() {
|
public void givenSameNameAndAddress_whenEquals_thenPersonsEqual() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person1 = new Person(name, address);
|
Person person1 = new Person(name, address);
|
||||||
Person person2 = new Person(name, address);
|
Person person2 = new Person(name, address);
|
||||||
|
|
||||||
assertTrue(person1.equals(person2));
|
assertTrue(person1.equals(person2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentObject_whenEquals_thenNotEqual() {
|
public void givenDifferentObject_whenEquals_thenNotEqual() {
|
||||||
|
|
||||||
Person person = new Person("John Doe", "100 Linda Ln.");
|
Person person = new Person("John Doe", "100 Linda Ln.");
|
||||||
|
|
||||||
assertFalse(person.equals(new Object()));
|
assertFalse(person.equals(new Object()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentName_whenEquals_thenPersonsNotEqual() {
|
public void givenDifferentName_whenEquals_thenPersonsNotEqual() {
|
||||||
|
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person1 = new Person("Jane Doe", address);
|
Person person1 = new Person("Jane Doe", address);
|
||||||
Person person2 = new Person("John Doe", address);
|
Person person2 = new Person("John Doe", address);
|
||||||
|
|
||||||
assertFalse(person1.equals(person2));
|
assertFalse(person1.equals(person2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentAddress_whenEquals_thenPersonsNotEqual() {
|
public void givenDifferentAddress_whenEquals_thenPersonsNotEqual() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
|
|
||||||
Person person1 = new Person(name, "100 Linda Ln.");
|
Person person1 = new Person(name, "100 Linda Ln.");
|
||||||
Person person2 = new Person(name, "200 London Ave.");
|
Person person2 = new Person(name, "200 London Ave.");
|
||||||
|
|
||||||
assertFalse(person1.equals(person2));
|
assertFalse(person1.equals(person2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSameNameAndAddress_whenHashCode_thenPersonsEqual() {
|
public void givenSameNameAndAddress_whenHashCode_thenPersonsEqual() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person1 = new Person(name, address);
|
Person person1 = new Person(name, address);
|
||||||
Person person2 = new Person(name, address);
|
Person person2 = new Person(name, address);
|
||||||
|
|
||||||
assertEquals(person1.hashCode(), person2.hashCode());
|
assertEquals(person1.hashCode(), person2.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentObject_whenHashCode_thenNotEqual() {
|
public void givenDifferentObject_whenHashCode_thenNotEqual() {
|
||||||
|
|
||||||
Person person = new Person("John Doe", "100 Linda Ln.");
|
Person person = new Person("John Doe", "100 Linda Ln.");
|
||||||
|
|
||||||
assertNotEquals(person.hashCode(), new Object().hashCode());
|
assertNotEquals(person.hashCode(), new Object().hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentName_whenHashCode_thenPersonsNotEqual() {
|
public void givenDifferentName_whenHashCode_thenPersonsNotEqual() {
|
||||||
|
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person1 = new Person("Jane Doe", address);
|
Person person1 = new Person("Jane Doe", address);
|
||||||
Person person2 = new Person("John Doe", address);
|
Person person2 = new Person("John Doe", address);
|
||||||
|
|
||||||
assertNotEquals(person1.hashCode(), person2.hashCode());
|
assertNotEquals(person1.hashCode(), person2.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentAddress_whenHashCode_thenPersonsNotEqual() {
|
public void givenDifferentAddress_whenHashCode_thenPersonsNotEqual() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
|
|
||||||
Person person1 = new Person(name, "100 Linda Ln.");
|
Person person1 = new Person(name, "100 Linda Ln.");
|
||||||
Person person2 = new Person(name, "200 London Ave.");
|
Person person2 = new Person(name, "200 London Ave.");
|
||||||
|
|
||||||
assertNotEquals(person1.hashCode(), person2.hashCode());
|
assertNotEquals(person1.hashCode(), person2.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidNameAndAddress_whenGetNameAndAddress_thenExpectedValuesReturned() {
|
public void givenValidNameAndAddress_whenGetNameAndAddress_thenExpectedValuesReturned() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person = new Person(name, address);
|
Person person = new Person(name, address);
|
||||||
|
|
||||||
assertEquals(name, person.name());
|
assertEquals(name, person.name());
|
||||||
assertEquals(address, person.address());
|
assertEquals(address, person.address());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidNameAndAddress_whenToString_thenCorrectStringReturned() {
|
public void givenValidNameAndAddress_whenToString_thenCorrectStringReturned() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person = new Person(name, address);
|
Person person = new Person(name, address);
|
||||||
|
|
||||||
assertEquals("Person[name=" + name + ", address=" + address + "]", person.toString());
|
assertEquals("Person[name=" + name + ", address=" + address + "]", person.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void givenNullName_whenConstruct_thenErrorThrown() {
|
public void givenNullName_whenConstruct_thenErrorThrown() {
|
||||||
new Person(null, "100 Linda Ln.");
|
new Person(null, "100 Linda Ln.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void givenNullAddress_whenConstruct_thenErrorThrown() {
|
public void givenNullAddress_whenConstruct_thenErrorThrown() {
|
||||||
new Person("John Doe", null);
|
new Person("John Doe", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUnknownAddress_whenConstructing_thenAddressPopulated() {
|
public void givenUnknownAddress_whenConstructing_thenAddressPopulated() {
|
||||||
|
|
||||||
String name = "John Doe";
|
String name = "John Doe";
|
||||||
|
|
||||||
Person person = new Person(name);
|
Person person = new Person(name);
|
||||||
|
|
||||||
assertEquals(name, person.name());
|
assertEquals(name, person.name());
|
||||||
assertEquals(Person.UNKWOWN_ADDRESS, person.address());
|
assertEquals(Person.UNKNOWN_ADDRESS, person.address());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUnnamed_whenConstructingThroughFactory_thenNamePopulated() {
|
public void givenUnnamed_whenConstructingThroughFactory_thenNamePopulated() {
|
||||||
|
|
||||||
String address = "100 Linda Ln.";
|
String address = "100 Linda Ln.";
|
||||||
|
|
||||||
Person person = Person.unnamed(address);
|
Person person = Person.unnamed(address);
|
||||||
|
|
||||||
assertEquals(Person.UNNAMED, person.name());
|
assertEquals(Person.UNNAMED, person.name());
|
||||||
assertEquals(address, person.address());
|
assertEquals(address, person.address());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue