|
| 1 | +package com.baeldung.openoptions; |
| 2 | + |
| 3 | +import org.hamcrest.CoreMatchers; |
| 4 | +import org.junit.AfterClass; |
| 5 | +import org.junit.BeforeClass; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.io.*; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.nio.file.StandardOpenOption; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +public class OpenOptionsUnitTest { |
| 17 | + |
| 18 | + private static final String HOME = System.getProperty("user.home"); |
| 19 | + private static final String DUMMY_FILE_NAME = "sample.txt"; |
| 20 | + private static final String EXISTING_FILE_NAME = "existingFile.txt"; |
| 21 | + |
| 22 | + private static final String DUMMY_TEXT = "This is a sample text."; |
| 23 | + private static final String ANOTHER_DUMMY_TEXT = "This is a another text."; |
| 24 | + |
| 25 | + @BeforeClass |
| 26 | + public static void beforeAll() throws IOException { |
| 27 | + Path path = Paths.get(HOME, DUMMY_FILE_NAME); |
| 28 | + |
| 29 | + try (OutputStream out = Files.newOutputStream(path)) { |
| 30 | + out.write(DUMMY_TEXT.getBytes()); |
| 31 | + } |
| 32 | + |
| 33 | + Files.createFile(Paths.get(HOME, EXISTING_FILE_NAME)); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterClass |
| 37 | + public static void afterAll() throws IOException { |
| 38 | + Files.delete(Paths.get(HOME, "newfile.txt")); |
| 39 | + Files.delete(Paths.get(HOME, "sparse.txt")); |
| 40 | + Files.delete(Paths.get(HOME, DUMMY_FILE_NAME)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenExistingPath_whenCreateNewFile_thenCorrect() throws IOException { |
| 45 | + Path path = Paths.get(HOME, "newfile.txt"); |
| 46 | + assertFalse(Files.exists(path)); |
| 47 | + |
| 48 | + Files.write(path, DUMMY_TEXT.getBytes(), StandardOpenOption.CREATE); |
| 49 | + assertTrue(Files.exists(path)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenExistingPath_whenReadExistingFile_thenCorrect() throws IOException { |
| 54 | + Path path = Paths.get(HOME, DUMMY_FILE_NAME); |
| 55 | + |
| 56 | + try (InputStream in = Files.newInputStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { |
| 57 | + String line; |
| 58 | + while ((line = reader.readLine()) != null) { |
| 59 | + assertThat(line, CoreMatchers.containsString(DUMMY_TEXT)); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenExistingPath_whenWriteToExistingFile_thenCorrect() throws IOException { |
| 66 | + Path path = Paths.get(HOME, DUMMY_FILE_NAME); |
| 67 | + |
| 68 | + try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) { |
| 69 | + out.write(ANOTHER_DUMMY_TEXT.getBytes()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenExistingPath_whenCreateSparseFile_thenCorrect() throws IOException { |
| 75 | + Path path = Paths.get(HOME, "sparse.txt"); |
| 76 | + Files.write(path, DUMMY_TEXT.getBytes(), StandardOpenOption.CREATE_NEW, StandardOpenOption.SPARSE); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenExistingPath_whenDeleteOnClose_thenCorrect() throws IOException { |
| 81 | + Path path = Paths.get(HOME, EXISTING_FILE_NAME); |
| 82 | + assertTrue(Files.exists(path)); // file was already created and exists |
| 83 | + |
| 84 | + try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND, StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE)) { |
| 85 | + out.write(ANOTHER_DUMMY_TEXT.getBytes()); |
| 86 | + } |
| 87 | + |
| 88 | + assertFalse(Files.exists(path)); // file is deleted |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenExistingPath_whenWriteAndSync_thenCorrect() throws IOException { |
| 93 | + Path path = Paths.get(HOME, DUMMY_FILE_NAME); |
| 94 | + Files.write(path, ANOTHER_DUMMY_TEXT.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.WRITE, StandardOpenOption.SYNC); |
| 95 | + } |
| 96 | +} |
0 commit comments