001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.el.tree.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020:
021: import junit.framework.TestCase;
022:
023: import de.odysseus.el.tree.impl.Scanner.*;
024:
025: import static de.odysseus.el.tree.impl.Scanner.Symbol.*;
026:
027: public class ScannerTest extends TestCase {
028: void assertEquals(Object[] a1, Object[] a2) {
029: assertTrue(Arrays.equals(a1, a2));
030: }
031:
032: Scanner.Symbol[] symbols(String expression) throws ScanException {
033: ArrayList<Symbol> list = new ArrayList<Symbol>();
034: Scanner scanner = new Scanner(expression);
035: Symbol symbol = scanner.next();
036: while (symbol != Symbol.EOF) {
037: list.add(symbol);
038: symbol = scanner.next();
039: }
040: return list.toArray(new Symbol[list.size()]);
041: }
042:
043: public void testInteger() throws ScanException {
044: Symbol[] expected = { START_EVAL_DYNAMIC, INTEGER, END_EVAL };
045:
046: assertEquals(expected, symbols("${0}"));
047: assertEquals(expected, symbols("${1}"));
048: assertEquals(expected, symbols("${01234567890}"));
049: }
050:
051: public void testFloat() throws ScanException {
052: Symbol[] expected = { START_EVAL_DYNAMIC, FLOAT, END_EVAL };
053:
054: assertEquals(expected, symbols("${0.}"));
055: assertEquals(expected, symbols("${023456789.}"));
056: assertEquals(expected, symbols("${.0}"));
057: assertEquals(expected, symbols("${.023456789}"));
058: assertEquals(expected, symbols("${0.0}"));
059:
060: assertEquals(expected, symbols("${0e0}"));
061: assertEquals(expected, symbols("${0E0}"));
062: assertEquals(expected, symbols("${0e+0}"));
063: assertEquals(expected, symbols("${0E+0}"));
064: assertEquals(expected, symbols("${0e+0}"));
065: assertEquals(expected, symbols("${0E+0}"));
066:
067: assertEquals(expected, symbols("${.0e0}"));
068: assertEquals(expected, symbols("${.0E0}"));
069: assertEquals(expected, symbols("${.0e+0}"));
070: assertEquals(expected, symbols("${.0E+0}"));
071: assertEquals(expected, symbols("${.0e-0}"));
072: assertEquals(expected, symbols("${.0E-0}"));
073:
074: assertEquals(expected, symbols("${0.e0}"));
075: assertEquals(expected, symbols("${0.E0}"));
076: assertEquals(expected, symbols("${0.e+0}"));
077: assertEquals(expected, symbols("${0.E+0}"));
078: assertEquals(expected, symbols("${0.e-0}"));
079: assertEquals(expected, symbols("${0.E-0}"));
080:
081: assertEquals(expected, symbols("${0.0e0}"));
082: assertEquals(expected, symbols("${0.0E0}"));
083: assertEquals(expected, symbols("${0.0e+0}"));
084: assertEquals(expected, symbols("${0.0E+0}"));
085: assertEquals(expected, symbols("${0.0e-0}"));
086: assertEquals(expected, symbols("${0.0E-0}"));
087: }
088:
089: public void testString() throws ScanException {
090: Symbol[] expected = { START_EVAL_DYNAMIC, STRING, END_EVAL };
091:
092: assertEquals(expected, symbols("${'foo'}"));
093: assertEquals(expected, symbols("${'f\"o'}"));
094: assertEquals(expected, symbols("${'f\\'o'}"));
095: try {
096: symbols("${'f\\oo'}");
097: fail();
098: } catch (ScanException e) {
099: }
100: try {
101: symbols("${'f\\\"o'}");
102: fail();
103: } catch (ScanException e) {
104: }
105: try {
106: symbols("${'foo");
107: fail();
108: } catch (ScanException e) {
109: }
110:
111: assertEquals(expected, symbols("${\"foo\"}"));
112: assertEquals(expected, symbols("${\"f\\\"o\"}"));
113: assertEquals(expected, symbols("${\"f'o\"}"));
114: try {
115: symbols("${\"f\\oo\"}");
116: fail();
117: } catch (ScanException e) {
118: }
119: try {
120: symbols("${\"f\\'o\"}");
121: fail();
122: } catch (ScanException e) {
123: }
124: try {
125: symbols("${\"foo");
126: fail();
127: } catch (ScanException e) {
128: }
129: }
130:
131: public void testKeywords() throws ScanException {
132: assertEquals(
133: new Symbol[] { START_EVAL_DYNAMIC, NULL, END_EVAL },
134: symbols("${null}"));
135: assertEquals(
136: new Symbol[] { START_EVAL_DYNAMIC, TRUE, END_EVAL },
137: symbols("${true}"));
138: assertEquals(
139: new Symbol[] { START_EVAL_DYNAMIC, FALSE, END_EVAL },
140: symbols("${false}"));
141: assertEquals(
142: new Symbol[] { START_EVAL_DYNAMIC, EMPTY, END_EVAL },
143: symbols("${empty}"));
144: assertEquals(
145: new Symbol[] { START_EVAL_DYNAMIC, DIV, END_EVAL },
146: symbols("${div}"));
147: assertEquals(
148: new Symbol[] { START_EVAL_DYNAMIC, MOD, END_EVAL },
149: symbols("${mod}"));
150: assertEquals(
151: new Symbol[] { START_EVAL_DYNAMIC, NOT, END_EVAL },
152: symbols("${not}"));
153: assertEquals(
154: new Symbol[] { START_EVAL_DYNAMIC, AND, END_EVAL },
155: symbols("${and}"));
156: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, OR, END_EVAL },
157: symbols("${or}"));
158: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LE, END_EVAL },
159: symbols("${le}"));
160: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LT, END_EVAL },
161: symbols("${lt}"));
162: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, EQ, END_EVAL },
163: symbols("${eq}"));
164: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, NE, END_EVAL },
165: symbols("${ne}"));
166: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, GE, END_EVAL },
167: symbols("${ge}"));
168: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, GT, END_EVAL },
169: symbols("${gt}"));
170: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, INSTANCEOF,
171: END_EVAL }, symbols("${instanceof}"));
172:
173: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, IDENTIFIER,
174: END_EVAL }, symbols("${xnull}"));
175: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, IDENTIFIER,
176: END_EVAL }, symbols("${nullx}"));
177: }
178:
179: public void testOperators() throws ScanException {
180: assertEquals(
181: new Symbol[] { START_EVAL_DYNAMIC, MUL, END_EVAL },
182: symbols("${*}"));
183: assertEquals(
184: new Symbol[] { START_EVAL_DYNAMIC, DIV, END_EVAL },
185: symbols("${/}"));
186: assertEquals(
187: new Symbol[] { START_EVAL_DYNAMIC, MOD, END_EVAL },
188: symbols("${%}"));
189: assertEquals(
190: new Symbol[] { START_EVAL_DYNAMIC, PLUS, END_EVAL },
191: symbols("${+}"));
192: assertEquals(
193: new Symbol[] { START_EVAL_DYNAMIC, MINUS, END_EVAL },
194: symbols("${-}"));
195: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, QUESTION,
196: END_EVAL }, symbols("${?}"));
197: assertEquals(
198: new Symbol[] { START_EVAL_DYNAMIC, COLON, END_EVAL },
199: symbols("${:}"));
200: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LBRACK,
201: END_EVAL }, symbols("${[}"));
202: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, RBRACK,
203: END_EVAL }, symbols("${]}"));
204: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LPAREN,
205: END_EVAL }, symbols("${(}"));
206: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, RPAREN,
207: END_EVAL }, symbols("${)}"));
208: assertEquals(
209: new Symbol[] { START_EVAL_DYNAMIC, COMMA, END_EVAL },
210: symbols("${,}"));
211: assertEquals(
212: new Symbol[] { START_EVAL_DYNAMIC, DOT, END_EVAL },
213: symbols("${.}"));
214: assertEquals(
215: new Symbol[] { START_EVAL_DYNAMIC, AND, END_EVAL },
216: symbols("${&&}"));
217: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, OR, END_EVAL },
218: symbols("${||}"));
219: assertEquals(
220: new Symbol[] { START_EVAL_DYNAMIC, NOT, END_EVAL },
221: symbols("${!}"));
222: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LE, END_EVAL },
223: symbols("${<=}"));
224: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, LT, END_EVAL },
225: symbols("${<}"));
226: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, EQ, END_EVAL },
227: symbols("${==}"));
228: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, NE, END_EVAL },
229: symbols("${!=}"));
230: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, GE, END_EVAL },
231: symbols("${>=}"));
232: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, GT, END_EVAL },
233: symbols("${>}"));
234:
235: try {
236: symbols("${&)");
237: fail();
238: } catch (ScanException e) {
239: }
240: try {
241: symbols("${|)");
242: fail();
243: } catch (ScanException e) {
244: }
245: try {
246: symbols("${=)");
247: fail();
248: } catch (ScanException e) {
249: }
250: }
251:
252: public void testWhitespace() throws ScanException {
253: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, END_EVAL },
254: symbols("${\t\n\r }"));
255: }
256:
257: public void testIdentifier() throws ScanException {
258: Symbol[] expected = { START_EVAL_DYNAMIC, IDENTIFIER, END_EVAL };
259:
260: assertEquals(expected, symbols("${foo}"));
261: assertEquals(expected, symbols("${foo_1}"));
262: }
263:
264: public void testText() throws ScanException {
265: Symbol[] expected = { TEXT };
266:
267: assertEquals(expected, symbols("foo"));
268: assertEquals(expected, symbols("foo\\"));
269: assertEquals(expected, symbols("foo\\$"));
270: assertEquals(expected, symbols("foo\\#"));
271: assertEquals(expected, symbols("foo\\${"));
272: assertEquals(expected, symbols("foo\\#{"));
273: assertEquals(expected, symbols("\\${foo}"));
274: assertEquals(expected, symbols("\\${foo}"));
275: }
276:
277: public void testMixed() throws ScanException {
278: assertEquals(new Symbol[] { TEXT, START_EVAL_DYNAMIC },
279: symbols("foo${"));
280: assertEquals(new Symbol[] { START_EVAL_DYNAMIC, IDENTIFIER },
281: symbols("${bar"));
282: assertEquals(
283: new Symbol[] { START_EVAL_DYNAMIC, END_EVAL, TEXT },
284: symbols("${}bar"));
285: assertEquals(new Symbol[] { TEXT, START_EVAL_DYNAMIC, END_EVAL,
286: TEXT }, symbols("foo${}bar"));
287: }
288:
289: public void testDeferred() throws ScanException {
290: assertEquals(new Symbol[] { START_EVAL_DEFERRED },
291: symbols("#{"));
292: }
293: }
|