Skip to content

Commit 9158054

Browse files
authored
Directory Visitor Upgrade (#3)
Documentation: * Create README.md Tests: * Update DirectoryVisitorTest - add class members describing the number of files and directories in the test directory, add test case, add test cases Visitors: * Rename SingleLevelFileVisitor to DirectoryVisitor * Rename SingleLevelFileVisitorTest to DirectoryVisitorTest * Update DirectoryVisitor - add method getFilePaths, improve method getFilePaths, add method getDirectoryPaths * Update DirectoryVisitorTest - add test case
1 parent f1090c3 commit 9158054

File tree

4 files changed

+203
-95
lines changed

4 files changed

+203
-95
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Files - JVM
2+
A repository of tools for working with Files on the JVM (Java Virtual Machine).
3+
4+
### Getting Started
5+
Each Module is available as an independent downloadable package that can be quickly imported using the GitHub Maven package registry.
6+
7+
You can import any module into your project using Gradle or maven.
8+
9+
Here is how to implement a package using Gradle:
10+
11+
1. Add this to your dependencies block:
12+
`implementation "io.github.dk96-os.files-jvm:visitors:$package_version"`
13+
14+
2. Add the GitHub packages maven repository.
15+
16+
### Visitors Module
17+
The concept of a File Visitor is built into the Java Standard Library.
18+
For more information on the Java Standard Library, see: [java.nio.file.FileVisitor](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/FileVisitor.html)
19+
20+
#### Directory Visitor
21+
The more important class in the Visitors module is the `Directory Visitor`.
22+
This class requires a Path to be given in the constructor.
23+
The path should be a valid directory, otherwise the `isValidDirectory` flag will be false, and all methods will return empty lists.
24+
25+
When the given Path is a valid directory, a `Simplified File Visitor` is created that collects the names of files and subdirectories that are the direct descendants (only one level deep).
26+
The names are stored for the lifetime of the `Directory Visitor`, and can be accessed as a list of Files, or a list of Directories.
27+
There are convenient methods available that can map the names into Lists of Path objects.
28+
29+
#### Simplified File Visitor
30+
Within the Visitors module, you will also find a `Simplified File Visitor`.
31+
This is an abstract class that provides reasonable default method implementations for most of the required `FileVisitor` methods.
32+
It is intended to make other classes in the Visitor Module easier to read by applying the default method implementations for `FileVisitor`.
33+
34+

visitors/src/main/java/files/jvm/visitors/SingleLevelFileVisitor.java renamed to visitors/src/main/java/files/jvm/visitors/DirectoryVisitor.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import java.util.Set;
1212

1313
/** Visits Files and Directories in a single level,
14-
* records the files and directories that were found in a list.
14+
* records the files and directories that were found.
1515
*/
16-
public class SingleLevelFileVisitor {
16+
public class DirectoryVisitor {
1717

1818
/** Whether the Path given in the constructor is a valid directory.
1919
*/
@@ -23,13 +23,16 @@ public class SingleLevelFileVisitor {
2323

2424
private final List<String> mFiles;
2525

26+
private final Path mPath;
27+
2628
/** Constructor.
2729
* @param path The path to visit.
2830
* @throws IOException Any Exception thrown while Visiting the Path.
2931
*/
30-
public SingleLevelFileVisitor(
32+
public DirectoryVisitor(
3133
final Path path
3234
) throws IOException {
35+
mPath = path;
3336
if (!Files.isDirectory(path)) {
3437
isValidDirectory = false;
3538
mDirectories = null;
@@ -90,4 +93,28 @@ public List<String> getFiles() {
9093
return mFiles;
9194
}
9295

96+
/** Obtain all files in this directory as a List of Paths.
97+
* @return A List of Paths, or an empty list.
98+
*/
99+
public List<Path> getFilePaths() {
100+
if (mFiles == null)
101+
return Collections.emptyList();
102+
return mFiles
103+
.parallelStream()
104+
.map(mPath::resolve)
105+
.toList();
106+
}
107+
108+
/** Obtain all Directories in this directory as a List of Paths.
109+
* @return A List of Paths, or an empty list.
110+
*/
111+
public List<Path> getDirectoryPaths() {
112+
if (mDirectories == null)
113+
return Collections.emptyList();
114+
return mDirectories
115+
.parallelStream()
116+
.map(mPath::resolve)
117+
.toList();
118+
}
119+
93120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package files.jvm.visitors;
2+
3+
import static junit.framework.TestCase.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Path;
12+
import java.util.List;
13+
14+
/** Testing SingleLevelFileVisitor class.
15+
*/
16+
public final class DirectoryVisitorTest {
17+
18+
private final Path testProjectDirectory = Path.of("test_directory");
19+
20+
private final int numberOfSubDirectories = 4;
21+
22+
private final int numberOfFiles = 5;
23+
24+
private DirectoryVisitor mInstance;
25+
26+
@Before
27+
public void testSetup() throws IOException {
28+
mInstance = new DirectoryVisitor(testProjectDirectory);
29+
}
30+
31+
@Test
32+
public void testInitialCondition() {
33+
assertTrue(
34+
mInstance.isValidDirectory
35+
);
36+
assertEquals(
37+
numberOfSubDirectories,
38+
mInstance.getDirectories().size()
39+
);
40+
assertEquals(
41+
numberOfFiles,
42+
mInstance.getFiles().size()
43+
);
44+
}
45+
46+
@Test
47+
public void testConstructor_FilePath_NotValidDirectory() throws IOException {
48+
final Path filePath = Path.of("test_project/build.gradle");
49+
mInstance = new DirectoryVisitor(filePath);
50+
assertFalse(
51+
mInstance.isValidDirectory
52+
);
53+
assertEquals(
54+
0,
55+
mInstance.getDirectories().size()
56+
);
57+
assertEquals(
58+
0,
59+
mInstance.getFiles().size()
60+
);
61+
}
62+
63+
@Test
64+
public void testConstructor_InvalidPath_Fails() throws IOException {
65+
final Path invalidPath = Path.of("invalid_path");
66+
mInstance = new DirectoryVisitor(invalidPath);
67+
assertFalse(
68+
mInstance.isValidDirectory
69+
);
70+
assertEquals(
71+
0,
72+
mInstance.getDirectories().size()
73+
);
74+
assertEquals(
75+
0,
76+
mInstance.getFiles().size()
77+
);
78+
}
79+
80+
@Test
81+
public void testConstructor_SampleDirectory_OnlyContainsSingleFile() throws IOException {
82+
final Path testSampleDirectory = testProjectDirectory.resolve("samples");
83+
mInstance = new DirectoryVisitor(testSampleDirectory);
84+
assertTrue(
85+
mInstance.isValidDirectory
86+
);
87+
assertEquals(
88+
0,
89+
mInstance.getDirectories().size()
90+
);
91+
assertEquals(
92+
1,
93+
mInstance.getFiles().size()
94+
);
95+
}
96+
97+
@Test
98+
public void testGetFilePaths_CorrectSize() {
99+
final List<Path> pathList = mInstance.getFilePaths();
100+
assertEquals(
101+
numberOfFiles,
102+
pathList.size()
103+
);
104+
}
105+
106+
@Test
107+
public void testGetFilePaths_InvalidDirectory_ReturnsEmptyList() throws IOException {
108+
final Path invalidPath = Path.of("invalid_path");
109+
mInstance = new DirectoryVisitor(invalidPath);
110+
assertFalse(
111+
mInstance.isValidDirectory
112+
);
113+
assertEquals(
114+
0, mInstance.getFilePaths().size()
115+
);
116+
}
117+
118+
@Test
119+
public void testGetDirectoryPaths_CorrectSize() {
120+
final List<Path> pathList = mInstance.getDirectoryPaths();
121+
assertEquals(
122+
numberOfSubDirectories,
123+
pathList.size()
124+
);
125+
}
126+
127+
@Test
128+
public void testGetDirectoryPaths_InvalidPath_ReturnsEmptyList() throws IOException {
129+
final Path invalidPath = Path.of("invalid_path");
130+
mInstance = new DirectoryVisitor(invalidPath);
131+
assertFalse(
132+
mInstance.isValidDirectory
133+
);
134+
assertEquals(
135+
0, mInstance.getDirectoryPaths().size()
136+
);
137+
}
138+
139+
}

visitors/src/test/java/files/jvm/visitors/SingleLevelFileVisitorTest.java

-92
This file was deleted.

0 commit comments

Comments
 (0)