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: }
|