001    /*
002     * $Id: MapCreationTest.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.test.objects.Root;
023    import org.junit.runner.RunWith;
024    import org.junit.runners.Parameterized;
025    import org.junit.runners.Parameterized.Parameters;
026    
027    import java.util.*;
028    
029    @RunWith(value = Parameterized.class)
030    public class MapCreationTest
031        extends OgnlTestCase
032    {
033    
034        private static Root ROOT = new Root();
035    
036        private static Map fooBarMap1;
037    
038        private static Map fooBarMap2;
039    
040        private static Map fooBarMap3;
041    
042        private static Map fooBarMap4;
043    
044        private static Map fooBarMap5;
045    
046        static
047        {
048            fooBarMap1 = new HashMap();
049            fooBarMap1.put( "foo", "bar" );
050            fooBarMap2 = new HashMap();
051            fooBarMap2.put( "foo", "bar" );
052            fooBarMap2.put( "bar", "baz" );
053            fooBarMap3 = new HashMap();
054            fooBarMap3.put( "foo", null );
055            fooBarMap3.put( "bar", "baz" );
056            fooBarMap4 = new LinkedHashMap();
057            fooBarMap4.put( "foo", "bar" );
058            fooBarMap4.put( "bar", "baz" );
059            fooBarMap5 = new TreeMap();
060            fooBarMap5.put( "foo", "bar" );
061            fooBarMap5.put( "bar", "baz" );
062        }
063    
064        private static Object[][] TESTS = {
065            // Map creation
066            { ROOT, "#{ \"foo\" : \"bar\" }", fooBarMap1 },
067            { ROOT, "#{ \"foo\" : \"bar\", \"bar\" : \"baz\"  }", fooBarMap2 },
068            { ROOT, "#{ \"foo\", \"bar\" : \"baz\"  }", fooBarMap3 },
069            { ROOT, "#@java.util.LinkedHashMap@{ \"foo\" : \"bar\", \"bar\" : \"baz\"  }", fooBarMap4 },
070            { ROOT, "#@java.util.TreeMap@{ \"foo\" : \"bar\", \"bar\" : \"baz\"  }", fooBarMap5 },
071    
072        };
073    
074        /*
075         * =================================================================== Public static methods
076         * ===================================================================
077         */
078        @Parameters
079        public static Collection<Object[]> data()
080        {
081            Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
082            for ( int i = 0; i < TESTS.length; i++ )
083            {
084                Object[] tmp = new Object[6];
085                tmp[0] = TESTS[i][1];
086                tmp[1] = TESTS[i][0];
087                tmp[2] = TESTS[i][1];
088    
089                switch ( TESTS[i].length )
090                {
091                    case 3:
092                        tmp[3] = TESTS[i][2];
093                        break;
094    
095                    case 4:
096                        tmp[3] = TESTS[i][2];
097                        tmp[4] = TESTS[i][3];
098                        break;
099    
100                    case 5:
101                        tmp[3] = TESTS[i][2];
102                        tmp[4] = TESTS[i][3];
103                        tmp[5] = TESTS[i][4];
104                        break;
105    
106                    default:
107                        throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
108                }
109    
110                data.add( tmp );
111            }
112            return data;
113        }
114    
115        /*
116         * =================================================================== Constructors
117         * ===================================================================
118         */
119        public MapCreationTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
120                                Object expectedAfterSetResult )
121        {
122            super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
123        }
124    }