2323import java .io .UncheckedIOException ;
2424import java .nio .file .Files ;
2525import java .nio .file .Path ;
26+ import java .nio .file .StandardOpenOption ;
2627import java .util .List ;
2728
2829import 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}
0 commit comments