Skip to content

Commit

Permalink
Merge pull request #883 from uhafner/merge-errors
Browse files Browse the repository at this point in the history
Error titles should not be printed for merged messages
  • Loading branch information
uhafner authored Dec 21, 2023
2 parents 523cefc + 4eb1cf6 commit d6fc9f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public void logError(final String message) {
lock.lock();
try {
if (lines < maxLines) {
if (StringUtils.isNotBlank(title) && errorMessages.isEmpty()) {
errorMessages.add(title);
}
errorMessages.add(message);
}
lines++;
Expand Down Expand Up @@ -155,13 +158,11 @@ public void logError(final String format, final Object... args) {
*/
@FormatMethod
public void logException(final Exception exception, final String format, final Object... args) {
logError(format, args);

lock.lock();
try {
if (lines < maxLines) {
errorMessages.add(String.format(format, args));
errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception)));
}
lines++;
errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception)));
}
finally {
lock.unlock();

Check warning on line 168 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 168 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to java/util/concurrent/locks/ReentrantLock::unlock (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)
Expand Down Expand Up @@ -215,9 +216,6 @@ public List<String> getErrorMessages() {
if (errorMessages.isEmpty()) {
return messages;

Check warning on line 217 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 217 (EmptyObjectReturnValsMutator)
Raw output
Survived mutations:
- replaced return value with Collections.emptyList for edu/hm/hafner/util/FilteredLog::getErrorMessages (org.pitest.mutationtest.engine.gregor.mutators.returns.EmptyObjectReturnValsMutator)
}
if (StringUtils.isNotBlank(title)) {
messages.add(title);
}
messages.addAll(errorMessages);
if (lines > maxLines) {
messages.add(String.format(" ... skipped logging of %d additional errors ...", lines - maxLines));
Expand Down Expand Up @@ -255,7 +253,6 @@ public void merge(final FilteredLog other) {
try {
infoMessages.addAll(other.getInfoMessages());
errorMessages.addAll(other.getErrorMessages());
lines += other.lines;
}
finally {
lock.unlock();

Check warning on line 258 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 258 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to java/util/concurrent/locks/ReentrantLock::unlock (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,24 @@ void shouldMergeLogger() {

assertThat(parent).hasOnlyInfoMessages("parent Info 1", "child Info 1")
.hasOnlyErrorMessages("Parent Errors", "parent Error 1", "Child Errors", "child Error 1");
assertThat(parent.size()).isEqualTo(2);
assertThat(parent.size()).isEqualTo(1);
}

@Test
void shouldSkipEmptyErrorLogWhenMerging() {
var parent = new FilteredLog("Parent Errors");

parent.logInfo("parent Info 1");

var child = new FilteredLog("Child Errors");
child.logInfo("child Info 1");
child.logError("child Error 1");

parent.merge(child);

assertThat(parent).hasOnlyInfoMessages("parent Info 1", "child Info 1")
.hasOnlyErrorMessages("Child Errors", "child Error 1");
assertThat(parent.size()).isZero();
}

@Test
Expand Down

0 comments on commit d6fc9f8

Please sign in to comment.