1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.whisker.cli;
20
21 import junit.framework.TestCase;
22
23 import org.apache.commons.cli.AlreadySelectedException;
24 import org.apache.commons.cli.ParseException;
25 import org.apache.creadur.whisker.app.Act;
26 import org.apache.creadur.whisker.app.StreamableResource;
27 import org.apache.creadur.whisker.app.Whisker;
28 import org.apache.creadur.whisker.app.load.StreamableClassPathResource;
29 import org.apache.creadur.whisker.app.load.StreamableFileNameResource;
30
31 public class TestCommandParsing extends TestCase {
32
33 private static final String LONG_OPT = "--";
34 private static final String SHORT_OPT = "-";
35 private Main subject;
36
37 @Override
38 protected void setUp() throws Exception {
39 subject = new Main(new Whisker());
40 }
41
42 public void testGenerateAndAuditAreMutuallyExclusive() throws Exception {
43 try {
44 subject.configure(
45 args(longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()), "PATH",
46 longOpt(CommandLineOption.ACT_TO_AUDIT.getLongName()),
47 longOpt(CommandLineOption.ACT_TO_GENERATE.getLongName())));
48
49 fail("Expected audit and generate to together to throw exception");
50 } catch (AlreadySelectedException e) {
51
52 }
53 }
54
55 public void testGenerateAndSkeletonAreMutuallyExclusive() throws Exception {
56 try {
57 subject.configure(
58 args(longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()), "PATH",
59 longOpt(CommandLineOption.ACT_TO_SKELETON.getLongName()),
60 longOpt(CommandLineOption.ACT_TO_GENERATE.getLongName())));
61
62 fail("Expected audit and generate to together to throw exception");
63 } catch (AlreadySelectedException e) {
64
65 }
66 }
67
68 public void testSkeletonAndAuditAreMutuallyExclusive() throws Exception {
69 try {
70 subject.configure(
71 args(longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()), "PATH",
72 longOpt(CommandLineOption.ACT_TO_AUDIT.getLongName()),
73 longOpt(CommandLineOption.ACT_TO_SKELETON.getLongName())));
74
75 fail("Expected audit and generate to together to throw exception");
76 } catch (AlreadySelectedException e) {
77
78 }
79 }
80
81
82 public void testSetGenerateAct() throws Exception {
83 checkSetActForOption(Act.GENERATE, CommandLineOption.ACT_TO_GENERATE);
84 }
85
86 public void testSetAuditAct() throws Exception {
87 checkSetActForOption(Act.AUDIT, CommandLineOption.ACT_TO_AUDIT);
88 }
89
90 public void testSetSkeletonAct() throws Exception {
91 checkSetActForOption(Act.SKELETON, CommandLineOption.ACT_TO_SKELETON);
92 }
93
94
95
96
97
98
99
100 private void checkSetActForOption(Act act, CommandLineOption option)
101 throws ParseException {
102 assertEquals(act + " arg should set property on Whisker", act, subject.configure(
103 args(longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()), "PATH",
104 shortOpt(CommandLineOption.SOURCE.getShortName()), "path", longOpt(option.getLongName()))).getAct());
105 assertEquals(act + "Audit arg should set property on Whisker", act, subject.configure(
106 args(longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()), "PATH",
107 shortOpt(CommandLineOption.SOURCE.getShortName()), "path", shortOpt(option.getShortName()))).getAct());
108 }
109
110 public void testSetSourceByCli() throws Exception {
111 checkSourceWithPath("/some/path");
112 checkSourceWithPath("/");
113 checkSourceWithPath("relative");
114 }
115
116 public void testAuditRequiresSource() throws Exception {
117 try {
118 subject.configure(args(
119 longOpt(CommandLineOption.ACT_TO_AUDIT.getLongName()),
120 shortOpt(CommandLineOption.LICENSE_DESCRIPTION.getShortName()), "some/path"));
121 fail("Audit requires source");
122 } catch (ParseException e) {
123
124 }
125 }
126
127
128
129
130 private void checkSourceWithPath(String aPath) throws Exception {
131 checkSource(aPath, shortOpt(CommandLineOption.SOURCE.getShortName()));
132 checkSource(aPath, longOpt(CommandLineOption.SOURCE.getLongName()));
133 }
134
135
136 private void checkSource(String aPath, String arg) throws ParseException {
137 assertEquals("Source arg should set property on Whisker", aPath,
138 subject.configure(args(arg, aPath,
139 longOpt(CommandLineOption.ACT_TO_AUDIT.getLongName()),
140 shortOpt(CommandLineOption.LICENSE_DESCRIPTION.getShortName()), "Whatever/bin")).getSource());
141 }
142
143
144 public void testSetLicenseDescriptorShortByCLI() throws Exception {
145 exerciseShortLicenseDescriptionWithPath("/some/path");
146 exerciseShortLicenseDescriptionWithPath("another/path");
147 exerciseShortLicenseDescriptionWithPath("short");
148 exerciseShortLicenseDescriptionWithPath("");
149 exerciseShortLicenseDescriptionWithPath("http://url.style/path");
150 }
151
152 public void testSetLicenseDescriptorLongByCLI() throws Exception {
153 exerciseLongLicenseDescriptionWithPath("/some/path");
154 exerciseLongLicenseDescriptionWithPath("another/path");
155 exerciseLongLicenseDescriptionWithPath("short");
156 exerciseLongLicenseDescriptionWithPath("");
157 exerciseLongLicenseDescriptionWithPath("http://url.style/path");
158 }
159
160
161
162
163 private void exerciseShortLicenseDescriptionWithPath(String aPath) throws Exception {
164 exerciseLicenseDescriptor(aPath, shortOpt(CommandLineOption.LICENSE_DESCRIPTION.getShortName()));
165 }
166
167
168
169
170
171
172 private String shortOpt(char licenseDescriptorShortOpt) {
173 return SHORT_OPT + licenseDescriptorShortOpt;
174 }
175
176
177
178
179 private void exerciseLongLicenseDescriptionWithPath(String aPath) throws Exception {
180 exerciseLicenseDescriptor(aPath, longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()));
181 }
182
183
184
185
186
187
188 private String longOpt(String licenseDescriptorLongOpt) {
189 return LONG_OPT + licenseDescriptorLongOpt;
190 }
191
192
193
194
195
196
197
198 private void exerciseLicenseDescriptor(String aPath, String arg)
199 throws ParseException {
200 final StreamableResource streamableResource = subject.configure(
201 args(arg, aPath, longOpt(CommandLineOption.ACT_TO_GENERATE.getLongName())))
202 .getLicenseDescriptor();
203
204 assertEquals("License descriptor arg should set property on Whisker", aPath,
205 name(streamableResource));
206 }
207
208 private String name(final StreamableResource streamableResource) {
209 if (streamableResource instanceof StreamableClassPathResource) {
210 return ((StreamableClassPathResource)streamableResource).getName();
211 } else {
212 return ((StreamableFileNameResource)streamableResource).getFileName();
213 }
214 }
215
216 private String[] args(String ...strings) {
217 return strings;
218 }
219 }