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