Skip to content

Commit df69a8f

Browse files
committed
Add proper tests for execute-around pattern
1 parent 885d5bb commit df69a8f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.iluwatar.execute.around;
2+
3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.TemporaryFolder;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertTrue;
15+
16+
/**
17+
* Date: 12/12/15 - 3:21 PM
18+
*
19+
* @author Jeroen Meulemeester
20+
*/
21+
public class SimpleFileWriterTest {
22+
23+
/**
24+
* Create a temporary folder, used to generate files in during this test
25+
*/
26+
@Rule
27+
public final TemporaryFolder testFolder = new TemporaryFolder();
28+
29+
/**
30+
* Verify if the given writer is not 'null'
31+
*/
32+
@Test
33+
public void testWriterNotNull() throws Exception {
34+
final File temporaryFile = this.testFolder.newFile();
35+
new SimpleFileWriter(temporaryFile.getPath(), Assert::assertNotNull);
36+
}
37+
38+
/**
39+
* Test if the {@link SimpleFileWriter} creates a file if it doesn't exist
40+
*/
41+
@Test
42+
public void testNonExistentFile() throws Exception {
43+
final File nonExistingFile = new File(this.testFolder.getRoot(), "non-existing-file");
44+
assertFalse(nonExistingFile.exists());
45+
46+
new SimpleFileWriter(nonExistingFile.getPath(), Assert::assertNotNull);
47+
assertTrue(nonExistingFile.exists());
48+
}
49+
50+
/**
51+
* Test if the data written to the file writer actually gets in the file
52+
*/
53+
@Test
54+
public void testActualWrite() throws Exception {
55+
final String testMessage = "Test message";
56+
57+
final File temporaryFile = this.testFolder.newFile();
58+
assertTrue(temporaryFile.exists());
59+
60+
new SimpleFileWriter(temporaryFile.getPath(), writer -> writer.write(testMessage));
61+
assertTrue(Files.lines(temporaryFile.toPath()).allMatch(testMessage::equals));
62+
}
63+
64+
/**
65+
* Verify if an {@link IOException} during the write ripples through
66+
*/
67+
@Test(expected = IOException.class)
68+
public void testIOException() throws Exception {
69+
final File temporaryFile = this.testFolder.newFile();
70+
new SimpleFileWriter(temporaryFile.getPath(), writer -> {
71+
throw new IOException("");
72+
});
73+
}
74+
75+
}

0 commit comments

Comments
 (0)