package tool.model.grammar;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.junit.Before;
import org.junit.Test;
import tool.model.ToolClass;
import tool.model.grammar.ForteParser.classFile_return;
import tool.model.grammar.ForteParser.sql_expression_return;
import tool.model.grammar.ForteParser.sql_select_return;
import tool.model.grammar.ForteParser.sql_statement_return;
import tool.model.grammar.ForteParser.statement_return;
public class TestClassFile {
private ForteParser parser;
private TokenStream getStream(File file) throws IOException{
CharStream stream =
new NoCaseFileStream(file.getAbsolutePath());
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
}
private TokenStream getStream(String source) throws IOException{
CharStream stream =
new NoCaseStringStream(source);
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
}
private void printTree(CommonTree tree, int indent){
if (tree != null){
StringBuffer sb = new StringBuffer(indent);
for (int i = 0; i < indent; i++)
sb = sb.append(" ");
for (int i = 0; i < tree.getChildCount(); i++){
System.out.println(sb.toString() + tree.getChild(i).toString());
printTree((CommonTree)tree.getChild(i), indent+1);
}
}
}
@Before
public void setUp() throws Exception {
parser = new ForteParser(null);
}
@Test
public void testClassFile() throws Exception{
File file = new File("TestData/AccTranDetDBHandler.CLA");
CommonTree tree = null;
CommonTokenStream tokens = (CommonTokenStream) getStream(file);
parser.setTokenStream(tokens);
parser.setTreeAdaptor(new CommonTreeAdaptor(){
public Object create(Token payload){
return new CommonTree(payload);
}
});
classFile_return result = parser.classFile();
if (parser.getNumberOfSyntaxErrors() > 0){
fail(parser.getNumberOfSyntaxErrors() + " Syntax error(s) in class " + file.getName());
}
tree = (CommonTree) result.getTree();
printTree(tree, 0);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
ForteCDFTree walker = new ForteCDFTree(nodes);
ToolClass toolClass = walker.classDeclaration().toolClass;
if (toolClass != null)
System.out.println("Class: " + toolClass.getToolName());
}
@Test
public void testSQLStatement() throws Exception{
CommonTokenStream tokens = (CommonTokenStream) getStream("SQL\n" +
" SELECT sum(abs.ledger_acc_bal)\n" +
" INTO :dnTranAmount\n" +
" FROM ledger_acc la,\n" +
" acc_bal_summary abs \n" +
" WHERE abs.acc_id = :theAccTranDet.dAccId \n" +
" AND abs.acc_type = :toOracle(theAccTranDet.sAccType)\n" +
" AND abs.ledger_acc_num = la.ledger_acc_num\n" +
" AND la.ledger_acc_type = :theAccTranDet.theLedgerAcc.iLedgerAccType\n" +
" AND abs.eff_from_Date <= TO_TIMESTAMP(:dnCalcToDate, :lTimestampFormat)\n" +
" AND (abs.eff_to_date >= TO_TIMESTAMP(:dnCalcToDate, :lTimestampFormat)\n" +
" OR abs.eff_to_date is null)\n" +
"\n" +
"ON Session SessionToUse;");
parser.setTokenStream(tokens);
parser.setTreeAdaptor(new CommonTreeAdaptor(){
public Object create(Token payload){
return new CommonTree(payload);
}
});
sql_statement_return result = parser.sql_statement();
if (parser.getNumberOfSyntaxErrors() > 0){
fail(parser.getNumberOfSyntaxErrors() + " Syntax error(s) in SQL Statement");
}
}
@Test
public void testStatement() throws Exception{
CommonTokenStream tokens = (CommonTokenStream) getStream("SQL\n" +
" SELECT sum(abs.ledger_acc_bal)\n" +
" INTO :dnTranAmount\n" +
" FROM ledger_acc la,\n" +
" acc_bal_summary abs \n" +
" WHERE abs.acc_id = :theAccTranDet.dAccId \n" +
" AND abs.acc_type = :toOracle(theAccTranDet.sAccType)\n" +
" AND abs.ledger_acc_num = la.ledger_acc_num\n" +
" AND la.ledger_acc_type = :theAccTranDet.theLedgerAcc.iLedgerAccType\n" +
" AND abs.eff_from_Date <= TO_TIMESTAMP(:dnCalcToDate, :lTimestampFormat)\n" +
" AND (abs.eff_to_date >= TO_TIMESTAMP(:dnCalcToDate, :lTimestampFormat)\n" +
" OR abs.eff_to_date is null)\n" +
"\n" +
"ON Session SessionToUse;");
parser.setTokenStream(tokens);
parser.setTreeAdaptor(new CommonTreeAdaptor(){
public Object create(Token payload){
return new CommonTree(payload);
}
});
statement_return result = parser.statement();
if (parser.getNumberOfSyntaxErrors() > 0){
fail(parser.getNumberOfSyntaxErrors() + " Syntax error(s) in Tool Statement");
}
}
}