Skip to content

Commit 4f29383

Browse files
authored
fix: watch active project resources (#21099)
* fix: watch active project resources * add tests for external file watcher * fix typo, increase sleep time * fix non-matching case
1 parent bc78ab7 commit 4f29383

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/ExternalDependencyWatcher.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public ExternalDependencyWatcher(VaadinContext context,
5757
}
5858
}
5959
} else {
60+
// Always watch src/main/resources/META-INF from active project
61+
hotdeployDependencyFolders.add(projectFolder.getAbsolutePath());
62+
6063
File pomFile = new File(projectFolder, "pom.xml");
6164
File parentPomFile = MavenUtils
6265
.getParentPomOfMultiModuleProject(pomFile);
@@ -78,10 +81,6 @@ public ExternalDependencyWatcher(VaadinContext context,
7881
for (String hotdeployDependencyFolder : hotdeployDependencyFolders) {
7982
Path moduleFolder = projectFolder.toPath()
8083
.resolve(hotdeployDependencyFolder).normalize();
81-
if (moduleFolder.equals(projectFolder.toPath())) {
82-
// Don't watch the active module
83-
continue;
84-
}
8584
Path metaInf = moduleFolder
8685
.resolve(Path.of("src", "main", "resources", "META-INF"));
8786
if (!watchDependencyFolder(metaInf.toFile(),

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

+118
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.vaadin.base.devserver;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.nio.file.Files;
56
import java.util.concurrent.atomic.AtomicReference;
67

78
import org.junit.Assert;
89
import org.junit.Test;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.Mockito;
12+
13+
import com.vaadin.flow.server.InitParameters;
14+
import com.vaadin.flow.server.VaadinContext;
15+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
916

1017
public class FileWatcherTest {
1118

@@ -26,4 +33,115 @@ public void fileWatcherTriggeredForModification() throws Exception {
2633
Thread.sleep(50); // The watcher is supposed to be triggered immediately
2734
Assert.assertEquals(newFile, changed.get());
2835
}
36+
37+
@Test
38+
public void externalDependencyWatcher_setViaParameter_TriggeredForModification()
39+
throws Exception {
40+
File projectFolder = Files.createTempDirectory("projectFolder")
41+
.toFile();
42+
projectFolder.deleteOnExit();
43+
44+
String metaInf = "/src/main/resources/META-INF/";
45+
String rootPorjectResourceFrontend = projectFolder.getAbsolutePath()
46+
+ metaInf + "resources/frontend";
47+
String subProjectLegacyFrontend = projectFolder.getAbsolutePath()
48+
+ "/fakeproject" + metaInf + "frontend";
49+
50+
new File(rootPorjectResourceFrontend).mkdirs();
51+
new File(subProjectLegacyFrontend).mkdirs();
52+
53+
File jarFrontendResources = Files
54+
.createTempDirectory("jarFrontendResources").toFile();
55+
jarFrontendResources.deleteOnExit();
56+
57+
VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
58+
ApplicationConfiguration config = Mockito
59+
.mock(ApplicationConfiguration.class);
60+
Mockito.when(config.getStringProperty(
61+
InitParameters.FRONTEND_HOTDEPLOY_DEPENDENCIES, null))
62+
.thenReturn("./,./fakeproject");
63+
Mockito.when(config.getProjectFolder()).thenReturn(projectFolder);
64+
65+
try (MockedStatic<ApplicationConfiguration> appConfig = Mockito
66+
.mockStatic(ApplicationConfiguration.class)) {
67+
appConfig.when(() -> ApplicationConfiguration.get(Mockito.any()))
68+
.thenReturn(config);
69+
new ExternalDependencyWatcher(vaadinContext, jarFrontendResources);
70+
71+
assertFileCountFound(jarFrontendResources, 0);
72+
73+
createFile(rootPorjectResourceFrontend + "/somestyles.css");
74+
assertFileCountFound(jarFrontendResources, 1);
75+
76+
createFile(subProjectLegacyFrontend + "/somejs.js");
77+
assertFileCountFound(jarFrontendResources, 2);
78+
79+
Assert.assertEquals("somestyles.css",
80+
jarFrontendResources.listFiles()[0].getName());
81+
Assert.assertEquals("somejs.js",
82+
jarFrontendResources.listFiles()[1].getName());
83+
}
84+
}
85+
86+
@Test
87+
public void externalDependencyWatcher_setAsDefaultForRunnerProjectButNotSubProject_TriggeredForModification()
88+
throws Exception {
89+
File projectFolder = Files.createTempDirectory("projectFolder")
90+
.toFile();
91+
projectFolder.deleteOnExit();
92+
93+
String metaInf = "/src/main/resources/META-INF/";
94+
String rootPorjectResourceFrontend = projectFolder.getAbsolutePath()
95+
+ metaInf + "resources/frontend";
96+
String subProjectLegacyFrontend = projectFolder.getAbsolutePath()
97+
+ "/fakeproject" + metaInf + "frontend";
98+
99+
new File(rootPorjectResourceFrontend).mkdirs();
100+
new File(subProjectLegacyFrontend).mkdirs();
101+
102+
File jarFrontendResources = Files
103+
.createTempDirectory("jarFrontendResources").toFile();
104+
jarFrontendResources.deleteOnExit();
105+
106+
VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
107+
ApplicationConfiguration config = Mockito
108+
.mock(ApplicationConfiguration.class);
109+
Mockito.when(config.getStringProperty(
110+
InitParameters.FRONTEND_HOTDEPLOY_DEPENDENCIES, null))
111+
.thenReturn(null);
112+
Mockito.when(config.getProjectFolder()).thenReturn(projectFolder);
113+
114+
try (MockedStatic<ApplicationConfiguration> appConfig = Mockito
115+
.mockStatic(ApplicationConfiguration.class)) {
116+
appConfig.when(() -> ApplicationConfiguration.get(Mockito.any()))
117+
.thenReturn(config);
118+
new ExternalDependencyWatcher(vaadinContext, jarFrontendResources);
119+
120+
assertFileCountFound(jarFrontendResources, 0);
121+
122+
createFile(rootPorjectResourceFrontend + "/somestyles.css");
123+
assertFileCountFound(jarFrontendResources, 1);
124+
125+
createFile(subProjectLegacyFrontend + "/somejs.js");
126+
assertFileCountFound(jarFrontendResources, 1);
127+
128+
Assert.assertEquals("somestyles.css",
129+
jarFrontendResources.listFiles()[0].getName());
130+
}
131+
}
132+
133+
private void assertFileCountFound(File directory, int count)
134+
throws InterruptedException {
135+
Thread.sleep(100);
136+
Assert.assertEquals(
137+
"Wrong amount of copied files found when there should be "
138+
+ count + ".",
139+
count, directory.listFiles().length);
140+
141+
}
142+
143+
private void createFile(String path) throws IOException {
144+
File newFile = new File(path);
145+
newFile.createNewFile();
146+
}
29147
}

0 commit comments

Comments
 (0)