Skip to content

Commit 3da48d9

Browse files
AlanAlan
authored andcommitted
Inclusion of log4j dependency rather than relying on
System.out.println() statements. Added unit tests for doa module.
1 parent 5956873 commit 3da48d9

File tree

10 files changed

+282
-96
lines changed

10 files changed

+282
-96
lines changed

dao/pom.xml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?xml version="1.0"?>
2-
<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"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>com.iluwatar</groupId>
7-
<artifactId>java-design-patterns</artifactId>
8-
<version>1.6.0</version>
9-
</parent>
10-
<artifactId>dao</artifactId>
11-
<dependencies>
12-
<dependency>
13-
<groupId>junit</groupId>
14-
<artifactId>junit</artifactId>
15-
<scope>test</scope>
16-
</dependency>
17-
</dependencies>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.iluwatar</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.6.0</version>
10+
</parent>
11+
<artifactId>dao</artifactId>
12+
13+
<properties>
14+
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>log4j</groupId>
25+
<artifactId>log4j</artifactId>
26+
</dependency>
27+
</dependencies>
1828
</project>

dao/src/main/java/com/iluwatar/dao/App.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.apache.log4j.Logger;
7+
68
/**
79
*
810
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
@@ -11,43 +13,38 @@
1113
*/
1214
public class App {
1315

16+
private static Logger LOGGER = Logger.getLogger(App.class);
17+
1418
/**
15-
* Program entry point
16-
* @param args command line args
19+
* Program entry point.
20+
*
21+
* @param args command line args.
1722
*/
18-
public static void main(String[] args) {
19-
20-
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
21-
22-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
23-
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
24-
25-
Customer customer = new Customer(4, "Dan", "Danson");
23+
public static void main(final String[] args) {
24+
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
25+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
26+
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
27+
final Customer customer = new Customer(4, "Dan", "Danson");
2628
customerDao.addCustomer(customer);
27-
28-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
29-
29+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
3030
customer.setFirstName("Daniel");
3131
customer.setLastName("Danielson");
3232
customerDao.updateCustomer(customer);
33-
34-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
35-
33+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
3634
customerDao.deleteCustomer(customer);
37-
38-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
35+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
3936
}
4037

4138
/**
42-
* Generate customers
43-
* @return list of customers
39+
* Generate customers.
40+
*
41+
* @return list of customers.
4442
*/
4543
public static List<Customer> generateSampleCustomers() {
46-
Customer customer1 = new Customer(1, "Adam", "Adamson");
47-
Customer customer2 = new Customer(2, "Bob", "Bobson");
48-
Customer customer3 = new Customer(3, "Carl", "Carlson");
49-
50-
List<Customer> customers = new ArrayList<Customer>();
44+
final Customer customer1 = new Customer(1, "Adam", "Adamson");
45+
final Customer customer2 = new Customer(2, "Bob", "Bobson");
46+
final Customer customer3 = new Customer(3, "Carl", "Carlson");
47+
final List<Customer> customers = new ArrayList<Customer>();
5148
customers.add(customer1);
5249
customers.add(customer2);
5350
customers.add(customer3);

dao/src/main/java/com/iluwatar/dao/Customer.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Customer {
1111
private String firstName;
1212
private String lastName;
1313

14-
public Customer(int id, String firstName, String lastName) {
14+
public Customer(final int id, final String firstName, final String lastName) {
1515
this.id = id;
1616
this.firstName = firstName;
1717
this.lastName = lastName;
@@ -21,50 +21,55 @@ public int getId() {
2121
return id;
2222
}
2323

24-
public void setId(int id) {
24+
public void setId(final int id) {
2525
this.id = id;
2626
}
2727

2828
public String getFirstName() {
2929
return firstName;
3030
}
3131

32-
public void setFirstName(String firstName) {
32+
public void setFirstName(final String firstName) {
3333
this.firstName = firstName;
3434
}
3535

3636
public String getLastName() {
3737
return lastName;
3838
}
3939

40-
public void setLastName(String lastName) {
40+
public void setLastName(final String lastName) {
4141
this.lastName = lastName;
4242
}
4343

4444
@Override
4545
public String toString() {
4646
return "Customer{" +
47-
"id=" + id +
48-
", firstName='" + firstName + '\'' +
49-
", lastName='" + lastName + '\'' +
47+
"id=" + getId() +
48+
", firstName='" + getFirstName() + '\'' +
49+
", lastName='" + getLastName() + '\'' +
5050
'}';
5151
}
5252

53+
/**
54+
* Checks if two objects are the same.
55+
*
56+
* @return true if the two objects are Customer objects and have the same id value, false otherwise.
57+
*/
5358
@Override
54-
public boolean equals(Object o) {
55-
if (this == o) return true;
56-
if (o == null || getClass() != o.getClass()) return false;
57-
58-
Customer customer = (Customer) o;
59-
60-
if (id != customer.id) return false;
61-
62-
return true;
59+
public boolean equals(final Object o) {
60+
boolean isEqual = false;
61+
final Customer customer = (Customer) o;
62+
if (getId() == customer.getId()) {
63+
isEqual = true;
64+
}
65+
return isEqual;
6366
}
64-
67+
6568
@Override
6669
public int hashCode() {
67-
int result = id;
70+
int result = getId();
71+
id += getFirstName().hashCode();
72+
id += getLastName().hashCode();
6873
return result;
6974
}
7075
}

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
*/
1010
public interface CustomerDao {
1111

12-
public List<Customer> getAllCustomers();
13-
public Customer getCusterById(int id);
14-
public void addCustomer(Customer customer);
15-
public void updateCustomer(Customer customer);
16-
public void deleteCustomer(Customer customer);
12+
List<Customer> getAllCustomers();
13+
Customer getCustomerById(final int id);
14+
void addCustomer(final Customer customer);
15+
void updateCustomer(final Customer customer);
16+
void deleteCustomer(final Customer customer);
1717
}

dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CustomerDaoImpl implements CustomerDao {
1717
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
1818
private List<Customer> customers;
1919

20-
public CustomerDaoImpl(List<Customer> customers) {
20+
public CustomerDaoImpl(final List<Customer> customers) {
2121
this.customers = customers;
2222
}
2323

@@ -27,31 +27,33 @@ public List<Customer> getAllCustomers() {
2727
}
2828

2929
@Override
30-
public Customer getCusterById(int id) {
31-
for (int i = 0; i < customers.size(); i++) {
32-
if (customers.get(i).getId() == id) {
33-
return customers.get(i);
30+
public Customer getCustomerById(final int id) {
31+
Customer customer = null;
32+
for (final Customer cus : getAllCustomers()) {
33+
if (cus.getId() == id) {
34+
customer = cus;
35+
break;
3436
}
3537
}
36-
// No customer found
37-
return null;
38+
return customer;
3839
}
3940

4041
@Override
41-
public void addCustomer(Customer customer) {
42+
public void addCustomer(final Customer customer) {
4243
customers.add(customer);
4344
}
4445

4546

4647
@Override
47-
public void updateCustomer(Customer customer) {
48-
if (customers.contains(customer)) {
49-
customers.set(customers.indexOf(customer), customer);
48+
public void updateCustomer(final Customer customer) {
49+
if (getAllCustomers().contains(customer)) {
50+
final int index = getAllCustomers().indexOf(customer);
51+
getAllCustomers().set(index, customer);
5052
}
5153
}
5254

5355
@Override
54-
public void deleteCustomer(Customer customer) {
55-
customers.remove(customer);
56+
public void deleteCustomer(final Customer customer) {
57+
getAllCustomers().remove(customer);
5658
}
5759
}

dao/src/test/java/com/iluwatar/dao/AppTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.iluwatar.dao;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
public class CustomerDaoImplTest {
14+
15+
private CustomerDaoImpl impl;
16+
private List<Customer> customers;
17+
private static final Customer CUSTOMER = new Customer(1, "Freddy", "Kruger");
18+
19+
@Before
20+
public void setUp() {
21+
customers = new ArrayList<Customer>();
22+
customers.add(CUSTOMER);
23+
impl = new CustomerDaoImpl(customers);
24+
}
25+
26+
@Test
27+
public void deleteExistingCustomer() {
28+
assertEquals(1, impl.getAllCustomers().size());
29+
impl.deleteCustomer(CUSTOMER);
30+
assertTrue(impl.getAllCustomers().isEmpty());
31+
}
32+
33+
@Test
34+
public void deleteNonExistingCustomer() {
35+
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
36+
impl.deleteCustomer(nonExistingCustomer);
37+
assertEquals(1, impl.getAllCustomers().size());
38+
}
39+
40+
@Test
41+
public void updateExistingCustomer() {
42+
final String newFirstname = "Bernard";
43+
final String newLastname = "Montgomery";
44+
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
45+
impl.updateCustomer(customer);
46+
final Customer cust = impl.getCustomerById(CUSTOMER.getId());
47+
assertEquals(newFirstname, cust.getFirstName());
48+
assertEquals(newLastname, cust.getLastName());
49+
}
50+
51+
@Test
52+
public void updateNonExistingCustomer() {
53+
final int nonExistingId = 999;
54+
final String newFirstname = "Douglas";
55+
final String newLastname = "MacArthur";
56+
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
57+
impl.updateCustomer(customer);
58+
assertNull(impl.getCustomerById(nonExistingId));
59+
final Customer existingCustomer = impl.getCustomerById(CUSTOMER.getId());
60+
assertEquals(CUSTOMER.getFirstName(), existingCustomer.getFirstName());
61+
assertEquals(CUSTOMER.getLastName(), existingCustomer.getLastName());
62+
}
63+
64+
@Test
65+
public void addCustomer() {
66+
final Customer newCustomer = new Customer(3, "George", "Patton");
67+
impl.addCustomer(newCustomer);
68+
assertEquals(2, impl.getAllCustomers().size());
69+
}
70+
71+
@Test
72+
public void getExistinCustomerById() {
73+
assertEquals(CUSTOMER, impl.getCustomerById(CUSTOMER.getId()));
74+
}
75+
76+
@Test
77+
public void getNonExistinCustomerById() {
78+
final int nonExistingId = 999;
79+
assertNull(impl.getCustomerById(nonExistingId));
80+
}
81+
}

0 commit comments

Comments
 (0)