View Javadoc
1   package org.djutils.cli;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.djunits.unit.DurationUnit;
6   import org.djunits.value.vdouble.scalar.Duration;
7   import org.junit.Test;
8   
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 TestCLISuperclass
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      public static class OptionsSub extends Options
49      {
50          /** */
51          @Option(names = {"-t", "--timeout"}, description = "timeout", defaultValue = "10min")
52          private Duration timeout;
53  
54          /** @return the timeout */
55          public Duration getTimeout()
56          {
57              return this.timeout;
58          }
59      }
60  
61      /**
62       * Test the CliUtil methods.
63       * @throws CliException on error
64       * @throws IllegalAccessException on error
65       * @throws IllegalArgumentException on error
66       * @throws NoSuchFieldException on error
67       */
68      @Test
69      public void testCli() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException
70      {
71          // do the command line overrrides work?
72          String[] args = new String[] {"-p", "1200"};
73          Options options = new Options();
74          CliUtil.execute(options, args);
75          assertEquals(1200, options.getPort());
76          assertEquals("1.0", options.getClass().getAnnotation(Command.class).version()[0]);
77          assertEquals("Program", options.getClass().getAnnotation(Command.class).name());
78          assertEquals("Test program for CLI", options.getClass().getAnnotation(Command.class).description()[0]);
79  
80          // do the command line overrides work for classes and subclasses? 
81          args = new String[] {"-p", "2400", "--timeout", "60s"};
82          OptionsSub optionsSub = new OptionsSub();
83          CliUtil.execute(optionsSub, args);
84          assertEquals(2400, optionsSub.getPort());
85          assertEquals(new Duration(60.0, DurationUnit.SECOND), optionsSub.getTimeout());
86          assertEquals("1.0", optionsSub.getClass().getSuperclass().getAnnotation(Command.class).version()[0]);
87          assertEquals("Program", optionsSub.getClass().getSuperclass().getAnnotation(Command.class).name());
88          assertEquals("Test program for CLI",
89                  optionsSub.getClass().getSuperclass().getAnnotation(Command.class).description()[0]);
90  
91          // does the change of command name, description and version work?
92          CliUtil.changeCommandName(OptionsSub.class, "NewName");
93          CliUtil.changeCommandDescription(OptionsSub.class, "NewDescription");
94          CliUtil.changeCommandVersion(OptionsSub.class, "1.1");
95          CliUtil.execute(optionsSub, args);
96          assertEquals("1.1", CliUtil.getCommandVersion(options)[0]);
97          assertEquals("NewName", CliUtil.getCommandName(options));
98          assertEquals("NewDescription", CliUtil.getCommandDescription(options)[0]);
99          
100         // does the change work for the main class?
101         args = new String[] {};
102         OptionsSub optionsChanged1 = new OptionsSub();
103         CliUtil.changeOptionDefault(OptionsSub.class, "timeout", "30s");
104         CliUtil.execute(optionsChanged1, args);
105         assertEquals(80, optionsChanged1.getPort());
106         assertEquals(new Duration(30.0, DurationUnit.SECOND), optionsChanged1.getTimeout());
107         
108         // does the change work for a subclass?
109         args = new String[] {};
110         OptionsSub optionsChanged2 = new OptionsSub();
111         CliUtil.changeOptionDefault(optionsChanged2.getClass().getSuperclass(), "port", "8080");
112         CliUtil.execute(optionsChanged2, args);
113         assertEquals(8080, optionsChanged2.getPort());
114         assertEquals(new Duration(30.0, DurationUnit.SECOND), optionsChanged2.getTimeout());
115 
116         // does the change stay?
117         args = new String[] {};
118         OptionsSub optionsChanged3 = new OptionsSub();
119         CliUtil.execute(optionsChanged3, args);
120         assertEquals(8080, optionsChanged3.getPort());
121         assertEquals(new Duration(30.0, DurationUnit.SECOND), optionsChanged3.getTimeout());
122         
123         // clean the override map
124         CliUtil.overrideMap.clear();
125     }
126 }