Source Code Cross Referenced for ArrayConverterTestCase.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:        package org.apache.commons.beanutils.converters;
018:
019:        import java.lang.reflect.Array;
020:        import java.util.ArrayList;
021:        import java.util.Locale;
022:
023:        import org.apache.commons.beanutils.ConversionException;
024:
025:        import junit.framework.TestCase;
026:        import junit.framework.TestSuite;
027:
028:        /**
029:         * Test Case for the ArrayConverter class.
030:         *
031:         * @version $Revision: 539812 $ $Date: 2007-05-20 02:13:29 +0100 (Sun, 20 May 2007) $
032:         */
033:        public class ArrayConverterTestCase extends TestCase {
034:
035:            /**
036:             * Construct a new Array Converter test case.
037:             * @param name Test Name
038:             */
039:            public ArrayConverterTestCase(String name) {
040:                super (name);
041:            }
042:
043:            // ------------------------------------------------------------------------
044:
045:            /**
046:             * Create Test Suite
047:             * @return test suite
048:             */
049:            public static TestSuite suite() {
050:                return new TestSuite(ArrayConverterTestCase.class);
051:            }
052:
053:            /** Set Up */
054:            public void setUp() throws Exception {
055:            }
056:
057:            /** Tear Down */
058:            public void tearDown() throws Exception {
059:            }
060:
061:            // ------------------------------------------------------------------------
062:
063:            /**
064:             * Test Converting using the IntegerConverter as the component Converter
065:             */
066:            public void testComponentIntegerConverter() {
067:
068:                IntegerConverter intConverter = new IntegerConverter(
069:                        new Integer(0));
070:                intConverter.setPattern("#,###");
071:                intConverter.setLocale(Locale.US);
072:                ArrayConverter arrayConverter = new ArrayConverter(int[].class,
073:                        intConverter, 0);
074:                arrayConverter.setAllowedChars(new char[] { ',', '-' });
075:                arrayConverter.setDelimiter(';');
076:
077:                // Expected results
078:                int[] intArray = new int[] { 1111, 2222, 3333, 4444 };
079:                String stringA = "1,111; 2,222; 3,333; 4,444";
080:                String stringB = intArray[0] + ";" + intArray[1] + ";"
081:                        + intArray[2] + ";" + intArray[3];
082:                String[] strArray = new String[] { "" + intArray[0],
083:                        "" + intArray[1], "" + intArray[2], "" + intArray[3] };
084:                long[] longArray = new long[] { intArray[0], intArray[1],
085:                        intArray[2], intArray[3] };
086:                Long[] LONGArray = new Long[] { new Long(intArray[0]),
087:                        new Long(intArray[1]), new Long(intArray[2]),
088:                        new Long(intArray[3]) };
089:                Integer[] IntegerArray = new Integer[] {
090:                        new Integer(intArray[0]), new Integer(intArray[1]),
091:                        new Integer(intArray[2]), new Integer(intArray[3]) };
092:                ArrayList strList = new ArrayList();
093:                ArrayList longList = new ArrayList();
094:                for (int i = 0; i < strArray.length; i++) {
095:                    strList.add(strArray[i]);
096:                    longList.add(LONGArray[i]);
097:                }
098:
099:                String msg = null;
100:
101:                // String --> int[]
102:                try {
103:                    msg = "String --> int[]";
104:                    checkArray(msg, intArray, arrayConverter.convert(
105:                            int[].class, stringA));
106:                } catch (Exception e) {
107:                    fail(msg + " failed " + e);
108:                }
109:
110:                // String --> int[] (with braces)
111:                try {
112:                    msg = "String --> Integer[] (with braces)";
113:                    checkArray(msg, IntegerArray, arrayConverter.convert(
114:                            Integer[].class, "{" + stringA + "}"));
115:                } catch (Exception e) {
116:                    fail(msg + " failed " + e);
117:                }
118:
119:                // String[] --> int[]
120:                try {
121:                    msg = "String[] --> int[]";
122:                    checkArray(msg, intArray, arrayConverter.convert(
123:                            int[].class, strArray));
124:                } catch (Exception e) {
125:                    fail(msg + " failed " + e);
126:                }
127:
128:                // String[] --> Integer[]
129:                try {
130:                    msg = "String[] --> Integer[]";
131:                    checkArray(msg, IntegerArray, arrayConverter.convert(
132:                            Integer[].class, strArray));
133:                } catch (Exception e) {
134:                    fail(msg + " failed " + e);
135:                }
136:
137:                // long[] --> int[]
138:                try {
139:                    msg = "long[] --> int[]";
140:                    checkArray(msg, intArray, arrayConverter.convert(
141:                            int[].class, longArray));
142:                } catch (Exception e) {
143:                    fail(msg + " failed " + e);
144:                }
145:
146:                // Long --> int[]
147:                try {
148:                    msg = "Long --> int[]";
149:                    checkArray(msg, new int[] { LONGArray[0].intValue() },
150:                            arrayConverter.convert(int[].class, LONGArray[0]));
151:                } catch (Exception e) {
152:                    fail(msg + " failed " + e);
153:                }
154:
155:                // LONG[] --> int[]
156:                try {
157:                    msg = "LONG[] --> int[]";
158:                    checkArray(msg, intArray, arrayConverter.convert(
159:                            int[].class, LONGArray));
160:                } catch (Exception e) {
161:                    fail(msg + " failed " + e);
162:                }
163:
164:                // Long --> String
165:                try {
166:                    msg = "Long --> String";
167:                    assertEquals(msg, LONGArray[0] + "", arrayConverter
168:                            .convert(String.class, LONGArray[0]));
169:                } catch (Exception e) {
170:                    fail(msg + " failed " + e);
171:                }
172:
173:                // LONG[] --> String (first)
174:                try {
175:                    msg = "LONG[] --> String (first)";
176:                    assertEquals(msg, LONGArray[0] + "", arrayConverter
177:                            .convert(String.class, LONGArray));
178:                } catch (Exception e) {
179:                    fail(msg + " failed " + e);
180:                }
181:
182:                // LONG[] --> String (all)
183:                try {
184:                    msg = "LONG[] --> String (all)";
185:                    arrayConverter.setOnlyFirstToString(false);
186:                    assertEquals(msg, stringB, arrayConverter.convert(
187:                            String.class, LONGArray));
188:                } catch (Exception e) {
189:                    fail(msg + " failed " + e);
190:                }
191:
192:                // Collection of Long --> String
193:                try {
194:                    msg = "Collection of Long --> String";
195:                    assertEquals(msg, stringB, arrayConverter.convert(
196:                            String.class, longList));
197:                } catch (Exception e) {
198:                    fail(msg + " failed " + e);
199:                }
200:
201:                // LONG[] --> String[]
202:                try {
203:                    msg = "long[] --> String[]";
204:                    checkArray(msg, strArray, arrayConverter.convert(
205:                            String[].class, LONGArray));
206:                } catch (Exception e) {
207:                    fail(msg + " failed " + e);
208:                }
209:
210:                // Collection of String --> Integer[]
211:                try {
212:                    msg = "Collection of String --> Integer[]";
213:                    checkArray(msg, IntegerArray, arrayConverter.convert(
214:                            Integer[].class, strList));
215:                } catch (Exception e) {
216:                    fail(msg + " failed " + e);
217:                }
218:
219:                // Collection of Long --> int[]
220:                try {
221:                    msg = "Collection of Long --> int[]";
222:                    checkArray(msg, intArray, arrayConverter.convert(
223:                            int[].class, longList));
224:                } catch (Exception e) {
225:                    fail(msg + " failed " + e);
226:                }
227:            }
228:
229:            /**
230:             * Test Converting a String[] to integer array (with leading/trailing whitespace)
231:             */
232:            public void testStringArrayToNumber() {
233:
234:                // Configure Converter
235:                IntegerConverter intConverter = new IntegerConverter();
236:                ArrayConverter arrayConverter = new ArrayConverter(int[].class,
237:                        intConverter);
238:
239:                // Test Data
240:                String[] array = new String[] { "10", "  11", "12  ", "  13  " };
241:                ArrayList list = new ArrayList();
242:                for (int i = 0; i < array.length; i++) {
243:                    list.add(array[i]);
244:                }
245:
246:                // Expected results
247:                String msg = null;
248:                int[] expectedInt = new int[] { 10, 11, 12, 13 };
249:                Integer[] expectedInteger = new Integer[] {
250:                        new Integer(expectedInt[0]),
251:                        new Integer(expectedInt[1]),
252:                        new Integer(expectedInt[2]),
253:                        new Integer(expectedInt[3]) };
254:
255:                // Test String[] --> int[]
256:                try {
257:                    msg = "String[] --> int[]";
258:                    checkArray(msg, expectedInt, arrayConverter.convert(
259:                            int[].class, array));
260:                } catch (Exception e) {
261:                    fail(msg + " failed " + e);
262:                }
263:
264:                // Test String[] --> Integer[]
265:                try {
266:                    msg = "String[] --> Integer[]";
267:                    checkArray(msg, expectedInteger, arrayConverter.convert(
268:                            Integer[].class, array));
269:                } catch (Exception e) {
270:                    fail(msg + " failed " + e);
271:                }
272:
273:                // Test List --> int[]
274:                try {
275:                    msg = "List --> int[]";
276:                    checkArray(msg, expectedInt, arrayConverter.convert(
277:                            int[].class, list));
278:                } catch (Exception e) {
279:                    fail(msg + " failed " + e);
280:                }
281:
282:                // Test List --> Integer[]
283:                try {
284:                    msg = "List --> Integer[]";
285:                    checkArray(msg, expectedInteger, arrayConverter.convert(
286:                            Integer[].class, list));
287:                } catch (Exception e) {
288:                    fail(msg + " failed " + e);
289:                }
290:            }
291:
292:            /**
293:             * Test the Matrix!!!! (parses a String into a 2 dimensional integer array or matrix)
294:             */
295:            public void testTheMatrix() {
296:
297:                // Test Date - create the Matrix!!
298:                // Following String uses two delimiter:
299:                //     - comma (",") to separate individual numbers
300:                //     - semi-colon (";") to separate lists of numbers
301:                String matrixString = "11,12,13 ; 21,22,23 ; 31,32,33 ; 41,42,43";
302:                int[][] expected = new int[][] { new int[] { 11, 12, 13 },
303:                        new int[] { 21, 22, 23 }, new int[] { 31, 32, 33 },
304:                        new int[] { 41, 42, 43 } };
305:
306:                // Construct an Integer Converter
307:                IntegerConverter integerConverter = new IntegerConverter();
308:
309:                // Construct an array Converter for an integer array (i.e. int[]) using
310:                // an IntegerConverter as the element converter.
311:                // N.B. Uses the default comma (i.e. ",") as the delimiter between individual numbers 
312:                ArrayConverter arrayConverter = new ArrayConverter(int[].class,
313:                        integerConverter);
314:
315:                // Construct a "Matrix" Converter which converts arrays of integer arrays using
316:                // the first (int[]) Converter as the element Converter.
317:                // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the different sets of numbers.
318:                //      Also the delimiter for the above array Converter needs to be added to this
319:                //      array Converter's "allowed characters"
320:                ArrayConverter matrixConverter = new ArrayConverter(
321:                        int[][].class, arrayConverter);
322:                matrixConverter.setDelimiter(';');
323:                matrixConverter.setAllowedChars(new char[] { ',' });
324:
325:                try {
326:                    // Do the Conversion
327:                    Object result = matrixConverter.convert(int[][].class,
328:                            matrixString);
329:
330:                    // Check it actually worked OK
331:                    assertEquals("Check int[][].class", int[][].class, result
332:                            .getClass());
333:                    int[][] matrix = (int[][]) result;
334:                    assertEquals("Check int[][] length", expected.length,
335:                            matrix.length);
336:                    for (int i = 0; i < expected.length; i++) {
337:                        assertEquals("Check int[" + i + "] length",
338:                                expected[i].length, matrix[i].length);
339:                        for (int j = 0; j < expected[i].length; j++) {
340:                            String label = "Matrix int[" + i + "," + j
341:                                    + "] element";
342:                            assertEquals(label, expected[i][j], matrix[i][j]);
343:                            // System.out.println(label + " = " + matrix[i][j]);
344:                        }
345:                    }
346:                } catch (Exception e) {
347:                    fail("Matrix Conversion threw " + e);
348:                }
349:            }
350:
351:            /**
352:             * Test Converting using the IntegerConverter as the component Converter
353:             */
354:            public void testInvalidWithDefault() {
355:                int[] zeroArray = new int[0];
356:                int[] oneArray = new int[1];
357:                IntegerConverter intConverter = new IntegerConverter();
358:
359:                assertEquals("Null Default", null, new ArrayConverter(
360:                        int[].class, intConverter, -1).convert(int[].class,
361:                        null));
362:                checkArray("Zero Length", zeroArray, new ArrayConverter(
363:                        int[].class, intConverter, 0)
364:                        .convert(int[].class, null));
365:                checkArray("One Length", oneArray, new ArrayConverter(
366:                        Integer[].class, intConverter, 1).convert(int[].class,
367:                        null));
368:            }
369:
370:            /**
371:             * Test Empty String
372:             */
373:            public void testEmptyString() {
374:                int[] zeroArray = new int[0];
375:                IntegerConverter intConverter = new IntegerConverter();
376:
377:                checkArray("Empty String", zeroArray, new ArrayConverter(
378:                        int[].class, intConverter, -1).convert(int[].class, ""));
379:                assertEquals("Default String", null, new ArrayConverter(
380:                        int[].class, intConverter).convert(String.class, null));
381:            }
382:
383:            /**
384:             * Test Errors creating the converter
385:             */
386:            public void testErrors() {
387:                try {
388:                    new ArrayConverter(null, new DateConverter());
389:                    fail("Default Type missing - expected IllegalArgumentException");
390:                } catch (IllegalArgumentException e) {
391:                    // expected result
392:                }
393:                try {
394:                    new ArrayConverter(Boolean.class, new DateConverter());
395:                    fail("Default Type not an array - expected IllegalArgumentException");
396:                } catch (IllegalArgumentException e) {
397:                    // expected result
398:                }
399:                try {
400:                    new ArrayConverter(int[].class, null);
401:                    fail("Component Converter missing - expected IllegalArgumentException");
402:                } catch (IllegalArgumentException e) {
403:                    // expected result
404:                }
405:            }
406:
407:            /**
408:             * Check that two arrays are the same.
409:             * @param msg Test prefix msg
410:             * @param expected Expected Array value
411:             * @param result Result array value
412:             */
413:            private void checkArray(String msg, Object expected, Object result) {
414:                assertNotNull(msg + " Expected Null", expected);
415:                assertNotNull(msg + " Result   Null", result);
416:                assertTrue(msg + " Result   not array", result.getClass()
417:                        .isArray());
418:                assertTrue(msg + " Expected not array", expected.getClass()
419:                        .isArray());
420:                int resultLth = Array.getLength(result);
421:                assertEquals(msg + " Size", Array.getLength(expected),
422:                        resultLth);
423:                assertEquals(msg + " Type", expected.getClass(), result
424:                        .getClass());
425:                for (int i = 0; i < resultLth; i++) {
426:                    Object expectElement = Array.get(expected, i);
427:                    Object resultElement = Array.get(result, i);
428:                    assertEquals(msg + " Element " + i, expectElement,
429:                            resultElement);
430:                }
431:            }
432:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.