Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class AssertErrorsErrorListener extends BaseErrorListener {
protected static final String LITERAL_BACKSLASH_N_PLACEHOLDER = "literal-backslash-n";
protected static final String LITERAL_BACKSLASH_N = "\\\\n";
protected List<String> errorMessages = new ArrayList<>();
private Scenario scenario = null;
private Log log = null;
private final Scenario scenario;
private final Log log;

public AssertErrorsErrorListener(Scenario scenario, Log log) {
this.scenario = scenario;
Expand All @@ -53,7 +53,7 @@ public AssertErrorsErrorListener(Scenario scenario, Log log) {

public void assertErrors(File errorMessagesFile, String encoding) throws AssertErrorsException {
if (!errorMessages.isEmpty()) {
List<String> expectedErrorMessages = null;
List<String> expectedErrorMessages;
String errorMessageFileName = null;
if (errorMessagesFile != null) {
errorMessageFileName = errorMessagesFile.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
package com.khubla.antlr.antlr4test;

public class AssertErrorsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;

public AssertErrorsException(String message) {
Expand Down
40 changes: 12 additions & 28 deletions src/main/java/com/khubla/antlr/antlr4test/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public class FileUtil {
*
* @param dir Directory
* @return list of files
* @throws Exception from getAllFiles
*/
public static List<File> getAllFiles(String dir) throws Exception {
public static List<File> getAllFiles(String dir) {
return getAllFiles(dir, null);
}

Expand All @@ -53,17 +52,20 @@ public static List<File> getAllFiles(String dir) throws Exception {
* @return list of files
*/
public static List<File> getAllFiles(String dir, String extension) {
final List<File> ret = new ArrayList<File>();
final List<File> ret = new ArrayList<>();
final File file = new File(dir);
if (file.exists()) {
final String[] list = file.list();
if (null != list) {
for (int i = 0; i < list.length; i++) {
final String fileName = dir + "/" + list[i];
for (String s : list) {
final String fileName = dir + "/" + s;
final File f2 = new File(fileName);
if (!f2.isHidden()) {
if (f2.isDirectory()) {
ret.addAll(getAllFiles(fileName, extension));
List<File> f2Files = getAllFiles(fileName, extension);
if (null != f2Files) {
ret.addAll(f2Files);
}
} else {
if (null != extension) {
if (f2.getName().endsWith(extension)) {
Expand All @@ -83,16 +85,10 @@ public static List<File> getAllFiles(String dir, String extension) {
}

public static List<String> getNonEmptyLines(File file, String encoding) throws IOException {
final List<String> nonEmptyLines = new ArrayList<String>();
BufferedReader br = null;
InputStreamReader isr = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
try {
isr = new InputStreamReader(fis, encoding);
try {
br = new BufferedReader(isr);
final List<String> nonEmptyLines = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(file)) {
try (InputStreamReader isr = new InputStreamReader(fis, encoding)) {
try (BufferedReader br = new BufferedReader(isr)) {
String line = br.readLine();
while (line != null) {
if (!"".equals(line.trim())) {
Expand All @@ -101,19 +97,7 @@ public static List<String> getNonEmptyLines(File file, String encoding) throws I
line = br.readLine();
}
return nonEmptyLines;
} finally {
if (br != null) {
br.close();
}
}
} finally {
if (isr != null) {
isr.close();
}
}
} finally {
if (fis != null) {
fis.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
import org.antlr.v4.runtime.*;

public interface GrammarInitializer {
public void initialize(Lexer lexer, Parser parser);
void initialize(Lexer lexer, Parser parser);
}
20 changes: 8 additions & 12 deletions src/main/java/com/khubla/antlr/antlr4test/GrammarTestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
package com.khubla.antlr.antlr4test;

import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.maven.plugin.*;
Expand All @@ -39,7 +38,7 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
/**
* @author Tom Everett
*/
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, requiresProject = true, threadSafe = false)
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST)
public class GrammarTestMojo extends AbstractMojo {
/**
* errors file
Expand Down Expand Up @@ -98,7 +97,7 @@ public class GrammarTestMojo extends AbstractMojo {
* testFileExtension
*/
@Parameter
private String testFileExtension = null;
private String testFileExtension;
/**
* basedir dir
*/
Expand All @@ -113,12 +112,12 @@ public class GrammarTestMojo extends AbstractMojo {
* Full qualified class name to initialize grammar (Lexer and/or Parser) before test starts
*/
@Parameter
private String grammarInitializer = null;
private String grammarInitializer;
/**
* List of test scenarios to be executed.
*/
@Parameter
private List<Scenario> scenarios = null;
private List<Scenario> scenarios;
/**
* read outputDirectory from pom project.build.outputDirectory
*/
Expand All @@ -132,20 +131,18 @@ public class GrammarTestMojo extends AbstractMojo {

/**
* ctor
*
* @throws MalformedURLException exception for malformed url *eye roll*
*/
public GrammarTestMojo() throws MalformedURLException {
public GrammarTestMojo() {
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {
try {
/*
* No scenario configuration has been given. Creates a default one.
*/
if (scenarios == null) {
scenarios = new ArrayList<Scenario>();
scenarios = new ArrayList<>();
}
if ((grammarName != null) && !"".equals(grammarName)) {
//
Expand Down Expand Up @@ -357,8 +354,7 @@ private void testScenarios() throws Exception {
}
if (scenario.isEnabled()) {
ScenarioExecutor executor = new ScenarioExecutor(this, scenario, mojoLogger);
executor.testGrammars();
executor = null;
executor.testExamples();
} else {
mojoLogger.warn("Scenario " + scenario.getScenarioName() + " is disabled. Skipping.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/khubla/antlr/antlr4test/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,12 @@ public class Scenario {
* Full qualified class name to initialize grammar (Lexer and/or Parser) before test starts
*/
@Parameter
private String grammarInitializer = null;
private String grammarInitializer;

public File getBaseDir() {
return baseDir;
}

public boolean getBinary() {
return binary;
}

public CaseInsensitiveType getCaseInsensitiveType() {
return caseInsensitiveType;
}
Expand Down Expand Up @@ -156,6 +152,10 @@ public String getTestFileExtension() {
return testFileExtension;
}

public boolean isBinary() {
return binary;
}

public boolean isEnabled() {
return enabled;
}
Expand Down
69 changes: 29 additions & 40 deletions src/main/java/com/khubla/antlr/antlr4test/ScenarioExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
import com.khubla.antlr.antlr4test.filestream.*;

public class ScenarioExecutor {
private Scenario scenario = null;
private Log log = null;
private GrammarTestMojo mojo = null;
private final Scenario scenario;
private final Log log;
private final GrammarTestMojo mojo;
private final HashMap<URL, ClassLoader> classLoaderMap = new HashMap<>();

public ScenarioExecutor(GrammarTestMojo mojo, Scenario scenario, Log log) {
Expand All @@ -54,7 +54,7 @@ public ScenarioExecutor(GrammarTestMojo mojo, Scenario scenario, Log log) {
this.log = log;
}

private ClassLoader getClassLoader(String path) throws MalformedURLException, ClassNotFoundException {
private ClassLoader getClassLoader(String path) throws MalformedURLException {
/*
* create a ClassLoader child of Thread.currentThread().getContextClassLoader().
*/
Expand All @@ -64,7 +64,7 @@ private ClassLoader getClassLoader(String path) throws MalformedURLException, Cl
/**
* build a ClassLoader that can find the files we need
*/
private ClassLoader getClassLoader(String path, ClassLoader parent) throws MalformedURLException, ClassNotFoundException {
private ClassLoader getClassLoader(String path, ClassLoader parent) throws MalformedURLException {
final URL antlrGeneratedURL = new File(path).toURI().toURL();
/*
* check if ClassLoader for this URL was already created.
Expand All @@ -80,9 +80,9 @@ private ClassLoader getClassLoader(String path, ClassLoader parent) throws Malfo
}

/**
* test a single grammar
* test a single example
*/
private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
private void testExample(Scenario scenario, File exampleFile) throws Exception {
/*
* figure out class names
*/
Expand Down Expand Up @@ -119,20 +119,20 @@ private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
*/
final Constructor<?> lexerConstructor = lexerClass.getConstructor(CharStream.class);
final Constructor<?> parserConstructor = parserClass.getConstructor(TokenStream.class);
log.info("Parsing :" + grammarFile.getAbsolutePath());
log.info("Parsing :" + exampleFile.getAbsolutePath());
CharStream antlrCharStream;
/*
* case
*/
if (scenario.getCaseInsensitiveType() == CaseInsensitiveType.None) {
antlrCharStream = CharStreams.fromPath(grammarFile.toPath(), Charset.forName(scenario.getFileEncoding()));
antlrCharStream = CharStreams.fromPath(exampleFile.toPath(), Charset.forName(scenario.getFileEncoding()));
} else {
antlrCharStream = new AntlrCaseInsensitiveFileStream(grammarFile.getAbsolutePath(), scenario.getFileEncoding(), scenario.getCaseInsensitiveType());
antlrCharStream = new AntlrCaseInsensitiveFileStream(exampleFile.getAbsolutePath(), scenario.getFileEncoding(), scenario.getCaseInsensitiveType());
}
/*
* binary
*/
if (true == scenario.getBinary()) {
if (scenario.isBinary()) {
antlrCharStream = new BinaryCharStream(antlrCharStream);
}
/*
Expand Down Expand Up @@ -171,7 +171,7 @@ private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
}
final Method method = parserClass.getMethod(scenario.getEntryPoint());
ParserRuleContext parserRuleContext = (ParserRuleContext) method.invoke(parser);
assertErrorsErrorListener.assertErrors(new File(grammarFile.getAbsolutePath() + GrammarTestMojo.ERRORS_SUFFIX), scenario.getFileEncoding());
assertErrorsErrorListener.assertErrors(new File(exampleFile.getAbsolutePath() + GrammarTestMojo.ERRORS_SUFFIX), scenario.getFileEncoding());
/*
* show the tree
*/
Expand All @@ -183,35 +183,24 @@ private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
/*
* check syntax
*/
final File treeFile = new File(grammarFile.getAbsolutePath() + GrammarTestMojo.TREE_SUFFIX);
final File treeFile = new File(exampleFile.getAbsolutePath() + GrammarTestMojo.TREE_SUFFIX);
if (treeFile.exists()) {
final String lispTree = Trees.toStringTree(parserRuleContext, parser);
if (null != lispTree) {
final String treeFileData = FileUtils.fileRead(treeFile, scenario.getFileEncoding());
if (null != treeFileData) {
if (0 != treeFileData.compareTo(lispTree)) {
final StringBuilder sb = new StringBuilder("Parse tree does not match '" + treeFile.getName() + "'. Differences: ");
for (final DiffMatchPatch.Diff diff : new DiffMatchPatch().diffMain(treeFileData, lispTree)) {
sb.append(diff.toString());
sb.append(", ");
}
throw new Exception(sb.toString());
} else {
log.info("Parse tree for '" + grammarFile.getName() + "' matches '" + treeFile.getName() + "'");
}
final String treeFileData = FileUtils.fileRead(treeFile, scenario.getFileEncoding());
if (0 != treeFileData.compareTo(lispTree)) {
final StringBuilder sb = new StringBuilder("Parse tree does not match '" + treeFile.getName() + "'. Differences: ");
for (final DiffMatchPatch.Diff diff : new DiffMatchPatch().diffMain(treeFileData, lispTree)) {
sb.append(diff.toString());
sb.append(", ");
}
throw new Exception(sb.toString());
} else {
log.info("Parse tree for '" + exampleFile.getName() + "' matches '" + treeFile.getName() + "'");
}
}
/*
* yup
*/
parser = null;
lexer = null;
parserRuleContext = null;
antlrCharStream = null;
}

public void testGrammars() throws Exception {
public void testExamples() throws Exception {
/*
* iterate examples
*/
Expand All @@ -226,7 +215,7 @@ public void testGrammars() throws Exception {
* file extension
*/
if ((scenario.getTestFileExtension() == null) || ((scenario.getTestFileExtension() != null) && (file.getName().endsWith(scenario.getTestFileExtension())))) {
testGrammar(scenario, file);
testExample(scenario, file);
}
}
/*
Expand All @@ -244,18 +233,18 @@ public void testGrammars() throws Exception {
* @return hex bytes
*/
private String tokToHex(Token token) {
String ret = "";
StringBuilder ret = new StringBuilder();
token.getInputStream().seek(0);
boolean first = true;
for (int i = token.getStartIndex(); i < (token.getStopIndex() + 1); i++) {
if (true == first) {
if (first) {
first = false;
} else {
ret += ",";
ret.append(",");
}
final int t = token.getInputStream().LA(i + 1);
ret += "0x" + String.format("%02X", (byte) t);
ret.append("0x").append(String.format("%02X", (byte) t));
}
return ret;
return ret.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* @author mario.schroeder
*/
public class AssertErrorsErrorListenerTest {
private Scenario scenario = null;
private Log log = null;
private AssertErrorsErrorListener classUnderTest = null;
private Scenario scenario;
private Log log;
private AssertErrorsErrorListener classUnderTest;

@Before
public void setUp() {
Expand Down
Loading