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.locale.converters;
019:
020: /**
021: * Test Case for the ByteLocaleConverter class.
022: *
023: * @author Niall Pemberton
024: * @version $Revision: 469728 $ $Date: 2006-11-01 01:08:34 +0000 (Wed, 01 Nov 2006) $
025: */
026:
027: public class ByteLocaleConverterTestCase extends
028: BaseLocaleConverterTestCase {
029:
030: // ---------------------------------------------------------- Constructors
031:
032: public ByteLocaleConverterTestCase(String name) {
033: super (name);
034: }
035:
036: // -------------------------------------------------- Overall Test Methods
037:
038: /**
039: * Set up instance variables required by this test case.
040: */
041: public void setUp() throws Exception {
042:
043: super .setUp();
044:
045: defaultIntegerPattern = "#,###";
046: defaultIntegerValue = ",123";
047:
048: localizedIntegerPattern = "#.###";
049: localizedIntegerValue = ".123";
050:
051: // Expected Values
052: expectedDecimalValue = "123.56";
053: expectedIntegerValue = "123";
054:
055: defaultValue = new Byte("99");
056: expectedValue = new Byte(expectedIntegerValue);
057:
058: }
059:
060: /**
061: * Tear down instance variables required by this test case.
062: */
063: public void tearDown() {
064: super .tearDown();
065: }
066:
067: // ------------------------------------------------------------------------
068:
069: /**
070: * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
071: */
072: public void testConstructorMain() {
073:
074: // ------------- Construct with localized pattern ------------
075: converter = new ByteLocaleConverter(defaultValue,
076: localizedLocale, localizedIntegerPattern, true);
077:
078: convertValueNoPattern(converter, "(A)", localizedIntegerValue,
079: expectedValue);
080: convertValueWithPattern(converter, "(A)",
081: localizedIntegerValue, localizedIntegerPattern,
082: expectedValue);
083: convertInvalid(converter, "(A)", defaultValue);
084: convertNull(converter, "(A)", defaultValue);
085:
086: // **************************************************************************
087: // Convert value in the wrong format - maybe you would expect it to throw an
088: // exception and return the default - it doesn't, DecimalFormat parses it
089: // quite happily turning ",123" into "0"
090: // I guess this is one of the limitations of DecimalFormat
091: // **************************************************************************
092: convertValueNoPattern(converter, "(B)", defaultIntegerValue,
093: new Byte("0"));
094:
095: // **************************************************************************
096: // Convert with non-localized pattern
097: // **************************************************************************
098: convertValueWithPattern(converter, "(B)", "123",
099: defaultIntegerPattern, new Byte("123"));
100: convertValueWithPattern(converter, "(B-2)",
101: localizedIntegerValue, defaultIntegerPattern,
102: defaultValue);
103:
104: // **************************************************************************
105: // Convert with specified type
106: //
107: // BaseLocaleConverter completely ignores the type - so even if we specify
108: // Double.class here it still returns a Byte.
109: // **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
110: // **************************************************************************
111: convertValueToType(converter, "(B)", Double.class,
112: localizedIntegerValue, localizedIntegerPattern,
113: expectedValue);
114:
115: // ------------- Construct with non-localized pattern ------------
116: converter = new ByteLocaleConverter(defaultValue,
117: localizedLocale, defaultIntegerPattern, false);
118:
119: convertValueNoPattern(converter, "(C)", localizedIntegerValue,
120: expectedValue);
121: convertValueWithPattern(converter, "(C)",
122: localizedIntegerValue, defaultIntegerPattern,
123: expectedValue);
124: convertInvalid(converter, "(C)", defaultValue);
125: convertNull(converter, "(C)", defaultValue);
126:
127: }
128:
129: /**
130: * Test Converter() constructor
131: *
132: * Uses the default locale, no default value
133: *
134: */
135: public void testConstructor_2() {
136:
137: // ------------- Construct using default locale ------------
138: converter = new ByteLocaleConverter();
139:
140: // Perform Tests
141: convertValueNoPattern(converter, defaultIntegerValue,
142: expectedValue);
143: convertValueWithPattern(converter, defaultIntegerValue,
144: defaultIntegerPattern, expectedValue);
145: convertInvalid(converter, null);
146: convertNull(converter, null);
147:
148: }
149:
150: /**
151: * Test Converter(locPattern) constructor
152: *
153: * Uses the default locale, no default value
154: *
155: */
156: public void testConstructor_3() {
157:
158: // ------------- Construct using localized pattern (default locale) --------
159: converter = new ByteLocaleConverter(true);
160:
161: // Perform Tests
162: convertValueNoPattern(converter, defaultIntegerValue,
163: expectedValue);
164: convertValueWithPattern(converter, defaultIntegerValue,
165: defaultIntegerPattern, expectedValue);
166: convertInvalid(converter, null);
167: convertNull(converter, null);
168:
169: }
170:
171: /**
172: * Test Converter(Locale) constructor
173: */
174: public void testConstructor_4() {
175:
176: // ------------- Construct using specified Locale --------
177: converter = new ByteLocaleConverter(localizedLocale);
178:
179: // Perform Tests
180: convertValueNoPattern(converter, localizedIntegerValue,
181: expectedValue);
182: convertValueWithPattern(converter, localizedIntegerValue,
183: defaultIntegerPattern, expectedValue);
184: convertInvalid(converter, null);
185: convertNull(converter, null);
186:
187: }
188:
189: /**
190: * Test Converter(Locale, locPattern) constructor
191: */
192: public void testConstructor_5() {
193:
194: // ------------- Construct using specified Locale --------
195: converter = new ByteLocaleConverter(localizedLocale, true);
196:
197: // Perform Tests
198: convertValueNoPattern(converter, localizedIntegerValue,
199: expectedValue);
200: convertValueWithPattern(converter, localizedIntegerValue,
201: localizedIntegerPattern, expectedValue);
202: convertInvalid(converter, null);
203: convertNull(converter, null);
204:
205: }
206:
207: /**
208: * Test Converter(Locale, pattern) constructor
209: */
210: public void testConstructor_6() {
211:
212: // ------------- Construct using specified Locale --------
213: converter = new ByteLocaleConverter(localizedLocale,
214: defaultIntegerPattern);
215:
216: // Perform Tests
217: convertValueNoPattern(converter, localizedIntegerValue,
218: expectedValue);
219: convertValueWithPattern(converter, localizedIntegerValue,
220: defaultIntegerPattern, expectedValue);
221: convertInvalid(converter, null);
222: convertNull(converter, null);
223:
224: }
225:
226: /**
227: * Test Converter(Locale, pattern, locPattern) constructor
228: */
229: public void testConstructor_7() {
230:
231: // ------------- Construct using specified Locale --------
232: converter = new ByteLocaleConverter(localizedLocale,
233: localizedIntegerPattern, true);
234:
235: // Perform Tests
236: convertValueNoPattern(converter, localizedIntegerValue,
237: expectedValue);
238: convertValueWithPattern(converter, localizedIntegerValue,
239: localizedIntegerPattern, expectedValue);
240: convertInvalid(converter, null);
241: convertNull(converter, null);
242:
243: }
244:
245: /**
246: * Test Converter(defaultValue) constructor
247: */
248: public void testConstructor_8() {
249:
250: // ------------- Construct using specified Locale --------
251: converter = new ByteLocaleConverter(defaultValue);
252:
253: // Perform Tests
254: convertValueNoPattern(converter, defaultIntegerValue,
255: expectedValue);
256: convertValueWithPattern(converter, defaultIntegerValue,
257: defaultIntegerPattern, expectedValue);
258: convertInvalid(converter, defaultValue);
259: convertNull(converter, defaultValue);
260:
261: }
262:
263: /**
264: * Test Converter(defaultValue, locPattern) constructor
265: */
266: public void testConstructor_9() {
267:
268: // ------------- Construct using specified Locale --------
269: converter = new ByteLocaleConverter(defaultValue, true);
270:
271: // Perform Tests
272: convertValueNoPattern(converter, defaultIntegerValue,
273: expectedValue);
274: convertValueWithPattern(converter, defaultIntegerValue,
275: defaultIntegerPattern, expectedValue);
276: convertInvalid(converter, defaultValue);
277: convertNull(converter, defaultValue);
278:
279: }
280:
281: }
|