Inclusion of log4j dependency rather than relying on

System.out.println() statements.
Added unit tests for doa module.
This commit is contained in:
Alan 2015-09-05 21:54:14 +01:00
parent 5956873fc9
commit 3da48d970c
10 changed files with 282 additions and 96 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" <project
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"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
@ -8,11 +9,20 @@
<version>1.6.0</version> <version>1.6.0</version>
</parent> </parent>
<artifactId>dao</artifactId> <artifactId>dao</artifactId>
<properties>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -3,6 +3,8 @@ package com.iluwatar.dao;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 * 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 { public class App {
private static Logger LOGGER = Logger.getLogger(App.class);
/** /**
* Program entry point * Program entry point.
* @param args command line args *
* @param args command line args.
*/ */
public static void main(String[] args) { public static void main(final String[] args) {
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); final Customer customer = new Customer(4, "Dan", "Danson");
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
Customer customer = new Customer(4, "Dan", "Danson");
customerDao.addCustomer(customer); customerDao.addCustomer(customer);
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
customer.setFirstName("Daniel"); customer.setFirstName("Daniel");
customer.setLastName("Danielson"); customer.setLastName("Danielson");
customerDao.updateCustomer(customer); customerDao.updateCustomer(customer);
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
customerDao.deleteCustomer(customer); customerDao.deleteCustomer(customer);
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
} }
/** /**
* Generate customers * Generate customers.
* @return list of customers *
* @return list of customers.
*/ */
public static List<Customer> generateSampleCustomers() { public static List<Customer> generateSampleCustomers() {
Customer customer1 = new Customer(1, "Adam", "Adamson"); final Customer customer1 = new Customer(1, "Adam", "Adamson");
Customer customer2 = new Customer(2, "Bob", "Bobson"); final Customer customer2 = new Customer(2, "Bob", "Bobson");
Customer customer3 = new Customer(3, "Carl", "Carlson"); final Customer customer3 = new Customer(3, "Carl", "Carlson");
final List<Customer> customers = new ArrayList<Customer>();
List<Customer> customers = new ArrayList<Customer>();
customers.add(customer1); customers.add(customer1);
customers.add(customer2); customers.add(customer2);
customers.add(customer3); customers.add(customer3);

View File

@ -11,7 +11,7 @@ public class Customer {
private String firstName; private String firstName;
private String lastName; 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.id = id;
this.firstName = firstName; this.firstName = firstName;
this.lastName = lastName; this.lastName = lastName;
@ -21,7 +21,7 @@ public class Customer {
return id; return id;
} }
public void setId(int id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
@ -29,7 +29,7 @@ public class Customer {
return firstName; return firstName;
} }
public void setFirstName(String firstName) { public void setFirstName(final String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
@ -37,34 +37,39 @@ public class Customer {
return lastName; return lastName;
} }
public void setLastName(String lastName) { public void setLastName(final String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }
@Override @Override
public String toString() { public String toString() {
return "Customer{" + return "Customer{" +
"id=" + id + "id=" + getId() +
", firstName='" + firstName + '\'' + ", firstName='" + getFirstName() + '\'' +
", lastName='" + lastName + '\'' + ", 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(Object o) { public boolean equals(final Object o) {
if (this == o) return true; boolean isEqual = false;
if (o == null || getClass() != o.getClass()) return false; final Customer customer = (Customer) o;
if (getId() == customer.getId()) {
Customer customer = (Customer) o; isEqual = true;
}
if (id != customer.id) return false; return isEqual;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = id; int result = getId();
id += getFirstName().hashCode();
id += getLastName().hashCode();
return result; return result;
} }
} }

View File

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

View File

@ -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. // 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(List<Customer> customers) { public CustomerDaoImpl(final List<Customer> customers) {
this.customers = customers; this.customers = customers;
} }
@ -27,31 +27,33 @@ public class CustomerDaoImpl implements CustomerDao {
} }
@Override @Override
public Customer getCusterById(int id) { public Customer getCustomerById(final int id) {
for (int i = 0; i < customers.size(); i++) { Customer customer = null;
if (customers.get(i).getId() == id) { for (final Customer cus : getAllCustomers()) {
return customers.get(i); if (cus.getId() == id) {
customer = cus;
break;
} }
} }
// No customer found return customer;
return null;
} }
@Override @Override
public void addCustomer(Customer customer) { public void addCustomer(final Customer customer) {
customers.add(customer); customers.add(customer);
} }
@Override @Override
public void updateCustomer(Customer customer) { public void updateCustomer(final Customer customer) {
if (customers.contains(customer)) { if (getAllCustomers().contains(customer)) {
customers.set(customers.indexOf(customer), customer); final int index = getAllCustomers().indexOf(customer);
getAllCustomers().set(index, customer);
} }
} }
@Override @Override
public void deleteCustomer(Customer customer) { public void deleteCustomer(final Customer customer) {
customers.remove(customer); getAllCustomers().remove(customer);
} }
} }

View File

@ -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);
}
}

View File

@ -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<Customer> customers;
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger");
@Before
public void setUp() {
customers = new ArrayList<Customer>();
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));
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@ -18,6 +18,7 @@
<jacoco.version>0.7.2.201409121644</jacoco.version> <jacoco.version>0.7.2.201409121644</jacoco.version>
<commons-dbcp.version>1.4</commons-dbcp.version> <commons-dbcp.version>1.4</commons-dbcp.version>
<camel.version>2.15.3</camel.version> <camel.version>2.15.3</camel.version>
<log4j.version>1.2.17</log4j.version>
</properties> </properties>
<modules> <modules>
<module>abstract-factory</module> <module>abstract-factory</module>
@ -122,6 +123,11 @@
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>