View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3;
18  
19  import java.util.Collection;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  /**
24   * Tests for ranges.
25   * @since 3.0
26   */
27  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
28  public class RangeTest extends JexlTestCase {
29  
30      public RangeTest() {
31          super("RangeTest");
32      }
33  
34      @Test
35      public void testIntegerRangeOne() throws Exception {
36          final JexlExpression e = JEXL.createExpression("(1..1)");
37          final JexlContext jc = new MapContext();
38  
39          final Object o = e.evaluate(jc);
40          Assert.assertTrue(o instanceof Collection<?>);
41          final Collection<?> c = (Collection<?>) o;
42          Assert.assertEquals(1, c.size());
43          final Object[] a = c.toArray();
44          Assert.assertEquals(1, a.length);
45          Assert.assertEquals(1, ((Number) a[0]).intValue());
46          Assert.assertFalse((Boolean) JEXL.createScript("empty x", "x").execute(null, e));
47      }
48  
49      @Test
50      public void testIntegerRange() throws Exception {
51          final JexlExpression e = JEXL.createExpression("(1..32)");
52          final JexlContext jc = new MapContext();
53  
54          final Object o0 = e.evaluate(jc);
55          final Object o = e.evaluate(jc);
56          Assert.assertTrue(o instanceof Collection<?>);
57          final Collection<?> c = (Collection<?>) o;
58          Assert.assertEquals(32, c.size());
59  
60          Assert.assertNotSame(o0, o);
61          Assert.assertEquals(o0.hashCode(), o.hashCode());
62          Assert.assertEquals(o0, o);
63  
64          int i = 0;
65          for (final Object v : c) {
66              i += 1;
67              Assert.assertEquals(i, ((Number) v).intValue());
68          }
69          Assert.assertEquals(32, i);
70  
71          Integer[] aa = c.<Integer>toArray(new Integer[32]);
72          Assert.assertEquals(32, aa.length);
73          for (int l = 0; l < 32; ++l) {
74              Assert.assertEquals((int) aa[l], l + 1);
75          }
76  
77          aa = c.<Integer>toArray(new Integer[2]);
78          Assert.assertEquals(32, aa.length);
79          for (int l = 0; l < 32; ++l) {
80              Assert.assertEquals((int) aa[l], l + 1);
81          }
82  
83          aa = c.<Integer>toArray(new Integer[34]);
84          Assert.assertEquals(34, aa.length);
85          for (int l = 0; l < 32; ++l) {
86              Assert.assertEquals((int) aa[l], l + 1);
87          }
88  
89          final Object[] oaa = c.toArray();
90          Assert.assertEquals(32, oaa.length);
91          for (int l = 0; l < 32; ++l) {
92              Assert.assertEquals(oaa[l], l + 1);
93          }
94      }
95  
96      @Test
97      public void testLongRange() throws Exception {
98          final JexlExpression e = JEXL.createExpression("(6789000001L..6789000032L)");
99          final JexlContext jc = new MapContext();
100 
101         final Object o0 = e.evaluate(jc);
102         final Object o = e.evaluate(jc);
103         Assert.assertTrue(o instanceof Collection<?>);
104         final Collection<?> c = (Collection<?>) o;
105         Assert.assertEquals(32, c.size());
106         Assert.assertFalse((Boolean) JEXL.createScript("empty x", "x").execute(null, e));
107 
108         Assert.assertNotSame(o0, o);
109         Assert.assertEquals(o0.hashCode(), o.hashCode());
110         Assert.assertEquals(o0, o);
111 
112         long i = 6789000000L;
113         for (final Object v : c) {
114             i += 1;
115             Assert.assertEquals(i, ((Number) v).longValue());
116         }
117         Assert.assertEquals(6789000032L, i);
118 
119         Long[] aa = c.<Long>toArray(new Long[32]);
120         Assert.assertEquals(32, aa.length);
121         for (int l = 0; l < 32; ++l) {
122             Assert.assertEquals((long) aa[l], 6789000001L + l);
123         }
124 
125         aa = c.<Long>toArray(new Long[2]);
126         Assert.assertEquals(32, aa.length);
127         for (int l = 0; l < 32; ++l) {
128             Assert.assertEquals((long) aa[l], 6789000001L + l);
129         }
130 
131         aa = c.<Long>toArray(new Long[34]);
132         Assert.assertEquals(34, aa.length);
133         for (int l = 0; l < 32; ++l) {
134             Assert.assertEquals((long) aa[l], 6789000001L + l);
135         }
136 
137         final Object[] oaa = c.toArray();
138         Assert.assertEquals(32, oaa.length);
139         for (int l = 0; l < 32; ++l) {
140             Assert.assertEquals(oaa[l], 6789000001L + l);
141         }
142     }
143 
144     @Test
145     public void testIntegerSum() throws Exception {
146         final JexlScript e = JEXL.createScript("var s = 0; for(var i : (1..5)) { s = s + i; }; s");
147         final JexlContext jc = new MapContext();
148 
149         final Object o = e.execute(jc);
150         Assert.assertEquals(15, ((Number) o).intValue());
151     }
152 
153     @Test
154     public void testIntegerContains() throws Exception {
155         final JexlScript e = JEXL.createScript("(x)->{ x =~ (1..10) }");
156         final JexlContext jc = new MapContext();
157 
158         Object o = e.execute(jc, 5);
159         Assert.assertEquals(Boolean.TRUE, o);
160         o = e.execute(jc, 0);
161         Assert.assertEquals(Boolean.FALSE, o);
162         o = e.execute(jc, 100);
163         Assert.assertEquals(Boolean.FALSE, o);
164     }
165 
166     @Test
167     public void testLongSum() throws Exception {
168         final JexlScript e = JEXL.createScript("var s = 0; for(var i : (6789000001L..6789000001L)) { s = s + i; }; s");
169         final JexlContext jc = new MapContext();
170 
171         final Object o = e.execute(jc);
172         Assert.assertEquals(6789000001L, ((Number) o).longValue());
173     }
174 
175     @Test
176     public void testLongContains() throws Exception {
177         final JexlScript e = JEXL.createScript("(x)->{ x =~ (90000000001L..90000000010L) }");
178         final JexlContext jc = new MapContext();
179 
180         Object o = e.execute(jc, 90000000005L);
181         Assert.assertEquals(Boolean.TRUE, o);
182         o = e.execute(jc, 0);
183         Assert.assertEquals(Boolean.FALSE, o);
184         o = e.execute(jc, 90000000011L);
185         Assert.assertEquals(Boolean.FALSE, o);
186     }
187 }