01: package org.geotools.data.sql.view;
02:
03: import java.io.IOException;
04: import java.util.List;
05:
06: import junit.framework.TestCase;
07: import net.sf.jsqlparser.expression.Expression;
08: import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
09: import net.sf.jsqlparser.schema.Table;
10: import net.sf.jsqlparser.statement.select.Join;
11: import net.sf.jsqlparser.statement.select.PlainSelect;
12:
13: import org.geotools.data.sql.SqlParser;
14:
15: public class SqlParserTest extends TestCase {
16:
17: protected void setUp() throws Exception {
18: super .setUp();
19: }
20:
21: protected void tearDown() throws Exception {
22: super .tearDown();
23: }
24:
25: public void testIllegalSelect() {
26: String wrongStatement = "SELECT table SET val1 = 1";
27: try {
28: SqlParser.parse(wrongStatement);
29: fail("No select statement should have been thrown an exception");
30: } catch (IOException e) {
31: //ok
32: }
33: }
34:
35: public void testNoSelect() {
36: String unsupportedStatement = "UPDATE table SET val1 = 1";
37: try {
38: SqlParser.parse(unsupportedStatement);
39: fail("No select statement should have been thrown an exception");
40: } catch (IOException e) {
41: //ok
42: }
43: }
44:
45: public void testSimpleSelect() throws IOException {
46: String statement = "SELECT * FROM table1 INNER JOIN table2 on table1.id = table2.parent";
47: PlainSelect select = (PlainSelect) SqlParser.parse(statement);
48:
49: assertNotNull(select);
50:
51: //I'll put this null checking only here as to demostrate
52: //the elements of a PlainSelect
53: assertNull(select.getDistinct());
54: assertNull(select.getGroupByColumnReferences());
55: assertNull(select.getHaving());
56: assertNull(select.getInto());
57: assertNull(select.getLimit());
58: assertNull(select.getOrderByElements());
59: assertNull(select.getTop());
60: assertNull(select.getWhere());
61:
62: assertNotNull(select.getFromItems());
63: assertNotNull(select.getSelectItems());
64:
65: List fromItems = select.getFromItems();
66: assertEquals(1, fromItems.size());
67: assertTrue(fromItems.get(0) instanceof Table);
68:
69: assertNotNull(select.getJoins());
70: Join join = (Join) select.getJoins().get(0);
71: assertFalse(join.isOuter());
72: assertNull(join.getRightItem().getAlias());
73: Expression joinExpr = join.getOnExpression();
74: assertTrue(joinExpr instanceof EqualsTo);
75: }
76: }
|