001 /*
002 * $Id: CollectionDirectPropertyTest.java 1104581 2011-05-17 21:50:32Z 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.ArrayList;
028 import java.util.Arrays;
029 import java.util.Collection;
030
031 @RunWith(value = Parameterized.class)
032 public class CollectionDirectPropertyTest
033 extends OgnlTestCase
034 {
035
036 private static Root ROOT = new Root();
037
038 private static Object[][] TESTS = {
039 // Collection direct properties
040 { Arrays.asList( "hello", "world" ), "size", 2 },
041 { Arrays.asList( "hello", "world" ), "isEmpty", Boolean.FALSE },
042 { Arrays.asList(), "isEmpty", Boolean.TRUE },
043 { Arrays.asList( "hello", "world" ), "iterator.next", "hello" },
044 { Arrays.asList( "hello", "world" ), "iterator.hasNext", Boolean.TRUE },
045 { Arrays.asList( "hello", "world" ), "#it = iterator, #it.next, #it.next, #it.hasNext", Boolean.FALSE },
046 { Arrays.asList( "hello", "world" ), "#it = iterator, #it.next, #it.next", "world" },
047 { Arrays.asList( "hello", "world" ), "size", 2 },
048 { ROOT, "map[\"test\"]", ROOT }, { ROOT, "map.size", ROOT.getMap().size() },
049 { ROOT, "map.keySet", ROOT.getMap().keySet() }, { ROOT, "map.values", ROOT.getMap().values() },
050 { ROOT, "map.keys.size", ROOT.getMap().keySet().size() },
051 { ROOT, "map[\"size\"]", ROOT.getMap().get( "size" ) },
052 { ROOT, "map.isEmpty", ROOT.getMap().isEmpty() ? Boolean.TRUE : Boolean.FALSE },
053 { ROOT, "map[\"isEmpty\"]", null }, };
054
055 /*
056 * =================================================================== Public static methods
057 * ===================================================================
058 */
059 @Parameters
060 public static Collection<Object[]> data()
061 {
062 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
063 for ( Object[] TEST : TESTS )
064 {
065 Object[] tmp = new Object[6];
066 tmp[0] = TEST[1];
067 tmp[1] = TEST[0];
068 tmp[2] = TEST[1];
069
070 switch ( TEST.length )
071 {
072 case 3:
073 tmp[3] = TEST[2];
074 break;
075
076 case 4:
077 tmp[3] = TEST[2];
078 tmp[4] = TEST[3];
079 break;
080
081 case 5:
082 tmp[3] = TEST[2];
083 tmp[4] = TEST[3];
084 tmp[5] = TEST[4];
085 break;
086
087 default:
088 throw new RuntimeException( "don't understand TEST format with length" );
089 }
090
091 data.add( tmp );
092 }
093 return data;
094 }
095
096 /*
097 * =================================================================== Constructors
098 * ===================================================================
099 */
100 public CollectionDirectPropertyTest( String name, Object root, String expressionString, Object expectedResult,
101 Object setValue, Object expectedAfterSetResult )
102 {
103 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
104 }
105 }