001: /*
002: * SQLLexerTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.formatter;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016: import junit.framework.TestCase;
017:
018: /**
019: *
020: * @author support@sql-workbench.net
021: */
022: public class SQLLexerTest extends TestCase {
023:
024: public SQLLexerTest(String testName) {
025: super (testName);
026: }
027:
028: private List getTokenList(String sql) {
029: ArrayList result = new ArrayList();
030:
031: SQLLexer l = new SQLLexer(sql);
032: SQLToken t = l.getNextToken(false, false);
033: while (t != null) {
034: result.add(t);
035: t = l.getNextToken(false, false);
036: }
037: return result;
038: }
039:
040: public void testQuotedIdentifier() {
041: try {
042: String sql = "Select \"one AND two\" from thetable;";
043: SQLLexer l = new SQLLexer(sql);
044: SQLToken select = l.getNextToken(false, false);
045:
046: assertEquals(select.getContents(), "SELECT");
047: SQLToken col = l.getNextToken(false, false);
048: assertEquals("\"one AND two\"", col.getContents());
049:
050: sql = "WbExport -file=\"c:\\Documents and Settings\\test.txt\" -type=text";
051: l = new SQLLexer(sql);
052: SQLToken t = l.getNextToken(false, false);
053: assertEquals("WBEXPORT", t.getText().toUpperCase());
054: t = l.getNextToken(false, false);
055: assertEquals("-", t.getText());
056: t = l.getNextToken(false, false);
057: assertEquals("file", t.getText());
058: t = l.getNextToken(false, false);
059: assertEquals("=", t.getText());
060: t = l.getNextToken(false, false);
061: assertEquals("\"c:\\Documents and Settings\\test.txt\"", t
062: .getContents());
063: } catch (Exception e) {
064: e.printStackTrace();
065: fail(e.getMessage());
066: }
067: }
068:
069: public void testLexer() {
070: // Test if the multi-word keywords are detected properly
071:
072: String sql = "-- create a view\nCREATE\nOR\nREPLACE view my_view as (SELECT * FROM bal);";
073:
074: List tokens = getTokenList(sql);
075:
076: SQLToken t = (SQLToken) tokens.get(0);
077:
078: assertEquals("CREATE OR REPLACE", t.getContents());
079: assertEquals(true, t.isReservedWord());
080:
081: sql = "SELECT * FROM bla INNER JOIN blub ON (x = y)";
082:
083: tokens = getTokenList(sql);
084: t = (SQLToken) tokens.get(0);
085: assertEquals("SELECT", t.getContents());
086:
087: t = (SQLToken) tokens.get(4);
088: assertEquals("INNER JOIN", t.getContents());
089: assertEquals(true, t.isReservedWord());
090:
091: sql = "SELECT * FROM bla INNER JOIN blub ON (x = y)\nOUTER JOIN blub2 on (y = y)";
092:
093: tokens = getTokenList(sql);
094: t = (SQLToken) tokens.get(0);
095: assertEquals("SELECT", t.getContents());
096:
097: t = (SQLToken) tokens.get(4);
098: assertEquals("INNER JOIN", t.getContents());
099: assertEquals(true, t.isReservedWord());
100:
101: t = (SQLToken) tokens.get(12);
102: assertEquals("OUTER JOIN", t.getContents());
103: assertEquals(true, t.isReservedWord());
104:
105: sql = "is null\nnot null\n--next\nunion\nall\n--next\n"
106: + "group by\n" + "order\tby\n"
107: + "create or replace package body \n"
108: + "materialized view\n" + "start with\n"
109: + "outer \t join\n"
110: + "cross join full join \t full\touter join\n"
111: + "inner join\n" + "left join\n"
112: + "left outer join\n" + "right join\n"
113: + "right \nouter\n\n join\njoin\n" + "is not null";
114: tokens = getTokenList(sql);
115: for (int i = 0; i < tokens.size(); i++) {
116: t = (SQLToken) tokens.get(i);
117: String v = t.getContents();
118: //System.out.println(i + ": " + v);
119: switch (i) {
120: case 0:
121: assertEquals("IS NULL", v);
122: break;
123: case 1:
124: assertEquals("NOT NULL", v);
125: break;
126: case 2:
127: assertEquals("UNION ALL", v);
128: break;
129: case 3:
130: assertEquals("GROUP BY", v);
131: break;
132: case 4:
133: assertEquals("ORDER BY", v);
134: break;
135: case 5:
136: assertEquals("CREATE OR REPLACE", v);
137: break;
138: case 6:
139: assertEquals("PACKAGE BODY", v);
140: break;
141: case 7:
142: assertEquals("MATERIALIZED VIEW", v);
143: break;
144: case 8:
145: assertEquals("START WITH", v);
146: break;
147: case 9:
148: assertEquals("OUTER JOIN", v);
149: break;
150: case 10:
151: assertEquals("CROSS JOIN", v);
152: break;
153: case 11:
154: assertEquals("FULL JOIN", v);
155: break;
156: case 12:
157: assertEquals("FULL OUTER JOIN", v);
158: break;
159: case 13:
160: assertEquals("INNER JOIN", v);
161: break;
162: case 14:
163: assertEquals("LEFT JOIN", v);
164: break;
165: case 15:
166: assertEquals("LEFT OUTER JOIN", v);
167: break;
168: case 16:
169: assertEquals("RIGHT JOIN", v);
170: break;
171: case 17:
172: assertEquals("RIGHT OUTER JOIN", v);
173: break;
174: case 18:
175: assertEquals("JOIN", v);
176: break;
177: case 19:
178: assertEquals("IS NOT NULL", v);
179: break;
180: }
181:
182: }
183: }
184:
185: }
|