View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.builder;
18  
19  import junit.framework.TestCase;
20  
21  import org.apache.commons.cli2.Argument;
22  import org.apache.commons.cli2.Group;
23  import org.apache.commons.cli2.option.DefaultOption;
24  import org.apache.commons.cli2.resource.ResourceConstants;
25  import org.apache.commons.cli2.resource.ResourceHelper;
26  
27  public class DefaultOptionBuilderTest
28      extends TestCase {
29      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
30      private DefaultOptionBuilder defaultOptionBuilder;
31  
32      /*
33       * @see TestCase#setUp()
34       */
35      protected void setUp()
36          throws Exception {
37          this.defaultOptionBuilder = new DefaultOptionBuilder();
38      }
39  
40      /*
41       * Class to test for void DefaultOptionBuilder(String, String, boolean)
42       */
43      public void testNew_NullShortPrefix() {
44          try {
45              new DefaultOptionBuilder(null, null, false);
46              fail("null short prefix is not permitted");
47          } catch (IllegalArgumentException e) {
48              assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX),
49                           e.getMessage());
50          }
51      }
52  
53      /*
54       * Class to test for void DefaultOptionBuilder(String, String, boolean)
55       */
56      public void testNew_EmptyShortPrefix() {
57          try {
58              new DefaultOptionBuilder("", null, false);
59              fail("empty short prefix is not permitted");
60          } catch (IllegalArgumentException e) {
61              assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX),
62                           e.getMessage());
63          }
64      }
65  
66      /*
67       * Class to test for void DefaultOptionBuilder(String, String, boolean)
68       */
69      public void testNew_NullLongPrefix() {
70          try {
71              new DefaultOptionBuilder("-", null, false);
72              fail("null long prefix is not permitted");
73          } catch (IllegalArgumentException e) {
74              assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX),
75                           e.getMessage());
76          }
77      }
78  
79      /*
80       * Class to test for void DefaultOptionBuilder(String, String, boolean)
81       */
82      public void testNew_EmptyLongPrefix() {
83          try {
84              new DefaultOptionBuilder("-", "", false);
85              fail("empty long prefix is not permitted");
86          } catch (IllegalArgumentException e) {
87              assertEquals(resources.getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX),
88                           e.getMessage());
89          }
90      }
91  
92      public void testCreate() {
93          try {
94              this.defaultOptionBuilder.create();
95              fail("options must have a name");
96          } catch (IllegalStateException e) {
97              assertEquals(resources.getMessage(ResourceConstants.OPTION_NO_NAME), e.getMessage());
98          }
99  
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 }