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.assertEquals;
017import static org.junit.Assert.fail;
018
019import org.junit.Test;
020
021import ch.qos.logback.core.pattern.FormatInfo;
022
023public class FormatInfoTest {
024
025    @Test
026    public void testEndingInDot() {
027        try {
028            FormatInfo.valueOf("45.");
029            fail("45. is not a valid format info string");
030        } catch (IllegalArgumentException iae) {
031            // OK
032        }
033    }
034
035    @Test
036    public void testBasic() {
037        {
038            FormatInfo fi = FormatInfo.valueOf("45");
039            FormatInfo witness = new FormatInfo();
040            witness.setMin(45);
041            assertEquals(witness, fi);
042        }
043
044        {
045            FormatInfo fi = FormatInfo.valueOf("4.5");
046            FormatInfo witness = new FormatInfo();
047            witness.setMin(4);
048            witness.setMax(5);
049            assertEquals(witness, fi);
050        }
051    }
052
053    @Test
054    public void testRightPad() {
055        {
056            FormatInfo fi = FormatInfo.valueOf("-40");
057            FormatInfo witness = new FormatInfo();
058            witness.setMin(40);
059            witness.setLeftPad(false);
060            assertEquals(witness, fi);
061        }
062
063        {
064            FormatInfo fi = FormatInfo.valueOf("-12.5");
065            FormatInfo witness = new FormatInfo();
066            witness.setMin(12);
067            witness.setMax(5);
068            witness.setLeftPad(false);
069            assertEquals(witness, fi);
070        }
071
072        {
073            FormatInfo fi = FormatInfo.valueOf("-14.-5");
074            FormatInfo witness = new FormatInfo();
075            witness.setMin(14);
076            witness.setMax(5);
077            witness.setLeftPad(false);
078            witness.setLeftTruncate(false);
079            assertEquals(witness, fi);
080        }
081    }
082
083    @Test
084    public void testMinOnly() {
085        {
086            FormatInfo fi = FormatInfo.valueOf("49");
087            FormatInfo witness = new FormatInfo();
088            witness.setMin(49);
089            assertEquals(witness, fi);
090        }
091
092        {
093            FormatInfo fi = FormatInfo.valueOf("-587");
094            FormatInfo witness = new FormatInfo();
095            witness.setMin(587);
096            witness.setLeftPad(false);
097            assertEquals(witness, fi);
098        }
099
100    }
101
102    @Test
103    public void testMaxOnly() {
104        {
105            FormatInfo fi = FormatInfo.valueOf(".49");
106            FormatInfo witness = new FormatInfo();
107            witness.setMax(49);
108            assertEquals(witness, fi);
109        }
110
111        {
112            FormatInfo fi = FormatInfo.valueOf(".-5");
113            FormatInfo witness = new FormatInfo();
114            witness.setMax(5);
115            witness.setLeftTruncate(false);
116            assertEquals(witness, fi);
117        }
118    }
119}