View Javadoc

1   /*
2    * $Id: ArrayCreationTest.java 1230454 2012-01-12 09:35:53Z 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.ExpressionSyntaxException;
23  import org.apache.commons.ognl.test.objects.Entry;
24  import org.apache.commons.ognl.test.objects.Root;
25  import org.apache.commons.ognl.test.objects.Simple;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.Parameterized;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  
33  @RunWith(value = Parameterized.class)
34  public class ArrayCreationTest
35      extends OgnlTestCase
36  {
37  
38      private static Root ROOT = new Root();
39  
40      private static Object[][] TESTS =
41          {
42              // Array creation
43              { ROOT, "new String[] { \"one\", \"two\" }", new String[] { "one", "two" } },
44              { ROOT, "new String[] { 1, 2 }", new String[] { "1", "2" } },
45              { ROOT, "new Integer[] { \"1\", 2, \"3\" }",
46                  new Integer[] { 1, 2, 3 } },
47              { ROOT, "new String[10]", new String[10] },
48              { ROOT, "new Object[4] { #root, #this }", ExpressionSyntaxException.class },
49              { ROOT, "new Object[4]", new Object[4] },
50              { ROOT, "new Object[] { #root, #this }", new Object[] { ROOT, ROOT } },
51              {
52                  ROOT,
53                  "new org.apache.commons.ognl.test.objects.Simple[] { new org.apache.commons.ognl.test.objects.Simple(), new org.apache.commons.ognl.test.objects.Simple(\"foo\", 1.0f, 2) }",
54                  new Simple[] { new Simple(), new Simple( "foo", 1.0f, 2 ) } },
55              { ROOT, "new org.apache.commons.ognl.test.objects.Simple[5]", new Simple[5] },
56              { ROOT, "new org.apache.commons.ognl.test.objects.Simple(new Object[5])", new Simple( new Object[5] ) },
57              { ROOT, "new org.apache.commons.ognl.test.objects.Simple(new String[5])", new Simple( new String[5] ) },
58              {
59                  ROOT,
60                  "objectIndex ? new org.apache.commons.ognl.test.objects.Entry[] { new org.apache.commons.ognl.test.objects.Entry(), new org.apache.commons.ognl.test.objects.Entry()} "
61                      + ": new org.apache.commons.ognl.test.objects.Entry[] { new org.apache.commons.ognl.test.objects.Entry(), new org.apache.commons.ognl.test.objects.Entry()} ",
62                  new Entry[] { new Entry(), new Entry() } } };
63  
64      /*
65       * =================================================================== Public static methods
66       * ===================================================================
67       */
68      @Parameters
69      public static Collection<Object[]> data()
70      {
71          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
72          for ( Object[] TEST : TESTS )
73          {
74              Object[] tmp = new Object[6];
75              tmp[0] = TEST[1];
76              tmp[1] = TEST[0];
77              tmp[2] = TEST[1];
78  
79              switch ( TEST.length )
80              {
81                  case 3:
82                      tmp[3] = TEST[2];
83                      break;
84  
85                  case 4:
86                      tmp[3] = TEST[2];
87                      tmp[4] = TEST[3];
88                      break;
89  
90                  case 5:
91                      tmp[3] = TEST[2];
92                      tmp[4] = TEST[3];
93                      tmp[5] = TEST[4];
94                      break;
95  
96                  default:
97                      throw new RuntimeException( "don't understand TEST format with length" );
98              }
99  
100             data.add( tmp );
101         }
102         return data;
103     }
104 
105     /*
106      * =================================================================== Constructors
107      * ===================================================================
108      */
109     public ArrayCreationTest( String name, Object root, String expressionString, Object expectedResult,
110                               Object setValue, Object expectedAfterSetResult )
111     {
112         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
113     }
114 }