1 5 package com.opensymphony.webwork.views.jsp; 6 7 import com.mockobjects.servlet.MockBodyContent; 8 import com.mockobjects.servlet.MockJspWriter; 9 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.tagext.TagSupport ; 12 import java.util.*; 13 14 15 19 public class IteratorTagTest extends AbstractUITagTest { 20 22 IteratorTag tag; 23 24 26 public void testArrayIterator() { 27 Foo foo = new Foo(); 28 foo.setArray(new String []{"test1", "test2", "test3"}); 29 30 stack.push(foo); 31 32 tag.setValue("array"); 33 34 iterateThreeStrings(); 35 } 36 37 public void testCollectionIterator() { 38 Foo foo = new Foo(); 39 ArrayList list = new ArrayList(); 40 list.add("test1"); 41 list.add("test2"); 42 list.add("test3"); 43 foo.setList(list); 44 45 stack.push(foo); 46 47 tag.setValue("list"); 48 49 iterateThreeStrings(); 50 } 51 52 public void testEnumerationWrapper() { 53 Vector v = new Vector(); 54 v.add("foo"); 55 v.add("bar"); 56 57 Iterator ei = new EnumeratorIterator(v.elements()); 58 assertTrue(ei.hasNext()); 59 assertEquals("foo", ei.next()); 60 assertTrue(ei.hasNext()); 61 assertEquals("bar", ei.next()); 62 assertFalse(ei.hasNext()); 63 64 try { 65 ei.remove(); 66 fail("Remove method not supported, expected an exception"); 67 } catch (Exception e) { 68 } 70 } 71 72 public void testIteratorWithDefaultValue() { 73 stack.push(new String []{"test1", "test2", "test3"}); 74 iterateThreeStrings(); 75 } 76 77 public void testMapIterator() { 78 Foo foo = new Foo(); 79 HashMap map = new HashMap(); 80 map.put("test1", "123"); 81 map.put("test2", "456"); 82 map.put("test3", "789"); 83 foo.setMap(map); 84 85 stack.push(foo); 86 87 tag.setValue("map"); 88 89 int result = 0; 90 91 try { 92 result = tag.doStartTag(); 93 } catch (JspException e) { 94 e.printStackTrace(); 95 fail(); 96 } 97 98 assertEquals(TagSupport.EVAL_BODY_AGAIN, result); 99 assertEquals(4, stack.size()); 100 assertTrue(stack.getRoot().peek() instanceof Map.Entry); 101 102 try { 103 result = tag.doAfterBody(); 104 } catch (JspException e) { 105 e.printStackTrace(); 106 fail(); 107 } 108 109 assertEquals(TagSupport.EVAL_BODY_AGAIN, result); 110 assertEquals(4, stack.size()); 111 assertTrue(stack.getRoot().peek() instanceof Map.Entry); 112 113 try { 114 result = tag.doAfterBody(); 115 } catch (JspException e) { 116 e.printStackTrace(); 117 fail(); 118 } 119 120 assertEquals(TagSupport.EVAL_BODY_AGAIN, result); 121 assertEquals(4, stack.size()); 122 assertTrue(stack.getRoot().peek() instanceof Map.Entry); 123 124 try { 125 result = tag.doAfterBody(); 126 } catch (JspException e) { 127 e.printStackTrace(); 128 fail(); 129 } 130 131 assertEquals(TagSupport.SKIP_BODY, result); 132 assertEquals(3, stack.size()); 133 } 134 135 public void testStatus() { 136 Foo foo = new Foo(); 137 foo.setArray(new String []{"test1", "test2", "test3"}); 138 139 stack.push(foo); 140 141 tag.setValue("array"); 142 tag.setStatus("fooStatus"); 143 144 int result = 0; 145 146 try { 147 result = tag.doStartTag(); 148 } catch (JspException e) { 149 e.printStackTrace(); 150 fail(); 151 } 152 153 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 154 assertEquals("test1", stack.getRoot().peek()); 155 assertEquals(4, stack.size()); 156 157 IteratorStatus status = (IteratorStatus) context.get("fooStatus"); 158 assertNotNull(status); 159 assertFalse(status.isLast()); 160 assertTrue(status.isFirst()); 161 assertEquals(0, status.getIndex()); 162 assertEquals(1, status.getCount()); 163 assertTrue(status.isOdd()); 164 assertFalse(status.isEven()); 165 166 try { 167 result = tag.doAfterBody(); 168 } catch (JspException e) { 169 e.printStackTrace(); 170 fail(); 171 } 172 173 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 174 assertEquals("test2", stack.getRoot().peek()); 175 assertEquals(4, stack.size()); 176 177 status = (IteratorStatus) context.get("fooStatus"); 178 assertNotNull(status); 179 assertFalse(status.isLast()); 180 assertFalse(status.isFirst()); 181 assertEquals(1, status.getIndex()); 182 assertEquals(2, status.getCount()); 183 assertFalse(status.isOdd()); 184 assertTrue(status.isEven()); 185 186 try { 187 result = tag.doAfterBody(); 188 } catch (JspException e) { 189 e.printStackTrace(); 190 fail(); 191 } 192 193 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 194 assertEquals("test3", stack.getRoot().peek()); 195 assertEquals(4, stack.size()); 196 197 status = (IteratorStatus) context.get("fooStatus"); 198 assertNotNull(status); 199 assertTrue(status.isLast()); 200 assertFalse(status.isFirst()); 201 assertEquals(2, status.getIndex()); 202 assertEquals(3, status.getCount()); 203 assertTrue(status.isOdd()); 204 assertFalse(status.isEven()); 205 } 206 207 protected void setUp() throws Exception { 208 super.setUp(); 209 210 tag = new IteratorTag(); 212 213 MockBodyContent mockBodyContent = new MockBodyContent(); 214 mockBodyContent.setupGetEnclosingWriter(new MockJspWriter()); 215 tag.setBodyContent(mockBodyContent); 216 217 tag.setPageContext(pageContext); 219 } 220 221 private void iterateThreeStrings() { 222 int result = 0; 223 224 try { 225 result = tag.doStartTag(); 226 } catch (JspException e) { 227 e.printStackTrace(); 228 fail(); 229 } 230 231 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 232 assertEquals("test1", stack.getRoot().peek()); 233 assertEquals(4, stack.size()); 234 235 try { 236 result = tag.doAfterBody(); 237 } catch (JspException e) { 238 e.printStackTrace(); 239 fail(); 240 } 241 242 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 243 assertEquals("test2", stack.getRoot().peek()); 244 assertEquals(4, stack.size()); 245 246 try { 247 result = tag.doAfterBody(); 248 } catch (JspException e) { 249 e.printStackTrace(); 250 fail(); 251 } 252 253 assertEquals(result, TagSupport.EVAL_BODY_AGAIN); 254 assertEquals("test3", stack.getRoot().peek()); 255 assertEquals(4, stack.size()); 256 257 try { 258 result = tag.doAfterBody(); 259 } catch (JspException e) { 260 e.printStackTrace(); 261 fail(); 262 } 263 264 assertEquals(result, TagSupport.SKIP_BODY); 265 assertEquals(3, stack.size()); 266 } 267 268 270 class Foo { 271 private Collection list; 272 private Map map; 273 private String [] array; 274 275 public void setArray(String [] array) { 276 this.array = array; 277 } 278 279 public String [] getArray() { 280 return array; 281 } 282 283 public void setList(Collection list) { 284 this.list = list; 285 } 286 287 public Collection getList() { 288 return list; 289 } 290 291 public void setMap(Map map) { 292 this.map = map; 293 } 294 295 public Map getMap() { 296 return map; 297 } 298 } 299 } 300 | Popular Tags |