View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.migrator.helper;
26  
27  import org.slf4j.migrator.helper.Abbreviator;
28  
29  import junit.framework.TestCase;
30  
31  public class AbbreviatorTest extends TestCase {
32  
33    static final char FS = '/';
34    static final String INPUT_0 = "/abc/123456/ABC";
35    static final String INPUT_1 = "/abc/123456/xxxxx/ABC";
36  
37    RandomHelper rh = new RandomHelper(FS);
38  
39    public AbbreviatorTest(String arg0) {
40      super(arg0);
41    }
42  
43    protected void setUp() throws Exception {
44      super.setUp();
45    }
46  
47    protected void tearDown() throws Exception {
48      super.tearDown();
49    }
50  
51    public void testSmoke() {
52      {
53        Abbreviator abb = new Abbreviator(2, 100, FS);
54        String r = abb.abbreviate(INPUT_0);
55        assertEquals(INPUT_0, r);
56      }
57  
58      {
59        Abbreviator abb = new Abbreviator(3, 8, FS);
60        String r = abb.abbreviate(INPUT_0);
61        assertEquals("/abc/.../ABC", r);
62      }
63      {
64        Abbreviator abb = new Abbreviator(3, 8, FS);
65        String r = abb.abbreviate(INPUT_0);
66        assertEquals("/abc/.../ABC", r);
67      }
68    }
69  
70    public void testImpossibleToAbbreviate() {
71      Abbreviator abb = new Abbreviator(2, 20, FS);
72      String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
73      String r = abb.abbreviate(in);
74      assertEquals(in, r);
75    }
76  
77    public void testNoFS() {
78      Abbreviator abb = new Abbreviator(2, 100, FS);
79      String r = abb.abbreviate("hello");
80      assertEquals("hello", r);
81  
82    }
83  
84    public void testZeroPrefix() {
85      {
86        Abbreviator abb = new Abbreviator(0, 100, FS);
87        String r = abb.abbreviate(INPUT_0);
88        assertEquals(INPUT_0, r);
89      }
90    }
91  
92    public void testTheories() {
93      int MAX_RANDOM_FIXED_LEN = 20;
94      int MAX_RANDOM_AVG_LEN = 20;
95      int MAX_RANDOM_MAX_LEN = 100;
96      for (int i = 0; i < 10000; i++) {
97  
98        //System.out.println("Test number " + i);
99  
100       // 0 <= fixedLen < MAX_RANDOM_FIXED_LEN
101       int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
102       // 5 <= averageLen < MAX_RANDOM_AVG_LEN
103       int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
104       // System.out.println("fixedLen="+fixedLen+", averageLen="+averageLen);
105 
106       int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
107       if (maxLen <= 1) {
108         continue;
109       }
110       // System.out.println("maxLen="+maxLen);
111       int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;
112 
113       if (targetLen > maxLen) {
114         targetLen = maxLen;
115       }
116       String filename = rh.buildRandomFileName(averageLen, maxLen);
117 
118       Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
119       String result = abb.abbreviate(filename);
120       assertTheory0(averageLen, filename, result, fixedLen, targetLen);
121       assertUsefulness(averageLen, filename, result, fixedLen, targetLen);
122       assertTheory1(filename, result, fixedLen, targetLen);
123       assertTheory2(filename, result, fixedLen, targetLen);
124     }
125   }
126 
127   // result length is smaller than original length 
128   void assertTheory0(int averageLen, String filename, String result,
129       int fixedLen, int targetLength) {
130       assertTrue("filename=[" + filename + "] result=[" + result + "]", result
131         .length() <= filename.length());
132   }
133 
134   // if conditions allow, result length should be to target length
135   void assertUsefulness(int averageLen, String filename, String result,
136       int fixedLen, int targetLength) {
137     int resLen = result.length();
138 
139     int margin = averageLen * 4;
140     if (targetLength > fixedLen + margin) {
141       assertTrue("filename=[" + filename + "], result=[" + result
142           + "] resultLength=" + resLen + " fixedLength=" + fixedLen
143           + ", targetLength=" + targetLength + ", avgLen=" + averageLen, result
144           .length() <= targetLength + averageLen);
145     }
146   }
147 
148   // result start with prefix found in filename
149   void assertTheory1(String filename, String result, int fixedLen,
150       int targetLength) {
151     String prefix = filename.substring(0, fixedLen);
152     assertTrue(result.startsWith(prefix));
153   }
154 
155   // The string /.../ is found in the result once at a position higher
156   // than fixedLen
157   void assertTheory2(String filename, String result, int fixedLen,
158       int targetLength) {
159     if (filename == result) {
160       return;
161     }
162     int fillerIndex = result.indexOf(Abbreviator.FILLER);
163     assertTrue(fillerIndex >= fixedLen);
164   }
165 }