19
19
import java .io .BufferedReader ;
20
20
import java .io .File ;
21
21
import java .io .FileReader ;
22
+ import java .nio .file .Path ;
23
+ import java .util .concurrent .TimeUnit ;
22
24
23
- import org .apache .logging .log4j .LogManager ;
24
25
import org .apache .logging .log4j .Logger ;
25
- import org .apache .logging .log4j .core .impl .Log4jPropertyKey ;
26
- import org .apache .logging .log4j .core .test .CoreLoggerContexts ;
26
+ import org .apache .logging .log4j .core .LoggerContext ;
27
+ import org .apache .logging .log4j .core .test .junit .LoggerContextSource ;
28
+ import org .apache .logging .log4j .test .junit .TempLoggingDir ;
29
+ import org .apache .logging .log4j .test .junit .UsingStatusListener ;
27
30
import org .junit .jupiter .api .Tag ;
28
31
import org .junit .jupiter .api .Test ;
29
- import org .junitpioneer .jupiter .SetSystemProperty ;
30
32
31
33
import static org .hamcrest .MatcherAssert .assertThat ;
32
- import static org .hamcrest .Matchers .containsString ;
34
+ import static org .hamcrest .Matchers .equalTo ;
33
35
import static org .junit .jupiter .api .Assertions .assertNull ;
34
- import static org .junit .jupiter .api .Assertions .assertTrue ;
35
36
36
37
@ Tag ("async" )
37
- @ SetSystemProperty ( key = Log4jPropertyKey . Constant . CONFIG_LOCATION , value = "AsyncLoggerConfigTest4.xml" )
38
+ @ UsingStatusListener
38
39
public class AsyncLoggerConfig4Test {
39
40
41
+ @ TempLoggingDir
42
+ private static Path loggingPath ;
43
+
40
44
@ Test
41
- public void testParameters () throws Exception {
42
- final File file = new File ( "target" , "AsyncLoggerConfigTest4.log" );
43
- assertTrue (! file . exists () || file . delete (), "Deleted old file before test" );
45
+ @ LoggerContextSource
46
+ public void testParameters ( final LoggerContext ctx ) throws Exception {
47
+ final File file = loggingPath . resolve ( "AsyncLoggerConfigTest4.log" ). toFile ( );
44
48
45
- final Logger log = LogManager .getLogger ("com.foo.Bar" );
49
+ final Logger log = ctx .getLogger ("com.foo.Bar" );
46
50
log .info ("Additive logging: {} for the price of {}!" , 2 , 1 );
47
- CoreLoggerContexts . stopLoggerContext ( file ); // stop async thread
51
+ ctx . stop ( 500 , TimeUnit . MILLISECONDS );
48
52
49
53
final BufferedReader reader = new BufferedReader (new FileReader (file ));
50
54
final String line1 = reader .readLine ();
@@ -56,13 +60,13 @@ public void testParameters() throws Exception {
56
60
file .delete ();
57
61
58
62
assertThat (line1 ,
59
- containsString ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
63
+ equalTo ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
60
64
assertThat (line2 ,
61
- containsString ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
65
+ equalTo ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
62
66
assertThat (line3 ,
63
- containsString ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
67
+ equalTo ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
64
68
assertThat (line4 ,
65
- containsString ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
66
- assertNull (line5 , "Expected only two lines to be logged" );
69
+ equalTo ("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!" ));
70
+ assertNull (line5 , "Expected only four lines to be logged" );
67
71
}
68
72
}
0 commit comments