package tool.model.grammar;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import junit.framework.Assert;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.junit.Test;
import tool.model.ToolPlan;
import tool.model.ToolServiceObject;
public class TestProject extends ANTLRTest{
IPath planRoot;
public TestProject(){
//this.planRoot = new Path("TestSmall");
//this.planRoot = new Path("TestLarge");
this.planRoot = new Path("/Users/peter/Documents/runtime-EclipseApplication/Demo30/Projects");
}
@Test
public void testCompatibilityLevel() throws RecognitionException{
ForteParser parser = getParser("CompatibilityLevel = 0;");
parser.compatibility_level();
syntaxErrors(parser);
}
@Test
public void testProjectProperty() throws RecognitionException{
ForteParser parser = getParser("CompatibilityLevel = 0;\r\n" +
" ProjectType = APPLICATION;\r\n" +
" Restricted = FALSE;\r\n" +
" MultiThreaded = TRUE;\r\n" +
" Internal = FALSE;\r\n" +
" LibraryName = 'auction';\r\n" +
" StartingMethod = (class = PaintingListWindow, method = Display);");
printTree(parser.project_property().getTree());
syntaxErrors(parser);
}
@Test
public void testProperty() throws RecognitionException{
ForteParser parser = getParser("RoseModelID = 'Self = P:3D816A230399'");
parser.property();
syntaxErrors(parser);
}
@Test
public void testExtendedProperty() throws RecognitionException{
ForteParser parser = getParser("extended = (RoseModelID = 'Self = P:3D816A230399');");
parser.extended_property();
syntaxErrors(parser);
}
@Test
public void testUUID() throws RecognitionException{
ForteParser parser = getParser("UUID = 'BA092140-8B17-11E0-BBF8-AD2A06DEAA77';");
parser.uuid();
syntaxErrors(parser);
}
@Test
public void testProjectConstant() throws RecognitionException{
ForteParser parser = getParser("constant ATO_BLANK_FIELD = '\'\'' HAS PROPERTY id = 0 ;");
parser.constantDeclaration();
syntaxErrors(parser);
}
@Test
public void testProjectConstants() throws RecognitionException{
ForteParser parser = getParser("constant ATO_BLANK_FIELD = '\'\'' HAS PROPERTY id = 0 ;\r\n" +
"constant ATO_COUNTRY_AUST = 'AUSTRALIA' HAS PROPERTY id = 0 ;\r\n" +
"constant ATO_DB_DATE_TEMPLATE = 'yyyymmdd' HAS PROPERTY id = 0 ;");
parser.projectConstants();
syntaxErrors(parser);
}
//@Test
public void testProjectConstantsFile() throws RecognitionException, IOException{
CharStream stream = new NoCaseFileStream("TestSmall//constants.txt");
ForteLexer lexer = new ForteLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ForteParser parser = new ForteParser(tokenStream);
parser.projectConstantsFILE();
syntaxErrors(parser);
}
@Test
public void testProjects() throws IOException, RecognitionException{
int totalErrors = 0;
File planFolderRoot = this.planRoot.toFile();
File[] planFolders = planFolderRoot.listFiles();
for (File folder : planFolders){
if (folder.isDirectory()){
testPRX(folder);
totalErrors += testSingleProjectCDF(folder);
totalErrors += testSingleProjectSO(folder);
totalErrors += testSingleProjectCUR(folder);
//totalErrors += testSingleProjectCEX(folder);
}
}
if (totalErrors > 0) {
System.err.println("Total Syntax Errors: " + totalErrors);
Assert.fail();
} else {
System.out.println("*** NO ERRORS ***");
}
}
public void testPRX(File project) throws IOException, RecognitionException{
File prx = new File(project, project.getName() + ".prx");
if (!prx.exists())
return;
CharStream stream =
new NoCaseFileStream(prx.getAbsolutePath());
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
ForteParser parser = new ForteParser(tokenStream);
CommonTree tree = (CommonTree) parser.project().getTree();
printTree(tree);
int errors = parser.getNumberOfSyntaxErrors();
System.out.println(prx.getName() + " completed with " + errors + " errors");
if (errors > 0){
Assert.fail(prx.getName() + " completed with " + errors + " errors");
}
ToolPlan plan = parseUsingPRXTree(tree, tokenStream, prx.getName());
//plan.writePrxFile("StringTemplates/Tool/PRX.stg", "Output/Tool/" + prx.getName());
}
private ToolPlan parseUsingPRXTree(CommonTree tree, TokenStream tokens, String name) throws RecognitionException{
ToolPlan plan = new ToolPlan(name);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
FortePRXTree walker = new FortePRXTree(plan, nodes);
walker.project();
int errors = walker.getNumberOfSyntaxErrors();
System.out.println(name + " completed with " + errors + " tree errors");
if (errors > 0){
Assert.fail(name + " completed with " + errors + " tree errors");
}
return plan;
}
public int testSingleProjectCDF(File folder) throws IOException{
int totalErrors = 0;
File[] cdfFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".cdf");
}
});
for (File cdf : cdfFiles){
TestClass clsTest = new TestClass();
try {
int errors = clsTest.testClassDefinitionFile(cdf, false);
totalErrors += errors;
} catch (RecognitionException e) {
System.out.println(cdf.getName() + " Syntax Error " + e.getMessage());
}
}
return totalErrors;
}
public int testSingleProjectCEX(File folder) throws RecognitionException, IOException {
int totalErrors = 0;
File[] cexFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".cex");
}
});
for (File cex : cexFiles){
TestClass clsTest = new TestClass();
int errors = clsTest.testClassImplementationFile(cex, false);
System.out.println("\t\t" + cex.getName() + " completed with " + errors + " errors");
totalErrors += errors;
}
return totalErrors;
}
public int testSingleProjectSO(File folder) throws RecognitionException, IOException {
int totalErrors = 0;
File[] soFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".so");
}
});
for (File so : soFiles){
int errors = testSO(so, true);
System.out.println("\t" + so.getName() + " completed with " + errors + " errors");
totalErrors += errors;
}
return totalErrors;
}
public int testSingleProjectCUR(File folder) throws RecognitionException, IOException {
int totalErrors = 0;
File[] curFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".cur");
}
});
for (File cur : curFiles){
int errors = testCUR(cur, true);
System.err.println("\t" + cur.getName() + " completed with " + errors + " errors");
totalErrors += errors;
}
return totalErrors;
}
public int testSO(File so, boolean failOnError) throws RecognitionException, IOException{
CommonTokenStream tokenStream = getStream(so);
ForteParser parser = new ForteParser(tokenStream);
CommonTree tree = (CommonTree) parser.serviceFile().getTree();
printTree(tree);
int errors = parser.getNumberOfSyntaxErrors();
errors+=parseUsingSOTree(tree, tokenStream, so.getName());
if (failOnError && errors > 0) Assert.fail() ;
return errors;
}
private int parseUsingSOTree(CommonTree tree, TokenStream tokens, String name) throws RecognitionException{
ToolServiceObject so = new ToolServiceObject(name);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
ForteSOTree walker = new ForteSOTree(so, nodes);
walker.serviceFile();
int errors = walker.getNumberOfSyntaxErrors();
System.out.println("\t" + name + " completed with " + errors + " tree errors");
if (errors > 0){
Assert.fail(name + " completed with " + errors + " tree errors");
}
return errors;
}
public int testCUR(File cur, boolean failOnError) throws RecognitionException, IOException{
CommonTokenStream tokenStream = getSQLStream(cur);
ForteParser parser = new ForteParser(tokenStream);
CommonTree tree = (CommonTree)parser.cursorFile().getTree();
printTree(tree);
int errors = parser.getNumberOfSyntaxErrors();
if (failOnError && errors > 0) Assert.fail() ;
return errors;
}
}