View Javadoc

1   /*
2    * $Id: NullStringCatenationTest.java 1180547 2011-10-09 05:44:18Z grobmeier $
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 java.util.ArrayList;
23  import java.util.Collection;
24  
25  import org.apache.commons.ognl.test.objects.Root;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  @RunWith(value = Parameterized.class)
32  public class NullStringCatenationTest
33      extends OgnlTestCase
34  {
35  
36      public static final String MESSAGE = "blarney";
37  
38      private static Root ROOT = new Root();
39  
40      private static Object[][] TESTS =
41      {
42          // Null string catenation
43          { ROOT, "\"bar\" + null", "barnull" }, // Catenate null to a string
44          { ROOT, "\"bar\" + nullObject", "barnull" }, // Catenate null to a string
45          { ROOT, "20.56 + nullObject", NullPointerException.class }, // Catenate null to a number
46          { ROOT, "(true ? 'tabHeader' : '') + (false ? 'tabHeader' : '')", "tabHeader" },
47          { ROOT, "theInt == 0 ? '5%' : theInt + '%'", "6%" },
48          { ROOT, "'width:' + width + ';'", "width:238px;" },
49          { ROOT, "theLong + '_' + index", "4_1" },
50          { ROOT, "'javascript:' + @org.apache.commons.ognl.test.NullStringCatenationTest@MESSAGE", "javascript:blarney" },
51          { ROOT,  "printDelivery ? '' : 'javascript:deliverySelected(' + property.carrier + ',' + currentDeliveryId + ')'", "" }, 
52          { ROOT, "bean2.id + '_' + theInt", "1_6" } 
53      };
54  
55      
56      /**
57       * Setup parameters for this test which are used to call this class constructor
58       * @return the collection of paramaters 
59       */
60      @Parameters
61      public static Collection<Object[]> data()
62      {
63          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
64          for ( int i = 0; i < TESTS.length; i++ )
65          {
66              Object[] tmp = new Object[4];
67              tmp[0] = TESTS[i][1];
68              tmp[1] = TESTS[i][0];
69              tmp[2] = TESTS[i][1];
70              tmp[3] = TESTS[i][2];
71  
72              data.add( tmp );
73          }
74          return data;
75      }
76  
77      /**
78       * Constructor: size of the Object[] returned by the @Parameter annotated method must match
79       * the number of arguments in this constructor  
80       */
81      public NullStringCatenationTest( String name, Object root, String expressionString, Object expectedResult)
82      {
83          super( name, root, expressionString, expectedResult );
84      }
85  
86      @Test
87      @Override
88      public void runTest() throws Exception
89      {
90          super.runTest();
91      }
92  }