Skip to content

Commit 0494756

Browse files
committed
KNOX-3440: Remove HSQLDB completely from Knox
1 parent a919d15 commit 0494756

15 files changed

Lines changed: 63 additions & 144 deletions

File tree

gateway-server/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,6 @@
586586
<artifactId>gateway-shell</artifactId>
587587
</dependency>
588588

589-
<dependency>
590-
<groupId>org.hsqldb</groupId>
591-
<artifactId>hsqldb</artifactId>
592-
<scope>provided</scope>
593-
<classifier>jdk8</classifier>
594-
</dependency>
595-
596589
<dependency>
597590
<groupId>com.h2database</groupId>
598591
<artifactId>h2</artifactId>

gateway-server/src/main/java/org/apache/knox/gateway/database/DataSourceProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static DataSource getDataSource(GatewayConfig gatewayConfig, AliasService
3333
case POSTGRESQL -> new PostgresDataSourceFactory();
3434
case MYSQL -> new MysqlDataSourceFactory();
3535
case MARIADB -> new MariaDBDataSourceFactory();
36-
case HSQL -> new HsqlDataSourceFactory();
3736
case H2 -> new H2DataSourceFactory();
3837
case ORACLE -> new OracleDataSourceFactory();
3938
};

gateway-server/src/main/java/org/apache/knox/gateway/database/DatabaseType.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ public enum DatabaseType {
4848
AbstractDataSourceFactory.KNOXIDF_TRUSTED_OIDC_ISSUERS_TABLE_SQL,
4949
AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL
5050
),
51-
HSQL("hsql",
52-
AbstractDataSourceFactory.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
53-
AbstractDataSourceFactory.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
54-
AbstractDataSourceFactory.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
55-
AbstractDataSourceFactory.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME,
56-
AbstractDataSourceFactory.KNOXIDF_FED_IDENTITY_TABLE_CREATE_SQL_FILE_NAME,
57-
AbstractDataSourceFactory.KNOXIDF_FED_IDENTITY_ATTR_TABLE_CREATE_SQL_FILE_NAME,
58-
AbstractDataSourceFactory.KNOXIDF_TRUSTED_OIDC_ISSUERS_TABLE_SQL,
59-
AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL
60-
),
6151
H2("h2",
6252
AbstractDataSourceFactory.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
6353
AbstractDataSourceFactory.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,

gateway-server/src/main/java/org/apache/knox/gateway/database/HsqlDataSourceFactory.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

gateway-server/src/main/java/org/apache/knox/gateway/database/KnoxDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class KnoxDatabase {
3232

3333
// Matches the leading "CREATE TABLE [IF NOT EXISTS] <name>" of a DDL statement so the table name
3434
// can be handed to the existence check. Case-insensitive; tolerates the IF NOT EXISTS that the
35-
// standard (PostgreSQL/MySQL/HSQL) scripts use but Derby and Oracle omit.
35+
// standard (PostgreSQL/MySQL/H2) scripts use but Oracle omit.
3636
private static final Pattern CREATE_TABLE_PATTERN =
3737
Pattern.compile("(?i)^\\s*CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?([\\w.]+)");
3838

gateway-server/src/test/java/org/apache/knox/gateway/database/KnoxDatabaseTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.List;
2929
import java.util.Map;
3030

31-
import org.hsqldb.jdbc.JDBCDataSource;
31+
import org.h2.jdbcx.JdbcDataSource;
3232
import org.junit.After;
3333
import org.junit.AfterClass;
3434
import org.junit.Before;
@@ -37,8 +37,8 @@
3737

3838
/**
3939
* Exercises {@link KnoxDatabase#createTablesIfNotExist(String)} and its
40-
* {@link KnoxDatabase#parseCreateTableStatements(String)} helper directly against an in-memory HSQL
41-
* database, using the standard (PostgreSQL/HSQL) KnoxIDF delegation-policy DDL as a representative
40+
* {@link KnoxDatabase#parseCreateTableStatements(String)} helper directly against an in-memory H2
41+
* database, using the standard KnoxIDF delegation-policy DDL as a representative
4242
* multi-statement, foreign-key-ordered script.
4343
*/
4444
public class KnoxDatabaseTest {
@@ -57,16 +57,16 @@ public class KnoxDatabaseTest {
5757
"DELEGATION_POLICY_RESOURCES",
5858
"DELEGATION_POLICY_RESOURCE_SCOPES");
5959

60-
private static JDBCDataSource dataSource;
60+
private static JdbcDataSource dataSource;
6161
private KnoxDatabase db;
6262

6363
@BeforeClass
6464
public static void setUpClass() {
65-
dataSource = new JDBCDataSource();
66-
dataSource.setDatabaseName("knox_database_test");
65+
dataSource = new JdbcDataSource();
6766
dataSource.setUser(USER);
6867
dataSource.setPassword(PASSWORD);
69-
dataSource.setUrl("jdbc:hsqldb:mem:knoxdatabasetest;sql.syntax_pgs=true"); // sql.syntax_pgs => use postgres syntax
68+
// In-memory H2; DB_CLOSE_DELAY=-1 keeps the database alive across connection open/close for the class.
69+
dataSource.setUrl("jdbc:h2:mem:knoxdatabasetest;DB_CLOSE_DELAY=-1");
7070
}
7171

7272
@Before

gateway-server/src/test/java/org/apache/knox/gateway/services/knoxidf/delegation/DelegationPolicySchemaTest.java

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.commons.io.IOUtils;
2020
import org.apache.knox.gateway.database.AbstractDataSourceFactory;
21+
import org.junit.After;
2122
import org.junit.AfterClass;
2223
import org.junit.BeforeClass;
2324
import org.junit.Test;
@@ -46,31 +47,36 @@ public class DelegationPolicySchemaTest {
4647
private static final String H2_DB = "delegationpolicies";
4748
// H2 is the OOTB embedded backend; DB_CLOSE_DELAY=-1 keeps the in-memory database alive for the class.
4849
private static final String H2_URL = "jdbc:h2:mem:" + H2_DB + ";DB_CLOSE_DELAY=-1";
49-
private static final String HSQL_URL = "jdbc:hsqldb:mem:delegationschema;ifexists=false";
50-
private static final String HSQL_USER = "SA";
51-
private static final String HSQL_PASSWORD = "";
50+
51+
// Child-first so the foreign keys don't block the truncation between tests.
52+
private static final String[] TABLES_CHILD_FIRST = {
53+
"DELEGATION_POLICY_RESOURCE_SCOPES", "DELEGATION_POLICY_RESOURCES",
54+
"DELEGATION_POLICY_GROUPS", "DELEGATION_POLICY_USERS", "DELEGATION_POLICIES"};
5255

5356
private static Connection h2Conn;
54-
private static Connection hsqlConn;
5557

5658
@BeforeClass
5759
public static void setUp() throws Exception {
5860
h2Conn = DriverManager.getConnection(H2_URL);
59-
hsqlConn = DriverManager.getConnection(HSQL_URL, HSQL_USER, HSQL_PASSWORD);
6061
// H2 (the OOTB embedded backend) uses the standard DDL script; IF NOT EXISTS makes it idempotent.
6162
runScript(h2Conn, loadSql(AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL));
62-
// Run standard DDL on HSQLDB
63-
runScript(hsqlConn, loadSql(AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL));
6463
// Verify Oracle DDL script is present on the classpath (execution requires an Oracle-backed test)
6564
loadSql(AbstractDataSourceFactory.ORACLE_KNOXIDF_DELEGATION_POLICY_TABLES_SQL);
6665
}
6766

67+
@After
68+
public void clearTables() throws Exception {
69+
// Reset mutable state so each test starts clean regardless of JUnit method order (notably the
70+
// "empty after DDL" assertion in testAllFiveTablesQueryable).
71+
try (Statement stmt = h2Conn.createStatement()) {
72+
for (String table : TABLES_CHILD_FIRST) {
73+
stmt.execute("DELETE FROM " + table);
74+
}
75+
}
76+
}
77+
6878
@AfterClass
6979
public static void tearDown() throws Exception {
70-
try (Connection conn = DriverManager.getConnection(HSQL_URL, HSQL_USER, HSQL_PASSWORD);
71-
Statement stmt = conn.createStatement()) {
72-
stmt.execute("SHUTDOWN");
73-
}
7480
if (h2Conn != null && !h2Conn.isClosed()) {
7581
try (Statement stmt = h2Conn.createStatement()) {
7682
stmt.execute("DROP ALL OBJECTS");
@@ -80,7 +86,7 @@ public static void tearDown() throws Exception {
8086
}
8187

8288
@Test
83-
public void testH2AllFiveTablesQueryable() throws Exception {
89+
public void testAllFiveTablesQueryable() throws Exception {
8490
for (String table : new String[]{
8591
"DELEGATION_POLICIES", "DELEGATION_POLICY_USERS", "DELEGATION_POLICY_GROUPS",
8692
"DELEGATION_POLICY_RESOURCES", "DELEGATION_POLICY_RESOURCE_SCOPES"}) {
@@ -93,28 +99,15 @@ public void testH2AllFiveTablesQueryable() throws Exception {
9399
}
94100

95101
@Test
96-
public void testHsqlAllFiveTablesQueryable() throws Exception {
97-
for (String table : new String[]{
98-
"DELEGATION_POLICIES", "DELEGATION_POLICY_USERS", "DELEGATION_POLICY_GROUPS",
99-
"DELEGATION_POLICY_RESOURCES", "DELEGATION_POLICY_RESOURCE_SCOPES"}) {
100-
try (Statement stmt = hsqlConn.createStatement();
101-
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + table)) {
102-
assertTrue("Table must be queryable: " + table, rs.next());
103-
assertEquals("Table must be empty after DDL: " + table, 0, rs.getInt(1));
104-
}
105-
}
106-
}
107-
108-
@Test
109-
public void testHsqlStandardSqlIdempotent() throws Exception {
102+
public void testStandardSqlIdempotent() throws Exception {
110103
// Running the script twice must not throw due to IF NOT EXISTS
111-
runScript(hsqlConn, loadSql(AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL));
104+
runScript(h2Conn, loadSql(AbstractDataSourceFactory.KNOXIDF_DELEGATION_POLICY_TABLES_SQL));
112105
}
113106

114107
@Test
115108
public void testDefaultValues() throws Exception {
116109
final String id = UUID.randomUUID().toString();
117-
try (Statement stmt = hsqlConn.createStatement()) {
110+
try (Statement stmt = h2Conn.createStatement()) {
118111
stmt.execute("INSERT INTO DELEGATION_POLICIES "
119112
+ "(registration_id, actor_authority, actor_id, created_at, updated_at) "
120113
+ "VALUES ('" + id + "', 'oidc', 'actor-id', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
@@ -131,21 +124,21 @@ public void testDefaultValues() throws Exception {
131124

132125
@Test
133126
public void testNotNullViolationActorAuthority() throws Exception {
134-
expectConstraintViolation(hsqlConn,
127+
expectConstraintViolation(h2Conn,
135128
"INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
136129
+ "VALUES ('" + UUID.randomUUID() + "', NULL, 'actor-id', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
137130
}
138131

139132
@Test
140133
public void testNotNullViolationActorId() throws Exception {
141-
expectConstraintViolation(hsqlConn,
134+
expectConstraintViolation(h2Conn,
142135
"INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
143136
+ "VALUES ('" + UUID.randomUUID() + "', 'oidc', NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
144137
}
145138

146139
@Test
147140
public void testNotNullViolationCreatedAt() throws Exception {
148-
expectConstraintViolation(hsqlConn,
141+
expectConstraintViolation(h2Conn,
149142
"INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
150143
+ "VALUES ('" + UUID.randomUUID() + "', 'oidc', 'actor-id', NULL, CURRENT_TIMESTAMP)");
151144
}
@@ -154,36 +147,36 @@ public void testNotNullViolationCreatedAt() throws Exception {
154147
public void testUniqueConstraintOnActorAuthorityAndId() throws Exception {
155148
final String id1 = UUID.randomUUID().toString();
156149
final String id2 = UUID.randomUUID().toString();
157-
try (Statement stmt = hsqlConn.createStatement()) {
150+
try (Statement stmt = h2Conn.createStatement()) {
158151
stmt.execute("INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
159152
+ "VALUES ('" + id1 + "', 'oidc', 'duplicateactor', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
160-
expectConstraintViolation(hsqlConn,
153+
expectConstraintViolation(h2Conn,
161154
"INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
162155
+ "VALUES ('" + id2 + "', 'oidc', 'duplicateactor', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
163156
}
164157
}
165158

166159
@Test
167160
public void testFkViolationUsers() throws Exception {
168-
expectConstraintViolation(hsqlConn,
161+
expectConstraintViolation(h2Conn,
169162
"INSERT INTO DELEGATION_POLICY_USERS (registration_id, username) VALUES ('" + UUID.randomUUID() + "', 'alice')");
170163
}
171164

172165
@Test
173166
public void testFkViolationGroups() throws Exception {
174-
expectConstraintViolation(hsqlConn,
167+
expectConstraintViolation(h2Conn,
175168
"INSERT INTO DELEGATION_POLICY_GROUPS (registration_id, group_name) VALUES ('" + UUID.randomUUID() + "', 'admins')");
176169
}
177170

178171
@Test
179172
public void testFkViolationResources() throws Exception {
180-
expectConstraintViolation(hsqlConn,
173+
expectConstraintViolation(h2Conn,
181174
"INSERT INTO DELEGATION_POLICY_RESOURCES (registration_id, resource_uri) VALUES ('" + UUID.randomUUID() + "', '/api/v1')");
182175
}
183176

184177
@Test
185178
public void testFkViolationResourceScopes() throws Exception {
186-
expectConstraintViolation(hsqlConn,
179+
expectConstraintViolation(h2Conn,
187180
"INSERT INTO DELEGATION_POLICY_RESOURCE_SCOPES (registration_id, resource_uri, scope) "
188181
+ "VALUES ('" + UUID.randomUUID() + "', '/api/v1', 'read')");
189182
}

gateway-server/src/test/java/org/apache/knox/gateway/services/knoxidf/trustedoidcissuer/TrustedOidcIssuersSchemaTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,16 @@ public class TrustedOidcIssuersSchemaTest {
4444
private static final String H2_DB = "trustedissuers";
4545
// H2 is the OOTB embedded backend; DB_CLOSE_DELAY=-1 keeps the in-memory database alive for the class.
4646
private static final String H2_URL = "jdbc:h2:mem:" + H2_DB + ";DB_CLOSE_DELAY=-1";
47-
private static final String HSQL_URL = "jdbc:hsqldb:mem:trustedissuersschema;ifexists=false";
48-
private static final String HSQL_USER = "SA";
49-
private static final String HSQL_PASSWORD = "";
5047

5148
private static Connection h2Conn;
52-
private static Connection hsqlConn;
5349

5450
@BeforeClass
5551
public static void setUp() throws SQLException {
5652
h2Conn = DriverManager.getConnection(H2_URL);
57-
hsqlConn = DriverManager.getConnection(HSQL_URL, HSQL_USER, HSQL_PASSWORD);
5853
}
5954

6055
@AfterClass
6156
public static void tearDown() throws Exception {
62-
// HSQLDB: follow JDBCTokenStateServiceTest pattern — new connection for SHUTDOWN
63-
try (Connection conn = DriverManager.getConnection(HSQL_URL, HSQL_USER, HSQL_PASSWORD);
64-
Statement stmt = conn.createStatement()) {
65-
stmt.execute("SHUTDOWN");
66-
}
67-
6857
// H2: drop all objects to release the in-memory database, then close the shared connection.
6958
if (h2Conn != null && !h2Conn.isClosed()) {
7059
try (Statement stmt = h2Conn.createStatement()) {
@@ -96,7 +85,7 @@ public void testH2DdlCreatesTable() throws Exception {
9685
@Test
9786
public void testStandardSqlIdempotent() throws Exception {
9887
String sql = loadSql(AbstractDataSourceFactory.KNOXIDF_TRUSTED_OIDC_ISSUERS_TABLE_SQL);
99-
try (Statement stmt = hsqlConn.createStatement()) {
88+
try (Statement stmt = h2Conn.createStatement()) {
10089
stmt.execute(sql);
10190
// Second execution must succeed due to IF NOT EXISTS
10291
stmt.execute(sql);

gateway-server/src/test/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateServiceTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,18 @@ public class JDBCTokenStateServiceTest {
6565

6666
@ClassRule
6767
public static final TemporaryFolder testFolder = new TemporaryFolder();
68-
public static final String CONNECTION_URL = "jdbc:hsqldb:mem:knox;ifexists=false";
69-
public static final String DB_NAME = "knox";
68+
public static final String CONNECTION_URL = "jdbc:h2:mem:knox;DB_CLOSE_DELAY=-1";
69+
// H2DataSourceFactory builds the URL as "jdbc:h2:" + getDatabaseName(), so the database name
70+
// carries the in-memory location and flags to produce the same URL the test opens directly above.
71+
public static final String DB_NAME = "mem:knox;DB_CLOSE_DELAY=-1";
7072
private static JDBCTokenStateService jdbcTokenStateService;
7173
private static TokenMAC tokenMAC;
7274

7375
@SuppressWarnings("PMD.JUnit4TestShouldUseBeforeAnnotation")
7476
@BeforeClass
7577
public static void setUp() throws Exception {
7678
final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
77-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.HSQL.type()).anyTimes();
79+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.H2.type()).anyTimes();
7880
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(CONNECTION_URL).anyTimes();
7981
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn(DB_NAME).anyTimes();
8082
final AliasService aliasService = EasyMock.createNiceMock(AliasService.class);

gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabaseTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,31 @@
2727
import java.time.Instant;
2828
import java.util.List;
2929

30-
import org.hsqldb.jdbc.JDBCDataSource;
30+
import org.h2.jdbcx.JdbcDataSource;
3131
import org.junit.After;
3232
import org.junit.AfterClass;
3333
import org.junit.Before;
3434
import org.junit.BeforeClass;
3535
import org.junit.Test;
3636

3737
public class RemoteConfigDatabaseTest {
38-
public static final String DB_NAME = "remote_config_test";
3938
public static final String USER = "sa";
4039
public static final String PASSWORD = "";
41-
private static JDBCDataSource dataSource;
40+
private static JdbcDataSource dataSource;
4241
private RemoteConfigDatabase db;
4342

4443
@BeforeClass
4544
public static void setUpClass() throws Exception {
46-
dataSource = new JDBCDataSource();
47-
dataSource.setDatabaseName(DB_NAME);
45+
dataSource = new JdbcDataSource();
4846
dataSource.setUser(USER);
4947
dataSource.setPassword(PASSWORD);
50-
dataSource.setUrl("jdbc:hsqldb:mem:knox;sql.syntax_pgs=true"); // sql.syntax_pgs => use postgres syntax
48+
// In-memory H2; DB_CLOSE_DELAY=-1 keeps the database alive across connection open/close for the class.
49+
dataSource.setUrl("jdbc:h2:mem:knox;DB_CLOSE_DELAY=-1");
5150
}
5251

5352
@Before
5453
public void setUp() throws Exception {
55-
db = new RemoteConfigDatabase(dataSource, "hsql");
54+
db = new RemoteConfigDatabase(dataSource, "h2");
5655
}
5756

5857
@After

0 commit comments

Comments
 (0)