2626import org .apache .commons .cli2 .Parent ;
2727import org .apache .commons .cli2 .WriteableCommandLine ;
2828import org .apache .commons .cli2 .commandline .WriteableCommandLineImpl ;
29+ import org .apache .commons .cli2 .resource .ResourceConstants ;
30+ import org .apache .commons .cli2 .resource .ResourceHelper ;
2931
3032/**
3133 * @author Rob Oxspring
32- *
34+ *
3335 * To change the template for this generated type comment go to
3436 * Window>Preferences>Java>Code Generation>Code and Comments
3537 */
36- public class SwitchTest extends ParentTestCase {
37-
38+ public class SwitchTest
39+ extends ParentTestCase {
3840 public static Switch buildDisplaySwitch () {
3941 final Set aliases = new HashSet ();
4042 aliases .add ("d" );
4143 aliases .add ("disp" );
42- return new Switch (
43- "+" ,
44- "-" ,
45- "display" ,
46- aliases ,
47- "Sets whether to display to screen" ,
48- true ,
49- null ,
50- null ,
51- 'd' ,
52- null );
44+
45+ return new Switch ("+" , "-" , "display" , aliases , "Sets whether to display to screen" , true ,
46+ null , null , 'd' , null );
5347 }
5448
5549 /*
5650 * (non-Javadoc)
57- *
51+ *
5852 * @see org.apache.commons.cli2.ParentTestCase#testProcessParent()
5953 */
60- public void testProcessParent () throws OptionException {
54+ public void testProcessParent ()
55+ throws OptionException {
6156 final Switch option = buildDisplaySwitch ();
6257 final List args = list ("+d" );
6358 final WriteableCommandLine commandLine = commandLine (option , args );
@@ -72,7 +67,8 @@ public void testProcessParent() throws OptionException {
7267 assertTrue (commandLine .getValues (option ).isEmpty ());
7368 }
7469
75- public void testProcessParent_Disabled () throws OptionException {
70+ public void testProcessParent_Disabled ()
71+ throws OptionException {
7672 final Switch option = buildDisplaySwitch ();
7773 final List args = list ("-disp" );
7874 final WriteableCommandLine commandLine = commandLine (option , args );
@@ -89,22 +85,22 @@ public void testProcessParent_Disabled() throws OptionException {
8985
9086 /*
9187 * (non-Javadoc)
92- *
88+ *
9389 * @see org.apache.commons.cli2.OptionTestCase#testCanProcess()
9490 */
9591 public void testCanProcess () {
9692 final Switch option = buildDisplaySwitch ();
97- assertTrue (option .canProcess (new WriteableCommandLineImpl (option ,null ),"+d" ));
93+ assertTrue (option .canProcess (new WriteableCommandLineImpl (option , null ), "+d" ));
9894 }
9995
10096 public void testCanProcess_BadMatch () {
10197 final Switch option = buildDisplaySwitch ();
102- assertFalse (option .canProcess (new WriteableCommandLineImpl (option ,null ),"-dont" ));
98+ assertFalse (option .canProcess (new WriteableCommandLineImpl (option , null ), "-dont" ));
10399 }
104100
105101 /*
106102 * (non-Javadoc)
107- *
103+ *
108104 * @see org.apache.commons.cli2.OptionTestCase#testPrefixes()
109105 */
110106 public void testPrefixes () {
@@ -114,29 +110,27 @@ public void testPrefixes() {
114110
115111 /*
116112 * (non-Javadoc)
117- *
113+ *
118114 * @see org.apache.commons.cli2.OptionTestCase#testProcess()
119115 */
120116 public void testProcess () {
121117 // TODO Auto-generated method stub
122-
123118 }
124119
125120 /*
126121 * (non-Javadoc)
127- *
122+ *
128123 * @see org.apache.commons.cli2.OptionTestCase#testTriggers()
129124 */
130125 public void testTriggers () {
131126 final Switch option = buildDisplaySwitch ();
132- assertContentsEqual (
133- list ("-d" , "+d" , "-disp" , "+disp" , "+display" , "-display" ),
134- option .getTriggers ());
127+ assertContentsEqual (list ("-d" , "+d" , "-disp" , "+disp" , "+display" , "-display" ),
128+ option .getTriggers ());
135129 }
136130
137131 /*
138132 * (non-Javadoc)
139- *
133+ *
140134 * @see org.apache.commons.cli2.OptionTestCase#testValidate()
141135 */
142136 public void testValidate () {
@@ -146,25 +140,22 @@ public void testValidate() {
146140 try {
147141 option .validate (commandLine );
148142 fail ("Missing an option" );
149- }
150- catch (OptionException moe ) {
143+ } catch (OptionException moe ) {
151144 assertSame (option , moe .getOption ());
152145 }
153146 }
154147
155148 /*
156149 * (non-Javadoc)
157- *
150+ *
158151 * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage()
159152 */
160153 public void testAppendUsage () {
161154 final Option option = buildDisplaySwitch ();
162155 final StringBuffer buffer = new StringBuffer ();
163156 option .appendUsage (buffer , DisplaySetting .ALL , null );
164157
165- assertEquals (
166- "+display|-display (+d|-d,+disp|-disp)" ,
167- buffer .toString ());
158+ assertEquals ("+display|-display (+d|-d,+disp|-disp)" , buffer .toString ());
168159 }
169160
170161 public void testAppendUsage_NoAlias () {
@@ -210,7 +201,7 @@ public void testAppendUsage_NoDisabledOrEnabled() {
210201
211202 /*
212203 * (non-Javadoc)
213- *
204+ *
214205 * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName()
215206 */
216207 public void testGetPreferredName () {
@@ -220,22 +211,97 @@ public void testGetPreferredName() {
220211
221212 /*
222213 * (non-Javadoc)
223- *
214+ *
224215 * @see org.apache.commons.cli2.OptionTestCase#testGetDescription()
225216 */
226217 public void testGetDescription () {
227218 final Option option = buildDisplaySwitch ();
228- assertEquals (
229- "Sets whether to display to screen" ,
230- option .getDescription ());
219+ assertEquals ("Sets whether to display to screen" , option .getDescription ());
220+ }
221+
222+ public void testNullPreferredName () {
223+ try {
224+ new Switch ("+" , "-" , null , null , "Sets whether to display to screen" , true , null , null ,
225+ 'd' , null );
226+ } catch (IllegalArgumentException exp ) {
227+ assertEquals ("wrong exception message" ,
228+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_PREFERRED_NAME_TOO_SHORT ),
229+ exp .getMessage ());
230+ }
231231 }
232+
233+ public void testEmptyPreferredName () {
234+ try {
235+ new Switch ("+" , "-" , "" , null , "Sets whether to display to screen" , true , null , null ,
236+ 'd' , null );
237+ } catch (IllegalArgumentException exp ) {
238+ assertEquals ("wrong exception message" ,
239+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_PREFERRED_NAME_TOO_SHORT ),
240+ exp .getMessage ());
241+ }
242+ }
243+
244+ public void testNullAliases () {
245+ try {
246+ new Switch ("+" , "-" , "display" , null , "Sets whether to display to screen" , true , null ,
247+ null , 'd' , null );
248+ } catch (IllegalArgumentException exp ) {
249+ assertEquals ("wrong exception message" ,
250+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_PREFERRED_NAME_TOO_SHORT ),
251+ exp .getMessage ());
252+ }
253+ }
254+
255+ public void testNullEnablePrefix () {
256+ try {
257+ new Switch (null , "-" , "display" , null , "Sets whether to display to screen" , true , null ,
258+ null , 'd' , null );
259+ } catch (IllegalArgumentException exp ) {
260+ assertEquals ("wrong exception message" ,
261+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_NO_ENABLED_PREFIX ),
262+ exp .getMessage ());
263+ }
264+ }
265+
266+ public void testNullDisablePrefix () {
267+ try {
268+ new Switch ("+" , null , "display" , null , "Sets whether to display to screen" , true , null ,
269+ null , 'd' , null );
270+ } catch (IllegalArgumentException exp ) {
271+ assertEquals ("wrong exception message" ,
272+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_NO_DISABLED_PREFIX ),
273+ exp .getMessage ());
274+ }
275+ }
276+
277+ public void testEnabledPrefixStartsWithDisabledPrefix () {
278+ try {
279+ new Switch ("-" , "-" , "display" , null , "Sets whether to display to screen" , true , null ,
280+ null , 'd' , null );
281+ } catch (IllegalArgumentException exp ) {
282+ assertEquals ("wrong exception message" ,
283+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_ENABLED_STARTS_WITH_DISABLED ),
284+ exp .getMessage ());
285+ }
286+ }
287+
288+ public void testDisabledPrefixStartsWithEnabledPrefix () {
289+ try {
290+ new Switch ("o" , "on" , "display" , null , "Sets whether to display to screen" , true , null ,
291+ null , 'd' , null );
292+ } catch (IllegalArgumentException exp ) {
293+ assertEquals ("wrong exception message" ,
294+ ResourceHelper .getResourceHelper ().getMessage (ResourceConstants .SWITCH_DISABLED_STARTWS_WITH_ENABLED ),
295+ exp .getMessage ());
296+ }
297+ }
298+
232299 /*
233300 * (non-Javadoc)
234- *
301+ *
235302 * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
236303 */
237304 public void testHelpLines () {
238305 // TODO Auto-generated method stub
239-
240306 }
241307}
0 commit comments