1 18 package org.apache.struts.taglib.logic; 19 20 import javax.servlet.ServletException ; 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.PageContext ; 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 import org.apache.cactus.JspTestCase; 26 import org.apache.cactus.WebRequest; 27 import org.apache.struts.util.LabelValueBean; 28 29 34 public class TestPresentTag extends JspTestCase { 35 protected final static String COOKIE_KEY = "org.apache.struts.taglib.logic.COOKIE_KEY"; 36 protected final static String HEADER_KEY = "org.apache.struts.taglib.logic.HEADER_KEY"; 37 protected final static String PARAMETER_KEY = "org.apache.struts.taglib.logic.PARAMETER_KEY"; 38 39 44 public TestPresentTag(String theName) { 45 super(theName); 46 } 47 48 53 public static void main(String [] theArgs) { 54 junit.awtui.TestRunner.main(new String [] {TestPresentTag.class.getName()}); 55 } 56 57 61 public static Test suite() { 62 return new TestSuite(TestPresentTag.class); 64 } 65 66 68 71 public void testApplicationScopeObjectPresent() throws ServletException , JspException { 72 PresentTag pt = new PresentTag(); 73 String testKey = "testApplicationScopePresent"; 74 String testStringValue = "abc"; 75 76 pageContext.setAttribute(testKey, testStringValue, PageContext.APPLICATION_SCOPE); 77 pt.setPageContext(pageContext); 78 pt.setName(testKey); 79 pt.setScope("application"); 80 81 assertEquals("Value present (not null)", true, pt.condition(true)); 82 } 83 84 87 public void testApplicationScopeObjectNotPresent() throws ServletException , JspException { 88 PresentTag pt = new PresentTag(); 89 String testKey = "testApplicationScopeNotPresent"; 90 91 pt.setPageContext(pageContext); 92 pt.setName(testKey); 93 pt.setScope("application"); 94 95 assertEquals("Value not present (null)", false, pt.condition(true)); 96 } 97 98 101 public void testSessionScopeObjectPresent() throws ServletException , JspException { 102 PresentTag pt = new PresentTag(); 103 String testKey = "testSessionScopePresent"; 104 String testStringValue = "abc"; 105 106 pageContext.setAttribute(testKey, testStringValue, PageContext.SESSION_SCOPE); 107 pt.setPageContext(pageContext); 108 pt.setName(testKey); 109 pt.setScope("session"); 110 111 assertEquals("Value present (not null)", true, pt.condition(true)); 112 } 113 114 117 public void testSessionScopeObjectNotPresent() throws ServletException , JspException { 118 PresentTag pt = new PresentTag(); 119 String testKey = "testSessionScopeNotPresent"; 120 121 pt.setPageContext(pageContext); 122 pt.setName(testKey); 123 pt.setScope("session"); 124 125 assertEquals("Value present (not null)", false, pt.condition(true)); 126 } 127 128 131 public void testRequestScopeObjectPresent() throws ServletException , JspException { 132 PresentTag pt = new PresentTag(); 133 String testKey = "testRequestScopePresent"; 134 String testStringValue = "abc"; 135 pt.setScope("request"); 136 137 pageContext.setAttribute(testKey, testStringValue, PageContext.REQUEST_SCOPE); 138 pt.setPageContext(pageContext); 139 pt.setName(testKey); 140 141 assertEquals("Value present (not null)", true, pt.condition(true)); 142 } 143 144 147 public void testRequestScopeObjectNotPresent() throws ServletException , JspException { 148 PresentTag pt = new PresentTag(); 149 String testKey = "testRequestScopeNotPresent"; 150 151 pt.setPageContext(pageContext); 152 pt.setName(testKey); 153 pt.setScope("request"); 154 155 assertEquals("Value not present (null)", false, pt.condition(true)); 156 } 157 158 161 public void testPageScopeObjectPresent() throws ServletException , JspException { 162 PresentTag pt = new PresentTag(); 163 String testKey = "testPageScopePresent"; 164 String testStringValue = "abc"; 165 pt.setScope("page"); 166 167 pageContext.setAttribute(testKey, testStringValue, PageContext.PAGE_SCOPE); 168 pt.setPageContext(pageContext); 169 pt.setName(testKey); 170 171 assertEquals("Value present (not null)", true, pt.condition(true)); 172 } 173 174 177 public void testPageScopeObjectNotPresent() throws ServletException , JspException { 178 PresentTag pt = new PresentTag(); 179 String testKey = "testPageScopeNotPresent"; 180 181 pt.setPageContext(pageContext); 182 pt.setName(testKey); 183 pt.setScope("page"); 184 185 assertEquals("Value not present (null)", false, pt.condition(true)); 186 } 187 188 192 public void testApplicationScopePropertyPresent() 193 throws ServletException , JspException { 194 PresentTag pt = new PresentTag(); 195 String testKey = "testApplicationScopePropertyPresent"; 196 197 String testStringValue = "The Value"; 198 LabelValueBean lvb = new LabelValueBean("The Key", testStringValue); 199 200 pageContext.setAttribute( 201 testKey, 202 lvb, 203 PageContext.APPLICATION_SCOPE); 204 pt.setPageContext(pageContext); 205 pt.setName(testKey); 206 pt.setScope("application"); 207 208 pt.setProperty("value"); 209 assertEquals("Property present (not null)", true, pt.condition(true)); 210 } 211 212 217 public void testApplicationScopePropertyNotPresent() 218 throws ServletException , JspException { 219 PresentTag pt = new PresentTag(); 220 String testKey = "testApplicationScopePropertyPresent"; 221 222 String testStringValue = null; 223 LabelValueBean lvb = new LabelValueBean("The Key", testStringValue); 224 225 pageContext.setAttribute( 226 testKey, 227 lvb, 228 PageContext.APPLICATION_SCOPE); 229 pt.setPageContext(pageContext); 230 pt.setName(testKey); 231 pt.setScope("application"); 232 233 pt.setProperty("value"); 234 assertEquals("Property present (not null)", false, pt.condition(true)); 235 } 236 237 241 public void testRequestScopePropertyPresent() 242 throws ServletException , JspException { 243 PresentTag pt = new PresentTag(); 244 String testKey = "testRequestScopePropertyPresent"; 245 246 String testStringValue = "The Value"; 247 LabelValueBean lvb = new LabelValueBean("The Key", testStringValue); 248 249 pageContext.setAttribute( 250 testKey, 251 lvb, 252 PageContext.REQUEST_SCOPE); 253 pt.setPageContext(pageContext); 254 pt.setName(testKey); 255 pt.setScope("request"); 256 257 pt.setProperty("value"); 258 assertEquals("Property present (not null)", true, pt.condition(true)); 259 } 260 261 266 public void testRequestScopePropertyNotPresent() 267 throws ServletException , JspException { 268 PresentTag pt = new PresentTag(); 269 String testKey = "testRequestScopePropertyNotPresent"; 270 271 String testStringValue = null; 272 LabelValueBean lvb = new LabelValueBean("The Key", testStringValue); 273 274 pageContext.setAttribute( 275 testKey, 276 lvb, 277 PageContext.REQUEST_SCOPE); 278 pt.setPageContext(pageContext); 279 pt.setName(testKey); 280 pt.setScope("request"); 281 282 pt.setProperty("value"); 283 assertEquals("Property present (not null)", false, pt.condition(true)); 284 } 285 286 289 294 295 298 308 309 312 public void testCookieNotPresent() throws ServletException , JspException { 313 PresentTag pt = new PresentTag(); 314 315 pt.setPageContext(pageContext); 316 pt.setCookie(COOKIE_KEY); 317 318 assertEquals("Cookie not present", false, pt.condition(true)); 319 } 320 321 324 public void beginHeaderPresent(WebRequest testRequest) { 325 testRequest.addHeader(HEADER_KEY, "header value"); 326 } 327 328 331 public void testHeaderPresent() throws ServletException , JspException { 332 PresentTag pt = new PresentTag(); 333 334 pt.setPageContext(pageContext); 335 pt.setHeader(HEADER_KEY); 336 337 assertEquals("Header present", true, pt.condition(true)); 338 } 339 340 343 public void testHeaderNotPresent() throws ServletException , JspException { 344 PresentTag pt = new PresentTag(); 345 346 pt.setPageContext(pageContext); 347 pt.setHeader(HEADER_KEY); 348 349 assertEquals("Header not present", false, pt.condition(true)); 350 } 351 352 355 public void beginParameterPresent(WebRequest testRequest) { 356 testRequest.addParameter(PARAMETER_KEY, "parameter value"); 357 } 358 359 362 public void testParameterPresent() throws ServletException , JspException { 363 PresentTag pt = new PresentTag(); 364 365 pt.setPageContext(pageContext); 366 pt.setParameter(PARAMETER_KEY); 367 368 assertEquals("Parameter present", true, pt.condition(true)); 369 } 370 371 374 public void testParameterNotPresent() throws ServletException , JspException { 375 PresentTag pt = new PresentTag(); 376 377 pt.setPageContext(pageContext); 378 pt.setParameter(PARAMETER_KEY); 379 380 assertEquals("Parameter not present", false, pt.condition(true)); 381 } 382 383 } 384 | Popular Tags |