001: //--------------------------------------------------------------------------
002: // Copyright (c) 2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package org.ognl.test;
032:
033: import java.math.*;
034: import junit.framework.TestSuite;
035: import ognl.OgnlException;
036: import org.ognl.test.objects.Simple;
037:
038: public class NumberFormatExceptionTest extends OgnlTestCase {
039: private static Simple SIMPLE = new Simple();
040:
041: private static Object[][] TESTS = {
042: // NumberFormatException handling (default is to throw NumberFormatException on bad string conversions)
043: { SIMPLE, "floatValue", new Float(0f), new Float(10f),
044: new Float(10f) }, /* set float to 10.0f */
045: { SIMPLE, "floatValue", new Float(10f), "x10x",
046: OgnlException.class }, /* set float to invalid format string, should yield OgnlException */
047:
048: { SIMPLE, "intValue", new Integer(0), new Integer(34),
049: new Integer(34) }, /* set int to 34 */
050: { SIMPLE, "intValue", new Integer(34), "foobar",
051: OgnlException.class }, /* set int to invalid format string, should yield OgnlException */
052: { SIMPLE, "intValue", new Integer(34), "",
053: OgnlException.class }, /* set int to empty string, should yield 0gnlException */
054: { SIMPLE, "intValue", new Integer(34), " \t",
055: OgnlException.class }, /* set int to whitespace-only string, should yield 0gnlException */
056: { SIMPLE, "intValue", new Integer(34), " \t1234\t\t",
057: new Integer(1234) }, /* set int to whitespace-laden valid string, should yield 1234 */
058:
059: { SIMPLE, "bigIntValue", BigInteger.valueOf(0),
060: BigInteger.valueOf(34), BigInteger.valueOf(34) }, /* set bigint to 34 */
061: { SIMPLE, "bigIntValue", BigInteger.valueOf(34), null, null }, /* set bigint to null string, should yield 0 */
062: { SIMPLE, "bigIntValue", null, "", OgnlException.class }, /* set bigint to empty string, should yield 0gnlException */
063: { SIMPLE, "bigIntValue", null, "foobar",
064: OgnlException.class }, /* set bigint to invalid format string, should yield OgnlException */
065:
066: { SIMPLE, "bigDecValue", new BigDecimal(0.0),
067: new BigDecimal(34.55), new BigDecimal(34.55) }, /* set bigdec to 34.55 */
068: { SIMPLE, "bigDecValue", new BigDecimal(34.55), null, null }, /* set bigdec to null string, should yield 0.0 */
069: { SIMPLE, "bigDecValue", null, "", OgnlException.class }, /* set bigdec to empty string, should yield 0gnlException */
070: { SIMPLE, "bigDecValue", null, "foobar",
071: OgnlException.class }, /* set bigdec to invalid format string, should yield OgnlException */
072: };
073:
074: /*===================================================================
075: Public static methods
076: ===================================================================*/
077: public static TestSuite suite() {
078: TestSuite result = new TestSuite();
079:
080: for (int i = 0; i < TESTS.length; i++) {
081: if (TESTS[i].length == 3) {
082: result.addTest(new NumberFormatExceptionTest(
083: (String) TESTS[i][1], TESTS[i][0],
084: (String) TESTS[i][1], TESTS[i][2]));
085: } else {
086: if (TESTS[i].length == 4) {
087: result.addTest(new NumberFormatExceptionTest(
088: (String) TESTS[i][1], TESTS[i][0],
089: (String) TESTS[i][1], TESTS[i][2],
090: TESTS[i][3]));
091: } else {
092: if (TESTS[i].length == 5) {
093: result.addTest(new NumberFormatExceptionTest(
094: (String) TESTS[i][1], TESTS[i][0],
095: (String) TESTS[i][1], TESTS[i][2],
096: TESTS[i][3], TESTS[i][4]));
097: } else {
098: throw new RuntimeException(
099: "don't understand TEST format");
100: }
101: }
102: }
103: }
104: return result;
105: }
106:
107: /*===================================================================
108: Constructors
109: ===================================================================*/
110: public NumberFormatExceptionTest() {
111: super ();
112: }
113:
114: public NumberFormatExceptionTest(String name) {
115: super (name);
116: }
117:
118: public NumberFormatExceptionTest(String name, Object root,
119: String expressionString, Object expectedResult,
120: Object setValue, Object expectedAfterSetResult) {
121: super (name, root, expressionString, expectedResult, setValue,
122: expectedAfterSetResult);
123: }
124:
125: public NumberFormatExceptionTest(String name, Object root,
126: String expressionString, Object expectedResult,
127: Object setValue) {
128: super (name, root, expressionString, expectedResult, setValue);
129: }
130:
131: public NumberFormatExceptionTest(String name, Object root,
132: String expressionString, Object expectedResult) {
133: super(name, root, expressionString, expectedResult);
134: }
135: }
|