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.ToolServiceObject;
public class ServiceTest extends ANTLRTest{
IPath planRoot;
public ServiceTest(){
this.planRoot = new Path("/Users/peter/Documents/runtime-EclipseApplication/Demo30/Projects");
}
@Test
public void testSimpleSO() throws RecognitionException{
CharStream stream =
new NoCaseStringStream("service ArtistService : WinDB.ArtistAccessMgr = (DialogDuration = transaction,\n" +
" Visibility = environment,\n" +
" FailOver = false,\n" +
" LoadBalance = false)\n" +
"HAS PROPERTY\n" +
" extended = (UUID = '9B874644-C9B6-1218-BAF3-AB731C62AA77');");
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
ForteParser parser = new ForteParser(tokenStream);
CommonTree tree = (CommonTree) parser.serviceDeclaration().getTree();
printTree(tree);
int errors = parser.getNumberOfSyntaxErrors();
errors+=parseUsingSOTree(tree, tokenStream, "ArtistService");
if (errors > 0) Assert.fail() ;
}
@Test
public void testProjects(){
File planFolderRoot = this.planRoot.toFile();
File[] planFolders = planFolderRoot.listFiles();
for (File folder : planFolders){
if (folder.isDirectory()){
testProjectSO(folder);
}
}
}
public void testProjectSO(File folder) {
File[] soFiles = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".so");
}
});
for (File so : soFiles){
int errors = 0;
try {
ForteParser parser = getParser(so);
CommonTree tree = (CommonTree) parser.serviceDeclaration().getTree();
printTree(tree);
errors = parser.getNumberOfSyntaxErrors();
errors+=parseUsingSOTree(tree, parser.getTokenStream(), so.getName());
if (errors > 0) Assert.fail() ;
} catch (IOException e) {
e.printStackTrace();
} catch (RecognitionException e) {
e.printStackTrace();
} finally {
System.out.println(so.getName() + " completed with " + errors + " errors");
}
}
}
private TokenStream createStream(File so) throws IOException{
CharStream stream =
new NoCaseFileStream(so.getAbsolutePath());
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
}
public void testSingleSO(File so){
try {
CharStream stream =
new NoCaseFileStream(so.getAbsolutePath());
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
ForteParser parser = new ForteParser(tokenStream);
parser.serviceDeclaration();
int errors = parser.getNumberOfSyntaxErrors();
System.out.println(so.getName() + " completed with " + errors + " errors");
if (errors > 0){
Assert.fail(so.getName() + " completed with " + errors + " errors");
}
} catch (RecognitionException e) {
Assert.fail(e.getMessage());
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
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;
}
}