Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8299] fix: validate null/blank prefix in RandomNameUtils.genRandomName (#8317)

Kwon Taeheon85bee783a94d
diff --git a/common/src/main/java/org/apache/gravitino/utils/RandomNameUtils.java b/common/src/main/java/org/apache/gravitino/utils/RandomNameUtils.java
index 3a0fca9420..2aa104c52c 100644
--- a/common/src/main/java/org/apache/gravitino/utils/RandomNameUtils.java
+++ b/common/src/main/java/org/apache/gravitino/utils/RandomNameUtils.java
@@ -20,6 +20,7 @@
package org.apache.gravitino.utils;
import java.util.UUID;
+import org.apache.commons.lang3.StringUtils;
/** Tools to generate random values. */
public class RandomNameUtils {
@@ -34,6 +35,9 @@ public class RandomNameUtils {
* @return A random string value.
*/
public static String genRandomName(String prefix) {
+ if (StringUtils.isBlank(prefix)) {
+ throw new IllegalArgumentException("Prefix cannot be null or empty");
+ }
return prefix + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8);
}
}
diff --git a/common/src/test/java/org/apache/gravitino/utils/TestRandomNameUtils.java b/common/src/test/java/org/apache/gravitino/utils/TestRandomNameUtils.java
new file mode 100644
index 0000000000..f0f3fc19f3
--- /dev/null
+++ b/common/src/test/java/org/apache/gravitino/utils/TestRandomNameUtils.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.gravitino.utils;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class TestRandomNameUtils {
+ @Test
+ public void testGeneratedNameStartsWithPrefix() {
+ String prefix = "testPrefix";
+ String result = RandomNameUtils.genRandomName(prefix);
+
+ Assertions.assertTrue(result.startsWith(prefix + "_"));
+ }
+
+ @Test
+ public void testNullPrefixThrowsIllegalArgumentException() {
+ Assertions.assertThrowsExactly(
+ IllegalArgumentException.class, () -> RandomNameUtils.genRandomName(null));
+ }
+
+ @Test
+ public void testBlankPrefixThrowsIllegalArgumentException() {
+ Assertions.assertThrowsExactly(
+ IllegalArgumentException.class, () -> RandomNameUtils.genRandomName(" "));
+ }
+}