001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.cli2.bug; 018 019import java.io.File; 020 021import junit.framework.TestCase; 022 023import org.apache.commons.cli2.CommandLine; 024import org.apache.commons.cli2.Group; 025import org.apache.commons.cli2.builder.ArgumentBuilder; 026import org.apache.commons.cli2.builder.DefaultOptionBuilder; 027import org.apache.commons.cli2.builder.GroupBuilder; 028import org.apache.commons.cli2.commandline.Parser; 029import org.apache.commons.cli2.option.DefaultOption; 030import org.apache.commons.cli2.validation.FileValidator; 031 032/** 033 * Test case for http://issues.apache.org/jira/browse/CLI-144. 034 * 035 * CLI 2 throws 036 * 037 * <pre> 038 * Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String 039 * </pre> 040 * 041 * when there should be no such exception 042 * 043 * @author David Biesack 044 */ 045public class BugCLI144Test extends TestCase { 046 public void testFileValidator() { 047 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); 048 final ArgumentBuilder abuilder = new ArgumentBuilder(); 049 final GroupBuilder gbuilder = new GroupBuilder(); 050 DefaultOption fileNameOption = obuilder.withShortName("f") 051 .withLongName("file-name").withRequired(true).withDescription( 052 "name of an existing file").withArgument( 053 abuilder.withName("file-name").withValidator( 054 FileValidator.getExistingFileInstance()) 055 .create()).create(); 056 Group options = gbuilder.withName("options").withOption(fileNameOption) 057 .create(); 058 Parser parser = new Parser(); 059 parser.setHelpTrigger("--help"); 060 parser.setGroup(options); 061 062 final String fileName = "src/test/org/apache/commons/cli2/bug/BugCLI144Test.java"; 063 CommandLine cl = parser 064 .parseAndHelp(new String[] { "--file-name", fileName }); 065 assertNotNull(cl); 066 assertEquals("Wrong file", new File(fileName), cl.getValue(fileNameOption)); 067 } 068}