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