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.lang.reflect.*;
034: import java.util.*;
035: import junit.framework.TestSuite;
036: import ognl.DefaultTypeConverter;
037: import ognl.TypeConverter;
038: import org.ognl.test.objects.Root;
039:
040: public class ArrayElementsTest extends OgnlTestCase {
041: private static String[] STRING_ARRAY = new String[] { "hello",
042: "world" };
043: private static int[] INT_ARRAY = new int[] { 10, 20 };
044: private static Root ROOT = new Root();
045:
046: private static Object[][] TESTS = {
047: // Array elements test
048: { STRING_ARRAY, "length", new Integer(2) },
049: { STRING_ARRAY, "#root[1]", "world" },
050: { INT_ARRAY, "#root[1]", new Integer(20) },
051: { INT_ARRAY, "#root[1]", new Integer(20), "50",
052: new Integer(50) },
053: { INT_ARRAY, "#root[1]", new Integer(50),
054: new String[] { "50", "100" }, new Integer(50) },
055: { ROOT, "intValue", new Integer(0),
056: new String[] { "50", "100" }, new Integer(50) },
057: { ROOT, "array", ROOT.getArray(),
058: new String[] { "50", "100" }, new int[] { 50, 100 } }, };
059:
060: /*===================================================================
061: Private static methods
062: ===================================================================*/
063: /*===================================================================
064: Public static methods
065: ===================================================================*/
066: public static TestSuite suite() {
067: TestSuite result = new TestSuite();
068:
069: for (int i = 0; i < TESTS.length; i++) {
070: if (TESTS[i].length == 3) {
071: result.addTest(new ArrayElementsTest(
072: (String) TESTS[i][1], TESTS[i][0],
073: (String) TESTS[i][1], TESTS[i][2]));
074: } else {
075: if (TESTS[i].length == 4) {
076: result.addTest(new ArrayElementsTest(
077: (String) TESTS[i][1], TESTS[i][0],
078: (String) TESTS[i][1], TESTS[i][2],
079: TESTS[i][3]));
080: } else {
081: if (TESTS[i].length == 5) {
082: result.addTest(new ArrayElementsTest(
083: (String) TESTS[i][1], TESTS[i][0],
084: (String) TESTS[i][1], TESTS[i][2],
085: TESTS[i][3], TESTS[i][4]));
086: } else {
087: throw new RuntimeException(
088: "don't understand TEST format");
089: }
090: }
091: }
092: }
093: return result;
094: }
095:
096: /*===================================================================
097: Constructors
098: ===================================================================*/
099: public ArrayElementsTest() {
100: super ();
101: }
102:
103: public ArrayElementsTest(String name) {
104: super (name);
105: }
106:
107: public ArrayElementsTest(String name, Object root,
108: String expressionString, Object expectedResult,
109: Object setValue, Object expectedAfterSetResult) {
110: super (name, root, expressionString, expectedResult, setValue,
111: expectedAfterSetResult);
112: }
113:
114: public ArrayElementsTest(String name, Object root,
115: String expressionString, Object expectedResult,
116: Object setValue) {
117: super (name, root, expressionString, expectedResult, setValue);
118: }
119:
120: public ArrayElementsTest(String name, Object root,
121: String expressionString, Object expectedResult) {
122: super (name, root, expressionString, expectedResult);
123: }
124:
125: /*===================================================================
126: Overridden methods
127: ===================================================================*/
128: protected void setUp() {
129: TypeConverter arrayConverter;
130:
131: super .setUp();
132: arrayConverter = new DefaultTypeConverter() {
133: public Object convertValue(Map context, Object target,
134: Member member, String propertyName, Object value,
135: Class toType) {
136: if (value.getClass().isArray()) {
137: if (!toType.isArray()) {
138: value = Array.get(value, 0);
139: }
140: }
141: return super.convertValue(context, target, member,
142: propertyName, value, toType);
143: }
144: };
145: context.setTypeConverter(arrayConverter);
146: }
147: }
|