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
15
16
17
18
19
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
32 public int getPort()
33 {
34 return this.port;
35 }
36
37
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
49 @Rule
50 public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
51
52
53
54
55
56
57
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
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
83 CliUtil.overrideMap.clear();
84 }
85
86
87
88
89
90
91
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
110 }
111 System.setSecurityManager(null);
112 String versionText = this.systemOutRule.getLog();
113 assertTrue(versionText.contains("2.0"));
114
115
116 CliUtil.overrideMap.clear();
117 }
118
119
120
121
122 @Test
123 public void testCliWrongValue()
124 {
125
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
138 }
139 System.setSecurityManager(null);
140 }
141
142
143
144
145 @Test
146 public void testCliWrongOption()
147 {
148
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
161 }
162
163 System.setSecurityManager(null);
164 }
165
166 }