22
22
import java .nio .file .Path ;
23
23
import java .nio .file .attribute .FileTime ;
24
24
25
+ import net .jcip .annotations .NotThreadSafe ;
25
26
import org .apache .commons .io .IOUtils ;
26
27
import org .hamcrest .CoreMatchers ;
27
28
import org .hamcrest .MatcherAssert ;
30
31
import org .junit .Rule ;
31
32
import org .junit .Test ;
32
33
import org .junit .rules .TemporaryFolder ;
34
+ import org .mockito .MockedStatic ;
33
35
import org .mockito .Mockito ;
34
36
35
37
import com .vaadin .flow .di .Lookup ;
38
40
import static com .vaadin .flow .server .frontend .TaskGenerateTsDefinitions .TS_DEFINITIONS ;
39
41
import static java .nio .charset .StandardCharsets .UTF_8 ;
40
42
43
+ @ NotThreadSafe
41
44
public class TaskGenerateTsDefinitionsTest {
42
45
@ Rule
43
46
public TemporaryFolder temporaryFolder = new TemporaryFolder ();
@@ -50,6 +53,7 @@ public void setUp() throws IOException {
50
53
Options options = new Options (Mockito .mock (Lookup .class ), outputFolder );
51
54
52
55
taskGenerateTsDefinitions = new TaskGenerateTsDefinitions (options );
56
+ taskGenerateTsDefinitions .warningEmitted = false ;
53
57
}
54
58
55
59
@ Test
@@ -199,7 +203,7 @@ public void tsDefinition_oldHillaContents_ignoringSingleLineComments_tsDefinitio
199
203
}
200
204
201
205
@ Test
202
- public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExceptionThrown ()
206
+ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndWarningLogged ()
203
207
throws Exception {
204
208
Path typesTSfile = new File (outputFolder , "types.d.ts" ).toPath ();
205
209
String originalContent = """
@@ -211,11 +215,17 @@ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExcepti
211
215
export type JTDForm = typeof jtdForms[number];
212
216
""" ;
213
217
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 ));
219
229
Assert .assertFalse (
220
230
"Should not generate types.d.ts when already existing" ,
221
231
taskGenerateTsDefinitions .shouldGenerate ());
@@ -227,17 +237,24 @@ public void customTsDefinition_missingFlowContents_tsDefinitionUpdatedAndExcepti
227
237
}
228
238
229
239
@ Test
230
- public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndExceptionThrown ()
240
+ public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndWarningLogged ()
231
241
throws Exception {
232
242
Path typesTSfile = new File (outputFolder , "types.d.ts" ).toPath ();
233
243
String originalContent = "import type { SchemaObject } from \" ../../types\" ;"
234
244
+ System .lineSeparator () + readPreviousContent ();
235
245
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
+
241
258
Assert .assertFalse (
242
259
"Should not generate types.d.ts when already existing" ,
243
260
taskGenerateTsDefinitions .shouldGenerate ());
@@ -248,6 +265,35 @@ public void customTsDefinition_oldFlowContents_tsDefinitionUpdatedAndExceptionTh
248
265
assertBackupFileCreated (originalContent );
249
266
}
250
267
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
+
251
297
@ Test
252
298
public void customTsDefinition_flowContents_tsDefinitionNotUpdated ()
253
299
throws Exception {
@@ -366,9 +412,12 @@ public void customTsDefinition_differentCSSModuleDefinition_tsDefinitionNotUpdat
366
412
367
413
private void assertBackupFileCreated (String originalContent )
368
414
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 ];
372
421
Assert .assertTrue ("Original types.d.ts backup should exist" ,
373
422
backupFile .exists ());
374
423
Assert .assertEquals (originalContent ,
0 commit comments