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.validation;
018
019import java.text.NumberFormat;
020
021import java.util.Arrays;
022import java.util.Iterator;
023import java.util.List;
024
025import junit.framework.TestCase;
026
027import org.apache.commons.cli2.resource.ResourceConstants;
028import org.apache.commons.cli2.resource.ResourceHelper;
029
030/**
031 * JUnit test case for NumberValidator.
032 *
033 * @author Rob Oxspring
034 * @author John Keyes
035 */
036public class NumberValidatorTest
037    extends TestCase {
038    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
039
040    public void testValidate_Number()
041        throws InvalidArgumentException {
042        final NumberFormat format = NumberFormat.getNumberInstance();
043
044        final Object[] array =
045            new Object[] { format.format(1d), format.format(1.07d), format.format(-.45d) };
046
047        {
048            final List list = Arrays.asList(array);
049            final Validator validator = NumberValidator.getNumberInstance();
050
051            validator.validate(list);
052
053            final Iterator i = list.iterator();
054            assertEquals(1d, ((Number) i.next()).doubleValue(), 0.0001);
055            assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
056            assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
057            assertFalse(i.hasNext());
058        }
059    }
060
061    public void testValidate_Currency()
062        throws InvalidArgumentException {
063        NumberFormat format = NumberFormat.getCurrencyInstance();
064        final Object[] array =
065            new Object[] { format.format(1d), format.format(1.07), format.format(-0.45) };
066        final List list = Arrays.asList(array);
067
068        final NumberValidator validator = NumberValidator.getCurrencyInstance();
069        assertEquals("incorrect currency format", format, validator.getFormat());
070
071        validator.validate(list);
072
073        final Iterator i = list.iterator();
074        assertEquals(1d, ((Number) i.next()).doubleValue(), 0.0001);
075        assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
076        assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
077        assertFalse(i.hasNext());
078    }
079
080    public void testValidate_Percent()
081        throws InvalidArgumentException {
082        final NumberFormat format = NumberFormat.getPercentInstance();
083
084        final Object[] array =
085            new Object[] {
086                             format.format(.01), format.format(1.07), format.format(-.45),
087                             format.format(0.001)
088            };
089        final List list = Arrays.asList(array);
090        final Validator validator = NumberValidator.getPercentInstance();
091
092        validator.validate(list);
093
094        final Iterator i = list.iterator();
095        assertEquals(0.01d, ((Number) i.next()).doubleValue(), 0.0001);
096        assertEquals(1.07d, ((Number) i.next()).doubleValue(), 0.0001);
097        assertEquals(-.45d, ((Number) i.next()).doubleValue(), 0.0001);
098        assertEquals(0.00001d, ((Number) i.next()).doubleValue(), 0.0001);
099        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}