View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.bug;
18  
19  import java.io.File;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.cli2.CommandLine;
24  import org.apache.commons.cli2.Group;
25  import org.apache.commons.cli2.builder.ArgumentBuilder;
26  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
27  import org.apache.commons.cli2.builder.GroupBuilder;
28  import org.apache.commons.cli2.commandline.Parser;
29  import org.apache.commons.cli2.option.DefaultOption;
30  import org.apache.commons.cli2.validation.FileValidator;
31  
32  /**
33   * Test case for http://issues.apache.org/jira/browse/CLI-144.
34   *
35   * CLI 2 throws
36   *
37   * <pre>
38   * Exception in thread &quot;main&quot; java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
39   * </pre>
40   *
41   * when there should be no such exception
42   *
43   * @author David Biesack
44   */
45  public class BugCLI144Test extends TestCase {
46  	public void testFileValidator() {
47  		final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
48          final ArgumentBuilder abuilder = new ArgumentBuilder();
49          final GroupBuilder gbuilder = new GroupBuilder();
50          DefaultOption fileNameOption = obuilder.withShortName("f")
51                  .withLongName("file-name").withRequired(true).withDescription(
52                          "name of an existing file").withArgument(
53                          abuilder.withName("file-name").withValidator(
54                                  FileValidator.getExistingFileInstance())
55                                  .create()).create();
56          Group options = gbuilder.withName("options").withOption(fileNameOption)
57                  .create();
58          Parser parser = new Parser();
59          parser.setHelpTrigger("--help");
60          parser.setGroup(options);
61  
62          final String fileName = "src/test/org/apache/commons/cli2/bug/BugCLI144Test.java";
63          CommandLine cl = parser
64                  .parseAndHelp(new String[] { "--file-name", fileName });
65          assertNotNull(cl);
66          assertEquals("Wrong file", new File(fileName), cl.getValue(fileNameOption));
67  	}
68  }