1 7 package com.inversoft.verge.mvc.view.jsp.error.test; 8 9 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.tagext.Tag ; 12 13 import com.inversoft.error.PropertyError; 14 import com.inversoft.junit.JspTestCallback; 15 import com.inversoft.junit.JspTestCase; 16 import com.inversoft.verge.mvc.model.ModelResolution; 17 import com.inversoft.verge.mvc.model.form.FormMetaData; 18 import com.inversoft.verge.mvc.view.jsp.error.HasErrorsTag; 19 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry; 20 import com.inversoft.verge.util.RequestContext; 21 22 23 32 public class HasErrorsTagTest extends JspTestCase { 33 34 private boolean called; 35 36 41 public HasErrorsTagTest(String name) { 42 super(name); 43 setLocal(true); 44 } 45 46 47 50 public void testComplex() { 51 HasErrorsTag tag = new HasErrorsTag(); 52 tag.setProperty("formBean.${type}[${currentCount}]"); 53 tag.setEquals(Boolean.TRUE); 54 tag.setPageContext(pageContext); 55 56 assertEquals("formBean.${type}[${currentCount}]", tag.getProperty()); 57 assertEquals(Boolean.TRUE, tag.getEquals()); 58 59 pageContext.setAttribute("type", "word"); 60 pageContext.setAttribute("currentCount", "1"); 61 62 RequestContext context = new RequestContext(request); 64 PropertyError error = new PropertyError("webBean.word[1]", "message"); 65 context.addError(error); 66 67 FormMetaData fmd = new FormMetaData("webBean", null); 69 ModelResolution mr = new ModelResolution(new Object (), fmd); 70 ModelResolutionRegistry.store("formBean", mr, pageContext); 71 72 called = false; 73 JspTestCallback callback = new JspTestCallback() { 74 public void callback() { 75 called = true; 76 } 77 }; 78 79 try { 80 assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback), 81 Tag.EVAL_PAGE); 82 assertTrue("SHould have called body", called); 83 } catch (JspException jspe) { 84 fail(jspe.toString()); 85 } 86 } 87 88 91 public void testNoProperty() { 92 HasErrorsTag tag = new HasErrorsTag(); 93 tag.setEquals(Boolean.TRUE); 94 tag.setPageContext(pageContext); 95 96 RequestContext context = new RequestContext(request); 98 PropertyError error = new PropertyError("webBean.word[1]", "message"); 99 context.addError(error); 100 101 called = false; 102 JspTestCallback callback = new JspTestCallback() { 103 public void callback() { 104 called = true; 105 } 106 }; 107 108 try { 109 assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback), 110 Tag.EVAL_PAGE); 111 assertTrue("Should have called body", called); 112 } catch (JspException jspe) { 113 fail(jspe.toString()); 114 } 115 } 116 117 120 public void testNoErrors() { 121 HasErrorsTag tag = new HasErrorsTag(); 122 tag.setEquals(Boolean.TRUE); 123 tag.setPageContext(pageContext); 124 125 called = false; 126 JspTestCallback callback = new JspTestCallback() { 127 public void callback() { 128 called = true; 129 } 130 }; 131 132 try { 133 assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback), 134 Tag.EVAL_PAGE); 135 assertFalse("Should have called body", called); 136 } catch (JspException jspe) { 137 fail(jspe.toString()); 138 } 139 } 140 141 144 public void testVariable() { 145 HasErrorsTag tag = new HasErrorsTag(); 146 tag.setProperty("formBean.${type}[${currentCount}]"); 147 tag.setEquals(Boolean.TRUE); 148 tag.setPageContext(pageContext); 149 tag.setVar("errorVariable"); 150 tag.setValue("value"); 151 152 assertEquals("errorVariable", tag.getVar()); 153 assertEquals("value", tag.getValue()); 154 155 pageContext.setAttribute("type", "word"); 156 pageContext.setAttribute("currentCount", "1"); 157 158 RequestContext context = new RequestContext(request); 160 PropertyError error = new PropertyError("webBean.word[1]", "message"); 161 context.addError(error); 162 163 FormMetaData fmd = new FormMetaData("webBean", null); 165 ModelResolution mr = new ModelResolution(new Object (), fmd); 166 ModelResolutionRegistry.store("formBean", mr, pageContext); 167 168 called = false; 169 JspTestCallback callback = new JspTestCallback() { 170 public void callback() { 171 called = true; 172 } 173 }; 174 175 try { 176 assertEquals("Should have returned EVAL_PAGE", runTag(tag, callback), 177 Tag.EVAL_PAGE); 178 assertTrue("SHould have called body", called); 179 assertEquals("value", pageContext.getAttribute("errorVariable")); 180 } catch (JspException jspe) { 181 fail(jspe.toString()); 182 } 183 } 184 } | Popular Tags |