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.util.*;
034: import junit.framework.TestSuite;
035: import ognl.NoSuchPropertyException;
036: import ognl.InappropriateExpressionException;
037: import org.ognl.test.objects.Root;
038:
039: public class SetterTest extends OgnlTestCase {
040: private static Root ROOT = new Root();
041:
042: private static Object[][] TESTS = {
043: // Setting values
044: { ROOT.getMap(), "newValue", null, new Integer(101) },
045: { ROOT, "settableList[0]", "foo", "quux" }, /* absolute indexes */
046: { ROOT, "settableList[0]", "quux" },
047: { ROOT, "settableList[2]", "baz", "quux" },
048: { ROOT, "settableList[2]", "quux" },
049: { ROOT, "settableList[$]", "quux", "oompa" }, /* special indexes */
050: { ROOT, "settableList[$]", "oompa" },
051: { ROOT, "settableList[^]", "quux", "oompa" },
052: { ROOT, "settableList[^]", "oompa" },
053: { ROOT, "settableList[|]", "bar", "oompa" },
054: { ROOT, "settableList[|]", "oompa" },
055: { ROOT, "map.newValue", new Integer(101), new Integer(555) },
056: { ROOT, "map", ROOT.getMap(), new HashMap(),
057: NoSuchPropertyException.class },
058: { ROOT.getMap(),
059: "newValue2 || put(\"newValue2\",987), newValue2",
060: new Integer(987), new Integer(1002) },
061: { ROOT, "map.(someMissingKey || newValue)",
062: new Integer(555), new Integer(666) },
063: { ROOT.getMap(), "newValue || someMissingKey",
064: new Integer(666), new Integer(666) }, // no setting happens!
065: { ROOT, "map.(newValue && aKey)", null, new Integer(54321) },
066: { ROOT, "map.(someMissingKey && newValue)", null, null }, // again, no setting
067: { null, "0", new Integer(0), null,
068: InappropriateExpressionException.class }, // illegal for setting, no property
069: { ROOT, "map[0]=\"map.newValue\", map[0](#this)",
070: new Integer(666), new Integer(888) }, };
071:
072: /*===================================================================
073: Public static methods
074: ===================================================================*/
075: public static TestSuite suite() {
076: TestSuite result = new TestSuite();
077:
078: for (int i = 0; i < TESTS.length; i++) {
079: if (TESTS[i].length == 3) {
080: result
081: .addTest(new SetterTest((String) TESTS[i][1],
082: TESTS[i][0], (String) TESTS[i][1],
083: TESTS[i][2]));
084: } else {
085: if (TESTS[i].length == 4) {
086: result.addTest(new SetterTest((String) TESTS[i][1],
087: TESTS[i][0], (String) TESTS[i][1],
088: TESTS[i][2], TESTS[i][3]));
089: } else {
090: if (TESTS[i].length == 5) {
091: result.addTest(new SetterTest(
092: (String) TESTS[i][1], TESTS[i][0],
093: (String) TESTS[i][1], TESTS[i][2],
094: TESTS[i][3], TESTS[i][4]));
095: } else {
096: throw new RuntimeException(
097: "don't understand TEST format");
098: }
099: }
100: }
101: }
102: return result;
103: }
104:
105: /*===================================================================
106: Constructors
107: ===================================================================*/
108: public SetterTest() {
109: super ();
110: }
111:
112: public SetterTest(String name) {
113: super (name);
114: }
115:
116: public SetterTest(String name, Object root,
117: String expressionString, Object expectedResult,
118: Object setValue, Object expectedAfterSetResult) {
119: super (name, root, expressionString, expectedResult, setValue,
120: expectedAfterSetResult);
121: }
122:
123: public SetterTest(String name, Object root,
124: String expressionString, Object expectedResult,
125: Object setValue) {
126: super (name, root, expressionString, expectedResult, setValue);
127: }
128:
129: public SetterTest(String name, Object root,
130: String expressionString, Object expectedResult) {
131: super(name, root, expressionString, expectedResult);
132: }
133: }
|