Skip to content

Commit a68c8b0

Browse files
authored
fix: also scan changed files for routes (#18823)
* fix: also scan changed files for routes * fix test method name
1 parent b042a2f commit a68c8b0

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

‎flow-tests/test-redeployment/src/test/java/com/vaadin/flow/uitest/ui/DevModeClassCacheIT.java‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.UncheckedIOException;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26+
import java.nio.file.StandardOpenOption;
2627
import java.util.List;
2728

2829
import net.jcip.annotations.NotThreadSafe;
@@ -42,6 +43,20 @@ public class DevModeClassCacheIT extends AbstractReloadIT {
4243
private static final Path VIEW_PATH = Path.of("com", "vaadin", "flow",
4344
"uitest", "ui", "reloadaddedviews");
4445

46+
private static final String NEW_CLASS_WITH_ROUTE = """
47+
package com.vaadin.flow.uitest.ui.reloadaddedviews;
48+
import com.vaadin.flow.component.html.Div;
49+
import com.vaadin.flow.router.Route;
50+
@Route("compiled-at-runtime")
51+
public class MyView extends Div {}
52+
""";
53+
54+
private static final String NEW_CLASS_WITHOUT_ROUTE = """
55+
package com.vaadin.flow.uitest.ui.reloadaddedviews;
56+
import com.vaadin.flow.component.html.Div;
57+
public class MyView extends Div {}
58+
""";
59+
4560
@Override
4661
protected String getTestPath() {
4762
return super.getTestPath().replace("/view", "");
@@ -80,8 +95,9 @@ public void testDevModeClassCachePopulated() {
8095
"com.vaadin.flow.uitest.ui",
8196
allSpans.get(5).getText().split(":")[1]);
8297

83-
// Ensure newly created classes are correctly added to the cache
84-
createNewViewReloadAndWait();
98+
// Ensure newly created classes in packages not previously used for
99+
// routes are correctly added to the cache
100+
createOrUpdateViewReloadAndWait(true);
85101

86102
waitForElementPresent(By.id("last-span"));
87103

@@ -96,10 +112,28 @@ public void testDevModeClassCachePopulated() {
96112
Assert.assertEquals("Unexpected cached route packages.",
97113
"com.vaadin.flow.uitest.ui,com.vaadin.flow.uitest.ui.reloadaddedviews",
98114
allSpans.get(5).getText().split(":")[1]);
115+
116+
// Modify the class to remove Route annotation and ensure the package is
117+
// removed from route packages
118+
createOrUpdateViewReloadAndWait(false);
119+
waitForElementPresent(By.id("last-span"));
120+
allSpans = $("span").all();
121+
Assert.assertEquals("Unexpected cached route packages.",
122+
"com.vaadin.flow.uitest.ui",
123+
allSpans.get(5).getText().split(":")[1]);
124+
125+
// Modify the class to add Route annotation and ensure the package is
126+
// once again added to route packages
127+
createOrUpdateViewReloadAndWait(true);
128+
waitForElementPresent(By.id("last-span"));
129+
allSpans = $("span").all();
130+
Assert.assertEquals("Unexpected cached route packages.",
131+
"com.vaadin.flow.uitest.ui,com.vaadin.flow.uitest.ui.reloadaddedviews",
132+
allSpans.get(5).getText().split(":")[1]);
99133
}
100134

101-
// create class on the fly
102-
protected void createNewViewReloadAndWait() {
135+
// create or modify class on the fly
136+
protected void createOrUpdateViewReloadAndWait(boolean withRoute) {
103137
String viewId = getViewId();
104138

105139
Path baseDir = new File(System.getProperty("user.dir", ".")).toPath();
@@ -109,13 +143,10 @@ protected void createNewViewReloadAndWait() {
109143
Path sourceFile = sourcePath.resolve("MyView.java");
110144
try {
111145
Files.createDirectories(sourcePath);
112-
Files.writeString(sourceFile, """
113-
package com.vaadin.flow.uitest.ui.reloadaddedviews;
114-
import com.vaadin.flow.component.html.Div;
115-
import com.vaadin.flow.router.Route;
116-
@Route("compiled-at-runtime")
117-
public class MyView extends Div {}
118-
""");
146+
Files.writeString(sourceFile,
147+
withRoute ? NEW_CLASS_WITH_ROUTE : NEW_CLASS_WITHOUT_ROUTE,
148+
StandardOpenOption.WRITE, StandardOpenOption.CREATE,
149+
StandardOpenOption.TRUNCATE_EXISTING);
119150
} catch (IOException e) {
120151
throw new UncheckedIOException(e);
121152
}
@@ -132,5 +163,4 @@ public class MyView extends Div {}
132163
}
133164
});
134165
}
135-
136166
}

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/ReloadEvent.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919
import java.util.HashSet;
2020
import java.util.Set;
21+
import java.util.stream.Collectors;
2122

2223
/**
2324
* This event is fired by {@link ReloadListener} when Spring Boot DevTools is
@@ -40,4 +41,20 @@ public Set<String> getChangedClasses() {
4041
public Set<String> getRemovedClasses() {
4142
return removedClasses;
4243
}
44+
45+
public Set<String> getAddedPackages() {
46+
return extractPackageNames(addedClasses);
47+
}
48+
49+
public Set<String> getChangedPackages() {
50+
return extractPackageNames(changedClasses);
51+
}
52+
53+
private Set<String> extractPackageNames(Set<String> classNames) {
54+
return classNames.stream()
55+
.map(name -> name.contains(".")
56+
? name.substring(0, name.lastIndexOf("."))
57+
: name)
58+
.collect(Collectors.toSet());
59+
}
4360
}

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinServletContextInitializer.java‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -735,19 +735,14 @@ private void initializeDevModeClassCache() {
735735
((ConfigurableApplicationContext) appContext)
736736
.addApplicationListener(new ReloadListener(e -> {
737737
// Updates cached white list and route packages
738-
Set<String> addedPackages = new HashSet<>();
739-
e.getAddedClasses().forEach(c -> {
740-
if (c.contains("/")) {
741-
c = c.replaceAll("/", ".");
742-
}
743-
if (c.contains(".")) {
744-
addedPackages.add(c.substring(0,
745-
c.lastIndexOf(".")));
746-
}
747-
});
748738
ReloadCache.dynamicWhiteList
749-
.addAll(addedPackages);
750-
ReloadCache.routePackages.addAll(addedPackages);
739+
.addAll(e.getAddedPackages());
740+
ReloadCache.dynamicWhiteList
741+
.addAll(e.getChangedPackages());
742+
ReloadCache.routePackages
743+
.addAll(e.getAddedPackages());
744+
ReloadCache.routePackages
745+
.addAll(e.getChangedPackages());
751746
}));
752747
}
753748
}

0 commit comments

Comments
 (0)