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.net;
15  
16  import ch.qos.logback.classic.Logger;
17  import ch.qos.logback.classic.util.LogbackMDCAdapter;
18  import org.junit.jupiter.api.AfterEach;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  
22  import ch.qos.logback.classic.Level;
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.PatternLayout;
25  import ch.qos.logback.classic.spi.ILoggingEvent;
26  import ch.qos.logback.classic.spi.LoggingEvent;
27  import ch.qos.logback.core.Layout;
28  import ch.qos.logback.core.helpers.CyclicBuffer;
29  import ch.qos.logback.core.spi.CyclicBufferTracker;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  public class DilutedSMTPAppenderTest {
35      LoggerContext lc = new LoggerContext();
36      Logger logger = lc.getLogger(this.getClass());
37      LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
38      SMTPAppender appender;
39      CyclicBufferTracker<ILoggingEvent> cbTracker;
40      CyclicBuffer<ILoggingEvent> cb;
41  
42      @BeforeEach
43      public void setUp() throws Exception {
44  
45          lc.setMDCAdapter(logbackMDCAdapter);
46          appender = new SMTPAppender();
47          appender.setContext(lc);
48          appender.setName("smtp");
49          appender.setFrom("user@host.dom");
50          appender.setLayout(buildLayout(lc));
51          appender.setSMTPHost("mail2.qos.ch");
52          appender.setSubject("logging report");
53          appender.addTo("sebastien.nospam@qos.ch");
54          appender.start();
55          cbTracker = appender.getCyclicBufferTracker();
56          cb = cbTracker.getOrCreate("", 0);
57  
58      }
59  
60      private static Layout<ILoggingEvent> buildLayout(LoggerContext lc) {
61          PatternLayout layout = new PatternLayout();
62          layout.setContext(lc);
63          layout.setFileHeader("Some header\n");
64          layout.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
65          layout.setFileFooter("Some footer");
66          layout.start();
67          return layout;
68      }
69  
70      @AfterEach
71      public void tearDown() throws Exception {
72          appender = null;
73      }
74  
75      @Test
76      public void testStart() {
77          assertEquals("sebastien.nospam@qos.ch%nopex", appender.getToAsListOfString().get(0));
78  
79          assertEquals("logging report", appender.getSubject());
80  
81          assertTrue(appender.isStarted());
82      }
83  
84      @Test
85      public void testAppendNonTriggeringEvent() {
86          LoggingEvent event = new LoggingEvent();
87          event.setThreadName("thead name");
88          event.setLevel(Level.DEBUG);
89          event.setLoggerContext(lc);
90          appender.subAppend(cb, event);
91          assertEquals(1, cb.length());
92      }
93  
94      @Test
95      public void testEntryConditionsCheck() {
96          appender.checkEntryConditions();
97          assertEquals(0, appender.getContext().getStatusManager().getCount());
98      }
99  
100     @Test
101     public void testTriggeringPolicy() {
102         appender.setEvaluator(null);
103         appender.checkEntryConditions();
104         assertEquals(1, appender.getContext().getStatusManager().getCount());
105     }
106 
107     @Test
108     public void testEntryConditionsCheckNoLayout() {
109         appender.setLayout(null);
110         appender.checkEntryConditions();
111         assertEquals(1, appender.getContext().getStatusManager().getCount());
112     }
113 
114 }