Skip to content

Commit

Permalink
Merge pull request #307 from AkashKumar7902/Developer
Browse files Browse the repository at this point in the history
217 add duplication check for test suites and test cases
  • Loading branch information
Rune-Christensen authored Aug 18, 2023
2 parents 518d232 + 1827f12 commit 426fa6c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openmainframeproject.cobolcheck.exceptions;

public class TestCaseAlreadyExistsException extends RuntimeException {
public TestCaseAlreadyExistsException(String message) {
super(message);
}
public TestCaseAlreadyExistsException(Throwable cause) {
super(cause);
}
public TestCaseAlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openmainframeproject.cobolcheck.exceptions;

public class TestSuiteAlreadyExistsException extends RuntimeException {
public TestSuiteAlreadyExistsException(String message) {
super(message);
}
public TestSuiteAlreadyExistsException(Throwable cause) {
super(cause);
}
public TestSuiteAlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.HashMap;
import java.util.HashSet;

/**
* Parses the concatenated test suite and writes Cobol test code to the output
Expand All @@ -22,6 +24,7 @@ public class TestSuiteParser {
private final KeywordExtractor keywordExtractor;
private TestSuiteWritingStyle testSuiteWritingStyle;
private List<String> testSuiteTokens;
private HashMap<String, HashSet<String>> testNamesHierarchy;
private String currentTestSuiteLine = "";
private int fileLineNumber = 0;
private int fileLineIndexNumber = 0;
Expand Down Expand Up @@ -158,6 +161,7 @@ public TestSuiteParser(KeywordExtractor keywordExtractor, MockRepository mockRep
this.beforeAfterRepo = beforeAfterRepo;
this.testSuiteErrorLog = testSuiteErrorLog;
testSuiteTokens = new ArrayList<>();
testNamesHierarchy = new HashMap<String, HashSet<String>>();
emptyTestSuite = true;
testCodePrefix = Config.getString(Constants.COBOLCHECK_PREFIX_CONFIG_KEY, Constants.DEFAULT_COBOLCHECK_PREFIX);
initializeCobolStatement();
Expand All @@ -177,7 +181,6 @@ public List<String> getParsedTestSuiteLines(BufferedReader testSuiteReader,
numericFields = numericFieldsList;
String testSuiteToken = getNextTokenFromTestSuite(testSuiteReader);
while (testSuiteToken != null) {

if (!testSuiteToken.startsWith(Constants.QUOTE) && !testSuiteToken.startsWith(Constants.APOSTROPHE)) {
testSuiteToken = testSuiteToken.toUpperCase(Locale.ROOT);
}
Expand Down Expand Up @@ -405,13 +408,27 @@ public List<String> getParsedTestSuiteLines(BufferedReader testSuiteReader,
if (expectTestsuiteName) {
expectTestsuiteName = false;
currentTestSuiteName = testSuiteToken;
if (testNamesHierarchy.containsKey(currentTestSuiteName)) {
throw new TestSuiteAlreadyExistsException(Messages.get("ERR031", currentTestSuiteName));
}
testNamesHierarchy.put(currentTestSuiteName, new HashSet<String>());
RunInfo.addTestSuiteNameToPathMapKeyValuePair(currentTestSuiteName, currentTestSuiteRealFile);
addTestSuiteNamelines(currentTestSuiteName, parsedTestSuiteLines);
initializeCobolStatement();
}
if (expectTestcaseName) {
expectTestcaseName = false;
currentTestCaseName = testSuiteToken;
HashSet<String> testCaseSet = testNamesHierarchy.get(currentTestSuiteName);
if (testCaseSet != null) {
if(testCaseSet.contains(currentTestCaseName)) {
throw new TestCaseAlreadyExistsException(Messages.get("ERR032", currentTestCaseName,
currentTestSuiteName));
}
else {
testCaseSet.add(currentTestCaseName);
}
}
addTestCaseNameLines(currentTestCaseName, parsedTestSuiteLines);
initializeCobolStatement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ ERR026 = ERR026: Constructor of Constants class should never be called.
ERR027 = ERR027: NumericFields.dataTypeOf() was called with empty fieldName.
ERR028 = ERR028: NumericFields.setDataTypeOf() was called with empty fieldName.
ERR029 = ERR029: NumericFields.setDataTypeOf() was called with null dataType.

ERR030 = ERR030: Command line missing program argument '-p programName' .

ERR031 = ERR031: A test suite with the name %1$s already exists.
ERR032 = ERR032: A test case with the name %1$s already exists in the test suite %2$s.

WRN001 = WRN001: No test suite directory for program %1$s was found under directory %2$s.
WRN002 = WRN002: No test suite files were found under directory %1$s.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmainframeproject.cobolcheck;

import org.openmainframeproject.cobolcheck.exceptions.TestSuiteAlreadyExistsException;
import org.openmainframeproject.cobolcheck.exceptions.TestCaseAlreadyExistsException;
import org.openmainframeproject.cobolcheck.exceptions.VerifyReferencesNonexistentMockException;
import org.openmainframeproject.cobolcheck.features.testSuiteParser.*;
import org.openmainframeproject.cobolcheck.features.writer.CobolWriter;
Expand Down Expand Up @@ -56,6 +58,29 @@ void commonSetup() {
cobolWriter = new CobolWriter(mockTestProgramSource);
numericFields = new NumericFields();
}

@Test
public void multiple_testsuites_with_same_name_exists_for_a_program() throws IOException {
String str1 = " TESTSUITE \"Name of test suite\"";
String str2 = " TESTSUITE \"Name of test suite\"";

Mockito.when(mockedReader.readLine()).thenReturn(str1, str2, null);

Throwable ex = assertThrows(TestSuiteAlreadyExistsException.class, () -> testSuiteParser.getParsedTestSuiteLines(mockedReader, numericFields));
assertEquals("ERR031: A test suite with the name \"Name of test suite\" already exists.", ex.getMessage());
}

@Test
public void multiple_testcases_with_same_name_exists_for_a_testsuite() throws IOException {
String str1 = " TESTSUITE \"Name of test suite\"";
String str2 = " TESTCASE \"Name of test case\"";
String str3 = " TESTCASE \"Name of test case\"";

Mockito.when(mockedReader.readLine()).thenReturn(str1, str2, str3, null);

Throwable ex = assertThrows(TestCaseAlreadyExistsException.class, () -> testSuiteParser.getParsedTestSuiteLines(mockedReader, numericFields));
assertEquals("ERR032: A test case with the name \"Name of test case\" already exists in the test suite \"Name of test suite\".", ex.getMessage());
}

@Test
public void it_stores_the_name_of_the_test_suite_after_detecting_the_TESTSUITE_keyword() {
Expand Down

0 comments on commit 426fa6c

Please sign in to comment.