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.VelocityException;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  
25  /**
26   * Make sure exceptions are thrown for strict comparisons that
27   * cannot be compared.
28   */
29  public class StrictCompareTestCase extends BaseTestCase
30  {
31      public StrictCompareTestCase(String name)
32      {
33          super(name);
34      }
35  
36      public void setUp() throws Exception
37      {
38          super.setUp();
39          engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
40          context.put("NULL", null);
41          context.put("a", "abc");
42          context.put("b", new Integer(3));
43          context.put("TRUE", Boolean.TRUE);
44      }
45  
46      public void testCompare()
47      {
48          assertVelocityEx("#if($a > $NULL)#end");
49          assertVelocityEx("#if($a < $NULL)#end");
50          assertVelocityEx("#if($a >= $NULL)#end");
51          assertVelocityEx("#if($a <= $NULL)#end");
52  
53          assertVelocityEx("#if($NULL > $a)#end");
54          assertVelocityEx("#if($NULL < $a)#end");
55          assertVelocityEx("#if($NULL >= $a)#end");        
56          assertVelocityEx("#if($NULL <= $a)#end");
57  
58          assertVelocityEx("#if($NULL >= $NULL)#end");
59          assertVelocityEx("#if($a >= $b)#end");
60          assertVelocityEx("#if($a <= $b)#end");
61          assertVelocityEx("#if($a > $b)#end");
62          assertVelocityEx("#if($a < $b)#end");
63  
64          assertVelocityEx("#if($a < 5)#end");
65          assertVelocityEx("#if($a > 5)#end");        
66      }
67      
68      /**
69       * Assert that we get a VelocityException when calling evaluate
70       */
71      public void assertVelocityEx(String template)
72      {
73          assertEvalException(template, VelocityException.class);
74      }
75  }