Updated unit .equals() and .hashCode() methods

Formatted code using this formatter:
https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml
Removed argument final declaration on interface
Updated addCustomer logic for cases where the added customer already
exists
This commit is contained in:
Alan 2015-09-14 21:22:33 +01:00
parent 9c43827004
commit 51dca28fe7
5 changed files with 254 additions and 249 deletions

View File

@ -43,23 +43,18 @@ public class Customer {
@Override @Override
public String toString() { public String toString() {
return "Customer{" + return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
"id=" + getId() + + getLastName() + '\'' + '}';
", firstName='" + getFirstName() + '\'' +
", lastName='" + getLastName() + '\'' +
'}';
} }
/**
* Checks if two objects are the same.
*
* @return true if the two objects are Customer objects and have the same id value, false otherwise.
*/
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {
boolean isEqual = false; boolean isEqual = false;
if (this == o) {
isEqual = true;
} else if (o != null && (getClass() == o.getClass())) {
final Customer customer = (Customer) o; final Customer customer = (Customer) o;
if (getId() == customer.getId()) { if (getId() == customer.getId())
isEqual = true; isEqual = true;
} }
return isEqual; return isEqual;
@ -68,8 +63,6 @@ public class Customer {
@Override @Override
public int hashCode() { public int hashCode() {
int result = getId(); int result = getId();
id += getFirstName().hashCode();
id += getLastName().hashCode();
return result; return result;
} }
} }

View File

@ -10,8 +10,12 @@ import java.util.List;
public interface CustomerDao { public interface CustomerDao {
List<Customer> getAllCustomers(); List<Customer> getAllCustomers();
Customer getCustomerById(final int id);
void addCustomer(final Customer customer); Customer getCustomerById(int id);
void updateCustomer(final Customer customer);
void deleteCustomer(final Customer customer); void addCustomer(Customer customer);
void updateCustomer(Customer customer);
void deleteCustomer(Customer customer);
} }

View File

@ -4,11 +4,12 @@ import java.util.List;
/** /**
* *
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism. * The data access object (DAO) is an object that provides an abstract interface to some type of
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database. * database or other persistence mechanism. By mapping application calls to the persistence layer,
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of * DAO provide some specific data operations without exposing details of the database. This
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS, * isolation supports the Single responsibility principle. It separates what data accesses the
* database schema, etc. * application needs, in terms of domain-specific objects and data types (the public interface of
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
* *
*/ */
public class CustomerDaoImpl implements CustomerDao { public class CustomerDaoImpl implements CustomerDao {
@ -40,8 +41,10 @@ public class CustomerDaoImpl implements CustomerDao {
@Override @Override
public void addCustomer(final Customer customer) { public void addCustomer(final Customer customer) {
if (getCustomerById(customer.getId()) == null) {
customers.add(customer); customers.add(customer);
} }
}
@Override @Override

View File

@ -50,7 +50,7 @@ public class CustomerDaoImplTest {
@Test @Test
public void updateNonExistingCustomer() { public void updateNonExistingCustomer() {
final int nonExistingId = 999; final int nonExistingId = getNonExistingCustomerId();
final String newFirstname = "Douglas"; final String newFirstname = "Douglas";
final String newLastname = "MacArthur"; final String newLastname = "MacArthur";
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
@ -68,6 +68,15 @@ public class CustomerDaoImplTest {
assertEquals(2, impl.getAllCustomers().size()); assertEquals(2, impl.getAllCustomers().size());
} }
@Test
public void addAlreadyAddedCustomer() {
final Customer newCustomer = new Customer(3, "George", "Patton");
impl.addCustomer(newCustomer);
assertEquals(2, impl.getAllCustomers().size());
impl.addCustomer(newCustomer);
assertEquals(2, impl.getAllCustomers().size());
}
@Test @Test
public void getExistinCustomerById() { public void getExistinCustomerById() {
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId()));
@ -78,4 +87,13 @@ public class CustomerDaoImplTest {
final int nonExistingId = 999; final int nonExistingId = 999;
assertNull(impl.getCustomerById(nonExistingId)); assertNull(impl.getCustomerById(nonExistingId));
} }
/**
* An arbitrary number which does not correspond to an active Customer id.
*
* @return an int of a customer id which doesn't exist
*/
private int getNonExistingCustomerId() {
return 999;
}
} }

View File

@ -1,8 +1,7 @@
package com.iluwatar.dao; package com.iluwatar.dao;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -20,64 +19,52 @@ public class CustomerTest {
} }
@Test @Test
public void getIndex() { public void getAndSetId() {
assertEquals(ID, customer.getId());
}
@Test
public void getFirstname() {
assertEquals(FIRSTNAME, customer.getFirstName());
}
@Test
public void getLastname() {
assertEquals(LASTNAME, customer.getLastName());
}
@Test
public void setIndex() {
final int newId = 2; final int newId = 2;
customer.setId(newId); customer.setId(newId);
assertEquals(newId, customer.getId()); assertEquals(newId, customer.getId());
} }
@Test @Test
public void setFirstname() { public void getAndSetFirstname() {
final String newFirstname = "Bill"; final String newFirstname = "Bill";
customer.setFirstName(newFirstname); customer.setFirstName(newFirstname);
assertEquals(newFirstname, customer.getFirstName()); assertEquals(newFirstname, customer.getFirstName());
} }
@Test @Test
public void setLastname() { public void getAndSetLastname() {
final String newLastname = "Clinton"; final String newLastname = "Clinton";
customer.setLastName(newLastname); customer.setLastName(newLastname);
assertEquals(newLastname, customer.getLastName()); assertEquals(newLastname, customer.getLastName());
} }
@Test @Test
public void equalsWithDifferentId() { public void notEqualWithDifferentId() {
final int newId = 2; final int newId = 2;
final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME); final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME);
assertFalse(customer.equals(otherCustomer)); assertNotEquals(customer, otherCustomer);
assertNotEquals(customer.hashCode(), otherCustomer.hashCode());
}
@Test
public void equalsWithSameObjectValues() {
final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME);
assertEquals(customer, otherCustomer);
assertEquals(customer.hashCode(), otherCustomer.hashCode());
} }
@Test @Test
public void equalsWithSameObjects() { public void equalsWithSameObjects() {
final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME); assertEquals(customer, customer);
assertTrue(customer.equals(otherCustomer)); assertEquals(customer.hashCode(), customer.hashCode());
}
@Test
public void testHashCode() {
assertTrue(customer.hashCode() > 0);
} }
@Test @Test
public void testToString() { public void testToString() {
final StringBuffer buffer = new StringBuffer(); final StringBuffer buffer = new StringBuffer();
buffer.append("Customer{id="); buffer.append("Customer{id=");
buffer.append(""+customer.getId()); buffer.append("" + customer.getId());
buffer.append(", firstName='"); buffer.append(", firstName='");
buffer.append(customer.getFirstName()); buffer.append(customer.getFirstName());
buffer.append("\', lastName='"); buffer.append("\', lastName='");