X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2Fparameterparser%2FParserTest.java;h=abbf10230df92933461964f60a28b073b8a4bfb0;hb=443c6a564efa9f5868fd81c0db23a480cbe3cab4;hp=e84a012cea96d065eba1a09fada8fec2a7c0391f;hpb=b34ba4499cfbca09bc794a810e460bf1c86dcd34;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 e84a012..abbf102 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -1,7 +1,7 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2018, 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. @@ -9,49 +9,65 @@ package eu.svjatoslav.commons.commandline.parameterparser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - +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 eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.Assert.*; public class ParserTest { - Parser parser; + Parser parser; + + @Before + public void setUp() { + parser = new Parser(); + } + + @Test + public void testParse() throws IOException { + + // define allowed parameters + 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"); - @Before - public void setUp() throws Exception { - parser = new Parser(); - } + FileParameter fileParameter = parser.add(new FileParameter("Input file") + .addAliases("-i").mustExist()); - @Test - public void testParse() { + createTemporaryFile(); - // define allowed parameters - final StringParameter helpParameter = parser - .createStringParameter("Show help screen") - .addAliases("--help", "-h").setMandatory(); + // check help generation + parser.showHelp(); - final StringParameter compileParameter = parser.createStringParameter( - "Compile code").addAliases("--compile", "-c"); + // parse arguments + parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); - // check help generation - parser.showHelp(); + // --help was in the arguments + assertTrue(helpParameter.isSpecified()); - // parse arguments - parser.parse(new String[] { "--help", "section" }); + // compile was not present + assertFalse(compileParameter.isSpecified()); - // --help was in the arguments - assertTrue(helpParameter.isParameterSpecified()); + // validate that help argument was "section" + assertEquals("section", helpParameter.getValue()); - // compile was not present - assertFalse(compileParameter.isParameterSpecified()); + assertTrue(fileParameter.isSpecified()); + assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); - // validate that help argument was "section" - assertEquals("section", helpParameter.getValue()); + } - } + private void createTemporaryFile() throws IOException { + File fileWithSpaces = new File("/tmp/file with spaces"); + FileWriter writer = new FileWriter(fileWithSpaces); + writer.write("test"); + writer.close(); + } }