ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222654083


##########
flink-connectors/flink-connector-files/src/test/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumeratorTest.java:
##########
@@ -0,0 +1,247 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.connector.file.src.testutils.TestingFileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.assertSplitsEqual;
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.toPaths;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Unit tests for the {@link NonSplittingRegexEnumerator}. */
+public class NonSplittingRegexEnumeratorTest {
+    /**
+     * Testing file system reference, to be cleaned up in an @After method. 
That way it also gets
+     * cleaned up on a test failure, without needing finally clauses in every 
test.
+     */
+    protected TestingFileSystem testFs;
+
+    @AfterEach
+    void unregisterTestFs() throws Exception {
+        if (testFs != null) {
+            testFs.unregister();
+        }
+    }
+
+    // ------------------------------------------------------------------------
+
+    @Test
+    void testIncludeSingleFile() throws Exception {
+        final Path[] testPaths =
+                new Path[] {
+                    new Path("testfs:///dir/file1"),
+                    new Path("testfs:///dir/nested/file.out"),
+                    new Path("testfs:///dir/nested/anotherfile.txt")
+                };
+        testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+        testFs.register();
+
+        Path baseDIr = new Path("testfs:///dir");
+        final NonSplittingRegexEnumerator enumerator =
+                createEnumerator(baseDIr.getPath() + "/nested/file.out");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+        assertThat(toPaths(splits)).containsExactlyInAnyOrder(testPaths[1]);
+    }
+
+    @Test
+    void testIncludeFilesFromRegexDirectory() throws Exception {
+        final Path[] testPaths =
+                new Path[] {
+                    new Path("testfs:///dir/file1"),
+                    new Path("testfs:///dir/nested/file.out"),
+                    new Path("testfs:///dir/nested/anotherFile.txt"),
+                    new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt")
+                };
+        testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+        testFs.register();
+
+        Path baseDIr = new Path("testfs:///dir");
+        final NonSplittingRegexEnumerator enumerator =
+                createEnumerator(baseDIr.getPath() + "/nest.[a-z]");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+        assertThat(toPaths(splits))
+                .containsExactlyInAnyOrder(Arrays.copyOfRange(testPaths, 1, 
testPaths.length));
+    }
+
+    @Test
+    void testIncludeSingleFileFromMultiDirectory() throws Exception {
+        final Path[] testPaths =
+                new Path[] {
+                    new Path("testfs:///dir/file1"),
+                    new Path("testfs:///dir/nested/file.out"),
+                    new Path("testfs:///dir/nested/anotherFile.txt"),
+                    new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt"),
+                    new Path("testfs:///dir/anotherNested/file.out"),
+                    new Path("testfs:///dir/anotherNested/nested/file.out"),
+                };
+        testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+        testFs.register();
+
+        Path baseDIr = new Path("testfs:///dir");
+        final NonSplittingRegexEnumerator enumerator =
+                createEnumerator(baseDIr.getPath() + "/.*/file.out");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+        assertThat(toPaths(splits))
+                .containsExactlyInAnyOrder(
+                        Arrays.stream(testPaths)
+                                .filter(p -> p.getPath().endsWith("file.out"))
+                                .toArray(Path[]::new));
+    }
+
+    @Test
+    void testDefaultHiddenFilesFilter() throws Exception {
+        final Path[] testPaths =
+                new Path[] {
+                    new Path("testfs:///visiblefile"),
+                    new Path("testfs:///.hiddenfile1"),
+                    new Path("testfs:///_hiddenfile2")
+                };
+        testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+        testFs.register();
+
+        Path baseDIr = new Path("testfs:///");
+        final NonSplittingRegexEnumerator enumerator = createEnumerator("/.*");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+        
assertThat(toPaths(splits)).isEqualTo(Collections.singletonList(testPaths[0]));
+    }
+
+    @Test
+    void testHiddenDirectories() throws Exception {
+        final Path[] testPaths =
+                new Path[] {
+                    new Path("testfs:///dir/visiblefile"),
+                    new Path("testfs:///dir/.hiddendir/file"),
+                    new Path("testfs:///_notvisible/afile")
+                };
+        testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+        testFs.register();
+
+        Path baseDIr = new Path("testfs:///");
+        final NonSplittingRegexEnumerator enumerator = createEnumerator("/.*");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+        
assertThat(toPaths(splits)).isEqualTo(Collections.singletonList(testPaths[0]));
+    }
+
+    @Test
+    void testFilesWithNoBlockInfo() throws Exception {
+        final Path testPath = new Path("testfs:///dir/file1");
+        testFs =
+                TestingFileSystem.createForFileStatus(
+                        "testfs",
+                        
TestingFileSystem.TestFileStatus.forFileWithBlocks(testPath, 12345L));
+        testFs.register();
+
+        final NonSplittingRegexEnumerator enumerator = 
createEnumerator("/.*/file.");
+        final Collection<FileSourceSplit> splits =
+                enumerator.enumerateSplits(new Path[] {new 
Path("testfs:///dir")}, 0);
+
+        assertThat(splits).hasSize(1);
+        assertSplitsEqual(
+                new FileSourceSplit("ignoredId", testPath, 0L, 12345L, 0, 
12345L),
+                splits.iterator().next());
+    }
+
+    @Test
+    void testFileWithIncorrectBlocks() throws Exception {

Review Comment:
   This test also lies in the `NonSplittingRecursiveEnumeratorTest`.
   I think `NonSplittingRecursiveEnumerator` and `NonSplittingRegexEnumerator` 
could also handle the files with blocks.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to