Skip to content

Commit 637c1b5

Browse files
committed
HHH-18644: New and improved hibernate-maven-plugin
- Isolate the message strings for method 'EnhanceMojo#performEnhancement()' - Use the isolated messages above and add verifications concerning the logging in 'EnhanceMojoTest#testPerformEnhancement()' Signed-off-by: Koen Aers <[email protected]>
1 parent d3e0d76 commit 637c1b5

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

src/main/java/org/hibernate/orm/tooling/maven/enhance/EnhanceMojo.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ private String determineClassName(File classFile) {
191191
.replace(File.separatorChar, '.');
192192
}
193193

194-
private void performEnhancement() {
195-
getLog().debug("Starting class enhancement") ;
194+
private void performEnhancement() {
195+
getLog().debug(STARTING_CLASS_ENHANCEMENT) ;
196196
for (File classFile : sourceSet) {
197197
long lastModified = classFile.lastModified();
198198
enhanceClass(classFile);
199199
final boolean timestampReset = classFile.setLastModified( lastModified );
200200
if ( !timestampReset ) {
201-
getLog().debug( "Setting lastModified failed for class file: " + classFile);
201+
getLog().debug(SETTING_LASTMODIFIED_FAILED_FOR_CLASS_FILE.formatted(classFile));
202202
}
203203
}
204204
getLog().debug("Ending class enhancement") ;
@@ -259,26 +259,29 @@ private boolean clearFile(File file) {
259259
return success;
260260
}
261261

262-
// info messages
263-
static final String SUCCESFULLY_CLEARED_FILE = "Succesfully cleared the contents of file: %s";
264-
static final String SUCCESFULLY_ENHANCED_CLASS_FILE = "Succesfully enhanced class file: %s";
265-
static final String SKIPPING_FILE = "Skipping file: %s";
266-
267-
// warning messages
268-
static final String PROBLEM_CLEARING_FILE = "Problem clearing file for writing out enhancements [ %s ]";
269-
270-
// error messages
271-
static final String UNABLE_TO_CREATE_FILE = "Unable to create file: %s";
272-
static final String UNABLE_TO_DELETE_FILE = "Unable to delete file: %s";
273-
static final String ERROR_WRITING_BYTES_TO_FILE = "Error writing bytes to file : %s";
274-
static final String ERROR_OPENING_FILE_FOR_WRITING = "Error opening file for writing : %s";
275-
static final String ERROR_WHILE_ENHANCING_CLASS_FILE = "An exception occurred while trying to class file: %s";
276-
277-
// debug messages
278-
static final String TRYING_TO_CLEAR_FILE = "Trying to clear the contents of file: %s";
279-
static final String AMOUNT_BYTES_WRITTEN_TO_FILE = "%s bytes were succesfully written to file: %s";
280-
static final String WRITING_BYTE_CODE_TO_FILE = "Writing byte code to file: %s";
281-
static final String DETERMINE_CLASS_NAME_FOR_FILE = "Determining class name for file: %s";
282-
static final String TRYING_TO_ENHANCE_CLASS_FILE = "Trying to enhance class file: %s";
262+
// info messages
263+
static final String SUCCESFULLY_CLEARED_FILE = "Succesfully cleared the contents of file: %s";
264+
static final String SUCCESFULLY_ENHANCED_CLASS_FILE = "Succesfully enhanced class file: %s";
265+
static final String SKIPPING_FILE = "Skipping file: %s";
266+
267+
// warning messages
268+
static final String PROBLEM_CLEARING_FILE = "Problem clearing file for writing out enhancements [ %s ]";
269+
270+
// error messages
271+
static final String UNABLE_TO_CREATE_FILE = "Unable to create file: %s";
272+
static final String UNABLE_TO_DELETE_FILE = "Unable to delete file: %s";
273+
static final String ERROR_WRITING_BYTES_TO_FILE = "Error writing bytes to file : %s";
274+
static final String ERROR_OPENING_FILE_FOR_WRITING = "Error opening file for writing : %s";
275+
static final String ERROR_WHILE_ENHANCING_CLASS_FILE = "An exception occurred while trying to class file: %s";
276+
277+
// debug messages
278+
static final String TRYING_TO_CLEAR_FILE = "Trying to clear the contents of file: %s";
279+
static final String AMOUNT_BYTES_WRITTEN_TO_FILE = "%s bytes were succesfully written to file: %s";
280+
static final String WRITING_BYTE_CODE_TO_FILE = "Writing byte code to file: %s";
281+
static final String DETERMINE_CLASS_NAME_FOR_FILE = "Determining class name for file: %s";
282+
static final String TRYING_TO_ENHANCE_CLASS_FILE = "Trying to enhance class file: %s";
283+
static final String STARTING_CLASS_ENHANCEMENT = "Starting class enhancement";
284+
static final String SETTING_LASTMODIFIED_FAILED_FOR_CLASS_FILE = "Setting lastModified failed for class file: %s";
285+
static final String ENDING_CLASS_ENHANCEMENT = "Ending class enhancement";
283286

284287
}

src/test/java/org/hibernate/orm/tooling/maven/enhance/EnhanceMojoTest.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,28 +379,46 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
379379
enhancerField.set(enhanceMojo, enhancer);
380380
assertEquals(0, calls.get(0));
381381
enhanceClassMethod.invoke(enhanceMojo, barClassFile);
382-
// check log messages
383-
for (String s: logMessages) {
384-
System.out.println(s);
385-
}
386382
long afterFirstRun = barClassFile.lastModified();
387383
assertEquals(1, calls.get(0));
388384
assertTrue(afterFirstRun >= beforeRuns);
389385
assertEquals("foobar", new String(Files.readAllBytes(barClassFile.toPath())));
386+
// verify log messages
387+
assertEquals(7, logMessages.size());
388+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_ENHANCE_CLASS_FILE.formatted(barClassFile)));
389+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.DETERMINE_CLASS_NAME_FOR_FILE.formatted(barClassFile)));
390+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.WRITING_BYTE_CODE_TO_FILE.formatted(barClassFile)));
391+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_CLEAR_FILE.formatted(barClassFile)));
392+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_CLEARED_FILE.formatted(barClassFile)));
393+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.AMOUNT_BYTES_WRITTEN_TO_FILE.formatted("foobar".length(), barClassFile)));
394+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_ENHANCED_CLASS_FILE.formatted(barClassFile)));
395+
// Second Run -> file is not modified
396+
logMessages.clear();
390397
enhanceClassMethod.invoke(enhanceMojo, barClassFile);
391398
long afterSecondRun = barClassFile.lastModified();
392399
assertEquals(2, calls.get(0));
393400
assertEquals(afterSecondRun, afterFirstRun);
394401
assertEquals("foobar", new String(Files.readAllBytes(barClassFile.toPath())));
402+
// verify log messages
403+
assertEquals(3, logMessages.size());
404+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_ENHANCE_CLASS_FILE.formatted(barClassFile)));
405+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.DETERMINE_CLASS_NAME_FOR_FILE.formatted(barClassFile)));
406+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SKIPPING_FILE.formatted(barClassFile)));
407+
// Third Run -> exception!
408+
logMessages.clear();
395409
try {
396410
enhanceClassMethod.invoke(enhanceMojo, barClassFile);
397411
fail();
398412
} catch (Throwable e) {
399-
System.out.println(e.getMessage());
400413
long afterThirdRun = barClassFile.lastModified();
401414
assertEquals(3, calls.get(0));
402415
assertEquals(afterThirdRun, afterFirstRun);
403416
assertEquals("foobar", new String(Files.readAllBytes(barClassFile.toPath())));
417+
// verify log messages
418+
assertEquals(3, logMessages.size());
419+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_ENHANCE_CLASS_FILE.formatted(barClassFile)));
420+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.DETERMINE_CLASS_NAME_FOR_FILE.formatted(barClassFile)));
421+
assertTrue(logMessages.contains(ERROR + EnhanceMojo.ERROR_WHILE_ENHANCING_CLASS_FILE.formatted(barClassFile)));
404422
}
405423
}
406424

@@ -435,6 +453,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
435453
assertTrue(hasRun.contains(true));
436454
assertEquals("foobar", new String(Files.readAllBytes(barClassFile.toPath())));
437455
assertEquals(lastModified, barClassFile.lastModified());
456+
// verify the log messages
457+
assertEquals(9, logMessages.size());
458+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.STARTING_CLASS_ENHANCEMENT));
459+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_ENHANCE_CLASS_FILE.formatted(barClassFile)));
460+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.DETERMINE_CLASS_NAME_FOR_FILE.formatted(barClassFile)));
461+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.WRITING_BYTE_CODE_TO_FILE.formatted(barClassFile)));
462+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_CLEAR_FILE.formatted(barClassFile)));
463+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_CLEARED_FILE.formatted(barClassFile)));
464+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.AMOUNT_BYTES_WRITTEN_TO_FILE.formatted("foobar".length(), barClassFile)));
465+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_ENHANCED_CLASS_FILE.formatted(barClassFile)));
466+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.ENDING_CLASS_ENHANCEMENT));
438467
}
439468

440469
@Test

0 commit comments

Comments
 (0)