|
| 1 | +/* |
| 2 | + * BSD 2-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2023, Swat.engineering |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package engineering.swat.watch; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.Files; |
| 33 | + |
| 34 | +import org.awaitility.Awaitility; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +public class APIErrorsTests { |
| 41 | + |
| 42 | + private TestDirectory testDir; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setup() throws IOException { |
| 46 | + testDir = new TestDirectory(); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + void cleanup() { |
| 51 | + if (testDir != null) { |
| 52 | + testDir.close(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void setupEverything() { |
| 58 | + Awaitility.setDefaultTimeout(TestHelper.NORMAL_WAIT); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void noDuplicateEvents() { |
| 63 | + assertThrowsExactly(IllegalArgumentException.class, () -> |
| 64 | + Watcher |
| 65 | + .watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN) |
| 66 | + .on(System.out::println) |
| 67 | + .on(System.err::println) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void onlyDirectoryWatchingOnDirectories() { |
| 73 | + assertThrowsExactly(IllegalArgumentException.class, () -> |
| 74 | + Watcher |
| 75 | + .watch(testDir.getTestFiles().get(0), WatchScope.PATH_AND_CHILDREN) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void doNotStartWithoutEventHandler() { |
| 81 | + assertThrowsExactly(IllegalStateException.class, () -> |
| 82 | + Watcher |
| 83 | + .watch(testDir.getTestDirectory(), WatchScope.PATH_AND_CHILDREN) |
| 84 | + .start() |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void noRelativePaths() { |
| 90 | + var relativePath = testDir.getTestDirectory().resolve("d1").relativize(testDir.getTestDirectory()); |
| 91 | + |
| 92 | + assertThrowsExactly(IllegalArgumentException.class, () -> |
| 93 | + Watcher |
| 94 | + .watch(relativePath, WatchScope.PATH_AND_CHILDREN) |
| 95 | + .start() |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void nonExistingDirectory() throws IOException { |
| 101 | + var nonExistingDir = testDir.getTestDirectory().resolve("testd1"); |
| 102 | + Files.createDirectory(nonExistingDir); |
| 103 | + var w = Watcher.watch(nonExistingDir, WatchScope.PATH_AND_CHILDREN); |
| 104 | + Files.delete(nonExistingDir); |
| 105 | + assertThrowsExactly(IllegalStateException.class, w::start); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +} |
0 commit comments