Skip to content

Commit f887e8b

Browse files
committed
Simplified the test cases by removing the unnecessary fail() calls in the catch blocks
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661378 13f79535-47bb-0310-9956-ffa450edef68
1 parent 665386d commit f887e8b

9 files changed

Lines changed: 470 additions & 829 deletions

src/test/org/apache/commons/cli/ApplicationTest.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
*/
3737
public class ApplicationTest extends TestCase {
3838

39-
/**
40-
*
41-
*/
42-
public void testLs() {
39+
public void testLs() throws Exception {
4340
// create the command line parser
4441
CommandLineParser parser = new PosixParser();
4542
Options options = new Options();
@@ -57,20 +54,15 @@ public void testLs() {
5754

5855
String[] args = new String[]{ "--block-size=10" };
5956

60-
try {
61-
CommandLine line = parser.parse( options, args );
62-
assertTrue( line.hasOption( "block-size" ) );
63-
assertEquals( line.getOptionValue( "block-size" ), "10" );
64-
}
65-
catch( ParseException exp ) {
66-
fail( "Unexpected exception:" + exp.getMessage() );
67-
}
57+
CommandLine line = parser.parse( options, args );
58+
assertTrue( line.hasOption( "block-size" ) );
59+
assertEquals( line.getOptionValue( "block-size" ), "10" );
6860
}
6961

7062
/**
7163
* Ant test
7264
*/
73-
public void testAnt() {
65+
public void testAnt() throws Exception {
7466
// use the GNU parser
7567
CommandLineParser parser = new GnuParser( );
7668
Options options = new Options();
@@ -95,26 +87,20 @@ public void testAnt() {
9587
"-Dproperty=value", "-Dproperty1=value1",
9688
"-projecthelp" };
9789

98-
try {
99-
CommandLine line = parser.parse( options, args );
100-
101-
// check multiple values
102-
String[] opts = line.getOptionValues( "D" );
103-
assertEquals( "property", opts[0] );
104-
assertEquals( "value", opts[1] );
105-
assertEquals( "property1", opts[2] );
106-
assertEquals( "value1", opts[3] );
90+
CommandLine line = parser.parse( options, args );
10791

108-
// check single value
109-
assertEquals( line.getOptionValue( "buildfile"), "mybuild.xml" );
92+
// check multiple values
93+
String[] opts = line.getOptionValues( "D" );
94+
assertEquals( "property", opts[0] );
95+
assertEquals( "value", opts[1] );
96+
assertEquals( "property1", opts[2] );
97+
assertEquals( "value1", opts[3] );
11098

111-
// check option
112-
assertTrue( line.hasOption( "projecthelp") );
113-
}
114-
catch( ParseException exp ) {
115-
fail( "Unexpected exception:" + exp.getMessage() );
116-
}
99+
// check single value
100+
assertEquals( line.getOptionValue( "buildfile"), "mybuild.xml" );
117101

102+
// check option
103+
assertTrue( line.hasOption( "projecthelp") );
118104
}
119105

120106
}

src/test/org/apache/commons/cli/ArgumentIsOptionTest.java

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,58 @@
1919

2020
import junit.framework.TestCase;
2121

22-
public class ArgumentIsOptionTest extends TestCase {
22+
public class ArgumentIsOptionTest extends TestCase
23+
{
2324
private Options options = null;
2425
private CommandLineParser parser = null;
2526

26-
public void setUp() {
27-
options = new Options().addOption("p", false, "Option p").addOption("attr",
28-
true, "Option accepts argument");
27+
public void setUp()
28+
{
29+
options = new Options().addOption("p", false, "Option p").addOption("attr", true, "Option accepts argument");
2930

3031
parser = new PosixParser();
3132
}
3233

33-
public void testOptionAndOptionWithArgument() {
34-
String[] args = new String[] {
34+
public void testOptionAndOptionWithArgument() throws Exception
35+
{
36+
String[] args = new String[]{
3537
"-p",
3638
"-attr",
3739
"p"
38-
};
40+
};
3941

40-
try {
41-
CommandLine cl = parser.parse(options, args);
42-
assertTrue("Confirm -p is set", cl.hasOption("p"));
43-
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
44-
assertTrue("Confirm arg of -attr",
42+
CommandLine cl = parser.parse(options, args);
43+
assertTrue("Confirm -p is set", cl.hasOption("p"));
44+
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
45+
assertTrue("Confirm arg of -attr",
4546
cl.getOptionValue("attr").equals("p"));
46-
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
47-
}
48-
catch (ParseException e) {
49-
fail(e.toString());
50-
}
47+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
5148
}
5249

53-
public void testOptionWithArgument() {
54-
String[] args = new String[] {
50+
public void testOptionWithArgument() throws Exception
51+
{
52+
String[] args = new String[]{
5553
"-attr",
5654
"p"
57-
};
55+
};
5856

59-
try {
60-
CommandLine cl = parser.parse(options, args);
61-
assertFalse("Confirm -p is set", cl.hasOption("p"));
62-
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
63-
assertTrue("Confirm arg of -attr",
57+
CommandLine cl = parser.parse(options, args);
58+
assertFalse("Confirm -p is set", cl.hasOption("p"));
59+
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
60+
assertTrue("Confirm arg of -attr",
6461
cl.getOptionValue("attr").equals("p"));
65-
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
66-
}
67-
catch (ParseException e) {
68-
fail(e.toString());
69-
}
62+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
7063
}
7164

72-
public void testOption() {
73-
String[] args = new String[] {
65+
public void testOption() throws Exception
66+
{
67+
String[] args = new String[]{
7468
"-p"
75-
};
69+
};
7670

77-
try {
78-
CommandLine cl = parser.parse(options, args);
79-
assertTrue("Confirm -p is set", cl.hasOption("p"));
80-
assertFalse("Confirm -attr is not set", cl.hasOption("attr"));
81-
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
82-
}
83-
catch (ParseException e) {
84-
fail(e.toString());
85-
}
71+
CommandLine cl = parser.parse(options, args);
72+
assertTrue("Confirm -p is set", cl.hasOption("p"));
73+
assertFalse("Confirm -attr is not set", cl.hasOption("attr"));
74+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
8675
}
8776
}

0 commit comments

Comments
 (0)