Source Code Cross Referenced for NumberConverterTestBase.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » converters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 java.math.BigDecimal;
021:        import java.math.BigInteger;
022:        import java.util.Calendar;
023:        import java.util.Date;
024:        import java.util.Locale;
025:
026:        import junit.framework.TestCase;
027:
028:        import org.apache.commons.beanutils.ConversionException;
029:
030:        /**
031:         * Abstract base for <Number>Converter classes.
032:         *
033:         * @author Rodney Waldhoff
034:         * @version $Revision: 541692 $ $Date: 2007-05-25 16:34:19 +0100 (Fri, 25 May 2007) $
035:         */
036:
037:        public abstract class NumberConverterTestBase extends TestCase {
038:
039:            /** Test Number values */
040:            protected Number[] numbers = new Number[4];
041:
042:            // ------------------------------------------------------------------------
043:
044:            public NumberConverterTestBase(String name) {
045:                super (name);
046:            }
047:
048:            // ------------------------------------------------------------------------
049:
050:            protected abstract NumberConverter makeConverter();
051:
052:            protected abstract NumberConverter makeConverter(Object defaultValue);
053:
054:            protected abstract Class getExpectedType();
055:
056:            // ------------------------------------------------------------------------
057:
058:            /**
059:             * Assumes ConversionException in response to covert(getExpectedType(),null).
060:             */
061:            public void testConvertNull() {
062:                try {
063:                    makeConverter().convert(getExpectedType(), null);
064:                    fail("Expected ConversionException");
065:                } catch (ConversionException e) {
066:                    // expected
067:                }
068:            }
069:
070:            /**
071:             * Assumes convert(getExpectedType(),Number) returns some non-null
072:             * instance of getExpectedType().
073:             */
074:            public void testConvertNumber() {
075:                String[] message = { "from Byte", "from Short", "from Integer",
076:                        "from Long", "from Float", "from Double",
077:                        "from BigDecimal", "from BigInteger",
078:                        "from Integer array", };
079:
080:                Object[] number = {
081:                        new Byte((byte) 7),
082:                        new Short((short) 8),
083:                        new Integer(9),
084:                        new Long(10),
085:                        new Float(11.1),
086:                        new Double(12.2),
087:                        new BigDecimal("17.2"),
088:                        new BigInteger("33"),
089:                        new Integer[] { new Integer(3), new Integer(2),
090:                                new Integer(1) } };
091:
092:                for (int i = 0; i < number.length; i++) {
093:                    Object val = makeConverter().convert(getExpectedType(),
094:                            number[i]);
095:                    assertNotNull("Convert " + message[i]
096:                            + " should not be null", val);
097:                    assertTrue("Convert " + message[i] + " should return a "
098:                            + getExpectedType().getName(), getExpectedType()
099:                            .isInstance(val));
100:                }
101:            }
102:
103:            /**
104:             * Convert Number --> String (using a Pattern, with default and specified Locales)  
105:             */
106:            public void testNumberToStringPattern() {
107:
108:                // Re-set the default Locale to Locale.US
109:                Locale defaultLocale = Locale.getDefault();
110:                Locale.setDefault(Locale.US);
111:
112:                NumberConverter converter = makeConverter();
113:                converter.setPattern("[0,0.0];(0,0.0)");
114:
115:                // Default Locale
116:                assertEquals("Default Locale " + numbers[0], "(12.0)",
117:                        converter.convert(String.class, numbers[0]));
118:                assertEquals("Default Locale " + numbers[1], "[13.0]",
119:                        converter.convert(String.class, numbers[1]));
120:
121:                // Locale.GERMAN
122:                converter.setLocale(Locale.GERMAN);
123:                assertEquals("Locale.GERMAN " + numbers[2], "(22,0)", converter
124:                        .convert(String.class, numbers[2]));
125:                assertEquals("Locale.GERMAN " + numbers[3], "[23,0]", converter
126:                        .convert(String.class, numbers[3]));
127:
128:                // Restore the default Locale
129:                Locale.setDefault(defaultLocale);
130:            }
131:
132:            /**
133:             * Convert Number --> String (using default and specified Locales)  
134:             */
135:            public void testNumberToStringLocale() {
136:
137:                // Re-set the default Locale to Locale.US
138:                Locale defaultLocale = Locale.getDefault();
139:                Locale.setDefault(Locale.US);
140:
141:                NumberConverter converter = makeConverter();
142:                converter.setUseLocaleFormat(true);
143:
144:                // Default Locale
145:                assertEquals("Default Locale " + numbers[0], "-12", converter
146:                        .convert(String.class, numbers[0]));
147:                assertEquals("Default Locale " + numbers[1], "13", converter
148:                        .convert(String.class, numbers[1]));
149:
150:                // Locale.GERMAN
151:                converter.setLocale(Locale.GERMAN);
152:                assertEquals("Locale.GERMAN " + numbers[2], "-22", converter
153:                        .convert(String.class, numbers[2]));
154:                assertEquals("Locale.GERMAN " + numbers[3], "23", converter
155:                        .convert(String.class, numbers[3]));
156:
157:                // Restore the default Locale
158:                Locale.setDefault(defaultLocale);
159:            }
160:
161:            /**
162:             * Convert Array --> Number
163:             */
164:            public void testStringArrayToInteger() {
165:
166:                Integer defaultValue = new Integer(-1);
167:                NumberConverter converter = makeConverter(defaultValue);
168:
169:                // Default Locale
170:                assertEquals("Valid First", new Integer(5), converter.convert(
171:                        Integer.class, new String[] { "5", "4", "3" }));
172:                assertEquals("Invalid First", defaultValue, converter.convert(
173:                        Integer.class, new String[] { "FOO", "1", "2" }));
174:                assertEquals("Null First", defaultValue, converter.convert(
175:                        Integer.class, new String[] { null, "1", "2" }));
176:                assertEquals("Long Array", new Integer(9), converter.convert(
177:                        Integer.class, new long[] { 9, 2, 6 }));
178:            }
179:
180:            /**
181:             * Convert Number --> String (default conversion)  
182:             */
183:            public void testNumberToStringDefault() {
184:
185:                NumberConverter converter = makeConverter();
186:
187:                // Default Number --> String conversion
188:                assertEquals("Default Convert " + numbers[0], numbers[0]
189:                        .toString(), converter
190:                        .convert(String.class, numbers[0]));
191:                assertEquals("Default Convert " + numbers[1], numbers[1]
192:                        .toString(), converter
193:                        .convert(String.class, numbers[1]));
194:
195:            }
196:
197:            /**
198:             * Convert String --> Number (using a Pattern, with default and specified Locales)  
199:             */
200:            public void testStringToNumberPattern() {
201:
202:                // Re-set the default Locale to Locale.US
203:                Locale defaultLocale = Locale.getDefault();
204:                Locale.setDefault(Locale.US);
205:
206:                NumberConverter converter = makeConverter();
207:                converter.setPattern("[0,0];(0,0)");
208:
209:                // Default Locale
210:                assertEquals("Default Locale " + numbers[0], numbers[0],
211:                        converter.convert(getExpectedType(), "(1,2)"));
212:                assertEquals("Default Locale " + numbers[1], numbers[1],
213:                        converter.convert(getExpectedType(), "[1,3]"));
214:
215:                // Locale.GERMAN
216:                converter.setLocale(Locale.GERMAN);
217:                assertEquals("Locale.GERMAN " + numbers[2], numbers[2],
218:                        converter.convert(getExpectedType(), "(2.2)"));
219:                assertEquals("Locale.GERMAN " + numbers[3], numbers[3],
220:                        converter.convert(getExpectedType(), "[2.3]"));
221:
222:                // Invalid Value
223:                try {
224:                    converter.convert(getExpectedType(), "1,2");
225:                    fail("Expected invalid value to cause ConversionException");
226:                } catch (Exception e) {
227:                    // expected result
228:                }
229:
230:                // Invalid Type (will try via String)
231:                Object obj = new Object() {
232:                    public String toString() {
233:                        return "dsdgsdsdg";
234:                    }
235:                };
236:                try {
237:                    converter.convert(getExpectedType(), obj);
238:                    fail("Expected invalid value to cause ConversionException");
239:                } catch (Exception e) {
240:                    // expected result
241:                }
242:
243:                // Restore the default Locale
244:                Locale.setDefault(defaultLocale);
245:            }
246:
247:            /**
248:             * Convert String --> Number (using default and specified Locales)  
249:             */
250:            public void testStringToNumberLocale() {
251:
252:                // Re-set the default Locale to Locale.US
253:                Locale defaultLocale = Locale.getDefault();
254:                Locale.setDefault(Locale.US);
255:
256:                NumberConverter converter = makeConverter();
257:                converter.setUseLocaleFormat(true);
258:
259:                // Default Locale
260:                assertEquals("Default Locale " + numbers[0], numbers[0],
261:                        converter.convert(getExpectedType(), "-0,012"));
262:                assertEquals("Default Locale " + numbers[1], numbers[1],
263:                        converter.convert(getExpectedType(), "0,013"));
264:
265:                // Invalid Value
266:                try {
267:                    converter.convert(getExpectedType(), "0,02x");
268:                    fail("Expected invalid value to cause ConversionException");
269:                } catch (Exception e) {
270:                    // expected result
271:                }
272:
273:                // Locale.GERMAN
274:                converter.setLocale(Locale.GERMAN);
275:                assertEquals("Locale.GERMAN " + numbers[2], numbers[2],
276:                        converter.convert(getExpectedType(), "-0.022"));
277:                assertEquals("Locale.GERMAN " + numbers[3], numbers[3],
278:                        converter.convert(getExpectedType(), "0.023"));
279:
280:                // Invalid Value
281:                try {
282:                    converter.convert(getExpectedType(), "0.02x");
283:                    fail("Expected invalid value to cause ConversionException");
284:                } catch (Exception e) {
285:                    // expected result
286:                }
287:
288:                // Restore the default Locale
289:                Locale.setDefault(defaultLocale);
290:            }
291:
292:            /**
293:             * Convert String --> Number (default conversion)  
294:             */
295:            public void testStringToNumberDefault() {
296:
297:                NumberConverter converter = makeConverter();
298:                converter.setUseLocaleFormat(false);
299:
300:                // Default String --> Number conversion
301:                assertEquals("Default Convert " + numbers[0], numbers[0],
302:                        converter.convert(getExpectedType(), numbers[0]
303:                                .toString()));
304:
305:                // Invalid
306:                try {
307:                    converter.convert(getExpectedType(), "12x");
308:                    fail("Expected invalid value to cause ConversionException");
309:                } catch (Exception e) {
310:                    // expected result
311:                }
312:            }
313:
314:            /**
315:             * Convert Boolean --> Number (default conversion)  
316:             */
317:            public void testBooleanToNumberDefault() {
318:
319:                NumberConverter converter = makeConverter();
320:
321:                // Other type --> String conversion
322:                assertEquals("Boolean.FALSE to Number ", 0, ((Number) converter
323:                        .convert(getExpectedType(), Boolean.FALSE)).intValue());
324:                assertEquals("Boolean.TRUE to Number ", 1, ((Number) converter
325:                        .convert(getExpectedType(), Boolean.TRUE)).intValue());
326:
327:            }
328:
329:            /**
330:             * Convert Date --> Long
331:             */
332:            public void testDateToNumber() {
333:
334:                NumberConverter converter = makeConverter();
335:
336:                Date dateValue = new Date();
337:                long longValue = dateValue.getTime();
338:
339:                // Date --> Long conversion
340:                assertEquals("Date to Long", new Long(longValue), converter
341:                        .convert(Long.class, dateValue));
342:
343:                // Date --> Integer
344:                try {
345:                    converter.convert(Integer.class, dateValue);
346:                    fail("Date to Integer - expected a ConversionException");
347:                } catch (ConversionException e) {
348:                    // expected result - too large for Integer
349:                }
350:
351:            }
352:
353:            /**
354:             * Convert Calendar --> Long
355:             */
356:            public void testCalendarToNumber() {
357:
358:                NumberConverter converter = makeConverter();
359:
360:                Calendar calendarValue = Calendar.getInstance();
361:                long longValue = calendarValue.getTime().getTime();
362:
363:                // Calendar --> Long conversion
364:                assertEquals("Calendar to Long", new Long(longValue), converter
365:                        .convert(Long.class, calendarValue));
366:
367:                // Calendar --> Integer
368:                try {
369:                    converter.convert(Integer.class, calendarValue);
370:                    fail("Calendar to Integer - expected a ConversionException");
371:                } catch (ConversionException e) {
372:                    // expected result - too large for Integer
373:                }
374:
375:            }
376:
377:            /**
378:             * Convert Other --> String (default conversion)  
379:             */
380:            public void testOtherToStringDefault() {
381:
382:                NumberConverter converter = makeConverter();
383:
384:                // Other type --> String conversion
385:                assertEquals("Default Convert ", "ABC", converter.convert(
386:                        String.class, new StringBuffer("ABC")));
387:
388:            }
389:
390:            /**
391:             * Convert Number --> String (using default and specified Locales)  
392:             */
393:            public void testInvalidDefault() {
394:
395:                Object defaultvalue = numbers[0];
396:                NumberConverter converter = makeConverter(defaultvalue);
397:
398:                // Default String --> Number conversion
399:                assertEquals("Invalid null ", defaultvalue, converter.convert(
400:                        getExpectedType(), null));
401:                assertEquals("Default XXXX ", defaultvalue, converter.convert(
402:                        getExpectedType(), "XXXX"));
403:            }
404:
405:            /**
406:             * Convert Number --> String (using default and specified Locales)  
407:             */
408:            public void testInvalidException() {
409:
410:                NumberConverter converter = makeConverter();
411:
412:                try {
413:                    converter.convert(getExpectedType(), null);
414:                    fail("Null test, expected ConversionException");
415:                } catch (ConversionException e) {
416:                    // expected result
417:                }
418:                try {
419:                    converter.convert(getExpectedType(), "XXXX");
420:                    fail("Invalid test, expected ConversionException");
421:                } catch (ConversionException e) {
422:                    // expected result
423:                }
424:            }
425:
426:            /**
427:             * Test specifying an invalid type.  
428:             */
429:            public void testInvalidType() {
430:
431:                NumberConverter converter = makeConverter();
432:
433:                try {
434:                    converter.convert(Object.class, numbers[0]);
435:                    fail("Invalid type test, expected ConversionException");
436:                } catch (ConversionException e) {
437:                    // expected result
438:                }
439:            }
440:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.