Added Junit5 extention to use LoggerInspection without @Rule annotation

This commit is contained in:
Andrea Boriero 2021-01-25 13:28:06 +01:00 committed by Jan Schatteman
parent ac18db61b7
commit 491cbabc6c
7 changed files with 122 additions and 15 deletions

View File

@ -11,11 +11,12 @@ import org.hibernate.internal.HEMLogging;
import org.hibernate.jpa.boot.spi.ProviderChecker;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable;
import org.junit.Rule;
import org.hibernate.testing.orm.logger.LogInspector;
import org.hibernate.testing.orm.logger.LoggerInspectionExtension;
import org.hibernate.testing.orm.logger.Inspector;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -26,18 +27,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*
* @author Gail Badner
*/
@ExtendWith(LoggerInspectionExtension.class)
public class DeprecatedProviderCheckerTest {
final static String DEPRECATED_PROVIDER_NAME = "org.hibernate.ejb.HibernatePersistence";
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
HEMLogging.messageLogger( ProviderChecker.class.getName() )
);
@Test
@TestForIssue( jiraKey = "HHH-13027")
public void testDeprecatedProvider() {
Triggerable triggerable = logInspection.watchForLogMessages( "HHH015016" );
@TestForIssue(jiraKey = "HHH-13027")
public void testDeprecatedProvider(@LogInspector Inspector logger) {
logger.init( HEMLogging.messageLogger( ProviderChecker.class.getName() ) );
Triggerable triggerable = logger.watchForLogMessages( "HHH015016" );
triggerable.reset();
assertTrue( ProviderChecker.hibernateProviderNamesContain( DEPRECATED_PROVIDER_NAME ) );
triggerable.wasTriggered();

View File

@ -19,16 +19,16 @@ import org.jboss.logging.DelegatingBasicLogger;
*
* @author <a href="mailto:sanne@hibernate.org">Sanne Grinovero</a> (C) 2015 Red Hat Inc.
*/
final class LogInspectionHelper {
public final class LogInspectionHelper {
private LogInspectionHelper() {
}
static void registerListener(LogListener listener, BasicLogger log) {
public static void registerListener(LogListener listener, BasicLogger log) {
convertType( log ).registerListener( listener );
}
static void clearAllListeners(BasicLogger log) {
public static void clearAllListeners(BasicLogger log) {
convertType( log ).clearAllListeners();
}

View File

@ -15,7 +15,7 @@ import java.util.concurrent.atomic.AtomicReference;
import org.jboss.logging.Logger.Level;
final class TriggerOnPrefixLogListener implements LogListener, Triggerable {
public final class TriggerOnPrefixLogListener implements LogListener, Triggerable {
private Set<String> expectedPrefixes = new HashSet<>();

View File

@ -13,6 +13,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ParameterContext;
@Inherited
@Target( ElementType.TYPE )

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.testing.orm.logger;
import java.util.Set;
import org.hibernate.testing.logger.LogInspectionHelper;
import org.hibernate.testing.logger.LogListener;
import org.hibernate.testing.logger.TriggerOnPrefixLogListener;
import org.hibernate.testing.logger.Triggerable;
import org.jboss.logging.BasicLogger;
public class Inspector {
private BasicLogger log;
public void init(BasicLogger logger){
log = logger;
}
public BasicLogger getLog(){
return log;
}
public Triggerable watchForLogMessages(String prefix) {
TriggerOnPrefixLogListener listener = new TriggerOnPrefixLogListener( prefix );
registerListener( listener );
return listener;
}
public Triggerable watchForLogMessages(Set<String> prefixes) {
TriggerOnPrefixLogListener listener = new TriggerOnPrefixLogListener( prefixes );
registerListener( listener );
return listener;
}
public void registerListener(LogListener listener) {
LogInspectionHelper.registerListener( listener, log );
}
}

View File

@ -0,0 +1,17 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.testing.orm.logger;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface LogInspector {
}

View File

@ -0,0 +1,46 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.testing.orm.logger;
import org.hibernate.testing.logger.LogInspectionHelper;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public final class LoggerInspectionExtension implements AfterEachCallback, BeforeEachCallback, ParameterResolver {
private Inspector inspector;
public LoggerInspectionExtension() {
inspector = new Inspector();
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
// do nothing
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
LogInspectionHelper.clearAllListeners( inspector.getLog() );
}
@Override
public boolean supportsParameter(
ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.isAnnotated( LogInspector.class );
}
@Override
public Object resolveParameter(
ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return inspector;
}
}