Formatting changes

This commit is contained in:
Niket Agrawal 2023-11-03 03:49:06 +05:30
parent de5c3baf31
commit 3fe81a3e07
2 changed files with 19 additions and 31 deletions

View File

@ -16,11 +16,6 @@
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>${h2.version}</version> <version>${h2.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
<dependency> <dependency>
<groupId>commons-dbutils</groupId> <groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId> <artifactId>commons-dbutils</artifactId>
@ -29,7 +24,6 @@
</dependencies> </dependencies>
<properties> <properties>
<h2.version>2.1.214</h2.version> <h2.version>2.1.214</h2.version>
<spring-jdbc.version>5.3.29</spring-jdbc.version>
<commons-dbutils.version>1.8.1</commons-dbutils.version> <commons-dbutils.version>1.8.1</commons-dbutils.version>
</properties> </properties>
</project> </project>

View File

@ -33,48 +33,43 @@ public class ResultSetToMapUnitTest {
private void initialDataSetup() throws SQLException { private void initialDataSetup() throws SQLException {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
String sql = "CREATE TABLE EMPLOYEE (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; String ddlQuery = "CREATE TABLE employee (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))";
statement.executeUpdate(sql); statement.execute(ddlQuery);
List<String> sqlQueryList = Arrays.asList("INSERT INTO EMPLOYEE VALUES (1, 'Steve','London')", "INSERT INTO EMPLOYEE VALUES (2, 'John','London')", "INSERT INTO EMPLOYEE VALUES (3, 'David', 'Sydney')", List<String> sqlQueryList = Arrays.asList("INSERT INTO employee VALUES (1, 'Steve','London')", "INSERT INTO employee VALUES (2, 'John','London')", "INSERT INTO employee VALUES (3, 'David', 'Sydney')",
"INSERT INTO EMPLOYEE VALUES (4, 'Kevin','London')", "INSERT INTO EMPLOYEE VALUES (5, 'Jade', 'Sydney')"); "INSERT INTO employee VALUES (4, 'Kevin','London')", "INSERT INTO employee VALUES (5, 'Jade', 'Sydney')");
for (String query : sqlQueryList) { for (String query : sqlQueryList) {
statement.executeUpdate(query); statement.execute(query);
} }
} }
@Test @Test
public void whenUsingNativeJava_thenConvertResultSetToMap() throws SQLException { public void whenUsingContainsKey_thenConvertResultSetToMap() throws SQLException {
ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery();
.executeQuery();
Map<String, List<String>> valueMap = new HashMap<>(); Map<String, List<String>> valueMap = new HashMap<>();
while (resultSet.next()) { while (resultSet.next()) {
String empCity = resultSet.getString("empCity"); String empCity = resultSet.getString("empCity");
String empName = resultSet.getString("empName"); String empName = resultSet.getString("empName");
if (!valueMap.containsKey(empCity)) { if (!valueMap.containsKey(empCity)) {
valueMap.putIfAbsent(empCity, new ArrayList<>()); valueMap.put(empCity, new ArrayList<>());
} }
valueMap.get(empCity) valueMap.get(empCity).add(empName);
.add(empName);
} }
assertEquals(3, valueMap.get("London") assertEquals(3, valueMap.get("London").size());
.size());
} }
@Test @Test
public void whenUsingLambda_thenConvertResultSetToMap() throws SQLException { public void whenUsingComputeIfAbsent_thenConvertResultSetToMap() throws SQLException {
ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery();
.executeQuery();
Map<String, List<String>> valueMap = new HashMap<>(); Map<String, List<String>> valueMap = new HashMap<>();
while (resultSet.next()) { while (resultSet.next()) {
String empCity = resultSet.getString("empCity"); String empCity = resultSet.getString("empCity");
String empName = resultSet.getString("empName"); String empName = resultSet.getString("empName");
valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()) valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName);
.add(empName);
} }
assertEquals(3, valueMap.get("London") assertEquals(3, valueMap.get("London").size());
.size());
} }
@Test @Test
@ -86,16 +81,15 @@ public class ResultSetToMapUnitTest {
while (resultSet.next()) { while (resultSet.next()) {
String empCity = resultSet.getString("empCity"); String empCity = resultSet.getString("empCity");
String empName = resultSet.getString("empName"); String empName = resultSet.getString("empName");
result.computeIfAbsent(empCity, data -> new ArrayList<>()) result.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName);
.add(empName);
} }
return result; return result;
} }
}; };
QueryRunner run = new QueryRunner(); QueryRunner run = new QueryRunner();
Map<String, List<String>> valueMap = run.query(connection, "SELECT * FROM EMPLOYEE", handler); Map<String, List<String>> valueMap = run.query(connection, "SELECT * FROM employee", handler);
assertEquals(3, valueMap.get("London") assertEquals(3, valueMap.get("London").size());
.size());
} }
@After @After