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