001: package com.mockrunner.test.web;
002:
003: import java.util.ArrayList;
004: import java.util.Enumeration;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.el.ELContext;
010: import javax.servlet.jsp.JspContext;
011: import javax.servlet.jsp.JspException;
012: import javax.servlet.jsp.JspWriter;
013: import javax.servlet.jsp.el.ExpressionEvaluator;
014: import javax.servlet.jsp.el.VariableResolver;
015: import javax.servlet.jsp.tagext.DynamicAttributes;
016: import javax.servlet.jsp.tagext.SimpleTag;
017: import javax.servlet.jsp.tagext.Tag;
018: import javax.servlet.jsp.tagext.TagSupport;
019:
020: import com.mockrunner.base.BaseTestCase;
021: import com.mockrunner.mock.web.MockJspWriter;
022: import com.mockrunner.mock.web.MockPageContext;
023: import com.mockrunner.tag.DynamicAttribute;
024: import com.mockrunner.tag.DynamicChild;
025: import com.mockrunner.tag.NestedBodyTag;
026: import com.mockrunner.tag.NestedSimpleTag;
027: import com.mockrunner.tag.NestedStandardTag;
028: import com.mockrunner.tag.NestedTag;
029: import com.mockrunner.tag.RuntimeAttribute;
030: import com.mockrunner.tag.TagUtil;
031:
032: public class TagUtilTest extends BaseTestCase {
033: private MockPageContext pageContext;
034: private Map testMap;
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: pageContext = getWebMockObjectFactory().getMockPageContext();
039: testMap = new HashMap();
040: }
041:
042: public void testCreateNestedTagInstanceParameters() {
043: try {
044: TagUtil.createNestedTagInstance(null, pageContext, testMap);
045: fail();
046: } catch (IllegalArgumentException exc) {
047: //should throw exception
048: }
049: try {
050: TagUtil.createNestedTagInstance((Object) null, pageContext,
051: testMap);
052: fail();
053: } catch (IllegalArgumentException exc) {
054: //should throw exception
055: }
056: try {
057: TagUtil
058: .createNestedTagInstance("abc", pageContext,
059: testMap);
060: fail();
061: } catch (IllegalArgumentException exc) {
062: //should throw exception
063: }
064: try {
065: TagUtil.createNestedTagInstance(TestSimpleTag.class, "abc",
066: testMap);
067: fail();
068: } catch (IllegalArgumentException exc) {
069: //should throw exception
070: }
071: try {
072: TagUtil.createNestedTagInstance(TestBodyTag.class,
073: new TestJspContext(), testMap);
074: fail();
075: } catch (IllegalArgumentException exc) {
076: //should throw exception
077: }
078: TagUtil.createNestedTagInstance(TestSimpleTag.class,
079: new TestJspContext(), testMap);
080: }
081:
082: public void testCreateNestedTagInstance() {
083: TagSupport tag = (TagSupport) TagUtil.createNestedTagInstance(
084: TestTag.class, pageContext, testMap);
085: assertTrue(tag instanceof NestedStandardTag);
086: assertTrue(((NestedTag) tag).getTag() instanceof TestTag);
087: tag = (TagSupport) TagUtil.createNestedTagInstance(
088: TestBodyTag.class, pageContext, testMap);
089: assertTrue(tag instanceof NestedBodyTag);
090: assertTrue(((NestedTag) tag).getTag() instanceof TestBodyTag);
091: SimpleTag simpleTag = (SimpleTag) TagUtil
092: .createNestedTagInstance(TestSimpleTag.class,
093: pageContext, testMap);
094: assertTrue(simpleTag instanceof NestedSimpleTag);
095: assertTrue(((NestedTag) simpleTag).getWrappedTag() instanceof TestSimpleTag);
096:
097: TestTag testTag = new TestTag();
098: tag = (TagSupport) TagUtil.createNestedTagInstance(testTag,
099: pageContext, testMap);
100: assertTrue(tag instanceof NestedStandardTag);
101: assertSame(testTag, ((NestedTag) tag).getTag());
102: TestBodyTag testBodyTag = new TestBodyTag();
103: tag = (TagSupport) TagUtil.createNestedTagInstance(testBodyTag,
104: pageContext, testMap);
105: assertTrue(tag instanceof NestedBodyTag);
106: assertSame(testBodyTag, ((NestedTag) tag).getTag());
107: TestSimpleTag testSimpleTag = new TestSimpleTag();
108: simpleTag = (SimpleTag) TagUtil.createNestedTagInstance(
109: testSimpleTag, pageContext, testMap);
110: assertTrue(simpleTag instanceof NestedSimpleTag);
111: assertSame(testSimpleTag, ((NestedTag) simpleTag)
112: .getWrappedTag());
113: }
114:
115: public void testPopulateTag() {
116: testMap.put("testString", "test");
117: testMap.put("testDouble", new Double(12.3));
118: testMap.put("testInteger", new Integer(100));
119: TestTag tag = new TestTag();
120: TagUtil.populateTag(tag, testMap);
121: assertEquals("test", tag.getTestString());
122: assertEquals(12.3, tag.getTestDouble(), 0.00);
123: assertEquals(new Integer(100), tag.getTestInteger());
124: testMap.put("testInteger", "3");
125: testMap.put("noValidProperty", "123");
126: testMap.put("testDouble", "notValid");
127: TagUtil.populateTag(tag, testMap);
128: assertEquals(new Integer(3), tag.getTestInteger());
129: assertEquals(0.0, tag.getTestDouble(), 0.00);
130: tag = new TestTag();
131: testMap.clear();
132: TagUtil.populateTag(tag, testMap);
133: assertNull(tag.getTestInteger());
134: assertNull(tag.getTestString());
135: assertEquals(0, tag.getTestDouble(), 0.0);
136: TestSimpleTag testSimpleTag = new TestSimpleTag();
137: testMap.put("booleanProperty", "true");
138: testMap.put("floatProperty", "123.x");
139: testMap.put("stringProperty", "aString");
140: TagUtil.populateTag(testSimpleTag, testMap);
141: assertEquals(0.0, testSimpleTag.getFloatProperty(), 0.0);
142: assertTrue(testSimpleTag.getBooleanProperty());
143: assertEquals("aString", testSimpleTag.getStringProperty());
144: }
145:
146: public void testPopulateTagWithArbitraryBeans() {
147: testMap.put("object", this );
148: testMap.put("tagUtilTest", this );
149: ArbitraryTag tag = new ArbitraryTag();
150: TagUtil.populateTag(tag, testMap);
151: assertSame(this , tag.getObject());
152: assertSame(this , tag.getTagUtilTest());
153: testMap.put("object", "abc");
154: TagUtil.populateTag(tag, testMap);
155: assertEquals("abc", tag.getObject());
156: assertSame(this , tag.getTagUtilTest());
157: }
158:
159: public void testPopulateDynamicAttributesWithNonDynamicTag() {
160: Object object = new Object();
161: testMap.put("object", new DynamicAttribute("uri", object));
162: ArbitraryTag tag = new ArbitraryTag();
163: try {
164: TagUtil.populateTag(tag, testMap);
165: fail();
166: } catch (IllegalArgumentException exc) {
167: //should throw exception
168: }
169: testMap.clear();
170: testMap.put("testobject", object);
171: TagUtil.populateTag(tag, testMap);
172: }
173:
174: public void testPopulateDynamicAttributesWithDynamicTag() {
175: Object object = new Object();
176: testMap.put("object", new DynamicAttribute("uri", object));
177: DynamicTag tag = new DynamicTag();
178: TagUtil.populateTag(tag, testMap);
179: assertNull(tag.getObject());
180: assertEquals(1, tag.getDynamicAttributesMap().size());
181: DynamicAttribute attribute = (DynamicAttribute) tag
182: .getDynamicAttributesMap().get("object");
183: assertEquals("uri", attribute.getUri());
184: assertSame(object, attribute.getValue());
185: tag.clearDynamicAttributes();
186: testMap.put("object", object);
187: testMap.put("testobject", object);
188: TagUtil.populateTag(tag, testMap);
189: assertSame(object, tag.getObject());
190: assertEquals(1, tag.getDynamicAttributesMap().size());
191: attribute = (DynamicAttribute) tag.getDynamicAttributesMap()
192: .get("testobject");
193: assertNull(attribute.getUri());
194: assertSame(object, attribute.getValue());
195: }
196:
197: public void testPopulateRuntimeAttribute() {
198: TestRuntimeAttribute runtimeAttribute = new TestRuntimeAttribute(
199: "runtimevalue");
200: testMap.put("test", new DynamicAttribute("uri",
201: runtimeAttribute));
202: testMap.put("object", runtimeAttribute);
203: testMap.put("xyz", runtimeAttribute);
204: DynamicTag tag = new DynamicTag();
205: TagUtil.populateTag(tag, testMap);
206: assertEquals("runtimevalue", tag.getObject());
207: assertEquals(2, tag.getDynamicAttributesMap().size());
208: DynamicAttribute attribute = (DynamicAttribute) tag
209: .getDynamicAttributesMap().get("test");
210: assertEquals("uri", attribute.getUri());
211: assertSame("runtimevalue", attribute.getValue());
212: attribute = (DynamicAttribute) tag.getDynamicAttributesMap()
213: .get("xyz");
214: assertNull(attribute.getUri());
215: assertSame("runtimevalue", attribute.getValue());
216: }
217:
218: public void testEvalBody() throws Exception {
219: TestTag testStandardTag = new TestTag();
220: TestBodyTag testBodyTag = new TestBodyTag();
221: TestSimpleTag testSimpleTag = new TestSimpleTag();
222: NestedStandardTag standardTag = (NestedStandardTag) TagUtil
223: .createNestedTagInstance(testStandardTag, pageContext,
224: new HashMap());
225: NestedBodyTag bodyTag = (NestedBodyTag) TagUtil
226: .createNestedTagInstance(testBodyTag, pageContext,
227: new HashMap());
228: NestedSimpleTag simpleTag = (NestedSimpleTag) TagUtil
229: .createNestedTagInstance(testSimpleTag, pageContext,
230: new HashMap());
231: List bodyList = new ArrayList();
232: bodyList.add(standardTag);
233: bodyList.add(bodyTag);
234: bodyList.add(simpleTag);
235: bodyList.add(new TestDynamicChild("thisisa"));
236: bodyList.add("test");
237: bodyList.add(new TestDynamicChild(null));
238: bodyList.add(new Integer(123));
239: bodyList.add(null);
240: TagUtil.evalBody(bodyList, pageContext);
241: assertTrue(testStandardTag.wasDoStartTagCalled());
242: assertTrue(testBodyTag.wasDoStartTagCalled());
243: assertTrue(testSimpleTag.wasDoTagCalled());
244: assertEquals(
245: "TestTagTestBodyTagTestSimpleTagthisisatestnull123null",
246: ((MockJspWriter) pageContext.getOut())
247: .getOutputAsString());
248: try {
249: TagUtil.evalBody(bodyList, "WrongContext");
250: fail();
251: } catch (IllegalArgumentException exc) {
252: //should throw exception
253: }
254: }
255:
256: public void testHandleException() throws Exception {
257: Tag tag = new TestTag();
258: RuntimeException runtimeExcToBeHandled = new RuntimeException();
259: try {
260: TagUtil.handleException(tag, runtimeExcToBeHandled);
261: fail();
262: } catch (Exception exc) {
263: assertSame(runtimeExcToBeHandled, exc);
264: }
265: JspException jspExcToBeHandled = new JspException();
266: try {
267: TagUtil.handleException(tag, jspExcToBeHandled);
268: fail();
269: } catch (JspException exc) {
270: assertSame(jspExcToBeHandled, exc);
271: }
272: ExceptionTestTag testTag = new ExceptionTestTag();
273: TagUtil.handleException(testTag, jspExcToBeHandled);
274: assertTrue(testTag.wasDoCatchCalled());
275: assertSame(jspExcToBeHandled, testTag.getCaughtException());
276: RuntimeException runtimeExcToBeRethrown = new RuntimeException();
277: testTag = new ExceptionTestTag(runtimeExcToBeRethrown);
278: try {
279: TagUtil.handleException(testTag, runtimeExcToBeHandled);
280: fail();
281: } catch (Exception exc) {
282: assertTrue(testTag.wasDoCatchCalled());
283: assertSame(runtimeExcToBeHandled, testTag
284: .getCaughtException());
285: assertSame(runtimeExcToBeRethrown, exc);
286: }
287: JspException jspExcToBeRethrown = new JspException();
288: testTag = new ExceptionTestTag(jspExcToBeRethrown);
289: try {
290: TagUtil.handleException(testTag, runtimeExcToBeHandled);
291: fail();
292: } catch (Exception exc) {
293: assertTrue(testTag.wasDoCatchCalled());
294: assertSame(runtimeExcToBeHandled, testTag
295: .getCaughtException());
296: assertSame(jspExcToBeRethrown, exc);
297: }
298: Exception excToBeHandled = new Exception();
299: testTag = new ExceptionTestTag(excToBeHandled);
300: try {
301: TagUtil.handleException(testTag, excToBeHandled);
302: fail();
303: } catch (JspException exc) {
304: assertTrue(testTag.wasDoCatchCalled());
305: assertSame(excToBeHandled, testTag.getCaughtException());
306: assertSame(excToBeHandled, exc.getRootCause());
307: }
308: excToBeHandled = new Exception();
309: testTag = new ExceptionTestTag();
310: TagUtil.handleException(testTag, excToBeHandled);
311: assertTrue(testTag.wasDoCatchCalled());
312: assertSame(excToBeHandled, testTag.getCaughtException());
313: }
314:
315: public void testHandleFinally() throws Exception {
316: Tag tag = new TestTag();
317: TagUtil.handleFinally(tag);
318: ExceptionTestTag testTag = new ExceptionTestTag();
319: TagUtil.handleFinally(testTag);
320: assertTrue(testTag.wasDoFinallyCalled());
321: }
322:
323: private class TestRuntimeAttribute implements RuntimeAttribute {
324: private Object value;
325:
326: public TestRuntimeAttribute(Object value) {
327: this .value = value;
328: }
329:
330: public Object evaluate() {
331: return value;
332: }
333: }
334:
335: public class ArbitraryTag extends TagSupport {
336: private TagUtilTest tagUtilTest;
337: private Object object;
338:
339: public Object getObject() {
340: return object;
341: }
342:
343: public TagUtilTest getTagUtilTest() {
344: return tagUtilTest;
345: }
346:
347: public void setObject(Object object) {
348: this .object = object;
349: }
350:
351: public void setTagUtilTest(TagUtilTest tagUtilTest) {
352: this .tagUtilTest = tagUtilTest;
353: }
354: }
355:
356: public class DynamicTag extends TagSupport implements
357: DynamicAttributes {
358: private Map dynamicAttributes = new HashMap();
359: private Object object;
360:
361: public void setDynamicAttribute(String uri, String localName,
362: Object value) throws JspException {
363: dynamicAttributes.put(localName, new DynamicAttribute(uri,
364: value));
365: }
366:
367: public void setObject(Object object) {
368: this .object = object;
369: }
370:
371: public Object getObject() {
372: return object;
373: }
374:
375: public void clearDynamicAttributes() {
376: dynamicAttributes.clear();
377: }
378:
379: public Map getDynamicAttributesMap() {
380: return dynamicAttributes;
381: }
382: }
383:
384: private class TestJspContext extends JspContext {
385: public Object findAttribute(String name) {
386: return null;
387: }
388:
389: public Object getAttribute(String name, int scope) {
390: return null;
391: }
392:
393: public Object getAttribute(String name) {
394: return null;
395: }
396:
397: public Enumeration getAttributeNamesInScope(int scope) {
398: return null;
399: }
400:
401: public int getAttributesScope(String name) {
402: return 0;
403: }
404:
405: public ExpressionEvaluator getExpressionEvaluator() {
406: return null;
407: }
408:
409: public JspWriter getOut() {
410: return null;
411: }
412:
413: public VariableResolver getVariableResolver() {
414: return null;
415: }
416:
417: public ELContext getELContext() {
418: return null;
419: }
420:
421: public void removeAttribute(String name, int scope) {
422:
423: }
424:
425: public void removeAttribute(String name) {
426:
427: }
428:
429: public void setAttribute(String name, Object value, int scope) {
430:
431: }
432:
433: public void setAttribute(String name, Object value) {
434:
435: }
436: }
437:
438: private class TestDynamicChild implements DynamicChild {
439: private String value;
440:
441: public TestDynamicChild(String value) {
442: this .value = value;
443: }
444:
445: public Object evaluate() {
446: return value;
447: }
448: }
449: }
|