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.application;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import junit.framework.TestCase;
023
024import org.apache.commons.cli2.CommandLine;
025import org.apache.commons.cli2.Group;
026import org.apache.commons.cli2.OptionException;
027import org.apache.commons.cli2.builder.ArgumentBuilder;
028import org.apache.commons.cli2.builder.DefaultOptionBuilder;
029import org.apache.commons.cli2.builder.GroupBuilder;
030import org.apache.commons.cli2.commandline.Parser;
031import org.apache.commons.cli2.option.PropertyOption;
032
033//TODO Build up AntTest like CpTest
034public class AntTest extends TestCase {
035    public void testAnt() throws OptionException {
036        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
037        final ArgumentBuilder abuilder = new ArgumentBuilder();
038        final GroupBuilder gbuilder = new GroupBuilder();
039
040        final Group options =
041            gbuilder
042                .withName("ant")
043                .withOption(
044                    obuilder
045                        .withShortName("help")
046                        .withDescription("print this message")
047                        .create())
048                .withOption(
049                    obuilder
050                        .withShortName("projecthelp")
051                        .withDescription("print project help information")
052                        .create())
053                .withOption(
054                    obuilder
055                        .withShortName("version")
056                        .withDescription("print the version information and exit")
057                        .create())
058                .withOption(
059                    obuilder
060                        .withShortName("diagnostics")
061                        .withDescription("print information that might be helpful to diagnose or report problems.")
062                        .create())
063                .withOption(
064                    obuilder
065                        .withShortName("quiet")
066                        .withShortName("q")
067                        .withDescription("be extra quiet")
068                        .create())
069                .withOption(
070                    obuilder
071                        .withShortName("verbose")
072                        .withShortName("v")
073                        .withDescription("be extra verbose")
074                        .create())
075                .withOption(
076                    obuilder
077                        .withShortName("debug")
078                        .withDescription("print debugging information")
079                        .create())
080                .withOption(
081                    obuilder
082                        .withShortName("emacs")
083                        .withDescription("produce logging information without adornments")
084                        .create())
085                .withOption(
086                    obuilder
087                        .withShortName("logfile")
088                        .withShortName("l")
089                        .withDescription("use given file for log")
090                        .withArgument(
091                            abuilder
092                                .withName("file")
093                                .withMinimum(1)
094                                .withMaximum(1)
095                                .create())
096                        .create())
097                .withOption(
098                    obuilder
099                        .withShortName("logger")
100                        .withDescription("the class which is to perform logging")
101                        .withArgument(
102                            abuilder
103                                .withName("classname")
104                                .withMinimum(1)
105                                .withMaximum(1)
106                                .create())
107                        .create())
108                .withOption(
109                    obuilder
110                        .withShortName("listener")
111                        .withDescription("add an instance of class as a project listener")
112                        .withArgument(
113                            abuilder
114                                .withName("classname")
115                                .withMinimum(1)
116                                .withMaximum(1)
117                                .create())
118                        .create())
119                .withOption(
120                    obuilder
121                        .withShortName("buildfile")
122                        .withShortName("file")
123                        .withShortName("f")
124                        .withDescription("use given buildfile")
125                        .withArgument(
126                            abuilder
127                                .withName("file")
128                                .withMinimum(1)
129                                .withMaximum(1)
130                                .create())
131                        .create())
132                .withOption(PropertyOption.INSTANCE)
133                .withOption(
134                    obuilder
135                        .withShortName("propertyfile")
136                        .withDescription("load all properties from file with -D properties taking precedence")
137                        .withArgument(
138                            abuilder
139                                .withName("name")
140                                .withMinimum(1)
141                                .withMaximum(1)
142                                .create())
143                        .create())
144                .withOption(
145                    obuilder
146                        .withShortName("inputhandler")
147                        .withDescription("the class which will handle input requests")
148                        .withArgument(
149                            abuilder
150                                .withName("class")
151                                .withMinimum(1)
152                                .withMaximum(1)
153                                .create())
154                        .create())
155                .withOption(
156                    obuilder
157                        .withShortName("find")
158                        .withDescription("search for buildfile towards the root of the filesystem and use it")
159                        .withArgument(
160                            abuilder
161                                .withName("file")
162                                .withMinimum(1)
163                                .withMaximum(1)
164                                .create())
165                        .create())
166                .withOption(abuilder.withName("target").create())
167                .create();
168
169        Parser parser = new Parser();
170        parser.setGroup(options);
171        CommandLine line =
172            parser.parse(
173                new String[] {
174                    "-buildfile",
175                    "mybuild.xml",
176                    "-Dproperty=value",
177                    "-Dproperty1=value1",
178                    "-projecthelp",
179                    "compile",
180                    "docs" });
181
182        // check properties
183        assertEquals(2, line.getProperties().size());
184        assertEquals("value", line.getProperty("property"));
185        assertEquals("value1", line.getProperty("property1"));
186
187        // check single values
188        assertEquals("mybuild.xml", line.getValue("-buildfile"));
189        assertTrue(line.hasOption("-projecthelp"));
190        assertFalse(line.hasOption("-help"));
191
192        assertTrue(line.hasOption("target"));
193        final List targets = new ArrayList();
194        targets.add("compile");
195        targets.add("docs");
196        assertEquals(targets, line.getValues("target"));
197    }
198}