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 LongConverter class.
27   *
28   * @version $Id$
29   */
30  
31  public class LongConverterTestCase extends NumberConverterTestBase {
32  
33      private Converter converter = null;
34  
35      // ------------------------------------------------------------------------
36  
37      public LongConverterTestCase(final String name) {
38          super(name);
39      }
40  
41      // ------------------------------------------------------------------------
42  
43      @Override
44      public void setUp() throws Exception {
45          converter = makeConverter();
46          numbers[0] = new Long("-12");
47          numbers[1] = new Long("13");
48          numbers[2] = new Long("-22");
49          numbers[3] = new Long("23");
50      }
51  
52      public static TestSuite suite() {
53          return new TestSuite(LongConverterTestCase.class);
54      }
55  
56      @Override
57      public void tearDown() throws Exception {
58          converter = null;
59      }
60  
61      // ------------------------------------------------------------------------
62  
63      @Override
64      protected NumberConverter makeConverter() {
65          return new LongConverter();
66      }
67  
68      @Override
69      protected NumberConverter makeConverter(final Object defaultValue) {
70          return new LongConverter(defaultValue);
71      }
72  
73      @Override
74      protected Class<?> getExpectedType() {
75          return Long.class;
76      }
77  
78      // ------------------------------------------------------------------------
79  
80      public void testSimpleConversion() throws Exception {
81          final String[] message= {
82              "from String",
83              "from String",
84              "from String",
85              "from String",
86              "from String",
87              "from String",
88              "from String",
89              "from Byte",
90              "from Short",
91              "from Integer",
92              "from Long",
93              "from Float",
94              "from Double"
95          };
96  
97          final Object[] input = {
98              String.valueOf(Long.MIN_VALUE),
99              "-17",
100             "-1",
101             "0",
102             "1",
103             "17",
104             String.valueOf(Long.MAX_VALUE),
105             new Byte((byte)7),
106             new Short((short)8),
107             new Integer(9),
108             new Long(10),
109             new Float(11.1),
110             new Double(12.2)
111         };
112 
113         final Long[] expected = {
114             new Long(Long.MIN_VALUE),
115             new Long(-17),
116             new Long(-1),
117             new Long(0),
118             new Long(1),
119             new Long(17),
120             new Long(Long.MAX_VALUE),
121             new Long(7),
122             new Long(8),
123             new Long(9),
124             new Long(10),
125             new Long(11),
126             new Long(12)
127         };
128 
129         for(int i=0;i<expected.length;i++) {
130             assertEquals(message[i] + " to Long",expected[i],converter.convert(Long.class,input[i]));
131             assertEquals(message[i] + " to long",expected[i],converter.convert(Long.TYPE,input[i]));
132             assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
133         }
134     }
135 
136 }
137