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  
18  package org.apache.commons.beanutils.converters;
19  
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.beanutils.ConversionException;
23  import org.apache.commons.beanutils.Converter;
24  
25  
26  /**
27   * Test Case for the IntegerConverter class.
28   *
29   * @version $Id$
30   */
31  
32  public class IntegerConverterTestCase extends NumberConverterTestBase {
33  
34      private Converter converter = null;
35  
36      // ------------------------------------------------------------------------
37  
38      public IntegerConverterTestCase(final String name) {
39          super(name);
40      }
41  
42      // ------------------------------------------------------------------------
43  
44      @Override
45      public void setUp() throws Exception {
46          converter = makeConverter();
47          numbers[0] = new Integer("-12");
48          numbers[1] = new Integer("13");
49          numbers[2] = new Integer("-22");
50          numbers[3] = new Integer("23");
51      }
52  
53      public static TestSuite suite() {
54          return new TestSuite(IntegerConverterTestCase.class);
55      }
56  
57      @Override
58      public void tearDown() throws Exception {
59          converter = null;
60      }
61  
62      // ------------------------------------------------------------------------
63  
64      @Override
65      protected NumberConverter makeConverter() {
66          return new IntegerConverter();
67      }
68  
69      @Override
70      protected NumberConverter makeConverter(final Object defaultValue) {
71          return new IntegerConverter(defaultValue);
72      }
73  
74      @Override
75      protected Class<?> getExpectedType() {
76          return Integer.class;
77      }
78  
79      // ------------------------------------------------------------------------
80  
81      public void testSimpleConversion() throws Exception {
82          final 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          final 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         final 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         final Converter converter = makeConverter();
142         final Class<?> clazz = Integer.class;
143 
144         final Long min         = new Long(Integer.MIN_VALUE);
145         final Long max         = new Long(Integer.MAX_VALUE);
146         final Long minMinusOne = new Long(min.longValue() - 1);
147         final 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 (final 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 (final Exception e) {
168             // expected result
169         }
170     }
171 
172     /**
173      * Tests whether an invalid default object causes an exception.
174      */
175     public void testInvalidDefaultObject() {
176         final NumberConverter converter = makeConverter();
177         try {
178             converter.setDefaultValue("notANumber");
179             fail("Invalid default value not detected!");
180         } catch (final ConversionException cex) {
181             // expected result
182         }
183     }
184 }
185