11package com .vaadin .base .devserver ;
22
33import java .io .File ;
4+ import java .io .IOException ;
45import java .nio .file .Files ;
56import java .util .concurrent .atomic .AtomicReference ;
67
78import org .junit .Assert ;
89import 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
1017public 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