1 package org.apache.commons.jelly.tags.velocity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Set;
20 import java.util.HashSet;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.jelly.JellyContext;
27 import org.apache.commons.collections.CollectionUtils;
28
29 /***
30 * Unit test for <code>JellyContextAdapter</code>.
31 *
32 * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
33 * @version $Id: JellyContextAdapterTest.java,v 1.3 2004/09/09 12:23:17 dion Exp $
34 */
35 public class JellyContextAdapterTest extends TestCase
36 {
37 JellyContext jellyContext;
38 JellyContextAdapter adapter;
39
40 /***
41 * Create the test case
42 *
43 * @param testName name of the test case
44 */
45 public JellyContextAdapterTest( String testName )
46 {
47 super( testName );
48 }
49
50 /***
51 * @return the suite of tests being tested
52 */
53 public static Test suite()
54 {
55 return new TestSuite( JellyContextAdapterTest.class );
56 }
57
58 public void setUp()
59 {
60 jellyContext = new JellyContext();
61 adapter = new JellyContextAdapter( jellyContext );
62 }
63
64 /***
65 * Test the behavior of null keys.
66 */
67 public void testNullKey()
68 {
69 adapter.setReadOnly( false );
70 adapter.put( null, new Object() );
71
72 assertTrue( adapter.get( null ) == null );
73 }
74
75 /***
76 * Test the behavior of null values.
77 */
78 public void testNullValue()
79 {
80 adapter.setReadOnly( false );
81 adapter.put( "key", null );
82
83 assertTrue( adapter.get( "key" ) == null );
84 }
85
86 /***
87 * Test that items can be added and retrieved from a read-write
88 * adpater. Also verify the key/value pair was actually inserted
89 * into the JellyContext.
90 */
91 public void testReadWritePut()
92 {
93 Object value = new Object();
94
95 adapter.setReadOnly( false );
96 adapter.put( "key", value );
97
98 assertTrue( "adapter: did not return the original value",
99 adapter.get( "key" ) == value );
100
101 assertTrue( "jellyContext: did not return the original value",
102 jellyContext.getVariable( "key" ) == value );
103 }
104
105 /***
106 * Test that items can be added and retrieved from a read-only
107 * adapter. Also verify the key/value pair was not inserted into
108 * the JellyContext.
109 */
110 public void testReadOnlyPut()
111 {
112 Object value = new Object();
113
114 adapter.setReadOnly( true );
115 adapter.put( "key", value );
116
117 assertTrue( "adapter: did not return the original value",
118 adapter.get( "key" ) == value );
119
120 assertTrue( "jellyContext: must return null when adapter is readonly",
121 jellyContext.getVariable( "key" ) == null );
122 }
123
124 /***
125 * Test that items can be removed from a read-write context. Also
126 * verify that the item is removed from the JellyContext.
127 */
128 public void testReadWriteRemove()
129 {
130 Object value = new Object();
131
132 adapter.setReadOnly( false );
133 adapter.put( "key", value );
134 Object oldValue = adapter.remove( "key" );
135
136 assertTrue( "Value returned from remove() is not the original",
137 value == oldValue );
138
139 assertTrue( "adapter: after removal of key, value should be null",
140 adapter.get( "key" ) == null );
141
142 assertTrue( "jellyContext: after removal of key, value should be null",
143 jellyContext.getVariable( "key" ) == null );
144
145 assertTrue( "Removal of non-existent key should return null",
146 adapter.remove( "non-existent key" ) == null );
147 }
148
149 /***
150 * Test that items can be removed from a read-only context. Also
151 * verify that the JellyContext is not impacted by removal of keys.
152 */
153 public void testReadOnlyRemove()
154 {
155 Object value = new Object();
156
157 adapter.setReadOnly( true );
158 adapter.put( "key", value );
159
160 Object oldValue = adapter.remove( "key" );
161
162 assertTrue( "Value returned from remove() is not the original",
163 value == oldValue );
164
165 assertTrue( "adapter: after removal of key, value should be null",
166 adapter.get( "key" ) == null );
167
168 assertTrue( "jellyContext: value should not be affected.",
169 jellyContext.getVariable( "key" ) == null );
170
171 assertTrue( "Removal of non-existent key should return null",
172 adapter.remove( "non-existent key" ) == null );
173 }
174
175 /***
176 * Test that items can shadow or hide items in the JellyContext.
177 * Removal of a key in the private context will unveil the key in
178 * the JellyContext if it exists.
179 */
180 public void testReadOnlyShadowingRemove()
181 {
182 Object value1 = new Object();
183 Object value2 = new Object();
184
185 adapter.setReadOnly( true );
186 adapter.put( "key", value1 );
187 jellyContext.setVariable( "key", value2 );
188
189 assertTrue( "adapter: before removal of key, value should be 1",
190 adapter.get( "key" ) == value1 );
191
192 adapter.remove( "key" );
193
194 assertTrue( "adapter: after removal of key, value should be 2",
195 adapter.get( "key" ) == value2 );
196
197 assertTrue( "jellyContext: value should not be affected.",
198 jellyContext.getVariable( "key" ) == value2 );
199 }
200
201 /***
202 * Test the containsKey method in a read-write adapter.
203 */
204 public void testReadWriteContainsKey()
205 {
206 Object value1 = new Object();
207 Object value2 = new Object();
208
209 adapter.setReadOnly( false );
210 adapter.put( "key1", value1 );
211 jellyContext.setVariable( "key2", value2 );
212
213 assertTrue( "adapter: did not contain the key",
214 adapter.containsKey( "key1" ) );
215
216 assertTrue( "adapter: should contain the key",
217 adapter.containsKey( "key2" ) );
218
219 assertTrue( "jellyContext: did not contain the key",
220 jellyContext.getVariable( "key1" ) != null );
221 }
222
223 /***
224 * Test the containsKey method in a read-only adapter.
225 */
226 public void testReadOnlyContainsKey()
227 {
228 Object value1= new Object();
229 Object value2 = new Object();
230
231 adapter.setReadOnly( true );
232 adapter.put( "key1", value1 );
233 jellyContext.setVariable( "key2", value2 );
234
235 assertTrue( "adapter: did not contain the key",
236 adapter.containsKey( "key1" ) );
237
238 assertTrue( "adapter: should not contain the key",
239 adapter.containsKey( "key2" ) );
240
241 assertTrue( "jellyContext: should not contain the key",
242 jellyContext.getVariable( "key1" ) == null );
243 }
244
245 /***
246 * Test the getKeys method of a read-write adapter.
247 */
248 public void testReadWriteGetKeys()
249 {
250 Object value1 = new Object();
251 Object value2 = new Object();
252 Object value3 = new Object();
253
254 adapter.setReadOnly( false );
255 adapter.put( "key1", value1 );
256 adapter.put( "key2", value2 );
257 jellyContext.setVariable( "key3", value3 );
258
259 Set expectedKeys = new HashSet();
260 expectedKeys.add( "key1" );
261 expectedKeys.add( "key2" );
262 expectedKeys.add( "key3" );
263
264 Set actualKeys = new HashSet();
265 CollectionUtils.addAll(actualKeys, adapter.getKeys());
266
267 assertTrue( "adapter: does not contain the correct key set",
268 actualKeys.containsAll( expectedKeys ) );
269 }
270
271 /***
272 * Test the getKeys method of a read-only adapter.
273 */
274 public void testReadOnlyGetKeys()
275 {
276 Object value1 = new Object();
277 Object value2 = new Object();
278 Object value3 = new Object();
279
280 adapter.setReadOnly( true );
281 adapter.put( "key1", value1 );
282 adapter.put( "key2", value2 );
283 jellyContext.setVariable( "key3", value3 );
284
285 Set expectedKeys = new HashSet();
286 expectedKeys.add( "key1" );
287 expectedKeys.add( "key2" );
288
289 Set actualKeys = new HashSet();
290 CollectionUtils.addAll(actualKeys, adapter.getKeys());
291
292 assertTrue( "adapter: does not contain the correct key set",
293 actualKeys.containsAll( expectedKeys ) );
294 }
295 }