ARTEMIS-4965 mitigate NPE when missing SQL property

This commit is contained in:
Justin Bertram 2024-08-29 15:19:58 -05:00 committed by Robbie Gemmell
parent eb19d586fd
commit 1503a40fe7
3 changed files with 71 additions and 5 deletions

View File

@ -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;
}

View File

@ -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));
}
}
}

View File

@ -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);
}