Add a new rule
This commit is contained in:
parent
45360d80cb
commit
220cb1716f
|
@ -8,6 +8,7 @@ import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc;
|
|||
import ca.uhn.fhir.jpa.term.VersionIndependentConcept;
|
||||
import ca.uhn.fhir.jpa.util.ExpungeOptions;
|
||||
import ca.uhn.fhir.jpa.util.JpaConstants;
|
||||
import ca.uhn.fhir.jpa.util.LoggingRule;
|
||||
import ca.uhn.fhir.util.StopWatch;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Bundle;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
|
||||
|
@ -27,6 +28,7 @@ import org.hl7.fhir.instance.model.api.IIdType;
|
|||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
|
@ -51,6 +53,9 @@ public abstract class BaseJpaTest {
|
|||
protected ArrayList<IServerInterceptor> myServerInterceptorList;
|
||||
protected IRequestOperationCallback myRequestOperationCallback = mock(IRequestOperationCallback.class);
|
||||
|
||||
@Rule
|
||||
private LoggingRule myLoggingRule = new LoggingRule();
|
||||
|
||||
@After
|
||||
public final void afterPerformCleanup() {
|
||||
BaseHapiFhirResourceDao.setDisableIncrementOnUpdateForUnitTest(false);
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright 2013 Brian Thomas Matthews
|
||||
*
|
||||
* 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://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.
|
||||
*/
|
||||
|
||||
package ca.uhn.fhir.jpa.util;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* This JUnit rule generates log messages to delineate the start and finish of a JUnit test case and also to note any exceptions
|
||||
* that are thrown.
|
||||
*
|
||||
* @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LoggingRule implements TestRule {
|
||||
/**
|
||||
* The key for the message used to log the start of the test case.
|
||||
*/
|
||||
private static final String START = "com.btmatthews.junit.rules.logging.START";
|
||||
/**
|
||||
* The key for the message used to log any exception thrown by the test case.
|
||||
*/
|
||||
private static final String EXCEPTION = "com.btmatthews.junit.rules.logging.EXCEPTION";
|
||||
/**
|
||||
* The key for the message used to log the finish of the test case.
|
||||
*/
|
||||
private static final String FINISH = "com.btmatthews.junit.rules.logging.FINISH";
|
||||
/**
|
||||
* The resource bundle from which the log messages are loaded.
|
||||
*/
|
||||
private static final ResourceBundle messages = ResourceBundle.getBundle("com.btmatthews.junit.rules.messages");
|
||||
|
||||
/**
|
||||
* Apply the test rule by building a wrapper {@link Statement} that logs a messages before and after evaluating
|
||||
* <code>statement</code> and if
|
||||
* @param statement The statement to be modified.
|
||||
* @param description A description of the test implemented in <code>statement</code>.
|
||||
* @return The modified statement.
|
||||
*/
|
||||
public Statement apply(final Statement statement, final Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
final Logger logger = LoggerFactory.getLogger(description.getTestClass());
|
||||
logger.info(MessageFormat.format(messages.getString(START), description.getDisplayName()));
|
||||
try {
|
||||
statement.evaluate();
|
||||
} catch (final Throwable e) {
|
||||
logger.info(MessageFormat.format(messages.getString(EXCEPTION), description.getDisplayName()), e);
|
||||
throw e;
|
||||
} finally {
|
||||
logger.info(MessageFormat.format(messages.getString(FINISH), description.getDisplayName()));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue