Skip to content

Commit faae1e7

Browse files
committed
Added extra tests for API
1 parent c33f915 commit faae1e7

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

src/test/java/engineering/swat/watch/SingleDirectoryTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,35 @@ void deleteOfFileInDirectoryShouldBeVisible() throws IOException, InterruptedExc
8989
.untilTrue(seenCreate);
9090
}
9191
}
92+
93+
@Test
94+
void alternativeAPITest() throws IOException, InterruptedException {
95+
var target = testDir.getTestFiles().get(0);
96+
var seenDelete = new AtomicBoolean(false);
97+
var seenCreate = new AtomicBoolean(false);
98+
var watchConfig = Watcher.watch(target.getParent(), WatchScope.PATH_AND_CHILDREN)
99+
.on(new WatchEventListener() {
100+
@Override
101+
public void onCreated(WatchEvent ev) {
102+
seenCreate.set(true);
103+
}
104+
105+
@Override
106+
public void onDeleted(WatchEvent ev) {
107+
seenDelete.set(true);
108+
}
109+
});
110+
try (var watch = watchConfig.start()) {
111+
112+
// Delete the file
113+
Files.delete(target);
114+
await("File deletion should generate delete event")
115+
.untilTrue(seenDelete);
116+
117+
// Re-create it again
118+
Files.writeString(target, "Hello World");
119+
await("File creation should generate create event")
120+
.untilTrue(seenCreate);
121+
}
122+
}
92123
}

0 commit comments

Comments
 (0)