Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 22, 2022
1 parent 6cc3084 commit 9d69ee8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Expand Up @@ -17,11 +17,13 @@
*/

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Objects;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -36,13 +38,33 @@ public class CachingOutputStreamTest
{

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
{
Path dir = Paths.get( "target/io" );
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
}
}

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

Thread.sleep( 250 );
waitLastModified();

try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) )
{
Expand All @@ -74,7 +96,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
assertEquals( modified, newModified );
modified = newModified;

Thread.sleep( 250 );
waitLastModified();

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

Thread.sleep( 250 );
waitLastModified();

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

Thread.sleep( 250 );
waitLastModified();

// same data but shorter
data = "Good mornon".getBytes( StandardCharsets.UTF_8 );
Expand All @@ -119,4 +141,5 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
assertNotEquals( modified, newModified );
modified = newModified;
}

}
29 changes: 25 additions & 4 deletions src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Objects;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -36,13 +37,33 @@ public class CachingWriterTest
{

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
{
Path dir = Paths.get( "target/io" );
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
}
}

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

Thread.sleep( 250 );
waitLastModified();

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

Thread.sleep( 250 );
waitLastModified();

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

Thread.sleep( 250 );
waitLastModified();

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

Thread.sleep( 250 );
waitLastModified();

// same data but shorter
data = "Good mornon";
Expand Down

0 comments on commit 9d69ee8

Please sign in to comment.