NameAbbreviatorTest xref

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.pattern;
    19
    20  import junit.framework.TestCase;
    21
    22
    23  /**
    24   * Tests for NameAbbrevator.
    25   *
    26   */
    27  public class NameAbbreviatorTest extends TestCase {
    28    /**
    29     * Create a new instance.
    30     *
    31     * @param name test name
    32     */
    33    public NameAbbreviatorTest(final String name) {
    34      super(name);
    35    }
    36
    37    /**
    38     * Check that getDefaultAbbreviator does not return null.
    39     *
    40     */
    41    public void testGetDefault() {
    42      NameAbbreviator abbrev = NameAbbreviator.getDefaultAbbreviator();
    43      assertNotNull(abbrev);
    44    }
    45
    46    /**
    47     * Check that "0" drops all name content.
    48     *
    49     */
    50    public void testZero() {
    51      NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("0");
    52      StringBuffer buf = new StringBuffer("DEBUG - ");
    53      int fieldStart = buf.length();
    54      buf.append("org.example.foo.bar");
    55      abbrev.abbreviate(fieldStart, buf);
    56      assertEquals("DEBUG - ", buf.toString());
    57    }
    58
    59    /**
    60     * Check that getAbbreviator(" ") returns default abbreviator.
    61     *
    62     */
    63    public void testBlank() {
    64      NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("   ");
    65      NameAbbreviator defaultAbbrev = NameAbbreviator.getDefaultAbbreviator();
    66      assertTrue(abbrev == defaultAbbrev);
    67    }
    68
    69    /**
    70     * Check that getAbbreviator("1").abbreviate() drops all but the final name element.
    71     *
    72     */
    73    public void testOne() {
    74      NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1");
    75      StringBuffer buf = new StringBuffer("DEBUG - ");
    76      int fieldStart = buf.length();
    77      buf.append("org.example.foo.bar");
    78      abbrev.abbreviate(fieldStart, buf);
    79      assertEquals("DEBUG - bar", buf.toString());
    80
    81      buf.setLength(0);
    82      buf.append("DEBUG - ");
    83      fieldStart = buf.length();
    84      buf.append("bar");
    85      abbrev.abbreviate(fieldStart, buf);
    86      assertEquals("DEBUG - bar", buf.toString());
    87
    88      buf.setLength(0);
    89      buf.append("DEBUG - ");
    90      fieldStart = buf.length();
    91      abbrev.abbreviate(fieldStart, buf);
    92      assertEquals("DEBUG - ", buf.toString());
    93    }
    94
    95    /**
    96     * Check that blanks are trimmed in evaluating abbreviation pattern.
    97     */
    98    public void testBlankOne() {
    99      NameAbbreviator abbrev = NameAbbreviator.getAbbreviator(" 1 ");
    100     StringBuffer buf = new StringBuffer("DEBUG - ");
    101     int fieldStart = buf.length();
    102     buf.append("org.example.foo.bar");
    103     abbrev.abbreviate(fieldStart, buf);
    104     assertEquals("DEBUG - bar", buf.toString());
    105
    106     buf.setLength(0);
    107     buf.append("DEBUG - ");
    108     fieldStart = buf.length();
    109     buf.append("bar");
    110     abbrev.abbreviate(fieldStart, buf);
    111     assertEquals("DEBUG - bar", buf.toString());
    112
    113     buf.setLength(0);
    114     buf.append("DEBUG - ");
    115     fieldStart = buf.length();
    116     abbrev.abbreviate(fieldStart, buf);
    117     assertEquals("DEBUG - ", buf.toString());
    118   }
    119
    120   /**
    121    * Check that getAbbreviator("2").abbreviate drops all but the last two elements.
    122    *
    123    */
    124   public void testTwo() {
    125     NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("2");
    126     StringBuffer buf = new StringBuffer("DEBUG - ");
    127     int fieldStart = buf.length();
    128     buf.append("org.example.foo.bar");
    129     abbrev.abbreviate(fieldStart, buf);
    130     assertEquals("DEBUG - foo.bar", buf.toString());
    131
    132     buf.setLength(0);
    133     buf.append("DEBUG - ");
    134     fieldStart = buf.length();
    135     buf.append("foo.bar");
    136     abbrev.abbreviate(fieldStart, buf);
    137     assertEquals("DEBUG - foo.bar", buf.toString());
    138
    139     buf.setLength(0);
    140     buf.append("DEBUG - ");
    141     fieldStart = buf.length();
    142     buf.append("bar");
    143     abbrev.abbreviate(fieldStart, buf);
    144     assertEquals("DEBUG - bar", buf.toString());
    145   }
    146
    147   /**
    148    * Check that getAbbreviator("1.").abbreviate abbreviates non-final elements
    149    * to one character.
    150    *
    151    */
    152   public void testOneDot() {
    153     NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1.");
    154     StringBuffer buf = new StringBuffer("DEBUG - ");
    155     int fieldStart = buf.length();
    156     buf.append("org.example.foo.bar");
    157     abbrev.abbreviate(fieldStart, buf);
    158     assertEquals("DEBUG - o.e.f.bar", buf.toString());
    159
    160     buf.setLength(0);
    161     buf.append("DEBUG - ");
    162     fieldStart = buf.length();
    163     buf.append("org.example.foo.");
    164     abbrev.abbreviate(fieldStart, buf);
    165     assertEquals("DEBUG - o.e.f.", buf.toString());
    166
    167
    168     buf.setLength(0);
    169     buf.append("DEBUG - ");
    170     fieldStart = buf.length();
    171     buf.append("foo.bar");
    172     abbrev.abbreviate(fieldStart, buf);
    173     assertEquals("DEBUG - f.bar", buf.toString());
    174
    175     buf.setLength(0);
    176     buf.append("DEBUG - ");
    177     fieldStart = buf.length();
    178     buf.append("bar");
    179     abbrev.abbreviate(fieldStart, buf);
    180     assertEquals("DEBUG - bar", buf.toString());
    181
    182     buf.setLength(0);
    183     buf.append("DEBUG - ");
    184     fieldStart = buf.length();
    185     abbrev.abbreviate(fieldStart, buf);
    186     assertEquals("DEBUG - ", buf.toString());
    187
    188     buf.setLength(0);
    189     buf.append("DEBUG - ");
    190     fieldStart = buf.length();
    191     buf.append(".");
    192     abbrev.abbreviate(fieldStart, buf);
    193     assertEquals("DEBUG - .", buf.toString());
    194   }
    195
    196   /**
    197    * Check that getAbbreviator("1~.").abbreviate abbreviates non-final elements
    198    * to one character and a tilde.
    199    *
    200    */
    201   public void testOneTildeDot() {
    202     NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1~.");
    203     StringBuffer buf = new StringBuffer("DEBUG - ");
    204     int fieldStart = buf.length();
    205     buf.append("org.example.foo.bar");
    206     abbrev.abbreviate(fieldStart, buf);
    207     assertEquals("DEBUG - o~.e~.f~.bar", buf.toString());
    208
    209     buf.setLength(0);
    210     buf.append("DEBUG - ");
    211     fieldStart = buf.length();
    212     buf.append("org.example.foo.");
    213     abbrev.abbreviate(fieldStart, buf);
    214     assertEquals("DEBUG - o~.e~.f~.", buf.toString());
    215
    216     buf.setLength(0);
    217     buf.append("DEBUG - ");
    218     fieldStart = buf.length();
    219     buf.append("foo.bar");
    220     abbrev.abbreviate(fieldStart, buf);
    221     assertEquals("DEBUG - f~.bar", buf.toString());
    222
    223     buf.setLength(0);
    224     buf.append("DEBUG - ");
    225     fieldStart = buf.length();
    226     buf.append("bar");
    227     abbrev.abbreviate(fieldStart, buf);
    228     assertEquals("DEBUG - bar", buf.toString());
    229
    230     buf.setLength(0);
    231     buf.append("DEBUG - ");
    232     fieldStart = buf.length();
    233     abbrev.abbreviate(fieldStart, buf);
    234     assertEquals("DEBUG - ", buf.toString());
    235
    236
    237     buf.setLength(0);
    238     buf.append("DEBUG - ");
    239     fieldStart = buf.length();
    240     buf.append(".");
    241     abbrev.abbreviate(fieldStart, buf);
    242     assertEquals("DEBUG - .", buf.toString());
    243
    244
    245     buf.setLength(0);
    246     buf.append("DEBUG - ");
    247     fieldStart = buf.length();
    248     buf.append("o.e.f.bar");
    249     abbrev.abbreviate(fieldStart, buf);
    250     assertEquals("DEBUG - o.e.f.bar", buf.toString());
    251   }
    252
    253   /**
    254    * Check that getAbbreviator("1.*.2").abbreviate drops all but the first
    255    * character from the first element, uses all of the second element and
    256    * drops all but the first two characters of the rest of the non-final elements.
    257    *
    258    */
    259   public void testMulti() {
    260     NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1.*.2");
    261     StringBuffer buf = new StringBuffer("DEBUG - ");
    262     int fieldStart = buf.length();
    263     buf.append("org.example.foo.bar");
    264     abbrev.abbreviate(fieldStart, buf);
    265     assertEquals("DEBUG - o.example.fo.bar", buf.toString());
    266
    267     buf.setLength(0);
    268     buf.append("DEBUG - ");
    269     fieldStart = buf.length();
    270     buf.append("org.example.foo.");
    271     abbrev.abbreviate(fieldStart, buf);
    272     assertEquals("DEBUG - o.example.fo.", buf.toString());
    273
    274     buf.setLength(0);
    275     buf.append("DEBUG - ");
    276     fieldStart = buf.length();
    277     buf.append("foo.bar");
    278     abbrev.abbreviate(fieldStart, buf);
    279     assertEquals("DEBUG - f.bar", buf.toString());
    280
    281     buf.setLength(0);
    282     buf.append("DEBUG - ");
    283     fieldStart = buf.length();
    284     buf.append("bar");
    285     abbrev.abbreviate(fieldStart, buf);
    286     assertEquals("DEBUG - bar", buf.toString());
    287
    288     buf.setLength(0);
    289     buf.append("DEBUG - ");
    290     fieldStart = buf.length();
    291     abbrev.abbreviate(fieldStart, buf);
    292     assertEquals("DEBUG - ", buf.toString());
    293
    294     buf.setLength(0);
    295     buf.append("DEBUG - ");
    296     fieldStart = buf.length();
    297     buf.append(".");
    298     abbrev.abbreviate(fieldStart, buf);
    299     assertEquals("DEBUG - .", buf.toString());
    300   }
    301
    302   /**
    303    * Check that getAbbreviator("-1").abbreviate() drops first name element.
    304    *
    305    */
    306   public void testMinusOne() {
    307     NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("-1");
    308     StringBuffer buf = new StringBuffer("DEBUG - ");
    309     int fieldStart = buf.length();
    310     buf.append("org.example.foo.bar");
    311     abbrev.abbreviate(fieldStart, buf);
    312     assertEquals("DEBUG - example.foo.bar", buf.toString());
    313
    314     buf.setLength(0);
    315     buf.append("DEBUG - ");
    316     fieldStart = buf.length();
    317     buf.append("bar");
    318     abbrev.abbreviate(fieldStart, buf);
    319     assertEquals("DEBUG - bar", buf.toString());
    320
    321     buf.setLength(0);
    322     buf.append("DEBUG - ");
    323     fieldStart = buf.length();
    324     abbrev.abbreviate(fieldStart, buf);
    325     assertEquals("DEBUG - ", buf.toString());
    326
    327     buf.setLength(0);
    328     buf.append("DEBUG - ");
    329     fieldStart = buf.length();
    330     buf.append(".");
    331     abbrev.abbreviate(fieldStart, buf);
    332     assertEquals("DEBUG - ", buf.toString());
    333
    334   }
    335
    336 }