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.validation;
18  
19  import java.text.NumberFormat;
20  
21  import java.util.Arrays;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.cli2.resource.ResourceConstants;
28  import org.apache.commons.cli2.resource.ResourceHelper;
29  
30  /**
31   * JUnit test case for NumberValidator.
32   *
33   * @author Rob Oxspring
34   * @author John Keyes
35   */
36  public class NumberValidatorTest
37      extends TestCase {
38      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
39  
40      public void testValidate_Number()
41          throws InvalidArgumentException {
42          final NumberFormat format = NumberFormat.getNumberInstance();
43  
44          final Object[] array =
45              new Object[] { format.format(1d), format.format(1.07d), format.format(-.45d) };
46  
47          {
48              final List list = Arrays.asList(array);
49              final Validator validator = NumberValidator.getNumberInstance();
50  
51              validator.validate(list);
52  
53              final Iterator i = list.iterator();
54              assertEquals(1d, ((Number) i.next()).doubleValue(), 0.0001);
55              assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
56              assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
57              assertFalse(i.hasNext());
58          }
59      }
60  
61      public void testValidate_Currency()
62          throws InvalidArgumentException {
63          NumberFormat format = NumberFormat.getCurrencyInstance();
64          final Object[] array =
65              new Object[] { format.format(1d), format.format(1.07), format.format(-0.45) };
66          final List list = Arrays.asList(array);
67  
68          final NumberValidator validator = NumberValidator.getCurrencyInstance();
69          assertEquals("incorrect currency format", format, validator.getFormat());
70  
71          validator.validate(list);
72  
73          final Iterator i = list.iterator();
74          assertEquals(1d, ((Number) i.next()).doubleValue(), 0.0001);
75          assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
76          assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
77          assertFalse(i.hasNext());
78      }
79  
80      public void testValidate_Percent()
81          throws InvalidArgumentException {
82          final NumberFormat format = NumberFormat.getPercentInstance();
83  
84          final Object[] array =
85              new Object[] {
86                               format.format(.01), format.format(1.07), format.format(-.45),
87                               format.format(0.001)
88              };
89          final List list = Arrays.asList(array);
90          final Validator validator = NumberValidator.getPercentInstance();
91  
92          validator.validate(list);
93  
94          final Iterator i = list.iterator();
95          assertEquals(0.01d, ((Number) i.next()).doubleValue(), 0.0001);
96          assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
97          assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
98          assertEquals(0.00001d, ((Number) i.next()).doubleValue(), 0.0001);
99          assertFalse(i.hasNext());
100     }
101 
102     public void testValidate_Integer()
103         throws InvalidArgumentException {
104         final Object[] array = new Object[] { "1", "107", "-45" };
105         final List list = Arrays.asList(array);
106         final Validator validator = NumberValidator.getIntegerInstance();
107 
108         validator.validate(list);
109 
110         final Iterator i = list.iterator();
111         assertEquals(1d, ((Number) i.next()).doubleValue(), 0.0001);
112         assertEquals(107d, ((Number) i.next()).doubleValue(), 0.0001);
113         assertEquals(-45d, ((Number) i.next()).doubleValue(), 0.0001);
114         assertFalse(i.hasNext());
115     }
116 
117     public void testValidate_ExcessChars() {
118         final Object[] array = new Object[] { "10DowningStreet" };
119         final List list = Arrays.asList(array);
120         final Validator validator = NumberValidator.getIntegerInstance();
121 
122         try {
123             validator.validate(list);
124             fail("InvalidArgumentException");
125         } catch (InvalidArgumentException e) {
126             assertEquals("10DowningStreet", e.getMessage());
127         }
128     }
129 
130     public void testValidate_Maximum() {
131         final Object[] array = new Object[] { "1", "107" };
132         final List list = Arrays.asList(array);
133         final NumberValidator validator = NumberValidator.getIntegerInstance();
134         Integer max = new Integer(100);
135 
136         validator.setMaximum(max);
137 
138         assertTrue("no minimum set", validator.getMinimum() == null);
139         assertEquals("incorrect maximum value", max, validator.getMaximum());
140 
141         try {
142             validator.validate(list);
143             fail("107 too big");
144         } catch (InvalidArgumentException ive) {
145             assertEquals(resources.getMessage(ResourceConstants.NUMBERVALIDATOR_NUMBER_OUTOFRANGE,
146                                               "107"), ive.getMessage());
147         }
148     }
149 
150     public void testValidate_Minimum() {
151         final Object[] array = new Object[] { "107", "1" };
152         final List list = Arrays.asList(array);
153         final NumberValidator validator = NumberValidator.getIntegerInstance();
154         Integer min = new Integer(100);
155         validator.setMinimum(min);
156 
157         assertTrue("no maximum set", validator.getMaximum() == null);
158         assertEquals("incorrect minimum value", min, validator.getMinimum());
159 
160         try {
161             validator.validate(list);
162             fail("1 too small");
163         } catch (InvalidArgumentException ive) {
164             assertEquals(resources.getMessage(ResourceConstants.NUMBERVALIDATOR_NUMBER_OUTOFRANGE,
165                                               "1"), ive.getMessage());
166         }
167     }
168 }