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

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

View File

@ -8,10 +8,14 @@ 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,56 +4,59 @@ 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 {
// Represents the DB structure for our example so we don't have to managed it ourselves // Represents the DB structure for our example so we don't have to managed it ourselves
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl. // Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
private List<Customer> customers; private List<Customer> customers;
public CustomerDaoImpl(final List<Customer> customers) { public CustomerDaoImpl(final List<Customer> customers) {
this.customers = customers; this.customers = customers;
}
@Override
public List<Customer> getAllCustomers() {
return customers;
}
@Override
public Customer getCustomerById(final int id) {
Customer customer = null;
for (final Customer cus : getAllCustomers()) {
if (cus.getId() == id) {
customer = cus;
break;
}
} }
return customer;
}
@Override @Override
public List<Customer> getAllCustomers() { public void addCustomer(final Customer customer) {
return customers; if (getCustomerById(customer.getId()) == null) {
customers.add(customer);
} }
}
@Override
public Customer getCustomerById(final int id) { @Override
Customer customer = null; public void updateCustomer(final Customer customer) {
for (final Customer cus : getAllCustomers()) { if (getAllCustomers().contains(customer)) {
if (cus.getId() == id) { final int index = getAllCustomers().indexOf(customer);
customer = cus; getAllCustomers().set(index, customer);
break;
}
}
return customer;
} }
}
@Override @Override
public void addCustomer(final Customer customer) { public void deleteCustomer(final Customer customer) {
customers.add(customer); getAllCustomers().remove(customer);
} }
}
@Override
public void updateCustomer(final Customer customer) {
if (getAllCustomers().contains(customer)) {
final int index = getAllCustomers().indexOf(customer);
getAllCustomers().set(index, customer);
}
}
@Override
public void deleteCustomer(final Customer customer) {
getAllCustomers().remove(customer);
}
}

View File

@ -12,70 +12,88 @@ import org.junit.Test;
public class CustomerDaoImplTest { public class CustomerDaoImplTest {
private CustomerDaoImpl impl; private CustomerDaoImpl impl;
private List<Customer> customers; private List<Customer> customers;
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger"); private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger");
@Before @Before
public void setUp() { public void setUp() {
customers = new ArrayList<Customer>(); customers = new ArrayList<Customer>();
customers.add(CUSTOMER); customers.add(CUSTOMER);
impl = new CustomerDaoImpl(customers); impl = new CustomerDaoImpl(customers);
} }
@Test @Test
public void deleteExistingCustomer() { public void deleteExistingCustomer() {
assertEquals(1, impl.getAllCustomers().size()); assertEquals(1, impl.getAllCustomers().size());
impl.deleteCustomer(CUSTOMER); impl.deleteCustomer(CUSTOMER);
assertTrue(impl.getAllCustomers().isEmpty()); assertTrue(impl.getAllCustomers().isEmpty());
} }
@Test @Test
public void deleteNonExistingCustomer() { public void deleteNonExistingCustomer() {
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
impl.deleteCustomer(nonExistingCustomer); impl.deleteCustomer(nonExistingCustomer);
assertEquals(1, impl.getAllCustomers().size()); assertEquals(1, impl.getAllCustomers().size());
} }
@Test @Test
public void updateExistingCustomer() { public void updateExistingCustomer() {
final String newFirstname = "Bernard"; final String newFirstname = "Bernard";
final String newLastname = "Montgomery"; final String newLastname = "Montgomery";
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
impl.updateCustomer(customer); impl.updateCustomer(customer);
final Customer cust = impl.getCustomerById(CUSTOMER.getId()); final Customer cust = impl.getCustomerById(CUSTOMER.getId());
assertEquals(newFirstname, cust.getFirstName()); assertEquals(newFirstname, cust.getFirstName());
assertEquals(newLastname, cust.getLastName()); assertEquals(newLastname, cust.getLastName());
} }
@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);
impl.updateCustomer(customer); impl.updateCustomer(customer);
assertNull(impl.getCustomerById(nonExistingId)); assertNull(impl.getCustomerById(nonExistingId));
final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId());
assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName());
assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName());
} }
@Test @Test
public void addCustomer() { public void addCustomer() {
final Customer newCustomer = new Customer(3, "George", "Patton"); final Customer newCustomer = new Customer(3, "George", "Patton");
impl.addCustomer(newCustomer); impl.addCustomer(newCustomer);
assertEquals(2, impl.getAllCustomers().size()); assertEquals(2, impl.getAllCustomers().size());
} }
@Test @Test
public void getExistinCustomerById() { public void addAlreadyAddedCustomer() {
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); final Customer newCustomer = new Customer(3, "George", "Patton");
} impl.addCustomer(newCustomer);
assertEquals(2, impl.getAllCustomers().size());
@Test impl.addCustomer(newCustomer);
public void getNonExistinCustomerById() { assertEquals(2, impl.getAllCustomers().size());
final int nonExistingId = 999; }
assertNull(impl.getCustomerById(nonExistingId));
} @Test
public void getExistinCustomerById() {
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId()));
}
@Test
public void getNonExistinCustomerById() {
final int nonExistingId = 999;
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,87 +1,74 @@
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;
public class CustomerTest { public class CustomerTest {
private Customer customer; private Customer customer;
private static final int ID = 1; private static final int ID = 1;
private static final String FIRSTNAME = "Winston"; private static final String FIRSTNAME = "Winston";
private static final String LASTNAME = "Churchill"; private static final String LASTNAME = "Churchill";
@Before @Before
public void setUp() { public void setUp() {
customer = new Customer(ID, FIRSTNAME, LASTNAME); customer = new Customer(ID, FIRSTNAME, LASTNAME);
} }
@Test @Test
public void getIndex() { public void getAndSetId() {
assertEquals(ID, customer.getId()); final int newId = 2;
} customer.setId(newId);
assertEquals(newId, customer.getId());
@Test }
public void getFirstname() {
assertEquals(FIRSTNAME, customer.getFirstName()); @Test
} public void getAndSetFirstname() {
final String newFirstname = "Bill";
@Test customer.setFirstName(newFirstname);
public void getLastname() { assertEquals(newFirstname, customer.getFirstName());
assertEquals(LASTNAME, customer.getLastName()); }
}
@Test
@Test public void getAndSetLastname() {
public void setIndex() { final String newLastname = "Clinton";
final int newId = 2; customer.setLastName(newLastname);
customer.setId(newId); assertEquals(newLastname, customer.getLastName());
assertEquals(newId, customer.getId()); }
}
@Test
@Test public void notEqualWithDifferentId() {
public void setFirstname() { final int newId = 2;
final String newFirstname = "Bill"; final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME);
customer.setFirstName(newFirstname); assertNotEquals(customer, otherCustomer);
assertEquals(newFirstname, customer.getFirstName()); assertNotEquals(customer.hashCode(), otherCustomer.hashCode());
} }
@Test @Test
public void setLastname() { public void equalsWithSameObjectValues() {
final String newLastname = "Clinton"; final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME);
customer.setLastName(newLastname); assertEquals(customer, otherCustomer);
assertEquals(newLastname, customer.getLastName()); assertEquals(customer.hashCode(), otherCustomer.hashCode());
} }
@Test @Test
public void equalsWithDifferentId() { public void equalsWithSameObjects() {
final int newId = 2; assertEquals(customer, customer);
final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME); assertEquals(customer.hashCode(), customer.hashCode());
assertFalse(customer.equals(otherCustomer)); }
}
@Test
@Test public void testToString() {
public void equalsWithSameObjects() { final StringBuffer buffer = new StringBuffer();
final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME); buffer.append("Customer{id=");
assertTrue(customer.equals(otherCustomer)); buffer.append("" + customer.getId());
} buffer.append(", firstName='");
buffer.append(customer.getFirstName());
@Test buffer.append("\', lastName='");
public void testHashCode() { buffer.append(customer.getLastName() + "\'}");
assertTrue(customer.hashCode() > 0); assertEquals(buffer.toString(), customer.toString());
} }
@Test
public void testToString() {
final StringBuffer buffer = new StringBuffer();
buffer.append("Customer{id=");
buffer.append(""+customer.getId());
buffer.append(", firstName='");
buffer.append(customer.getFirstName());
buffer.append("\', lastName='");
buffer.append(customer.getLastName() + "\'}");
assertEquals(buffer.toString(), customer.toString());
}
} }