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;
015
016import static org.junit.Assert.*;
017
018import org.junit.Test;
019
020import ch.qos.logback.core.Context;
021import ch.qos.logback.core.ContextBase;
022import ch.qos.logback.core.pattern.PatternLayoutBase;
023import ch.qos.logback.core.pattern.parser.test.AbstractPatternLayoutBaseTest;
024
025public class SamplePatternLayoutTest extends AbstractPatternLayoutBaseTest<Object> {
026
027    Context context = new ContextBase();
028
029    public PatternLayoutBase<Object> getPatternLayoutBase() {
030        return new SamplePatternLayout<Object>();
031    }
032
033    public Object getEventObject() {
034        return new Object();
035    }
036
037    @Test
038    public void testOK() {
039        PatternLayoutBase<Object> plb = getPatternLayoutBase();
040        Context context = new ContextBase();
041        plb.setContext(context);
042        plb.setPattern("x%OTT");
043        plb.start();
044        String s = plb.doLayout(new Object());
045        // System.out.println(s);
046
047        // StatusManager sm = context.getStatusManager();
048        // StatusPrinter.print(sm);
049        assertEquals("x123", s);
050    }
051
052    @Test
053    public void testEscapeClosingParentheses() {
054        PatternLayoutBase<Object> plb = getPatternLayoutBase();
055        Context context = new ContextBase();
056        plb.setContext(context);
057        plb.setPattern("x(%OTT\\)y");
058        plb.start();
059        String s = plb.doLayout(new Object());
060        assertEquals("x(123)y", s);
061    }
062
063    @Test
064    public void testEscapeBothParentheses() {
065        PatternLayoutBase<Object> plb = getPatternLayoutBase();
066        Context context = new ContextBase();
067        plb.setContext(context);
068        plb.setPattern("x\\(%OTT\\)y");
069        plb.start();
070        String s = plb.doLayout(new Object());
071        assertEquals("x(123)y", s);
072    }
073
074    @Test
075    public void testPercentAsLiteral() {
076        PatternLayoutBase<Object> plb = getPatternLayoutBase();
077        Context context = new ContextBase();
078        plb.setContext(context);
079        plb.setPattern("hello \\% world");
080        plb.start();
081        String s = plb.doLayout(new Object());
082        assertEquals("hello % world", s);
083    }
084
085    @Override
086    public Context getContext() {
087        return context;
088    }
089}