/* * Reference ETL Parser for Java * Copyright (c) 2000-2009 Constantine A Plotnikov * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package net.sf.etl.tests.lexer; import java.io.StringReader; import java.net.URL; import junit.framework.TestCase; import net.sf.etl.parsers.Lexer; import net.sf.etl.parsers.LexerFactory; import net.sf.etl.parsers.LexerStates; import net.sf.etl.parsers.TextPos; import net.sf.etl.parsers.Token; import net.sf.etl.parsers.TokenKey; import net.sf.etl.parsers.Tokens; import org.xml.sax.InputSource; /** * A base class for lexer test cases * * @author const */ public abstract class LexerTestCase extends TestCase { /** * active lexer */ Lexer lexer; /** * a constructor * * @param name * name of test */ public LexerTestCase(String name) { super(name); } /** * start parsing resource * * @param resourceName * name of resource */ protected void startWithResource(String resourceName) { final URL in = this.getClass().getResource(resourceName); assertNotNull(in); final LexerFactory f = LexerFactory.newInstance(); lexer = f.newLexer(); lexer.parse(new InputSource(in.toString())); } /** * star parsing text * * @param text * text to parse */ protected void start(String text) { final LexerFactory f = LexerFactory.newInstance(); lexer = f.newLexer(); final InputSource input = new InputSource(new StringReader(text)); input.setSystemId("local.etl"); lexer.parse(input); } /** * star parsing text * * @param text * text to parse * @param start * a start position in stream * @param state * initial state of parser */ protected void start(String text, TextPos start, LexerStates state) { final LexerFactory f = LexerFactory.newInstance(); lexer = f.newLexer(); lexer.reparse("local.etl", new StringReader(text), start, state); } /** * end parsing */ protected void endLexing() { lexer.close(); } /** * read token of the specified kind * * @param kind * kind of the token */ protected void read(Tokens kind) { lexer.advance(); final Token tk = lexer.current(); assertEquals("token kind check: " + tk, kind, tk.kind()); } /** * read token with specified kind and text * * @param kind * kind of the token * @param text * text of the token */ protected void read(Tokens kind, String text) { lexer.advance(); final Token tk = lexer.current(); assertEquals("token text check: " + tk, text, tk.text()); assertEquals("token kind check: " + tk, kind, tk.kind()); } /** * read token with specified kind and text * * @param key * the key of the token * @param text * the text of the token */ protected void read(TokenKey key, String text) { lexer.advance(); final Token tk = lexer.current(); assertEquals("token text check: " + tk, text, tk.text()); assertEquals("token key check: " + tk, key, tk.key()); } /** * read token with specified kind and text * * @param kind * kind of the token * @param text * text of the token * @param suffix * suffix of token */ protected void readNumberWithSuffix(Tokens kind, String text, String suffix) { lexer.advance(); final Token tk = lexer.current(); assertEquals("token text check: " + tk, text, tk.text()); assertEquals("token kind check: " + tk, kind, tk.kind()); assertEquals("token suffix check: " + tk, suffix, tk.suffix()); } /** * read string token with specified quote and text * * @param text * text of the token * @param quote * quote of token */ protected void readString(String text, String quote) { readString(Tokens.STRING, text, quote); } /** * read string token with specified kind, quote, and text * * @param kind * kind of the token * @param text * text of the token * @param quote * quote of token */ protected void readString(Tokens kind, String text, String quote) { lexer.advance(); final Token tk = lexer.current(); assertEquals("token text check: " + tk, text, tk.text()); assertEquals("token kind check: " + tk, kind, tk.kind()); assertEquals("token suffix check: " + tk, quote, tk.quote()); } /** * read string token with specified quote and text * * @param text * text of the token * @param quote * quote of token */ protected void readMultilineString(String text, String quote) { readString(Tokens.MULTILINE_STRING, text, quote); } /** * Check if text matches intended token * * @param key * the token key * @param text * the text to parse */ protected void checkSingleToken(TokenKey key, String text) { this.start(text); try { this.read(key, text); } finally { this.endLexing(); } } }