1
1
/*
2
- * Copyright 2002-2025 the original author or authors.
2
+ * Copyright 2002-2019 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 .extensions ;
17
+ package org .springframework .kafka .test .rule ;
18
18
19
- import java .lang .reflect .Method ;
20
19
import java .util .ArrayList ;
21
20
import java .util .Arrays ;
22
21
import java .util .Collections ;
23
22
import java .util .List ;
24
23
25
24
import 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 ;
29
28
30
29
import org .springframework .kafka .test .utils .JUnitUtils ;
31
30
import org .springframework .kafka .test .utils .JUnitUtils .LevelsContainer ;
32
31
33
32
/**
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
35
34
* while a test method is running. Useful for performance or scalability tests
36
35
* where we don't want to generate a large log in a tight inner loop.
37
36
*
38
37
* @author Dave Syer
39
38
* @author Artem Bilan
40
39
* @author Gary Russell
41
- * @author Sanghyoek An
42
40
*
43
41
*/
44
- public class Log4j2LevelAdjuster implements InvocationInterceptor {
42
+ public class Log4j2LevelAdjuster implements MethodRule {
45
43
46
44
private final List <Class <?>> classes ;
47
45
@@ -62,25 +60,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) {
62
60
}
63
61
64
62
@ 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
+ }
82
79
}
83
- }
80
+
81
+ };
84
82
}
85
83
86
84
}
0 commit comments