@Test void runSqlImplementationTest() { JUnitGeneratorHelper helper = new JUnitGeneratorHelper(); String expected = "\n" + "private static java.util.ArrayList<java.util.HashMap<java.lang.String, java.lang.String>> runSql(\n" + " java.lang.String query, boolean isUpdate) throws java.sql.SQLException {\n" + " java.sql.Connection connection = java.sql.DriverManager.getConnection(DB_JDBC_URL, DB_USER, DB_PASSWORD);\n" + " java.sql.Statement statement = connection.createStatement();\n" + " if (isUpdate == true) {\n" + " statement.executeUpdate(query);\n" + " return null;\n" + " } else {\n" + " java.util.ArrayList<java.util.HashMap<java.lang.String, java.lang.String>> tableStructure = new java.util.ArrayList<java.util.HashMap<java.lang.String, java.lang.String>>();\n" + " java.sql.ResultSet resultSet = statement.executeQuery(query);\n" + " java.util.List<java.lang.String> columns = getResultColumns(resultSet);\n" + " while (resultSet.next()) {\n" + " java.util.HashMap<java.lang.String, java.lang.String> values = new java.util.HashMap<>();\n" + " for (String column : columns) {\n" + " Object value = resultSet.getObject(column);\n" + " values.put(column, value != null ? value.toString() : \"NULL\");\n" + " }\n" + " tableStructure.add(values);\n" + " }\n" + " return tableStructure;\n" + " }\n" + "}\n"; assertThat(helper.buildRunSqlImplementation().toString()).isEqualTo(expected); }
public MethodSpec buildRunSqlImplementation() { MethodSpec.Builder runSql = MethodSpec.methodBuilder(METHOD_RUN_SQL) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .returns(RETURN_TYPE_RUN_SQL) .addParameter(String.class, "query") .addParameter(TypeName.BOOLEAN, "isUpdate") .addException(SQLException.class) .addJavadoc("Executes an SQL query on the database.\n\n" + "@param query The query to execute.\n" + "@param isUpdate Whether the query is a data modification statement.\n\n" + "@returns The resulting table, or null if the query is an update.\n"); runSql.addStatement( "$T connection = $T.getConnection($L, $L, $L)", Connection.class, DriverManager.class, FIELD_DB_JDBC_URL, FIELD_DB_USER, FIELD_DB_PASSWORD) .addStatement("$T statement = connection.createStatement()", Statement.class) .beginControlFlow("if (isUpdate == true)") .addStatement("statement.executeUpdate(query)") .addStatement("return null") .nextControlFlow("else") .addStatement("$T tableStructure = new $T()", RETURN_TYPE_RUN_SQL, RETURN_TYPE_RUN_SQL) .addStatement("$T resultSet = statement.executeQuery(query)", ResultSet.class) .addStatement("$T columns = $L(resultSet)", ParameterizedTypeName.get(List.class, String.class), METHOD_GET_RESULT_COLUMNS) .beginControlFlow("while (resultSet.next())") .addStatement("$T values = new $T<>()", ParameterizedTypeName.get(HashMap.class, String.class, String.class), HashMap.class) .beginControlFlow("for (String column : columns)") .addStatement("Object value = resultSet.getObject(column)") .addStatement("values.put(column, value != null ? value.toString() : \"NULL\")") .endControlFlow() .addStatement("tableStructure.add(values)") .endControlFlow() .addStatement("return tableStructure") .endControlFlow(); return runSql.build(); }
JUnitGeneratorHelper { public MethodSpec buildRunSqlImplementation() { MethodSpec.Builder runSql = MethodSpec.methodBuilder(METHOD_RUN_SQL) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .returns(RETURN_TYPE_RUN_SQL) .addParameter(String.class, "query") .addParameter(TypeName.BOOLEAN, "isUpdate") .addException(SQLException.class) .addJavadoc("Executes an SQL query on the database.\n\n" + "@param query The query to execute.\n" + "@param isUpdate Whether the query is a data modification statement.\n\n" + "@returns The resulting table, or null if the query is an update.\n"); runSql.addStatement( "$T connection = $T.getConnection($L, $L, $L)", Connection.class, DriverManager.class, FIELD_DB_JDBC_URL, FIELD_DB_USER, FIELD_DB_PASSWORD) .addStatement("$T statement = connection.createStatement()", Statement.class) .beginControlFlow("if (isUpdate == true)") .addStatement("statement.executeUpdate(query)") .addStatement("return null") .nextControlFlow("else") .addStatement("$T tableStructure = new $T()", RETURN_TYPE_RUN_SQL, RETURN_TYPE_RUN_SQL) .addStatement("$T resultSet = statement.executeQuery(query)", ResultSet.class) .addStatement("$T columns = $L(resultSet)", ParameterizedTypeName.get(List.class, String.class), METHOD_GET_RESULT_COLUMNS) .beginControlFlow("while (resultSet.next())") .addStatement("$T values = new $T<>()", ParameterizedTypeName.get(HashMap.class, String.class, String.class), HashMap.class) .beginControlFlow("for (String column : columns)") .addStatement("Object value = resultSet.getObject(column)") .addStatement("values.put(column, value != null ? value.toString() : \"NULL\")") .endControlFlow() .addStatement("tableStructure.add(values)") .endControlFlow() .addStatement("return tableStructure") .endControlFlow(); return runSql.build(); } }
JUnitGeneratorHelper { public MethodSpec buildRunSqlImplementation() { MethodSpec.Builder runSql = MethodSpec.methodBuilder(METHOD_RUN_SQL) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .returns(RETURN_TYPE_RUN_SQL) .addParameter(String.class, "query") .addParameter(TypeName.BOOLEAN, "isUpdate") .addException(SQLException.class) .addJavadoc("Executes an SQL query on the database.\n\n" + "@param query The query to execute.\n" + "@param isUpdate Whether the query is a data modification statement.\n\n" + "@returns The resulting table, or null if the query is an update.\n"); runSql.addStatement( "$T connection = $T.getConnection($L, $L, $L)", Connection.class, DriverManager.class, FIELD_DB_JDBC_URL, FIELD_DB_USER, FIELD_DB_PASSWORD) .addStatement("$T statement = connection.createStatement()", Statement.class) .beginControlFlow("if (isUpdate == true)") .addStatement("statement.executeUpdate(query)") .addStatement("return null") .nextControlFlow("else") .addStatement("$T tableStructure = new $T()", RETURN_TYPE_RUN_SQL, RETURN_TYPE_RUN_SQL) .addStatement("$T resultSet = statement.executeQuery(query)", ResultSet.class) .addStatement("$T columns = $L(resultSet)", ParameterizedTypeName.get(List.class, String.class), METHOD_GET_RESULT_COLUMNS) .beginControlFlow("while (resultSet.next())") .addStatement("$T values = new $T<>()", ParameterizedTypeName.get(HashMap.class, String.class, String.class), HashMap.class) .beginControlFlow("for (String column : columns)") .addStatement("Object value = resultSet.getObject(column)") .addStatement("values.put(column, value != null ? value.toString() : \"NULL\")") .endControlFlow() .addStatement("tableStructure.add(values)") .endControlFlow() .addStatement("return tableStructure") .endControlFlow(); return runSql.build(); }  }
JUnitGeneratorHelper { public MethodSpec buildRunSqlImplementation() { MethodSpec.Builder runSql = MethodSpec.methodBuilder(METHOD_RUN_SQL) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .returns(RETURN_TYPE_RUN_SQL) .addParameter(String.class, "query") .addParameter(TypeName.BOOLEAN, "isUpdate") .addException(SQLException.class) .addJavadoc("Executes an SQL query on the database.\n\n" + "@param query The query to execute.\n" + "@param isUpdate Whether the query is a data modification statement.\n\n" + "@returns The resulting table, or null if the query is an update.\n"); runSql.addStatement( "$T connection = $T.getConnection($L, $L, $L)", Connection.class, DriverManager.class, FIELD_DB_JDBC_URL, FIELD_DB_USER, FIELD_DB_PASSWORD) .addStatement("$T statement = connection.createStatement()", Statement.class) .beginControlFlow("if (isUpdate == true)") .addStatement("statement.executeUpdate(query)") .addStatement("return null") .nextControlFlow("else") .addStatement("$T tableStructure = new $T()", RETURN_TYPE_RUN_SQL, RETURN_TYPE_RUN_SQL) .addStatement("$T resultSet = statement.executeQuery(query)", ResultSet.class) .addStatement("$T columns = $L(resultSet)", ParameterizedTypeName.get(List.class, String.class), METHOD_GET_RESULT_COLUMNS) .beginControlFlow("while (resultSet.next())") .addStatement("$T values = new $T<>()", ParameterizedTypeName.get(HashMap.class, String.class, String.class), HashMap.class) .beginControlFlow("for (String column : columns)") .addStatement("Object value = resultSet.getObject(column)") .addStatement("values.put(column, value != null ? value.toString() : \"NULL\")") .endControlFlow() .addStatement("tableStructure.add(values)") .endControlFlow() .addStatement("return tableStructure") .endControlFlow(); return runSql.build(); }  MethodSpec buildRunSqlImplementation(); MethodSpec buildRunSqlEmpty(); MethodSpec buildMapMaker(); MethodSpec buildGetResultColumns(); }
JUnitGeneratorHelper { public MethodSpec buildRunSqlImplementation() { MethodSpec.Builder runSql = MethodSpec.methodBuilder(METHOD_RUN_SQL) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .returns(RETURN_TYPE_RUN_SQL) .addParameter(String.class, "query") .addParameter(TypeName.BOOLEAN, "isUpdate") .addException(SQLException.class) .addJavadoc("Executes an SQL query on the database.\n\n" + "@param query The query to execute.\n" + "@param isUpdate Whether the query is a data modification statement.\n\n" + "@returns The resulting table, or null if the query is an update.\n"); runSql.addStatement( "$T connection = $T.getConnection($L, $L, $L)", Connection.class, DriverManager.class, FIELD_DB_JDBC_URL, FIELD_DB_USER, FIELD_DB_PASSWORD) .addStatement("$T statement = connection.createStatement()", Statement.class) .beginControlFlow("if (isUpdate == true)") .addStatement("statement.executeUpdate(query)") .addStatement("return null") .nextControlFlow("else") .addStatement("$T tableStructure = new $T()", RETURN_TYPE_RUN_SQL, RETURN_TYPE_RUN_SQL) .addStatement("$T resultSet = statement.executeQuery(query)", ResultSet.class) .addStatement("$T columns = $L(resultSet)", ParameterizedTypeName.get(List.class, String.class), METHOD_GET_RESULT_COLUMNS) .beginControlFlow("while (resultSet.next())") .addStatement("$T values = new $T<>()", ParameterizedTypeName.get(HashMap.class, String.class, String.class), HashMap.class) .beginControlFlow("for (String column : columns)") .addStatement("Object value = resultSet.getObject(column)") .addStatement("values.put(column, value != null ? value.toString() : \"NULL\")") .endControlFlow() .addStatement("tableStructure.add(values)") .endControlFlow() .addStatement("return tableStructure") .endControlFlow(); return runSql.build(); }  MethodSpec buildRunSqlImplementation(); MethodSpec buildRunSqlEmpty(); MethodSpec buildMapMaker(); MethodSpec buildGetResultColumns(); static final String METHOD_RUN_SQL; static final ParameterizedTypeName RETURN_TYPE_RUN_SQL; static final String METHOD_MAP_MAKER; static final String METHOD_GET_RESULT_COLUMNS; }