001    /*
002     * $Id: ObjectIndexedPropertyTest.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.OgnlException;
023    import org.apache.commons.ognl.test.objects.Bean1;
024    import org.apache.commons.ognl.test.objects.ObjectIndexed;
025    import org.junit.runner.RunWith;
026    import org.junit.runners.Parameterized;
027    import org.junit.runners.Parameterized.Parameters;
028    
029    import java.util.ArrayList;
030    import java.util.Collection;
031    
032    @RunWith(value = Parameterized.class)
033    public class ObjectIndexedPropertyTest
034        extends OgnlTestCase
035    {
036    
037        private static ObjectIndexed OBJECT_INDEXED = new ObjectIndexed();
038    
039        private static Bean1 root = new Bean1();
040    
041        private static Object[][] TESTS = {
042            // Arbitrary indexed properties
043            { OBJECT_INDEXED, "attributes[\"bar\"]", "baz" }, // get non-indexed property through
044            // attributes Map
045            { OBJECT_INDEXED, "attribute[\"foo\"]", "bar" }, // get indexed property
046            { OBJECT_INDEXED, "attribute[\"bar\"]", "baz", "newValue", "newValue" }, // set
047            // indexed
048            // property
049            { OBJECT_INDEXED, "attribute[\"bar\"]", "newValue" },// get indexed property back to
050            // confirm
051            { OBJECT_INDEXED, "attributes[\"bar\"]", "newValue" }, // get property back through Map
052            // to confirm
053            { OBJECT_INDEXED, "attribute[\"other\"].attribute[\"bar\"]", "baz" }, // get indexed
054            // property from
055            // indexed, then
056            // through other
057            { OBJECT_INDEXED, "attribute[\"other\"].attributes[\"bar\"]", "baz" }, // get property
058            // back through
059            // Map to
060            // confirm
061            { OBJECT_INDEXED, "attribute[$]", OgnlException.class }, // illegal DynamicSubscript
062            // access to object indexed
063            // property
064            { root, "bean2.bean3.indexedValue[25]", null } };
065    
066        /*
067         * =================================================================== Public static methods
068         * ===================================================================
069         */
070        @Parameters
071        public static Collection<Object[]> data()
072        {
073            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
074            for ( int i = 0; i < TESTS.length; i++ )
075            {
076                Object[] tmp = new Object[6];
077                tmp[0] = TESTS[i][1];
078                tmp[1] = TESTS[i][0];
079                tmp[2] = TESTS[i][1];
080    
081                switch ( TESTS[i].length )
082                {
083                    case 3:
084                        tmp[3] = TESTS[i][2];
085                        break;
086    
087                    case 4:
088                        tmp[3] = TESTS[i][2];
089                        tmp[4] = TESTS[i][3];
090                        break;
091    
092                    case 5:
093                        tmp[3] = TESTS[i][2];
094                        tmp[4] = TESTS[i][3];
095                        tmp[5] = TESTS[i][4];
096                        break;
097    
098                    default:
099                        throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
100                }
101    
102                data.add( tmp );
103            }
104            return data;
105        }
106    
107        /*
108         * =================================================================== Constructors
109         * ===================================================================
110         */
111        public ObjectIndexedPropertyTest( String name, Object root, String expressionString, Object expectedResult,
112                                          Object setValue, Object expectedAfterSetResult )
113        {
114            super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
115        }
116    }