mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 11:25:58 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -66,14 +66,14 @@ public class App {
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,10 @@ package com.iluwatar.dao;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/**
|
||||
* Custom exception.
|
||||
*/
|
||||
/** Custom exception. */
|
||||
public class CustomException extends Exception {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
|
||||
@@ -30,9 +30,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* A customer POJO that represents the data that will be read from the data source.
|
||||
*/
|
||||
/** A customer POJO that represents the data that will be read from the data source. */
|
||||
@Setter
|
||||
@Getter
|
||||
@ToString
|
||||
@@ -40,8 +38,7 @@ import lombok.ToString;
|
||||
@AllArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
@EqualsAndHashCode.Include
|
||||
private int id;
|
||||
@EqualsAndHashCode.Include private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
}
|
||||
|
||||
@@ -24,18 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
/**
|
||||
* Customer Schema SQL Class.
|
||||
*/
|
||||
/** Customer Schema SQL Class. */
|
||||
public final class CustomerSchemaSql {
|
||||
|
||||
private CustomerSchemaSql() {
|
||||
}
|
||||
private CustomerSchemaSql() {}
|
||||
|
||||
public static final String CREATE_SCHEMA_SQL =
|
||||
"CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
+ "LNAME VARCHAR(100))";
|
||||
"CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), " + "LNAME VARCHAR(100))";
|
||||
|
||||
public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
|
||||
|
||||
}
|
||||
|
||||
@@ -38,9 +38,7 @@ import javax.sql.DataSource;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
|
||||
*/
|
||||
/** An implementation of {@link CustomerDao} that persists customers in RDBMS. */
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class DbCustomerDao implements CustomerDao {
|
||||
@@ -60,22 +58,24 @@ public class DbCustomerDao implements CustomerDao {
|
||||
var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
|
||||
var resultSet = statement.executeQuery(); // NOSONAR
|
||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||
Spliterator.ORDERED) {
|
||||
return StreamSupport.stream(
|
||||
new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE, Spliterator.ORDERED) {
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Customer> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createCustomer(resultSet));
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Customer> action) {
|
||||
try {
|
||||
if (!resultSet.next()) {
|
||||
return false;
|
||||
}
|
||||
action.accept(createCustomer(resultSet));
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e); // NOSONAR
|
||||
}
|
||||
}
|
||||
},
|
||||
false)
|
||||
.onClose(() -> mutedClose(connection, statement, resultSet));
|
||||
} catch (SQLException e) {
|
||||
throw new CustomException(e.getMessage(), e);
|
||||
}
|
||||
@@ -96,21 +96,18 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
private Customer createCustomer(ResultSet resultSet) throws SQLException {
|
||||
return new Customer(resultSet.getInt("ID"),
|
||||
resultSet.getString("FNAME"),
|
||||
resultSet.getString("LNAME"));
|
||||
return new Customer(
|
||||
resultSet.getInt("ID"), resultSet.getString("FNAME"), resultSet.getString("LNAME"));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<Customer> getById(int id) throws Exception {
|
||||
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
|
||||
statement.setInt(1, id);
|
||||
resultSet = statement.executeQuery();
|
||||
@@ -128,9 +125,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean add(Customer customer) throws Exception {
|
||||
if (getById(customer.getId()).isPresent()) {
|
||||
@@ -138,7 +133,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
statement.setString(2, customer.getFirstName());
|
||||
statement.setString(3, customer.getLastName());
|
||||
@@ -149,15 +144,12 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean update(Customer customer) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement =
|
||||
connection
|
||||
.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
var statement =
|
||||
connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
statement.setString(1, customer.getFirstName());
|
||||
statement.setString(2, customer.getLastName());
|
||||
statement.setInt(3, customer.getId());
|
||||
@@ -167,13 +159,11 @@ public class DbCustomerDao implements CustomerDao {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean delete(Customer customer) throws Exception {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
||||
|
||||
@@ -31,17 +31,14 @@ import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory and
|
||||
* data is lost when the application exits.
|
||||
* <br>
|
||||
* data is lost when the application exits. <br>
|
||||
* This implementation is useful as temporary database or for testing.
|
||||
*/
|
||||
public class InMemoryCustomerDao implements CustomerDao {
|
||||
|
||||
private final Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||
|
||||
/**
|
||||
* An eagerly evaluated stream of customers stored in memory.
|
||||
*/
|
||||
/** An eagerly evaluated stream of customers stored in memory. */
|
||||
@Override
|
||||
public Stream<Customer> getAll() {
|
||||
return idToCustomer.values().stream();
|
||||
|
||||
@@ -24,23 +24,19 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that DAO example runs without errors.
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Tests that DAO example runs without errors. */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
* Issue: Add at least one assertion to this test case. Solution: Inserted assertion to check
|
||||
* whether the execution of the main method in {@link App#main(String[])} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteDaoWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link Customer}.
|
||||
*/
|
||||
/** Tests {@link Customer}. */
|
||||
class CustomerTest {
|
||||
|
||||
private Customer customer;
|
||||
@@ -89,7 +87,10 @@ class CustomerTest {
|
||||
|
||||
@Test
|
||||
void testToString() {
|
||||
assertEquals(String.format("Customer(id=%s, firstName=%s, lastName=%s)",
|
||||
customer.getId(), customer.getFirstName(), customer.getLastName()), customer.toString());
|
||||
assertEquals(
|
||||
String.format(
|
||||
"Customer(id=%s, firstName=%s, lastName=%s)",
|
||||
customer.getId(), customer.getFirstName(), customer.getLastName()),
|
||||
customer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +44,7 @@ import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* Tests {@link DbCustomerDao}.
|
||||
*/
|
||||
/** Tests {@link DbCustomerDao}. */
|
||||
class DbCustomerDaoTest {
|
||||
|
||||
private static final String DB_URL = "jdbc:h2:mem:dao;DB_CLOSE_DELAY=-1";
|
||||
@@ -61,14 +59,12 @@ class DbCustomerDaoTest {
|
||||
@BeforeEach
|
||||
void createSchema() throws SQLException {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the scenario where DB connectivity is present.
|
||||
*/
|
||||
/** Represents the scenario where DB connectivity is present. */
|
||||
@Nested
|
||||
class ConnectionSuccess {
|
||||
|
||||
@@ -160,8 +156,8 @@ class DbCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
|
||||
Exception {
|
||||
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation()
|
||||
throws Exception {
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
final var customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
|
||||
@@ -218,7 +214,9 @@ class DbCustomerDaoTest {
|
||||
void updatingACustomerFailsWithFeedbackToTheClient() {
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
assertThrows(Exception.class, () -> dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname)));
|
||||
assertThrows(
|
||||
Exception.class,
|
||||
() -> dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -230,7 +228,6 @@ class DbCustomerDaoTest {
|
||||
void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> dao.getAll());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,7 +238,7 @@ class DbCustomerDaoTest {
|
||||
@AfterEach
|
||||
void deleteSchema() throws SQLException {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link InMemoryCustomerDao}.
|
||||
*/
|
||||
/** Tests {@link InMemoryCustomerDao}. */
|
||||
class InMemoryCustomerDaoTest {
|
||||
|
||||
private InMemoryCustomerDao dao;
|
||||
@@ -48,8 +46,7 @@ class InMemoryCustomerDaoTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the scenario when the DAO operations are being performed on a non-existent
|
||||
* customer.
|
||||
* Represents the scenario when the DAO operations are being performed on a non-existent customer.
|
||||
*/
|
||||
@Nested
|
||||
class NonExistingCustomer {
|
||||
@@ -121,8 +118,8 @@ class InMemoryCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
|
||||
Exception {
|
||||
void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation()
|
||||
throws Exception {
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
final var customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
|
||||
|
||||
Reference in New Issue
Block a user