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,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>