Skip to content

Commit

Permalink
Updated DatabaseManager
Browse files Browse the repository at this point in the history
Added a second constructor to make JUnit testing of the class feasible. Second constructor should not be used to create object in production code.
  • Loading branch information
roaraf committed Mar 5, 2026
1 parent 3a453b3 commit dce997e
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ public DatabaseManager() {
}
}

/**
* Constructs a new {@code DatabaseManager} using database credentials
* <p>
* Used primarily for JUnit tests. Production should use the constructor
* using environment variables.
* </p>
*
* @param databaseURL the url to the database
* @param username the username used to log in to the database
* @param password the password used to log in to the database
* @throws IllegalArgumentException if databaseURL, username, or password is
* {@code null} or blank
*/

public DatabaseManager(String databaseURL, String username, String password) {
this.databaseURL = databaseURL;
this.username = username;
this.password = password;

if (this.databaseURL == null || this.databaseURL.isBlank()) {
throw new IllegalArgumentException("Database environment variable URL has not been set");
}

if (this.username == null || this.username.isBlank()) {
throw new IllegalArgumentException("Username environment variable has not been set");
}

if (this.password == null || this.password.isBlank()) {
throw new IllegalArgumentException("Password environment variable has not been set");
}
}

/**
* Creates the {@code charities} table if it does not already exist.
* <p>
Expand All @@ -74,7 +106,7 @@ url VARCHAR(255),
s.execute(sql_query);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error creating tables.");
throw new RuntimeException("Error creating table.");
}
}

Expand Down Expand Up @@ -125,7 +157,7 @@ INSERT INTO charities (org_number, name, status, url, is_pre_approved)


// Temporary table for parity check
sql_query = "CREATE TEMPORARY TABLE temp (org_number VARCHAR(100) PRIMARY KEY)";
sql_query = "CREATE TEMPORARY TABLE IF NOT EXISTS temp (org_number VARCHAR(100) PRIMARY KEY)";
try (PreparedStatement temp_create = conn.prepareStatement(sql_query)) {
temp_create.execute();
}
Expand Down

0 comments on commit dce997e

Please sign in to comment.