From 90f231ad564cb47b07a5b6e80a3da28f6e693f3a Mon Sep 17 00:00:00 2001 From: Yifan Cai Date: Mon, 22 Sep 2025 15:57:25 -0700 Subject: [PATCH] Fixed incorrect error message constant for keyspace name length validation patch by Yifan Cai; reviewed by Maxwell Guo, Stefan Miklosovic for CASSANDRA-20915 --- CHANGES.txt | 1 + .../cassandra/schema/KeyspaceMetadata.java | 3 +- .../schema/KeyspaceMetadataTest.java | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/unit/org/apache/cassandra/schema/KeyspaceMetadataTest.java diff --git a/CHANGES.txt b/CHANGES.txt index 93b8cdc0f4..57e633f2a4 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 4.0.19 + * Fixed incorrect error message constant for keyspace name length validation (CASSANDRA-20915) * update shaded cassandra-driver-core to 3.11.5 (CASSANDRA-20904) * Prevent too long table names not fitting file names (CASSANDRA-20389) * Fix IndexOutOfBoundsException in sstablemetadata tool when a range tombstone is a max clustering value (CASSANDRA-20855) diff --git a/src/java/org/apache/cassandra/schema/KeyspaceMetadata.java b/src/java/org/apache/cassandra/schema/KeyspaceMetadata.java index e8a033b7a2..ad9d7579ac 100644 --- a/src/java/org/apache/cassandra/schema/KeyspaceMetadata.java +++ b/src/java/org/apache/cassandra/schema/KeyspaceMetadata.java @@ -45,7 +45,6 @@ import org.apache.cassandra.service.StorageService; import static java.lang.String.format; import static com.google.common.collect.Iterables.any; -import static org.apache.cassandra.schema.SchemaConstants.TABLE_NAME_LENGTH; /** * An immutable representation of keyspace metadata (name, params, tables, types, and functions). @@ -66,7 +65,7 @@ public final class KeyspaceMetadata implements SchemaElement keyspaceName)); if (keyspaceName.length() > SchemaConstants.NAME_LENGTH) throw exceptionBuilder.apply(format("Keyspace name must not be more than %d characters long (got %d characters for \"%s\")", - TABLE_NAME_LENGTH, keyspaceName.length(), keyspaceName)); + SchemaConstants.NAME_LENGTH, keyspaceName.length(), keyspaceName)); } public enum Kind diff --git a/test/unit/org/apache/cassandra/schema/KeyspaceMetadataTest.java b/test/unit/org/apache/cassandra/schema/KeyspaceMetadataTest.java new file mode 100644 index 0000000000..4cf14fbf18 --- /dev/null +++ b/test/unit/org/apache/cassandra/schema/KeyspaceMetadataTest.java @@ -0,0 +1,45 @@ +/* + * 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.cassandra.schema; + +import org.junit.Test; + +import org.apache.cassandra.exceptions.ConfigurationException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class KeyspaceMetadataTest +{ + @Test + public void testValidateKeyspaceNameTooLongShowsCorrectLimit() + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SchemaConstants.NAME_LENGTH + 1; i++) + sb.append('a'); + + String longKeyspaceName = sb.toString(); + String expectedMessage = String.format("Keyspace name must not be more than %d characters long (got %d characters for \"%s\")", + SchemaConstants.NAME_LENGTH, + longKeyspaceName.length(), + longKeyspaceName); + + assertThatThrownBy(() -> KeyspaceMetadata.validateKeyspaceName(longKeyspaceName, ConfigurationException::new)) + .isInstanceOf(ConfigurationException.class) + .hasMessage(expectedMessage); + } +}