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.core.pattern.parser.test;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018
019import org.junit.Test;
020
021import ch.qos.logback.core.Context;
022import ch.qos.logback.core.ContextBase;
023import ch.qos.logback.core.pattern.ExceptionalConverter;
024import ch.qos.logback.core.pattern.PatternLayoutBase;
025import ch.qos.logback.core.status.StatusManager;
026import ch.qos.logback.core.testUtil.StatusChecker;
027import ch.qos.logback.core.util.StatusPrinter;
028
029abstract public class AbstractPatternLayoutBaseTest<E> {
030
031    abstract public PatternLayoutBase<E> getPatternLayoutBase();
032
033    abstract public E getEventObject();
034
035    abstract public Context getContext();
036
037    @Test
038    public void testUnStarted() {
039        PatternLayoutBase<E> plb = getPatternLayoutBase();
040        Context context = new ContextBase();
041        plb.setContext(context);
042        String s = plb.doLayout(getEventObject());
043        assertEquals("", s);
044        StatusManager sm = context.getStatusManager();
045        StatusPrinter.print(sm);
046    }
047
048    /**
049     * This test checks that the pattern layout implementation starts its
050     * converters. ExceptionalConverter throws an exception if it's convert
051     * method is called before being started.
052     */
053    @Test
054    public void testConverterStart() {
055        PatternLayoutBase<E> plb = getPatternLayoutBase();
056        plb.setContext(getContext());
057        plb.getInstanceConverterMap().put("EX", ExceptionalConverter.class.getName());
058        plb.setPattern("%EX");
059        plb.start();
060        String result = plb.doLayout(getEventObject());
061        assertFalse(result.contains("%PARSER_ERROR_EX"));
062        // System.out.println("========="+result);
063    }
064
065    @Test
066    public void testStarted() {
067        PatternLayoutBase<E> plb = getPatternLayoutBase();
068        Context context = new ContextBase();
069        plb.setContext(context);
070        String s = plb.doLayout(getEventObject());
071        assertEquals("", s);
072        StatusManager sm = context.getStatusManager();
073        StatusPrinter.print(sm);
074    }
075
076    @Test
077    public void testNullPattern() {
078        // System.out.println("testNullPattern");
079        PatternLayoutBase<E> plb = getPatternLayoutBase();
080        Context context = new ContextBase();
081        plb.setContext(context);
082        plb.setPattern(null);
083        plb.start();
084        String s = plb.doLayout(getEventObject());
085        assertEquals("", s);
086        StatusChecker checker = new StatusChecker(context.getStatusManager());
087        // StatusPrinter.print(context);
088        checker.assertContainsMatch("Empty or null pattern.");
089    }
090
091    @Test
092    public void testEmptyPattern() {
093        // System.out.println("testNullPattern");
094        PatternLayoutBase<E> plb = getPatternLayoutBase();
095        Context context = new ContextBase();
096        plb.setContext(context);
097        plb.setPattern("");
098        plb.start();
099        String s = plb.doLayout(getEventObject());
100        assertEquals("", s);
101        StatusChecker checker = new StatusChecker(context.getStatusManager());
102        // StatusPrinter.print(context);
103        checker.assertContainsMatch("Empty or null pattern.");
104    }
105
106}