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.commandline;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.Iterator;
022import java.util.Set;
023
024import org.apache.commons.cli2.CommandLine;
025import org.apache.commons.cli2.CommandLineTestCase;
026import org.apache.commons.cli2.Option;
027import org.apache.commons.cli2.WriteableCommandLine;
028import org.apache.commons.cli2.option.PropertyOption;
029import org.apache.commons.cli2.builder.DefaultOptionBuilder;
030
031/**
032 * @author Rob Oxspring
033 */
034public class DefaultingCommandLineTest
035    extends CommandLineTestCase {
036    private CommandLine first;
037    private CommandLine second;
038    private Option inFirst = new DefaultOptionBuilder().withLongName("infirst").create();
039    private Option inBoth = new DefaultOptionBuilder().withLongName("inboth").create();
040    private Option inSecond = new DefaultOptionBuilder().withLongName("insecond").create();
041
042    /* (non-Javadoc)
043     * @see org.apache.commons.cli2.CommandLineTest#createCommandLine()
044     */
045    protected final CommandLine createCommandLine() {
046        final WriteableCommandLine writeable = new WriteableCommandLineImpl(root, new ArrayList());
047        writeable.addOption(present);
048        writeable.addProperty(new PropertyOption(), "present", "present property");
049        writeable.addSwitch(bool, true);
050        writeable.addValue(present, "present value");
051        writeable.addOption(multiple);
052        writeable.addValue(multiple, "value 1");
053        writeable.addValue(multiple, "value 2");
054        writeable.addValue(multiple, "value 3");
055
056        final DefaultingCommandLine defaults = new DefaultingCommandLine();
057        defaults.appendCommandLine(writeable);
058
059        return defaults;
060    }
061
062    public void setUp()
063        throws Exception {
064        super.setUp();
065
066        WriteableCommandLine writeable;
067
068        writeable = new WriteableCommandLineImpl(root, new ArrayList());
069        writeable.addOption(inFirst);
070        writeable.addOption(inBoth);
071        writeable.addProperty("infirst", "infirst first value");
072        writeable.addProperty("inboth", "inboth first value");
073        writeable.addSwitch(inFirst, true);
074        writeable.addSwitch(inBoth, true);
075        writeable.addValue(inFirst, "infirst first value 1");
076        writeable.addValue(inFirst, "infirst first value 2");
077        writeable.addValue(inBoth, "inboth first value 1");
078        writeable.addValue(inBoth, "inboth first value 2");
079        first = writeable;
080
081        writeable = new WriteableCommandLineImpl(root, new ArrayList());
082        writeable.addOption(inSecond);
083        writeable.addOption(inBoth);
084        writeable.addProperty("insecond", "insecond second value");
085        writeable.addProperty("inboth", "inboth second value");
086        writeable.addSwitch(inSecond, true);
087        writeable.addSwitch(inBoth, true);
088        writeable.addValue(inSecond, "insecond second value 1");
089        writeable.addValue(inSecond, "insecond second value 2");
090        writeable.addValue(inBoth, "inboth second value 1");
091        writeable.addValue(inBoth, "inboth second value 2");
092        second = writeable;
093    }
094
095    public final void testAppendCommandLine() {
096        final DefaultingCommandLine defaults = new DefaultingCommandLine();
097        Iterator i;
098
099        i = defaults.commandLines();
100        assertFalse(i.hasNext());
101
102        defaults.appendCommandLine(first);
103        i = defaults.commandLines();
104        assertSame(first, i.next());
105        assertFalse(i.hasNext());
106
107        defaults.appendCommandLine(second);
108        i = defaults.commandLines();
109        assertSame(first, i.next());
110        assertSame(second, i.next());
111        assertFalse(i.hasNext());
112    }
113
114    public final void testInsertCommandLine() {
115        final DefaultingCommandLine defaults = new DefaultingCommandLine();
116        Iterator i;
117
118        i = defaults.commandLines();
119        assertFalse(i.hasNext());
120
121        defaults.insertCommandLine(0, first);
122        i = defaults.commandLines();
123        assertSame(first, i.next());
124        assertFalse(i.hasNext());
125
126        defaults.insertCommandLine(0, second);
127        i = defaults.commandLines();
128        assertSame(second, i.next());
129        assertSame(first, i.next());
130        assertFalse(i.hasNext());
131    }
132
133    public void testTriggers() {
134        final DefaultingCommandLine defaults = new DefaultingCommandLine();
135        defaults.appendCommandLine(first);
136        defaults.appendCommandLine(second);
137
138        Set set = defaults.getOptionTriggers();
139        Iterator iter = set.iterator();
140        assertEquals("wrong # of triggers", 3, set.size());
141        assertTrue("cannot find trigger", set.contains("--insecond"));
142        assertTrue("cannot find trigger", set.contains("--inboth"));
143        assertTrue("cannot find trigger", set.contains("--infirst"));
144    }
145
146    public void testDefaults() {
147        final DefaultingCommandLine defaults = new DefaultingCommandLine();
148
149        assertEquals("wrong # of defaults", 0, defaults.getValues("--insecond").size());
150        assertEquals("wrong Set of defaults", Collections.EMPTY_LIST, defaults.getValues("--insecond", null));
151    }
152}