diff --git a/src/main/java/org/web3j/console/project/InteractiveOptions.java b/src/main/java/org/web3j/console/project/InteractiveOptions.java index 4083589..20d94c9 100644 --- a/src/main/java/org/web3j/console/project/InteractiveOptions.java +++ b/src/main/java/org/web3j/console/project/InteractiveOptions.java @@ -19,6 +19,8 @@ import java.util.Optional; import java.util.Scanner; +import org.web3j.console.project.utills.InputVerifier; + class InteractiveOptions { private final Scanner scanner; @@ -34,13 +36,21 @@ class InteractiveOptions { } protected final String getProjectName() { - print("Please enter the project name (Required Field): "); - return getUserInput(); + print("Please enter the project name (Required Field):"); + String projectName = getUserInput(); + while (!InputVerifier.classNameIsValid(projectName)) { + projectName = getUserInput(); + } + return projectName; } protected final String getPackageName() { print("Please enter the package name for your project (Required Field): "); - return getUserInput(); + String packageName = getUserInput(); + while (!InputVerifier.packageNameIsValid(packageName)) { + packageName = getUserInput(); + } + return packageName; } protected final Optional getProjectDestination() { diff --git a/src/main/java/org/web3j/console/project/ProjectCreator.java b/src/main/java/org/web3j/console/project/ProjectCreator.java index 0b55159..34fcd41 100644 --- a/src/main/java/org/web3j/console/project/ProjectCreator.java +++ b/src/main/java/org/web3j/console/project/ProjectCreator.java @@ -27,7 +27,6 @@ public class ProjectCreator { public static final String COMMAND_NEW = "new"; - static final String COMMAND_INTERACTIVE = "interactive"; final ProjectStructure projectStructure; final TemplateProvider templateProvider; diff --git a/src/test/java/org/web3j/console/project/ProjectCreatorTest.java b/src/test/java/org/web3j/console/project/ProjectCreatorTest.java index 915df96..edb157a 100644 --- a/src/test/java/org/web3j/console/project/ProjectCreatorTest.java +++ b/src/test/java/org/web3j/console/project/ProjectCreatorTest.java @@ -22,6 +22,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; +import java.util.NoSuchElementException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ import org.web3j.console.project.utills.ClassExecutor; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; public class ProjectCreatorTest extends ClassExecutor { private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); @@ -97,6 +98,52 @@ public void testWhenInteractiveAndArgumentsAreCorrect() assertEquals(0, process.exitValue()); } + @Test + public void testWhenInteractiveAndFirstInputIsInvalidClassName() + throws IOException, InterruptedException { + final String[] args = {"new"}; + Process process = + executeClassAsSubProcessAndReturnProcess( + ProjectCreator.class, Collections.emptyList(), Arrays.asList(args)) + .start(); + BufferedWriter writer = + new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); + writer.write("#$%^%#$test", 0, "#$%^%#$test".length()); + writer.newLine(); + writer.write("test", 0, "test".length()); + writer.newLine(); + writer.write("org.com", 0, "org.com".length()); + writer.newLine(); + writer.write(tempDirPath, 0, tempDirPath.length()); + writer.newLine(); + writer.close(); + process.waitFor(); + assertEquals(0, process.exitValue()); + } + + @Test + public void testWhenInteractiveAndFirstInputIsInvalidPackageName() + throws IOException, InterruptedException { + final String[] args = {"new"}; + Process process = + executeClassAsSubProcessAndReturnProcess( + ProjectCreator.class, Collections.emptyList(), Arrays.asList(args)) + .start(); + BufferedWriter writer = + new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); + writer.write("test", 0, "test".length()); + writer.newLine(); + writer.write("@#@$%@%@$#@org.com", 0, "@#@$%@%@$#@org.com".length()); + writer.newLine(); + writer.write("org.com", 0, "org.com".length()); + writer.newLine(); + writer.write(tempDirPath, 0, tempDirPath.length()); + writer.newLine(); + writer.close(); + process.waitFor(); + assertEquals(0, process.exitValue()); + } + @Test public void testWhenInteractiveAndArgumentsAreEmpty() { final String input = " \n \n \n"; @@ -104,10 +151,7 @@ public void testWhenInteractiveAndArgumentsAreEmpty() { System.setIn(inputStream); final String[] args = {"new"}; - ProjectCreator.main(args); - assertTrue( - outContent - .toString() - .contains("Please make sure the required parameters are not empty.")); + + assertThrows(NoSuchElementException.class, () -> ProjectCreator.main(args)); } } diff --git a/src/test/java/org/web3j/console/project/ProjectImporterTest.java b/src/test/java/org/web3j/console/project/ProjectImporterTest.java index bd50404..41d2a1a 100644 --- a/src/test/java/org/web3j/console/project/ProjectImporterTest.java +++ b/src/test/java/org/web3j/console/project/ProjectImporterTest.java @@ -23,6 +23,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; +import java.util.NoSuchElementException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -32,7 +33,7 @@ import org.web3j.console.project.utills.ClassExecutor; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; public class ProjectImporterTest extends ClassExecutor { private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); @@ -119,16 +120,67 @@ public void testWhenInteractiveAndArgumentsAreCorrect() assertEquals(0, process.exitValue()); } + @Test + public void testWhenInteractiveAndFirstInputIsInvalidClassName() + throws IOException, InterruptedException { + String formattedPath = + "/web3j/console/src/test/resources/Solidity".replace("/", File.separator); + final String[] args = {"import"}; + Process process = + executeClassAsSubProcessAndReturnProcess( + ProjectImporter.class, Collections.emptyList(), Arrays.asList(args)) + .start(); + BufferedWriter writer = + new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); + writer.write("#$%^%#$test", 0, "#$%^%#$test".length()); + writer.newLine(); + writer.write("test", 0, "test".length()); + writer.newLine(); + writer.write("org.com", 0, "org.com".length()); + writer.newLine(); + writer.write(formattedPath, 0, formattedPath.length()); + writer.newLine(); + writer.write(tempDirPath, 0, tempDirPath.length()); + writer.newLine(); + writer.close(); + process.waitFor(); + assertEquals(0, process.exitValue()); + } + + @Test + public void testWhenInteractiveAndFirstInputIsInvalidPackageName() + throws IOException, InterruptedException { + String formattedPath = + "/web3j/console/src/test/resources/Solidity".replace("/", File.separator); + final String[] args = {"import"}; + Process process = + executeClassAsSubProcessAndReturnProcess( + ProjectImporter.class, Collections.emptyList(), Arrays.asList(args)) + .start(); + BufferedWriter writer = + new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); + writer.write("test", 0, "test".length()); + writer.newLine(); + writer.write("@#@$%@%@$#@org.com", 0, "@#@$%@%@$#@org.com".length()); + writer.newLine(); + writer.write("org.com", 0, "org.com".length()); + writer.newLine(); + writer.write(formattedPath, 0, formattedPath.length()); + writer.newLine(); + writer.write(tempDirPath, 0, tempDirPath.length()); + writer.newLine(); + writer.close(); + process.waitFor(); + assertEquals(0, process.exitValue()); + } + @Test public void testWhenInteractiveAndArgumentsAreEmpty() { final String input = " \n \n \n \n"; inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); final String[] args = {"import"}; - ProjectImporter.main(args); - assertTrue( - outContent - .toString() - .contains("Please make sure the required parameters are not empty.")); + + assertThrows(NoSuchElementException.class, () -> ProjectImporter.main(args)); } }