1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
14
14
* limitations under the License.
15
15
*/
16
16
17
- package org .springframework .kafka .test .rule ;
17
+ package org .springframework .kafka .test .extensions ;
18
18
19
+ import java .lang .reflect .Method ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
23
24
24
25
import org .apache .logging .log4j .Level ;
25
- import org .junit .rules . MethodRule ;
26
- import org .junit .runners . model . FrameworkMethod ;
27
- import org .junit .runners . model . Statement ;
26
+ import org .junit .jupiter . api . extension . ExtensionContext ;
27
+ import org .junit .jupiter . api . extension . InvocationInterceptor ;
28
+ import org .junit .jupiter . api . extension . ReflectiveInvocationContext ;
28
29
29
30
import org .springframework .kafka .test .utils .JUnitUtils ;
30
31
import org .springframework .kafka .test .utils .JUnitUtils .LevelsContainer ;
31
32
32
33
/**
33
- * A JUnit method @Rule that changes the logger level for a set of classes
34
+ * A JUnit extension @ that changes the logger level for a set of classes
34
35
* while a test method is running. Useful for performance or scalability tests
35
36
* where we don't want to generate a large log in a tight inner loop.
36
37
*
37
38
* @author Dave Syer
38
39
* @author Artem Bilan
39
40
* @author Gary Russell
41
+ * @author Sanghyoek An
40
42
*
41
43
*/
42
- public class Log4j2LevelAdjuster implements MethodRule {
44
+ public class Log4j2LevelAdjuster implements InvocationInterceptor {
43
45
44
46
private final List <Class <?>> classes ;
45
47
@@ -60,25 +62,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) {
60
62
}
61
63
62
64
@ Override
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
- }
79
- }
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 ;
80
70
81
- };
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 );
82
+ }
83
+ }
82
84
}
83
85
84
86
}
0 commit comments