Skip to content

Improved unit tests in dao module and added log4j dependency. #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class AppTest {

private App app = new App();;
private App app = new App();
private KingdomFactory elfFactory;
private KingdomFactory orcFactory;

Expand Down
42 changes: 26 additions & 16 deletions dao/pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
<?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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.6.0</version>
</parent>
<artifactId>dao</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.6.0</version>
</parent>
<artifactId>dao</artifactId>

<properties>

</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>
47 changes: 22 additions & 25 deletions dao/src/main/java/com/iluwatar/dao/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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
Expand All @@ -11,43 +13,38 @@
*/
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<Customer> generateSampleCustomers() {
Customer customer1 = new Customer(1, "Adam", "Adamson");
Customer customer2 = new Customer(2, "Bob", "Bobson");
Customer customer3 = new Customer(3, "Carl", "Carlson");

List<Customer> customers = new ArrayList<Customer>();
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<Customer> customers = new ArrayList<Customer>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Verbosity can be reduced here by using <>.

customers.add(customer1);
customers.add(customer2);
customers.add(customer3);
Expand Down
98 changes: 48 additions & 50 deletions dao/src/main/java/com/iluwatar/dao/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,63 @@
*
*/
public class Customer {

private int id;
private String firstName;
private String lastName;

public Customer(int id, String firstName, 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(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(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(String lastName) {
this.lastName = lastName;
}

@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
public String getLastName() {
return lastName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
public void setLastName(final String lastName) {
this.lastName = lastName;
}

Customer customer = (Customer) o;
@Override
public String toString() {
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
+ getLastName() + '\'' + '}';
}

if (id != customer.id) return false;

return true;
@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;
}
return isEqual;
}

@Override
public int hashCode() {
int result = id;
return result;
}
}
@Override
public int hashCode() {
int result = getId();
return result;
}
}
18 changes: 11 additions & 7 deletions dao/src/main/java/com/iluwatar/dao/CustomerDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
*
*/
public interface CustomerDao {

public List<Customer> getAllCustomers();
public Customer getCusterById(int id);
public void addCustomer(Customer customer);
public void updateCustomer(Customer customer);
public void deleteCustomer(Customer customer);
}

List<Customer> getAllCustomers();

Customer getCustomerById(int id);

void addCustomer(Customer customer);

void updateCustomer(Customer customer);

void deleteCustomer(Customer customer);
}
85 changes: 45 additions & 40 deletions dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,59 @@

/**
*
* 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<Customer> customers;

public CustomerDaoImpl(List<Customer> customers) {
this.customers = customers;
}

@Override
public List<Customer> getAllCustomers() {
return 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<Customer> customers;

public CustomerDaoImpl(final List<Customer> 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
public Customer getCusterById(int id) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getId() == id) {
return customers.get(i);
}
}
// No customer found
return null;
}

@Override
public void addCustomer(Customer customer) {
customers.add(customer);
@Override
public void addCustomer(final Customer customer) {
if (getCustomerById(customer.getId()) == null) {
customers.add(customer);
}
}


@Override
public void updateCustomer(Customer customer) {
if (customers.contains(customer)) {
customers.set(customers.indexOf(customer), 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(Customer customer) {
customers.remove(customer);
}
}
@Override
public void deleteCustomer(final Customer customer) {
getAllCustomers().remove(customer);
}
}
19 changes: 0 additions & 19 deletions dao/src/test/java/com/iluwatar/dao/AppTest.java

This file was deleted.

Loading