X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2Fparameterparser%2FParserTest.java;h=44cd67ef063b44b7cc11d1aef1dd431c0dbf067f;hb=b91077678c2a0af17a2f852c0b3901813837117a;hp=c5fcda45d64cc0a4f17e987f4a01a202b69fe34b;hpb=9bf004ce4e9b5edff36c65fcc8cc0f303390d7fc;p=svjatoslav_commons.git diff --git a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java index c5fcda4..44cd67e 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -1,18 +1,18 @@ /* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. + * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.commons.commandline.parameterparser; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter; import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; import org.junit.Before; import org.junit.Test; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + import static org.junit.Assert.*; public class ParserTest { @@ -20,36 +20,49 @@ public class ParserTest { Parser parser; @Before - public void setUp() throws Exception { + public void setUp() { parser = new Parser(); } @Test - public void testParse() { + public void testParse() throws IOException { // define allowed parameters - final StringParameter helpParameter = parser - .add(new StringParameter("Show help screen")) - .addAliases("--help", "-h").setMandatory(); + final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") + .addAliases("--help", "-h").setMandatory()); + + final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) + .addAliases("--compile", "-c"); + + FileParameter fileParameter = parser.add(new FileParameter("Input file") + .addAliases("-i").mustExist()); - final StringParameter compileParameter = parser.add( - new StringParameter("Compile code")).addAliases("--compile", - "-c"); + createTemporaryFile(); // check help generation parser.showHelp(); // parse arguments - parser.parse(new String[]{"--help", "section"}); + parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); // --help was in the arguments - assertTrue(helpParameter.isParameterSpecified()); + assertTrue(helpParameter.isSpecified()); // compile was not present - assertFalse(compileParameter.isParameterSpecified()); + assertFalse(compileParameter.isSpecified()); // validate that help argument was "section" assertEquals("section", helpParameter.getValue()); + assertTrue(fileParameter.isSpecified()); + assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); + + } + + private void createTemporaryFile() throws IOException { + File fileWithSpaces = new File("/tmp/file with spaces"); + FileWriter writer = new FileWriter(fileWithSpaces); + writer.write("test"); + writer.close(); } }