Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate user input when it is entered #13

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/main/java/org/web3j/console/project/InteractiveOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
cfelde marked this conversation as resolved.
Show resolved Hide resolved
}
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<String> getProjectDestination() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 50 additions & 6 deletions src/test/java/org/web3j/console/project/ProjectCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -97,17 +98,60 @@ 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";
inputStream = new ByteArrayInputStream(input.getBytes());
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));
}
}
64 changes: 58 additions & 6 deletions src/test/java/org/web3j/console/project/ProjectImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}
}