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.pattern;
015
016import static org.junit.Assert.*;
017
018import org.junit.Test;
019
020import ch.qos.logback.classic.pattern.TargetLengthBasedClassNameAbbreviator;
021
022public class TargetLengthBasedClassNameAbbreviatorTest {
023
024    @Test
025    public void testShortName() {
026        {
027            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(100);
028            String name = "hello";
029            assertEquals(name, abbreviator.abbreviate(name));
030        }
031        {
032            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(100);
033            String name = "hello.world";
034            assertEquals(name, abbreviator.abbreviate(name));
035        }
036    }
037
038    @Test
039    public void testNoDot() {
040        TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
041        String name = "hello";
042        assertEquals(name, abbreviator.abbreviate(name));
043    }
044
045    @Test
046    public void testOneDot() {
047        {
048            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
049            String name = "hello.world";
050            assertEquals("h.world", abbreviator.abbreviate(name));
051        }
052
053        {
054            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
055            String name = "h.world";
056            assertEquals("h.world", abbreviator.abbreviate(name));
057        }
058
059        {
060            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
061            String name = ".world";
062            assertEquals(".world", abbreviator.abbreviate(name));
063        }
064    }
065
066    @Test
067    public void testTwoDot() {
068        {
069            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
070            String name = "com.logback.Foobar";
071            assertEquals("c.l.Foobar", abbreviator.abbreviate(name));
072        }
073
074        {
075            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
076            String name = "c.logback.Foobar";
077            assertEquals("c.l.Foobar", abbreviator.abbreviate(name));
078        }
079
080        {
081            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
082            String name = "c..Foobar";
083            assertEquals("c..Foobar", abbreviator.abbreviate(name));
084        }
085        {
086            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
087            String name = "..Foobar";
088            assertEquals("..Foobar", abbreviator.abbreviate(name));
089        }
090    }
091
092    @Test
093    public void test3Dot() {
094        {
095            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
096            String name = "com.logback.xyz.Foobar";
097            assertEquals("c.l.x.Foobar", abbreviator.abbreviate(name));
098        }
099        {
100            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(13);
101            String name = "com.logback.xyz.Foobar";
102            assertEquals("c.l.x.Foobar", abbreviator.abbreviate(name));
103        }
104        {
105            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(14);
106            String name = "com.logback.xyz.Foobar";
107            assertEquals("c.l.xyz.Foobar", abbreviator.abbreviate(name));
108        }
109
110        {
111            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(15);
112            String name = "com.logback.alligator.Foobar";
113            assertEquals("c.l.a.Foobar", abbreviator.abbreviate(name));
114        }
115    }
116
117    @Test
118    public void testXDot() {
119        {
120            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(21);
121            String name = "com.logback.wombat.alligator.Foobar";
122            assertEquals("c.l.w.a.Foobar", abbreviator.abbreviate(name));
123        }
124
125        {
126            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(22);
127            String name = "com.logback.wombat.alligator.Foobar";
128            assertEquals("c.l.w.alligator.Foobar", abbreviator.abbreviate(name));
129        }
130
131        {
132            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
133            String name = "com.logback.wombat.alligator.tomato.Foobar";
134            assertEquals("c.l.w.a.t.Foobar", abbreviator.abbreviate(name));
135        }
136
137        {
138            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(21);
139            String name = "com.logback.wombat.alligator.tomato.Foobar";
140            assertEquals("c.l.w.a.tomato.Foobar", abbreviator.abbreviate(name));
141        }
142
143        {
144            TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(29);
145            String name = "com.logback.wombat.alligator.tomato.Foobar";
146            assertEquals("c.l.w.alligator.tomato.Foobar", abbreviator.abbreviate(name));
147        }
148    }
149}