001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.net;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertTrue;
018
019import org.junit.After;
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.classic.Level;
024import ch.qos.logback.classic.LoggerContext;
025import ch.qos.logback.classic.PatternLayout;
026import ch.qos.logback.classic.spi.ILoggingEvent;
027import ch.qos.logback.classic.spi.LoggingEvent;
028import ch.qos.logback.core.Layout;
029import ch.qos.logback.core.helpers.CyclicBuffer;
030import ch.qos.logback.core.spi.CyclicBufferTracker;
031
032public class DilutedSMTPAppenderTest {
033
034    SMTPAppender appender;
035    CyclicBufferTracker<ILoggingEvent> cbTracker;
036    CyclicBuffer<ILoggingEvent> cb;
037
038    @Before
039    public void setUp() throws Exception {
040        LoggerContext lc = new LoggerContext();
041        appender = new SMTPAppender();
042        appender.setContext(lc);
043        appender.setName("smtp");
044        appender.setFrom("user@host.dom");
045        appender.setLayout(buildLayout(lc));
046        appender.setSMTPHost("mail2.qos.ch");
047        appender.setSubject("logging report");
048        appender.addTo("sebastien.nospam@qos.ch");
049        appender.start();
050        cbTracker = appender.getCyclicBufferTracker();
051        cb = cbTracker.getOrCreate("", 0);
052
053    }
054
055    private static Layout<ILoggingEvent> buildLayout(LoggerContext lc) {
056        PatternLayout layout = new PatternLayout();
057        layout.setContext(lc);
058        layout.setFileHeader("Some header\n");
059        layout.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
060        layout.setFileFooter("Some footer");
061        layout.start();
062        return layout;
063    }
064
065    @After
066    public void tearDown() throws Exception {
067        appender = null;
068    }
069
070    @Test
071    public void testStart() {
072        assertEquals("sebastien.nospam@qos.ch%nopex", appender.getToAsListOfString().get(0));
073
074        assertEquals("logging report", appender.getSubject());
075
076        assertTrue(appender.isStarted());
077    }
078
079    @Test
080    public void testAppendNonTriggeringEvent() {
081        LoggingEvent event = new LoggingEvent();
082        event.setThreadName("thead name");
083        event.setLevel(Level.DEBUG);
084        appender.subAppend(cb, event);
085        assertEquals(1, cb.length());
086    }
087
088    @Test
089    public void testEntryConditionsCheck() {
090        appender.checkEntryConditions();
091        assertEquals(0, appender.getContext().getStatusManager().getCount());
092    }
093
094    @Test
095    public void testTriggeringPolicy() {
096        appender.setEvaluator(null);
097        appender.checkEntryConditions();
098        assertEquals(1, appender.getContext().getStatusManager().getCount());
099    }
100
101    @Test
102    public void testEntryConditionsCheckNoLayout() {
103        appender.setLayout(null);
104        appender.checkEntryConditions();
105        assertEquals(1, appender.getContext().getStatusManager().getCount());
106    }
107
108}