/*
* 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.phrase_parser;
import java.net.URL;
import junit.framework.TestCase;
import net.sf.etl.parsers.PhraseParser;
import net.sf.etl.parsers.PhraseParserFactory;
import net.sf.etl.parsers.PhraseToken;
import net.sf.etl.parsers.PhraseTokens;
import net.sf.etl.parsers.Tokens;
import org.xml.sax.InputSource;
/**
* base class for phrase parser test cases
*
* @author const
*/
public abstract class PharseParserTestCase extends TestCase {
/**
* a phrase parser used in the test case
*/
PhraseParser parser;
/**
* a constructor
*/
public PharseParserTestCase() {
super();
}
/**
* a constructor
*
* @param arg0
* test name
*/
public PharseParserTestCase(String arg0) {
super(arg0);
}
/**
* start parsing resource
*
* @param resourceName
* name of resource
*/
protected void startWithResource(String resourceName) {
final URL in = this.getClass().getResource(resourceName);
assertNotNull(in);
parser = PhraseParserFactory.newInstance().newPhraseParser();
parser.parse(new InputSource(in.toString()));
}
/**
* end parsing. this method closes all active streams.
*/
protected void endLexing() {
parser.close();
}
/**
* skip ignorable tokens
*/
void skipIgnorable() {
while (true) {
switch (parser.current().kind()) {
case IGNORABLE:
case CONTROL:
parser.advance();
break;
default:
return;
}
}
}
/**
* read control token
*
* @param ckind
* control token kind
*/
protected void read(PhraseTokens ckind) {
parser.advance();
skipIgnorable();
final PhraseToken tk = parser.current();
assertEquals("token kind check: " + parser, ckind, tk.kind());
}
/**
* read control token that and check token kind
*
* @param ckind
* control kind
* @param kind
* token kind
*/
protected void read(PhraseTokens ckind, Tokens kind) {
parser.advance();
skipIgnorable();
final PhraseToken tk = parser.current();
assertEquals("token kind check: " + parser, ckind, tk.kind());
assertEquals("token kind check: " + parser, kind, tk.token().kind());
}
/**
* read control token that and check token kind and text
*
* @param ckind
* control kind
* @param kind
* token kind
* @param text
* token text
*/
protected void read(PhraseTokens ckind, Tokens kind, String text) {
parser.advance();
skipIgnorable();
final PhraseToken tk = parser.current();
assertEquals("token kind check: " + parser, ckind, tk.kind());
assertEquals("token text check: " + parser, text, tk.token().text());
assertEquals("token kind check: " + parser, kind, tk.token().kind());
}
}