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