View Javadoc

    1   package org.apache.turbine.util.parser;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import static org.junit.Assert.assertEquals;
    25  import static org.junit.Assert.assertNull;
    26  import static org.junit.Assert.assertTrue;
    27  import static org.junit.Assert.assertFalse;
    28  import static org.junit.Assert.fail;
    29
    30  import java.math.BigDecimal;
    31
    32  import org.apache.fulcrum.parser.BaseValueParser;
    33  import org.apache.fulcrum.parser.ParserService;
    34  import org.apache.turbine.services.TurbineServices;
    35  import org.apache.turbine.test.BaseTestCase;
    36  import org.apache.turbine.util.TurbineConfig;
    37  import org.junit.AfterClass;
    38  import org.junit.Before;
    39  import org.junit.BeforeClass;
    40  import org.junit.Ignore;
    41  import org.junit.Test;
    42
    43  /**
    44   * Testing of the Fulcrum BaseValueParser class
    45   *
    46   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    47   * @version $Id: BaseValueParserTest.java 222043 2004-12-06 17:47:33Z painter $
    48   */
    49  public class BaseValueParserTest extends BaseTestCase
    50  {
    51      private org.apache.fulcrum.parser.BaseValueParser parser;
    52
    53      private ParserService parserService;
    54
    55      private static TurbineConfig tc = null;
    56
    57
    58      /**
    59       * Performs any initialization that must happen before each test is run.
    60       */
    61      @BeforeClass
    62      public static void init() {
    63              tc =
    64                  new TurbineConfig(
    65                      ".",
    66                      "/conf/test/CompleteTurbineResources.properties");
    67              tc.initialize();
    68      }
    69      @Before
    70      public void setUp()
    71      {
    72          try
    73          {
    74              parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
    75              parser = parserService.getParser(BaseValueParser.class);
    76          }
    77          catch (InstantiationException e)
    78          {
    79              fail(e.getMessage());
    80          }
    81      }
    82
    83      /**
    84       * Clean up after each test is run.
    85       */
    86      @AfterClass
    87      public static void tearDown()
    88      {
    89          if (tc != null)
    90          {
    91              tc.dispose();
    92          }
    93      }
    94
    95      @Test public void testGetByte()
    96      {
    97          // no param
    98          byte result = parser.getByte("invalid");
    99          assertEquals(result, 0);
    100
    101         // default
    102         result = parser.getByte("default", (byte)3);
    103         assertEquals(result, 3);
    104
    105         // param exists
    106         parser.add("exists", "1");
    107         result = parser.getByte("exists");
    108         assertEquals(result, 1);
    109
    110         // unparsable value
    111         parser.add("unparsable", "a");
    112         result = parser.getByte("unparsable");
    113         assertEquals(result, 0);
    114     }
    115
    116     @Test public void testGetByteObject()
    117     {
    118         // no param
    119         Byte result = parser.getByteObject("invalid");
    120         assertNull(result);
    121
    122         // default
    123         result = parser.getByteObject("default", new Byte((byte)3));
    124         assertEquals(result, new Byte((byte)3));
    125
    126         // param exists
    127         parser.add("exists", "1");
    128         result = parser.getByteObject("exists");
    129         assertEquals(result, new Byte((byte)1));
    130
    131         // unparsable value
    132         parser.add("unparsable", "a");
    133         result = parser.getByteObject("unparsable");
    134         assertNull(result);
    135     }
    136
    137     @Test public void testGetInt()
    138     {
    139         // no param
    140         int result = parser.getInt("invalid");
    141         assertEquals(result, 0);
    142
    143         // default
    144         result = parser.getInt("default", 3);
    145         assertEquals(result, 3);
    146
    147         // param exists
    148         parser.add("exists", "1");
    149         result = parser.getInt("exists");
    150         assertEquals(result, 1);
    151
    152         // unparsable value
    153         parser.add("unparsable", "a");
    154         result = parser.getInt("unparsable");
    155         assertEquals(result, 0);
    156
    157         // array
    158         parser.add("array", "1");
    159         parser.add("array", "2");
    160         parser.add("array", "3");
    161         int arrayResult[] = parser.getInts("array");
    162         int compare[] = {1,2,3};
    163         assertEquals(arrayResult.length, compare.length);
    164         for( int i=0; i<compare.length; i++)
    165         {
    166             assertEquals(compare[i], arrayResult[i]);
    167         }
    168
    169         // array w/ unparsable element
    170         parser.add("array2", "1");
    171         parser.add("array2", "a");
    172         parser.add("array2", "3");
    173         int arrayResult2[] = parser.getInts("array2");
    174         int compare2[] = {1,0,3};
    175         assertEquals(arrayResult2.length, compare2.length);
    176         for( int i=0; i<compare2.length; i++)
    177         {
    178             assertEquals(compare2[i], arrayResult2[i] );
    179         }
    180     }
    181
    182     @Test public void testGetIntObject()
    183     {
    184         // no param
    185         Integer result = parser.getIntObject("invalid");
    186         assertNull(result);
    187
    188         // default
    189         result = parser.getIntObject("default", new Integer(3));
    190         assertEquals(result, new Integer(3));
    191
    192         // param exists
    193         parser.add("exists", "1");
    194         result = parser.getIntObject("exists");
    195         assertEquals(result, new Integer(1));
    196
    197         // unparsable value
    198         parser.add("unparsable", "a");
    199         result = parser.getIntObject("unparsable");
    200         assertNull(result);
    201
    202         // array
    203         parser.add("array", "1");
    204         parser.add("array", "2");
    205         parser.add("array", "3");
    206         Integer arrayResult[] = parser.getIntObjects("array");
    207         Integer compare[] = {new Integer(1), new Integer(2), new Integer(3)};
    208         assertEquals(arrayResult.length, compare.length);
    209         for( int i=0; i<compare.length; i++)
    210         {
    211             assertEquals(compare[i], arrayResult[i]);
    212         }
    213
    214         // array w/ unparsable element
    215         parser.add("array2", "1");
    216         parser.add("array2", "a");
    217         parser.add("array2", "3");
    218         Integer arrayResult2[] = parser.getIntObjects("array2");
    219         Integer compare2[] = {new Integer(1), null, new Integer(3)};
    220         assertEquals(arrayResult2.length, compare2.length);
    221         for( int i=0; i<compare2.length; i++)
    222         {
    223             assertEquals(compare2[i], arrayResult2[i] );
    224         }
    225     }
    226
    227     @Test public void testGetFloat()
    228     {
    229         // no param
    230         float result = parser.getFloat("invalid");
    231         assertEquals(result, 0, 0);
    232
    233         // default
    234         result = parser.getFloat("default", 3);
    235         assertEquals(result, 3, 0);
    236
    237         // param exists
    238         parser.add("exists", "1");
    239         result = parser.getFloat("exists");
    240         assertEquals(result, 1, 0);
    241
    242         // unparsable value
    243         parser.add("unparsable", "a");
    244         result = parser.getFloat("unparsable");
    245         assertEquals(result, 0, 0);
    246
    247         // array
    248         parser.add("array", "1");
    249         parser.add("array", "2");
    250         parser.add("array", "3");
    251         float arrayResult[] = parser.getFloats("array");
    252         float compare[] = {1,2,3};
    253         assertEquals(arrayResult.length, compare.length);
    254         for( int i=0; i<compare.length; i++)
    255         {
    256             assertEquals(compare[i], arrayResult[i], 0);
    257         }
    258
    259         // array w/ unparsable element
    260         parser.add("array2", "1");
    261         parser.add("array2", "a");
    262         parser.add("array2", "3");
    263         float arrayResult2[] = parser.getFloats("array2");
    264         float compare2[] = {1,0,3};
    265         assertEquals(arrayResult2.length, compare2.length);
    266         for( int i=0; i<compare2.length; i++)
    267         {
    268             assertEquals(compare2[i], arrayResult2[i], 0);
    269         }
    270     }
    271
    272     @Test public void testGetFloatObject()
    273     {
    274         // no param
    275         Float result = parser.getFloatObject("invalid");
    276         assertNull(result);
    277
    278         // default
    279         result = parser.getFloatObject("default", new Float(3));
    280         assertEquals(result, new Float(3));
    281
    282         // param exists
    283         parser.add("exists", "1");
    284         result = parser.getFloatObject("exists");
    285         assertEquals(result, new Float(1));
    286
    287         // unparsable value
    288         parser.add("unparsable", "a");
    289         result = parser.getFloatObject("unparsable");
    290         assertNull(result);
    291
    292         // array
    293         parser.add("array", "1");
    294         parser.add("array", "2");
    295         parser.add("array", "3");
    296         Float arrayResult[] = parser.getFloatObjects("array");
    297         Float compare[] = {new Float(1), new Float(2), new Float(3)};
    298         assertEquals(arrayResult.length, compare.length);
    299         for( int i=0; i<compare.length; i++)
    300         {
    301             assertEquals(compare[i], arrayResult[i]);
    302         }
    303
    304         // array w/ unparsable element
    305         parser.add("array2", "1");
    306         parser.add("array2", "a");
    307         parser.add("array2", "3");
    308         Float arrayResult2[] = parser.getFloatObjects("array2");
    309         Float compare2[] = {new Float(1), null, new Float(3)};
    310         assertEquals(arrayResult2.length, compare2.length);
    311         for( int i=0; i<compare2.length; i++)
    312         {
    313             assertEquals(compare2[i], arrayResult2[i] );
    314         }
    315     }
    316
    317     @Test public void testGetDouble()
    318     {
    319         // no param
    320         double result = parser.getDouble("invalid");
    321         assertEquals(result, 0, 0);
    322
    323         // default
    324         result = parser.getDouble("default", 3);
    325         assertEquals(result, 3, 0);
    326
    327         // param exists
    328         parser.add("exists", "1");
    329         result = parser.getDouble("exists");
    330         assertEquals(result, 1, 0);
    331
    332         // unparsable value
    333         parser.add("unparsable", "a");
    334         result = parser.getDouble("unparsable");
    335         assertEquals(result, 0, 0);
    336
    337         // array
    338         parser.add("array", "1");
    339         parser.add("array", "2");
    340         parser.add("array", "3");
    341         double arrayResult[] = parser.getDoubles("array");
    342         double compare[] = {1,2,3};
    343         assertEquals(arrayResult.length, compare.length);
    344         for( int i=0; i<compare.length; i++)
    345         {
    346             assertEquals(compare[i], arrayResult[i], 0);
    347         }
    348
    349         // array w/ unparsable element
    350         parser.add("array2", "1");
    351         parser.add("array2", "a");
    352         parser.add("array2", "3");
    353         double arrayResult2[] = parser.getDoubles("array2");
    354         double compare2[] = {1,0,3};
    355         assertEquals(arrayResult2.length, compare2.length);
    356         for( int i=0; i<compare2.length; i++)
    357         {
    358             assertEquals(compare2[i], arrayResult2[i], 0);
    359         }
    360     }
    361
    362     @Test public void testGetDoubleObject()
    363     {
    364         // no param
    365         Double result = parser.getDoubleObject("invalid");
    366         assertNull(result);
    367
    368         // default
    369         result = parser.getDoubleObject("default", new Double(3));
    370         assertEquals(result, new Double(3));
    371
    372         // param exists
    373         parser.add("exists", "1");
    374         result = parser.getDoubleObject("exists");
    375         assertEquals(result, new Double(1));
    376
    377         // unparsable value
    378         parser.add("unparsable", "a");
    379         result = parser.getDoubleObject("unparsable");
    380         assertNull(result);
    381
    382         // array
    383         parser.add("array", "1");
    384         parser.add("array", "2");
    385         parser.add("array", "3");
    386         Double arrayResult[] = parser.getDoubleObjects("array");
    387         Double compare[] = {new Double(1), new Double(2), new Double(3)};
    388         assertEquals(arrayResult.length, compare.length);
    389         for( int i=0; i<compare.length; i++)
    390         {
    391             assertEquals(compare[i], arrayResult[i]);
    392         }
    393
    394         // array w/ unparsable element
    395         parser.add("array2", "1");
    396         parser.add("array2", "a");
    397         parser.add("array2", "3");
    398         Double arrayResult2[] = parser.getDoubleObjects("array2");
    399         Double compare2[] = {new Double(1), null, new Double(3)};
    400         assertEquals(arrayResult2.length, compare2.length);
    401         for( int i=0; i<compare2.length; i++)
    402         {
    403             assertEquals(compare2[i], arrayResult2[i] );
    404         }
    405     }
    406
    407     @Test public void testGetLong()
    408     {
    409         // no param
    410         long result = parser.getLong("invalid");
    411         assertEquals(result, 0);
    412
    413         // default
    414         result = parser.getLong("default", 3);
    415         assertEquals(result, 3);
    416
    417         // param exists
    418         parser.add("exists", "1");
    419         result = parser.getLong("exists");
    420         assertEquals(result, 1);
    421
    422         // unparsable value
    423         parser.add("unparsable", "a");
    424         result = parser.getLong("unparsable");
    425         assertEquals(result, 0);
    426
    427         // array
    428         parser.add("array", "1");
    429         parser.add("array", "2");
    430         parser.add("array", "3");
    431         long arrayResult[] = parser.getLongs("array");
    432         long compare[] = {1,2,3};
    433         assertEquals(arrayResult.length, compare.length);
    434         for( int i=0; i<compare.length; i++)
    435         {
    436             assertEquals(compare[i], arrayResult[i]);
    437         }
    438
    439         // array w/ unparsable element
    440         parser.add("array2", "1");
    441         parser.add("array2", "a");
    442         parser.add("array2", "3");
    443         long arrayResult2[] = parser.getLongs("array2");
    444         long compare2[] = {1,0,3};
    445         assertEquals(arrayResult2.length, compare2.length);
    446         for( int i=0; i<compare2.length; i++)
    447         {
    448             assertEquals(compare2[i], arrayResult2[i]);
    449         }
    450     }
    451
    452     @Test public void testGetLongObject()
    453     {
    454         // no param
    455         Long result = parser.getLongObject("invalid");
    456         assertNull(result);
    457
    458         // default
    459         result = parser.getLongObject("default", new Long(3));
    460         assertEquals(result, new Long(3));
    461
    462         // param exists
    463         parser.add("exists", "1");
    464         result = parser.getLongObject("exists");
    465         assertEquals(result, new Long(1));
    466
    467         // unparsable value
    468         parser.add("unparsable", "a");
    469         result = parser.getLongObject("unparsable");
    470         assertNull(result);
    471
    472         // array
    473         parser.add("array", "1");
    474         parser.add("array", "2");
    475         parser.add("array", "3");
    476         Long arrayResult[] = parser.getLongObjects("array");
    477         Long compare[] = {new Long(1), new Long(2), new Long(3)};
    478         assertEquals(arrayResult.length, compare.length);
    479         for( int i=0; i<compare.length; i++)
    480         {
    481             assertEquals(compare[i], arrayResult[i]);
    482         }
    483
    484         // array w/ unparsable element
    485         parser.add("array2", "1");
    486         parser.add("array2", "a");
    487         parser.add("array2", "3");
    488         Long arrayResult2[] = parser.getLongObjects("array2");
    489         Long compare2[] = {new Long(1), null, new Long(3)};
    490         assertEquals(arrayResult2.length, compare2.length);
    491         for( int i=0; i<compare2.length; i++)
    492         {
    493             assertEquals(compare2[i], arrayResult2[i] );
    494         }
    495     }
    496
    497     @Test public void testGetBoolean()
    498     {
    499         // no param
    500         boolean result = parser.getBoolean("invalid");
    501         assertFalse(result);
    502
    503         // default
    504         result = parser.getBoolean("default", true);
    505         assertTrue(result);
    506
    507         // true values - Case is intentional
    508         parser.add("true1", "trUe");
    509         result = parser.getBoolean("true1");
    510         assertTrue(result);
    511         parser.add("true2", "yEs");
    512         result = parser.getBoolean("true2");
    513         assertTrue(result);
    514         parser.add("true3", "1");
    515         result = parser.getBoolean("true3");
    516         assertTrue(result);
    517         parser.add("true4", "oN");
    518         result = parser.getBoolean("true4");
    519         assertTrue(result);
    520
    521         // unparsable value
    522         parser.add("unparsable", "a");
    523         result = parser.getBoolean("unparsable");
    524         assertFalse(result);
    525
    526     }
    527
    528     @Test public void testGetBooleanObject()
    529     {
    530         // no param
    531         Boolean result = parser.getBooleanObject("invalid");
    532         assertNull(result);
    533
    534         // default
    535         result = parser.getBooleanObject("default", Boolean.TRUE);
    536         assertEquals(result, Boolean.TRUE);
    537
    538         // true values - Case is intentional
    539         parser.add("true1", "trUe");
    540         result = parser.getBooleanObject("true1");
    541         assertEquals(result, Boolean.TRUE);
    542         parser.add("true2", "yEs");
    543         result = parser.getBooleanObject("true2");
    544         assertEquals(result, Boolean.TRUE);
    545         parser.add("true3", "1");
    546         result = parser.getBooleanObject("true3");
    547         assertEquals(result, Boolean.TRUE);
    548         parser.add("true4", "oN");
    549         result = parser.getBooleanObject("true4");
    550         assertEquals(result, Boolean.TRUE);
    551
    552         // false values - Case is intentional
    553         parser.add("false1", "falSe");
    554         result = parser.getBooleanObject("false1");
    555         assertEquals(result, Boolean.FALSE);
    556         parser.add("false2", "nO");
    557         result = parser.getBooleanObject("false2");
    558         assertEquals(result, Boolean.FALSE);
    559         parser.add("false3", "0");
    560         result = parser.getBooleanObject("false3");
    561         assertEquals(result, Boolean.FALSE);
    562         parser.add("false4", "oFf");
    563         result = parser.getBooleanObject("false4");
    564         assertEquals(result, Boolean.FALSE);
    565
    566
    567         // unparsable value
    568         parser.add("unparsable", "a");
    569         result = parser.getBooleanObject("unparsable");
    570         assertNull(result);
    571     }
    572
    573     /**
    574      * There seems to be some sort of Java version issue that causes this to fail
    575      * on Java 1.4.2_09 on OSX.
    576      *
    577      */
    578     @Ignore public void testGetBigDecimal()
    579     {
    580         // no param
    581         BigDecimal result = parser.getBigDecimal("invalid");
    582         assertEquals(new BigDecimal(0), result);
    583
    584         // default
    585         result = parser.getBigDecimal("default", new BigDecimal(3));
    586         assertEquals(result, new BigDecimal(3));
    587
    588         // param exists
    589         parser.add("exists", "1");
    590         result = parser.getBigDecimal("exists");
    591         assertEquals(result, new BigDecimal(1));
    592
    593         // unparsable value
    594         parser.add("unparsable", "a");
    595         result = parser.getBigDecimal("unparsable");
    596         assertEquals(new BigDecimal(0), result);
    597
    598         // array
    599         parser.add("array", "1");
    600         parser.add("array", "2");
    601         parser.add("array", "3");
    602         BigDecimal arrayResult[] = parser.getBigDecimals("array");
    603         BigDecimal compare[] = {new BigDecimal(1), new BigDecimal(2),
    604                                 new BigDecimal(3)};
    605         assertEquals(arrayResult.length, compare.length);
    606         for( int i=0; i<compare.length; i++)
    607         {
    608             assertEquals(compare[i], arrayResult[i]);
    609         }
    610
    611         // array w/ unparsable element
    612         parser.add("array2", "1");
    613         parser.add("array2", "a");
    614         parser.add("array2", "3");
    615         BigDecimal arrayResult2[] = parser.getBigDecimals("array2");
    616         BigDecimal compare2[] = {new BigDecimal(1), null, new BigDecimal(3)};
    617         assertEquals(arrayResult2.length, compare2.length);
    618         for( int i=0; i<compare2.length; i++)
    619         {
    620             assertEquals(compare2[i], arrayResult2[i] );
    621         }
    622     }
    623
    624
    625     public void getString()
    626     {
    627         // no param
    628         String result = parser.getString("invalid");
    629         assertNull(result);
    630
    631         // default
    632         result = parser.getString("default", "default");
    633         assertEquals(result, "default");
    634
    635         // null value
    636         parser.add("null", "null");
    637         assertNull( parser.getString("null"));
    638
    639         // only return the first added
    640         parser.add("multiple", "test");
    641         parser.add("multiple", "test2");
    642         assertEquals("test2", parser.getString("multiple"));
    643
    644         // array
    645         parser.add("array", "line1");
    646         parser.add("array", "line2");
    647         parser.add("array", "line3");
    648         String arrayResult[] = parser.getStrings("array");
    649         String compare[] = {"line1","line2","line3"};
    650         assertEquals(arrayResult.length, compare.length);
    651         for( int i=0; i<compare.length; i++)
    652         {
    653             assertEquals(compare[i], arrayResult[i]);
    654         }
    655
    656     }
    657
    658     @Test public void testRecycling() throws Exception {
    659     		parser.setCharacterEncoding("fake");
    660     		parser.recycle();
    661     		assertEquals("US-ASCII",parser.getCharacterEncoding());
    662     }
    663
    664 }