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  
18  package org.apache.commons.beanutils.converters;
19  
20  import java.text.DecimalFormat;
21  import java.text.NumberFormat;
22  import java.util.Locale;
23  
24  import junit.framework.TestSuite;
25  
26  import org.apache.commons.beanutils.ConversionException;
27  import org.apache.commons.beanutils.Converter;
28  
29  
30  /***
31   * Test Case for the IntegerConverter class.
32   *
33   * @author Rodney Waldhoff
34   * @version $Revision: 471348 $ $Date: 2006-11-05 02:59:26 +0000 (Sun, 05 Nov 2006) $
35   */
36  
37  public class IntegerConverterTestCase extends NumberConverterTestBase {
38  
39      private Converter converter = null;
40  
41      // ------------------------------------------------------------------------
42  
43      public IntegerConverterTestCase(String name) {
44          super(name);
45      }
46      
47      // ------------------------------------------------------------------------
48  
49      public void setUp() throws Exception {
50          converter = makeConverter();
51          numbers[0] = new Integer("-12");
52          numbers[1] = new Integer("13");
53          numbers[2] = new Integer("-22");
54          numbers[3] = new Integer("23");
55      }
56  
57      public static TestSuite suite() {
58          return new TestSuite(IntegerConverterTestCase.class);        
59      }
60  
61      public void tearDown() throws Exception {
62          converter = null;
63      }
64  
65      // ------------------------------------------------------------------------
66      
67      protected NumberConverter makeConverter() {
68          return new IntegerConverter();
69      }
70      
71      protected NumberConverter makeConverter(Object defaultValue) {
72          return new IntegerConverter(defaultValue);
73      }
74      
75      protected Class getExpectedType() {
76          return Integer.class;
77      }
78  
79      // ------------------------------------------------------------------------
80  
81      public void testSimpleConversion() throws Exception {
82          String[] message= { 
83              "from String",
84              "from String",
85              "from String",
86              "from String",
87              "from String",
88              "from String",
89              "from String",
90              "from Byte",
91              "from Short",
92              "from Integer",
93              "from Long",
94              "from Float",
95              "from Double"
96          };
97          
98          Object[] input = { 
99              String.valueOf(Integer.MIN_VALUE),
100             "-17",
101             "-1",
102             "0",
103             "1",
104             "17",
105             String.valueOf(Integer.MAX_VALUE),
106             new Byte((byte)7),
107             new Short((short)8),
108             new Integer(9),
109             new Long(10),
110             new Float(11.1),
111             new Double(12.2)
112         };
113         
114         Integer[] expected = { 
115             new Integer(Integer.MIN_VALUE),
116             new Integer(-17),
117             new Integer(-1),
118             new Integer(0),
119             new Integer(1),
120             new Integer(17),
121             new Integer(Integer.MAX_VALUE),
122             new Integer(7),
123             new Integer(8),
124             new Integer(9),
125             new Integer(10),
126             new Integer(11),
127             new Integer(12)
128         };
129         
130         for(int i=0;i<expected.length;i++) {
131             assertEquals(message[i] + " to Integer",expected[i],converter.convert(Integer.class,input[i]));
132             assertEquals(message[i] + " to int",expected[i],converter.convert(Integer.TYPE,input[i]));
133             assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
134         }
135     }
136 
137     /***
138      * Test Invalid Amounts (too big/small)
139      */
140     public void testInvalidAmount() {
141         Converter converter = makeConverter();
142         Class clazz = Integer.class;
143 
144         Long min         = new Long(Integer.MIN_VALUE);
145         Long max         = new Long(Integer.MAX_VALUE);
146         Long minMinusOne = new Long(min.longValue() - 1);
147         Long maxPlusOne  = new Long(max.longValue() + 1);
148 
149         // Minimum
150         assertEquals("Minimum", new Integer(Integer.MIN_VALUE), converter.convert(clazz, min));
151 
152         // Maximum
153         assertEquals("Maximum", new Integer(Integer.MAX_VALUE), converter.convert(clazz, max));
154 
155         // Too Small
156         try {
157             assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
158             fail("Less than minimum, expected ConversionException");
159         } catch (Exception e) {
160             // expected result
161         }
162 
163         // Too Large
164         try {
165             assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
166             fail("More than maximum, expected ConversionException");
167         } catch (Exception e) {
168             // expected result
169         }
170     }
171 }
172