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.runtime.RuntimeConstants;
24  
25  /**
26   * Used to check that nulls are properly handled in #if statements
27   */
28  public class IfNullTestCase extends BaseTestCase
29  {
30      public IfNullTestCase(final String name)
31      {
32          super(name);
33      }
34  
35      protected void setUpContext(VelocityContext context)
36      {
37          context.put("nullToString", new NullToString());
38          context.put("notnull", new Object());
39      }
40  
41      public void testIfEquals()
42      {
43          // both null
44          assertEvalEquals("foo", "#if( $null == $otherNull )foo#{else}bar#end");
45          assertEvalEquals("foo", "#if( $null == $nullToString )foo#{else}bar#end");
46          assertEvalEquals("foo", "#if( $nullToString == $null )foo#{else}bar#end");
47          // left null, right not
48          assertEvalEquals("bar", "#if( $nullToString == $notnull )foo#{else}bar#end");
49          assertEvalEquals("bar", "#if( $null == $notnull )foo#{else}bar#end");
50          // right null, left not
51          assertEvalEquals("bar", "#if( $notnull == $nullToString )foo#{else}bar#end");
52          assertEvalEquals("bar", "#if( $notnull == $null )foo#{else}bar#end");
53      }
54  
55      public void testIfNotEquals()
56      {
57          // both null
58          assertEvalEquals("bar", "#if( $null != $otherNull )foo#{else}bar#end");
59          assertEvalEquals("bar", "#if( $null != $nullToString )foo#{else}bar#end");
60          assertEvalEquals("bar", "#if( $nullToString != $null )foo#{else}bar#end");
61          // left null, right not
62          assertEvalEquals("foo", "#if( $nullToString != $notnull )foo#{else}bar#end");
63          assertEvalEquals("foo", "#if( $null != $notnull )foo#{else}bar#end");
64          // right null, left not
65          assertEvalEquals("foo", "#if( $notnull != $nullToString )foo#{else}bar#end");
66          assertEvalEquals("foo", "#if( $notnull != $null )foo#{else}bar#end");
67      }
68  
69      public void testIfValue()
70      {
71          assertEvalEquals("bar", "#if( $null )foo#{else}bar#end");
72          assertEvalEquals("bar", "#if( $nullToString )foo#{else}bar#end");
73          assertEvalEquals("foo", "#if( !$null )foo#{else}bar#end");
74          assertEvalEquals("foo", "#if( !$nullToString )foo#{else}bar#end");
75      }
76  
77      public void testIfAnd()
78      {
79          assertEvalEquals("bar", "#if( $null && $nullToString )foo#{else}bar#end");
80          assertEvalEquals("bar", "#if( $nullToString && $null )foo#{else}bar#end");
81          assertEvalEquals("bar", "#if( $null && $notnull )foo#{else}bar#end");
82          assertEvalEquals("bar", "#if( $notnull && $nullToString )foo#{else}bar#end");
83      }
84  
85      public void testIfOr()
86      {
87          assertEvalEquals("bar", "#if( $null || $nullToString )foo#{else}bar#end");
88          assertEvalEquals("bar", "#if( $nullToString || $null )foo#{else}bar#end");
89          assertEvalEquals("foo", "#if( $null || $notnull )foo#{else}bar#end");
90          assertEvalEquals("foo", "#if( $notnull || $nullToString )foo#{else}bar#end");
91      }
92  
93      public void testToStringNullCheckConfig()
94      {
95          engine.setProperty(RuntimeConstants.DIRECTIVE_IF_TOSTRING_NULLCHECK, Boolean.FALSE);
96          assertEvalEquals("bar", "#if( $null )foo#{else}bar#end");
97          assertEvalEquals("foo", "#if( $nullToString )foo#{else}bar#end");
98      }
99  
100     public static class NullToString
101     {
102         public String toString()
103         {
104             return null;
105         }
106     }
107 
108 }
109 
110