1
1
package org .cryptomator .cryptofs ;
2
2
3
+ import org .hamcrest .CoreMatchers ;
4
+ import org .hamcrest .MatcherAssert ;
3
5
import org .junit .jupiter .api .Assertions ;
4
6
import org .junit .jupiter .api .BeforeEach ;
7
+ import org .junit .jupiter .api .DisplayName ;
5
8
import org .junit .jupiter .api .Test ;
9
+ import org .junit .jupiter .params .ParameterizedTest ;
10
+ import org .junit .jupiter .params .provider .CsvSource ;
11
+ import org .mockito .Mockito ;
6
12
7
13
import java .io .IOException ;
14
+ import java .nio .file .AccessDeniedException ;
15
+ import java .nio .file .AccessMode ;
8
16
import java .nio .file .FileStore ;
9
17
import java .nio .file .FileSystem ;
10
18
import java .nio .file .Path ;
@@ -19,79 +27,62 @@ public class ReadonlyFlagTest {
19
27
private FileStore fileStore = mock (FileStore .class );
20
28
private FileSystemProvider provider = mock (FileSystemProvider .class );
21
29
private FileSystem fileSystem = mock (FileSystem .class );
22
- private Path path = mock (Path .class );
30
+ private Path path = mock (Path .class , "test-path" );
23
31
24
32
private CryptoFileSystemProperties properties = mock (CryptoFileSystemProperties .class );
25
33
26
- private ReadonlyFlag inTest ;
27
-
28
34
@ BeforeEach
29
35
public void setup () throws IOException {
30
36
when (path .getFileSystem ()).thenReturn (fileSystem );
31
37
when (fileSystem .provider ()).thenReturn (provider );
32
38
when (provider .getFileStore (path )).thenReturn (fileStore );
33
39
}
34
40
35
- @ Test
36
- public void testReadonlyFlagIsSetIfReadonlyIsSetOnProperties () throws IOException {
37
- when (properties .readonly ()).thenReturn (true );
38
-
39
- inTest = new ReadonlyFlag (properties , path );
40
-
41
- Assertions .assertTrue (inTest .isSet ());
42
- }
43
-
44
- @ Test
45
- public void testReadonlyFlagIsSetIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsReadonly () throws IOException {
46
- when (properties .readonly ()).thenReturn (false );
47
- when (fileStore .isReadOnly ()).thenReturn (true );
48
-
49
- inTest = new ReadonlyFlag (properties , path );
50
-
51
- Assertions .assertTrue (inTest .isSet ());
52
- }
53
-
54
- @ Test
55
- public void testReadonlyFlagIsNotSetIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsNotReadonly () throws IOException {
56
- when (properties .readonly ()).thenReturn (false );
57
- when (fileStore .isReadOnly ()).thenReturn (false );
58
-
59
- inTest = new ReadonlyFlag (properties , path );
60
-
61
- Assertions .assertFalse (inTest .isSet ());
41
+ @ DisplayName ("isSet()" )
42
+ @ ParameterizedTest (name = "readonlyFlag: {0}, writeProtected: {1} -> mounted readonly {2}" )
43
+ @ CsvSource ({
44
+ "false, false, false" ,
45
+ "true, false, true" ,
46
+ "false, true, true" ,
47
+ "true, true, true" ,
48
+ })
49
+ public void testIsSet (boolean readonlyFlag , boolean writeProtected , boolean expectedResult ) throws IOException {
50
+ when (properties .readonly ()).thenReturn (readonlyFlag );
51
+ if (writeProtected ) {
52
+ Mockito .doThrow (new AccessDeniedException (path .toString ())).when (provider ).checkAccess (path , AccessMode .WRITE );
53
+ }
54
+ ReadonlyFlag inTest = new ReadonlyFlag (properties , path );
55
+
56
+ boolean result = inTest .isSet ();
57
+
58
+ MatcherAssert .assertThat (result , CoreMatchers .is (expectedResult ));
59
+ Assertions .assertEquals (expectedResult , result );
62
60
}
63
61
64
- @ Test
65
- public void testAssertWritableThrowsIOExceptionIfReadonlyIsSetOnProperties () throws IOException {
66
- when (properties .readonly ()).thenReturn (true );
67
-
68
- inTest = new ReadonlyFlag (properties , path );
69
-
70
- Assertions .assertThrows (ReadOnlyFileSystemException .class , () -> {
71
- inTest .assertWritable ();
72
- });
73
- }
74
-
75
- @ Test
76
- public void testAssertWritableThrowsIOExceptionIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsReadonly () throws IOException {
77
- when (properties .readonly ()).thenReturn (false );
78
- when (fileStore .isReadOnly ()).thenReturn (true );
79
-
80
- inTest = new ReadonlyFlag (properties , path );
81
-
82
- Assertions .assertThrows (ReadOnlyFileSystemException .class , () -> {
83
- inTest .assertWritable ();
84
- });
85
- }
86
-
87
- @ Test
88
- public void testAssertWritableDoesNotThrowIOExceptionIfReadonlyIsNotSetOnPropertiesAndFilestoreOfVaultIsNotReadonly () throws IOException {
89
- when (properties .readonly ()).thenReturn (false );
90
- when (fileStore .isReadOnly ()).thenReturn (false );
91
-
92
- inTest = new ReadonlyFlag (properties , path );
93
-
94
- inTest .assertWritable ();
62
+ @ DisplayName ("assertWritable()" )
63
+ @ ParameterizedTest (name = "readonlyFlag: {0}, writeProtected: {1} -> mounted readonly {2}" )
64
+ @ CsvSource ({
65
+ "false, false, false" ,
66
+ "true, false, true" ,
67
+ "false, true, true" ,
68
+ "true, true, true" ,
69
+ })
70
+ public void testAssertWritable (boolean readonlyFlag , boolean writeProtected , boolean expectedResult ) throws IOException {
71
+ when (properties .readonly ()).thenReturn (readonlyFlag );
72
+ if (writeProtected ) {
73
+ Mockito .doThrow (new AccessDeniedException (path .toString ())).when (provider ).checkAccess (path , AccessMode .WRITE );
74
+ }
75
+ ReadonlyFlag inTest = new ReadonlyFlag (properties , path );
76
+
77
+ if (expectedResult ) {
78
+ Assertions .assertThrows (ReadOnlyFileSystemException .class , () -> {
79
+ inTest .assertWritable ();
80
+ });
81
+ } else {
82
+ Assertions .assertDoesNotThrow (() -> {
83
+ inTest .assertWritable ();
84
+ });
85
+ }
95
86
}
96
87
97
88
}
0 commit comments