-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added config options and argument option -e, --error-log, for error…
… log path and name - Added mock errors to testsuite error log - At the end of the testsuites an exception will be thrown, if any errors were found - Fixed bug, where an 88 reverse compare, would show one value not being reversed. - Minor refactoring Signed-off-by: dakaa16 <davidkaan@gmail.com>
- Loading branch information
Showing
22 changed files
with
580 additions
and
211 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/org/openmainframeproject/cobolcheck/exceptions/TestSuiteSyntaxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.openmainframeproject.cobolcheck.exceptions; | ||
|
||
public class TestSuiteSyntaxException extends RuntimeException { | ||
public TestSuiteSyntaxException(String message) { | ||
super(message); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../openmainframeproject/cobolcheck/exceptions/VerifyReferencesNonexistentMockException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
...main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/KeywordSyntax.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
.../java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteErrorLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package org.openmainframeproject.cobolcheck.features.testSuiteParser; | ||
|
||
import org.openmainframeproject.cobolcheck.services.Config; | ||
import org.openmainframeproject.cobolcheck.services.Constants; | ||
import org.openmainframeproject.cobolcheck.services.Messages; | ||
import org.openmainframeproject.cobolcheck.services.cobolLogic.TokenExtractor; | ||
import org.openmainframeproject.cobolcheck.services.filehelpers.EncodingIO; | ||
import org.openmainframeproject.cobolcheck.services.filehelpers.FilePermission; | ||
import org.openmainframeproject.cobolcheck.services.log.Log; | ||
|
||
import java.io.*; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class TestSuiteErrorLog { | ||
|
||
|
||
|
||
public enum ErrorTypes {SYNTAX_ERROR, RUNTIME_ERROR, WARNING} | ||
private Keyword lastKeyword; | ||
private String fileMessage = "%1s in file: %2s"; | ||
private String lineIndexMessage = "Unexpected token on line %1s, index %2s:"; | ||
|
||
private String followingExpectedGotMessage = "Following <%1s>" + Constants.NEWLINE + | ||
"Expected %2s" + Constants.NEWLINE + "Got <%3s>"; | ||
private String keywordInBlock = "Cannot have Cobol Check keyword <%1s> inside a %2s block"; | ||
|
||
private boolean errorOccured = false; | ||
|
||
private final List<String> cobolCheckStartingAndEndingKeywords = Arrays.asList(Constants.TESTSUITE_KEYWORD, | ||
Constants.TESTCASE_KEYWORD, Constants.EXPECT_KEYWORD, Constants.MOCK_KEYWORD, Constants.ENDMOCK_KEYWORD, | ||
Constants.VERIFY_KEYWORD, Constants.BEFORE_EACH_TOKEN, Constants.END_BEFORE_TOKEN, Constants.AFTER_EACH_TOKEN, | ||
Constants.END_AFTER_TOKEN); | ||
|
||
private String errorLogPath; | ||
|
||
private String lastErrorLogMessage; | ||
|
||
public TestSuiteErrorLog(){ | ||
errorLogPath = getTestSuiteParserErrorLogPath(); | ||
initializeTestSuiteErrorLogWriter(errorLogPath); | ||
} | ||
|
||
public boolean hasErrorOccured() { | ||
return errorOccured; | ||
} | ||
|
||
public String getLastErrorMessage(){ return lastErrorLogMessage; } | ||
|
||
public void checkExpectedTokenSyntax(Keyword currentKeyword, String currentFile, int lineNumber, int lineIndex){ | ||
if (lastKeyword != null){ | ||
String error = ""; | ||
if (!lastKeyword.getvalidNextKeys().contains(currentKeyword.value())){ | ||
errorOccured = true; | ||
error += String.format(fileMessage, displayErrorType(ErrorTypes.SYNTAX_ERROR), currentFile) + Constants.NEWLINE; | ||
error += String.format(lineIndexMessage, lineNumber, lineIndex) + Constants.NEWLINE; | ||
error += String.format(followingExpectedGotMessage, lastKeyword.value(), | ||
Arrays.toString(lastKeyword.getvalidNextKeys().toArray()), currentKeyword.value()) + | ||
Constants.NEWLINE + Constants.NEWLINE; | ||
outputError(error); | ||
} | ||
} | ||
lastKeyword = currentKeyword; | ||
} | ||
|
||
public void checkSyntaxInsideBlock(String blockKeyword, List<String> cobolLines, TokenExtractor tokenExtractor, String currentFile, int lineNumber) { | ||
int revertedCount = cobolLines.size(); | ||
for (String line : cobolLines){ | ||
List<String> keywords = tokenExtractor.extractTokensFrom(line); | ||
for(String keyword : keywords){ | ||
if (cobolCheckStartingAndEndingKeywords.contains(keyword.toUpperCase(Locale.ROOT))){ | ||
errorOccured = true; | ||
String error = ""; | ||
int index = line.indexOf(keyword) + 1; | ||
error += String.format(fileMessage, displayErrorType(ErrorTypes.SYNTAX_ERROR), currentFile) + Constants.NEWLINE; | ||
error += String.format(lineIndexMessage, lineNumber - revertedCount, index) + Constants.NEWLINE; | ||
error += String.format(keywordInBlock, keyword, blockKeyword) + Constants.NEWLINE + Constants.NEWLINE; | ||
outputError(error); | ||
} | ||
} | ||
revertedCount -=1; | ||
} | ||
} | ||
|
||
public void logIdenticalMocks(Mock mock){ | ||
String error = ""; | ||
errorOccured = true; | ||
error += String.format(fileMessage, displayErrorType(ErrorTypes.RUNTIME_ERROR), mock.getTestSuiteFileName()) + Constants.NEWLINE; | ||
error += String.format(lineIndexMessage, mock.getDeclarationLineNumberInOriginalFile(), | ||
mock.getDeclarationIndexNumberInOriginalFile()) + Constants.NEWLINE; | ||
String message = "Mock <" + mock.getIdentifier() + "> already exists " + | ||
(mock.getArguments().isEmpty() ? "" : "with the given arguments ") + "in this " + | ||
mock.getScope().name() + ((mock.getScope() == MockScope.Local) ? " testcase " : " testsuite ") + | ||
"scope"; | ||
error += message + Constants.NEWLINE + Constants.NEWLINE; | ||
outputError(error); | ||
} | ||
|
||
public void logVerifyReferencesNonExistentMock(VerifyMockCount verify) { | ||
String error = ""; | ||
errorOccured = true; | ||
error += String.format(fileMessage, displayErrorType(ErrorTypes.RUNTIME_ERROR), verify.getTestSuiteFileName()) + Constants.NEWLINE; | ||
error += String.format(lineIndexMessage, verify.getDeclarationLineNumberInOriginalFile(), | ||
verify.getDeclarationIndexNumberInOriginalFile()) + Constants.NEWLINE; | ||
String message = "Verify references non existent mock. Mock does not exist for: " + verify.getType() + " " + verify.getIdentifier() + | ||
((verify.getArguments().isEmpty()) ? " with no arguments" : " with the given arguments"); | ||
error += message + Constants.NEWLINE + Constants.NEWLINE; | ||
outputError(error); | ||
} | ||
|
||
public void logUnusedMocks(List<Mock> mocks){ | ||
for (Mock mock : mocks){ | ||
if (!mock.isUsed()){ | ||
String error = ""; | ||
error += String.format(fileMessage, displayErrorType(ErrorTypes.WARNING), mock.getTestSuiteFileName()) + Constants.NEWLINE; | ||
error += String.format(lineIndexMessage, mock.getDeclarationLineNumberInOriginalFile(), | ||
mock.getDeclarationIndexNumberInOriginalFile()) + Constants.NEWLINE; | ||
error += "Mock <" + mock.getType() + "> <" + mock.getIdentifier() + "> does not reference " + | ||
"any construct in the source code" + Constants.NEWLINE + Constants.NEWLINE; | ||
outputError(error); | ||
} | ||
} | ||
} | ||
|
||
private void outputError(String error) { | ||
lastErrorLogMessage = error; | ||
System.out.println(error); | ||
BufferedWriter errorLogWriter = null; | ||
try { | ||
errorLogWriter = (BufferedWriter) EncodingIO.getWriterWithCorrectEncoding(errorLogPath, true); | ||
errorLogWriter.write(error); | ||
errorLogWriter.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
errorLogWriter.close(); | ||
} catch (Exception e) { | ||
System.out.println("TestSuite error log file could not be closed: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private String displayErrorType(ErrorTypes errorType){ | ||
return errorType.name().replace("_", " "); | ||
} | ||
|
||
/** | ||
* Gets the path for the TestSuite Parser error log | ||
*/ | ||
private String getTestSuiteParserErrorLogPath(){ | ||
StringBuilder testSuiteParserErrorLogPath = new StringBuilder(); | ||
String configPath = Config.getTestsuiteparserErrorLogPath(); | ||
if (configPath == null) | ||
configPath = "." + Constants.FILE_SEPARATOR; | ||
if (configPath.equals("." + Constants.FILE_SEPARATOR)) | ||
testSuiteParserErrorLogPath.append(new File(Constants.EMPTY_STRING).getAbsolutePath()); | ||
else | ||
testSuiteParserErrorLogPath.append(new File(configPath)); | ||
|
||
testSuiteParserErrorLogPath.append(Constants.FILE_SEPARATOR); | ||
testSuiteParserErrorLogPath.append(Config.getTestsuiteparserErrorLogName()); | ||
return testSuiteParserErrorLogPath.toString(); | ||
} | ||
|
||
/** | ||
* Returns a Writer for the TestSuite Parser error log. | ||
*/ | ||
private void initializeTestSuiteErrorLogWriter(String path){ | ||
String testSuiteParserErrorLogPath = path; | ||
Writer testSourceWriter = null; | ||
try { | ||
File testSuiteParserErrorLogFile = new File(testSuiteParserErrorLogPath); | ||
if (testSuiteParserErrorLogFile.exists()) | ||
testSuiteParserErrorLogFile.delete(); | ||
|
||
Writer filecreator = new FileWriter(testSuiteParserErrorLogPath); | ||
filecreator.close(); | ||
testSourceWriter = EncodingIO.getWriterWithCorrectEncoding(testSuiteParserErrorLogPath); | ||
FilePermission.setFilePermissionForAllUsers(testSuiteParserErrorLogPath, Config.getGeneratedFilesPermissionAll()); | ||
Log.info(Messages.get("INF013", testSuiteParserErrorLogPath)); | ||
testSourceWriter.close(); | ||
} catch (IOException testSourceOutException) { | ||
System.out.println(testSourceOutException.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.