View Javadoc
1   package org.djutils.cli;
2   
3   import static org.junit.Assert.assertTrue;
4   import static org.junit.Assert.fail;
5   
6   import org.junit.Rule;
7   import org.junit.Test;
8   import org.junit.contrib.java.lang.system.SystemOutRule;
9   
10  import picocli.CommandLine.Command;
11  import picocli.CommandLine.Option;
12  
13  /**
14   * Program to test the CLI. <br>
15   * <br>
16   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
18   * source code and binary code of this software is proprietary information of Delft University of Technology.
19   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
20   */
21  public class TestCLIHelpVersion
22  {
23      /** */
24      @Command(description = "Test program for CLI", name = "Program", mixinStandardHelpOptions = true, version = "1.0")
25      public static class Options implements Checkable
26      {
27          /** */
28          @Option(names = {"-p", "--port"}, description = "Internet port to use", defaultValue = "80")
29          private int port;
30  
31          /** @return the port number */
32          public int getPort()
33          {
34              return this.port;
35          }
36  
37          /** {@inheritDoc} */
38          @Override
39          public void check() throws Exception
40          {
41              if (this.port <= 0 || this.port > 65535)
42              {
43                  throw new Exception("Port should be between 1 and 65535");
44              }
45          }
46      }
47  
48      /** store the System.out.print() information in a log. */
49      @Rule
50      public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
51  
52      /**
53       * Test the CliUtil "--help" option (that calls System.exit).
54       * @throws CliException on error
55       * @throws IllegalAccessException on error
56       * @throws IllegalArgumentException on error
57       * @throws NoSuchFieldException on error
58       */
59      @Test
60      public void testCliHelp() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException
61      {
62          System.setSecurityManager(new ExitHelper.NoExitSecurityManager());
63          String[] args = new String[] {"--help"};
64          Options options = new Options();
65          CliUtil.changeCommandVersion(options, "2.0");
66          CliUtil.changeCommandName(options, "Program2");
67          CliUtil.changeCommandDescription(options, "2nd version of program");
68          try
69          {
70              CliUtil.execute(options, args);
71              fail("Program should have exited");
72          }
73          catch (ExitHelper.ExitException e)
74          {
75              // ok!
76          }
77          System.setSecurityManager(null);
78          String helpText = this.systemOutRule.getLog();
79          assertTrue(helpText.contains("Program2"));
80          assertTrue(helpText.contains("2nd version of program"));
81          
82          // clean the override map
83          CliUtil.overrideMap.clear();
84      }
85  
86      /**
87       * Test the CliUtil "-V" option (that calls System.exit).
88       * @throws CliException on error
89       * @throws IllegalAccessException on error
90       * @throws IllegalArgumentException on error
91       * @throws NoSuchFieldException on error
92       */
93      @Test
94      public void testCliVersion() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException
95      {
96          System.setSecurityManager(new ExitHelper.NoExitSecurityManager());
97          String[] args = new String[] {"-V"};
98          Options options = new Options();
99          CliUtil.changeCommandVersion(options, "2.0");
100         CliUtil.changeCommandName(options, "Program2");
101         CliUtil.changeCommandDescription(options, "2nd version of program");
102         try
103         {
104             CliUtil.execute(options, args);
105             fail("Program should have exited");
106         }
107         catch (ExitHelper.ExitException e)
108         {
109             // ok!
110         }
111         System.setSecurityManager(null);
112         String versionText = this.systemOutRule.getLog();
113         assertTrue(versionText.contains("2.0"));
114         
115         // clean the override map
116         CliUtil.overrideMap.clear();
117     }
118 
119     /**
120      * Test the CliUtil methods with a wrong port.
121      */
122     @Test
123     public void testCliWrongValue()
124     {
125         // prevent exit to really exit
126         System.setSecurityManager(new ExitHelper.NoExitSecurityManager());
127 
128         String[] args = new String[] {"-p", "120000"};
129         Options options = new Options();
130         try
131         {
132             CliUtil.execute(options, args);
133             fail("the program should exit with an error message when a wrong port is provided");
134         }
135         catch (ExitHelper.ExitException e)
136         {
137             // ok!
138         }
139         System.setSecurityManager(null);
140     }
141 
142     /**
143      * Test the CliUtil methods with a wrong option.
144      */
145     @Test
146     public void testCliWrongOption()
147     {
148         // prevent exit to really exit
149         System.setSecurityManager(new ExitHelper.NoExitSecurityManager());
150 
151         String[] args = new String[] {"--wrongOption=50"};
152         Options options = new Options();
153         try
154         {
155             CliUtil.execute(options, args);
156             fail("the program should exit with an error message when a wrong option is provided");
157         }
158         catch (ExitHelper.ExitException e)
159         {
160             // ok!
161         }
162 
163         System.setSecurityManager(null);
164     }
165 
166 }