1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.VelocityContext;
23  import org.apache.velocity.exception.MathException;
24  import org.apache.velocity.runtime.RuntimeConstants;
25  
26  /**
27   * This class tests support for strict math mode.
28   */
29  public class StrictMathTestCase extends BaseEvalTestCase
30  {
31      public StrictMathTestCase(String name)
32      {
33         super(name);
34      }
35  
36      public void setUp() throws Exception
37      {
38          super.setUp();
39          engine.setProperty(RuntimeConstants.STRICT_MATH, Boolean.TRUE);
40          context.put("num", new Integer(5));
41          context.put("zero", new Integer(0));
42      }
43  
44      protected void assertNullMathEx(String operation)
45      {
46          String leftnull = "#set( $foo = $null "+operation+" $num )";
47          assertEvalException(leftnull, MathException.class);
48          String rightnull = "#set( $foo = $num "+operation+" $null )";
49          assertEvalException(rightnull, MathException.class);
50      }
51  
52      protected void assertImaginaryMathEx(String operation)
53      {
54          String infinity = "#set( $foo = $num "+operation+" $zero )";
55          assertEvalException(infinity, MathException.class);
56      }
57     
58  
59      public void testAdd()
60      {
61          assertNullMathEx("+");
62      }
63  
64      public void testSub()
65      {
66          assertNullMathEx("-");
67      }
68  
69      public void testMul()
70      {
71          assertNullMathEx("*");
72      }
73  
74      public void testMod()
75      {
76          assertNullMathEx("%");
77          assertImaginaryMathEx("%");
78      }
79  
80      public void testDiv()
81      {
82          assertNullMathEx("/");
83          assertImaginaryMathEx("/");
84      }
85  
86  }