13
13
*/
14
14
package ch .qos .logback .core .rolling ;
15
15
16
+ import ch .qos .logback .core .util .Duration ;
17
+ import ch .qos .logback .core .util .FileSize ;
16
18
import org .junit .jupiter .api .AfterEach ;
17
19
import org .junit .jupiter .api .Assertions ;
18
20
import org .junit .jupiter .api .BeforeEach ;
21
+ import org .junit .jupiter .api .DisplayName ;
19
22
import org .junit .jupiter .api .Test ;
20
23
21
24
import ch .qos .logback .core .Appender ;
27
30
import ch .qos .logback .core .testUtil .CoreTestConstants ;
28
31
import ch .qos .logback .core .testUtil .RandomUtil ;
29
32
import ch .qos .logback .core .status .testUtil .StatusChecker ;
33
+
34
+ import java .io .File ;
35
+ import java .io .FileInputStream ;
36
+ import java .io .IOException ;
37
+ import java .io .InputStream ;
38
+ import java .nio .file .Files ;
39
+ import java .util .Collections ;
40
+ import java .util .List ;
30
41
//import ch.qos.logback.core.util.StatusPrinter;
31
42
32
43
public class RollingFileAppenderTest extends AbstractAppenderTest <Object > {
@@ -37,13 +48,14 @@ public class RollingFileAppenderTest extends AbstractAppenderTest<Object> {
37
48
TimeBasedRollingPolicy <Object > tbrp = new TimeBasedRollingPolicy <Object >();
38
49
int diff = RandomUtil .getPositiveInt ();
39
50
String randomOutputDir = CoreTestConstants .OUTPUT_DIR_PREFIX + diff + "/" ;
51
+ DummyEncoder <Object > encoder ;
40
52
41
53
@ BeforeEach
42
54
public void setUp () throws Exception {
43
55
// noStartTest fails if the context is set in setUp
44
56
// rfa.setContext(context);
45
-
46
- rfa .setEncoder (new DummyEncoder < Object >() );
57
+ encoder = new DummyEncoder <>();
58
+ rfa .setEncoder (encoder );
47
59
rfa .setName ("test" );
48
60
tbrp .setContext (context );
49
61
tbrp .setParent (rfa );
@@ -260,4 +272,54 @@ public void collidingFileNamePattern() {
260
272
checker .assertContainsMatch (Status .ERROR , "'FileNamePattern' option has the same value" );
261
273
}
262
274
275
+ @ Test
276
+ @ DisplayName ("Checks header and footer are written when the files are rolled" )
277
+ public void testHeaderFooterWritten () throws IOException , InterruptedException {
278
+ for (int i = 0 ; i < 8 ; i ++) {
279
+ File file = new File (CoreTestConstants .OUTPUT_DIR_PREFIX + "header-" + i + ".log" );
280
+ file .deleteOnExit ();
281
+ }
282
+ encoder .setFileHeader ("HEADER" );
283
+ encoder .setFileFooter ("FOOTER" );
284
+ rfa .setContext (context );
285
+ FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy ();
286
+ fixedWindowRollingPolicy .setContext (context );
287
+ fixedWindowRollingPolicy .setParent (rfa );
288
+ fixedWindowRollingPolicy .setMaxIndex (3 );
289
+ String fileNamePattern = CoreTestConstants .OUTPUT_DIR_PREFIX + "header-%i.log" ;
290
+ fixedWindowRollingPolicy .setFileNamePattern (fileNamePattern );
291
+ rfa .setRollingPolicy (fixedWindowRollingPolicy );
292
+ rfa .setFile (CoreTestConstants .OUTPUT_DIR_PREFIX + "header-0.log" );
293
+ fixedWindowRollingPolicy .start ();
294
+ rfa .setImmediateFlush (true );
295
+ SizeBasedTriggeringPolicy <Object > sbtp = new SizeBasedTriggeringPolicy <>();
296
+ sbtp .setMaxFileSize (new FileSize (10 ));
297
+ sbtp .setCheckIncrement (Duration .buildByMilliseconds (10 ));
298
+
299
+ rfa .setTriggeringPolicy (sbtp );
300
+ rfa .getTriggeringPolicy ().start ();
301
+ rfa .start ();
302
+
303
+ for (int i = 0 ; i < 100 ; i ++) {
304
+ rfa .doAppend ("data" + i );
305
+ File file = new File (CoreTestConstants .OUTPUT_DIR_PREFIX + "header-" + fixedWindowRollingPolicy .getMaxIndex () + ".log" );
306
+ if (file .exists ()) {
307
+ break ;
308
+ }
309
+ Thread .sleep (5 );
310
+ }
311
+ rfa .stop ();
312
+
313
+ for (int i = 0 ; i < fixedWindowRollingPolicy .getMaxIndex (); i ++) {
314
+ File file = new File (CoreTestConstants .OUTPUT_DIR_PREFIX + "header-" + i + ".log" );
315
+ Assertions .assertTrue (file .exists ());
316
+ List <String > lines = Files .readAllLines (file .toPath ());
317
+ Assertions .assertTrue (lines .size () > 2 , "At least 2 lines per file are expected in " + file );
318
+ Assertions .assertEquals ("HEADER" , lines .get (0 ));
319
+ Assertions .assertEquals ("FOOTER" , lines .get (lines .size () - 1 ));
320
+ Assertions .assertEquals (1 , Collections .frequency (lines , "HEADER" ));
321
+ Assertions .assertEquals (1 , Collections .frequency (lines , "FOOTER" ));
322
+ }
323
+ }
324
+
263
325
}
0 commit comments