Skip to content

Commit

Permalink
Replace usages of RuntimeSQLException with UncheckedSQLException (#513)
Browse files Browse the repository at this point in the history
Closes #509
  • Loading branch information
sleberknight authored Aug 4, 2024
1 parent a213273 commit 0980016
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/kiwiproject/test/h2/H2DatabaseTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.h2.jdbcx.JdbcDataSource;
import org.kiwiproject.test.jdbc.RuntimeSQLException;
import org.kiwiproject.jdbc.UncheckedSQLException;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
* Utilities for H2 databases.
* <p>
Expand All @@ -33,7 +34,7 @@ public class H2DatabaseTestHelper {
*
* @return a {@link H2FileBasedDatabase} that represents the new database
* @throws IllegalStateException if the database directory could not be created
* @throws RuntimeSQLException if any other error occurs
* @throws UncheckedSQLException if any other error occurs
* @see System#getProperty(String)
*/
public static H2FileBasedDatabase buildH2FileBasedDatabase() {
Expand All @@ -55,7 +56,7 @@ private static File newH2DatabaseDirectory() {
* @param h2DatabaseDirectory the directory where the database resides
* @return a {@link DataSource} that can be used to connect to the H2 database
* @throws IllegalStateException if the database directory could not be created
* @throws RuntimeSQLException if any other error occurs
* @throws UncheckedSQLException if any other error occurs
*/
public static DataSource buildH2DataSource(File h2DatabaseDirectory) {
try {
Expand Down Expand Up @@ -84,7 +85,7 @@ private static DataSource buildH2DatabaseWithTestTable(File h2DatabaseDirectory)
LOG.trace("Successfully created test_table");
return dataSource;
} catch (SQLException e) {
throw new RuntimeSQLException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.kiwiproject.test.jdbc;

import org.kiwiproject.base.KiwiDeprecated;
import org.kiwiproject.base.KiwiPreconditions;
import org.kiwiproject.base.KiwiDeprecated.Severity;
import org.kiwiproject.base.KiwiPreconditions;

import java.sql.SQLException;

Expand All @@ -11,6 +11,7 @@
*
* @deprecated replaced by UncheckedSQLException in kiwi 4.2.0
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated(since = "3.6.0", forRemoval = true)
@KiwiDeprecated(
removeAt = "4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.jdbc.UncheckedSQLException;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
Expand All @@ -21,6 +21,8 @@
import java.util.Objects;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
* A very simple implementation of {@link DataSource} intended to be used only during tests. As its name
* suggests, this implementation stores a single {@link Connection} which is always returned by the
Expand Down Expand Up @@ -120,7 +122,7 @@ private static Connection getConnectionFromDriverManager(String url, String user
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new RuntimeSQLException(f("Error getting Connection for URL {} and username {}", url, username), e);
throw new UncheckedSQLException(f("Error getting Connection for URL {} and username {}", url, username), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.sql.SQLException;

@SuppressWarnings("removal")
@DisplayName("RuntimeSQLException")
class RuntimeSQLExceptionTest {

Expand All @@ -29,4 +30,4 @@ void shouldConstructWithSQLException() {
.hasMessage("Statement error")
.hasCauseReference(sqlEx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.kiwiproject.jdbc.UncheckedSQLException;
import org.kiwiproject.test.junit.jupiter.H2FileBasedDatabaseExtension;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
Expand All @@ -23,6 +23,8 @@
import java.util.logging.Logger;
import java.util.stream.IntStream;

import javax.sql.DataSource;

@DisplayName("SimpleSingleConnectionDataSource")
class SimpleSingleConnectionDataSourceTest {

Expand All @@ -47,7 +49,7 @@ void shouldThrow_GivenInvalidCredentials() {

var thrown = catchThrowable(() -> new SimpleSingleConnectionDataSource(url, "bad_user"));

assertThat(thrown).isExactlyInstanceOf(RuntimeSQLException.class);
assertThat(thrown).isExactlyInstanceOf(UncheckedSQLException.class);

// The actual cause (as of the time I write this) is a org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException.
// Since I do not want to be so specific to a vendor implementation, just check that there is a non-null cause.
Expand Down Expand Up @@ -75,7 +77,7 @@ private Connection tryGetConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeSQLException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down Expand Up @@ -126,7 +128,7 @@ private Connection tryGetConnection(String username, String password) {
try {
return dataSource.getConnection(username, password);
} catch (SQLException e) {
throw new RuntimeSQLException(e);
throw new UncheckedSQLException(e);
}
}
}
Expand Down

0 comments on commit 0980016

Please sign in to comment.