Skip to content

Commit

Permalink
Added a couple more methods to the logback assert test helper (#6004)
Browse files Browse the repository at this point in the history
* add negative method

* add throwable matcher for logback

* logback asserts

* merge master

* update StaticLogbackTestExtension usage
  • Loading branch information
fil512 committed Jun 15, 2024
1 parent 57d1815 commit c1c6d4f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
19 changes: 19 additions & 0 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR - Core Library
* %%
* Copyright (C) 2014 - 2024 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.fhir.parser;

import ca.uhn.fhir.parser.path.EncodeContextPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Server Test Utilities
* %%
* Copyright (C) 2014 - 2024 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.fhir.jpa.term;

import ca.uhn.fhir.context.FhirContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,37 @@ protected LogEventAssert toAssert(ILoggingEvent value, String description) {
protected LogEventIterableAssert newAbstractIterableAssert(Iterable<? extends ILoggingEvent> iterable) {
return new LogEventIterableAssert((Collection<ILoggingEvent>) iterable);
}

public LogEventIterableAssert doesNotHaveEventWithMessage(String thePartial) {
isNotNull();

matches(logEvents -> logEvents.stream()
.map(ILoggingEvent::getFormattedMessage)
.noneMatch(message -> message.contains(thePartial)),
"Log Events should have no message with "+ thePartial + " in it.");
return this;
}

public LogEventIterableAssert hasEventWithLevelAndMessageAndThrew(Level theLevel, String thePartial, String theExceptionMessage) {
isNotNull();

matches(logEvents -> logEvents.stream()
.filter(message -> message.getMessage().contains(thePartial))
.filter(message -> message.getLevel() == theLevel)
.anyMatch(message -> message.getThrowableProxy().getMessage().contains(theExceptionMessage)),
"Log Events should have at least one message with "+ thePartial + " in it.");

return this;
}

public LogEventIterableAssert doesNotHaveEventWithLevelAndMessage(Level theLevel, String thePartial) {
isNotNull();

matches(logEvents -> logEvents.stream()
.filter(e -> e.getLevel() == theLevel)
.map(ILoggingEvent::getFormattedMessage)
.noneMatch(message -> message.contains(thePartial)),
"Log Events should have no " + theLevel.toString() + " message with "+ thePartial + " in it.");
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package ca.uhn.test.util;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand All @@ -42,6 +44,16 @@ public StaticLogbackTestExtension() {
myLogbackTestExtension = new LogbackTestExtension();
}

public static StaticLogbackTestExtension withThreshold(Level theLevel) {
LogbackTestExtension logbackTestExtension = new LogbackTestExtension();
logbackTestExtension.setUp(theLevel);
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setLevel(theLevel.levelStr);
logbackTestExtension.getAppender().addFilter(thresholdFilter);

return new StaticLogbackTestExtension(logbackTestExtension);
}

@Override
public void beforeAll(ExtensionContext theExtensionContext) throws Exception {
myLogbackTestExtension.beforeEach(theExtensionContext);
Expand All @@ -55,4 +67,13 @@ public void afterAll(ExtensionContext theExtensionContext) throws Exception {
public List<ILoggingEvent> filterLoggingEventsWithMessageEqualTo(String theMessageText) {
return myLogbackTestExtension.filterLoggingEventsWithMessageEqualTo(theMessageText);
}

/**
* Returns a copy to avoid concurrent modification errors.
* @return A copy of the log events so far.
*/
public java.util.List<ILoggingEvent> getLogEvents() {
return myLogbackTestExtension.getLogEvents();
}

}

0 comments on commit c1c6d4f

Please sign in to comment.