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    *      https://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.internal;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.NoSuchElementException;
29  
30  import org.apache.commons.jexl3.JexlTestCase;
31  import org.junit.jupiter.api.AfterEach;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Basic checks on ranges.
37   */
38  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
39  class RangeTest extends JexlTestCase {
40  
41      public RangeTest() {
42          super("RangeTest");
43      }
44  
45      private void checkIteration(final IntegerRange ir, final int first, final int last) {
46          final Iterator<Integer> ii = ir.iterator();
47          if (ii.hasNext()) {
48              int l = ii.next();
49              assertEquals(first, l);
50              while(ii.hasNext()) {
51                  l = ii.next();
52              }
53              assertEquals(last, l);
54          } else {
55              fail("empty iterator?");
56          }
57      }
58  
59      private void checkIteration(final LongRange lr, final long first, final long last) {
60          final Iterator<Long> ii = lr.iterator();
61          if (ii.hasNext()) {
62              long l = ii.next();
63              assertEquals(first, l);
64              while(ii.hasNext()) {
65                  l = ii.next();
66              }
67              assertEquals(last, l);
68          } else {
69              fail("empty iterator?");
70          }
71      }
72  
73      @BeforeEach
74      @Override
75      public void setUp() throws Exception {
76          // ensure jul logging is only error
77          java.util.logging.Logger.getLogger(org.apache.commons.jexl3.JexlEngine.class.getName()).setLevel(java.util.logging.Level.SEVERE);
78      }
79  
80      @AfterEach
81      @Override
82      public void tearDown() throws Exception {
83          super.tearDown();
84      }
85  
86      @Test
87      void testAscIterator() {
88          final Iterator<Integer> ii = new AscIntegerIterator(3, 5);
89          Integer i = 3;
90          while (ii.hasNext()) {
91              assertEquals(i, ii.next());
92              i += 1;
93          }
94          assertThrows(NoSuchElementException.class, ii::next, "iterator exhausted");
95          assertThrows(UnsupportedOperationException.class, ii::remove, "remove not implemented");
96      }
97  
98      @Test
99      void testAscLongIterator() {
100         final Iterator<Long> ii = new AscLongIterator(3L, 5L);
101         Long i = 3L;
102         while (ii.hasNext()) {
103             assertEquals(i, ii.next());
104             i += 1;
105         }
106         assertThrows(NoSuchElementException.class, ii::next, "iterator exhausted");
107         assertThrows(UnsupportedOperationException.class, ii::remove, "remove not implemented");
108     }
109 
110     @Test void testMisc() {
111         assertEquals("?", Scope.UNDEFINED.toString());
112         assertEquals("??", Scope.UNDECLARED.toString());
113     }
114 
115     @Test
116     void testRanges() throws Exception {
117         final LongRange lr0 = LongRange.create(20,10);
118         assertEquals(10L, lr0.getMin());
119         assertEquals(20L, lr0.getMax());
120         assertFalse(lr0.isEmpty());
121         assertTrue(lr0.contains(10L));
122         assertTrue(lr0.contains(20L));
123         assertFalse(lr0.contains(30L));
124         assertFalse(lr0.contains(5L));
125         assertFalse(lr0.contains(null));
126         checkIteration(lr0, 20L, 10L);
127         final LongRange lr1 = LongRange.create(10,20);
128         checkIteration(lr1, 10L, 20L);
129         assertTrue(lr0.containsAll(lr1));
130         final LongRange lr2 = LongRange.create(10,15);
131         assertNotEquals(lr0, lr2);
132         assertTrue(lr0.containsAll(lr2));
133         assertFalse(lr2.containsAll(lr1));
134         final IntegerRange ir0 = IntegerRange.create(20,10);
135         checkIteration(ir0, 20, 10);
136         assertEquals(10, ir0.getMin());
137         assertEquals(20, ir0.getMax());
138         assertFalse(ir0.isEmpty());
139         assertTrue(ir0.contains(10));
140         assertTrue(ir0.contains(20));
141         assertFalse(ir0.contains(30));
142         assertFalse(ir0.contains(5));
143         assertFalse(ir0.contains(null));
144         final IntegerRange ir1 = IntegerRange.create(10,20);
145         checkIteration(ir1, 10, 20);
146         assertTrue(ir0.containsAll(ir1));
147         assertNotEquals(ir0, lr0);
148         assertNotEquals(ir1, lr1);
149         final IntegerRange ir2 = IntegerRange.create(10,15);
150         assertNotEquals(ir0, ir2);
151         assertTrue(ir0.containsAll(ir2));
152         assertFalse(ir2.containsAll(ir1));
153 
154         long lc0 = 20;
155         final Iterator<Long> il0 = lr0.iterator();
156         while(il0.hasNext()) {
157             final long v0 = il0.next();
158             assertEquals(lc0, v0);
159             try {
160                 switch((int)v0) {
161                     case 10:  il0.remove(); fail(); break;
162                     case 11: lr1.add(v0); fail(); break;
163                     case 12: lr1.remove(v0); fail(); break;
164                     case 13: lr1.addAll(Collections.singletonList(v0)); fail(); break;
165                     case 14: lr1.removeAll(Collections.singletonList(v0)); fail(); break;
166                     case 15: lr1.retainAll(Collections.singletonList(v0)); fail(); break;
167                 }
168             } catch (final UnsupportedOperationException xuo) {
169                 // ok
170             }
171             lc0 -= 1;
172         }
173         assertEquals(9L, lc0);
174         assertThrows(NoSuchElementException.class, il0::next);
175 
176         int ic0 = 20;
177         final Iterator<Integer> ii0 = ir0.iterator();
178         while(ii0.hasNext()) {
179             final int v0 = ii0.next();
180             assertEquals(ic0, v0);
181             try {
182                 switch(v0) {
183                     case 10: ii0.remove(); fail(); break;
184                     case 11: ir1.add(v0); fail(); break;
185                     case 12: ir1.remove(v0); fail(); break;
186                     case 13: ir1.addAll(Collections.singletonList(v0)); fail(); break;
187                     case 14: ir1.removeAll(Collections.singletonList(v0)); fail(); break;
188                     case 15: ir1.retainAll(Collections.singletonList(v0)); fail(); break;
189                 }
190             } catch (final UnsupportedOperationException xuo) {
191                 // ok
192             }
193             ic0 -= 1;
194         }
195         assertEquals(9, ic0);
196         assertThrows(NoSuchElementException.class, ii0::next);
197     }
198 
199 }
200