Skip to content

Commit

Permalink
Merge pull request #13 from AlexandrouR/validateParams
Browse files Browse the repository at this point in the history
Validate user input when it is entered
  • Loading branch information
cfelde authored Nov 4, 2019
2 parents 116b98a + ccea58b commit 9f9c888
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 16 deletions.
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();
}
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));
}
}

0 comments on commit 9f9c888

Please sign in to comment.