001    /*
002     * $Id: ArithmeticAndLogicalOperatorsTest.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.junit.Before;
023    import org.junit.runner.RunWith;
024    import org.junit.runners.Parameterized;
025    import org.junit.runners.Parameterized.Parameters;
026    
027    import java.math.BigDecimal;
028    import java.util.ArrayList;
029    import java.util.Collection;
030    
031    @RunWith(value = Parameterized.class)
032    public class ArithmeticAndLogicalOperatorsTest
033        extends OgnlTestCase
034    {
035    
036        private static Object[][] TESTS = {
037            // Double-valued arithmetic expressions
038            { "-1d", new Double( -1 ) },
039            { "+1d", new Double( 1 ) },
040            { "--1f", new Double( 1 ) },
041            { "2*2.0", new Double( 4.0 ) },
042            { "5/2.", new Double( 2.5 ) },
043            { "5+2D", new Double( 7 ) },
044            { "5f-2F", new Double( 3.0 ) },
045            { "5.+2*3", new Double( 11 ) },
046            { "(5.+2)*3", new Double( 21 ) },
047    
048            // BigDecimal-valued arithmetic expressions
049            { "-1b", new Integer( -1 ) },
050            { "+1b", new Integer( 1 ) },
051            { "--1b", new Integer( 1 ) },
052            { "2*2.0b", new Double( 4.0 ) },
053            { "5/2.B", new Integer( 2 ) },
054            { "5.0B/2", new Double( 2.5 ) },
055            { "5+2b", new Integer( 7 ) },
056            { "5-2B", new Integer( 3 ) },
057            { "5.+2b*3", new Double( 11 ) },
058            { "(5.+2b)*3", new Double( 21 ) },
059    
060            // Integer-valued arithmetic expressions
061            { "-1", new Integer( -1 ) },
062            { "+1", new Integer( 1 ) },
063            { "--1", new Integer( 1 ) },
064            { "2*2", new Integer( 4 ) },
065            { "5/2", new Integer( 2 ) },
066            { "5+2", new Integer( 7 ) },
067            { "5-2", new Integer( 3 ) },
068            { "5+2*3", new Integer( 11 ) },
069            { "(5+2)*3", new Integer( 21 ) },
070            { "~1", new Integer( ~1 ) },
071            { "5%2", new Integer( 1 ) },
072            { "5<<2", new Integer( 20 ) },
073            { "5>>2", new Integer( 1 ) },
074            { "5>>1+1", new Integer( 1 ) },
075            { "-5>>>2", new Integer( -5 >>> 2 ) },
076            { "-5L>>>2", new Long( -5L >>> 2 ) },
077            { "5. & 3", new Long( 1 ) },
078            { "5 ^3", new Integer( 6 ) },
079            { "5l&3|5^3", new Long( 7 ) },
080            { "5&(3|5^3)", new Long( 5 ) },
081            { "true ? 1 : 1/0", new Integer( 1 ) },
082    
083            // BigInteger-valued arithmetic expressions
084            { "-1h", Integer.valueOf( -1 ) },
085            { "+1H", Integer.valueOf( 1 ) },
086            { "--1h", Integer.valueOf( 1 ) },
087            { "2h*2", Integer.valueOf( 4 ) },
088            { "5/2h", Integer.valueOf( 2 ) },
089            { "5h+2", Integer.valueOf( 7 ) },
090            { "5-2h", Integer.valueOf( 3 ) },
091            { "5+2H*3", Integer.valueOf( 11 ) },
092            { "(5+2H)*3", Integer.valueOf( 21 ) },
093            { "~1h", Integer.valueOf( ~1 ) },
094            { "5h%2", Integer.valueOf( 1 ) },
095            { "5h<<2", Integer.valueOf( 20 ) },
096            { "5h>>2", Integer.valueOf( 1 ) },
097            { "5h>>1+1", Integer.valueOf( 1 ) },
098            { "-5h>>>2", Integer.valueOf( -2 ) },
099            { "5.b & 3", new Long( 1 ) },
100            { "5h ^3", Integer.valueOf( 6 ) },
101            { "5h&3|5^3", new Long( 7 ) },
102            { "5H&(3|5^3)", new Long( 5 ) },
103    
104            // Logical expressions
105            { "!1", Boolean.FALSE }, { "!null", Boolean.TRUE },
106            { "5<2", Boolean.FALSE },
107            { "5>2", Boolean.TRUE },
108            { "5<=5", Boolean.TRUE },
109            { "5>=3", Boolean.TRUE },
110            { "5<-5>>>2", Boolean.TRUE },
111            { "5==5.0", Boolean.TRUE },
112            { "5!=5.0", Boolean.FALSE },
113            { "null in {true,false,null}", Boolean.TRUE },
114            { "null not in {true,false,null}", Boolean.FALSE },
115            { "null in {true,false,null}.toArray()", Boolean.TRUE },
116            { "5 in {true,false,null}", Boolean.FALSE },
117            { "5 not in {true,false,null}", Boolean.TRUE },
118            { "5 instanceof java.lang.Integer", Boolean.TRUE },
119            { "5. instanceof java.lang.Integer", Boolean.FALSE },
120            { "!false || true", Boolean.TRUE },
121            { "!(true && true)", Boolean.FALSE },
122            { "(1 > 0 && true) || 2 > 0", Boolean.TRUE },
123    
124            // Logical expressions (string versions)
125            { "2 or 0", Integer.valueOf( 2 ) }, { "1 and 0", Integer.valueOf( 0 ) }, { "1 bor 0", new Integer( 1 ) },
126            { "true && 12", Integer.valueOf( 12 ) }, { "1 xor 0", new Integer( 1 ) }, { "1 band 0", new Long( 0 ) },
127            { "1 eq 1", Boolean.TRUE }, { "1 neq 1", Boolean.FALSE }, { "1 lt 5", Boolean.TRUE },
128            { "1 lte 5", Boolean.TRUE }, { "1 gt 5", Boolean.FALSE }, { "1 gte 5", Boolean.FALSE },
129            { "1 lt 5", Boolean.TRUE }, { "1 shl 2", new Integer( 4 ) }, { "4 shr 2", new Integer( 1 ) },
130            { "4 ushr 2", new Integer( 1 ) }, { "not null", Boolean.TRUE }, { "not 1", Boolean.FALSE },
131    
132            { "#x > 0", Boolean.TRUE }, { "#x < 0", Boolean.FALSE }, { "#x == 0", Boolean.FALSE },
133            { "#x == 1", Boolean.TRUE }, { "0 > #x", Boolean.FALSE }, { "0 < #x", Boolean.TRUE },
134            { "0 == #x", Boolean.FALSE }, { "1 == #x", Boolean.TRUE }, { "\"1\" > 0", Boolean.TRUE },
135            { "\"1\" < 0", Boolean.FALSE }, { "\"1\" == 0", Boolean.FALSE }, { "\"1\" == 1", Boolean.TRUE },
136            { "0 > \"1\"", Boolean.FALSE }, { "0 < \"1\"", Boolean.TRUE }, { "0 == \"1\"", Boolean.FALSE },
137            { "1 == \"1\"", Boolean.TRUE }, { "#x + 1", "11" }, { "1 + #x", "11" }, { "#y == 1", Boolean.TRUE },
138            { "#y == \"1\"", Boolean.TRUE }, { "#y + \"1\"", "11" }, { "\"1\" + #y", "11" } };
139    
140        /*
141         * =================================================================== Public static methods
142         * ===================================================================
143         */
144        @Parameters
145        public static Collection<Object[]> data()
146        {
147            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
148            for ( Object[] TEST : TESTS )
149            {
150                Object[] tmp = new Object[4];
151                tmp[0] = TEST[0] + " (" + TEST[1] + ")";
152                tmp[1] = null;
153                tmp[2] = TEST[0];
154                tmp[3] = TEST[1];
155    
156                data.add( tmp );
157            }
158            return data;
159        }
160    
161        /*
162         * =================================================================== Constructors
163         * ===================================================================
164         */
165    
166        public ArithmeticAndLogicalOperatorsTest( String name, Object root, String expressionString, Object expectedResult )
167        {
168            super( name, root, expressionString, expectedResult );
169        }
170        /*
171         * =================================================================== Overridden methods
172         * ===================================================================
173         */
174        @Before
175        @Override
176        public void setUp()
177        {
178            super.setUp();
179            _context.put( "x", "1" );
180            _context.put("y", new BigDecimal(1));
181        }
182    }