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.builder;
018
019import junit.framework.TestCase;
020
021import org.apache.commons.cli2.Argument;
022import org.apache.commons.cli2.Group;
023import org.apache.commons.cli2.option.DefaultOption;
024import org.apache.commons.cli2.resource.ResourceConstants;
025import org.apache.commons.cli2.resource.ResourceHelper;
026
027public class DefaultOptionBuilderTest
028    extends TestCase {
029    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
030    private DefaultOptionBuilder defaultOptionBuilder;
031
032    /*
033     * @see TestCase#setUp()
034     */
035    protected void setUp()
036        throws Exception {
037        this.defaultOptionBuilder = new DefaultOptionBuilder();
038    }
039
040    /*
041     * Class to test for void DefaultOptionBuilder(String, String, boolean)
042     */
043    public void testNew_NullShortPrefix() {
044        try {
045            new DefaultOptionBuilder(null, null, false);
046            fail("null short prefix is not permitted");
047        } catch (IllegalArgumentException e) {
048            assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX),
049                         e.getMessage());
050        }
051    }
052
053    /*
054     * Class to test for void DefaultOptionBuilder(String, String, boolean)
055     */
056    public void testNew_EmptyShortPrefix() {
057        try {
058            new DefaultOptionBuilder("", null, false);
059            fail("empty short prefix is not permitted");
060        } catch (IllegalArgumentException e) {
061            assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX),
062                         e.getMessage());
063        }
064    }
065
066    /*
067     * Class to test for void DefaultOptionBuilder(String, String, boolean)
068     */
069    public void testNew_NullLongPrefix() {
070        try {
071            new DefaultOptionBuilder("-", null, false);
072            fail("null long prefix is not permitted");
073        } catch (IllegalArgumentException e) {
074            assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX),
075                         e.getMessage());
076        }
077    }
078
079    /*
080     * Class to test for void DefaultOptionBuilder(String, String, boolean)
081     */
082    public void testNew_EmptyLongPrefix() {
083        try {
084            new DefaultOptionBuilder("-", "", false);
085            fail("empty long prefix is not permitted");
086        } catch (IllegalArgumentException e) {
087            assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX),
088                         e.getMessage());
089        }
090    }
091
092    public void testCreate() {
093        try {
094            this.defaultOptionBuilder.create();
095            fail("options must have a name");
096        } catch (IllegalStateException e) {
097            assertEquals(resources.getMessage(ResourceConstants.OPTION_NO_NAME), e.getMessage());
098        }
099
100        this.defaultOptionBuilder.withShortName("j");
101        this.defaultOptionBuilder.create();
102        this.defaultOptionBuilder.withLongName("jkeyes");
103        this.defaultOptionBuilder.create();
104
105        {
106            DefaultOptionBuilder builder = new DefaultOptionBuilder("-", "--", true);
107            builder.withShortName("mx");
108        }
109    }
110
111    public void testName() {
112        // withLongName && this.preferred != null
113        {
114            this.defaultOptionBuilder.withShortName("a");
115            this.defaultOptionBuilder.withLongName("apples");
116        }
117        // withShortName && this.preferred != null
118        {
119            this.defaultOptionBuilder.withLongName("apples");
120            this.defaultOptionBuilder.withShortName("a");
121        }
122        // withShortName && this.preferred != null
123        {
124            this.defaultOptionBuilder.withLongName("apples");
125            this.defaultOptionBuilder.withShortName("a");
126        }
127    }
128
129    public void testWithDescription() {
130        String description = "desc";
131        this.defaultOptionBuilder.withShortName("a");
132        this.defaultOptionBuilder.withDescription(description);
133
134        DefaultOption opt = this.defaultOptionBuilder.create();
135        assertEquals("wrong description found", description, opt.getDescription());
136    }
137
138    public void testWithRequired() {
139        {
140            boolean required = false;
141            this.defaultOptionBuilder.withShortName("a");
142            this.defaultOptionBuilder.withRequired(required);
143
144            DefaultOption opt = this.defaultOptionBuilder.create();
145            assertEquals("wrong required found", required, opt.isRequired());
146        }
147
148        {
149            boolean required = true;
150            this.defaultOptionBuilder.withShortName("a");
151            this.defaultOptionBuilder.withRequired(required);
152
153            DefaultOption opt = this.defaultOptionBuilder.create();
154            assertEquals("wrong required found", required, opt.isRequired());
155        }
156    }
157
158    public void testWithChildren() {
159        GroupBuilder gbuilder = new GroupBuilder();
160
161        this.defaultOptionBuilder.withShortName("a");
162        this.defaultOptionBuilder.withRequired(true);
163
164        DefaultOption opt = this.defaultOptionBuilder.create();
165
166        Group group = gbuilder.withName("withchildren").withOption(opt).create();
167
168        {
169            this.defaultOptionBuilder.withShortName("b");
170            this.defaultOptionBuilder.withChildren(group);
171
172            DefaultOption option = this.defaultOptionBuilder.create();
173            assertEquals("wrong children found", group, option.getChildren());
174        }
175    }
176
177    public void testWithArgument() {
178        ArgumentBuilder abuilder = new ArgumentBuilder();
179        abuilder.withName("myarg");
180
181        Argument arg = abuilder.create();
182
183        this.defaultOptionBuilder.withShortName("a");
184        this.defaultOptionBuilder.withRequired(true);
185        this.defaultOptionBuilder.withArgument(arg);
186
187        DefaultOption opt = this.defaultOptionBuilder.create();
188
189        assertEquals("wrong argument found", arg, opt.getArgument());
190    }
191
192    public void testWithId() {
193        this.defaultOptionBuilder.withShortName("a");
194        this.defaultOptionBuilder.withId(0);
195
196        DefaultOption opt = this.defaultOptionBuilder.create();
197
198        assertEquals("wrong id found", 0, opt.getId());
199    }
200}