Skip to content

Commit 4473d24

Browse files
committed
Improve tests
1 parent 6cc3084 commit 4473d24

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
*/
1818

1919
import java.io.IOException;
20+
import java.io.OutputStream;
2021
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.Files;
2223
import java.nio.file.Path;
2324
import java.nio.file.Paths;
2425
import java.nio.file.attribute.FileTime;
26+
import java.util.Objects;
2527

2628
import org.junit.Before;
2729
import org.junit.Test;
@@ -36,13 +38,33 @@ public class CachingOutputStreamTest
3638
{
3739

3840
Path tempDir;
41+
Path checkLastModified;
42+
FileTime lm;
3943

4044
@Before
4145
public void setup() throws IOException
4246
{
4347
Path dir = Paths.get( "target/io" );
4448
Files.createDirectories( dir );
4549
tempDir = Files.createTempDirectory( dir, "temp-" );
50+
checkLastModified = tempDir.resolve( ".check" );
51+
Files.newOutputStream( checkLastModified ).close();
52+
lm = Files.getLastModifiedTime( checkLastModified );
53+
}
54+
55+
private void waitLastModified() throws IOException, InterruptedException
56+
{
57+
while ( true )
58+
{
59+
Files.newOutputStream( checkLastModified ).close();
60+
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
61+
if ( !Objects.equals( nlm, lm ) )
62+
{
63+
lm = nlm;
64+
break;
65+
}
66+
Thread.sleep( 10 );
67+
}
4668
}
4769

4870
@Test
@@ -61,7 +83,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
6183
assertArrayEquals( data, read );
6284
FileTime modified = Files.getLastModifiedTime( path );
6385

64-
Thread.sleep( 250 );
86+
waitLastModified();
6587

6688
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) )
6789
{
@@ -74,7 +96,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
7496
assertEquals( modified, newModified );
7597
modified = newModified;
7698

77-
Thread.sleep( 250 );
99+
waitLastModified();
78100

79101
// write longer data
80102
data = "Good morning!".getBytes( StandardCharsets.UTF_8 );
@@ -89,7 +111,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
89111
assertNotEquals( modified, newModified );
90112
modified = newModified;
91113

92-
Thread.sleep( 250 );
114+
waitLastModified();
93115

94116
// different data same size
95117
data = "Good mornong!".getBytes( StandardCharsets.UTF_8 );
@@ -104,7 +126,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
104126
assertNotEquals( modified, newModified );
105127
modified = newModified;
106128

107-
Thread.sleep( 250 );
129+
waitLastModified();
108130

109131
// same data but shorter
110132
data = "Good mornon".getBytes( StandardCharsets.UTF_8 );
@@ -119,4 +141,5 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
119141
assertNotEquals( modified, newModified );
120142
modified = newModified;
121143
}
144+
122145
}

src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Path;
2323
import java.nio.file.Paths;
2424
import java.nio.file.attribute.FileTime;
25+
import java.util.Objects;
2526

2627
import org.junit.Before;
2728
import org.junit.Test;
@@ -36,13 +37,33 @@ public class CachingWriterTest
3637
{
3738

3839
Path tempDir;
40+
Path checkLastModified;
41+
FileTime lm;
3942

4043
@Before
4144
public void setup() throws IOException
4245
{
4346
Path dir = Paths.get( "target/io" );
4447
Files.createDirectories( dir );
4548
tempDir = Files.createTempDirectory( dir, "temp-" );
49+
checkLastModified = tempDir.resolve( ".check" );
50+
Files.newOutputStream( checkLastModified ).close();
51+
lm = Files.getLastModifiedTime( checkLastModified );
52+
}
53+
54+
private void waitLastModified() throws IOException, InterruptedException
55+
{
56+
while ( true )
57+
{
58+
Files.newOutputStream( checkLastModified ).close();
59+
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
60+
if ( !Objects.equals( nlm, lm ) )
61+
{
62+
lm = nlm;
63+
break;
64+
}
65+
Thread.sleep( 10 );
66+
}
4667
}
4768

4869
@Test
@@ -61,7 +82,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
6182
assertEquals( data, read );
6283
FileTime modified = Files.getLastModifiedTime( path );
6384

64-
Thread.sleep( 250 );
85+
waitLastModified();
6586

6687
try ( CachingWriter cos = new CachingWriter( path, StandardCharsets.UTF_8, 4 ) )
6788
{
@@ -74,7 +95,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
7495
assertEquals( modified, newModified );
7596
modified = newModified;
7697

77-
Thread.sleep( 250 );
98+
waitLastModified();
7899

79100
// write longer data
80101
data = "Good morning!";
@@ -89,7 +110,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
89110
assertNotEquals( modified, newModified );
90111
modified = newModified;
91112

92-
Thread.sleep( 250 );
113+
waitLastModified();
93114

94115
// different data same size
95116
data = "Good mornong!";
@@ -104,7 +125,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
104125
assertNotEquals( modified, newModified );
105126
modified = newModified;
106127

107-
Thread.sleep( 250 );
128+
waitLastModified();
108129

109130
// same data but shorter
110131
data = "Good mornon";

0 commit comments

Comments
 (0)