Merge branch 'eugenp:master' into master
This commit is contained in:
commit
0952447f3a
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class EmployeeDelomboked {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public EmployeeDelomboked() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == this) {
|
||||||
|
return true;
|
||||||
|
} else if (!(o instanceof Employee)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
Employee other = (Employee) o;
|
||||||
|
if (!other.canEqual(this)) {
|
||||||
|
return false;
|
||||||
|
} else if (this.getId() != other.getId()) {
|
||||||
|
return false;
|
||||||
|
} else if (this.getAge() != other.getAge()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
Object this$name = this.getName();
|
||||||
|
Object other$name = other.getName();
|
||||||
|
if (this$name == null) {
|
||||||
|
if (other$name == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (this$name.equals(other$name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean canEqual(Object other) {
|
||||||
|
return other instanceof Employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
final int PRIME = 59;
|
||||||
|
int result = 1;
|
||||||
|
result = result * PRIME + this.getId();
|
||||||
|
result = result * PRIME + this.getAge();
|
||||||
|
Object $name = this.getName();
|
||||||
|
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.commonissue;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
private Manager manager;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.commonissue;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode(exclude = "manager")
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class EmployeeV2 {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
private ManagerV2 manager;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.commonissue;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Manager {
|
||||||
|
private String name;
|
||||||
|
private Employee assistantManager;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.commonissue;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ManagerV2 {
|
||||||
|
private String name;
|
||||||
|
private EmployeeV2 assistantManager;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@EqualsAndHashCode(exclude = {"id", "age"})
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class EmployeeDelomboked {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (o == this) return true;
|
||||||
|
if (!(o instanceof Employee)) return false;
|
||||||
|
final Employee other = (Employee) o;
|
||||||
|
if (!other.canEqual((Object) this)) return false;
|
||||||
|
final Object this$name = this.getName();
|
||||||
|
final Object other$name = other.getName();
|
||||||
|
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean canEqual(final Object other) {
|
||||||
|
return other instanceof Employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int PRIME = 59;
|
||||||
|
int result = 1;
|
||||||
|
final Object $name = this.getName();
|
||||||
|
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
@EqualsAndHashCode.Exclude
|
||||||
|
private int id;
|
||||||
|
@EqualsAndHashCode.Exclude
|
||||||
|
private int age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class EmployeeDelomboked {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (o == this) return true;
|
||||||
|
if (!(o instanceof com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee)) return false;
|
||||||
|
final com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee other = (com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee) o;
|
||||||
|
if (!other.canEqual((Object) this)) return false;
|
||||||
|
final Object this$name = this.getName();
|
||||||
|
final Object other$name = other.getName();
|
||||||
|
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
final int PRIME = 59;
|
||||||
|
int result = 1;
|
||||||
|
final Object $name = this.getName();
|
||||||
|
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.classlevel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(of = {"name", "id"})
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.fieldlevel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
@EqualsAndHashCode.Include
|
||||||
|
private String name;
|
||||||
|
@EqualsAndHashCode.Include
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
@EqualsAndHashCode.Include
|
||||||
|
public boolean hasOddId() {
|
||||||
|
return id % 2 != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class EmployeeDelomboked {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public boolean hasOddId() {
|
||||||
|
return id % 2 != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (o == this) return true;
|
||||||
|
if (!(o instanceof Employee)) return false;
|
||||||
|
final Employee other = (Employee) o;
|
||||||
|
if (!other.canEqual((Object) this)) return false;
|
||||||
|
if (this.getId() != other.getId()) return false;
|
||||||
|
if (this.getAge() != other.getAge()) return false;
|
||||||
|
if (this.hasOddId() != other.hasOddId()) return false;
|
||||||
|
final Object this$name = this.getName();
|
||||||
|
final Object other$name = other.getName();
|
||||||
|
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
final int PRIME = 59;
|
||||||
|
int result = 1;
|
||||||
|
result = result * PRIME + this.getId();
|
||||||
|
result = result * PRIME + this.getAge();
|
||||||
|
result = result * PRIME + (this.hasOddId() ? 79 : 97);
|
||||||
|
final Object $name = this.getName();
|
||||||
|
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.inheritance;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Employee {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
private int age;
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.inheritance;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class Manager extends Employee {
|
||||||
|
private String departmentName;
|
||||||
|
private int uid;
|
||||||
|
|
||||||
|
public Manager(String departmentName, int uid, String name, int id, int age) {
|
||||||
|
super(name, id, age);
|
||||||
|
this.departmentName = departmentName;
|
||||||
|
this.uid = uid;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.inheritance;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class ManagerDelomboked extends Employee {
|
||||||
|
private String departmentName;
|
||||||
|
private int uid;
|
||||||
|
|
||||||
|
public ManagerDelomboked(String departmentName, int uid, String name, int id, int age) {
|
||||||
|
super(name, id, age);
|
||||||
|
this.departmentName = departmentName;
|
||||||
|
this.uid = uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (o == this) return true;
|
||||||
|
if (!(o instanceof com.baeldung.lombok.equalsandhashcode.inheritance.Manager)) return false;
|
||||||
|
final com.baeldung.lombok.equalsandhashcode.inheritance.Manager other = (com.baeldung.lombok.equalsandhashcode.inheritance.Manager) o;
|
||||||
|
if (!other.canEqual((Object) this)) return false;
|
||||||
|
if (!super.equals(o)) return false;
|
||||||
|
if (this.getUid() != other.getUid()) return false;
|
||||||
|
final Object this$departmentName = this.getDepartmentName();
|
||||||
|
final Object other$departmentName = other.getDepartmentName();
|
||||||
|
if (this$departmentName == null ? other$departmentName != null : !this$departmentName.equals(other$departmentName)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
final int PRIME = 59;
|
||||||
|
int result = super.hashCode();
|
||||||
|
result = result * PRIME + this.getUid();
|
||||||
|
final Object $departmentName = this.getDepartmentName();
|
||||||
|
result = result * PRIME + ($departmentName == null ? 43 : $departmentName.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.inheritance;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
public class ManagerV2 extends Employee {
|
||||||
|
private String departmentName;
|
||||||
|
private int uid;
|
||||||
|
|
||||||
|
public ManagerV2(String departmentName, int uid, String name, int id, int age) {
|
||||||
|
super(name, id, age);
|
||||||
|
this.departmentName = departmentName;
|
||||||
|
this.uid = uid;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeOnClassLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfAllFieldsAreSame() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeThree = new Employee("Mannat", 15, 22);
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertNotEquals(employee, employeeThree);
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
assertNotEquals(employee.hashCode(), employeeThree.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.commonissue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = StackOverflowError.class)
|
||||||
|
public void whenUsingEqualsAndHashInObjectWithCircularDependency_thenCallingEqualsAndHasCodeWillThrowStackOverFlowError() {
|
||||||
|
Manager manager = new Manager("Mannat", null);
|
||||||
|
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22, manager);
|
||||||
|
|
||||||
|
manager.setAssistantManager(employee);
|
||||||
|
|
||||||
|
employee.setManager(manager);
|
||||||
|
|
||||||
|
int hash = employee.hashCode();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashInObjectAndExcludingCircularDependencyField_thenNoStackOverFlowError() {
|
||||||
|
ManagerV2 manager = new ManagerV2("Mannat", null);
|
||||||
|
|
||||||
|
EmployeeV2 employee = new EmployeeV2("Nimi", 15, 22, manager);
|
||||||
|
|
||||||
|
manager.setAssistantManager(employee);
|
||||||
|
|
||||||
|
employee.setManager(manager);
|
||||||
|
|
||||||
|
int hash = employee.hashCode();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeOnClassLevelAndExcludingFields_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfExcludedFieldsAreNotSame() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 17, 21);
|
||||||
|
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.exclude.fieldLevel;
|
||||||
|
|
||||||
|
import com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeWithExcludingFields_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfExcludedFieldsAreNotSame() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 17, 21);
|
||||||
|
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.classlevel;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeOnClassLevelAndIncludeFields_thenTwoObjectsAreEqualAndHaveSameHashCodeIfIncludedFieldsAreSame() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeThree = new Employee("Nimi", 15, 23);
|
||||||
|
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertEquals(employee, employeeThree);
|
||||||
|
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
assertEquals(employee.hashCode(), employeeThree.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.fieldlevel;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeOnFieldLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfIncludedFieldsAreSame() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeThree = new Employee("Nimi", 15, 23);
|
||||||
|
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertEquals(employee, employeeThree);
|
||||||
|
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
assertEquals(employee.hashCode(), employeeThree.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeOnMethodLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfMethodsReturnSameValue() {
|
||||||
|
Employee employee = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeTwo = new Employee("Nimi", 15, 22);
|
||||||
|
Employee employeeThree = new Employee("Nimi", 15, 23);
|
||||||
|
|
||||||
|
assertEquals(employee, employeeTwo);
|
||||||
|
assertNotEquals(employee, employeeThree);
|
||||||
|
|
||||||
|
assertEquals(employee.hashCode(), employeeTwo.hashCode());
|
||||||
|
assertNotEquals(employee.hashCode(), employeeThree.hashCode());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.lombok.equalsandhashcode.inheritance;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
public class EqualsAndHashCodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeAndCallSuperIsTrue_thenTwoObjectsAreEqualAndHaveSameHashCodeIfAllFieldsIncludingParentsFieldsAreSame() {
|
||||||
|
Manager manager = new Manager("Opto", 444, "Nimi", 15, 22);
|
||||||
|
Manager managerTwo = new Manager("Opto", 444, "Nimi", 15, 22);
|
||||||
|
Manager managerThree = new Manager("Opto", 444, "Nimi", 15, 23);
|
||||||
|
|
||||||
|
assertEquals(manager, managerTwo);
|
||||||
|
assertNotEquals(managerTwo, managerThree);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingEqualsAndHashCodeAndCallSuperIsFalse_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfAllParentsFieldsAreNotSame() {
|
||||||
|
ManagerV2 manager = new ManagerV2("Opto", 444, "Nimi", 15, 22);
|
||||||
|
ManagerV2 managerTwo = new ManagerV2("Opto", 444, "Nimi", 15, 22);
|
||||||
|
ManagerV2 managerThree = new ManagerV2("Opto", 444, "Nimi", 14, 21);
|
||||||
|
assertEquals(manager, managerTwo);
|
||||||
|
assertEquals(managerTwo, managerThree);
|
||||||
|
}
|
||||||
|
}
|
@ -40,6 +40,7 @@
|
|||||||
<module>rest-assured</module>
|
<module>rest-assured</module>
|
||||||
<module>rest-testing</module>
|
<module>rest-testing</module>
|
||||||
<module>selenium-junit-testng</module>
|
<module>selenium-junit-testng</module>
|
||||||
|
<module>selenium-webdriver</module>
|
||||||
<module>spring-mockito</module>
|
<module>spring-mockito</module>
|
||||||
<module>spring-testing-2</module>
|
<module>spring-testing-2</module>
|
||||||
<module>spring-testing</module>
|
<module>spring-testing</module>
|
||||||
|
BIN
testing-modules/selenium-webdriver/1688web.png
Normal file
BIN
testing-modules/selenium-webdriver/1688web.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
1
testing-modules/selenium-webdriver/README.md
Normal file
1
testing-modules/selenium-webdriver/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
### Relevant Articles:
|
59
testing-modules/selenium-webdriver/pom.xml
Normal file
59
testing-modules/selenium-webdriver/pom.xml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>selenium-webdriver</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>selenium-webdriver</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>testing-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.seleniumhq.selenium</groupId>
|
||||||
|
<artifactId>selenium-java</artifactId>
|
||||||
|
<version>${selenium-java.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testng</groupId>
|
||||||
|
<artifactId>testng</artifactId>
|
||||||
|
<version>${testng.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.github.bonigarcia</groupId>
|
||||||
|
<artifactId>webdrivermanager</artifactId>
|
||||||
|
<version>${webdrivermanager.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
<resource>
|
||||||
|
<directory>src/test/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<testng.version>6.10</testng.version>
|
||||||
|
<selenium-java.version>4.8.3</selenium-java.version>
|
||||||
|
<webdrivermanager.version>5.3.2</webdrivermanager.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.selenium.webdriver.fileupload;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||||
|
|
||||||
|
public class FileUploadWebDriverUnitTest {
|
||||||
|
|
||||||
|
private WebDriver driver;
|
||||||
|
|
||||||
|
private static final String URL = "http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload";
|
||||||
|
private static final String INPUT_NAME = "file_upload";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
WebDriverManager.firefoxdriver()
|
||||||
|
.setup();
|
||||||
|
driver = new FirefoxDriver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown() {
|
||||||
|
driver.quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFileUploadPage_whenInputFilePath_thenFileUploadEndsWithFilename() {
|
||||||
|
driver.get(URL);
|
||||||
|
|
||||||
|
String filePath = System.getProperty("user.dir") + "/1688web.png";
|
||||||
|
WebElement inputElement = driver.findElement(By.name(INPUT_NAME));
|
||||||
|
WebElement submitButton = driver.findElement(By.name("http_submit"));
|
||||||
|
|
||||||
|
inputElement.sendKeys(filePath);
|
||||||
|
|
||||||
|
String actualFilePath = inputElement.getAttribute("value");
|
||||||
|
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
|
||||||
|
|
||||||
|
submitButton.click();
|
||||||
|
|
||||||
|
Assert.assertTrue(actualFilePath.endsWith(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user