View Javadoc
1   package org.synchronoss.cpo.jdbc.parser;
2   
3   /*-
4    * [[
5    * jdbc
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import static org.testng.Assert.assertTrue;
26  import static org.testng.Assert.fail;
27  
28  import java.text.ParseException;
29  import java.util.List;
30  import org.synchronoss.cpo.core.parser.BoundExpressionParser;
31  import org.testng.annotations.Test;
32  
33  // TODO - add more units for single quotes, double quotes, inner selects, etc
34  public class BoundExpressionParserTest {
35  
36    @Test
37    public void testSelect() {
38      try {
39        String query = "select * from table where a = ? and b = ? and c = ? and d = '0'";
40        BoundExpressionParser parser = new BoundExpressionParser();
41        parser.setExpression(query);
42        List<String> colList = parser.parse();
43  
44        assertTrue(colList.size() == 3);
45        assertTrue(colList.get(0).equals("A"));
46        assertTrue(colList.get(1).equals("B"));
47        assertTrue(colList.get(2).equals("C"));
48  
49      } catch (ParseException ex) {
50        fail(ex.getMessage());
51      }
52    }
53  
54    @Test
55    public void testSelectWithFunction() {
56      try {
57        String query = "select * from table where a = ? and UPPER(b) = ? and c = ? and d = '0'";
58        BoundExpressionParser parser = new BoundExpressionParser();
59        parser.setExpression(query);
60        List<String> colList = parser.parse();
61  
62        assertTrue(colList.size() == 3);
63        assertTrue(colList.get(0).equals("A"));
64        assertTrue(colList.get(1).equals("B"));
65        assertTrue(colList.get(2).equals("C"));
66  
67      } catch (ParseException ex) {
68        fail(ex.getMessage());
69      }
70    }
71  
72    @Test
73    public void testInsert() {
74      try {
75        String query = "insert into table(a, b, c, d) values(?, ?, ?, SYSDATE)";
76        BoundExpressionParser parser = new BoundExpressionParser();
77        parser.setExpression(query);
78        List<String> colList = parser.parse();
79  
80        assertTrue(colList.size() == 3);
81        assertTrue(colList.get(0).equals("A"));
82        assertTrue(colList.get(1).equals("B"));
83        assertTrue(colList.get(2).equals("C"));
84  
85      } catch (ParseException ex) {
86        fail(ex.getMessage());
87      }
88    }
89  }