Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,17 @@ public void testCountQuery() {
assertThat(output, containsString("4"));
assertThat(output, containsString("1 row(s) returned"));
}
}

@Test
@Order(15)
public void testInvalidStatementPrintsErrorWithoutStackTrace() {
MainMethodResult result = invokeMain(ExecuteH2DatabaseStatement.class, dbFilePath,
"SELECT * FROM customers");
String errorOutput = result.getTextWrittenToStandardError();

assertThat(errorOutput, containsString("Error:"));
assertThat(errorOutput, containsString("Table \"CUSTOMERS\" not found"));
assertThat(errorOutput, not(containsString("Exception in thread")));
assertThat(errorOutput, not(containsString("\tat ")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDate;
Expand Down Expand Up @@ -183,4 +185,16 @@ public void testDatabaseFilePathIsDisplayed() {
assertThat(output, containsString("Reading schema from H2 database:"));
assertThat(output, containsString(dbFilePath));
}

@Test
public void testInvalidDatabasePathPrintsErrorWithoutStackTrace(@TempDir Path tempDirectory) {
String invalidDatabasePath = tempDirectory.resolve("missing-db").toAbsolutePath() + ";IFEXISTS=TRUE";

MainMethodResult result = invokeMain(PrintH2DatabaseSchema.class, invalidDatabasePath);
String errorOutput = result.getTextWrittenToStandardError();

assertThat(errorOutput, containsString("Error:"));
assertThat(errorOutput, not(containsString("Exception in thread")));
assertThat(errorOutput, not(containsString("\tat ")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class ExecuteH2DatabaseStatement {
* Main method that takes a database file path and SQL statement as arguments.
*
* @param args command line arguments: args[0] = database file path, args[1] = SQL statement
* @throws SQLException if a database error occurs
*/
public static void main(String[] args) throws SQLException {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Missing required arguments");
System.err.println("Usage: java ExecuteH2DatabaseStatement <database-file-path> <sql-statement>");
Expand All @@ -39,6 +38,8 @@ public static void main(String[] args) throws SQLException {

try (Connection connection = H2DatabaseHelper.createFileBasedConnection(dbFile)) {
executeStatement(connection, sqlStatement);
} catch (SQLException ex) {
System.err.println("Error: " + ex.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ private static void printIndexes(DatabaseMetaData metaData, String tableName) th
* Main method that takes a database file path and prints the schema.
*
* @param args command line arguments where args[0] is the path to the H2 database file
* @throws SQLException if a database error occurs
*/
public static void main(String[] args) throws SQLException {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Missing database file path argument");
System.err.println("Usage: java PrintH2DatabaseSchema <database-file-path>");
Expand All @@ -212,6 +211,8 @@ public static void main(String[] args) throws SQLException {

try (Connection connection = H2DatabaseHelper.createFileBasedConnection(dbFile)) {
printDatabaseSchema(connection);
} catch (SQLException ex) {
System.err.println("Error: " + ex.getMessage());
}
}
}
Loading