11/*
2- * Copyright 2002-2025 the original author or authors.
2+ * Copyright 2002-2019 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1414 * limitations under the License.
1515 */
1616
17- package org .springframework .kafka .test .extensions ;
17+ package org .springframework .kafka .test .rule ;
1818
19- import java .lang .reflect .Method ;
2019import java .util .ArrayList ;
2120import java .util .Arrays ;
2221import java .util .Collections ;
2322import java .util .List ;
2423
2524import org .apache .logging .log4j .Level ;
26- import org .junit .jupiter . api . extension . ExtensionContext ;
27- import org .junit .jupiter . api . extension . InvocationInterceptor ;
28- import org .junit .jupiter . api . extension . ReflectiveInvocationContext ;
25+ import org .junit .rules . MethodRule ;
26+ import org .junit .runners . model . FrameworkMethod ;
27+ import org .junit .runners . model . Statement ;
2928
3029import org .springframework .kafka .test .utils .JUnitUtils ;
3130import org .springframework .kafka .test .utils .JUnitUtils .LevelsContainer ;
3231
3332/**
34- * A JUnit extension @ that changes the logger level for a set of classes
33+ * A JUnit method @Rule that changes the logger level for a set of classes
3534 * while a test method is running. Useful for performance or scalability tests
3635 * where we don't want to generate a large log in a tight inner loop.
3736 *
3837 * @author Dave Syer
3938 * @author Artem Bilan
4039 * @author Gary Russell
41- * @author Sanghyoek An
4240 *
4341 */
44- public class Log4j2LevelAdjuster implements InvocationInterceptor {
42+ public class Log4j2LevelAdjuster implements MethodRule {
4543
4644 private final List <Class <?>> classes ;
4745
@@ -62,25 +60,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) {
6260 }
6361
6462 @ Override
65- public void interceptTestMethod (Invocation <Void > invocation ,
66- ReflectiveInvocationContext <Method > invocationContext ,
67- ExtensionContext extensionContext ) throws Throwable {
68- String methodName = extensionContext .getRequiredTestMethod ().getName ();
69- LevelsContainer container = null ;
70-
71- try {
72- container = JUnitUtils .adjustLogLevels (
73- methodName ,
74- Log4j2LevelAdjuster .this .classes ,
75- Log4j2LevelAdjuster .this .categories ,
76- Log4j2LevelAdjuster .this .level );
77- invocation .proceed ();
78- }
79- finally {
80- if (container != null ) {
81- JUnitUtils .revertLevels (methodName , container );
63+ public Statement apply (final Statement base , FrameworkMethod method , Object target ) {
64+ return new Statement () {
65+ @ Override
66+ public void evaluate () throws Throwable {
67+ LevelsContainer container = null ;
68+ try {
69+ container = JUnitUtils .adjustLogLevels (method .getName (),
70+ Log4j2LevelAdjuster .this .classes , Log4j2LevelAdjuster .this .categories ,
71+ Log4j2LevelAdjuster .this .level );
72+ base .evaluate ();
73+ }
74+ finally {
75+ if (container != null ) {
76+ JUnitUtils .revertLevels (method .getName (), container );
77+ }
78+ }
8279 }
83- }
80+
81+ };
8482 }
8583
8684}
0 commit comments