001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.converters;
019:
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.beanutils.Converter;
023:
024: /**
025: * Test Case for the ByteConverter class.
026: *
027: * @author Rodney Waldhoff
028: * @version $Revision: 471348 $ $Date: 2006-11-05 02:59:26 +0000 (Sun, 05 Nov 2006) $
029: */
030:
031: public class ByteConverterTestCase extends NumberConverterTestBase {
032:
033: private Converter converter = null;
034:
035: // ------------------------------------------------------------------------
036:
037: public ByteConverterTestCase(String name) {
038: super (name);
039: }
040:
041: // ------------------------------------------------------------------------
042:
043: public void setUp() throws Exception {
044: converter = makeConverter();
045: numbers[0] = new Byte("-12");
046: numbers[1] = new Byte("13");
047: numbers[2] = new Byte("-22");
048: numbers[3] = new Byte("23");
049: }
050:
051: public static TestSuite suite() {
052: return new TestSuite(ByteConverterTestCase.class);
053: }
054:
055: public void tearDown() throws Exception {
056: converter = null;
057: }
058:
059: // ------------------------------------------------------------------------
060:
061: protected NumberConverter makeConverter() {
062: return new ByteConverter();
063: }
064:
065: protected NumberConverter makeConverter(Object defaultValue) {
066: return new ByteConverter(defaultValue);
067: }
068:
069: protected Class getExpectedType() {
070: return Byte.class;
071: }
072:
073: // ------------------------------------------------------------------------
074:
075: public void testSimpleConversion() throws Exception {
076: String[] message = { "from String", "from String",
077: "from String", "from String", "from String",
078: "from String", "from String", "from Byte",
079: "from Short", "from Integer", "from Long",
080: "from Float", "from Double" };
081:
082: Object[] input = { String.valueOf(Byte.MIN_VALUE), "-17", "-1",
083: "0", "1", "17", String.valueOf(Byte.MAX_VALUE),
084: new Byte((byte) 7), new Short((short) 8),
085: new Integer(9), new Long(10), new Float(11.1),
086: new Double(12.2) };
087:
088: Byte[] expected = { new Byte(Byte.MIN_VALUE),
089: new Byte((byte) -17), new Byte((byte) -1),
090: new Byte((byte) 0), new Byte((byte) 1),
091: new Byte((byte) 17), new Byte(Byte.MAX_VALUE),
092: new Byte((byte) 7), new Byte((byte) 8),
093: new Byte((byte) 9), new Byte((byte) 10),
094: new Byte((byte) 11), new Byte((byte) 12) };
095:
096: for (int i = 0; i < expected.length; i++) {
097: assertEquals(message[i] + " to Byte", expected[i],
098: converter.convert(Byte.class, input[i]));
099: assertEquals(message[i] + " to byte", expected[i],
100: converter.convert(Byte.TYPE, input[i]));
101: assertEquals(message[i] + " to null type", expected[i],
102: converter.convert(null, input[i]));
103: }
104: }
105:
106: /**
107: * Test Invalid Amounts (too big/small)
108: */
109: public void testInvalidAmount() {
110: Converter converter = makeConverter();
111: Class clazz = Byte.class;
112:
113: Long min = new Long(Byte.MIN_VALUE);
114: Long max = new Long(Byte.MAX_VALUE);
115: Long minMinusOne = new Long(min.longValue() - 1);
116: Long maxPlusOne = new Long(max.longValue() + 1);
117:
118: // Minimum
119: assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter
120: .convert(clazz, min));
121:
122: // Maximum
123: assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter
124: .convert(clazz, max));
125:
126: // Too Small
127: try {
128: assertEquals("Minimum - 1", null, converter.convert(clazz,
129: minMinusOne));
130: fail("Less than minimum, expected ConversionException");
131: } catch (Exception e) {
132: // expected result
133: }
134:
135: // Too Large
136: try {
137: assertEquals("Maximum + 1", null, converter.convert(clazz,
138: maxPlusOne));
139: fail("More than maximum, expected ConversionException");
140: } catch (Exception e) {
141: // expected result
142: }
143: }
144:
145: }
|