View Javadoc
1   package org.djutils.cli;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   
6   import org.junit.Test;
7   
8   import picocli.CommandLine;
9   import picocli.CommandLine.Command;
10  import picocli.CommandLine.Option;
11  
12  /**
13   * Program to test the CLI. <br>
14   * <br>
15   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
17   * source code and binary code of this software is proprietary information of Delft University of Technology.
18   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
19   */
20  public class TestCLICommandLine
21  {
22      /** */
23      @Command(description = "Test program for CLI", name = "Program", mixinStandardHelpOptions = true, version = "1.0")
24      public static class Options implements Checkable
25      {
26          /** */
27          @Option(names = {"-p", "--port"}, description = "Internet port to use", defaultValue = "80")
28          private int port;
29  
30          /** @return the port number */
31          public int getPort()
32          {
33              return this.port;
34          }
35  
36          /** {@inheritDoc} */
37          @Override
38          public void check() throws Exception
39          {
40              if (this.port <= 0 || this.port > 65535)
41              {
42                  throw new Exception("Port should be between 1 and 65535");
43              }
44          }
45      }
46  
47      /**
48       * Test the CliUtil methods.
49       * @throws CliException on error
50       * @throws IllegalAccessException on error
51       * @throws IllegalArgumentException on error
52       * @throws NoSuchFieldException on error
53       */
54      @Test
55      public void testCli() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException
56      {
57          // prevent exit to really exit
58          System.setSecurityManager(new ExitHelper.NoExitSecurityManager());
59  
60          // test CliUtil with CommandLine
61          String[] args = new String[] {"-p", "2400"};
62          Options options = new Options();
63          CommandLine cmd = new CommandLine(options);
64          CliUtil.execute(cmd, args);
65          assertEquals(2400, options.getPort());
66          assertEquals("1.0", options.getClass().getAnnotation(Command.class).version()[0]);
67          assertEquals("Program", options.getClass().getAnnotation(Command.class).name());
68          assertEquals("Test program for CLI", options.getClass().getAnnotation(Command.class).description()[0]);
69  
70          // test CliUtil with CommandLine and check() method
71          args = new String[] {"-p", "240000"};
72          options = new Options();
73          cmd = new CommandLine(options);
74          try
75          {
76              CliUtil.execute(cmd, args);
77              fail("check() is not called: the program should exit with an error message when a wrong port is provided");
78          }
79          catch (ExitHelper.ExitException e)
80          {
81              // ok!
82          }
83  
84          System.setSecurityManager(null);
85      }
86  }