diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/Block.java b/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/Block.java new file mode 100644 index 00000000..7a43207f --- /dev/null +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/Block.java @@ -0,0 +1,25 @@ +package org.openmainframeproject.cobolcheck.features.interpreter; + +import java.util.ArrayList; +import java.util.List; + +public class Block { + + private List lines; + + public Block(){ + lines = new ArrayList<>(); + } + + void addLine(String line){ + lines.add(line); + } + + List getLines(){ + return lines; + } + + void removeLines(){ + lines.clear(); + } +} diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/InterpreterController.java b/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/InterpreterController.java index adcca3c9..37055c4e 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/InterpreterController.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/InterpreterController.java @@ -27,7 +27,7 @@ public class InterpreterController { private boolean insideSectionOrParagraphMockBody; private TreeMap currentDataStructure; private final String stubTag; - private MockedSectionOrPargraph mockedSection; + private Block block; public InterpreterController(BufferedReader sourceReader) { if (sourceReader == null) { @@ -40,7 +40,7 @@ public InterpreterController(BufferedReader sourceReader) { tokenExtractor = new StringTokenizerExtractor(); currentDataStructure = new TreeMap<>(); stubTag = Config.getStubTag(); - mockedSection = new MockedSectionOrPargraph(); + block = new Block(); } //Getters for lists of specific source lines @@ -507,27 +507,27 @@ private void resetPossibleMockValues(){ possibleMockType = null; } - public List getSectionLines(){ - return mockedSection.getSectionLines(); + public List getBlockLines(){ + return block.getLines(); } - public void removeSectionLines(){ - mockedSection.removeSectionLines(); + public void removeBlockLines(){ + block.removeLines(); } - public void addMockedSectionLine(){ + public void addBlockLine(){ if(Interpreter.shouldLineBeStubbed(reader.getCurrentLine(), reader.getState())) - mockedSection.addSectionLine(StringHelper.stubLine(reader.getCurrentLine().getUnNumberedString(), stubTag)); - else mockedSection.addSectionLine(reader.getCurrentLine().getUnNumberedString()); + block.addLine(StringHelper.stubLine(reader.getCurrentLine().getUnNumberedString(), stubTag)); + else block.addLine(reader.getCurrentLine().getUnNumberedString()); } - public void addMockedSectionLine(String line){ - mockedSection.addSectionLine(line); + public void addBlockLine(String line){ + block.addLine(line); } - public void addMockedSectionLines(List lines){ + public void addBlockLines(List lines){ for (String line : lines){ - mockedSection.addSectionLine(line); + block.addLine(line); } } diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/MockedSectionOrPargraph.java b/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/MockedSectionOrPargraph.java deleted file mode 100644 index c6d5cdc8..00000000 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/interpreter/MockedSectionOrPargraph.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.openmainframeproject.cobolcheck.features.interpreter; - -import java.util.ArrayList; -import java.util.List; - -public class MockedSectionOrPargraph { - - private List sectionLines; - - public MockedSectionOrPargraph(){ - sectionLines = new ArrayList<>(); - } - - void addSectionLine(String line){ - sectionLines.add(line); - } - - List getSectionLines(){ - return sectionLines; - } - - void removeSectionLines(){ - sectionLines.clear(); - } -} diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/CobolGenerator.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/CobolGenerator.java index d12be6c8..5c266f2f 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/CobolGenerator.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/CobolGenerator.java @@ -75,7 +75,7 @@ static void addStartAndEndTags(List lines){ lines.add(getInjectEndTagComment()); } - static List generateWhenOtherMockLines(String identifier, String type, List commentLines, List bodyLines){ + static List generateWhenOtherLines(String identifier, String type, List commentLines, List bodyLines){ List lines = new ArrayList<>(); if(type.equals(Constants.SECTION_TOKEN)) lines.add(String.format(WHEN_OTHER_SECTION_HEADER_FORMAT, identifier)); diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/Mock.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/Mock.java index cc669e65..ba5717d0 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/Mock.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/Mock.java @@ -166,7 +166,4 @@ private String getArgumentText(){ return combinedArgs.substring(0, combinedArgs.length() - 2); } - public void addLinesWithoutMoving(List lines) { - this.lines.addAll(lines); - } } diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/MockGenerator.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/MockGenerator.java index 895967eb..eb188db6 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/MockGenerator.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/MockGenerator.java @@ -156,36 +156,6 @@ private List generateParagraphsForMock(Mock mock, boolean withComment){ body.addAll(mock.getLines()); return CobolGenerator.generateParagraphLines(mock.getGeneratedMockIdentifier(), comments, body); } - - /**Generates the lines for Paragraphs based on mocks, - * for each mock in a given list. - * @param mocks - The mocks to generate Paragraphs for - * @return The generated lines - */ - List generateWhenOtherMock(Mock mock, boolean withComments){ - List lines = new ArrayList<>(); - lines.addAll(CobolGenerator.generateCommentBlock("Paragraphs called when mocking")); - lines.addAll(generateParagraphsForWhenOtherMock(mock, withComments)); - lines.add(""); - return lines; - } - - String generateWhenOtherMockPerformCall(Mock mock){ - return String.format(performFormat, mock.getGeneratedMockIdentifier()); - } - - private List generateParagraphsForWhenOtherMock(Mock mock, boolean withComment){ - List comments = new ArrayList<>(); - if (withComment){ - for (String line : mock.getCommentText()){ - comments.add(StringHelper.commentOutLine(line)); - } - } - List body = new ArrayList<>(); - body.addAll(mock.getLines()); - return CobolGenerator.generateWhenOtherMockLines(mock.getGeneratedMockIdentifier(),mock.getType(), comments, body); - } - } diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParser.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParser.java index 69502c34..1006c435 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParser.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParser.java @@ -75,6 +75,7 @@ public class TestSuiteParser { private String currentTestCaseName = Constants.EMPTY_STRING; private int testCaseNumber = 0; private boolean expectNumericCompare; + private int whenOtherNumber=0; // Lines inserted into the test program private static final String COBOL_PERFORM_INITIALIZE = " PERFORM %sINITIALIZE"; @@ -1160,14 +1161,14 @@ public String getCurrentFieldName() { return currentFieldName; } - public Mock getWhenOtherMock(String type, List lines, String itdentifier, boolean withComments){ - mockNumber += 1; - Mock mock = new Mock(currentTestSuiteName ,currentTestCaseName, testSuiteNumber, testCaseNumber,mockNumber); - mock.addLinesWithoutMoving(lines); - mock.setScope(MockScope.Local); - mock.setType(type); - mock.setIdentifier(itdentifier); - return mock; + public WhenOther getWhenOtherBlock(String type, List lines, String itdentifier, boolean withComments){ + WhenOther whenOther = new WhenOther(currentTestSuiteName ,currentTestCaseName, testSuiteNumber, testCaseNumber,whenOtherNumber); + whenOther.addLines(lines); + whenOther.setScope(MockScope.Local); + whenOther.setType(type); + whenOther.setIdentifier(itdentifier); + whenOtherNumber += 1; + return whenOther; } diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParserController.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParserController.java index c10a6a97..32b6be9f 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParserController.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/TestSuiteParserController.java @@ -21,6 +21,7 @@ public class TestSuiteParserController { private BeforeAfterRepo beforeAfterRepo; private MockGenerator mockGenerator; private BufferedReader testSuiteReader; + private WhenOtherGenerator whenOtherGenerator; private TestSuiteErrorLog testSuiteErrorLog; @@ -54,6 +55,7 @@ public TestSuiteParserController(String testFileNames) { testSuiteParser = new TestSuiteParser(new KeywordExtractor(), mockRepository, beforeAfterRepo, testSuiteErrorLog); mockGenerator = new MockGenerator(); testCodePrefix = Config.getTestCodePrefix(); + whenOtherGenerator = new WhenOtherGenerator(); } //Used for testing only @@ -314,17 +316,15 @@ public void prepareNextParse() { Config.setDecimalPointIsCommaFromFile(); } - public Mock getWhenOtherMock(String type, List lines, String identifier, boolean withComments){ - // Local scope - return testSuiteParser.getWhenOtherMock(type,lines, identifier, withComments); - } - - public String generateWhenOtherMockPerformCall(Mock mock){ - return mockGenerator.generateWhenOtherMockPerformCall(mock); - } - - public List generateWhenOtherMock(Mock mock, boolean withComments){ - return mockGenerator.generateWhenOtherMock(mock, withComments); + public List generateWhenOtherBlock(String type, List blocklines, String sourceLine, String identifier, boolean withComments) throws IOException{ + List lines = new ArrayList<>(); + WhenOther whenOther = testSuiteParser.getWhenOtherBlock(type, blocklines, identifier, true); + lines.add(whenOtherGenerator.generateWhenOtherCall(whenOther)); + lines.addAll(this.getEndEvaluateLine()); + lines.add(sourceLine); + lines.add(""); + lines.addAll(whenOtherGenerator.generateWhenOther(whenOther, withComments)); + return lines; } diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOther.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOther.java new file mode 100644 index 00000000..a27012e2 --- /dev/null +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOther.java @@ -0,0 +1,78 @@ +package org.openmainframeproject.cobolcheck.features.testSuiteParser; + + +import java.util.ArrayList; +import java.util.List; + +import org.openmainframeproject.cobolcheck.services.Config; +import org.openmainframeproject.cobolcheck.services.StringHelper; + +public class WhenOther { + + private String identifier; + private String type; + private List lines; + private MockScope scope; + private String testSuiteName; + private String testCaseName; + private int testSuiteNumber; + private int testCaseNumber; + private int mockNumber; + + + public WhenOther(String testSuiteName, String testCaseName, int testSuiteNumber, int testCaseNumber, int mockNumber) { + this.testSuiteName = testSuiteName; + this.testCaseName = testCaseName; + this.testSuiteNumber = testSuiteNumber; + this.testCaseNumber = testCaseNumber; + this.mockNumber = mockNumber; + lines = new ArrayList<>(); + } + + public String getGeneratedWhenOtherIdentifier(){ + return Config.getTestCodePrefix() + getGeneratedWhenOtherIdentifierRoot(); + } + + public String getGeneratedWhenOtherIdentifierRoot(){ + return testSuiteNumber + "-" + testCaseNumber + "-" + mockNumber + "-WO"; + } + + public void addLines(List lines) { + this.lines.addAll(lines); + } + + public void setScope(MockScope scope) { + this.scope = scope; + } + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return this.type; + } + + public List getLines() { + return this.lines; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + + public List getCommentText(){ + List newLines = new ArrayList<>(); + newLines.add(" *****************************************************************"); + newLines.add( "When other block of: " + type + ": " + identifier); + newLines.add("In testsuite: " + testSuiteName); + if (scope == MockScope.Local){ + newLines.add("In testcase: " + testCaseName); + } + newLines.add(" *****************************************************************"); + return newLines; + + } + +} diff --git a/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOtherGenerator.java b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOtherGenerator.java new file mode 100644 index 00000000..60b5bdf6 --- /dev/null +++ b/src/main/java/org/openmainframeproject/cobolcheck/features/testSuiteParser/WhenOtherGenerator.java @@ -0,0 +1,37 @@ +package org.openmainframeproject.cobolcheck.features.testSuiteParser; + +import java.util.ArrayList; +import java.util.List; + +import org.openmainframeproject.cobolcheck.services.StringHelper; + +public class WhenOtherGenerator { + private final String performFormat = " PERFORM %s"; + + + public String generateWhenOtherCall(WhenOther whenOther) { + return String.format(performFormat, whenOther.getGeneratedWhenOtherIdentifier()); + } + + List generateWhenOther(WhenOther whenOther, boolean withComments){ + List lines = new ArrayList<>(); + lines.addAll(CobolGenerator.generateCommentBlock("WhenOther block called when mocking")); + lines.addAll(generateParagraphsForWhenOther(whenOther, withComments)); + lines.add(""); + return lines; + } + + private List generateParagraphsForWhenOther(WhenOther whenOther, boolean withComments){ + List comments = new ArrayList<>(); + if (withComments){ + for (String line : whenOther.getCommentText()){ + comments.add(StringHelper.commentOutLine(line)); + } + } + List body = new ArrayList<>(); + body.addAll(whenOther.getLines()); + return CobolGenerator.generateWhenOtherLines(whenOther.getGeneratedWhenOtherIdentifier(), whenOther.getType(), comments, body); + } + + +} diff --git a/src/main/java/org/openmainframeproject/cobolcheck/workers/Generator.java b/src/main/java/org/openmainframeproject/cobolcheck/workers/Generator.java index a8b382e1..1122b2d2 100644 --- a/src/main/java/org/openmainframeproject/cobolcheck/workers/Generator.java +++ b/src/main/java/org/openmainframeproject/cobolcheck/workers/Generator.java @@ -143,8 +143,8 @@ private String tryInsertEndEvaluateAtMockedCompomentEnd(String sourceLine) throw if (interpreter.isInsideSectionOrParagraphMockBody()){ if (interpreter.isCurrentLineEndingSectionOrParagraph()){ if (interpreter.canWriteEndEvaluateBeforeCurrentLine()){ - writeWhenOtherMockedSection(sourceLine); - interpreter.removeSectionLines(); + writeWhenOtherBlock(sourceLine); + interpreter.removeBlockLines(); interpreter.setInsideSectionOrParagraphMockBody(false); return ""; } @@ -206,9 +206,9 @@ private void processingAfterEchoingSourceLineToOutput() throws IOException { List arguments = interpreter.getPossibleMockArgs(); if (testSuiteParserController.mockExistsFor(identifier, type, arguments)){ if(interpreter.isInsideSectionOrParagraphMockBody()){ - interpreter.addMockedSectionLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments)); + interpreter.addBlockLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments)); if (type.equals(Constants.CALL_TOKEN)){ - interpreter.addMockedSectionLine(" CONTINUE"); + interpreter.addBlockLine(" CONTINUE"); } }else writerController.writeLines(testSuiteParserController.generateMockPerformCalls(identifier, type, arguments)); if (type.equals(Constants.SECTION_TOKEN) || type.equals(Constants.PARAGRAPH_TOKEN)){ @@ -226,19 +226,14 @@ private void closeReadersAndWriters(String programName) { writerController.closeWriter(programName); } - private void writeWhenOtherMockedSection(String sourceLine) throws IOException{ - Mock mock = testSuiteParserController.getWhenOtherMock(currentMockType, interpreter.getSectionLines(), currentIdentifier, true); - writerController.writeLine(testSuiteParserController.generateWhenOtherMockPerformCall(mock)); - writerController.writeLines(testSuiteParserController.getEndEvaluateLine()); - writerController.writeLine(sourceLine); - writerController.writeLine(""); - writerController.writeLines(testSuiteParserController.generateWhenOtherMock(mock,true)); + private void writeWhenOtherBlock(String sourceLine) throws IOException{ + writerController.writeLines(testSuiteParserController.generateWhenOtherBlock(currentMockType, interpreter.getBlockLines(), sourceLine, currentIdentifier, true)); } private void echoingSourceLineToOutput(String sourceLine){ try{ if(interpreter.isInsideSectionOrParagraphMockBody()){ - interpreter.addMockedSectionLine(); + interpreter.addBlockLine(); } sourceLine = tryInsertEndEvaluateAtMockedCompomentEnd(sourceLine); if(!interpreter.isInsideSectionOrParagraphMockBody()){