View Javadoc

1   /*
2    * $Id: ConstantTest.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.ExpressionSyntaxException;
23  import org.junit.Before;
24  import org.junit.runner.RunWith;
25  import org.junit.runners.Parameterized;
26  import org.junit.runners.Parameterized.Parameters;
27  
28  import java.math.BigDecimal;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Collection;
32  
33  @RunWith(value = Parameterized.class)
34  public class ConstantTest
35      extends OgnlTestCase
36  {
37  
38      private static Object[][] TESTS = {
39          { "12345", new Integer( 12345 ) },
40          { "0x100", new Integer( 256 ) },
41          { "0xfE", new Integer( 254 ) },
42          { "01000", new Integer( 512 ) },
43          { "1234L", new Integer( 1234 ) },
44          { "12.34", new Double( 12.34 ) },
45          { ".1234", new Double( .12340000000000 ) },
46          { "12.34f", Double.valueOf( 12.34 ) },
47          { "12.", new Double( 12 ) },
48          { "12e+1d", new Double( 120 ) },
49          { "'x'", new Character( 'x' ) },
50          { "'\\n'", new Character( '\n' ) },
51          { "'\\u048c'", new Character( '\u048c' ) },
52          { "'\\47'", new Character( '\47' ) },
53          { "'\\367'", new Character( '\367' ) },
54          { "'\\367", ExpressionSyntaxException.class },
55          { "'\\x'", ExpressionSyntaxException.class },
56          { "\"hello world\"", "hello world" },
57          { "\"\\u00a0\\u0068ell\\'o\\\\\\n\\r\\f\\t\\b\\\"\\167orld\\\"\"", "\u00a0hell'o\\\n\r\f\t\b\"world\"" },
58          { "\"hello world", ExpressionSyntaxException.class },
59          { "\"hello\\x world\"", ExpressionSyntaxException.class },
60          { "null", null },
61          { "true", Boolean.TRUE },
62          { "false", Boolean.FALSE },
63          { "{ false, true, null, 0, 1. }",
64              Arrays.asList( new Object[] { Boolean.FALSE, Boolean.TRUE, null, new Integer( 0 ), new Double( 1 ) } ) },
65          { "'HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"'",
66              "HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"" }, };
67  
68      /*
69       * =================================================================== Public static methods
70       * ===================================================================
71       */
72      @Parameters
73      public static Collection<Object[]> data()
74      {
75          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
76          for ( int i = 0; i < TESTS.length; i++ )
77          {
78              Object[] tmp = new Object[6];
79              tmp[0] = TESTS[i][0] + " (" + TESTS[i][1] + ")";
80              tmp[1] = null;
81              tmp[2] = TESTS[i][0];
82              tmp[3] = TESTS[i][1];
83              tmp[4] = null;
84              tmp[5] = null;
85  
86              data.add( tmp );
87          }
88          return data;
89      }
90  
91      /*
92       * =================================================================== Constructors
93       * ===================================================================
94       */
95      public ConstantTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
96                           Object expectedAfterSetResult )
97      {
98          super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
99      }
100 
101     @Before
102     @Override
103     public void setUp()
104     {
105         super.setUp();
106         _context.put( "x", "1" );
107         _context.put( "y", new BigDecimal( 1 ) );
108     }
109 }