View Javadoc
1   package org.codehaus.plexus.util.io;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.nio.charset.StandardCharsets;
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.nio.file.attribute.FileTime;
25  import java.util.Objects;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertNotEquals;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  public class CachingOutputStreamTest {
37  
38      Path tempDir;
39      Path checkLastModified;
40  
41      @BeforeEach
42      public void setup() throws IOException {
43          Path dir = Paths.get("target/io");
44          Files.createDirectories(dir);
45          tempDir = Files.createTempDirectory(dir, "temp-");
46          checkLastModified = tempDir.resolve(".check");
47      }
48  
49      private void waitLastModified() throws IOException, InterruptedException {
50          Files.newOutputStream(checkLastModified).close();
51          FileTime lm = Files.getLastModifiedTime(checkLastModified);
52          while (true) {
53              Files.newOutputStream(checkLastModified).close();
54              FileTime nlm = Files.getLastModifiedTime(checkLastModified);
55              if (!Objects.equals(nlm, lm)) {
56                  break;
57              }
58              Thread.sleep(10);
59          }
60      }
61  
62      @Test
63      public void testWriteNoExistingFile() throws IOException, InterruptedException {
64          byte[] data = "Hello world!".getBytes(StandardCharsets.UTF_8);
65          Path path = tempDir.resolve("file.txt");
66          assertFalse(Files.exists(path));
67  
68          try (CachingOutputStream cos = new CachingOutputStream(path, 4)) {
69              cos.write(data);
70          }
71          assertTrue(Files.exists(path));
72          byte[] read = Files.readAllBytes(path);
73          assertArrayEquals(data, read);
74          FileTime modified = Files.getLastModifiedTime(path);
75  
76          waitLastModified();
77  
78          try (CachingOutputStream cos = new CachingOutputStream(path, 4)) {
79              cos.write(data);
80          }
81          assertTrue(Files.exists(path));
82          read = Files.readAllBytes(path);
83          assertArrayEquals(data, read);
84          FileTime newModified = Files.getLastModifiedTime(path);
85          assertEquals(modified, newModified);
86          modified = newModified;
87  
88          waitLastModified();
89  
90          // write longer data
91          data = "Good morning!".getBytes(StandardCharsets.UTF_8);
92          try (CachingOutputStream cos = new CachingOutputStream(path, 4)) {
93              cos.write(data);
94          }
95          assertTrue(Files.exists(path));
96          read = Files.readAllBytes(path);
97          assertArrayEquals(data, read);
98          newModified = Files.getLastModifiedTime(path);
99          assertNotEquals(modified, newModified);
100         modified = newModified;
101 
102         waitLastModified();
103 
104         // different data same size
105         data = "Good mornong!".getBytes(StandardCharsets.UTF_8);
106         try (CachingOutputStream cos = new CachingOutputStream(path, 4)) {
107             cos.write(data);
108         }
109         assertTrue(Files.exists(path));
110         read = Files.readAllBytes(path);
111         assertArrayEquals(data, read);
112         newModified = Files.getLastModifiedTime(path);
113         assertNotEquals(modified, newModified);
114         modified = newModified;
115 
116         waitLastModified();
117 
118         // same data but shorter
119         data = "Good mornon".getBytes(StandardCharsets.UTF_8);
120         try (CachingOutputStream cos = new CachingOutputStream(path, 4)) {
121             cos.write(data);
122         }
123         assertTrue(Files.exists(path));
124         read = Files.readAllBytes(path);
125         assertArrayEquals(data, read);
126         newModified = Files.getLastModifiedTime(path);
127         assertNotEquals(modified, newModified);
128         modified = newModified;
129     }
130 }