Skip to content

Commit 1f93ceb

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

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ private void enhanceClass(File classFile) {
222222
}
223223

224224
private void writeByteCodeToFile(byte[] bytes, File file) {
225-
getLog().debug("Writing byte code to file: " + file);
225+
getLog().debug(WRITING_BYTE_CODE_TO_FILE.formatted(file));
226226
if (clearFile(file)) {
227227
try {
228228
Files.write( file.toPath(), bytes);
229-
getLog().debug("" + bytes.length + " bytes were succesfully written to file: " + file);
229+
getLog().debug(AMOUNT_BYTES_WRITTEN_TO_FILE.formatted(bytes.length, file));
230230
}
231231
catch (FileNotFoundException e) {
232-
getLog().error( "Error opening file for writing : " + file, e );
232+
getLog().error(ERROR_OPENING_FILE_FOR_WRITING.formatted(file), e );
233233
}
234234
catch (IOException e) {
235-
getLog().error( "Error writing bytes to file : " + file, e );
235+
getLog().error(ERROR_WRITING_BYTES_TO_FILE.formatted(file), e );
236236
}
237237
}
238238
}
@@ -268,8 +268,12 @@ private boolean clearFile(File file) {
268268
// error messages
269269
static final String UNABLE_TO_CREATE_FILE = "Unable to create file: %s";
270270
static final String UNABLE_TO_DELETE_FILE = "Unable to delete file: %s";
271+
static final String ERROR_WRITING_BYTES_TO_FILE = "Error writing bytes to file : %s";
272+
static final String ERROR_OPENING_FILE_FOR_WRITING = "Error opening file for writing : %s";
271273

272274
// debug messages
273275
static final String TRYING_TO_CLEAR_FILE = "Trying to clear the contents of file: %s";
276+
static final String AMOUNT_BYTES_WRITTEN_TO_FILE = "%s bytes were succesfully written to file: %s";
277+
static final String WRITING_BYTE_CODE_TO_FILE = "Writing byte code to file: %s";
274278

275279
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ void testWriteByteCodeToFile() throws Exception {
336336
assertTrue(modified > 0);
337337
// File should be contain 'foobar'
338338
assertEquals(new String(Files.readAllBytes(fooTxtFile.toPath())), "foobar");
339+
// check log messages
340+
assertEquals(4, logMessages.size());
341+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.WRITING_BYTE_CODE_TO_FILE.formatted(fooTxtFile)));
342+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.TRYING_TO_CLEAR_FILE.formatted(fooTxtFile)));
343+
assertTrue(logMessages.contains(INFO + EnhanceMojo.SUCCESFULLY_CLEARED_FILE.formatted(fooTxtFile)));
344+
assertTrue(logMessages.contains(DEBUG + EnhanceMojo.AMOUNT_BYTES_WRITTEN_TO_FILE.formatted(6, fooTxtFile)));
339345
}
340346

341347
@Test

0 commit comments

Comments
 (0)