View Javadoc

1   /*
2    * $Id: SetterTest.java 1104080 2011-05-17 09:22:09Z 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.InappropriateExpressionException;
23  import org.apache.commons.ognl.NoSuchPropertyException;
24  import org.apache.commons.ognl.test.objects.Root;
25  import org.junit.runner.RunWith;
26  import org.junit.runners.Parameterized;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  import java.util.*;
30  
31  @RunWith(value = Parameterized.class)
32  public class SetterTest
33      extends OgnlTestCase
34  {
35      private static Root ROOT = new Root();
36  
37      static Set _list = new HashSet();
38      static
39      {
40          _list.add( "Test1" );
41      }
42  
43      private static Object[][] TESTS = {
44          // Setting values
45          { ROOT.getMap(), "newValue", null, new Integer( 101 ) },
46          { ROOT, "settableList[0]", "foo", "quux" }, // absolute indexes
47          { ROOT, "settableList[0]", "quux" },
48          { ROOT, "settableList[2]", "baz", "quux" },
49          { ROOT, "settableList[2]", "quux" },
50          { ROOT, "settableList[$]", "quux", "oompa" }, // special indexes
51          { ROOT, "settableList[$]", "oompa" },
52          { ROOT, "settableList[^]", "quux", "oompa" },
53          { ROOT, "settableList[^]", "oompa" },
54          { ROOT, "settableList[|]", "bar", "oompa" },
55          { ROOT, "settableList[|]", "oompa" },
56          { ROOT, "map.newValue", new Integer( 101 ), new Integer( 555 ) },
57          { ROOT, "map", ROOT.getMap(), new HashMap(), NoSuchPropertyException.class },
58          { ROOT.getMap(), "newValue2 || put(\"newValue2\",987), newValue2", new Integer( 987 ), new Integer( 1002 ) },
59          { ROOT, "map.(someMissingKey || newValue)", new Integer( 555 ), new Integer( 666 ) },
60          { ROOT.getMap(), "newValue || someMissingKey", new Integer( 666 ), new Integer( 666 ) }, // no setting happens!
61          { ROOT, "map.(newValue && aKey)", null, new Integer( 54321 ) },
62          { ROOT, "map.(someMissingKey && newValue)", null, null }, // again, no setting
63          { null, "0", new Integer( 0 ), null, InappropriateExpressionException.class }, // illegal for setting, no
64                                                                                         // property
65          { ROOT, "map[0]=\"map.newValue\", map[0](#this)", new Integer( 666 ), new Integer( 888 ) },
66          { ROOT, "selectedList", null, _list, IllegalArgumentException.class },
67          { ROOT, "openTransitionWin", Boolean.FALSE, Boolean.TRUE } };
68  
69      /*
70       * =================================================================== Public static methods
71       * ===================================================================
72       */
73      @Parameters
74      public static Collection<Object[]> data()
75      {
76          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
77          for ( int i = 0; i < TESTS.length; i++ )
78          {
79              Object[] tmp = new Object[6];
80              tmp[0] = TESTS[i][1];
81              tmp[1] = TESTS[i][0];
82              tmp[2] = TESTS[i][1];
83  
84              switch ( TESTS[i].length )
85              {
86                  case 3:
87                      tmp[3] = TESTS[i][2];
88                      break;
89  
90                  case 4:
91                      tmp[3] = TESTS[i][2];
92                      tmp[4] = TESTS[i][3];
93                      break;
94  
95                  case 5:
96                      tmp[3] = TESTS[i][2];
97                      tmp[4] = TESTS[i][3];
98                      tmp[5] = TESTS[i][4];
99                      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 }