mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 06:59:29 +00:00
fix: Fix sonar issues (#2925)
* Remove unused member which was also causing a false positive sonar issue. Fixes sonar issue https://sonarcloud.io/project/issues?open=AY3gHwu5DIZTZkppqVEG&id=iluwatar_java-design-patterns * Fixes sonar issue https://sonarcloud.io/project/issues?open=AXK0OzDA-CiGJS70dLki&id=iluwatar_java-design-patterns related to "Refactor the code of the lambda to not have multiple invocations throwing the same checked exception." Also, updated the code to use Instant and Duration to deal with time instead of int. Added the awaitility library to perform assertions in test which is more reliable than using Thread.sleep directly to wait for events to happen. * checkstyle fix * Add sneaky throws to fix sonar lint issue. This is fine as the newFile method is not being tested but instead the new SimpleFileWriter(...) is. * The first booking needs to happen outside the assertions. Fixed other warnings * Use records to pass around related objects instead of using a large number of individual params, which sonar did not like. * Checkstyle fixes * checkstyle fixes * Remove complexity to keep sonar happy. * Split into different methods to reduce complexity. Could be broken down even further but currently Sonar is happy. * Move files to correct package * Add valid assertions to tests * rename constants to avoid confusion * Sonar warning related to cognitive complexity can be suppressed as the methods are quite generic and not functional enough to be separated out. * Use constants to keep Sonar happy * Use correct constant naming conventions * Use correct constant naming conventions * Use lombok to define noargsconstructor * Use a single method to do the logging * Remove unused constructor and redundant method * Use a reusable method for logging
This commit is contained in:
@@ -40,6 +40,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final String BANGLORE = "Banglore";
|
||||
private static final String KARNATAKA = "Karnataka";
|
||||
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
@@ -53,11 +57,11 @@ public class App {
|
||||
|
||||
// Orders to insert into database
|
||||
final var order1 = new Order("JBL headphone", "Ram",
|
||||
new ShippingAddress("Bangalore", "Karnataka", "560040"));
|
||||
new ShippingAddress(BANGLORE, KARNATAKA, "560040"));
|
||||
final var order2 = new Order("MacBook Pro", "Manjunath",
|
||||
new ShippingAddress("Bangalore", "Karnataka", "581204"));
|
||||
new ShippingAddress(BANGLORE, KARNATAKA, "581204"));
|
||||
final var order3 = new Order("Carrie Soto is Back", "Shiva",
|
||||
new ShippingAddress("Bangalore", "Karnataka", "560004"));
|
||||
new ShippingAddress(BANGLORE, KARNATAKA, "560004"));
|
||||
|
||||
// Create table for orders - Orders(id, name, orderedBy, city, state, pincode).
|
||||
// We can see that table is different from the Order object we have.
|
||||
@@ -102,4 +106,4 @@ public class App {
|
||||
LOGGER.error("Error deleting table");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
package com.iluwatar.embedded.value;
|
||||
|
||||
import static java.sql.PreparedStatement.RETURN_GENERATED_KEYS;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -33,6 +35,8 @@ import java.util.ArrayList;
|
||||
import java.util.stream.Stream;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Communicates with H2 database with the help of JDBC API.
|
||||
* Inherits the SQL queries and methods from @link AbstractDataSource class.
|
||||
@@ -68,7 +72,7 @@ public class DataSource implements DataSourceInterface {
|
||||
public boolean createSchema() {
|
||||
try (Statement createschema = conn.createStatement()) {
|
||||
createschema.execute(CREATE_SCHEMA);
|
||||
insertIntoOrders = conn.prepareStatement(INSERT_ORDER, PreparedStatement.RETURN_GENERATED_KEYS);
|
||||
insertIntoOrders = conn.prepareStatement(INSERT_ORDER, RETURN_GENERATED_KEYS);
|
||||
getschema = conn.createStatement();
|
||||
queryOrders = conn.createStatement();
|
||||
removeorder = conn.prepareStatement(REMOVE_ORDER);
|
||||
@@ -204,4 +208,4 @@ public class DataSource implements DataSourceInterface {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,10 +32,10 @@ import org.junit.jupiter.api.Test;
|
||||
* Check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void doesNotThrowException() {
|
||||
void doesNotThrowException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user