|
2 | 2 |
|
3 | 3 | import static org.hamcrest.MatcherAssert.assertThat; |
4 | 4 | import static org.hamcrest.Matchers.equalTo; |
5 | | -import static org.junit.Assert.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 6 |
|
7 | | -import java.io.File; |
8 | 7 | import java.io.IOException; |
9 | 8 | import java.nio.channels.ClosedChannelException; |
10 | 9 | import java.nio.charset.StandardCharsets; |
11 | 10 | import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
12 | 12 | import java.nio.file.StandardOpenOption; |
13 | | -import org.junit.Before; |
14 | | -import org.junit.Rule; |
15 | | -import org.junit.Test; |
16 | | -import org.junit.rules.TemporaryFolder; |
17 | | - |
18 | | -public class FileChannelWriterTest { |
19 | | - @Rule |
20 | | - public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
21 | | - |
22 | | - File file; |
23 | | - FileChannelWriter writer; |
24 | | - |
25 | | - @Before |
26 | | - public void setUp() throws Exception { |
27 | | - file = temporaryFolder.newFile(); |
28 | | - writer = new FileChannelWriter(file.toPath(), StandardCharsets.UTF_8, true, true, StandardOpenOption.WRITE); |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +class FileChannelWriterTest { |
| 17 | + |
| 18 | + private Path file; |
| 19 | + private FileChannelWriter writer; |
| 20 | + |
| 21 | + @BeforeEach |
| 22 | + void setUp() throws Exception { |
| 23 | + file = Files.createTempFile("junit", null); |
| 24 | + writer = new FileChannelWriter(file, StandardCharsets.UTF_8, true, true, StandardOpenOption.WRITE); |
29 | 25 | } |
30 | 26 |
|
31 | 27 | @Test |
32 | | - public void write() throws Exception { |
| 28 | + void write() throws Exception { |
33 | 29 | writer.write("helloooo"); |
34 | 30 | writer.close(); |
35 | 31 |
|
36 | 32 | assertContent("helloooo"); |
37 | 33 | } |
38 | 34 |
|
39 | | - |
40 | 35 | @Test |
41 | | - public void flush() throws Exception { |
| 36 | + void flush() throws Exception { |
42 | 37 | writer.write("hello é è à".toCharArray()); |
43 | 38 |
|
44 | 39 | writer.flush(); |
45 | 40 | assertContent("hello é è à"); |
46 | 41 | } |
47 | 42 |
|
48 | 43 | @Test |
49 | | - public void close() throws Exception { |
| 44 | + void close() throws Exception { |
50 | 45 | writer.write("helloooo"); |
51 | 46 | writer.close(); |
52 | 47 |
|
53 | 48 | assertThrows(ClosedChannelException.class, () -> writer.write("helloooo")); |
54 | 49 | } |
55 | 50 |
|
56 | | - |
57 | 51 | private void assertContent(String string) throws IOException { |
58 | | - assertThat(Files.readString(file.toPath(), StandardCharsets.UTF_8), equalTo(string)); |
| 52 | + assertThat(Files.readString(file, StandardCharsets.UTF_8), equalTo(string)); |
59 | 53 | } |
60 | 54 | } |
0 commit comments