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>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
@ -29,7 +24,6 @@
</dependencies>
<properties>
<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>
</properties>
</project>

View File

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