001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.cli2.builder; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import junit.framework.TestCase; 025 026import org.apache.commons.cli2.option.ArgumentImpl; 027import org.apache.commons.cli2.resource.ResourceConstants; 028import org.apache.commons.cli2.resource.ResourceHelper; 029import org.apache.commons.cli2.validation.DateValidator; 030import org.apache.commons.cli2.validation.Validator; 031 032public class ArgumentBuilderTest 033 extends TestCase { 034 private static final ResourceHelper resources = ResourceHelper.getResourceHelper(); 035 private ArgumentBuilder argumentBuilder; 036 037 /* 038 * @see TestCase#setUp() 039 */ 040 protected void setUp() 041 throws Exception { 042 this.argumentBuilder = new ArgumentBuilder(); 043 } 044 045 public void testConsumeRemaining() { 046 this.argumentBuilder.withConsumeRemaining("--"); 047 this.argumentBuilder.withName("arg"); 048 049 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 050 051 assertEquals("incorrect consume remaining token", "--", arg.getConsumeRemaining()); 052 } 053 054 public void testNullConsumeRemaining() { 055 try { 056 this.argumentBuilder.withConsumeRemaining(null); 057 fail("cannot use null consume remaining token"); 058 } catch (IllegalArgumentException exp) { 059 assertEquals("wrong exception message", 060 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING), 061 exp.getMessage()); 062 } 063 } 064 065 public void testEmptyConsumeRemaining() { 066 try { 067 this.argumentBuilder.withConsumeRemaining(""); 068 fail("cannot use empty string consume remaining token"); 069 } catch (IllegalArgumentException exp) { 070 assertEquals("wrong exception message", 071 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING), 072 exp.getMessage()); 073 } 074 } 075 076 public void testDefault() { 077 this.argumentBuilder.withDefault("defaultString"); 078 this.argumentBuilder.withName("arg"); 079 080 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 081 082 assertEquals("incorrect number of default values", 1, arg.getDefaultValues().size()); 083 assertEquals("incorrect default value", "defaultString", arg.getDefaultValues().get(0)); 084 } 085 086 public void testDefaultX2() { 087 this.argumentBuilder.withDefault("defaultString1"); 088 this.argumentBuilder.withDefault("defaultString2"); 089 this.argumentBuilder.withName("arg"); 090 091 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 092 093 assertEquals("incorrect number of default values", 2, arg.getDefaultValues().size()); 094 assertEquals("incorrect default value-1", "defaultString1", arg.getDefaultValues().get(0)); 095 assertEquals("incorrect default value-2", "defaultString2", arg.getDefaultValues().get(1)); 096 } 097 098 public void testNullDefault() { 099 try { 100 this.argumentBuilder.withDefault(null); 101 fail("cannot use null default"); 102 } catch (IllegalArgumentException exp) { 103 assertEquals("wrong exception message", 104 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT), 105 exp.getMessage()); 106 } 107 } 108 109 public void testDefaults() { 110 final List defaults = new ArrayList(); 111 defaults.add("one"); 112 defaults.add("two"); 113 114 this.argumentBuilder.withDefaults(defaults); 115 this.argumentBuilder.withName("arg"); 116 117 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 118 119 assertEquals("incorrect number of default values", 2, arg.getDefaultValues().size()); 120 assertEquals("incorrect default value-1", "one", arg.getDefaultValues().get(0)); 121 assertEquals("incorrect default value-2", "two", arg.getDefaultValues().get(1)); 122 assertEquals("incorrect default values list", defaults, arg.getDefaultValues()); 123 124 } 125 126 public void testNullDefaults() { 127 try { 128 this.argumentBuilder.withDefaults(null); 129 fail("cannot use null defaults"); 130 } catch (IllegalArgumentException exp) { 131 assertEquals("wrong exception message", 132 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS), 133 exp.getMessage()); 134 } 135 } 136 137 public void testId() { 138 this.argumentBuilder.withId(1); 139 this.argumentBuilder.withName("arg"); 140 141 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 142 143 assertEquals("incorrect id", 1, arg.getId()); 144 } 145 146 public void testInitialSeparator() { 147 this.argumentBuilder.withInitialSeparator(','); 148 this.argumentBuilder.withName("arg"); 149 150 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 151 152 assertEquals("incorrect initial separator", ',', arg.getInitialSeparator()); 153 } 154 155 public void testMaximum() { 156 this.argumentBuilder.withMaximum(1); 157 this.argumentBuilder.withName("arg"); 158 159 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 160 161 assertEquals("incorrect maximum", 1, arg.getMaximum()); 162 } 163 164 public void testNegativeMaximum() { 165 try { 166 this.argumentBuilder.withMaximum(-1); 167 fail("cannot use negative maximum"); 168 } catch (IllegalArgumentException exp) { 169 assertEquals("wrong exception message", 170 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM), 171 exp.getMessage()); 172 } 173 } 174 175 public void testMinimum() { 176 this.argumentBuilder.withMinimum(1); 177 this.argumentBuilder.withName("arg"); 178 179 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 180 181 assertEquals("incorrect maximum", 1, arg.getMinimum()); 182 } 183 184 public void testNegativeMinimum() { 185 try { 186 this.argumentBuilder.withMinimum(-1); 187 fail("cannot use negative minimum"); 188 } catch (IllegalArgumentException exp) { 189 assertEquals("wrong exception message", 190 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM), 191 exp.getMessage()); 192 } 193 } 194 195 public void testName() { 196 this.argumentBuilder.withName("arg"); 197 198 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 199 200 assertEquals("incorrect preferred name", "arg", arg.getPreferredName()); 201 } 202 203 public void testNullName() { 204 try { 205 this.argumentBuilder.withName(null); 206 fail("cannot use null name"); 207 } catch (IllegalArgumentException exp) { 208 assertEquals("wrong exception message", 209 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME), 210 exp.getMessage()); 211 } 212 } 213 214 public void testEmptyName() { 215 try { 216 this.argumentBuilder.withName(""); 217 fail("cannot use empty name"); 218 } catch (IllegalArgumentException exp) { 219 assertEquals("wrong exception message", 220 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME), 221 exp.getMessage()); 222 } 223 } 224 225 public void testSubsequentSeparator() { 226 this.argumentBuilder.withSubsequentSeparator(':'); 227 this.argumentBuilder.withName("arg"); 228 229 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 230 231 assertEquals("incorrect subsequent separator", ':', arg.getSubsequentSeparator()); 232 } 233 234 public void testValidator() { 235 Validator validator = DateValidator.getDateInstance(); 236 this.argumentBuilder.withValidator(validator); 237 this.argumentBuilder.withName("arg"); 238 239 ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create(); 240 241 assertEquals("incorrect validator", validator, arg.getValidator()); 242 } 243 244 public void testNullValidator() { 245 try { 246 this.argumentBuilder.withValidator(null); 247 fail("cannot use null validator"); 248 } catch (IllegalArgumentException exp) { 249 assertEquals("wrong exception message", 250 resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR), 251 exp.getMessage()); 252 } 253 } 254}