AbbreviatorTest xref

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