View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import static org.junit.Assert.*;
17  
18  import org.junit.Test;
19  
20  import ch.qos.logback.classic.pattern.TargetLengthBasedClassNameAbbreviator;
21  
22  public class TargetLengthBasedClassNameAbbreviatorTest {
23  
24      @Test
25      public void testShortName() {
26          {
27              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(100);
28              String name = "hello";
29              assertEquals(name, abbreviator.abbreviate(name));
30          }
31          {
32              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(100);
33              String name = "hello.world";
34              assertEquals(name, abbreviator.abbreviate(name));
35          }
36      }
37  
38      @Test
39      public void testNoDot() {
40          TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
41          String name = "hello";
42          assertEquals(name, abbreviator.abbreviate(name));
43      }
44  
45      @Test
46      public void testOneDot() {
47          {
48              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
49              String name = "hello.world";
50              assertEquals("h.world", abbreviator.abbreviate(name));
51          }
52  
53          {
54              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
55              String name = "h.world";
56              assertEquals("h.world", abbreviator.abbreviate(name));
57          }
58  
59          {
60              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
61              String name = ".world";
62              assertEquals(".world", abbreviator.abbreviate(name));
63          }
64      }
65  
66      @Test
67      public void testTwoDot() {
68          {
69              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
70              String name = "com.logback.Foobar";
71              assertEquals("c.l.Foobar", abbreviator.abbreviate(name));
72          }
73  
74          {
75              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
76              String name = "c.logback.Foobar";
77              assertEquals("c.l.Foobar", abbreviator.abbreviate(name));
78          }
79  
80          {
81              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
82              String name = "c..Foobar";
83              assertEquals("c..Foobar", abbreviator.abbreviate(name));
84          }
85          {
86              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
87              String name = "..Foobar";
88              assertEquals("..Foobar", abbreviator.abbreviate(name));
89          }
90      }
91  
92      @Test
93      public void test3Dot() {
94          {
95              TargetLengthBasedClassNameAbbreviator abbreviator = new TargetLengthBasedClassNameAbbreviator(1);
96              String name = "com.logback.xyz.Foobar";
97              assertEquals("c.l.x.Foobar", abbreviator.abbreviate(name));
98          }
99          {
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 }