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.option;
018
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.List;
022import java.util.ListIterator;
023import java.util.Set;
024
025import org.apache.commons.cli2.DisplaySetting;
026import org.apache.commons.cli2.Option;
027import org.apache.commons.cli2.OptionException;
028import org.apache.commons.cli2.Parent;
029import org.apache.commons.cli2.WriteableCommandLine;
030import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
031import org.apache.commons.cli2.resource.ResourceConstants;
032import org.apache.commons.cli2.resource.ResourceHelper;
033
034/**
035 * @author Rob Oxspring
036 *
037 * To change the template for this generated type comment go to
038 * Window>Preferences>Java>Code Generation>Code and Comments
039 */
040public class CommandTest
041    extends ParentTestCase {
042    public static Command buildStartCommand() {
043        return new Command("start", "Begins the process", Collections.singleton("go"), false, null,
044                           null, 0);
045    }
046
047    public static Command buildCommitCommand() {
048        return new Command("commit", "Commit the changes to the database", null, true, null, null, 0);
049    }
050
051    public static Command buildLoginCommand() {
052        return new Command("login", "Initiates a session for the user", null, false,
053                           ArgumentTest.buildUsernameArgument(), null, 0);
054    }
055
056    /*
057     * (non-Javadoc)
058     *
059     * @see org.apache.commons.cli2.ParentTestCase#testProcessParent()
060     */
061    public void testProcessParent()
062        throws OptionException {
063        final Command option = buildStartCommand();
064        final List args = list("go");
065        final WriteableCommandLine commandLine = commandLine(option, args);
066        final ListIterator iterator = args.listIterator();
067        option.processParent(commandLine, iterator);
068
069        assertFalse(iterator.hasNext());
070        assertTrue(commandLine.hasOption(option));
071        assertTrue(commandLine.hasOption("start"));
072        assertTrue(commandLine.hasOption("go"));
073        assertTrue(commandLine.getValues(option).isEmpty());
074    }
075
076    public void testProcessParent_Spare()
077        throws OptionException {
078        final Command option = buildLoginCommand();
079        final List args = list("login", "rob");
080        final WriteableCommandLine commandLine = commandLine(option, args);
081        final ListIterator iterator = args.listIterator();
082        option.processParent(commandLine, iterator);
083
084        assertEquals("rob", iterator.next());
085        assertFalse(iterator.hasNext());
086        assertTrue(commandLine.hasOption(option));
087        assertTrue(commandLine.hasOption("login"));
088        assertTrue(commandLine.getValues(option).isEmpty());
089    }
090
091    /*
092     * (non-Javadoc)
093     *
094     * @see org.apache.commons.cli2.OptionTestCase#testCanProcess()
095     */
096    public void testCanProcess() {
097        final Command option = buildStartCommand();
098        assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start"));
099    }
100
101    public void testCanProcess_BadMatch() {
102        final Command option = buildStartCommand();
103        assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "stop"));
104    }
105
106    public void testCanProcess_Alias() {
107        final Command option = buildStartCommand();
108        assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "go"));
109    }
110
111    /*
112     * (non-Javadoc)
113     *
114     * @see org.apache.commons.cli2.OptionTestCase#testPrefixes()
115     */
116    public void testPrefixes() {
117        final Command option = buildStartCommand();
118        assertTrue(option.getPrefixes().isEmpty());
119    }
120
121    /*
122     * (non-Javadoc)
123     *
124     * @see org.apache.commons.cli2.OptionTestCase#testProcess()
125     */
126    public void testProcess()
127        throws OptionException {
128        final Command option = buildLoginCommand();
129        final List args = list("login", "rob");
130        final WriteableCommandLine commandLine = commandLine(option, args);
131        final ListIterator iterator = args.listIterator();
132        option.process(commandLine, iterator);
133
134        assertFalse(iterator.hasNext());
135        assertTrue(commandLine.hasOption(option));
136        assertTrue(commandLine.hasOption("login"));
137        assertEquals("rob", commandLine.getValue(option));
138    }
139
140    /*
141     * (non-Javadoc)
142     *
143     * @see org.apache.commons.cli2.OptionTestCase#testTriggers()
144     */
145    public void testTriggers() {
146        final Command option = buildStartCommand();
147        final Set triggers = option.getTriggers();
148        assertContentsEqual(list("start", "go"), triggers);
149    }
150
151    /*
152     * (non-Javadoc)
153     *
154     * @see org.apache.commons.cli2.OptionTestCase#testValidate()
155     */
156    public void testValidate() {
157        final Parent option = buildCommitCommand();
158        final WriteableCommandLine commandLine = commandLine(option, list());
159
160        try {
161            option.validate(commandLine);
162            fail("Missing an option");
163        } catch (OptionException moe) {
164            assertSame(option, moe.getOption());
165        }
166    }
167
168    /*
169     * (non-Javadoc)
170     *
171     * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage()
172     */
173    public void testAppendUsage() {
174        final Option option = buildStartCommand();
175        final StringBuffer buffer = new StringBuffer();
176        option.appendUsage(buffer, DisplaySetting.ALL, null);
177
178        assertEquals("[start (go)]", buffer.toString());
179    }
180
181    public void testNullPreferredName() {
182        try {
183            new Command(null, "", Collections.singleton("go"), false, null, null, 0);
184        } catch (IllegalArgumentException exp) {
185            assertEquals("wrong exception name",
186                         ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
187                         exp.getMessage());
188        }
189    }
190
191    public void testEmotyPreferredName() {
192        try {
193            new Command("", "", Collections.singleton("go"), false, null, null, 0);
194        } catch (IllegalArgumentException exp) {
195            assertEquals("wrong exception name",
196                         ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
197                         exp.getMessage());
198        }
199    }
200
201    public void testAppendUsage_NoOptional() {
202        final Option option = buildStartCommand();
203        final StringBuffer buffer = new StringBuffer();
204        final Set settings = new HashSet(DisplaySetting.ALL);
205        settings.remove(DisplaySetting.DISPLAY_OPTIONAL);
206        option.appendUsage(buffer, settings, null);
207
208        assertEquals("start (go)", buffer.toString());
209    }
210
211    public void testAppendUsage_NoAlias() {
212        final Option option = buildStartCommand();
213        final StringBuffer buffer = new StringBuffer();
214        final Set settings = new HashSet(DisplaySetting.ALL);
215        settings.remove(DisplaySetting.DISPLAY_ALIASES);
216        option.appendUsage(buffer, settings, null);
217
218        assertEquals("[start]", buffer.toString());
219    }
220
221    /*
222     * (non-Javadoc)
223     *
224     * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName()
225     */
226    public void testGetPreferredName() {
227        final Option option = buildStartCommand();
228        assertEquals("start", option.getPreferredName());
229    }
230
231    /*
232     * (non-Javadoc)
233     *
234     * @see org.apache.commons.cli2.OptionTestCase#testGetDescription()
235     */
236    public void testGetDescription() {
237        final Option option = buildLoginCommand();
238        assertEquals("Initiates a session for the user", option.getDescription());
239    }
240
241    /*
242     * (non-Javadoc)
243     *
244     * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
245     */
246    public void testHelpLines() {
247        // TODO Auto-generated method stub
248    }
249}