From 1503a40fe7c4013fc1a2965f805572ec96f20bec Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Thu, 29 Aug 2024 15:19:58 -0500 Subject: [PATCH] ARTEMIS-4965 mitigate NPE when missing SQL property --- .../jdbc/store/sql/PropertySQLProvider.java | 18 ++++-- .../store/sql/PropertySQLProviderTest.java | 55 +++++++++++++++++++ .../journal/ActiveMQJournalBundle.java | 3 + 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProviderTest.java diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java index 0201c7e8b5..1514c10408 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java @@ -28,6 +28,7 @@ import java.util.function.Function; import java.util.stream.Stream; import org.apache.activemq.artemis.jdbc.store.drivers.JDBCConnectionProvider; +import org.apache.activemq.artemis.journal.ActiveMQJournalBundle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.invoke.MethodHandles; @@ -78,7 +79,7 @@ public class PropertySQLProvider implements SQLProvider { protected PropertySQLProvider(Factory.SQLDialect dialect, String tableName, Properties sqlProperties) { this.dialect = dialect; this.sql = sqlProperties; - final LetterCase tableNamesCase = LetterCase.parse(sql("table-names-case", dialect, sqlProperties)); + final LetterCase tableNamesCase = LetterCase.parse(sql("table-names-case", dialect, sqlProperties, true)); this.tableName = tableNamesCase.apply(tableName); } @@ -266,7 +267,7 @@ public class PropertySQLProvider implements SQLProvider { @Override public String currentTimestampTimeZoneId() { - return sql("current-timestamp-timezone-id"); + return sql("current-timestamp-timezone-id", false); } @Override @@ -294,11 +295,15 @@ public class PropertySQLProvider implements SQLProvider { return format(sql("initialize-nodeId"), tableName, NODE_ID_ROW_ID); } - private String sql(final String key) { - return sql(key, dialect, sql); + protected String sql(final String key) { + return sql(key, true); } - private static String sql(final String key, final Factory.SQLDialect dialect, final Properties sql) { + protected String sql(final String key, final boolean checkNull) { + return sql(key, dialect, sql, checkNull); + } + + private static String sql(final String key, final Factory.SQLDialect dialect, final Properties sql, final boolean checkNull) { if (dialect != null) { String result = sql.getProperty(key + "." + dialect.getKey()); if (result != null) { @@ -306,6 +311,9 @@ public class PropertySQLProvider implements SQLProvider { } } String result = sql.getProperty(key); + if (checkNull && result == null) { + throw ActiveMQJournalBundle.BUNDLE.propertyNotFound(key, dialect != null ? dialect.toString() : null); + } return result; } diff --git a/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProviderTest.java b/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProviderTest.java new file mode 100644 index 0000000000..1762e10f95 --- /dev/null +++ b/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProviderTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.artemis.jdbc.store.sql; + +import org.apache.activemq.artemis.tests.util.ArtemisTestCase; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; + +public class PropertySQLProviderTest extends ArtemisTestCase { + + @Test + public void testGetProperty() { + for (SQLProvider.DatabaseStoreType storeType : SQLProvider.DatabaseStoreType.values()) { + PropertySQLProvider factory = (PropertySQLProvider) new PropertySQLProvider.Factory((PropertySQLProvider.Factory.SQLDialect) null).create("who-cares", storeType); + factory.sql("create-file-table"); + } + } + + @Test + public void testGetMissingProperty() { + for (SQLProvider.DatabaseStoreType storeType : SQLProvider.DatabaseStoreType.values()) { + PropertySQLProvider factory = (PropertySQLProvider) new PropertySQLProvider.Factory((PropertySQLProvider.Factory.SQLDialect) null).create("who-cares", storeType); + try { + factory.sql("missing-property"); + fail(); + } catch (IllegalStateException e) { + // expected + } + } + } + + @Test + public void testGetMissingPropertyNoCheck() { + for (SQLProvider.DatabaseStoreType storeType : SQLProvider.DatabaseStoreType.values()) { + PropertySQLProvider factory = (PropertySQLProvider) new PropertySQLProvider.Factory((PropertySQLProvider.Factory.SQLDialect) null).create("who-cares", storeType); + assertNull(factory.sql("missing-property", false)); + } + } +} diff --git a/artemis-journal/src/main/java/org/apache/activemq/artemis/journal/ActiveMQJournalBundle.java b/artemis-journal/src/main/java/org/apache/activemq/artemis/journal/ActiveMQJournalBundle.java index 92ac8415b9..369fdd49d5 100644 --- a/artemis-journal/src/main/java/org/apache/activemq/artemis/journal/ActiveMQJournalBundle.java +++ b/artemis-journal/src/main/java/org/apache/activemq/artemis/journal/ActiveMQJournalBundle.java @@ -53,4 +53,7 @@ public interface ActiveMQJournalBundle { @Message(id = 149007, value = "Thread dump being generated as the asynchronous file.open is not responding fast enough.") String threadDumpAfterFileOpenTimeout(); + @Message(id = 149008, value = "Property '{}' not found for dialect '{}'") + IllegalStateException propertyNotFound(String property, String dialect); + }