aboutsummaryrefslogtreecommitdiff
path: root/slf4j-migrator/src/test/java/org/slf4j/migrator/helper/AbbreviatorTest.java
blob: ab8c8cbbc74a2d1046dbda7c737474aa4b8ee05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Copyright (c) 2004-2011 QOS.ch
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.slf4j.migrator.helper;

import org.slf4j.migrator.helper.Abbreviator;

import junit.framework.TestCase;

public class AbbreviatorTest extends TestCase {

    static final char FS = '/';
    static final String INPUT_0 = "/abc/123456/ABC";
    static final String INPUT_1 = "/abc/123456/xxxxx/ABC";

    RandomHelper rh = new RandomHelper(FS);

    public AbbreviatorTest(String arg0) {
        super(arg0);
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testSmoke() {
        {
            Abbreviator abb = new Abbreviator(2, 100, FS);
            String r = abb.abbreviate(INPUT_0);
            assertEquals(INPUT_0, r);
        }

        {
            Abbreviator abb = new Abbreviator(3, 8, FS);
            String r = abb.abbreviate(INPUT_0);
            assertEquals("/abc/.../ABC", r);
        }
        {
            Abbreviator abb = new Abbreviator(3, 8, FS);
            String r = abb.abbreviate(INPUT_0);
            assertEquals("/abc/.../ABC", r);
        }
    }

    public void testImpossibleToAbbreviate() {
        Abbreviator abb = new Abbreviator(2, 20, FS);
        String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
        String r = abb.abbreviate(in);
        assertEquals(in, r);
    }

    public void testNoFS() {
        Abbreviator abb = new Abbreviator(2, 100, FS);
        String r = abb.abbreviate("hello");
        assertEquals("hello", r);

    }

    public void testZeroPrefix() {
        {
            Abbreviator abb = new Abbreviator(0, 100, FS);
            String r = abb.abbreviate(INPUT_0);
            assertEquals(INPUT_0, r);
        }
    }

    public void testTheories() {
        int MAX_RANDOM_FIXED_LEN = 20;
        int MAX_RANDOM_AVG_LEN = 20;
        int MAX_RANDOM_MAX_LEN = 100;
        for (int i = 0; i < 10000; i++) {

            // System.out.println("Test number " + i);

            // 0 <= fixedLen < MAX_RANDOM_FIXED_LEN
            int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
            // 5 <= averageLen < MAX_RANDOM_AVG_LEN
            int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
            // System.out.println("fixedLen="+fixedLen+", averageLen="+averageLen);

            int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
            if (maxLen <= 1) {
                continue;
            }
            // System.out.println("maxLen="+maxLen);
            int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;

            if (targetLen > maxLen) {
                targetLen = maxLen;
            }
            String filename = rh.buildRandomFileName(averageLen, maxLen);

            Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
            String result = abb.abbreviate(filename);
            assertTheory0(averageLen, filename, result, fixedLen, targetLen);
            assertUsefulness(averageLen, filename, result, fixedLen, targetLen);
            assertTheory1(filename, result, fixedLen, targetLen);
            assertTheory2(filename, result, fixedLen, targetLen);
        }
    }

    // result length is smaller than original length
    void assertTheory0(int averageLen, String filename, String result, int fixedLen, int targetLength) {
        assertTrue("filename=[" + filename + "] result=[" + result + "]", result.length() <= filename.length());
    }

    // if conditions allow, result length should be to target length
    void assertUsefulness(int averageLen, String filename, String result, int fixedLen, int targetLength) {
        int resLen = result.length();

        int margin = averageLen * 4;
        if (targetLength > fixedLen + margin) {
            assertTrue("filename=[" + filename + "], result=[" + result + "] resultLength=" + resLen + " fixedLength=" + fixedLen + ", targetLength="
                            + targetLength + ", avgLen=" + averageLen, result.length() <= targetLength + averageLen);
        }
    }

    // result start with prefix found in filename
    void assertTheory1(String filename, String result, int fixedLen, int targetLength) {
        String prefix = filename.substring(0, fixedLen);
        assertTrue(result.startsWith(prefix));
    }

    // The string /.../ is found in the result once at a position higher
    // than fixedLen
    void assertTheory2(String filename, String result, int fixedLen, int targetLength) {
        if (filename == result) {
            return;
        }
        int fillerIndex = result.indexOf(Abbreviator.FILLER);
        assertTrue(fillerIndex >= fixedLen);
    }
}