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