11package org .cryptomator .cryptofs ;
22
3+ import org .hamcrest .CoreMatchers ;
4+ import org .hamcrest .MatcherAssert ;
35import org .junit .jupiter .api .Assertions ;
46import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .DisplayName ;
58import 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 ;
612
713import java .io .IOException ;
14+ import java .nio .file .AccessDeniedException ;
15+ import java .nio .file .AccessMode ;
816import java .nio .file .FileStore ;
917import java .nio .file .FileSystem ;
1018import java .nio .file .Path ;
@@ -19,79 +27,62 @@ public class ReadonlyFlagTest {
1927 private FileStore fileStore = mock (FileStore .class );
2028 private FileSystemProvider provider = mock (FileSystemProvider .class );
2129 private FileSystem fileSystem = mock (FileSystem .class );
22- private Path path = mock (Path .class );
30+ private Path path = mock (Path .class , "test-path" );
2331
2432 private CryptoFileSystemProperties properties = mock (CryptoFileSystemProperties .class );
2533
26- private ReadonlyFlag inTest ;
27-
2834 @ BeforeEach
2935 public void setup () throws IOException {
3036 when (path .getFileSystem ()).thenReturn (fileSystem );
3137 when (fileSystem .provider ()).thenReturn (provider );
3238 when (provider .getFileStore (path )).thenReturn (fileStore );
3339 }
3440
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 );
6260 }
6361
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+ }
9586 }
9687
9788}
0 commit comments