1 5 package com.opensymphony.webwork.views.jsp; 6 7 import com.mockobjects.servlet.MockHttpServletRequest; 8 import com.mockobjects.servlet.MockPageContext; 9 import com.opensymphony.xwork.ActionContext; 10 import com.opensymphony.xwork.util.OgnlValueStack; 11 import junit.framework.TestCase; 12 13 import javax.servlet.jsp.JspException ; 14 import javax.servlet.jsp.tagext.TagSupport ; 15 16 17 21 public class IfTagTest extends TestCase { 22 24 IfTag tag; 25 MockPageContext pageContext; 26 OgnlValueStack stack; 27 28 30 public void testNonBooleanTest() { 31 Foo foo = new Foo(); 33 foo.setNum(1); 34 stack.push(foo); 35 36 tag.setTest("num"); 38 39 int result = 0; 40 41 try { 42 result = tag.doStartTag(); 43 } catch (JspException e) { 44 e.printStackTrace(); 45 fail(); 46 } 47 48 assertEquals(TagSupport.EVAL_BODY_INCLUDE, result); 49 50 try { 51 result = tag.doEndTag(); 52 } catch (JspException e) { 53 e.printStackTrace(); 54 fail(); 55 } 56 } 57 58 public void testTestError() { 59 Foo foo = new Foo(); 61 foo.setNum(2); 62 stack.push(foo); 63 64 tag.setTest("nuuuuum == 2"); 66 67 int result = 0; 68 69 try { 70 result = tag.doStartTag(); 71 } catch (JspException e) { 72 e.printStackTrace(); 73 fail(); 74 } 75 76 assertEquals(TagSupport.SKIP_BODY, result); 77 78 try { 79 result = tag.doEndTag(); 80 } catch (JspException e) { 81 e.printStackTrace(); 82 fail(); 83 } 84 } 85 86 public void testTestFalse() { 87 Foo foo = new Foo(); 89 foo.setNum(2); 90 stack.push(foo); 91 92 tag.setTest("num != 2"); 94 95 int result = 0; 96 97 try { 98 result = tag.doStartTag(); 99 } catch (JspException e) { 100 e.printStackTrace(); 101 fail(); 102 } 103 104 assertEquals(TagSupport.SKIP_BODY, result); 105 106 try { 107 result = tag.doEndTag(); 108 } catch (JspException e) { 109 e.printStackTrace(); 110 fail(); 111 } 112 } 113 114 public void testTestTrue() { 115 Foo foo = new Foo(); 117 foo.setNum(2); 118 stack.push(foo); 119 120 tag.setTest("num == 2"); 122 123 int result = 0; 124 125 try { 126 result = tag.doStartTag(); 127 } catch (JspException e) { 128 e.printStackTrace(); 129 fail(); 130 } 131 132 assertEquals(TagSupport.EVAL_BODY_INCLUDE, result); 133 134 try { 135 result = tag.doEndTag(); 136 } catch (JspException e) { 137 e.printStackTrace(); 138 fail(); 139 } 140 } 141 142 protected void setUp() throws Exception { 143 tag = new IfTag(); 145 stack = new OgnlValueStack(); 146 147 MockHttpServletRequest request = new MockHttpServletRequest(); 149 ActionContext.getContext().setValueStack(stack); 150 request.setupGetAttribute(stack); 151 152 pageContext = new MockPageContext(); 154 pageContext.setRequest(request); 155 156 tag.setPageContext(pageContext); 158 } 159 160 162 class Foo { 163 int num; 164 165 public void setNum(int num) { 166 this.num = num; 167 } 168 169 public int getNum() { 170 return num; 171 } 172 } 173 } 174 | Popular Tags |