Skip to content

Commit dc9b402

Browse files
authored
test: refactor FileWatcherTest to clean up resources (#21133)
Stops watchers after test execution and uses JUnit temporary folder rule to ensure temporary directories are deleted.
1 parent 0528f4f commit dc9b402

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

vaadin-dev-server/src/test/java/com/vaadin/base/devserver/FileWatcherTest.java

+52-41
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.util.Arrays;
67
import java.util.concurrent.atomic.AtomicReference;
78

89
import org.junit.Assert;
10+
import org.junit.Rule;
911
import org.junit.Test;
12+
import org.junit.rules.TemporaryFolder;
1013
import org.mockito.MockedStatic;
1114
import org.mockito.Mockito;
1215

@@ -16,43 +19,48 @@
1619

1720
public class FileWatcherTest {
1821

22+
@Rule
23+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
24+
1925
@Test
2026
public void fileWatcherTriggeredForModification() throws Exception {
2127
AtomicReference<File> changed = new AtomicReference<>();
2228

23-
File dir = Files.createTempDirectory("watched").toFile();
29+
File dir = temporaryFolder.newFolder("watched");
2430
FileWatcher watcher = new FileWatcher(file -> {
2531
changed.set(file);
2632
}, dir);
2733

2834
watcher.start();
2935

30-
File newFile = new File(dir, "newFile.txt");
31-
newFile.createNewFile();
36+
try {
37+
File newFile = new File(dir, "newFile.txt");
38+
newFile.createNewFile();
3239

33-
Thread.sleep(50); // The watcher is supposed to be triggered immediately
34-
Assert.assertEquals(newFile, changed.get());
40+
Thread.sleep(50); // The watcher is supposed to be triggered
41+
// immediately
42+
Assert.assertEquals(newFile, changed.get());
43+
} finally {
44+
watcher.stop();
45+
}
3546
}
3647

3748
@Test
3849
public void externalDependencyWatcher_setViaParameter_TriggeredForModification()
3950
throws Exception {
40-
File projectFolder = Files.createTempDirectory("projectFolder")
41-
.toFile();
42-
projectFolder.deleteOnExit();
51+
File projectFolder = temporaryFolder.newFolder("projectFolder");
4352

4453
String metaInf = "/src/main/resources/META-INF/";
45-
String rootPorjectResourceFrontend = projectFolder.getAbsolutePath()
54+
String rootProjectResourceFrontend = projectFolder.getAbsolutePath()
4655
+ metaInf + "resources/frontend";
4756
String subProjectLegacyFrontend = projectFolder.getAbsolutePath()
4857
+ "/fakeproject" + metaInf + "frontend";
4958

50-
new File(rootPorjectResourceFrontend).mkdirs();
59+
new File(rootProjectResourceFrontend).mkdirs();
5160
new File(subProjectLegacyFrontend).mkdirs();
5261

53-
File jarFrontendResources = Files
54-
.createTempDirectory("jarFrontendResources").toFile();
55-
jarFrontendResources.deleteOnExit();
62+
File jarFrontendResources = temporaryFolder
63+
.newFolder("jarFrontendResources");
5664

5765
VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
5866
ApplicationConfiguration config = Mockito
@@ -66,29 +74,29 @@ public void externalDependencyWatcher_setViaParameter_TriggeredForModification()
6674
.mockStatic(ApplicationConfiguration.class)) {
6775
appConfig.when(() -> ApplicationConfiguration.get(Mockito.any()))
6876
.thenReturn(config);
69-
new ExternalDependencyWatcher(vaadinContext, jarFrontendResources);
77+
try (var watcher = new ExternalDependencyWatcher(vaadinContext,
78+
jarFrontendResources)) {
7079

71-
assertFileCountFound(jarFrontendResources, 0);
80+
assertFileCountFound(jarFrontendResources, 0);
7281

73-
createFile(rootPorjectResourceFrontend + "/somestyles.css");
74-
assertFileCountFound(jarFrontendResources, 1);
82+
createFile(rootProjectResourceFrontend + "/somestyles.css");
83+
assertFileCountFound(jarFrontendResources, 1);
7584

76-
createFile(subProjectLegacyFrontend + "/somejs.js");
77-
assertFileCountFound(jarFrontendResources, 2);
85+
createFile(subProjectLegacyFrontend + "/somejs.js");
86+
assertFileCountFound(jarFrontendResources, 2);
7887

79-
Assert.assertEquals("somestyles.css",
80-
jarFrontendResources.listFiles()[0].getName());
81-
Assert.assertEquals("somejs.js",
82-
jarFrontendResources.listFiles()[1].getName());
88+
Assert.assertEquals("somestyles.css",
89+
jarFrontendResources.listFiles()[0].getName());
90+
Assert.assertEquals("somejs.js",
91+
jarFrontendResources.listFiles()[1].getName());
92+
}
8393
}
8494
}
8595

8696
@Test
8797
public void externalDependencyWatcher_setAsDefaultForRunnerProjectButNotSubProject_TriggeredForModification()
8898
throws Exception {
89-
File projectFolder = Files.createTempDirectory("projectFolder")
90-
.toFile();
91-
projectFolder.deleteOnExit();
99+
File projectFolder = temporaryFolder.newFolder("projectFolder");
92100

93101
String metaInf = "/src/main/resources/META-INF/";
94102
String rootPorjectResourceFrontend = projectFolder.getAbsolutePath()
@@ -99,9 +107,8 @@ public void externalDependencyWatcher_setAsDefaultForRunnerProjectButNotSubProje
99107
new File(rootPorjectResourceFrontend).mkdirs();
100108
new File(subProjectLegacyFrontend).mkdirs();
101109

102-
File jarFrontendResources = Files
103-
.createTempDirectory("jarFrontendResources").toFile();
104-
jarFrontendResources.deleteOnExit();
110+
File jarFrontendResources = temporaryFolder
111+
.newFolder("jarFrontendResources");
105112

106113
VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
107114
ApplicationConfiguration config = Mockito
@@ -115,33 +122,37 @@ public void externalDependencyWatcher_setAsDefaultForRunnerProjectButNotSubProje
115122
.mockStatic(ApplicationConfiguration.class)) {
116123
appConfig.when(() -> ApplicationConfiguration.get(Mockito.any()))
117124
.thenReturn(config);
118-
new ExternalDependencyWatcher(vaadinContext, jarFrontendResources);
125+
try (var watcher = new ExternalDependencyWatcher(vaadinContext,
126+
jarFrontendResources)) {
119127

120-
assertFileCountFound(jarFrontendResources, 0);
128+
assertFileCountFound(jarFrontendResources, 0);
121129

122-
createFile(rootPorjectResourceFrontend + "/somestyles.css");
123-
assertFileCountFound(jarFrontendResources, 1);
130+
createFile(rootPorjectResourceFrontend + "/somestyles.css");
131+
assertFileCountFound(jarFrontendResources, 1);
124132

125-
createFile(subProjectLegacyFrontend + "/somejs.js");
126-
assertFileCountFound(jarFrontendResources, 1);
133+
createFile(subProjectLegacyFrontend + "/somejs.js");
134+
assertFileCountFound(jarFrontendResources, 1);
127135

128-
Assert.assertEquals("somestyles.css",
129-
jarFrontendResources.listFiles()[0].getName());
136+
Assert.assertEquals("somestyles.css",
137+
jarFrontendResources.listFiles()[0].getName());
138+
}
130139
}
131140
}
132141

133142
private void assertFileCountFound(File directory, int count)
134143
throws InterruptedException {
135-
Thread.sleep(100);
144+
Thread.sleep(500);
145+
File[] files = directory.listFiles();
136146
Assert.assertEquals(
137147
"Wrong amount of copied files found when there should be "
138-
+ count + ".",
139-
count, directory.listFiles().length);
148+
+ count + ". Current files were: "
149+
+ Arrays.toString(files),
150+
count, files.length);
140151

141152
}
142153

143154
private void createFile(String path) throws IOException {
144155
File newFile = new File(path);
145-
newFile.createNewFile();
156+
Files.writeString(newFile.toPath(), "some text");
146157
}
147158
}

0 commit comments

Comments
 (0)