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