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.exception.MathException;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  
25  /**
26   * This class tests support for strict math mode.
27   */
28  public class StrictMathTestCase extends BaseTestCase
29  {
30      public StrictMathTestCase(String name)
31      {
32         super(name);
33      }
34  
35      public void setUp() throws Exception
36      {
37          super.setUp();
38          engine.setProperty(RuntimeConstants.STRICT_MATH, Boolean.TRUE);
39          context.put("num", new Integer(5));
40          context.put("zero", new Integer(0));
41      }
42  
43      protected void assertNullMathEx(String operation)
44      {
45          String leftnull = "#set( $foo = $null "+operation+" $num )";
46          assertEvalException(leftnull, MathException.class);
47          String rightnull = "#set( $foo = $num "+operation+" $null )";
48          assertEvalException(rightnull, MathException.class);
49      }
50  
51      protected void assertImaginaryMathEx(String operation)
52      {
53          String infinity = "#set( $foo = $num "+operation+" $zero )";
54          assertEvalException(infinity, MathException.class);
55      }
56     
57  
58      public void testAdd()
59      {
60          assertNullMathEx("+");
61      }
62  
63      public void testSub()
64      {
65          assertNullMathEx("-");
66      }
67  
68      public void testMul()
69      {
70          assertNullMathEx("*");
71      }
72  
73      public void testMod()
74      {
75          assertNullMathEx("%");
76          assertImaginaryMathEx("%");
77      }
78  
79      public void testDiv()
80      {
81          assertNullMathEx("/");
82          assertImaginaryMathEx("/");
83      }
84  
85  }