X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2Fparameterparser%2FParserTest.java;h=03b7c19b052386dcd730542413686cf591c0b36f;hb=965adace2b47642e2ca84f499cc32683c12059c6;hp=0e654b4e8c16c2406b47edc6acb164d14344a4f7;hpb=26f09b1ebcafae67855b55ad588d5332a107d202;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 0e654b4..03b7c19 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -1,65 +1,73 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public License - * as published by the Free Software Foundation. + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. */ 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.arguments.IntegerArgument; +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 { - @Before - public void setUp() throws Exception { - parser = new Parser(); - } + // define allowed parameters + final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") + .addAliases("--help", "-h").setMandatory()); - @Test - public void testParse() { + final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) + .addAliases("--compile", "-c"); - // define allowed parameters - final Parameter helpParameter = new Parameter("Show help screen", "-h", - "--help"); - parser.addParameter(helpParameter); + FileParameter fileParameter = parser.add(new FileParameter("Input file") + .addAliases("-i").mustExist()); - final Parameter compileParameter = new Parameter("Compile code", "-c", - "--compile"); - parser.addParameter(compileParameter); + createTemporaryFile(); - final Parameter bitrateParameter = new Parameter(false, true, false, - new IntegerArgument(), "Target bitrate", "-b", "--bitrate"); - parser.addParameter(bitrateParameter); + // check help generation + parser.showHelp(); - // check help generation - parser.showHelp(); + // parse arguments + parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); - // parse arguments - parser.parse(new String[] { "--help", "-b", "123" }); + // --help was in the arguments + assertTrue(helpParameter.isSpecified()); - // --help was in the arguments - assertTrue(helpParameter.isParameterSpecified()); + // compile was not present + assertFalse(compileParameter.isSpecified()); - // compile was not present - assertFalse(compileParameter.isParameterSpecified()); + // validate that help argument was "section" + assertEquals("section", helpParameter.getValue()); - // bitrate is given as 123 - assertTrue(bitrateParameter.isParameterSpecified()); + assertTrue(fileParameter.isSpecified()); + assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); - assertEquals(123, (int) bitrateParameter.getArgumentsAsIntegers() - .get(0)); - } + } + private void createTemporaryFile() throws IOException { + File fileWithSpaces = new File("/tmp/file with spaces"); + FileWriter writer = new FileWriter(fileWithSpaces); + writer.write("test"); + writer.close(); + } }