001    /*
002     * $Id: PrimitiveNullHandlingTest.java 1104080 2011-05-17 09:22:09Z mcucchiara $
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.Simple;
026    import org.junit.runner.RunWith;
027    import org.junit.runners.Parameterized;
028    import org.junit.runners.Parameterized.Parameters;
029    
030    @RunWith(value = Parameterized.class)
031    public class PrimitiveNullHandlingTest
032        extends OgnlTestCase
033    {
034    
035        private static Simple SIMPLE = new Simple();
036    
037        static
038        {
039            SIMPLE.setFloatValue( 10.56f );
040            SIMPLE.setIntValue( 34 );
041        }
042    
043        private static Object[][] TESTS = {
044            // Primitive null handling
045            { SIMPLE, "floatValue", new Float( 10.56f ), null, new Float( 0f ) }, // set float to
046            // null, should
047            // yield 0.0f
048            { SIMPLE, "intValue", new Integer( 34 ), null, new Integer( 0 ) },// set int to null,
049            // should yield 0
050            { SIMPLE, "booleanValue", Boolean.FALSE, Boolean.TRUE, Boolean.TRUE },// set boolean
051            // to TRUE,
052            // should yield
053            // true
054            { SIMPLE, "booleanValue", Boolean.TRUE, null, Boolean.FALSE }, // set boolean to null,
055        // should yield false
056    
057            };
058    
059        /*
060         * =================================================================== Public static methods
061         * ===================================================================
062         */
063        @Parameters
064        public static Collection<Object[]> data()
065        {
066            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
067            for ( int i = 0; i < TESTS.length; i++ )
068            {
069                Object[] tmp = new Object[6];
070                tmp[0] = TESTS[i][1];
071                tmp[1] = TESTS[i][0];
072                tmp[2] = TESTS[i][1];
073    
074                switch ( TESTS[i].length )
075                {
076                    case 3:
077                        tmp[3] = TESTS[i][2];
078                        break;
079    
080                    case 4:
081                        tmp[3] = TESTS[i][2];
082                        tmp[4] = TESTS[i][3];
083                        break;
084    
085                    case 5:
086                        tmp[3] = TESTS[i][2];
087                        tmp[4] = TESTS[i][3];
088                        tmp[5] = TESTS[i][4];
089                        break;
090    
091                    default:
092                        throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
093                }
094    
095                data.add( tmp );
096            }
097            return data;
098        }
099    
100        /*
101         * =================================================================== Constructors
102         * ===================================================================
103         */
104        public PrimitiveNullHandlingTest( String name, Object root, String expressionString, Object expectedResult,
105                                          Object setValue, Object expectedAfterSetResult )
106        {
107            super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
108        }
109    }