View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.boolex;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  import static org.junit.Assert.fail;
20  
21  import java.io.IOException;
22  
23  import org.junit.Test;
24  import org.slf4j.MDC;
25  import org.slf4j.MarkerFactory;
26  
27  import ch.qos.logback.classic.Level;
28  import ch.qos.logback.classic.Logger;
29  import ch.qos.logback.classic.LoggerContext;
30  import ch.qos.logback.classic.pattern.ConverterTest;
31  import ch.qos.logback.classic.spi.ILoggingEvent;
32  import ch.qos.logback.classic.spi.LoggingEvent;
33  import ch.qos.logback.core.boolex.EvaluationException;
34  import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
35  import ch.qos.logback.core.boolex.Matcher;
36  import ch.qos.logback.core.filter.EvaluatorFilter;
37  import ch.qos.logback.core.spi.FilterReply;
38  import ch.qos.logback.core.testUtil.RandomUtil;
39  import ch.qos.logback.core.util.StatusPrinter;
40  
41  public class JaninoEventEvaluatorTest {
42  
43      LoggerContext loggerContext = new LoggerContext();
44      Logger logger = loggerContext.getLogger(ConverterTest.class);
45  
46      Matcher matcherX = new Matcher();
47  
48      JaninoEventEvaluator jee = new JaninoEventEvaluator();
49  
50      int diff = RandomUtil.getPositiveInt();
51  
52      public JaninoEventEvaluatorTest() {
53          jee.setContext(loggerContext);
54  
55          matcherX.setName("x");
56          matcherX.setRegex("^Some\\s.*");
57          matcherX.start();
58  
59      }
60  
61      LoggingEvent makeLoggingEvent(Exception ex) {
62          return new LoggingEvent(ch.qos.logback.core.pattern.FormattingConverter.class.getName(), logger, Level.INFO, "Some message", ex, null);
63      }
64  
65      @Test
66      public void testBasic() throws Exception {
67          jee.setExpression("message.equals(\"Some message\")");
68          jee.start();
69  
70          StatusPrinter.print(loggerContext);
71          ILoggingEvent event = makeLoggingEvent(null);
72          assertTrue(jee.evaluate(event));
73      }
74  
75      @Test
76      public void testLevel() throws Exception {
77          jee.setExpression("level > DEBUG");
78          jee.start();
79  
80          ILoggingEvent event = makeLoggingEvent(null);
81          assertTrue(jee.evaluate(event));
82      }
83  
84      @Test
85      public void testtimeStamp() throws Exception {
86          jee.setExpression("timeStamp > 10");
87          jee.start();
88  
89          ILoggingEvent event = makeLoggingEvent(null);
90          assertTrue(jee.evaluate(event));
91      }
92  
93      @Test
94      public void testWithMatcher() throws Exception {
95          jee.setExpression("x.matches(message)");
96          jee.addMatcher(matcherX);
97          jee.start();
98  
99          ILoggingEvent event = makeLoggingEvent(null);
100         assertTrue(jee.evaluate(event));
101     }
102 
103     @Test
104     public void mdcAsString() throws Exception {
105         String k = "key" + diff;
106 
107         MDC.put("key" + diff, "value" + diff);
108         jee.setExpression("((String) mdc.get(\"" + k + "\")).contains(\"alue\")");
109         jee.start();
110         StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
111 
112         LoggingEvent event = makeLoggingEvent(null);
113         assertTrue(jee.evaluate(event));
114         MDC.remove(k);
115     }
116 
117     @Test
118     public void marker() throws Exception {
119         jee.setExpression("marker.contains(\"BLUE\")");
120         jee.start();
121 
122         LoggingEvent event = makeLoggingEvent(null);
123         event.setMarker(MarkerFactory.getMarker("BLUE"));
124         assertTrue(jee.evaluate(event));
125     }
126 
127     @Test
128     public void withNullMarker_LBCORE_118() throws Exception {
129         jee.setExpression("marker.contains(\"BLUE\")");
130         jee.start();
131 
132         ILoggingEvent event = makeLoggingEvent(null);
133         try {
134             jee.evaluate(event);
135             fail("We should not reach this point");
136         } catch (EvaluationException ee) {
137             // received an exception as expected
138         }
139     }
140 
141     @Test
142     public void evaluatorFilterWithNullMarker_LBCORE_118() throws Exception {
143         EvaluatorFilter<ILoggingEvent> ef = new EvaluatorFilter<ILoggingEvent>();
144         ef.setContext(loggerContext);
145 
146         ef.setOnMatch(FilterReply.ACCEPT);
147         ef.setOnMismatch(FilterReply.DENY);
148 
149         jee.setExpression("marker.contains(\"BLUE\")");
150         jee.start();
151 
152         ef.setEvaluator(jee);
153         ef.start();
154         ILoggingEvent event = makeLoggingEvent(null);
155         assertEquals(FilterReply.NEUTRAL, ef.decide(event));
156 
157     }
158 
159     @Test
160     public void testComplex() throws Exception {
161         jee.setExpression("level >= INFO && x.matches(message) && marker.contains(\"BLUE\")");
162         jee.addMatcher(matcherX);
163         jee.start();
164 
165         LoggingEvent event = makeLoggingEvent(null);
166         event.setMarker(MarkerFactory.getMarker("BLUE"));
167         assertTrue(jee.evaluate(event));
168     }
169 
170     /**
171      * check that evaluator with bogus exp does not start
172      * 
173      * @throws Exception
174      */
175     @Test
176     public void testBogusExp1() {
177         jee.setExpression("mzzzz.get(\"key\").equals(null)");
178         jee.setName("bogus");
179         jee.start();
180 
181         assertFalse(jee.isStarted());
182     }
183 
184     // check that eval stops after errors
185     @Test
186     public void testBogusExp2() {
187         jee.setExpression("mdc.get(\"keyXN89\").equals(null)");
188         jee.setName("bogus");
189         jee.start();
190 
191         assertTrue(jee.isStarted());
192 
193         ILoggingEvent event = makeLoggingEvent(null);
194 
195         for (int i = 0; i < JaninoEventEvaluatorBase.ERROR_THRESHOLD; i++) {
196             try {
197                 jee.evaluate(event);
198                 fail("should throw an exception");
199             } catch (EvaluationException e) {
200             }
201         }
202         // after a few attempts the evaluator should processPriorToRemoval
203         assertFalse(jee.isStarted());
204 
205     }
206 
207     static final long LEN = 10 * 1000;
208 
209     // with 6 parameters 400 nanos
210     // with 7 parameters 460 nanos (all levels + selected fields from
211     // LoggingEvent)
212     // with 10 parameters 510 nanos (all levels + fields)
213     void loop(JaninoEventEvaluator jee, String msg) throws Exception {
214         ILoggingEvent event = makeLoggingEvent(null);
215         // final long start = System.nanoTime();
216         for (int i = 0; i < LEN; i++) {
217             jee.evaluate(event);
218         }
219         // final long end = System.nanoTime();
220         // System.out.println(msg + (end - start) / LEN + " nanos");
221     }
222 
223     @Test
224     public void testLoop1() throws Exception {
225         jee.setExpression("timeStamp > 10");
226         jee.start();
227 
228         loop(jee, "timestamp > 10]: ");
229     }
230 
231     @Test
232     public void testLoop2() throws Exception {
233         jee.setExpression("x.matches(message)");
234         jee.addMatcher(matcherX);
235         jee.start();
236 
237         loop(jee, "x.matches(message): ");
238     }
239 
240     @Test
241     public void throwable_LBCLASSIC_155_I() throws EvaluationException {
242         jee.setExpression("throwable instanceof java.io.IOException");
243         jee.start();
244 
245         LoggingEvent event = makeLoggingEvent(new IOException(""));
246         assertTrue(jee.evaluate(event));
247     }
248 
249     @Test
250     public void throwable_LBCLASSIC_155_II() throws EvaluationException {
251         jee.setExpression("throwableProxy.getClassName().contains(\"IO\")");
252         jee.start();
253 
254         LoggingEvent event = makeLoggingEvent(new IOException(""));
255         assertTrue(jee.evaluate(event));
256     }
257 
258     @Test
259     public void nullMDC() throws EvaluationException {
260         MDC.clear();
261         jee.setExpression("mdc.isEmpty()");
262         jee.start();
263 
264         LoggingEvent event = makeLoggingEvent(null);
265         assertTrue(jee.evaluate(event));
266     }
267 }