2626import java .io .UncheckedIOException ;
2727import java .nio .channels .FileChannel ;
2828import java .nio .file .FileSystem ;
29- import java .nio .file .Files ;
3029import java .nio .file .Path ;
3130import java .nio .file .StandardOpenOption ;
3231import java .nio .file .attribute .PosixFilePermissions ;
@@ -48,7 +47,6 @@ public class OpenCryptoFileTest {
4847 private static AtomicReference <Path > CURRENT_FILE_PATH ;
4948 private ReadonlyFlag readonlyFlag = mock (ReadonlyFlag .class );
5049 private FileCloseListener closeListener = mock (FileCloseListener .class );
51- private ChunkCache chunkCache = mock (ChunkCache .class );
5250 private Cryptor cryptor = mock (Cryptor .class );
5351 private FileHeaderCryptor fileHeaderCryptor = mock (FileHeaderCryptor .class );
5452 private FileHeaderHolder headerHolder = mock (FileHeaderHolder .class );
@@ -72,7 +70,7 @@ public static void tearDown() throws IOException {
7270
7371 @ Test
7472 public void testCloseTriggersCloseListener () {
75- OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , chunkCache , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
73+ OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
7674 openCryptoFile .close ();
7775 verify (closeListener ).close (CURRENT_FILE_PATH .get (), openCryptoFile );
7876 }
@@ -83,7 +81,7 @@ public void testCloseImmediatelyIfOpeningFirstChannelFails() {
8381 UncheckedIOException expectedException = new UncheckedIOException (new IOException ("fail!" ));
8482 EffectiveOpenOptions options = Mockito .mock (EffectiveOpenOptions .class );
8583 Mockito .when (options .createOpenOptionsForEncryptedFile ()).thenThrow (expectedException );
86- OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , chunkCache , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
84+ OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
8785
8886 UncheckedIOException exception = Assertions .assertThrows (UncheckedIOException .class , () -> {
8987 openCryptoFile .newFileChannel (options );
@@ -93,19 +91,20 @@ public void testCloseImmediatelyIfOpeningFirstChannelFails() {
9391 }
9492
9593 @ Test
96- @ DisplayName ("Opening a file channel with TRUNCATE_EXISTING sets the file size to 0 " )
97- public void testFileSizeZerodOnTruncateExisting () throws IOException {
94+ @ DisplayName ("Opening a file channel with TRUNCATE_EXISTING calls truncate(0) on the cleartextChannel " )
95+ public void testCleartextChannelTruncateCalledOnTruncateExisting () throws IOException {
9896 EffectiveOpenOptions options = EffectiveOpenOptions .from (EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE , StandardOpenOption .TRUNCATE_EXISTING ), readonlyFlag );
97+ var cleartextChannel = mock (CleartextFileChannel .class );
9998 Mockito .when (headerHolder .get ()).thenReturn (Mockito .mock (FileHeader .class ));
10099 Mockito .when (cryptor .fileHeaderCryptor ()).thenReturn (fileHeaderCryptor );
101100 Mockito .when (fileHeaderCryptor .headerSize ()).thenReturn (42 );
102101 Mockito .when (openCryptoFileComponent .newChannelComponent ()).thenReturn (channelComponentFactory );
103102 Mockito .when (channelComponentFactory .create (any (), any (), any ())).thenReturn (channelComponent );
104- Mockito .when (channelComponent .channel ()).thenReturn (mock ( CleartextFileChannel . class ) );
105- OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , chunkCache , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
103+ Mockito .when (channelComponent .channel ()).thenReturn (cleartextChannel );
104+ OpenCryptoFile openCryptoFile = new OpenCryptoFile (closeListener , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
106105
107106 openCryptoFile .newFileChannel (options );
108- verify (fileSize ). set (0L );
107+ verify (cleartextChannel ). truncate (0L );
109108 }
110109
111110 @ Nested
@@ -114,7 +113,7 @@ public class InitFilHeaderTests {
114113
115114 EffectiveOpenOptions options = Mockito .mock (EffectiveOpenOptions .class );
116115 FileChannel cipherFileChannel = Mockito .mock (FileChannel .class , "cipherFilechannel" );
117- OpenCryptoFile inTest = new OpenCryptoFile (closeListener , chunkCache , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
116+ OpenCryptoFile inTest = new OpenCryptoFile (closeListener , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , fileSize , lastModified , openCryptoFileComponent );
118117
119118 @ Test
120119 @ DisplayName ("Skip file header init, if the file header already exists in memory" )
@@ -198,7 +197,7 @@ public class FileChannelFactoryTest {
198197 public void setup () throws IOException {
199198 FS = Jimfs .newFileSystem ("OpenCryptoFileTest.FileChannelFactoryTest" , Configuration .unix ().toBuilder ().setAttributeViews ("basic" , "posix" ).build ());
200199 CURRENT_FILE_PATH = new AtomicReference <>(FS .getPath ("currentFile" ));
201- openCryptoFile = new OpenCryptoFile (closeListener , chunkCache , cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , realFileSize , lastModified , openCryptoFileComponent );
200+ openCryptoFile = new OpenCryptoFile (closeListener ,cryptor , headerHolder , chunkIO , CURRENT_FILE_PATH , realFileSize , lastModified , openCryptoFileComponent );
202201 cleartextFileChannel = mock (CleartextFileChannel .class );
203202 listener = new AtomicReference <>();
204203 ciphertextChannel = new AtomicReference <>();
@@ -260,19 +259,6 @@ public void testGetSizeAfterCreatingSecondFileChannel() {
260259 Assertions .assertEquals (0l , openCryptoFile .size ().get ());
261260 }
262261
263-
264- @ Test
265- @ Order (20 )
266- @ DisplayName ("TRUNCATE_EXISTING leads to chunk cache invalidation" )
267- public void testTruncateExistingInvalidatesChunkCache () throws IOException {
268- Mockito .when (cryptor .fileHeaderCryptor ()).thenReturn (fileHeaderCryptor );
269- Mockito .when (fileHeaderCryptor .headerSize ()).thenReturn (43 );
270- Files .write (CURRENT_FILE_PATH .get (), new byte [0 ]);
271- EffectiveOpenOptions options = EffectiveOpenOptions .from (EnumSet .of (StandardOpenOption .TRUNCATE_EXISTING , StandardOpenOption .WRITE ), readonlyFlag );
272- openCryptoFile .newFileChannel (options );
273- verify (chunkCache ).invalidateStale ();
274- }
275-
276262 @ Test
277263 @ Order (100 )
278264 @ DisplayName ("closeListener triggers chunkIO.unregisterChannel()" )
0 commit comments