From 3da48d970c40552aabd662bf0d1ec29ca5d7b302 Mon Sep 17 00:00:00 2001 From: Alan Date: Sat, 5 Sep 2015 21:54:14 +0100 Subject: [PATCH 1/4] Inclusion of log4j dependency rather than relying on System.out.println() statements. Added unit tests for doa module. --- dao/pom.xml | 42 +++++---- dao/src/main/java/com/iluwatar/dao/App.java | 47 +++++----- .../main/java/com/iluwatar/dao/Customer.java | 41 +++++---- .../java/com/iluwatar/dao/CustomerDao.java | 10 +-- .../com/iluwatar/dao/CustomerDaoImpl.java | 28 +++--- .../test/java/com/iluwatar/dao/AppTest.java | 19 ---- .../com/iluwatar/dao/CustomerDaoImplTest.java | 81 +++++++++++++++++ .../java/com/iluwatar/dao/CustomerTest.java | 87 +++++++++++++++++++ dao/src/test/resources/log4j.xml | 17 ++++ pom.xml | 6 ++ 10 files changed, 282 insertions(+), 96 deletions(-) delete mode 100644 dao/src/test/java/com/iluwatar/dao/AppTest.java create mode 100644 dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java create mode 100644 dao/src/test/java/com/iluwatar/dao/CustomerTest.java create mode 100644 dao/src/test/resources/log4j.xml diff --git a/dao/pom.xml b/dao/pom.xml index a7a5c74ca..b55a8b1f3 100644 --- a/dao/pom.xml +++ b/dao/pom.xml @@ -1,18 +1,28 @@ - - 4.0.0 - - com.iluwatar - java-design-patterns - 1.6.0 - - dao - - - junit - junit - test - - + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.6.0 + + dao + + + + + + + + junit + junit + test + + + log4j + log4j + + diff --git a/dao/src/main/java/com/iluwatar/dao/App.java b/dao/src/main/java/com/iluwatar/dao/App.java index ac6794973..d3ee667ce 100644 --- a/dao/src/main/java/com/iluwatar/dao/App.java +++ b/dao/src/main/java/com/iluwatar/dao/App.java @@ -3,6 +3,8 @@ package com.iluwatar.dao; import java.util.ArrayList; import java.util.List; +import org.apache.log4j.Logger; + /** * * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly @@ -11,43 +13,38 @@ import java.util.List; */ public class App { + private static Logger LOGGER = Logger.getLogger(App.class); + /** - * Program entry point - * @param args command line args + * Program entry point. + * + * @param args command line args. */ - public static void main(String[] args) { - - CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); - - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2)); - - Customer customer = new Customer(4, "Dan", "Danson"); + public static void main(final String[] args) { + final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2)); + final Customer customer = new Customer(4, "Dan", "Danson"); customerDao.addCustomer(customer); - - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); customer.setFirstName("Daniel"); customer.setLastName("Danielson"); customerDao.updateCustomer(customer); - - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); customerDao.deleteCustomer(customer); - - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); } /** - * Generate customers - * @return list of customers + * Generate customers. + * + * @return list of customers. */ public static List generateSampleCustomers() { - Customer customer1 = new Customer(1, "Adam", "Adamson"); - Customer customer2 = new Customer(2, "Bob", "Bobson"); - Customer customer3 = new Customer(3, "Carl", "Carlson"); - - List customers = new ArrayList(); + final Customer customer1 = new Customer(1, "Adam", "Adamson"); + final Customer customer2 = new Customer(2, "Bob", "Bobson"); + final Customer customer3 = new Customer(3, "Carl", "Carlson"); + final List customers = new ArrayList(); customers.add(customer1); customers.add(customer2); customers.add(customer3); diff --git a/dao/src/main/java/com/iluwatar/dao/Customer.java b/dao/src/main/java/com/iluwatar/dao/Customer.java index 8cfdd8034..652eda7b2 100644 --- a/dao/src/main/java/com/iluwatar/dao/Customer.java +++ b/dao/src/main/java/com/iluwatar/dao/Customer.java @@ -11,7 +11,7 @@ public class Customer { private String firstName; private String lastName; - public Customer(int id, String firstName, String lastName) { + public Customer(final int id, final String firstName, final String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; @@ -21,7 +21,7 @@ public class Customer { return id; } - public void setId(int id) { + public void setId(final int id) { this.id = id; } @@ -29,7 +29,7 @@ public class Customer { return firstName; } - public void setFirstName(String firstName) { + public void setFirstName(final String firstName) { this.firstName = firstName; } @@ -37,34 +37,39 @@ public class Customer { return lastName; } - public void setLastName(String lastName) { + public void setLastName(final String lastName) { this.lastName = lastName; } @Override public String toString() { return "Customer{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + + "id=" + getId() + + ", 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 - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Customer customer = (Customer) o; - - if (id != customer.id) return false; - - return true; + public boolean equals(final Object o) { + boolean isEqual = false; + final Customer customer = (Customer) o; + if (getId() == customer.getId()) { + isEqual = true; + } + return isEqual; } - + @Override public int hashCode() { - int result = id; + int result = getId(); + id += getFirstName().hashCode(); + id += getLastName().hashCode(); return result; } } \ No newline at end of file diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java index 2a50d2bfb..54f388dde 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java @@ -9,9 +9,9 @@ import java.util.List; */ public interface CustomerDao { - public List getAllCustomers(); - public Customer getCusterById(int id); - public void addCustomer(Customer customer); - public void updateCustomer(Customer customer); - public void deleteCustomer(Customer customer); + List getAllCustomers(); + Customer getCustomerById(final int id); + void addCustomer(final Customer customer); + void updateCustomer(final Customer customer); + void deleteCustomer(final Customer customer); } \ No newline at end of file diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java b/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java index 4403b57ed..e5c431452 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java @@ -17,7 +17,7 @@ public class CustomerDaoImpl implements CustomerDao { // Note: Normally this would be in the form of an actual database and not part of the Dao Impl. private List customers; - public CustomerDaoImpl(List customers) { + public CustomerDaoImpl(final List customers) { this.customers = customers; } @@ -27,31 +27,33 @@ public class CustomerDaoImpl implements CustomerDao { } @Override - public Customer getCusterById(int id) { - for (int i = 0; i < customers.size(); i++) { - if (customers.get(i).getId() == id) { - return customers.get(i); + public Customer getCustomerById(final int id) { + Customer customer = null; + for (final Customer cus : getAllCustomers()) { + if (cus.getId() == id) { + customer = cus; + break; } } - // No customer found - return null; + return customer; } @Override - public void addCustomer(Customer customer) { + public void addCustomer(final Customer customer) { customers.add(customer); } @Override - public void updateCustomer(Customer customer) { - if (customers.contains(customer)) { - customers.set(customers.indexOf(customer), customer); + public void updateCustomer(final Customer customer) { + if (getAllCustomers().contains(customer)) { + final int index = getAllCustomers().indexOf(customer); + getAllCustomers().set(index, customer); } } @Override - public void deleteCustomer(Customer customer) { - customers.remove(customer); + public void deleteCustomer(final Customer customer) { + getAllCustomers().remove(customer); } } \ No newline at end of file diff --git a/dao/src/test/java/com/iluwatar/dao/AppTest.java b/dao/src/test/java/com/iluwatar/dao/AppTest.java deleted file mode 100644 index 5eb1ebb11..000000000 --- a/dao/src/test/java/com/iluwatar/dao/AppTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.iluwatar.dao; - -import org.junit.Test; - -import com.iluwatar.dao.App; - -/** - * - * Application test - * - */ -public class AppTest { - - @Test - public void test() { - String[] args = {}; - App.main(args); - } -} diff --git a/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java new file mode 100644 index 000000000..ae87be156 --- /dev/null +++ b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java @@ -0,0 +1,81 @@ +package com.iluwatar.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class CustomerDaoImplTest { + + private CustomerDaoImpl impl; + private List customers; + private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger"); + + @Before + public void setUp() { + customers = new ArrayList(); + customers.add(CUSTOMER); + impl = new CustomerDaoImpl(customers); + } + + @Test + public void deleteExistingCustomer() { + assertEquals(1, impl.getAllCustomers().size()); + impl.deleteCustomer(CUSTOMER); + assertTrue(impl.getAllCustomers().isEmpty()); + } + + @Test + public void deleteNonExistingCustomer() { + final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); + impl.deleteCustomer(nonExistingCustomer); + assertEquals(1, impl.getAllCustomers().size()); + } + + @Test + public void updateExistingCustomer() { + final String newFirstname = "Bernard"; + final String newLastname = "Montgomery"; + final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); + impl.updateCustomer(customer); + final Customer cust = impl.getCustomerById(CUSTOMER.getId()); + assertEquals(newFirstname, cust.getFirstName()); + assertEquals(newLastname, cust.getLastName()); + } + + @Test + public void updateNonExistingCustomer() { + final int nonExistingId = 999; + final String newFirstname = "Douglas"; + final String newLastname = "MacArthur"; + final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); + impl.updateCustomer(customer); + assertNull(impl.getCustomerById(nonExistingId)); + final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); + assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); + assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); + } + + @Test + public void addCustomer() { + final Customer newCustomer = new Customer(3, "George", "Patton"); + impl.addCustomer(newCustomer); + assertEquals(2, impl.getAllCustomers().size()); + } + + @Test + public void getExistinCustomerById() { + assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); + } + + @Test + public void getNonExistinCustomerById() { + final int nonExistingId = 999; + assertNull(impl.getCustomerById(nonExistingId)); + } +} diff --git a/dao/src/test/java/com/iluwatar/dao/CustomerTest.java b/dao/src/test/java/com/iluwatar/dao/CustomerTest.java new file mode 100644 index 000000000..cd90a6643 --- /dev/null +++ b/dao/src/test/java/com/iluwatar/dao/CustomerTest.java @@ -0,0 +1,87 @@ +package com.iluwatar.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class CustomerTest { + + private Customer customer; + private static final int ID = 1; + private static final String FIRSTNAME = "Winston"; + private static final String LASTNAME = "Churchill"; + + @Before + public void setUp() { + customer = new Customer(ID, FIRSTNAME, LASTNAME); + } + + @Test + public void getIndex() { + 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; + customer.setId(newId); + assertEquals(newId, customer.getId()); + } + + @Test + public void setFirstname() { + final String newFirstname = "Bill"; + customer.setFirstName(newFirstname); + assertEquals(newFirstname, customer.getFirstName()); + } + + @Test + public void setLastname() { + final String newLastname = "Clinton"; + customer.setLastName(newLastname); + assertEquals(newLastname, customer.getLastName()); + } + + @Test + public void equalsWithDifferentId() { + final int newId = 2; + final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME); + assertFalse(customer.equals(otherCustomer)); + } + + @Test + public void equalsWithSameObjects() { + final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME); + assertTrue(customer.equals(otherCustomer)); + } + + @Test + public void testHashCode() { + assertTrue(customer.hashCode() > 0); + } + + @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()); + } +} diff --git a/dao/src/test/resources/log4j.xml b/dao/src/test/resources/log4j.xml new file mode 100644 index 000000000..136817f50 --- /dev/null +++ b/dao/src/test/resources/log4j.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7b0d80bd8..a4cabcd13 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 0.7.2.201409121644 1.4 2.15.3 + 1.2.17 abstract-factory @@ -122,6 +123,11 @@ ${junit.version} test + + log4j + log4j + ${log4j.version} + From 9c4382700455fb11aaf9893d1d4b870bf60e7522 Mon Sep 17 00:00:00 2001 From: Alan Date: Sat, 5 Sep 2015 22:00:27 +0100 Subject: [PATCH 2/4] Removed erroneous semi-colon. --- .../src/test/java/com/iluwatar/abstractfactory/AppTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java index 78cdf8a44..4d3659245 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java @@ -7,7 +7,7 @@ import org.junit.Test; public class AppTest { - private App app = new App();; + private App app = new App(); private KingdomFactory elfFactory; private KingdomFactory orcFactory; From 51dca28fe71681540142d01db601e18a662de651 Mon Sep 17 00:00:00 2001 From: Alan Date: Mon, 14 Sep 2015 21:22:33 +0100 Subject: [PATCH 3/4] 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 --- .../main/java/com/iluwatar/dao/Customer.java | 107 ++++++------- .../java/com/iluwatar/dao/CustomerDao.java | 18 ++- .../com/iluwatar/dao/CustomerDaoImpl.java | 87 +++++----- .../com/iluwatar/dao/CustomerDaoImplTest.java | 150 ++++++++++-------- .../java/com/iluwatar/dao/CustomerTest.java | 141 ++++++++-------- 5 files changed, 254 insertions(+), 249 deletions(-) diff --git a/dao/src/main/java/com/iluwatar/dao/Customer.java b/dao/src/main/java/com/iluwatar/dao/Customer.java index 652eda7b2..e6d7f7763 100644 --- a/dao/src/main/java/com/iluwatar/dao/Customer.java +++ b/dao/src/main/java/com/iluwatar/dao/Customer.java @@ -6,70 +6,63 @@ package com.iluwatar.dao; * */ public class Customer { - - private int id; - private String firstName; - private String lastName; - public Customer(final int id, final String firstName, final String lastName) { - this.id = id; - this.firstName = firstName; - this.lastName = lastName; - } + private int id; + private String firstName; + private String lastName; - public int getId() { - return id; - } + public Customer(final int id, final String firstName, final String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } - public void setId(final int id) { - this.id = id; - } + public int getId() { + return id; + } - public String getFirstName() { - return firstName; - } + public void setId(final int id) { + this.id = id; + } - public void setFirstName(final String firstName) { - this.firstName = firstName; - } + public String getFirstName() { + return firstName; + } - public String getLastName() { - return lastName; - } + public void setFirstName(final String firstName) { + this.firstName = firstName; + } - public void setLastName(final String lastName) { - this.lastName = lastName; - } + public String getLastName() { + return lastName; + } - @Override - public String toString() { - return "Customer{" + - "id=" + getId() + - ", firstName='" + getFirstName() + '\'' + - ", lastName='" + getLastName() + '\'' + - '}'; - } + public void setLastName(final String lastName) { + this.lastName = lastName; + } - /** - * 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 - public boolean equals(final Object o) { - boolean isEqual = false; - final Customer customer = (Customer) o; - if (getId() == customer.getId()) { - isEqual = true; - } - return isEqual; + @Override + public String toString() { + return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='" + + getLastName() + '\'' + '}'; + } + + @Override + public boolean equals(final Object o) { + boolean isEqual = false; + if (this == o) { + isEqual = true; + } else if (o != null && (getClass() == o.getClass())) { + final Customer customer = (Customer) o; + if (getId() == customer.getId()) + isEqual = true; } - - @Override - public int hashCode() { - int result = getId(); - id += getFirstName().hashCode(); - id += getLastName().hashCode(); - return result; - } -} \ No newline at end of file + return isEqual; + } + + @Override + public int hashCode() { + int result = getId(); + return result; + } +} diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java index 54f388dde..79d23ba20 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerDao.java @@ -8,10 +8,14 @@ import java.util.List; * */ public interface CustomerDao { - - List getAllCustomers(); - Customer getCustomerById(final int id); - void addCustomer(final Customer customer); - void updateCustomer(final Customer customer); - void deleteCustomer(final Customer customer); -} \ No newline at end of file + + List getAllCustomers(); + + Customer getCustomerById(int id); + + void addCustomer(Customer customer); + + void updateCustomer(Customer customer); + + void deleteCustomer(Customer customer); +} diff --git a/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java b/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java index e5c431452..e590fb1a6 100644 --- a/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java +++ b/dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java @@ -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. - * By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database. - * This isolation supports the Single responsibility principle. It separates what data accesses the 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. + * The data access object (DAO) is an object that provides an abstract interface to some type of + * database or other persistence mechanism. By mapping application calls to the persistence layer, + * DAO provide some specific data operations without exposing details of the database. This + * isolation supports the Single responsibility principle. It separates what data accesses the + * 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 { - // 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. - private List customers; + // 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. + private List customers; - public CustomerDaoImpl(final List customers) { - this.customers = customers; + public CustomerDaoImpl(final List customers) { + this.customers = customers; + } + + @Override + public List 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 - public List getAllCustomers() { - return customers; + @Override + public void addCustomer(final Customer customer) { + if (getCustomerById(customer.getId()) == null) { + customers.add(customer); } + } - @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 + public void updateCustomer(final Customer customer) { + if (getAllCustomers().contains(customer)) { + final int index = getAllCustomers().indexOf(customer); + getAllCustomers().set(index, customer); } + } - @Override - public void addCustomer(final Customer customer) { - customers.add(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); - } -} \ No newline at end of file + @Override + public void deleteCustomer(final Customer customer) { + getAllCustomers().remove(customer); + } +} diff --git a/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java index ae87be156..e5e5eb220 100644 --- a/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java +++ b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java @@ -12,70 +12,88 @@ import org.junit.Test; public class CustomerDaoImplTest { - private CustomerDaoImpl impl; - private List customers; - private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger"); - - @Before - public void setUp() { - customers = new ArrayList(); - customers.add(CUSTOMER); - impl = new CustomerDaoImpl(customers); - } - - @Test - public void deleteExistingCustomer() { - assertEquals(1, impl.getAllCustomers().size()); - impl.deleteCustomer(CUSTOMER); - assertTrue(impl.getAllCustomers().isEmpty()); - } - - @Test - public void deleteNonExistingCustomer() { - final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); - impl.deleteCustomer(nonExistingCustomer); - assertEquals(1, impl.getAllCustomers().size()); - } - - @Test - public void updateExistingCustomer() { - final String newFirstname = "Bernard"; - final String newLastname = "Montgomery"; - final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); - impl.updateCustomer(customer); - final Customer cust = impl.getCustomerById(CUSTOMER.getId()); - assertEquals(newFirstname, cust.getFirstName()); - assertEquals(newLastname, cust.getLastName()); - } - - @Test - public void updateNonExistingCustomer() { - final int nonExistingId = 999; - final String newFirstname = "Douglas"; - final String newLastname = "MacArthur"; - final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); - impl.updateCustomer(customer); - assertNull(impl.getCustomerById(nonExistingId)); - final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); - assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); - assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); - } - - @Test - public void addCustomer() { - final Customer newCustomer = new Customer(3, "George", "Patton"); - impl.addCustomer(newCustomer); - assertEquals(2, impl.getAllCustomers().size()); - } - - @Test - public void getExistinCustomerById() { - assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId())); - } - - @Test - public void getNonExistinCustomerById() { - final int nonExistingId = 999; - assertNull(impl.getCustomerById(nonExistingId)); - } + private CustomerDaoImpl impl; + private List customers; + private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger"); + + @Before + public void setUp() { + customers = new ArrayList(); + customers.add(CUSTOMER); + impl = new CustomerDaoImpl(customers); + } + + @Test + public void deleteExistingCustomer() { + assertEquals(1, impl.getAllCustomers().size()); + impl.deleteCustomer(CUSTOMER); + assertTrue(impl.getAllCustomers().isEmpty()); + } + + @Test + public void deleteNonExistingCustomer() { + final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund"); + impl.deleteCustomer(nonExistingCustomer); + assertEquals(1, impl.getAllCustomers().size()); + } + + @Test + public void updateExistingCustomer() { + final String newFirstname = "Bernard"; + final String newLastname = "Montgomery"; + final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname); + impl.updateCustomer(customer); + final Customer cust = impl.getCustomerById(CUSTOMER.getId()); + assertEquals(newFirstname, cust.getFirstName()); + assertEquals(newLastname, cust.getLastName()); + } + + @Test + public void updateNonExistingCustomer() { + final int nonExistingId = getNonExistingCustomerId(); + final String newFirstname = "Douglas"; + final String newLastname = "MacArthur"; + final Customer customer = new Customer(nonExistingId, newFirstname, newLastname); + impl.updateCustomer(customer); + assertNull(impl.getCustomerById(nonExistingId)); + final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId()); + assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName()); + assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName()); + } + + @Test + public void addCustomer() { + final Customer newCustomer = new Customer(3, "George", "Patton"); + impl.addCustomer(newCustomer); + 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 + 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; + } } diff --git a/dao/src/test/java/com/iluwatar/dao/CustomerTest.java b/dao/src/test/java/com/iluwatar/dao/CustomerTest.java index cd90a6643..8511a577b 100644 --- a/dao/src/test/java/com/iluwatar/dao/CustomerTest.java +++ b/dao/src/test/java/com/iluwatar/dao/CustomerTest.java @@ -1,87 +1,74 @@ package com.iluwatar.dao; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotEquals; import org.junit.Before; import org.junit.Test; public class CustomerTest { - private Customer customer; - private static final int ID = 1; - private static final String FIRSTNAME = "Winston"; - private static final String LASTNAME = "Churchill"; - - @Before - public void setUp() { - customer = new Customer(ID, FIRSTNAME, LASTNAME); - } - - @Test - public void getIndex() { - 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; - customer.setId(newId); - assertEquals(newId, customer.getId()); - } - - @Test - public void setFirstname() { - final String newFirstname = "Bill"; - customer.setFirstName(newFirstname); - assertEquals(newFirstname, customer.getFirstName()); - } - - @Test - public void setLastname() { - final String newLastname = "Clinton"; - customer.setLastName(newLastname); - assertEquals(newLastname, customer.getLastName()); - } - - @Test - public void equalsWithDifferentId() { - final int newId = 2; - final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME); - assertFalse(customer.equals(otherCustomer)); - } - - @Test - public void equalsWithSameObjects() { - final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME); - assertTrue(customer.equals(otherCustomer)); - } - - @Test - public void testHashCode() { - assertTrue(customer.hashCode() > 0); - } - - @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()); - } + private Customer customer; + private static final int ID = 1; + private static final String FIRSTNAME = "Winston"; + private static final String LASTNAME = "Churchill"; + + @Before + public void setUp() { + customer = new Customer(ID, FIRSTNAME, LASTNAME); + } + + @Test + public void getAndSetId() { + final int newId = 2; + customer.setId(newId); + assertEquals(newId, customer.getId()); + } + + @Test + public void getAndSetFirstname() { + final String newFirstname = "Bill"; + customer.setFirstName(newFirstname); + assertEquals(newFirstname, customer.getFirstName()); + } + + @Test + public void getAndSetLastname() { + final String newLastname = "Clinton"; + customer.setLastName(newLastname); + assertEquals(newLastname, customer.getLastName()); + } + + @Test + public void notEqualWithDifferentId() { + final int newId = 2; + final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME); + 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 + public void equalsWithSameObjects() { + assertEquals(customer, customer); + assertEquals(customer.hashCode(), customer.hashCode()); + } + + @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()); + } } From d0af12b1ee59e261b4368d28c101169a2305e943 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 16 Sep 2015 21:32:52 +0100 Subject: [PATCH 4/4] Removed magic number. --- dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java index e5e5eb220..230314d80 100644 --- a/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java +++ b/dao/src/test/java/com/iluwatar/dao/CustomerDaoImplTest.java @@ -84,7 +84,7 @@ public class CustomerDaoImplTest { @Test public void getNonExistinCustomerById() { - final int nonExistingId = 999; + final int nonExistingId = getNonExistingCustomerId(); assertNull(impl.getCustomerById(nonExistingId)); }