View Javadoc
1   package ch.qos.logback.classic.joran.sanity;
2   
3   import ch.qos.logback.classic.LoggerContext;
4   import ch.qos.logback.classic.model.LoggerModel;
5   import ch.qos.logback.classic.model.RootLoggerModel;
6   import ch.qos.logback.core.model.AppenderModel;
7   import ch.qos.logback.core.model.Model;
8   import ch.qos.logback.core.model.conditional.IfModel;
9   import ch.qos.logback.core.status.Status;
10  import ch.qos.logback.core.status.testUtil.StatusChecker;
11  import ch.qos.logback.core.util.StatusPrinter;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  public class IfNestedWithinSecondPhaseElementSCTest {
16  
17      LoggerContext context = new LoggerContext();
18      IfNestedWithinSecondPhaseElementSC inwspeChecker = new IfNestedWithinSecondPhaseElementSC();
19      StatusChecker statusChecker = new StatusChecker(context);
20  
21      @BeforeEach
22      public void setUp() throws Exception {
23          inwspeChecker.setContext(context);
24      }
25  
26      @Test
27      public void smoke() {
28  
29          ClassicTopModel topModel = new ClassicTopModel();
30          inwspeChecker.check(topModel);
31          statusChecker.assertIsWarningOrErrorFree();
32      }
33  
34      @Test
35      public void singleAppender() {
36          ClassicTopModel topModel = new ClassicTopModel();
37          AppenderModel appenderModel0 = new AppenderModel();
38          appenderModel0.setLineNumber(1);
39          topModel.addSubModel(appenderModel0);
40          inwspeChecker.check(topModel);
41          statusChecker.assertIsWarningOrErrorFree();
42      }
43  
44      @Test
45      public void singleLoggerWithNestedIf() {
46          ClassicTopModel topModel = new ClassicTopModel();
47          Model rootLoggerModel = setupModel(new RootLoggerModel(), "root", 1);
48          topModel.addSubModel(rootLoggerModel);
49  
50          Model ifModel0 = setupModel(new IfModel(), "if", 2);
51          rootLoggerModel.addSubModel(ifModel0);
52  
53          Model loggerModel = setupModel(new LoggerModel(), "logger", 3);
54          topModel.addSubModel(loggerModel);
55  
56          Model ifModel1 = setupModel(new IfModel(), "if", 4);
57          loggerModel.addSubModel(ifModel1);
58  
59          Model appenderModel = setupModel(new LoggerModel(), "appender", 5);
60          topModel.addSubModel(appenderModel);
61  
62          Model ifModel2 = setupModel(new IfModel(), "if", 6);
63          appenderModel.addSubModel(ifModel2);
64  
65  
66          inwspeChecker.check(topModel);
67          StatusPrinter.print(context);
68          // Element <root> at line 1 contains a nested <if> element at line 2
69          String regex0 = "Element <root> at line 1 contains a nested <if> element at line 2";
70          statusChecker.assertContainsMatch(Status.WARN, regex0);
71  
72          String regex1 = "Element <logger> at line 3 contains a nested <if> element at line 4";
73          statusChecker.assertContainsMatch(Status.WARN, regex1);
74  
75          String regex2 = "Element <appender> at line 5 contains a nested <if> element at line 6";
76          statusChecker.assertContainsMatch(Status.WARN, regex2);
77  
78      }
79  
80  
81  
82      private Model setupModel(Model m, String tag, int line) {
83          m.setLineNumber(line);
84          m.setTag(tag);
85          return m;
86      }
87  
88  
89  }