1   package org.slf4j.migrator.helper;
2   
3   import org.slf4j.migrator.helper.Abbreviator;
4   
5   import junit.framework.TestCase;
6   
7   public class AbbreviatorTest extends TestCase {
8   
9     static final char FS = '/';
10    static final String INPUT_0 = "/abc/123456/ABC";
11    static final String INPUT_1 = "/abc/123456/xxxxx/ABC";
12  
13    RandomHelper rh = new RandomHelper(FS);
14  
15    public AbbreviatorTest(String arg0) {
16      super(arg0);
17    }
18  
19    protected void setUp() throws Exception {
20      super.setUp();
21    }
22  
23    protected void tearDown() throws Exception {
24      super.tearDown();
25    }
26  
27    public void testSmoke() {
28      {
29        Abbreviator abb = new Abbreviator(2, 100, FS);
30        String r = abb.abbreviate(INPUT_0);
31        assertEquals(INPUT_0, r);
32      }
33  
34      {
35        Abbreviator abb = new Abbreviator(3, 8, FS);
36        String r = abb.abbreviate(INPUT_0);
37        assertEquals("/abc/.../ABC", r);
38      }
39      {
40        Abbreviator abb = new Abbreviator(3, 8, FS);
41        String r = abb.abbreviate(INPUT_0);
42        assertEquals("/abc/.../ABC", r);
43      }
44    }
45  
46    public void testImpossibleToAbbreviate() {
47      Abbreviator abb = new Abbreviator(2, 20, FS);
48      String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
49      String r = abb.abbreviate(in);
50      assertEquals(in, r);
51    }
52  
53    public void testNoFS() {
54      Abbreviator abb = new Abbreviator(2, 100, FS);
55      String r = abb.abbreviate("hello");
56      assertEquals("hello", r);
57  
58    }
59  
60    public void testZeroPrefix() {
61      {
62        Abbreviator abb = new Abbreviator(0, 100, FS);
63        String r = abb.abbreviate(INPUT_0);
64        assertEquals(INPUT_0, r);
65      }
66    }
67  
68    public void testTheories() {
69      int MAX_RANDOM_FIXED_LEN = 20;
70      int MAX_RANDOM_AVG_LEN = 20;
71      int MAX_RANDOM_MAX_LEN = 100;
72      for (int i = 0; i < 10000; i++) {
73  
74        //System.out.println("Test number " + i);
75  
76        // 0 <= fixedLen < MAX_RANDOM_FIXED_LEN
77        int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
78        // 5 <= averageLen < MAX_RANDOM_AVG_LEN
79        int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
80        // System.out.println("fixedLen="+fixedLen+", averageLen="+averageLen);
81  
82        int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
83        if (maxLen <= 1) {
84          continue;
85        }
86        // System.out.println("maxLen="+maxLen);
87        int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;
88  
89        if (targetLen > maxLen) {
90          targetLen = maxLen;
91        }
92        String filename = rh.buildRandomFileName(averageLen, maxLen);
93  
94        Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
95        String result = abb.abbreviate(filename);
96        assertTheory0(averageLen, filename, result, fixedLen, targetLen);
97        assertUsefulness(averageLen, filename, result, fixedLen, targetLen);
98        assertTheory1(filename, result, fixedLen, targetLen);
99        assertTheory2(filename, result, fixedLen, targetLen);
100     }
101   }
102 
103   // result length is smaller than original length 
104   void assertTheory0(int averageLen, String filename, String result,
105       int fixedLen, int targetLength) {
106       assertTrue("filename=[" + filename + "] result=[" + result + "]", result
107         .length() <= filename.length());
108   }
109 
110   // if conditions allow, result length should be to target length
111   void assertUsefulness(int averageLen, String filename, String result,
112       int fixedLen, int targetLength) {
113     int resLen = result.length();
114 
115     int margin = averageLen * 4;
116     if (targetLength > fixedLen + margin) {
117       assertTrue("filename=[" + filename + "], result=[" + result
118           + "] resultLength=" + resLen + " fixedLength=" + fixedLen
119           + ", targetLength=" + targetLength + ", avgLen=" + averageLen, result
120           .length() <= targetLength + averageLen);
121     }
122   }
123 
124   // result start with prefix found in filename
125   void assertTheory1(String filename, String result, int fixedLen,
126       int targetLength) {
127     String prefix = filename.substring(0, fixedLen);
128     assertTrue(result.startsWith(prefix));
129   }
130 
131   // The string /.../ is found in the result once at a position higher
132   // than fixedLen
133   void assertTheory2(String filename, String result, int fixedLen,
134       int targetLength) {
135     if (filename == result) {
136       return;
137     }
138     int fillerIndex = result.indexOf(Abbreviator.FILLER);
139     assertTrue(fillerIndex >= fixedLen);
140   }
141 }