ctubbsii commented on code in PR #3549:
URL: https://github.com/apache/accumulo/pull/3549#discussion_r1244455624
##########
core/src/main/java/org/apache/accumulo/core/metadata/ValidationUtil.java:
##########
@@ -62,9 +62,12 @@ public static void validateRFileName(String fileName) {
public static void validateFileName(String fileName) {
Objects.requireNonNull(fileName);
- if (!fileName.matches("[\\dA-Za-z._-]+")) {
- throw new IllegalArgumentException(
- "Provided filename (" + fileName + ") contains invalid characters.");
+ for (int i = 0; i < fileName.length(); i++) {
+ final char ch = fileName.charAt(i);
+ if (!(Character.isLetterOrDigit(ch) || ch == '-' || ch == '.' || ch ==
'_')) {
+ throw new IllegalArgumentException(
+ "Provided filename (" + fileName + ") contains invalid
characters.");
+ }
Review Comment:
This would separate out the predicate, and I think makes it a bit more
readable instead of constantly calling "charAt" and worrying about off-by-one
errors on the loop variable.
```suggestion
IntPredicate validFileChar = ch -> Character.isLetterOrDigit(ch) || ch
== '-' || ch == '.' || ch == '_';
if (!fileName.codePoints().allMatch(validFileChar)) {
throw new IllegalArgumentException(
"Provided filename (" + fileName + ") contains invalid
characters.");
}
```
##########
core/src/test/java/org/apache/accumulo/core/util/ValidationUtilTest.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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
+ *
+ * https://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.accumulo.core.util;
+
+import static
org.apache.accumulo.core.metadata.ValidationUtil.validateFileName;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import org.junit.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class ValidationUtilTest {
+ @ParameterizedTest
+ @ValueSource(strings = {"F0001acd.rf", "F0001acd.rf_tmp"})
+ public void testValidateFileNameSuccess(String value) {
+ validateFileName(value);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings =
{"hdfs://nn:8020/accumulo/tables/2/default_tablet/F0001acd.rf",
+ "./F0001acd.rf", "/F0001acd.rf"})
+ public void testValidateFileNameException(String value) {
+ Exception ex = assertThrows(IllegalArgumentException.class, () ->
validateFileName(value));
+ assertEquals("Provided filename (" + value + ") contains invalid
characters.", ex.getMessage());
+ }
Review Comment:
Parameterized tests are overkill here. It's more readable, and less overhead
(less test code to maintain, and update across JUnit versions), if you just did:
```java
Set.of(item1, item2, item3).forEach(value -> {
assertEquals(...);
});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]