2222import java .nio .file .Path ;
2323import java .nio .file .attribute .FileTime ;
2424
25+ import net .jcip .annotations .NotThreadSafe ;
2526import org .apache .commons .io .IOUtils ;
2627import org .hamcrest .CoreMatchers ;
2728import org .hamcrest .MatcherAssert ;
3031import org .junit .Rule ;
3132import org .junit .Test ;
3233import org .junit .rules .TemporaryFolder ;
34+ import org .mockito .MockedStatic ;
3335import org .mockito .Mockito ;
3436
3537import com .vaadin .flow .di .Lookup ;
3840import static com .vaadin .flow .server .frontend .TaskGenerateTsDefinitions .TS_DEFINITIONS ;
3941import static java .nio .charset .StandardCharsets .UTF_8 ;
4042
43+ @ NotThreadSafe
4144public class TaskGenerateTsDefinitionsTest {
4245 @ Rule
4346 public TemporaryFolder temporaryFolder = new TemporaryFolder ();
@@ -50,6 +53,7 @@ public void setUp() throws IOException {
5053 Options options = new Options (Mockito .mock (Lookup .class ), outputFolder );
5154
5255 taskGenerateTsDefinitions = new TaskGenerateTsDefinitions (options );
56+ taskGenerateTsDefinitions .warningEmitted = false ;
5357 }
5458
5559 @ Test
@@ -199,7 +203,7 @@ public void tsDefinition_oldHillaContents_ignoringSingleLineComments_tsDefinitio
199203 }
200204
201205 @ Test
202- public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExceptionThrown ()
206+ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndWarningLogged ()
203207 throws Exception {
204208 Path typesTSfile = new File (outputFolder , "types.d.ts" ).toPath ();
205209 String originalContent = """
@@ -211,11 +215,17 @@ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExcepti
211215 export type JTDForm = typeof jtdForms[number];
212216 """ ;
213217 Files .writeString (typesTSfile , originalContent );
214- ExecutionFailedException exception = Assert .assertThrows (
215- ExecutionFailedException .class ,
216- taskGenerateTsDefinitions ::execute );
217- MatcherAssert .assertThat (exception .getMessage (), CoreMatchers
218- .containsString (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
218+
219+ MockLogger logger = new MockLogger ();
220+ try (MockedStatic <AbstractTaskClientGenerator > client = Mockito
221+ .mockStatic (AbstractTaskClientGenerator .class ,
222+ Mockito .CALLS_REAL_METHODS )) {
223+ client .when (() -> AbstractTaskClientGenerator .log ())
224+ .thenReturn (logger );
225+ taskGenerateTsDefinitions .execute ();
226+ }
227+ Assert .assertTrue (logger .getLogs ()
228+ .contains (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
219229 Assert .assertFalse (
220230 "Should not generate types.d.ts when already existing" ,
221231 taskGenerateTsDefinitions .shouldGenerate ());
@@ -227,17 +237,24 @@ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExcepti
227237 }
228238
229239 @ Test
230- public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndExceptionThrown ()
240+ public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndWarningLogged ()
231241 throws Exception {
232242 Path typesTSfile = new File (outputFolder , "types.d.ts" ).toPath ();
233243 String originalContent = "import type { SchemaObject } from \" ../../types\" ;"
234244 + System .lineSeparator () + readPreviousContent ();
235245 Files .writeString (typesTSfile , originalContent );
236- ExecutionFailedException exception = Assert .assertThrows (
237- ExecutionFailedException .class ,
238- taskGenerateTsDefinitions ::execute );
239- MatcherAssert .assertThat (exception .getMessage (), CoreMatchers
240- .containsString (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
246+
247+ MockLogger logger = new MockLogger ();
248+ try (MockedStatic <AbstractTaskClientGenerator > client = Mockito
249+ .mockStatic (AbstractTaskClientGenerator .class ,
250+ Mockito .CALLS_REAL_METHODS )) {
251+ client .when (() -> AbstractTaskClientGenerator .log ())
252+ .thenReturn (logger );
253+ taskGenerateTsDefinitions .execute ();
254+ }
255+ Assert .assertTrue (logger .getLogs ()
256+ .contains (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
257+
241258 Assert .assertFalse (
242259 "Should not generate types.d.ts when already existing" ,
243260 taskGenerateTsDefinitions .shouldGenerate ());
@@ -248,6 +265,35 @@ public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndExceptionTh
248265 assertBackupFileCreated (originalContent );
249266 }
250267
268+ @ Test
269+ public void contentUpdateForSecondTime_tsDefinitionUpdatedAndWarningLoggedOnce ()
270+ throws Exception {
271+ Path typesTSfile = new File (outputFolder , "types.d.ts" ).toPath ();
272+ String originalContent = "import type { SchemaObject } from \" ../../types\" ;"
273+ + System .lineSeparator () + readPreviousContent ();
274+ Files .writeString (typesTSfile , originalContent );
275+
276+ MockLogger logger = new MockLogger ();
277+ try (MockedStatic <AbstractTaskClientGenerator > client = Mockito
278+ .mockStatic (AbstractTaskClientGenerator .class ,
279+ Mockito .CALLS_REAL_METHODS )) {
280+ client .when (() -> AbstractTaskClientGenerator .log ())
281+ .thenReturn (logger );
282+ taskGenerateTsDefinitions .execute ();
283+
284+ Assert .assertTrue (logger .getLogs ()
285+ .contains (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
286+
287+ logger .clearLogs ();
288+
289+ Files .writeString (typesTSfile , originalContent );
290+
291+ taskGenerateTsDefinitions .execute ();
292+ }
293+ Assert .assertFalse (logger .getLogs ()
294+ .contains (TaskGenerateTsDefinitions .UPDATE_MESSAGE ));
295+ }
296+
251297 @ Test
252298 public void customTsDefinition_flowContents_tsDefinitionNotUpdated ()
253299 throws Exception {
@@ -366,9 +412,12 @@ public void customTsDefinition_differentCSSModuleDefinition_tsDefinitionNotUpdat
366412
367413 private void assertBackupFileCreated (String originalContent )
368414 throws IOException {
369- File backupFile = new File (
370- taskGenerateTsDefinitions .getGeneratedFile ().getParent (),
371- TS_DEFINITIONS + ".flowBackup" );
415+ File [] backups = taskGenerateTsDefinitions .getGeneratedFile ()
416+ .getParentFile ()
417+ .listFiles (file -> file .getName ().endsWith (".bak" ));
418+ Assert .assertEquals (1 , backups .length );
419+
420+ File backupFile = backups [0 ];
372421 Assert .assertTrue ("Original types.d.ts backup should exist" ,
373422 backupFile .exists ());
374423 Assert .assertEquals (originalContent ,
0 commit comments