|
| 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 CLI 2 throws |
| 34 | + * |
| 35 | + * <pre> |
| 36 | + * Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String |
| 37 | + * </pre> |
| 38 | + * |
| 39 | + * when there should be no such exception |
| 40 | + * |
| 41 | + * @author David Biesack |
| 42 | + */ |
| 43 | +public class BugCLI144Test extends TestCase { |
| 44 | + public void testFileValidator() { |
| 45 | + final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); |
| 46 | + final ArgumentBuilder abuilder = new ArgumentBuilder(); |
| 47 | + final GroupBuilder gbuilder = new GroupBuilder(); |
| 48 | + DefaultOption fileNameOption = obuilder.withShortName("f") |
| 49 | + .withLongName("file-name").withRequired(true).withDescription( |
| 50 | + "name of an existing file").withArgument( |
| 51 | + abuilder.withName("file-name").withValidator( |
| 52 | + FileValidator.getExistingFileInstance()) |
| 53 | + .create()).create(); |
| 54 | + Group options = gbuilder.withName("options").withOption(fileNameOption) |
| 55 | + .create(); |
| 56 | + Parser parser = new Parser(); |
| 57 | + parser.setHelpTrigger("--help"); |
| 58 | + parser.setGroup(options); |
| 59 | + |
| 60 | + final String fileName = "src/test/org/apache/commons/cli2/bug/BugCLI144Test.java"; |
| 61 | + CommandLine cl = parser |
| 62 | + .parseAndHelp(new String[] { "--file-name", fileName }); |
| 63 | + assertNotNull(cl); |
| 64 | + assertEquals("Wrong file", new File(fileName), cl.getValue(fileNameOption)); |
| 65 | + } |
| 66 | +} |
0 commit comments