1   /*
2    * Created on 2004/09/13
3    */
4   package org.apache.velocity.tools.generic;
5   
6   import org.ieee.shinobu.demo.velocity.AbstractVelocityTestCase;
7   
8   /***
9    * @author shinobu
10   */
11  public class NullToolTest extends AbstractVelocityTestCase {
12  
13      public void testRemoveWithoutTool() throws Exception {
14          this.getContext().put("ctx", this.getContext());
15  
16          this.setTemplate("" +
17                  "#set ($foo = 'bar')\n" +
18                  "#set ($nulled = $ctx.remove('foo'))\n" +
19                  "$foo\n" +
20                  "");
21  
22          this.setExpected("" +
23                  "$foo\n" +
24                  "");
25  
26          this.assertVelocity();
27      }
28  
29      public void testRemove() throws Exception {
30          this.getContext().put("ctx", this.getContext());
31          this.getContext().put("null", new NullTool());
32  
33          this.setTemplate("" +
34                  "#set ($foo = 'bar')\n" +
35                  "$null.setNull($ctx, 'foo')" +
36                  "$foo\n" +
37                  "");
38  
39          this.setExpected("" +
40                  "$foo\n" +
41                  "");
42  
43          this.assertVelocity();
44      }
45  
46      public void testRemoveNoContext() throws Exception {
47          this.getContext().put("null", new NullTool());
48  
49          this.setTemplate("" +
50                  "#set ($foo = 'bar')\n" +
51                  "$null.setNull($ctx, 'foo')" +
52                  "$foo\n" +
53                  "");
54  
55          this.setExpected("" +
56                  "bar\n" +
57                  "");
58  
59          this.assertVelocity();
60      }
61  
62      public void testIsNull() throws Exception {
63          this.getContext().put("ctx", this.getContext());
64          this.getContext().put("null", new NullTool());
65          this.getContext().put("foo", "bar");
66          this.getContext().put("zero", new Integer(0));
67          this.getContext().put("false", Boolean.FALSE);
68  
69          this.setTemplate("" +
70                  "#if ($null.isNull($foo))\n" +
71                  "foo\n" +
72                  "#end\n" +
73                  "#if ($null.isNull($hoge))\n" +
74                  "hoge\n" +
75                  "#end\n" +
76                  "#if ($null.isNull($zero))\n" +
77                  "zero\n" +
78                  "#end\n" +
79                  "#if ($null.isNull($false))\n" +
80                  "false\n" +
81                  "#end\n" +
82                  "");
83  
84          this.setExpected("" +
85                  "hoge\n" +
86                  "");
87  
88          this.assertVelocity();
89      }
90  
91      public void testIsNotNull() throws Exception {
92          this.getContext().put("ctx", this.getContext());
93          this.getContext().put("null", new NullTool());
94          this.getContext().put("foo", "bar");
95          this.getContext().put("zero", new Integer(0));
96          this.getContext().put("false", Boolean.FALSE);
97  
98          this.setTemplate("" +
99                  "#if ($null.isNotNull($foo))\n" +
100                 "foo\n" +
101                 "#end\n" +
102                 "#if ($null.isNotNull($hoge))\n" +
103                 "hoge\n" +
104                 "#end\n" +
105                 "#if ($null.isNotNull($zero))\n" +
106                 "zero\n" +
107                 "#end\n" +
108                 "#if ($null.isNotNull($false))\n" +
109                 "false\n" +
110                 "#end\n" +
111                 "");
112 
113         this.setExpected("" +
114                 "foo\n" +
115                 "zero\n" +
116                 "false\n" +
117                 "");
118 
119         this.assertVelocity();
120     }
121 
122     public void testNullableSet() throws Exception {
123         this.getContext().put("ctx", this.getContext());
124         this.getContext().put("null", new NullTool());
125 
126         this.setTemplate("" +
127                 "$foo\n" +
128                 "$null.set($ctx, 'foo', 'bar')" +
129                 "$foo\n" +
130                 "$null.set($ctx, 'foo', $notSet)" +
131                 "$foo\n" +
132                 "");
133 
134         this.setExpected("" +
135                 "$foo\n" +
136                 "bar\n" +
137                 "$foo\n" +
138                 "");
139 
140         this.assertVelocity();
141     }
142 
143     public void testNullableSetNoContext() throws Exception {
144         this.getContext().put("null", new NullTool());
145 
146         this.setTemplate("" +
147                 "$foo\n" +
148                 "$null.set($ctx, 'foo', 'bar')" +
149                 "$foo\n" +
150                 "$null.set($ctx, 'foo', $notSet)" +
151                 "$foo\n" +
152                 "");
153 
154         this.setExpected("" +
155                 "$foo\n" +
156                 "$foo\n" +
157                 "$foo\n" +
158                 "");
159 
160         this.assertVelocity();
161     }
162 
163     public void testNull() throws Exception {
164         this.getContext().put("ctx", this.getContext());
165         this.getContext().put("null", new NullTool());
166 
167         this.setTemplate("" +
168                 "$null.null\n" +
169                 "$null.getNull()\n" +
170                 "");
171 
172         this.setExpected("" +
173                 "$null.null\n" +
174                 "$null.getNull()\n" +
175                 "");
176 
177         this.assertVelocity();
178     }
179 
180 }