package org.djutils.cli; import static org.junit.Assert.assertEquals; import org.junit.Test; import picocli.CommandLine.Command; import picocli.CommandLine.Option; /** * Program to test the CLI.
*
* Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See * for project information www.simulation.tudelft.nl. The * source code and binary code of this software is proprietary information of Delft University of Technology. * @author Alexander Verbraeck */ public class TestCLI { /** */ @Command(description = "Test program for CLI", name = "Program", mixinStandardHelpOptions = true, version = "1.0") public static class Options implements Checkable { /** */ @Option(names = {"-p", "--port"}, description = "Internet port to use", defaultValue = "80") private int port; /** @return the port number */ public int getPort() { return this.port; } @Override public void check() throws Exception { if (this.port <= 0 || this.port > 65535) { throw new Exception("Port should be between 1 and 65535"); } } } /** * Test the CliUtil methods. * @throws CliException on error * @throws IllegalAccessException on error * @throws IllegalArgumentException on error * @throws NoSuchFieldException on error */ @Test public void testCli() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException { String[] args = new String[] {"-p", "1200"}; Options options = new Options(); CliUtil.execute(options, args); assertEquals(1200, options.getPort()); assertEquals("1.0", options.getClass().getAnnotation(Command.class).version()[0]); assertEquals("Program", options.getClass().getAnnotation(Command.class).name()); assertEquals("Test program for CLI", options.getClass().getAnnotation(Command.class).description()[0]); args = new String[] {}; options = new Options(); CliUtil.execute(options, args); assertEquals(80, options.getPort()); args = new String[] {}; options = new Options(); CliUtil.changeOptionDefault(options, "port", "8080"); CliUtil.execute(options, args); assertEquals(8080, options.getPort()); // change the 'Options' args = new String[] {}; options = new Options(); CliUtil.changeCommandVersion(options, "2.0"); CliUtil.changeCommandName(options, "Program2"); CliUtil.changeCommandDescription(options, "2nd version of program"); CliUtil.execute(options, args); assertEquals("2.0", options.getClass().getAnnotation(Command.class).version()[0]); assertEquals("Program2", options.getClass().getAnnotation(Command.class).name()); assertEquals("2nd version of program", options.getClass().getAnnotation(Command.class).description()[0]); } }