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