diff --git a/console/src/main/java/org/web3j/console/project/ProjectCreator.java b/console/src/main/java/org/web3j/console/project/ProjectCreator.java index e38464a00..19ba0f795 100644 --- a/console/src/main/java/org/web3j/console/project/ProjectCreator.java +++ b/console/src/main/java/org/web3j/console/project/ProjectCreator.java @@ -53,7 +53,7 @@ public class ProjectCreator { public static void main(String[] args) { if (args.length > 0 && args[0].equals(COMMAND_NEW)) { args = tail(args); - if (args.length > 0 && args[0].equals(COMMAND_INTERACTIVE)) { + if (args.length == 0) { final InteractiveOptions options = new InteractiveOptions(); final List stringOptions = new ArrayList<>(); stringOptions.add("-n"); diff --git a/console/src/main/java/org/web3j/console/project/ProjectImporter.java b/console/src/main/java/org/web3j/console/project/ProjectImporter.java index 41ca67b76..76ae905f0 100644 --- a/console/src/main/java/org/web3j/console/project/ProjectImporter.java +++ b/console/src/main/java/org/web3j/console/project/ProjectImporter.java @@ -41,7 +41,7 @@ public ProjectImporter( public static void main(String[] args) { if (args.length > 0 && args[0].equals(COMMAND_IMPORT)) { args = tail(args); - if (args.length > 0 && args[0].equals(COMMAND_INTERACTIVE)) { + if (args.length == 0) { final InteractiveImporter options = new InteractiveImporter(); final List stringOptions = new ArrayList<>(); stringOptions.add("-n"); diff --git a/console/src/test/java/org/web3j/console/project/ProjectCreatorTest.java b/console/src/test/java/org/web3j/console/project/ProjectCreatorTest.java index 3aa2d4205..887c875d4 100644 --- a/console/src/test/java/org/web3j/console/project/ProjectCreatorTest.java +++ b/console/src/test/java/org/web3j/console/project/ProjectCreatorTest.java @@ -12,20 +12,26 @@ */ package org.web3j.console.project; +import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.util.Arrays; +import java.util.Collections; import org.junit.Before; import org.junit.Test; import picocli.CommandLine; -import org.web3j.console.WalletTester; +import org.web3j.console.project.utills.ClassExecutor; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class ProjectCreatorTest extends WalletTester { +public class ProjectCreatorTest extends ClassExecutor { private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private InputStream inputStream; @@ -48,43 +54,53 @@ public void testWhenCorrectArgsArePassedProjectStructureCreated() { } @Test - public void runTestWhenArgumentsAreEmpty() { - final String[] args = {"", ""}; - ProjectCreator.main(args); - assertTrue( - errContent - .toString() - .contains( - "Missing required options [--package=, --project name=]")); + public void testWithPicoCliWhenArgumentsAreCorrect() throws IOException, InterruptedException { + final String[] args = {"new", "-p", "org.com", "-n", "Test", "-o" + tempDirPath}; + int extiCode = + executeClassAsSubProcessAndReturnProcess( + ProjectCreator.class, Collections.emptyList(), Arrays.asList(args)) + .inheritIO() + .start() + .waitFor(); + assertEquals(0, extiCode); } @Test - public void runTestWhenArgumentsAreNotEmpty() { - final String[] args = {"new", "-p", "org.com", "-n", "Test", "-o" + tempDirPath}; + public void testWithPicoCliWhenArgumentsAreEmpty() { + final String[] args = {"new", "-n= ", "-p= "}; ProjectCreator.main(args); - assertTrue( - outContent - .toString() - .contains( - "Project created with name: Test at location: /var/folders/6x/vzg_hr7x4nz2z043t2_wtjsc0000gn/T/TempFileProvider3473379777787806210/Test")); + assertEquals( + outContent.toString(), "Please make sure the required parameters are not empty.\n"); } @Test - public void createNewProjectInteractive() { - final String input = "Test\norg.com\n" + tempDirPath + "\n"; - inputStream = new ByteArrayInputStream(input.getBytes()); - System.setIn(inputStream); - final String[] args = {"new", "interactive"}; - ProjectCreator.main(args); - assertTrue(outContent.toString().contains("Project created with name:")); + public void testWhenInteractiveAndArgumentsAreCorrect() + 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(tempDirPath, 0, tempDirPath.length()); + writer.newLine(); + writer.close(); + process.waitFor(); + assertEquals(0, process.exitValue()); } @Test - public void createNewProjectInteractiveWhenArgsAreEmpty() { + public void testWhenInteractiveAndArgumentsAreEmpty() { final String input = " \n \n \n"; inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); - final String[] args = {"new", "interactive"}; + + final String[] args = {"new"}; ProjectCreator.main(args); assertTrue( outContent diff --git a/console/src/test/java/org/web3j/console/project/ProjectImporterTest.java b/console/src/test/java/org/web3j/console/project/ProjectImporterTest.java index 1c41eaad6..ab76b6e54 100644 --- a/console/src/test/java/org/web3j/console/project/ProjectImporterTest.java +++ b/console/src/test/java/org/web3j/console/project/ProjectImporterTest.java @@ -12,22 +12,27 @@ */ package org.web3j.console.project; +import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.util.Arrays; +import java.util.Collections; import org.junit.Before; import org.junit.Test; import picocli.CommandLine; -import org.web3j.TempFileProvider; +import org.web3j.console.project.utills.ClassExecutor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class ProjectImporterTest extends TempFileProvider { +public class ProjectImporterTest extends ClassExecutor { private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private InputStream inputStream; @@ -50,18 +55,8 @@ public void testWhenCorrectArgsArePassedProjectStructureCreated() { } @Test - public void runTestWhenArgumentsAreEmpty() { - final String[] args = {"", "", ""}; - ProjectImporter.main(args); - assertTrue( - errContent - .toString() - .contains( - "Missing required options [--solidity path=, --package=, --project name=]")); - } + public void testWithPicoCliWhenArgumentsAreCorrect() throws IOException, InterruptedException { - @Test - public void runTestWhenArgumentsAreNotEmpty() { final String formattedSolidityTestProject = File.separator + "web3j" @@ -79,28 +74,54 @@ public void runTestWhenArgumentsAreNotEmpty() { final String[] args = { "-p=org.com", "-n=Test", "-o=" + tempDirPath, "-s=" + formattedSolidityTestProject }; + int exitCode = + executeClassAsSubProcessAndReturnProcess( + ProjectImporter.class, Collections.emptyList(), Arrays.asList(args)) + .inheritIO() + .start() + .waitFor(); + assertEquals(0, exitCode); + } + + @Test + public void testWithPicoCliWhenArgumentsAreEmpty() { + final String[] args = {"import", "-p= ", "-n= ", "-s= "}; ProjectImporter.main(args); - assertTrue(outContent.toString().contains("Project created with name:")); + assertEquals( + outContent.toString(), "Please make sure the required parameters are not empty.\n"); } @Test - public void createImportProjectInteractive() { + public void testWhenInteractiveAndArgumentsAreCorrect() + throws IOException, InterruptedException { String formattedPath = "/web3j/console/src/test/resources/Solidity".replaceAll("/", File.separator); - final String input = "Test\norg.com\n" + formattedPath + "\n" + tempDirPath + "\n"; - inputStream = new ByteArrayInputStream(input.getBytes()); - System.setIn(inputStream); - final String[] args = {"import", "interactive"}; - ProjectImporter.main(args); - assertTrue(outContent.toString().contains("Project created with name:")); + 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(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 runTestWhenArgumentsAreNewInteractive() { + public void testWhenInteractiveAndArgumentsAreEmpty() { final String input = " \n \n \n \n"; inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); - final String[] args = {"import", "interactive"}; + final String[] args = {"import"}; ProjectImporter.main(args); assertTrue( outContent diff --git a/console/src/test/java/org/web3j/console/project/ProjectTest.java b/console/src/test/java/org/web3j/console/project/ProjectTest.java index 9b610185d..334d67ad1 100644 --- a/console/src/test/java/org/web3j/console/project/ProjectTest.java +++ b/console/src/test/java/org/web3j/console/project/ProjectTest.java @@ -13,7 +13,6 @@ package org.web3j.console.project; import java.io.File; -import java.io.IOException; import org.junit.Before; import org.junit.Test; @@ -23,27 +22,27 @@ import static org.junit.Assert.assertTrue; public class ProjectTest extends TempFileProvider { - private final ProjectStructure projectStructure = - new ProjectStructure(tempDirPath, "test", "test"); - private final TemplateProvider templateProviderNew = - new TemplateProvider.Builder() - .loadGradlewBatScript("gradlew.bat.template") - .loadGradlewScript("gradlew.template") - .loadMainJavaClass("Template.java") - .loadGradleBuild("build.gradle.template") - .loadGradleSettings("settings.gradle.template") - .loadGradlewWrapperSettings("gradlew-wrapper.properties.template") - .loadGradleJar("gradle-wrapper.jar") - .loadSolidityGreeter("Greeter.sol") - .withPackageNameReplacement(s -> s.replaceAll("", "test")) - .withProjectNameReplacement(s -> s.replaceAll("", "test")) - .build(); - - public ProjectTest() throws IOException {} + private ProjectStructure projectStructure; + private TemplateProvider templateProviderNew; @Before - public void setUpProject() { - new Project.Builder() + public void setUpProject() throws Exception { + setUp(); + projectStructure = new ProjectStructure(tempDirPath, "test", "test"); + templateProviderNew = + new TemplateProvider.Builder() + .loadGradlewBatScript("gradlew.bat.template") + .loadGradlewScript("gradlew.template") + .loadMainJavaClass("Template.java") + .loadGradleBuild("build.gradle.template") + .loadGradleSettings("settings.gradle.template") + .loadGradlewWrapperSettings("gradlew-wrapper.properties.template") + .loadGradleJar("gradle-wrapper.jar") + .loadSolidityGreeter("Greeter.sol") + .withPackageNameReplacement(s -> s.replaceAll("", "test")) + .withProjectNameReplacement(s -> s.replaceAll("", "test")) + .build(); + Project.builder() .withTemplateProvider(templateProviderNew) .withProjectStructure(projectStructure) .build(); diff --git a/console/src/test/java/org/web3j/console/project/utills/ClassExecutor.java b/console/src/test/java/org/web3j/console/project/utills/ClassExecutor.java new file mode 100644 index 000000000..30aad9396 --- /dev/null +++ b/console/src/test/java/org/web3j/console/project/utills/ClassExecutor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2019 Web3 Labs LTD. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.console.project.utills; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.web3j.TempFileProvider; + +public class ClassExecutor extends TempFileProvider { + public ProcessBuilder executeClassAsSubProcessAndReturnProcess( + Class classToExecute, List jvmArgs, List args) + throws IOException, InterruptedException { + String javaHome = System.getProperty("java.home"); + String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; + String classPath = System.getProperty("java.class.path"); + String className = classToExecute.getName(); + + List command = new ArrayList<>(); + command.add(javaBin); + command.addAll(jvmArgs); + command.add("-cp"); + command.add(classPath); + command.add(className); + command.addAll(args); + return new ProcessBuilder(command); + } +}