001    /*
002     * $Id: SetterTest.java 1104080 2011-05-17 09:22:09Z mcucchiara $
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     * http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    package org.apache.commons.ognl.test;
021    
022    import org.apache.commons.ognl.InappropriateExpressionException;
023    import org.apache.commons.ognl.NoSuchPropertyException;
024    import org.apache.commons.ognl.test.objects.Root;
025    import org.junit.runner.RunWith;
026    import org.junit.runners.Parameterized;
027    import org.junit.runners.Parameterized.Parameters;
028    
029    import java.util.*;
030    
031    @RunWith(value = Parameterized.class)
032    public class SetterTest
033        extends OgnlTestCase
034    {
035        private static Root ROOT = new Root();
036    
037        static Set _list = new HashSet();
038        static
039        {
040            _list.add( "Test1" );
041        }
042    
043        private static Object[][] TESTS = {
044            // Setting values
045            { ROOT.getMap(), "newValue", null, new Integer( 101 ) },
046            { ROOT, "settableList[0]", "foo", "quux" }, // absolute indexes
047            { ROOT, "settableList[0]", "quux" },
048            { ROOT, "settableList[2]", "baz", "quux" },
049            { ROOT, "settableList[2]", "quux" },
050            { ROOT, "settableList[$]", "quux", "oompa" }, // special indexes
051            { ROOT, "settableList[$]", "oompa" },
052            { ROOT, "settableList[^]", "quux", "oompa" },
053            { ROOT, "settableList[^]", "oompa" },
054            { ROOT, "settableList[|]", "bar", "oompa" },
055            { ROOT, "settableList[|]", "oompa" },
056            { ROOT, "map.newValue", new Integer( 101 ), new Integer( 555 ) },
057            { ROOT, "map", ROOT.getMap(), new HashMap(), NoSuchPropertyException.class },
058            { ROOT.getMap(), "newValue2 || put(\"newValue2\",987), newValue2", new Integer( 987 ), new Integer( 1002 ) },
059            { ROOT, "map.(someMissingKey || newValue)", new Integer( 555 ), new Integer( 666 ) },
060            { ROOT.getMap(), "newValue || someMissingKey", new Integer( 666 ), new Integer( 666 ) }, // no setting happens!
061            { ROOT, "map.(newValue && aKey)", null, new Integer( 54321 ) },
062            { ROOT, "map.(someMissingKey && newValue)", null, null }, // again, no setting
063            { null, "0", new Integer( 0 ), null, InappropriateExpressionException.class }, // illegal for setting, no
064                                                                                           // property
065            { ROOT, "map[0]=\"map.newValue\", map[0](#this)", new Integer( 666 ), new Integer( 888 ) },
066            { ROOT, "selectedList", null, _list, IllegalArgumentException.class },
067            { ROOT, "openTransitionWin", Boolean.FALSE, Boolean.TRUE } };
068    
069        /*
070         * =================================================================== Public static methods
071         * ===================================================================
072         */
073        @Parameters
074        public static Collection<Object[]> data()
075        {
076            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
077            for ( int i = 0; i < TESTS.length; i++ )
078            {
079                Object[] tmp = new Object[6];
080                tmp[0] = TESTS[i][1];
081                tmp[1] = TESTS[i][0];
082                tmp[2] = TESTS[i][1];
083    
084                switch ( TESTS[i].length )
085                {
086                    case 3:
087                        tmp[3] = TESTS[i][2];
088                        break;
089    
090                    case 4:
091                        tmp[3] = TESTS[i][2];
092                        tmp[4] = TESTS[i][3];
093                        break;
094    
095                    case 5:
096                        tmp[3] = TESTS[i][2];
097                        tmp[4] = TESTS[i][3];
098                        tmp[5] = TESTS[i][4];
099                        break;
100    
101                    default:
102                        throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
103                }
104    
105                data.add( tmp );
106            }
107            return data;
108        }
109    
110        /*
111         * =================================================================== Constructors
112         * ===================================================================
113         */
114        public SetterTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
115                           Object expectedAfterSetResult )
116        {
117            super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
118        }
119    }