Source Code Cross Referenced for BooleanArrayConverterTestCase.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 org.apache.commons.beanutils.ConvertUtils;
021:        import org.apache.commons.beanutils.ConversionException;
022:
023:        import junit.framework.TestCase;
024:
025:        /**
026:         * Test conversions of String[]->boolean[] and String->boolean[].
027:         *
028:         * <p>Note that the tests here don't rigorously test conversions of individual
029:         * strings to booleans, as the BooleanArrayConverter class uses a
030:         * BooleanConverter instance to do those conversions, and the BooleanConverter
031:         * class has its own unit tests. Here, the tests focus on the array-related
032:         * behaviour.</p>
033:         */
034:        public class BooleanArrayConverterTestCase extends TestCase {
035:
036:            public static final String[] STANDARD_TRUES = new String[] { "yes",
037:                    "y", "true", "on", "1" };
038:
039:            public static final String[] STANDARD_FALSES = new String[] { "no",
040:                    "n", "false", "off", "0" };
041:
042:            public BooleanArrayConverterTestCase(String name) {
043:                super (name);
044:            }
045:
046:            /**
047:             * Check that an object of type String[] with valid boolean string
048:             * values gets converted nicely.
049:             */
050:            public void testStandardStringArrayConversion() {
051:                String[] values = { "true", "false", "yes", "no", "y", "n",
052:                        "1", "0", };
053:
054:                BooleanArrayConverter converter = new BooleanArrayConverter();
055:                boolean[] results = (boolean[]) converter.convert(null, values);
056:
057:                assertNotNull(results);
058:                assertEquals(8, results.length);
059:                assertTrue(results[0]);
060:                assertFalse(results[1]);
061:                assertTrue(results[2]);
062:                assertFalse(results[3]);
063:                assertTrue(results[4]);
064:                assertFalse(results[5]);
065:                assertTrue(results[6]);
066:                assertFalse(results[7]);
067:            }
068:
069:            /**
070:             * Check that an object whose toString method returns a list of boolean
071:             * values gets converted nicely.
072:             */
073:            public void testStandardStringConversion() {
074:                BooleanArrayConverter converter = new BooleanArrayConverter();
075:
076:                StringBuffer input = new StringBuffer();
077:                boolean[] results;
078:
079:                // string has {}
080:                input.setLength(0);
081:                input.append("{true, 'yes', Y, 1, 'FALSE', \"no\", 'n', 0}");
082:                results = (boolean[]) converter.convert(null, input);
083:
084:                assertNotNull(results);
085:                assertEquals(8, results.length);
086:                assertTrue(results[0]);
087:                assertTrue(results[1]);
088:                assertTrue(results[2]);
089:                assertTrue(results[3]);
090:                assertFalse(results[4]);
091:                assertFalse(results[5]);
092:                assertFalse(results[6]);
093:                assertFalse(results[7]);
094:
095:                // string does not have {}
096:                input.setLength(0);
097:                input.append("'falsE', 'no', 'N', 0, \"truE\", yeS, 'y', '1'");
098:                results = (boolean[]) converter.convert(null, input);
099:
100:                assertNotNull(results);
101:                assertEquals(8, results.length);
102:                assertFalse(results[0]);
103:                assertFalse(results[1]);
104:                assertFalse(results[2]);
105:                assertFalse(results[3]);
106:                assertTrue(results[4]);
107:                assertTrue(results[5]);
108:                assertTrue(results[6]);
109:                assertTrue(results[7]);
110:
111:                // string has only one element, non-quoted
112:                input.setLength(0);
113:                input.append("y");
114:                results = (boolean[]) converter.convert(null, input);
115:
116:                assertNotNull(results);
117:                assertEquals(1, results.length);
118:                assertTrue(results[0]);
119:
120:                // string has only one element, quoted with ".
121:                input.setLength(0);
122:                input.append("\"1\"");
123:                results = (boolean[]) converter.convert(null, input);
124:
125:                assertNotNull(results);
126:                assertEquals(1, results.length);
127:                assertTrue(results[0]);
128:
129:                // string has only one element, quoted with '
130:                // Here we also pass an object of type String rather than the 
131:                // StringBuffer
132:                results = (boolean[]) converter.convert(null, "'yes'");
133:
134:                assertNotNull(results);
135:                assertEquals(1, results.length);
136:                assertTrue(results[0]);
137:
138:            }
139:
140:            /**
141:             * Check that the user can specify non-standard true/false values by
142:             * providing a customised BooleanConverter.
143:             */
144:            public void testAdditionalStrings() {
145:                String[] trueStrings = { "sure" };
146:                String[] falseStrings = { "nope" };
147:                BooleanConverter bc = new BooleanConverter(trueStrings,
148:                        falseStrings, BooleanConverter.NO_DEFAULT);
149:                BooleanArrayConverter converter = new BooleanArrayConverter(bc,
150:                        BooleanArrayConverter.NO_DEFAULT);
151:
152:                boolean[] results = (boolean[]) converter.convert(null,
153:                        "NOPE, sure, sure");
154:                assertNotNull(results);
155:                assertEquals(3, results.length);
156:                assertFalse(results[0]);
157:                assertTrue(results[1]);
158:                assertTrue(results[2]);
159:
160:                try {
161:                    // the literal string 'true' should no longer be recognised as
162:                    // a true value..
163:                    converter.convert(null, "true");
164:                    fail("Converting invalid string should have generated an exception");
165:                } catch (Exception ex) {
166:                    // ok, expected
167:                }
168:            }
169:
170:            /**
171:             * Check that when the input string cannot be split into a String[], and
172:             * there is no default value then an exception is thrown.
173:             */
174:            public void testInvalidStringWithoutDefault() {
175:                BooleanArrayConverter converter = new BooleanArrayConverter();
176:                try {
177:                    converter.convert(null, "true!");
178:                    fail("Converting invalid string should have generated an exception");
179:                } catch (ConversionException expected) {
180:                    // Exception is successful test
181:                }
182:            }
183:
184:            /**
185:             * Check that when the input string cannot be split into a String[], and
186:             * there is a default value then that default is returned.
187:             */
188:            public void testInvalidStringWithDefault() {
189:                boolean[] defaults = new boolean[1];
190:                BooleanArrayConverter converter = new BooleanArrayConverter(
191:                        defaults);
192:                Object o = converter.convert(null, "true!");
193:                assertSame("Unexpected object returned for failed conversion",
194:                        o, defaults);
195:            }
196:
197:            /**
198:             * Check that when one of the elements in a comma-separated string is not
199:             * a valid boolean, and there is no default value then an exception is thrown.
200:             */
201:            public void testInvalidElementWithoutDefault() {
202:                BooleanArrayConverter converter = new BooleanArrayConverter();
203:                try {
204:                    converter.convert(null, "true,bogus");
205:                    fail("Converting invalid string should have generated an exception");
206:                } catch (ConversionException expected) {
207:                    // Exception is successful test
208:                }
209:            }
210:
211:            /**
212:             * Check that when one of the elements in a comma-separated string is not
213:             * a valid boolean, and there is a default value then the default value
214:             * is returned.
215:             * <p>
216:             * Note that the default value is for the complete array object returned,
217:             * not for the failed element.
218:             */
219:            public void testInvalidElementWithDefault() {
220:                boolean[] defaults = new boolean[1];
221:                BooleanArrayConverter converter = new BooleanArrayConverter(
222:                        defaults);
223:                Object o = converter.convert(null, "true,bogus");
224:                assertSame("Unexpected object returned for failed conversion",
225:                        o, defaults);
226:            }
227:
228:            /**
229:             * Check that when a custom BooleanConverter is used, and that converter
230:             * has a (per-element) default, then that element (and just that element)
231:             * is assigned the default value.
232:             * <p>
233:             * With the standard BooleanArrayConverter, if <i>any</i> of the elements
234:             * in the array are bad, then the array-wide default value is returned.
235:             * However by specifying a custom BooleanConverter which has a per-element
236:             * default, the unrecognised elements get that per-element default but the
237:             * others are converted as expected.
238:             */
239:            public void testElementDefault() {
240:                boolean[] defaults = new boolean[1];
241:                BooleanConverter bc = new BooleanConverter(Boolean.TRUE);
242:                BooleanArrayConverter converter = new BooleanArrayConverter(bc,
243:                        defaults);
244:                boolean[] results = (boolean[]) converter.convert(null,
245:                        "true,bogus");
246:
247:                assertEquals(2, results.length);
248:                assertTrue(results[0]);
249:                assertTrue(results[1]);
250:            }
251:
252:            /**
253:             * Check that registration of a custom converter works.
254:             */
255:            public void testRegistration() {
256:                String[] trueStrings = { "sure" };
257:                String[] falseStrings = { "nope" };
258:                BooleanConverter bc = new BooleanConverter(trueStrings,
259:                        falseStrings, BooleanConverter.NO_DEFAULT);
260:
261:                BooleanArrayConverter converter = new BooleanArrayConverter(bc,
262:                        BooleanArrayConverter.NO_DEFAULT);
263:
264:                ConvertUtils.register(converter, BooleanArrayConverter.MODEL);
265:                boolean[] sample = new boolean[0];
266:                boolean[] results = (boolean[]) ConvertUtils.convert(
267:                        "sure,nope", sample.getClass());
268:
269:                assertEquals(2, results.length);
270:                assertTrue(results[0]);
271:                assertFalse(results[1]);
272:            }
273:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.