57
57
import org .springframework .util .StringUtils ;
58
58
59
59
import static org .assertj .core .api .Assertions .assertThat ;
60
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
60
61
import static org .assertj .core .api .Assertions .contentOf ;
61
62
import static org .hamcrest .Matchers .containsString ;
62
63
import static org .hamcrest .Matchers .not ;
@@ -381,10 +382,53 @@ public void testCleanHistoryOnStartPropertyWithXmlConfiguration() {
381
382
assertThat (getRollingPolicy ().isCleanHistoryOnStart ()).isTrue ();
382
383
}
383
384
385
+ @ Test
386
+ public void testMaxFileSizePropertyNegativeValue () {
387
+ assertThatThrownBy (() -> {
388
+ MockEnvironment environment = new MockEnvironment ();
389
+ environment .setProperty ("logging.file.max-size" , "-100MB" );
390
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
391
+ environment );
392
+ File file = new File (tmpDir (), "logback-test.log" );
393
+ LogFile logFile = getLogFile (file .getPath (), null );
394
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
395
+ }).isInstanceOf (IllegalArgumentException .class )
396
+ .hasMessageContaining ("MaxFileSize must not be a negative" );
397
+ }
398
+
399
+ @ Test
400
+ public void testMaxFileSizePropertyInvalidFormat () {
401
+ assertThatThrownBy (() -> {
402
+ MockEnvironment environment = new MockEnvironment ();
403
+ environment .setProperty ("logging.file.max-size" , "100 :)" );
404
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
405
+ environment );
406
+ File file = new File (tmpDir (), "logback-test.log" );
407
+ LogFile logFile = getLogFile (file .getPath (), null );
408
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
409
+ }).isInstanceOf (IllegalArgumentException .class )
410
+ .hasMessageContaining ("does not match data size pattern" );
411
+ }
412
+
413
+ @ Test
414
+ public void testMaxFileSizePropertyLogbackFormat () {
415
+ MockEnvironment environment = new MockEnvironment ();
416
+ environment .setProperty ("logging.file.max-size" , "100 kbs" );
417
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
418
+ environment );
419
+ File file = new File (tmpDir (), "logback-test.log" );
420
+ LogFile logFile = getLogFile (file .getPath (), null );
421
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
422
+ this .logger .info ("Hello world" );
423
+ assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
424
+ assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "maxFileSize" )
425
+ .toString ()).isEqualTo ("100 KB" );
426
+ }
427
+
384
428
@ Test
385
429
public void testMaxFileSizeProperty () {
386
430
MockEnvironment environment = new MockEnvironment ();
387
- environment .setProperty ("logging.file.max-size" , "100MB " );
431
+ environment .setProperty ("logging.file.max-size" , "+10TB " );
388
432
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
389
433
environment );
390
434
File file = new File (tmpDir (), "logback-test.log" );
@@ -393,7 +437,22 @@ public void testMaxFileSizeProperty() {
393
437
this .logger .info ("Hello world" );
394
438
assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
395
439
assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "maxFileSize" )
396
- .toString ()).isEqualTo ("100 MB" );
440
+ .toString ()).isEqualTo ("10240 GB" );
441
+ }
442
+
443
+ @ Test
444
+ public void testMaxFileSizePropertyRawBytes () {
445
+ MockEnvironment environment = new MockEnvironment ();
446
+ environment .setProperty ("logging.file.max-size" , "1024" );
447
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
448
+ environment );
449
+ File file = new File (tmpDir (), "logback-test.log" );
450
+ LogFile logFile = getLogFile (file .getPath (), null );
451
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
452
+ this .logger .info ("Hello world" );
453
+ assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
454
+ assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "maxFileSize" )
455
+ .toString ()).isEqualTo ("1 KB" );
397
456
}
398
457
399
458
@ Test
@@ -427,7 +486,7 @@ public void testMaxHistoryProperty() {
427
486
}
428
487
429
488
@ Test
430
- public void testMaxHistoryPropertyWithXmlConfiguration () throws Exception {
489
+ public void testMaxHistoryPropertyWithXmlConfiguration () {
431
490
MockEnvironment environment = new MockEnvironment ();
432
491
environment .setProperty ("logging.file.max-history" , "30" );
433
492
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
@@ -441,11 +500,53 @@ public void testMaxHistoryPropertyWithXmlConfiguration() throws Exception {
441
500
assertThat (getRollingPolicy ().getMaxHistory ()).isEqualTo (30 );
442
501
}
443
502
503
+ @ Test
504
+ public void testTotalSizeCapPropertyNegativeValue () {
505
+ assertThatThrownBy (() -> {
506
+ MockEnvironment environment = new MockEnvironment ();
507
+ environment .setProperty ("logging.file.total-size-cap" , "-101 Mb" );
508
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
509
+ environment );
510
+ File file = new File (tmpDir (), "logback-test.log" );
511
+ LogFile logFile = getLogFile (file .getPath (), null );
512
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
513
+ }).isInstanceOf (IllegalArgumentException .class )
514
+ .hasMessageContaining ("TotalSizeCap must not be a negative" );
515
+ }
516
+
517
+ @ Test
518
+ public void testTotalSizeCapPropertyInvalidFormat () {
519
+ assertThatThrownBy (() -> {
520
+ MockEnvironment environment = new MockEnvironment ();
521
+ environment .setProperty ("logging.file.total-size-cap" , "101 :)" );
522
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
523
+ environment );
524
+ File file = new File (tmpDir (), "logback-test.log" );
525
+ LogFile logFile = getLogFile (file .getPath (), null );
526
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
527
+ }).isInstanceOf (IllegalArgumentException .class )
528
+ .hasMessageContaining ("does not match data size pattern" );
529
+ }
530
+
531
+ @ Test
532
+ public void testTotalSizeCapPropertyLogbackFormat () {
533
+ MockEnvironment environment = new MockEnvironment ();
534
+ environment .setProperty ("logging.file.total-size-cap" , "101 mbs" );
535
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
536
+ environment );
537
+ File file = new File (tmpDir (), "logback-test.log" );
538
+ LogFile logFile = getLogFile (file .getPath (), null );
539
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
540
+ this .logger .info ("Hello world" );
541
+ assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
542
+ assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "totalSizeCap" )
543
+ .toString ()).isEqualTo ("101 MB" );
544
+ }
545
+
444
546
@ Test
445
547
public void testTotalSizeCapProperty () {
446
- String expectedSize = "101 MB" ;
447
548
MockEnvironment environment = new MockEnvironment ();
448
- environment .setProperty ("logging.file.total-size-cap" , expectedSize );
549
+ environment .setProperty ("logging.file.total-size-cap" , "101MB" );
449
550
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
450
551
environment );
451
552
File file = new File (tmpDir (), "logback-test.log" );
@@ -454,14 +555,28 @@ public void testTotalSizeCapProperty() {
454
555
this .logger .info ("Hello world" );
455
556
assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
456
557
assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "totalSizeCap" )
457
- .toString ()).isEqualTo (expectedSize );
558
+ .toString ()).isEqualTo ("101 MB" );
559
+ }
560
+
561
+ @ Test
562
+ public void testTotalSizeCapPropertyRawBytes () {
563
+ MockEnvironment environment = new MockEnvironment ();
564
+ environment .setProperty ("logging.file.total-size-cap" , "10485760" );
565
+ LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
566
+ environment );
567
+ File file = new File (tmpDir (), "logback-test.log" );
568
+ LogFile logFile = getLogFile (file .getPath (), null );
569
+ this .loggingSystem .initialize (loggingInitializationContext , null , logFile );
570
+ this .logger .info ("Hello world" );
571
+ assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
572
+ assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "totalSizeCap" )
573
+ .toString ()).isEqualTo ("10 MB" );
458
574
}
459
575
460
576
@ Test
461
577
public void testTotalSizeCapPropertyWithXmlConfiguration () {
462
- String expectedSize = "101 MB" ;
463
578
MockEnvironment environment = new MockEnvironment ();
464
- environment .setProperty ("logging.file.total-size-cap" , expectedSize );
579
+ environment .setProperty ("logging.file.total-size-cap" , "101MB" );
465
580
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext (
466
581
environment );
467
582
File file = new File (tmpDir (), "logback-test.log" );
@@ -471,7 +586,7 @@ public void testTotalSizeCapPropertyWithXmlConfiguration() {
471
586
this .logger .info ("Hello world" );
472
587
assertThat (getLineWithText (file , "Hello world" )).contains ("INFO" );
473
588
assertThat (ReflectionTestUtils .getField (getRollingPolicy (), "totalSizeCap" )
474
- .toString ()).isEqualTo (expectedSize );
589
+ .toString ()).isEqualTo ("101 MB" );
475
590
}
476
591
477
592
@ Test
0 commit comments