PageRenderTime 30ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/test/java/com/hazelcast/query/ParserTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 170 lines | 130 code | 25 blank | 15 comment | 2 complexity | 323ea2fe45d8a4cd7511c7eed4736e9d MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.query;
 18
 19import org.junit.Test;
 20import org.junit.runner.RunWith;
 21
 22import java.security.SecureRandom;
 23import java.util.Arrays;
 24import java.util.List;
 25import java.util.Random;
 26
 27import static junit.framework.Assert.*;
 28
 29@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 30public class ParserTest {
 31    Parser parser = new Parser();
 32
 33    @Test
 34    public void parseEmpty() {
 35        List<String> l = parser.toPrefix("");
 36        assertEquals(Arrays.asList(), l);
 37    }
 38
 39    @Test
 40    public void parseAEqB() {
 41        String s = "a = b";
 42        List<String> list = parser.toPrefix(s);
 43        assertEquals(Arrays.asList("a", "b", "="), list);
 44    }
 45
 46    @Test
 47    public void parseAeqBandXgrtY() {
 48        assertTrue(parser.hasHigherPrecedence("=", "AND"));
 49        assertFalse(parser.hasHigherPrecedence("=", ">"));
 50        List<String> list = parser.toPrefix("a = b AND x > y");
 51        assertEquals(Arrays.asList("a", "b", "=", "x", "y", ">", "AND"), list);
 52    }
 53
 54    @Test
 55    public void parseAeqBandOpenBsmlCorDgtEclose() {
 56        String s = "A = B AND ( B < C OR D > E )";
 57        List<String> list = parser.toPrefix(s);
 58        assertEquals(Arrays.asList("A", "B", "=", "B", "C", "<", "D", "E", ">", "OR", "AND"), list);
 59    }
 60
 61    @Test
 62    public void testComplexStatement() {
 63        String s = "age > 5 AND ( ( ( active = true ) AND ( age = 23 ) ) OR age > 40 ) AND ( salary > 10 ) OR age = 10";
 64        List<String> list = parser.toPrefix(s);
 65        assertEquals(Arrays.asList("age", "5", ">", "active", "true", "=", "age", "23", "=", "AND", "age", "40", ">", "OR", "AND", "salary", "10", ">", "AND", "age", "10", "=", "OR"), list);
 66    }
 67
 68    @Test
 69    public void testTwoInnerParanthesis() {
 70        String s = "a and b AND ( ( ( a > c AND b > d ) OR ( x = y ) ) ) OR t > u";
 71        List<String> list = parser.toPrefix(s);
 72        assertEquals(Arrays.asList("a", "b", "and", "a", "c", ">", "b", "d", ">", "AND", "x", "y", "=", "OR", "AND", "t", "u", ">", "OR"), list);
 73    }
 74
 75    @Test
 76    public void testBetweenAnd() {
 77        String s = "a and b between 10 and 15";
 78        List<String> list = parser.toPrefix(s);
 79        assertEquals(Arrays.asList("a", "b", "10", "15", "between", "and"), list);
 80    }
 81
 82    @Test
 83    public void testBetween() {
 84        String s = "b between 10 and 15";
 85        List<String> list = parser.toPrefix(s);
 86        assertEquals(Arrays.asList("b", "10", "15", "between"), list);
 87    }
 88
 89    @Test
 90    public void testIn() {
 91        String s = "a and b OR c in ( 4, 5, 6 )";
 92        List<String> list = parser.toPrefix(s);
 93        assertEquals(Arrays.asList("a", "b", "and", "c", "4,5,6", "in", "OR"), list);
 94    }
 95
 96    @Test
 97    public void testNot() {
 98        String s = "a and not(b) OR c not in ( 4, 5, 6 )";
 99        List<String> list = parser.toPrefix(s);
100        assertEquals(Arrays.asList("a", "b", "not", "and", "c", "4,5,6", "in", "not", "OR"), list);
101    }
102
103    @Test
104    public void testNotEqual() {
105        String s = "b != 30";
106        List<String> list = parser.toPrefix(s);
107        assertEquals(Arrays.asList("b", "30", "!="), list);
108    }
109
110    @Test
111    public void split1() {
112        List<String> tokens = parser.split("a and b");
113        assertEquals(Arrays.asList("a", "and", "b"), tokens);
114    }
115
116    @Test
117    public void split2() {
118        List<String> tokens = parser.split("(a and b)");
119        assertEquals(Arrays.asList("(", "a", "and", "b", ")"), tokens);
120    }
121
122    @Test
123    public void split3() {
124        List<String> tokens = parser.split("((a and b))");
125        assertEquals(Arrays.asList("(", "(", "a", "and", "b", ")", ")"), tokens);
126    }
127
128    @Test
129    public void split4() {
130        List<String> tokens = parser.split("a and b AND(((a>c AND b> d) OR (x = y )) ) OR t>u");
131        assertEquals(Arrays.asList("a", "and", "b", "AND", "(", "(", "(", "a", ">", "c", "AND", "b", ">", "d", ")", "OR", "(", "x", "=", "y", ")", ")", ")", "OR", "t", ">", "u"), tokens);
132    }
133
134    @Test
135    public void split5() {
136        List<String> tokens = parser.split("a and b AND(((a>=c AND b> d) OR (x <> y )) ) OR t>u");
137        assertEquals(Arrays.asList("a", "and", "b", "AND", "(", "(", "(", "a", ">=", "c", "AND", "b", ">", "d", ")", "OR", "(", "x", "<>", "y", ")", ")", ")", "OR", "t", ">", "u"), tokens);
138    }
139
140    @Test
141    public void testComplexStatementWithGreaterAndEqueals() {
142        String s = "age>=5 AND ((( active = true ) AND (age = 23 )) OR age > 40) AND( salary>10 ) OR age=10";
143        List<String> list = parser.toPrefix(s);
144        assertEquals(Arrays.asList("age", "5", ">=", "active", "true", "=", "age", "23", "=", "AND", "age", "40", ">", "OR", "AND", "salary", "10", ">", "AND", "age", "10", "=", "OR"), list);
145    }
146
147    @Test(expected = NullPointerException.class)
148    public void parserShouldNotAcceptNull() {
149        parser.toPrefix(null);
150        fail();
151    }
152
153    @Test
154    public void parserShouldThrowOnInvalidInput() {
155        parser.toPrefix(")");
156    }
157
158    @Test
159    public void shouldNotThrowOnRandomInput() {
160        Random random = new SecureRandom();
161        StringBuilder stringBuilder = new StringBuilder();
162        for (int i = 0; i < 1000; i++) {
163            stringBuilder.setLength(0);
164            for (int n = 0; n < 1000; n++) {
165                stringBuilder.append((char) (random.nextInt() & 0xFFFF));
166            }
167            parser.toPrefix(stringBuilder.toString());
168        }
169    }
170}