Skip to content

Commit 2d47442

Browse files
infeocoderabbitai[bot]overheadhunter
authored
Make DosFileAttributes immutable (#305)
references #42 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Sebastian Stenzel <[email protected]>
1 parent 75c3478 commit 2d47442

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

src/main/java/org/cryptomator/cryptofs/attr/CryptoDosFileAttributes.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,41 @@
1919

2020
final class CryptoDosFileAttributes extends CryptoBasicFileAttributes implements DosFileAttributes {
2121

22-
private final boolean readonlyFileSystem;
23-
private final DosFileAttributes delegate;
24-
25-
public CryptoDosFileAttributes(DosFileAttributes delegate, CiphertextFileType ciphertextFileType, Path ciphertextPath, Cryptor cryptor, Optional<OpenCryptoFile> openCryptoFile, CryptoFileSystemProperties fileSystemProperties) {
22+
private final boolean isReadOnly;
23+
private final boolean isArchive;
24+
private final boolean isHidden;
25+
private final boolean isSystem;
26+
27+
public CryptoDosFileAttributes(DosFileAttributes delegate, //
28+
CiphertextFileType ciphertextFileType, //
29+
Path ciphertextPath, //
30+
Cryptor cryptor, //
31+
Optional<OpenCryptoFile> openCryptoFile, //
32+
CryptoFileSystemProperties fileSystemProperties) {
2633
super(delegate, ciphertextFileType, ciphertextPath, cryptor, openCryptoFile);
27-
this.readonlyFileSystem = fileSystemProperties.readonly();
28-
this.delegate = delegate;
34+
this.isReadOnly = fileSystemProperties.readonly() || delegate.isReadOnly();
35+
this.isHidden = delegate.isHidden();
36+
this.isArchive = delegate.isArchive();
37+
this.isSystem = delegate.isSystem();
2938
}
3039

3140
@Override
3241
public boolean isReadOnly() {
33-
return readonlyFileSystem || delegate.isReadOnly();
42+
return isReadOnly;
3443
}
3544

3645
@Override
3746
public boolean isHidden() {
38-
return delegate.isHidden();
47+
return isHidden;
3948
}
4049

4150
@Override
4251
public boolean isArchive() {
43-
return delegate.isArchive();
52+
return isArchive;
4453
}
4554

4655
@Override
4756
public boolean isSystem() {
48-
return delegate.isSystem();
57+
return isSystem;
4958
}
5059
}

src/test/java/org/cryptomator/cryptofs/attr/CryptoDosFileAttributesTest.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77
import org.cryptomator.cryptolib.api.FileContentCryptor;
88
import org.cryptomator.cryptolib.api.FileHeaderCryptor;
99
import org.junit.jupiter.api.Assertions;
10-
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.DisplayName;
1212
import org.junit.jupiter.api.Nested;
13-
import org.junit.jupiter.api.TestInstance;
1413
import org.junit.jupiter.params.ParameterizedTest;
1514
import org.junit.jupiter.params.provider.CsvSource;
1615

1716
import java.nio.file.attribute.DosFileAttributes;
1817
import java.util.Optional;
1918

2019
import static org.cryptomator.cryptofs.common.CiphertextFileType.FILE;
20+
import static org.mockito.Mockito.atMostOnce;
2121
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.times;
23+
import static org.mockito.Mockito.verify;
2224
import static org.mockito.Mockito.when;
2325

24-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2526
public class CryptoDosFileAttributesTest {
2627

2728
private DosFileAttributes delegate = mock(DosFileAttributes.class);
@@ -32,9 +33,9 @@ public class CryptoDosFileAttributesTest {
3233
private OpenCryptoFile openCryptoFile = mock(OpenCryptoFile.class);
3334
private CryptoFileSystemProperties cryptoFileSystemProperties = mock(CryptoFileSystemProperties.class);
3435

35-
@BeforeAll
36+
@BeforeEach
3637
public void setup() {
37-
when(delegate.size()).thenReturn(0l);
38+
when(delegate.size()).thenReturn(0L);
3839
when(cryptor.fileHeaderCryptor()).thenReturn(headerCryptor);
3940
when(cryptor.fileContentCryptor()).thenReturn(contentCryptor);
4041
when(headerCryptor.headerSize()).thenReturn(0);
@@ -43,77 +44,85 @@ public void setup() {
4344
}
4445

4546
@Nested
46-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
4747
@DisplayName("on read-write filesystem")
4848
public class ReadWriteFileSystem {
4949

5050
private CryptoDosFileAttributes inTest;
5151

52-
@BeforeAll
53-
public void setup() {
52+
@BeforeEach
53+
public void beforeEach() {
5454
when(cryptoFileSystemProperties.readonly()).thenReturn(false);
55-
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
5655
}
5756

5857
@DisplayName("isArchive()")
5958
@ParameterizedTest(name = "is {0} if delegate.isArchive() is {0}")
6059
@CsvSource({"true", "false"})
61-
public void testIsArchiveDelegates(boolean value) {
60+
public void testIsArchiveImmutable(boolean value) {
6261
when(delegate.isArchive()).thenReturn(value);
62+
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
6363

64-
Assertions.assertSame(value, inTest.isArchive());
64+
verify(delegate, times(1)).isArchive();
65+
Assertions.assertEquals(value, inTest.isArchive());
66+
verify(delegate, times(1)).isArchive();
6567
}
6668

6769
@DisplayName("isHidden()")
6870
@ParameterizedTest(name = "is {0} if delegate.isHidden() is {0}")
6971
@CsvSource({"true", "false"})
70-
public void testIsHiddenDelegates(boolean value) {
72+
public void testIsHiddenImmutable(boolean value) {
7173
when(delegate.isHidden()).thenReturn(value);
74+
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
7275

73-
Assertions.assertSame(value, inTest.isHidden());
76+
verify(delegate, times(1)).isHidden();
77+
Assertions.assertEquals(value, inTest.isHidden());
78+
verify(delegate, times(1)).isHidden();
7479
}
7580

7681
@DisplayName("isReadOnly()")
7782
@ParameterizedTest(name = "is {0} if delegate.readOnly() is {0}")
7883
@CsvSource({"true", "false"})
79-
public void testIsReadOnlyDelegates(boolean value) {
84+
public void testIsReadOnlyImmutable(boolean value) {
8085
when(delegate.isReadOnly()).thenReturn(value);
86+
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
8187

82-
Assertions.assertSame(value, inTest.isReadOnly());
88+
verify(delegate, times(1)).isReadOnly();
89+
Assertions.assertEquals(value, inTest.isReadOnly());
90+
verify(delegate, times(1)).isReadOnly();
8391
}
8492

8593
@DisplayName("isSystem()")
8694
@ParameterizedTest(name = "is {0} if delegate.isSystem() is {0}")
8795
@CsvSource({"true", "false"})
88-
public void testIsSystemDelegates(boolean value) {
96+
public void testIsSystemImmutable(boolean value) {
8997
when(delegate.isSystem()).thenReturn(value);
98+
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
9099

91-
Assertions.assertSame(value, inTest.isSystem());
100+
verify(delegate, times(1)).isSystem();
101+
Assertions.assertEquals(value, inTest.isSystem());
102+
verify(delegate, times(1)).isSystem();
92103
}
93104

94105
}
95106

96107
@Nested
97-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
98108
@DisplayName("on read-only filesystem")
99109
public class ReadOnlyFileSystem {
100110

101-
private CryptoDosFileAttributes inTest;
102-
103-
@BeforeAll
104-
public void setup() {
111+
@BeforeEach
112+
public void beforeEach() {
105113
when(cryptoFileSystemProperties.readonly()).thenReturn(true);
106-
inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
107114
}
108115

109116
@DisplayName("isReadOnly()")
110117
@ParameterizedTest(name = "is true if delegate.readOnly() is {0}")
111118
@CsvSource({"true", "false"})
112119
public void testIsReadOnlyForReadonlyFileSystem(boolean value) {
113120
when(delegate.isReadOnly()).thenReturn(value);
121+
var inTest = new CryptoDosFileAttributes(delegate, FILE, path, cryptor, Optional.of(openCryptoFile), cryptoFileSystemProperties);
114122

123+
verify(delegate, atMostOnce()).isReadOnly();
115124
Assertions.assertTrue(inTest.isReadOnly());
125+
verify(delegate, atMostOnce()).isReadOnly();
116126
}
117-
118127
}
119128
}

0 commit comments

Comments
 (0)