View Javadoc

1   /*
2    * $Id: ArithmeticAndLogicalOperatorsTest.java 1104080 2011-05-17 09:22:09Z mcucchiara $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  package org.apache.commons.ognl.test;
21  
22  import org.junit.Before;
23  import org.junit.runner.RunWith;
24  import org.junit.runners.Parameterized;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import java.math.BigDecimal;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  
31  @RunWith(value = Parameterized.class)
32  public class ArithmeticAndLogicalOperatorsTest
33      extends OgnlTestCase
34  {
35  
36      private static Object[][] TESTS = {
37          // Double-valued arithmetic expressions
38          { "-1d", new Double( -1 ) },
39          { "+1d", new Double( 1 ) },
40          { "--1f", new Double( 1 ) },
41          { "2*2.0", new Double( 4.0 ) },
42          { "5/2.", new Double( 2.5 ) },
43          { "5+2D", new Double( 7 ) },
44          { "5f-2F", new Double( 3.0 ) },
45          { "5.+2*3", new Double( 11 ) },
46          { "(5.+2)*3", new Double( 21 ) },
47  
48          // BigDecimal-valued arithmetic expressions
49          { "-1b", new Integer( -1 ) },
50          { "+1b", new Integer( 1 ) },
51          { "--1b", new Integer( 1 ) },
52          { "2*2.0b", new Double( 4.0 ) },
53          { "5/2.B", new Integer( 2 ) },
54          { "5.0B/2", new Double( 2.5 ) },
55          { "5+2b", new Integer( 7 ) },
56          { "5-2B", new Integer( 3 ) },
57          { "5.+2b*3", new Double( 11 ) },
58          { "(5.+2b)*3", new Double( 21 ) },
59  
60          // Integer-valued arithmetic expressions
61          { "-1", new Integer( -1 ) },
62          { "+1", new Integer( 1 ) },
63          { "--1", new Integer( 1 ) },
64          { "2*2", new Integer( 4 ) },
65          { "5/2", new Integer( 2 ) },
66          { "5+2", new Integer( 7 ) },
67          { "5-2", new Integer( 3 ) },
68          { "5+2*3", new Integer( 11 ) },
69          { "(5+2)*3", new Integer( 21 ) },
70          { "~1", new Integer( ~1 ) },
71          { "5%2", new Integer( 1 ) },
72          { "5<<2", new Integer( 20 ) },
73          { "5>>2", new Integer( 1 ) },
74          { "5>>1+1", new Integer( 1 ) },
75          { "-5>>>2", new Integer( -5 >>> 2 ) },
76          { "-5L>>>2", new Long( -5L >>> 2 ) },
77          { "5. & 3", new Long( 1 ) },
78          { "5 ^3", new Integer( 6 ) },
79          { "5l&3|5^3", new Long( 7 ) },
80          { "5&(3|5^3)", new Long( 5 ) },
81          { "true ? 1 : 1/0", new Integer( 1 ) },
82  
83          // BigInteger-valued arithmetic expressions
84          { "-1h", Integer.valueOf( -1 ) },
85          { "+1H", Integer.valueOf( 1 ) },
86          { "--1h", Integer.valueOf( 1 ) },
87          { "2h*2", Integer.valueOf( 4 ) },
88          { "5/2h", Integer.valueOf( 2 ) },
89          { "5h+2", Integer.valueOf( 7 ) },
90          { "5-2h", Integer.valueOf( 3 ) },
91          { "5+2H*3", Integer.valueOf( 11 ) },
92          { "(5+2H)*3", Integer.valueOf( 21 ) },
93          { "~1h", Integer.valueOf( ~1 ) },
94          { "5h%2", Integer.valueOf( 1 ) },
95          { "5h<<2", Integer.valueOf( 20 ) },
96          { "5h>>2", Integer.valueOf( 1 ) },
97          { "5h>>1+1", Integer.valueOf( 1 ) },
98          { "-5h>>>2", Integer.valueOf( -2 ) },
99          { "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 }