1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }