View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   
9   import java.nio.file.PathMatcher;
10  import java.nio.file.Paths;
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import org.junit.jupiter.api.Test;
15  
16  class FileSystemUtilTest {
17  
18      /**
19       * If no configuration is provided (in oshi.properties) then file store is included by default.
20       */
21      @Test
22      void testIsFileStoreIncludedByDefault() {
23          assertThat("file store included by default", !FileSystemUtil.isFileStoreExcluded("/any-path", "/any-volume",
24                  new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()));
25      }
26  
27      /**
28       * Test path includes and excludes.
29       */
30      @Test
31      void testIsFileStoreExcludedSimple() {
32          List<PathMatcher> pathExcludes = FileSystemUtil.parseFileSystemConfig("excluded-path");
33          List<PathMatcher> pathIncludes = FileSystemUtil.parseFileSystemConfig("included-path");
34          List<PathMatcher> volumeExcludes = FileSystemUtil.parseFileSystemConfig("excluded-volume");
35          List<PathMatcher> volumeIncludes = FileSystemUtil.parseFileSystemConfig("included-volume");
36  
37          assertThat("excluded excluded-path", FileSystemUtil.isFileStoreExcluded("excluded-path", "", pathIncludes,
38                  pathExcludes, volumeIncludes, volumeExcludes));
39          assertThat("included included-path", !FileSystemUtil.isFileStoreExcluded("included-path", "", pathIncludes,
40                  pathExcludes, volumeIncludes, volumeExcludes));
41  
42          assertThat("excluded excluded-volume", FileSystemUtil.isFileStoreExcluded("", "excluded-volume", pathIncludes,
43                  pathExcludes, volumeIncludes, volumeExcludes));
44          assertThat("included included-volume", !FileSystemUtil.isFileStoreExcluded("", "included-volume", pathIncludes,
45                  pathExcludes, volumeIncludes, volumeExcludes));
46      }
47  
48      /**
49       * Test that includes has priority over excludes.
50       */
51      @Test
52      void testIsFileStoreExcludedPriority() {
53          List<PathMatcher> pathExcludes = FileSystemUtil.parseFileSystemConfig("path,path-excluded");
54          List<PathMatcher> pathIncludes = FileSystemUtil.parseFileSystemConfig("path");
55          List<PathMatcher> volumeExcludes = FileSystemUtil.parseFileSystemConfig("volume,volume-excluded");
56          List<PathMatcher> volumeIncludes = FileSystemUtil.parseFileSystemConfig("volume");
57  
58          assertThat("excluded path-exclude", FileSystemUtil.isFileStoreExcluded("path-excluded", "", pathIncludes,
59                  pathExcludes, volumeIncludes, volumeExcludes));
60          // "path" is both included and excluded and since included has priority, it
61          // should be included
62          assertThat("included path", !FileSystemUtil.isFileStoreExcluded("path", "", pathIncludes, pathExcludes,
63                  volumeIncludes, volumeExcludes));
64  
65          assertThat("excluded volume-excluded", FileSystemUtil.isFileStoreExcluded("", "volume-excluded", pathIncludes,
66                  pathExcludes, volumeIncludes, volumeExcludes));
67          // "volume" is both included and excluded and since included has priority, it
68          // should be included
69          assertThat("included volume", !FileSystemUtil.isFileStoreExcluded("", "volume", pathIncludes, pathExcludes,
70                  volumeIncludes, volumeExcludes));
71      }
72  
73      @Test
74      void testParseFileSystemConfigSimple() {
75          List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("simple-path");
76          assertThat("simple-path is matched", FileSystemUtil.matches(Paths.get("simple-path"), matchers));
77          assertThat("other-path is not matched", !FileSystemUtil.matches(Paths.get("other-path"), matchers));
78      }
79  
80      @Test
81      void testWithGlobPrefix() {
82          List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("glob:simple-path");
83          assertThat("simple-path is matched", FileSystemUtil.matches(Paths.get("simple-path"), matchers));
84          assertThat("other-path is not matched", !FileSystemUtil.matches(Paths.get("other-path"), matchers));
85      }
86  
87      @Test
88      void testWithMoreItems() {
89          List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("simple-path1,simple-path2,simple-path3");
90          assertThat("simple-path1 is matched", FileSystemUtil.matches(Paths.get("simple-path1"), matchers));
91          assertThat("simple-path2 is matched", FileSystemUtil.matches(Paths.get("simple-path2"), matchers));
92          assertThat("simple-path3 is matched", FileSystemUtil.matches(Paths.get("simple-path3"), matchers));
93          assertThat("other-path is not matched", !FileSystemUtil.matches(Paths.get("other-path"), matchers));
94      }
95  
96      @Test
97      void testWithMultiDirPattern() {
98          List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("**/complex-path");
99          assertThat("/complex-path is matched", FileSystemUtil.matches(Paths.get("/complex-path"), matchers));
100         assertThat("/var/complex-path is matched", FileSystemUtil.matches(Paths.get("/var/complex-path"), matchers));
101         assertThat("other-path is not matched", !FileSystemUtil.matches(Paths.get("other-path"), matchers));
102     }
103 
104     @Test
105     void testWithSuffixPattern() {
106         List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("suffix-path*");
107         assertThat("suffix-path is matched", FileSystemUtil.matches(Paths.get("suffix-path"), matchers));
108         assertThat("suffix-path/ is matched", FileSystemUtil.matches(Paths.get("suffix-path/"), matchers));
109         assertThat("suffix-path1 is matched", FileSystemUtil.matches(Paths.get("suffix-path1"), matchers));
110         assertThat("suffix-path/a is not matched", !FileSystemUtil.matches(Paths.get("suffix-path/a"), matchers));
111         assertThat("suffix-path/a/b/c is not matched",
112                 !FileSystemUtil.matches(Paths.get("suffix-path/a/b/c"), matchers));
113         assertThat("123-suffix-path is not matched", !FileSystemUtil.matches(Paths.get("123-suffix-path"), matchers));
114     }
115 
116     @Test
117     void testWithSuffixMultiDirPattern() {
118         List<PathMatcher> matchers = FileSystemUtil.parseFileSystemConfig("suffix-path/**");
119         assertThat("suffix-path/ is not matched", !FileSystemUtil.matches(Paths.get("suffix-path/"), matchers));
120         assertThat("suffix-path/a is matched", FileSystemUtil.matches(Paths.get("suffix-path/a"), matchers));
121         assertThat("suffix-path/a/b/c is matched", FileSystemUtil.matches(Paths.get("suffix-path/a/b/c"), matchers));
122         assertThat("123-suffix-path is not matched", !FileSystemUtil.matches(Paths.get("123-suffix-path"), matchers));
123     }
124 }