View Javadoc

1   /*
2    * $Id: CollectionDirectPropertyTest.java 1104581 2011-05-17 21:50:32Z mcucchiara $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  package org.apache.commons.ognl.test;
21  
22  import org.apache.commons.ognl.test.objects.Root;
23  import org.junit.runner.RunWith;
24  import org.junit.runners.Parameterized;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collection;
30  
31  @RunWith(value = Parameterized.class)
32  public class CollectionDirectPropertyTest
33      extends OgnlTestCase
34  {
35  
36      private static Root ROOT = new Root();
37  
38      private static Object[][] TESTS = {
39          // Collection direct properties
40          { Arrays.asList( "hello", "world" ), "size", 2 },
41          { Arrays.asList( "hello", "world" ), "isEmpty", Boolean.FALSE },
42          { Arrays.asList(), "isEmpty", Boolean.TRUE },
43          { Arrays.asList( "hello", "world" ), "iterator.next", "hello" },
44          { Arrays.asList( "hello", "world" ), "iterator.hasNext", Boolean.TRUE },
45          { Arrays.asList( "hello", "world" ), "#it = iterator, #it.next, #it.next, #it.hasNext", Boolean.FALSE },
46          { Arrays.asList( "hello", "world" ), "#it = iterator, #it.next, #it.next", "world" },
47          { Arrays.asList( "hello", "world" ), "size", 2 },
48          { ROOT, "map[\"test\"]", ROOT }, { ROOT, "map.size", ROOT.getMap().size() },
49          { ROOT, "map.keySet", ROOT.getMap().keySet() }, { ROOT, "map.values", ROOT.getMap().values() },
50          { ROOT, "map.keys.size", ROOT.getMap().keySet().size() },
51          { ROOT, "map[\"size\"]", ROOT.getMap().get( "size" ) },
52          { ROOT, "map.isEmpty", ROOT.getMap().isEmpty() ? Boolean.TRUE : Boolean.FALSE },
53          { ROOT, "map[\"isEmpty\"]", null }, };
54  
55      /*
56       * =================================================================== Public static methods
57       * ===================================================================
58       */
59      @Parameters
60      public static Collection<Object[]> data()
61      {
62          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
63          for ( Object[] TEST : TESTS )
64          {
65              Object[] tmp = new Object[6];
66              tmp[0] = TEST[1];
67              tmp[1] = TEST[0];
68              tmp[2] = TEST[1];
69  
70              switch ( TEST.length )
71              {
72                  case 3:
73                      tmp[3] = TEST[2];
74                      break;
75  
76                  case 4:
77                      tmp[3] = TEST[2];
78                      tmp[4] = TEST[3];
79                      break;
80  
81                  case 5:
82                      tmp[3] = TEST[2];
83                      tmp[4] = TEST[3];
84                      tmp[5] = TEST[4];
85                      break;
86  
87                  default:
88                      throw new RuntimeException( "don't understand TEST format with length" );
89              }
90  
91              data.add( tmp );
92          }
93          return data;
94      }
95  
96      /*
97       * =================================================================== Constructors
98       * ===================================================================
99       */
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 }