Skip to content

Commit 944ae82

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

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,38 @@ private void writeByteCodeToFile(byte[] bytes, File file) {
238238
}
239239

240240
private boolean clearFile(File file) {
241-
getLog().debug("Trying to clear the contents of file: " + file);
241+
getLog().debug(TRYING_TO_CLEAR_FILE.formatted(file));
242242
boolean success = false;
243243
if ( file.delete() ) {
244244
try {
245245
if ( !file.createNewFile() ) {
246-
getLog().error( "Unable to create file: " + file);
246+
getLog().error(UNABLE_TO_CREATE_FILE.formatted(file));
247247
} else {
248-
getLog().info("Succesfully cleared the contents of file: " + file);
248+
getLog().info(SUCCESFULLY_CLEARED_FILE.formatted(file));
249249
success = true;
250250
}
251251
}
252252
catch (IOException e) {
253-
getLog().warn( "Problem clearing file for writing out enhancements [" + file + "]", e);
253+
getLog().warn(PROBLEM_CLEARING_FILE.formatted(file), e);
254254
}
255255
}
256256
else {
257-
getLog().error( "Unable to delete file : " + file);
257+
getLog().error(UNABLE_TO_DELETE_FILE.formatted(file));
258258
}
259259
return success;
260260
}
261+
262+
// info messages
263+
static final String SUCCESFULLY_CLEARED_FILE = "Succesfully cleared the contents of file: %s";
264+
265+
// warning messages
266+
static final String PROBLEM_CLEARING_FILE = "Problem clearing file for writing out enhancements [ %s ]";
267+
268+
// error messages
269+
static final String UNABLE_TO_CREATE_FILE = "Unable to create file: %s";
270+
static final String UNABLE_TO_DELETE_FILE = "Unable to delete file: %s";
271+
272+
// debug messages
273+
static final String TRYING_TO_CLEAR_FILE = "Trying to clear the contents of file: %s";
261274

262275
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ void testClearFile() throws Exception {
312312
// last modification 'after' should be after 'before'
313313
assertNotEquals(0, modified);
314314
assertTrue(modified > 0);
315+
// check log messages
316+
assertEquals(4, logMessages.size());
317+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_CLEAR_FILE.formatted("foobar")));
318+
assertTrue(logMessages.contains(ERROR + EnhanceMojo.UNABLE_TO_DELETE_FILE.formatted("foobar")));
319+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_CLEAR_FILE.formatted(fooTxtFile)));
320+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_CLEARED_FILE.formatted(fooTxtFile)));
315321
}
316322

317323
@Test
@@ -528,17 +534,22 @@ private Log createLog() {
528534
@Override
529535
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
530536
if ("info".equals(method.getName())) {
531-
logMessages.add("[INFO] " + args[0]);
537+
logMessages.add(INFO + args[0]);
532538
} else if ("warn".equals(method.getName())) {
533-
logMessages.add("[WARNING] " + args[0]);
539+
logMessages.add(WARNING + args[0]);
534540
} else if ("error".equals(method.getName())) {
535-
logMessages.add("[ERROR] " + args[0]);
541+
logMessages.add(ERROR + args[0]);
536542
} else if ("debug".equals(method.getName())) {
537-
logMessages.add("[DEBUG] " + args[0]);
543+
logMessages.add(DEBUG + args[0]);
538544
}
539545
return null;
540546
}
541547
});
542548
}
549+
550+
static final String DEBUG = "[DEBUG] ";
551+
static final String ERROR = "[ERROR] ";
552+
static final String WARNING = "[WARNING] ";
553+
static final String INFO = "[INFO] ";
543554

544555
}

0 commit comments

Comments
 (0)