001    /*
002     * $Id: NullStringCatenationTest.java 1180547 2011-10-09 05:44:18Z grobmeier $
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     * http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    package org.apache.commons.ognl.test;
021    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    
025    import org.apache.commons.ognl.test.objects.Root;
026    import org.junit.Test;
027    import org.junit.runner.RunWith;
028    import org.junit.runners.Parameterized;
029    import org.junit.runners.Parameterized.Parameters;
030    
031    @RunWith(value = Parameterized.class)
032    public class NullStringCatenationTest
033        extends OgnlTestCase
034    {
035    
036        public static final String MESSAGE = "blarney";
037    
038        private static Root ROOT = new Root();
039    
040        private static Object[][] TESTS =
041        {
042            // Null string catenation
043            { ROOT, "\"bar\" + null", "barnull" }, // Catenate null to a string
044            { ROOT, "\"bar\" + nullObject", "barnull" }, // Catenate null to a string
045            { ROOT, "20.56 + nullObject", NullPointerException.class }, // Catenate null to a number
046            { ROOT, "(true ? 'tabHeader' : '') + (false ? 'tabHeader' : '')", "tabHeader" },
047            { ROOT, "theInt == 0 ? '5%' : theInt + '%'", "6%" },
048            { ROOT, "'width:' + width + ';'", "width:238px;" },
049            { ROOT, "theLong + '_' + index", "4_1" },
050            { ROOT, "'javascript:' + @org.apache.commons.ognl.test.NullStringCatenationTest@MESSAGE", "javascript:blarney" },
051            { ROOT,  "printDelivery ? '' : 'javascript:deliverySelected(' + property.carrier + ',' + currentDeliveryId + ')'", "" }, 
052            { ROOT, "bean2.id + '_' + theInt", "1_6" } 
053        };
054    
055        
056        /**
057         * Setup parameters for this test which are used to call this class constructor
058         * @return the collection of paramaters 
059         */
060        @Parameters
061        public static Collection<Object[]> data()
062        {
063            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
064            for ( int i = 0; i < TESTS.length; i++ )
065            {
066                Object[] tmp = new Object[4];
067                tmp[0] = TESTS[i][1];
068                tmp[1] = TESTS[i][0];
069                tmp[2] = TESTS[i][1];
070                tmp[3] = TESTS[i][2];
071    
072                data.add( tmp );
073            }
074            return data;
075        }
076    
077        /**
078         * Constructor: size of the Object[] returned by the @Parameter annotated method must match
079         * the number of arguments in this constructor  
080         */
081        public NullStringCatenationTest( String name, Object root, String expressionString, Object expectedResult)
082        {
083            super( name, root, expressionString, expectedResult );
084        }
085    
086        @Test
087        @Override
088        public void runTest() throws Exception
089        {
090            super.runTest();
091        }
092    }