Skip to content

Commit

Permalink
Add negative test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DimuthuMadushan committed Nov 9, 2023
1 parent 2c4a288 commit 7677800
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
public class CopybookTypeGenerationException extends Exception {
private final Diagnostic diagnostic;

public CopybookTypeGenerationException(DiagnosticMessages diagnosticMessage, Location location) {
super(diagnosticMessage.getDescription());
this.diagnostic = createDiagnostic(diagnosticMessage, location);
}

public CopybookTypeGenerationException(DiagnosticMessages diagnosticMessage, Location location, String... args) {
super(generateDescription(diagnosticMessage, args));
this.diagnostic = createDiagnostic(diagnosticMessage, location, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NodeFactory;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.RecordFieldNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
Expand All @@ -50,7 +49,6 @@
import static io.ballerina.compiler.syntax.tree.NodeFactory.createSimpleNameReferenceNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createTypeDefinitionNode;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.PUBLIC_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.QUESTION_MARK_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.SEMICOLON_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.TYPE_KEYWORD;
import static io.ballerina.tools.copybook.generator.CodeGeneratorUtils.createImportDeclarationNodes;
Expand All @@ -75,19 +73,6 @@ public String generateSourceCode() throws FormatterException {
return Formatter.format(generatedSyntaxTree);
}

public List<io.ballerina.compiler.syntax.tree.Node> addRecordFields(List<CopybookNode> fields) {
List<io.ballerina.compiler.syntax.tree.Node> recordFieldList = new ArrayList<>();
for (CopybookNode fieldNode : fields) {
String fieldNameStr = CodeGeneratorUtils.escapeIdentifier(fieldNode.getName().trim());
IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(fieldNameStr);
TypeDescriptorNode typeDescriptorNode = createSimpleNameReferenceNode(createIdentifierToken(fieldNameStr));
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null,
typeDescriptorNode, fieldName, createToken(QUESTION_MARK_TOKEN), createToken(SEMICOLON_TOKEN));
recordFieldList.add(recordFieldNode);
}
return recordFieldList;
}

public TypeDefinitionNode generateTypeDefNode(CopybookNode node) {
IdentifierToken typeName = AbstractNodeFactory.createIdentifierToken(CodeGeneratorUtils.getValidName(
node.getName().trim()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.ballerina.tools.copybook.cmd.TestUtils.WHITESPACE_REGEX;
import static io.ballerina.tools.copybook.cmd.TestUtils.assertStringsWithoutWhiteSpace;
import static io.ballerina.tools.copybook.cmd.TestUtils.readContentWithFormat;

public class CopybookCmdTest extends CopybookTest {
Expand All @@ -45,8 +45,55 @@ public void testCopybookHelpFlag() throws IOException {
new CommandLine(copybookCmd).parseArgs(args);
copybookCmd.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("ballerina-copybook - Generate Ballerina types for given cobol copybook\n" +
" definitions"));
String message = "ballerina-copybook - Generate Ballerina types for given cobol copybook definitions";
assertStringsWithoutWhiteSpace(output, message);
}

@Test(description = "Test copybook command without arguments")
public void testCopybookCmdUsage() throws IOException {
String[] args = {""};
CopybookCmd copybookCmd = new CopybookCmd(printStream, tmpDir, false);
new CommandLine(copybookCmd).parseArgs(args);
copybookCmd.execute();
String output = readOutput(true);
String message = "ballerina-copybook - Generate Ballerina types for given cobol copybook definitions bal" +
"copybook [-i | --input] <copybook-definition-file-path> [-o | --output] <output-location>";
assertStringsWithoutWhiteSpace(output, message);
}

@Test(description = "Test copybook command with invalid file extension")
public void testCopybookWithInvalidFileExtension() throws IOException {
String[] args = {"-i", resourceDir.resolve("expectedGenTypes/copybook.bal").toString()};
CopybookCmd copybookCmd = new CopybookCmd(printStream, tmpDir, false);
new CommandLine(copybookCmd).parseArgs(args);
copybookCmd.execute();
String output = readOutput(true);
String message = "Copybook tool support only the Copybook definition files with .cpy or .cob extension." +
"Please provide the path of the input file with -i or --input flag.";
assertStringsWithoutWhiteSpace(output, message);
}

@Test(description = "Test copybook command with invalid file")
public void testCopybookWithInvalidFile() throws IOException {
String[] args = {"-i", resourceDir.resolve("expectedGenTypes/cob.cpy").toString()};
CopybookCmd copybookCmd = new CopybookCmd(printStream, tmpDir, false);
new CommandLine(copybookCmd).parseArgs(args);
copybookCmd.execute();
String output = readOutput(true);
String message = "The copybook definition file does not exist in the given path";
assertStringsWithoutWhiteSpace(output, message);
}

@Test(description = "Test copybook command without arguments")
public void testCopybookWithoutArguments() throws IOException {
String[] args = {"-i"};
CopybookCmd copybookCmd = new CopybookCmd(printStream, tmpDir, false);
new CommandLine(copybookCmd).parseArgs(args);
copybookCmd.execute();
String output = readOutput(true);
String message = "The input file path argument is missing. Please provide the path of the Copybook definition" +
" file with -i or --input flag.";
assertStringsWithoutWhiteSpace(output, message);
}

@Test(description = "Test copybook command with help flag")
Expand Down Expand Up @@ -108,9 +155,6 @@ public void testTypeGenerationForInvalidSchema() throws IOException {
String message = "Copybook types generation failed: " +
"Error at line 3, column 28: Unsupported picture string I(30) found in copybook schema\n" +
"Error at line 4, column 28: Unsupported picture string I(10) found in copybook schema";
// Replace following as Windows environment requirement
output = (output.trim()).replaceAll(WHITESPACE_REGEX, "");
message = (message.trim()).replaceAll(WHITESPACE_REGEX, "");
Assert.assertTrue(output.contains(message));
assertStringsWithoutWhiteSpace(output, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package io.ballerina.tools.copybook.cmd;

import org.testng.Assert;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -37,4 +39,11 @@ protected static String readContentWithFormat(Path filePath) throws IOException
schemaLines.close();
return schemaContent;
}

protected static void assertStringsWithoutWhiteSpace(String output, String expected) {
// Replace following as Windows environment requirement
String actualString = output.replaceAll(WHITESPACE_REGEX, "");
String expectedString = expected.replaceAll(WHITESPACE_REGEX, "");
Assert.assertTrue(actualString.contains(expectedString));
}
}

0 comments on commit 7677800

Please sign in to comment.