001    /*
002     * $Id: PropertyArithmeticAndLogicalOperatorsTest.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 org.apache.commons.ognl.test.objects.Root;
023    import org.apache.commons.ognl.test.objects.SimpleNumeric;
024    import org.apache.commons.ognl.test.objects.TestModel;
025    import org.junit.runner.RunWith;
026    import org.junit.runners.Parameterized;
027    import org.junit.runners.Parameterized.Parameters;
028    
029    import java.util.ArrayList;
030    import java.util.Arrays;
031    import java.util.Collection;
032    
033    /**
034     *
035     */
036    @RunWith(value = Parameterized.class)
037    public class PropertyArithmeticAndLogicalOperatorsTest
038        extends OgnlTestCase
039    {
040    
041        private static Root ROOT = new Root();
042    
043        private static TestModel MODEL = new TestModel();
044    
045        private static SimpleNumeric NUMERIC = new SimpleNumeric();
046    
047        private static Object[][] TESTS = {
048            { ROOT, "objectIndex > 0", Boolean.TRUE },
049            { ROOT, "false", Boolean.FALSE },
050            { ROOT, "!false || true", Boolean.TRUE },
051            { ROOT, "property.bean3.value >= 24", Boolean.TRUE },
052            { ROOT, "genericIndex-1", new Integer( 1 ) },
053            { ROOT, "((renderNavigation ? 0 : 1) + map.size) * theInt",
054                new Integer( ( ( ROOT.getRenderNavigation() ? 0 : 1 ) + ROOT.getMap().size() ) * ROOT.getTheInt() ) },
055            { ROOT, "{theInt + 1}", Arrays.asList( new Integer( ROOT.getTheInt() + 1 ) ) },
056            { MODEL, "(unassignedCopyModel.optionCount > 0 && canApproveCopy) || entry.copy.size() > 0", Boolean.TRUE },
057            { ROOT, " !(printDelivery || @Boolean@FALSE)", Boolean.FALSE },
058            { ROOT, "(getIndexedProperty('nested').size - 1) > genericIndex", Boolean.FALSE },
059            { ROOT, "(getIndexedProperty('nested').size + 1) >= genericIndex", Boolean.TRUE },
060            { ROOT, "(getIndexedProperty('nested').size + 1) == genericIndex", Boolean.TRUE },
061            { ROOT, "(getIndexedProperty('nested').size + 1) < genericIndex", Boolean.FALSE },
062            { ROOT, "map.size * genericIndex",
063                new Integer( ROOT.getMap().size() * ( (Integer) ROOT.getGenericIndex() ).intValue() ) },
064            { ROOT, "property == property", Boolean.TRUE }, { ROOT, "property.bean3.value % 2 == 0", Boolean.TRUE },
065            { ROOT, "genericIndex % 3 == 0", Boolean.FALSE },
066            { ROOT, "genericIndex % theInt == property.bean3.value", Boolean.FALSE },
067            { ROOT, "theInt / 100.0", ROOT.getTheInt() / 100.0 },
068            { ROOT, "@java.lang.Long@valueOf('100') == @java.lang.Long@valueOf('100')", Boolean.TRUE },
069            { NUMERIC, "budget - timeBilled", new Double( NUMERIC.getBudget() - NUMERIC.getTimeBilled() ) },
070            { NUMERIC, "(budget % tableSize) == 0", Boolean.TRUE } };
071    
072    
073        @Parameters
074        public static Collection<Object[]> data()
075        {
076            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
077            for ( int i = 0; i < TESTS.length; i++ )
078            {
079                Object[] tmp = new Object[6];
080                tmp[0] = TESTS[i][1];
081                tmp[1] = TESTS[i][0];
082                tmp[2] = TESTS[i][1];
083                tmp[3] = TESTS[i][2];
084    
085                if ( TESTS[i].length == 5 )
086                {
087                    tmp[4] = TESTS[i][3];
088                    tmp[5] = TESTS[i][4];
089                }
090    
091                data.add( tmp );
092            }
093            return data;
094        }
095    
096        /*
097         * =================================================================== Constructors
098         * ===================================================================
099         */
100        public PropertyArithmeticAndLogicalOperatorsTest( String name, Object root, String expressionString,
101                                                          Object expectedResult, Object setValue,
102                                                          Object expectedAfterSetResult )
103        {
104            super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
105        }
106    }