0001: /*
0002: * $Id: $
0003: *
0004: * Licensed to the Apache Software Foundation (ASF) under one
0005: * or more contributor license agreements. See the NOTICE file
0006: * distributed with this work for additional information
0007: * regarding copyright ownership. The ASF licenses this file
0008: * to you under the Apache License, Version 2.0 (the
0009: * "License"); you may not use this file except in compliance
0010: * with the License. You may obtain a copy of the License at
0011: *
0012: * http://www.apache.org/licenses/LICENSE-2.0
0013: *
0014: * Unless required by applicable law or agreed to in writing,
0015: * software distributed under the License is distributed on an
0016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0017: * KIND, either express or implied. See the License for the
0018: * specific language governing permissions and limitations
0019: * under the License.
0020: */
0021: package org.apache.struts.taglib;
0022:
0023: import junit.framework.Test;
0024: import junit.framework.TestSuite;
0025:
0026: import org.apache.commons.logging.Log;
0027: import org.apache.commons.logging.LogFactory;
0028: import org.apache.struts.Globals;
0029: import org.apache.struts.action.ActionMessage;
0030: import org.apache.struts.action.ActionMessages;
0031: import org.apache.struts.config.ActionConfig;
0032: import org.apache.struts.config.ModuleConfig;
0033: import org.apache.struts.config.impl.ModuleConfigImpl;
0034: import org.apache.struts.mock.MockFormBean;
0035: import org.apache.struts.mock.MockHttpServletRequest;
0036: import org.apache.struts.mock.MockHttpServletResponse;
0037: import org.apache.struts.mock.MockPageContext;
0038: import org.apache.struts.mock.MockServletConfig;
0039: import org.apache.struts.mock.MockServletContext;
0040: import org.apache.struts.taglib.html.Constants;
0041:
0042: import javax.servlet.jsp.JspException;
0043: import javax.servlet.jsp.PageContext;
0044:
0045: import java.net.MalformedURLException;
0046:
0047: import java.util.HashMap;
0048: import java.util.Iterator;
0049: import java.util.Locale;
0050: import java.util.Map;
0051:
0052: /**
0053: * Unit tests for the TagUtils.
0054: */
0055: public class TestTagUtils extends TagTestBase {
0056: private static final Log log = LogFactory
0057: .getLog(TestTagUtils.class);
0058:
0059: /**
0060: * Defines the testcase name for JUnit.
0061: *
0062: * @param theName the testcase's name.
0063: */
0064: public TestTagUtils(String theName) {
0065: super (theName);
0066: }
0067:
0068: /**
0069: * Start the tests.
0070: *
0071: * @param theArgs the arguments. Not used
0072: */
0073: public static void main(String[] theArgs) {
0074: junit.awtui.TestRunner.main(new String[] { TestTagUtils.class
0075: .getName() });
0076: }
0077:
0078: /**
0079: * @return a test suite (<code>TestSuite</code>) that includes all methods
0080: * starting with "test"
0081: */
0082: public static Test suite() {
0083: // All methods starting with "test" will be executed in the test suite.
0084: return new TestSuite(TestTagUtils.class);
0085: }
0086:
0087: /**
0088: * Test Operators.
0089: */
0090: public void testFilter() {
0091: assertNull("Null", null);
0092:
0093: // Test Null
0094: assertNull("Filter Test 1", tagutils.filter(null));
0095:
0096: // Test Empty String
0097: assertEquals("Filter Test 2", "", tagutils.filter(""));
0098:
0099: // Test Single Character
0100: assertEquals("Filter Test 3", "a", tagutils.filter("a"));
0101:
0102: // Test Multiple Characters
0103: assertEquals("Filter Test 4", "adhdfhdfhadhf", tagutils
0104: .filter("adhdfhdfhadhf"));
0105:
0106: // Test Each filtered Character
0107: assertEquals("Filter Test 5", "<", tagutils.filter("<"));
0108: assertEquals("Filter Test 6", ">", tagutils.filter(">"));
0109: assertEquals("Filter Test 7", "&", tagutils.filter("&"));
0110: assertEquals("Filter Test 8", """, tagutils.filter("\""));
0111: assertEquals("Filter Test 9", "'", tagutils.filter("'"));
0112:
0113: // Test filtering beginning, middle, end
0114: assertEquals("Filter Test 10", "a<", tagutils.filter("a<"));
0115: assertEquals("Filter Test 11", "<a", tagutils.filter("<a"));
0116: assertEquals("Filter Test 12", "a<a", tagutils.filter("a<a"));
0117:
0118: // Test filtering beginning, middle, end
0119: assertEquals("Filter Test 13", "abc<", tagutils
0120: .filter("abc<"));
0121: assertEquals("Filter Test 14", "<abc", tagutils
0122: .filter("<abc"));
0123: assertEquals("Filter Test 15", "abc<abc", tagutils
0124: .filter("abc<abc"));
0125:
0126: // Test Multiple Characters
0127: assertEquals(
0128: "Filter Test 16",
0129: "<input type="text" value='Me & You'>",
0130: tagutils
0131: .filter("<input type=\"text\" value='Me & You'>"));
0132: }
0133:
0134: // ---------------------------------------------------- computeParameters()
0135: // No parameters and no transaction token
0136: public void testComputeParameters0a() {
0137: Map map = null;
0138:
0139: try {
0140: map = tagutils.computeParameters(pageContext, null, null,
0141: null, null, null, null, null, false);
0142: } catch (JspException e) {
0143: fail("JspException: " + e);
0144: }
0145:
0146: assertNull("Map is null", map);
0147: }
0148:
0149: // No parameters but add transaction token
0150: public void testComputeParameters0b() {
0151: request.getSession().setAttribute(
0152: Globals.TRANSACTION_TOKEN_KEY, "token");
0153:
0154: Map map = null;
0155:
0156: try {
0157: map = tagutils.computeParameters(pageContext, null, null,
0158: null, null, null, null, null, true);
0159: } catch (JspException e) {
0160: fail("JspException: " + e);
0161: }
0162:
0163: assertNotNull("Map is not null", map);
0164: assertEquals("One parameter in the returned map", 1, map.size());
0165: assertTrue("Transaction token parameter present", map
0166: .containsKey(Constants.TOKEN_KEY));
0167: assertEquals("Transaction token parameter value", "token",
0168: (String) map.get(Constants.TOKEN_KEY));
0169: }
0170:
0171: // invalid scope name is requested
0172: public void testComputeParametersInvalidScope() {
0173: Map map = null;
0174:
0175: try {
0176: map = tagutils.computeParameters(pageContext, null, null,
0177: null, "session", "i-do-not-exist", null, null,
0178: false);
0179:
0180: fail("JspException not thrown");
0181: } catch (JspException e) {
0182: assertNull("map is null", map);
0183: }
0184: }
0185:
0186: // specified bean is not found
0187: public void testComputeParametersBeanNotFound() {
0188: Map map = null;
0189:
0190: try {
0191: map = tagutils.computeParameters(pageContext, null, null,
0192: null, null, "i-do-not-exist", null, null, false);
0193:
0194: fail("JspException not thrown");
0195: } catch (JspException e) {
0196: assertNull("map is null", map);
0197: }
0198: }
0199:
0200: // accessing this property causes an exception
0201: public void testComputeParametersPropertyThrowsException() {
0202: request.getSession().setAttribute("SomeBean",
0203: new MockFormBean(true));
0204:
0205: Map map = null;
0206:
0207: try {
0208: map = tagutils.computeParameters(pageContext, null, null,
0209: null, null, "SomeBean", "justThrowAnException",
0210: null, false);
0211: fail("JspException not thrown");
0212: } catch (JspException e) {
0213: assertNull("map is null", map);
0214: }
0215: }
0216:
0217: public void testComputeParametersParamIdParamPropThrowException() {
0218: request.getSession().setAttribute("SomeBean",
0219: new MockFormBean(true));
0220:
0221: Map map = null;
0222:
0223: try {
0224: map = tagutils.computeParameters(pageContext, "paramId",
0225: "SomeBean", "justThrowAnException", null, null,
0226: null, null, false);
0227:
0228: fail("JspException not thrown");
0229: } catch (JspException e) {
0230: assertNull("map is null", map);
0231: }
0232: }
0233:
0234: public void testComputeParametersParamValueToString() {
0235: request.getSession().setAttribute("SomeBean",
0236: new MockFormBean(false, false, new Double(1)));
0237:
0238: Map map = null;
0239:
0240: try {
0241: map = tagutils.computeParameters(pageContext, "paramId",
0242: "SomeBean", "doubleValue", null, null, null, null,
0243: false);
0244:
0245: assertNotNull("map is null", map);
0246:
0247: String val = (String) map.get("paramId");
0248:
0249: assertTrue("paramId should be 1.0", "1.0".equals(val));
0250: } catch (JspException e) {
0251: fail("JspException not thrown");
0252: }
0253: }
0254:
0255: public void skiptestComputeParametersParamIdAsStringArray() {
0256: Map params = new HashMap();
0257:
0258: // String[] vals = new String[]{"test0, test1"};
0259: params.put("fooParamId", "fooParamValue");
0260:
0261: request.getSession().setAttribute("SomeBean", params);
0262:
0263: Map map = null;
0264:
0265: try {
0266: map = tagutils.computeParameters(pageContext, "fooParamId",
0267: "SomeBean", null, null, "SomeBean", null, null,
0268: false);
0269:
0270: // map = tagutils.computeParameters(
0271: // page, "paramId", "SomeBean", "stringArray", null,
0272: // null, null, null, false);
0273: assertNotNull("map is null", map);
0274:
0275: String val = (String) map.get("key0");
0276:
0277: assertTrue("paramId should be \"test\"", "1.0".equals(val));
0278: } catch (JspException e) {
0279: fail("JspException not thrown");
0280: }
0281: }
0282:
0283: // Single parameter -- name
0284: public void testComputeParameters1a() {
0285: request.getSession().setAttribute("attr", "bar");
0286:
0287: Map map = null;
0288:
0289: try {
0290: map = tagutils.computeParameters(pageContext, "foo",
0291: "attr", null, null, null, null, null, false);
0292: } catch (JspException e) {
0293: fail("JspException: " + e);
0294: }
0295:
0296: assertNotNull("Map is not null", map);
0297: assertEquals("One parameter in the returned map", 1, map.size());
0298: assertTrue("Parameter present", map.containsKey("foo"));
0299: assertEquals("Parameter value", "bar", (String) map.get("foo"));
0300: }
0301:
0302: // Single parameter -- scope + name
0303: public void testComputeParameters1b() {
0304: request.setAttribute("attr", "bar");
0305:
0306: Map map = null;
0307:
0308: try {
0309: map = tagutils.computeParameters(pageContext, "foo",
0310: "attr", null, "request", null, null, null, false);
0311: } catch (JspException e) {
0312: fail("JspException: " + e);
0313: }
0314:
0315: assertNotNull("Map is not null", map);
0316: assertEquals("One parameter in the returned map", 1, map.size());
0317: assertTrue("Parameter present", map.containsKey("foo"));
0318: assertEquals("Parameter value", "bar", (String) map.get("foo"));
0319: }
0320:
0321: // Single parameter -- scope + name + property
0322: public void testComputeParameters1c() {
0323: request.setAttribute("attr", new MockFormBean("bar"));
0324:
0325: Map map = null;
0326:
0327: try {
0328: map = tagutils.computeParameters(pageContext, "foo",
0329: "attr", "stringProperty", "request", null, null,
0330: null, false);
0331: } catch (JspException e) {
0332: fail("JspException: " + e);
0333: }
0334:
0335: assertNotNull("Map is not null", map);
0336: assertEquals("One parameter in the returned map", 1, map.size());
0337: assertTrue("Parameter present", map.containsKey("foo"));
0338: assertEquals("Parameter value", "bar", (String) map.get("foo"));
0339: }
0340:
0341: // Provided map -- name
0342: public void testComputeParameters2a() {
0343: Map map = new HashMap();
0344:
0345: map.put("foo1", "bar1");
0346: map.put("foo2", "bar2");
0347: request.getSession().setAttribute("attr", map);
0348:
0349: try {
0350: map = tagutils.computeParameters(pageContext, null, null,
0351: null, null, "attr", null, null, false);
0352: } catch (JspException e) {
0353: fail("JspException: " + e);
0354: }
0355:
0356: assertNotNull("Map is not null", map);
0357: assertEquals("Two parameter in the returned map", 2, map.size());
0358: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0359: assertEquals("Parameter foo1 value", "bar1", (String) map
0360: .get("foo1"));
0361: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0362: assertEquals("Parameter foo2 value", "bar2", (String) map
0363: .get("foo2"));
0364: }
0365:
0366: // Provided map -- scope + name
0367: public void testComputeParameters2b() {
0368: Map map = new HashMap();
0369:
0370: map.put("foo1", "bar1");
0371: map.put("foo2", "bar2");
0372: request.setAttribute("attr", map);
0373:
0374: try {
0375: map = tagutils.computeParameters(pageContext, null, null,
0376: null, null, "attr", null, "request", false);
0377: } catch (JspException e) {
0378: fail("JspException: " + e);
0379: }
0380:
0381: assertNotNull("Map is not null", map);
0382: assertEquals("Two parameter in the returned map", 2, map.size());
0383: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0384: assertEquals("Parameter foo1 value", "bar1", (String) map
0385: .get("foo1"));
0386: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0387: assertEquals("Parameter foo2 value", "bar2", (String) map
0388: .get("foo2"));
0389: }
0390:
0391: // Provided map -- scope + name + property
0392: public void testComputeParameters2c() {
0393: request.setAttribute("attr", new MockFormBean());
0394:
0395: Map map = null;
0396:
0397: try {
0398: map = tagutils
0399: .computeParameters(pageContext, null, null, null,
0400: null, "attr", "mapProperty", "request",
0401: false);
0402: } catch (JspException e) {
0403: fail("JspException: " + e);
0404: }
0405:
0406: assertNotNull("Map is not null", map);
0407: assertEquals("Two parameter in the returned map", 2, map.size());
0408: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0409: assertEquals("Parameter foo1 value", "bar1", (String) map
0410: .get("foo1"));
0411: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0412: assertEquals("Parameter foo2 value", "bar2", (String) map
0413: .get("foo2"));
0414: }
0415:
0416: // Provided map -- name with one key and two values
0417: public void testComputeParameters2d() {
0418: Map map = new HashMap();
0419:
0420: map.put("foo", new String[] { "bar1", "bar2" });
0421: request.getSession().setAttribute("attr", map);
0422:
0423: try {
0424: map = tagutils.computeParameters(pageContext, null, null,
0425: null, null, "attr", null, null, false);
0426: } catch (JspException e) {
0427: fail("JspException: " + e);
0428: }
0429:
0430: assertNotNull("Map is not null", map);
0431: assertEquals("One parameter in the returned map", 1, map.size());
0432: assertTrue("Parameter foo present", map.containsKey("foo"));
0433: assertTrue("Parameter foo value type",
0434: map.get("foo") instanceof String[]);
0435:
0436: String[] values = (String[]) map.get("foo");
0437:
0438: assertEquals("Values count", 2, values.length);
0439: }
0440:
0441: // Kitchen sink combination of parameters with a merge
0442: public void testComputeParameters3a() {
0443: request.setAttribute("attr", new MockFormBean("bar3"));
0444: request.getSession().setAttribute(
0445: Globals.TRANSACTION_TOKEN_KEY, "token");
0446:
0447: Map map = null;
0448:
0449: try {
0450: map = tagutils.computeParameters(pageContext, "foo1",
0451: "attr", "stringProperty", "request", "attr",
0452: "mapProperty", "request", true);
0453: } catch (JspException e) {
0454: fail("JspException: " + e);
0455: }
0456:
0457: assertNotNull("Map is not null", map);
0458: assertEquals("Three parameter in the returned map", 3, map
0459: .size());
0460:
0461: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0462: assertTrue("Parameter foo1 value type",
0463: map.get("foo1") instanceof String[]);
0464:
0465: String[] values = (String[]) map.get("foo1");
0466:
0467: assertEquals("Values count", 2, values.length);
0468:
0469: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0470: assertEquals("Parameter foo2 value", "bar2", (String) map
0471: .get("foo2"));
0472:
0473: assertTrue("Transaction token parameter present", map
0474: .containsKey(Constants.TOKEN_KEY));
0475: assertEquals("Transaction token parameter value", "token",
0476: (String) map.get(Constants.TOKEN_KEY));
0477: }
0478:
0479: // Kitchen sink combination of parameters with a merge
0480: // with array values in map
0481: public void testComputeParameters3aa() {
0482: request.setAttribute("attr", new MockFormBean("bar3"));
0483: request.getSession().setAttribute(
0484: Globals.TRANSACTION_TOKEN_KEY, "token");
0485:
0486: Map map = null;
0487:
0488: try {
0489: map = tagutils.computeParameters(pageContext, "foo1",
0490: "attr", "stringProperty", "request", "attr",
0491: "mapPropertyArrayValues", "request", true);
0492: } catch (JspException e) {
0493: fail("JspException: " + e);
0494: }
0495:
0496: assertNotNull("Map is not null", map);
0497: assertEquals("Three parameter in the returned map", 3, map
0498: .size());
0499:
0500: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0501: assertTrue("Parameter foo1 value type",
0502: map.get("foo1") instanceof String[]);
0503:
0504: String[] values = (String[]) map.get("foo1");
0505:
0506: assertEquals("Values count", 3, values.length);
0507:
0508: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0509:
0510: String[] arrayValues = (String[]) map.get("foo2");
0511: String val = arrayValues[0];
0512:
0513: assertEquals("Parameter foo2 value", "bar2", val);
0514:
0515: assertTrue("Transaction token parameter present", map
0516: .containsKey(Constants.TOKEN_KEY));
0517: assertEquals("Transaction token parameter value", "token",
0518: (String) map.get(Constants.TOKEN_KEY));
0519: }
0520:
0521: // Kitchen sink combination of parameters with a merge
0522: public void testComputeParameters3b() {
0523: request.setAttribute("attr", new MockFormBean("bar3"));
0524: request.getSession().setAttribute(
0525: Globals.TRANSACTION_TOKEN_KEY, "token");
0526:
0527: Map map = null;
0528:
0529: try {
0530: map = tagutils.computeParameters(pageContext, "foo1",
0531: "attr", "stringProperty", "request", "attr",
0532: "mapProperty", "request", true);
0533: } catch (JspException e) {
0534: fail("JspException: " + e);
0535: }
0536:
0537: assertNotNull("Map is not null", map);
0538: assertEquals("Three parameter in the returned map", 3, map
0539: .size());
0540:
0541: assertTrue("Parameter foo1 present", map.containsKey("foo1"));
0542: assertTrue("Parameter foo1 value type",
0543: map.get("foo1") instanceof String[]);
0544:
0545: String[] values = (String[]) map.get("foo1");
0546:
0547: assertEquals("Values count", 2, values.length);
0548:
0549: assertTrue("Parameter foo2 present", map.containsKey("foo2"));
0550: assertEquals("Parameter foo2 value", "bar2", (String) map
0551: .get("foo2"));
0552:
0553: assertTrue("Transaction token parameter present", map
0554: .containsKey(Constants.TOKEN_KEY));
0555: assertEquals("Transaction token parameter value", "token",
0556: (String) map.get(Constants.TOKEN_KEY));
0557: }
0558:
0559: // ----------------------------------------------------------- computeURL()
0560: // Default module -- Forward only
0561: public void testComputeURL1a() {
0562: request.setPathElements("/myapp", "/action.do", null, null);
0563:
0564: String url = null;
0565:
0566: try {
0567: url = tagutils.computeURL(pageContext, "foo", null, null,
0568: null, null, null, null, false);
0569: } catch (MalformedURLException e) {
0570: fail("MalformedURLException: " + e);
0571: }
0572:
0573: assertNotNull("url present", url);
0574: assertEquals("url value", "/myapp/bar.jsp", url);
0575: }
0576:
0577: // Default module -- Href only
0578: public void testComputeURL1b() {
0579: request.setPathElements("/myapp", "/action.do", null, null);
0580:
0581: String url = null;
0582:
0583: try {
0584: url = tagutils.computeURL(pageContext, null,
0585: "http://foo.com/bar", null, null, null, null, null,
0586: false);
0587: } catch (MalformedURLException e) {
0588: fail("MalformedURLException: " + e);
0589: }
0590:
0591: assertNotNull("url present", url);
0592: assertEquals("url value", "http://foo.com/bar", url);
0593: }
0594:
0595: // Default module -- Page only
0596: public void testComputeURL1c() {
0597: request.setPathElements("/myapp", "/action.do", null, null);
0598:
0599: String url = null;
0600:
0601: try {
0602: url = tagutils.computeURL(pageContext, null, null, "/bar",
0603: null, null, null, null, false);
0604: } catch (MalformedURLException e) {
0605: fail("MalformedURLException: " + e);
0606: }
0607:
0608: assertNotNull("url present", url);
0609: assertEquals("url value", "/myapp/bar", url);
0610: }
0611:
0612: // Default module -- Forward with pattern
0613: public void testComputeURL1d() {
0614: moduleConfig.getControllerConfig().setForwardPattern(
0615: "$C/WEB-INF/pages$M$P");
0616: request.setPathElements("/myapp", "/action.do", null, null);
0617:
0618: String url = null;
0619:
0620: try {
0621: url = tagutils.computeURL(pageContext, "foo", null, null,
0622: null, null, null, null, false);
0623: } catch (MalformedURLException e) {
0624: fail("MalformedURLException: " + e);
0625: }
0626:
0627: assertNotNull("url present", url);
0628: assertEquals("url value", "/myapp/WEB-INF/pages/bar.jsp", url);
0629: }
0630:
0631: // Default module -- Page with pattern
0632: public void testComputeURL1e() {
0633: moduleConfig.getControllerConfig().setPagePattern(
0634: "$C/WEB-INF/pages$M$P");
0635: request.setPathElements("/myapp", "/action.do", null, null);
0636:
0637: String url = null;
0638:
0639: try {
0640: url = tagutils.computeURL(pageContext, null, null, "/bar",
0641: null, null, null, null, false);
0642: } catch (MalformedURLException e) {
0643: fail("MalformedURLException: " + e);
0644: }
0645:
0646: assertNotNull("url present", url);
0647: assertEquals("url value", "/myapp/WEB-INF/pages/bar", url);
0648: }
0649:
0650: // Default module -- Forward with relative path (non-context-relative)
0651: public void testComputeURL1f() {
0652: request.setPathElements("/myapp", "/action.do", null, null);
0653:
0654: String url = null;
0655:
0656: try {
0657: url = tagutils.computeURL(pageContext, "relative1", null,
0658: null, null, null, null, null, false);
0659: } catch (MalformedURLException e) {
0660: fail("MalformedURLException: " + e);
0661: }
0662:
0663: assertNotNull("url present", url);
0664: assertEquals("url value",
0665: // "/myapp/relative.jsp",
0666: "relative.jsp", url);
0667: }
0668:
0669: // Default module -- Forward with relative path (context-relative)
0670: public void testComputeURL1g() {
0671: request.setPathElements("/myapp", "/action.do", null, null);
0672:
0673: String url = null;
0674:
0675: try {
0676: url = tagutils.computeURL(pageContext, "relative2", null,
0677: null, null, null, null, null, false);
0678: } catch (MalformedURLException e) {
0679: fail("MalformedURLException: " + e);
0680: }
0681:
0682: assertNotNull("url present", url);
0683: assertEquals("url value",
0684: // "/myapp/relative.jsp",
0685: "relative.jsp", url);
0686: }
0687:
0688: // Default module -- Forward with external path
0689: public void testComputeURL1h() {
0690: request.setPathElements("/myapp", "/action.do", null, null);
0691:
0692: String url = null;
0693:
0694: try {
0695: url = tagutils.computeURL(pageContext, "external", null,
0696: null, null, null, null, null, false);
0697: } catch (MalformedURLException e) {
0698: fail("MalformedURLException: " + e);
0699: }
0700:
0701: assertNotNull("url present", url);
0702: assertEquals("url value", "http://struts.apache.org/", url);
0703: }
0704:
0705: // Second module -- Forward only
0706: public void testComputeURL2a() {
0707: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0708: request.setPathElements("/myapp", "/2/action.do", null, null);
0709:
0710: String url = null;
0711:
0712: try {
0713: url = tagutils.computeURL(pageContext, "foo", null, null,
0714: null, null, null, null, false);
0715: } catch (MalformedURLException e) {
0716: fail("MalformedURLException: " + e);
0717: }
0718:
0719: assertNotNull("url present", url);
0720: assertEquals("url value", "/myapp/2/baz.jsp", url);
0721: }
0722:
0723: // Second module -- Href only
0724: public void testComputeURL2b() {
0725: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0726: request.setPathElements("/myapp", "/2/action.do", null, null);
0727:
0728: String url = null;
0729:
0730: try {
0731: url = tagutils.computeURL(pageContext, null,
0732: "http://foo.com/bar", null, null, null, null, null,
0733: false);
0734: } catch (MalformedURLException e) {
0735: fail("MalformedURLException: " + e);
0736: }
0737:
0738: assertNotNull("url present", url);
0739: assertEquals("url value", "http://foo.com/bar", url);
0740: }
0741:
0742: // Second module -- Page only
0743: public void testComputeURL2c() {
0744: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0745: request.setPathElements("/myapp", "/2/action.do", null, null);
0746:
0747: String url = null;
0748:
0749: try {
0750: url = tagutils.computeURL(pageContext, null, null, "/bar",
0751: null, null, null, null, false);
0752: } catch (MalformedURLException e) {
0753: fail("MalformedURLException: " + e);
0754: }
0755:
0756: assertNotNull("url present", url);
0757: assertEquals("url value", "/myapp/2/bar", url);
0758: }
0759:
0760: // Default module -- Forward with pattern
0761: public void testComputeURL2d() {
0762: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0763: moduleConfig2.getControllerConfig().setForwardPattern(
0764: "$C/WEB-INF/pages$M$P");
0765: request.setPathElements("/myapp", "/2/action.do", null, null);
0766:
0767: String url = null;
0768:
0769: try {
0770: url = tagutils.computeURL(pageContext, "foo", null, null,
0771: null, null, null, null, false);
0772: } catch (MalformedURLException e) {
0773: fail("MalformedURLException: " + e);
0774: }
0775:
0776: assertNotNull("url present", url);
0777: assertEquals("url value", "/myapp/WEB-INF/pages/2/baz.jsp", url);
0778: }
0779:
0780: // Second module -- Page with pattern
0781: public void testComputeURL2e() {
0782: moduleConfig2.getControllerConfig().setPagePattern(
0783: "$C/WEB-INF/pages$M$P");
0784: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0785: request.setPathElements("/myapp", "/2/action.do", null, null);
0786:
0787: String url = null;
0788:
0789: try {
0790: url = tagutils.computeURL(pageContext, null, null, "/bar",
0791: null, null, null, null, false);
0792: } catch (MalformedURLException e) {
0793: fail("MalformedURLException: " + e);
0794: }
0795:
0796: assertNotNull("url present", url);
0797: assertEquals("url value", "/myapp/WEB-INF/pages/2/bar", url);
0798: }
0799:
0800: // Second module -- Forward with relative path (non-context-relative)
0801: public void testComputeURL2f() {
0802: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0803: request.setPathElements("/myapp", "/2/action.do", null, null);
0804:
0805: String url = null;
0806:
0807: try {
0808: url = tagutils.computeURL(pageContext, "relative1", null,
0809: null, null, null, null, null, false);
0810: } catch (MalformedURLException e) {
0811: fail("MalformedURLException: " + e);
0812: }
0813:
0814: assertNotNull("url present", url);
0815: assertEquals("url value",
0816: // "/myapp/2/relative.jsp",
0817: "relative.jsp", url);
0818: }
0819:
0820: // Second module -- Forward with relative path (context-relative)
0821: public void testComputeURL2g() {
0822: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0823: request.setPathElements("/myapp", "/2/action.do", null, null);
0824:
0825: String url = null;
0826:
0827: try {
0828: url = tagutils.computeURL(pageContext, "relative2", null,
0829: null, null, null, null, null, false);
0830: } catch (MalformedURLException e) {
0831: fail("MalformedURLException: " + e);
0832: }
0833:
0834: assertNotNull("url present", url);
0835: assertEquals("url value",
0836: // "/myapp/relative.jsp",
0837: "relative.jsp", url);
0838: }
0839:
0840: // Second module -- Forward with external path
0841: public void testComputeURL2h() {
0842: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
0843: request.setPathElements("/myapp", "/2/action.do", null, null);
0844:
0845: String url = null;
0846:
0847: try {
0848: url = tagutils.computeURL(pageContext, "external", null,
0849: null, null, null, null, null, false);
0850: } catch (MalformedURLException e) {
0851: fail("MalformedURLException: " + e);
0852: }
0853:
0854: assertNotNull("url present", url);
0855: assertEquals("url value", "http://struts.apache.org/", url);
0856: }
0857:
0858: // Add parameters only -- forward URL
0859: public void testComputeURL3a() {
0860: request.setPathElements("/myapp", "/action.do", null, null);
0861:
0862: Map map = new HashMap();
0863:
0864: map.put("foo1", "bar1");
0865: map.put("foo2", "bar2");
0866:
0867: String url = null;
0868:
0869: try {
0870: url = tagutils.computeURL(pageContext, null, null, "/bar",
0871: null, null, map, null, false);
0872: } catch (MalformedURLException e) {
0873: fail("MalformedURLException: " + e);
0874: }
0875:
0876: assertNotNull("url present", url);
0877: assertTrue("url value", url
0878: .equals("/myapp/bar?foo1=bar1&foo2=bar2")
0879: || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
0880: }
0881:
0882: // Add anchor only -- forward URL
0883: public void testComputeURL3b() {
0884: request.setPathElements("/myapp", "/action.do", null, null);
0885:
0886: String url = null;
0887:
0888: try {
0889: url = tagutils.computeURL(pageContext, null, null, "/bar",
0890: null, null, null, "anchor", false);
0891: } catch (MalformedURLException e) {
0892: fail("MalformedURLException: " + e);
0893: }
0894:
0895: assertNotNull("url present", url);
0896: assertEquals("url value", "/myapp/bar#anchor", url);
0897: }
0898:
0899: // Add parameters + anchor -- forward URL
0900: public void testComputeURL3c() {
0901: request.setPathElements("/myapp", "/action.do", null, null);
0902:
0903: Map map = new HashMap();
0904:
0905: map.put("foo1", "bar1");
0906: map.put("foo2", "bar2");
0907:
0908: String url = null;
0909:
0910: try {
0911: url = tagutils.computeURL(pageContext, null, null, "/bar",
0912: null, null, map, "anchor", false);
0913: } catch (MalformedURLException e) {
0914: fail("MalformedURLException: " + e);
0915: }
0916:
0917: assertNotNull("url present", url);
0918: assertTrue(
0919: "url value",
0920: url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor")
0921: || url
0922: .equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor"));
0923: }
0924:
0925: // Add parameters only -- redirect URL
0926: public void testComputeURL3d() {
0927: request.setPathElements("/myapp", "/action.do", null, null);
0928:
0929: Map map = new HashMap();
0930:
0931: map.put("foo1", "bar1");
0932: map.put("foo2", "bar2");
0933:
0934: String url = null;
0935:
0936: try {
0937: url = tagutils.computeURL(pageContext, null, null, "/bar",
0938: null, null, map, null, true);
0939: } catch (MalformedURLException e) {
0940: fail("MalformedURLException: " + e);
0941: }
0942:
0943: assertNotNull("url present", url);
0944: assertTrue("url value", url
0945: .equals("/myapp/bar?foo1=bar1&foo2=bar2")
0946: || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
0947: }
0948:
0949: // Add anchor only -- redirect URL
0950: public void testComputeURL3e() {
0951: request.setPathElements("/myapp", "/action.do", null, null);
0952:
0953: String url = null;
0954:
0955: try {
0956: url = tagutils.computeURL(pageContext, null, null, "/bar",
0957: null, null, null, "anchor", true);
0958: } catch (MalformedURLException e) {
0959: fail("MalformedURLException: " + e);
0960: }
0961:
0962: assertNotNull("url present", url);
0963: assertEquals("url value", "/myapp/bar#anchor", url);
0964: }
0965:
0966: // Add parameters + anchor -- redirect URL
0967: public void testComputeURL3f() {
0968: request.setPathElements("/myapp", "/action.do", null, null);
0969:
0970: Map map = new HashMap();
0971:
0972: map.put("foo1", "bar1");
0973: map.put("foo2", "bar2");
0974:
0975: String url = null;
0976:
0977: try {
0978: url = tagutils.computeURL(pageContext, null, null, "/bar",
0979: null, null, map, "anchor", false);
0980: } catch (MalformedURLException e) {
0981: fail("MalformedURLException: " + e);
0982: }
0983:
0984: assertNotNull("url present", url);
0985: assertTrue(
0986: "url value",
0987: url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor")
0988: || url
0989: .equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor"));
0990: }
0991:
0992: // Add parameters only -- forward URL -- do not encode seperator
0993: public void testComputeURL3g() {
0994: request.setPathElements("/myapp", "/action.do", null, null);
0995:
0996: Map map = new HashMap();
0997:
0998: map.put("foo1", "bar1");
0999: map.put("foo2", "bar2");
1000:
1001: String url = null;
1002:
1003: try {
1004: url = tagutils.computeURLWithCharEncoding(pageContext,
1005: null, null, "/bar", null, null, map, null, false,
1006: false, false);
1007: } catch (MalformedURLException e) {
1008: fail("MalformedURLException: " + e);
1009: }
1010:
1011: assertNotNull("url present", url);
1012: assertTrue("url value", url
1013: .equals("/myapp/bar?foo1=bar1&foo2=bar2")
1014: || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
1015: }
1016:
1017: // Add parameters only
1018: // -- forward URL
1019: // -- do not encode seperator
1020: // -- send param with null value
1021: public void testComputeURL3h() {
1022: request.setPathElements("/myapp", "/action.do", null, null);
1023:
1024: Map map = new HashMap();
1025:
1026: map.put("foo1", null);
1027:
1028: String url = null;
1029:
1030: try {
1031: url = tagutils.computeURLWithCharEncoding(pageContext,
1032: null, null, "/bar", null, null, map, null, false,
1033: false, false);
1034: } catch (MalformedURLException e) {
1035: fail("MalformedURLException: " + e);
1036: }
1037:
1038: assertNotNull("url present", url);
1039: assertTrue("url value", url.equals("/myapp/bar?foo1="));
1040: }
1041:
1042: // Add parameters only
1043: // -- forward URL
1044: // -- do not encode seperator
1045: // -- send param with null value
1046: // -- add ? to page
1047: public void testComputeURL3i() {
1048: request.setPathElements("/myapp", "/action.do", null, null);
1049:
1050: Map map = new HashMap();
1051:
1052: map.put("foo1", null);
1053:
1054: String url = null;
1055:
1056: try {
1057: url = tagutils.computeURLWithCharEncoding(pageContext,
1058: null, null, "/bar?", null, null, map, null, false,
1059: false, false);
1060: } catch (MalformedURLException e) {
1061: fail("MalformedURLException: " + e);
1062: }
1063:
1064: assertNotNull("url present", url);
1065: assertTrue("url value", url.equals("/myapp/bar?&foo1="));
1066: }
1067:
1068: // Add parameters only
1069: // -- forward URL
1070: // -- do not encode seperator
1071: // -- send param with null value
1072: // -- add ? and param to page
1073: public void testComputeURL3j() {
1074: request.setPathElements("/myapp", "/action.do", null, null);
1075:
1076: Map map = new HashMap();
1077:
1078: map.put("foo1", null);
1079: map.put("foo2", "bar2");
1080:
1081: String url = null;
1082:
1083: try {
1084: url = tagutils.computeURLWithCharEncoding(pageContext,
1085: null, null, "/bar?a=b", null, null, map, null,
1086: false, false, false);
1087: } catch (MalformedURLException e) {
1088: fail("MalformedURLException: " + e);
1089: }
1090:
1091: assertNotNull("url present", url);
1092: assertTrue("url value", url
1093: .equals("/myapp/bar?a=b&foo1=&foo2=bar2")
1094: || url.equals("/myapp/bar?a=b&foo2=bar2&foo1="));
1095: }
1096:
1097: // -- Add Parameters
1098: // -- Parameter as String Array
1099: public void testComputeURL3k() {
1100: request.setPathElements("/myapp", "/action.do", null, null);
1101:
1102: Map map = new HashMap();
1103:
1104: map.put("foo1", new String[] { "bar1", "baz1" });
1105:
1106: String url = null;
1107:
1108: try {
1109: url = tagutils.computeURL(pageContext, null, null, "/bar",
1110: null, null, map, null, false);
1111: } catch (MalformedURLException e) {
1112: fail("MalformedURLException: " + e);
1113: }
1114:
1115: assertNotNull("url present", url);
1116: assertTrue("url value", url
1117: .equals("/myapp/bar?foo1=bar1&foo1=baz1")
1118: || url.equals("/myapp/bar?foo1=baz1&foo1=bar1"));
1119: }
1120:
1121: // -- Add Parameters
1122: // -- Parameter as non String or String Array
1123: public void testComputeURL3l() {
1124: request.setPathElements("/myapp", "/action.do", null, null);
1125:
1126: Map map = new HashMap();
1127:
1128: map.put("foo1", new Double(0));
1129:
1130: String url = null;
1131:
1132: try {
1133: url = tagutils.computeURL(pageContext, null, null, "/bar",
1134: null, null, map, null, false);
1135: } catch (MalformedURLException e) {
1136: fail("MalformedURLException: " + e);
1137: }
1138:
1139: assertNotNull("url present", url);
1140: assertTrue("url value", url.equals("/myapp/bar?foo1=0.0"));
1141: }
1142:
1143: // -- Add Parameters
1144: // -- Parameter as non String or String Array
1145: // -- with ? on path
1146: public void testComputeURL3m() {
1147: request.setPathElements("/myapp", "/action.do", null, null);
1148:
1149: Map map = new HashMap();
1150:
1151: map.put("foo1", new Double(0));
1152:
1153: String url = null;
1154:
1155: try {
1156: url = tagutils.computeURL(pageContext, null, null, "/bar?",
1157: null, null, map, null, false);
1158: } catch (MalformedURLException e) {
1159: fail("MalformedURLException: " + e);
1160: }
1161:
1162: assertNotNull("url present", url);
1163: assertTrue("url value", url.equals("/myapp/bar?&foo1=0.0"));
1164: }
1165:
1166: public void testComputeURLCharacterEncoding() {
1167: request.setPathElements("/myapp", "/action.do", null, null);
1168:
1169: String url = null;
1170:
1171: try {
1172: url = tagutils.computeURLWithCharEncoding(pageContext,
1173: "foo", null, null, null, null, null, null, false,
1174: true, true);
1175: fail("Exception not thrown");
1176: } catch (MalformedURLException e) {
1177: fail("MalformedURLException: " + e);
1178: } catch (UnsupportedOperationException e) {
1179: assertNull("url should be null", url);
1180: }
1181: }
1182:
1183: public void testComputeURLCharacterEncodingMultipleSpecifier() {
1184: verifyBadSetOfSpecifiers("foo", "foo", null, null);
1185: verifyBadSetOfSpecifiers("foo", null, "foo", null);
1186: verifyBadSetOfSpecifiers("foo", null, null, "foo");
1187:
1188: verifyBadSetOfSpecifiers(null, "foo", "foo", null);
1189: verifyBadSetOfSpecifiers(null, "foo", null, "foo");
1190:
1191: verifyBadSetOfSpecifiers(null, null, "foo", "foo");
1192: }
1193:
1194: public void testComputeURLCharacterEncodingAction() {
1195: ActionConfig actionConfig = new ActionConfig();
1196:
1197: actionConfig.setName("baz");
1198: actionConfig.setPath("/baz");
1199:
1200: moduleConfig.addActionConfig(actionConfig);
1201:
1202: request.setPathElements("/myapp", "/foo.do", null, null);
1203:
1204: Map map = new HashMap();
1205:
1206: map.put("foo1", "bar1");
1207: map.put("foo2", "bar2");
1208:
1209: String url = null;
1210:
1211: try {
1212: url = tagutils.computeURL(pageContext, null, null, null,
1213: "baz", null, map, "anchor", false);
1214: } catch (MalformedURLException e) {
1215: fail("MalformedURLException: " + e);
1216: }
1217:
1218: assertNotNull("url present", url);
1219: assertTrue(
1220: "url value",
1221: url.equals("/myapp/baz?foo1=bar1&foo2=bar2#anchor")
1222: || url
1223: .equals("/myapp/baz?foo2=bar2&foo1=bar1#anchor"));
1224: }
1225:
1226: // -------------------------------------------------------------- pageURL()
1227: // Default module (default pagePattern)
1228: public void testPageURL1() {
1229: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1230: request.setPathElements("/myapp", "/action.do", null, null);
1231:
1232: String page = null;
1233: String result = null;
1234:
1235: // Straight substitution
1236: page = "/mypages/index.jsp";
1237: result = tagutils.pageURL(request, page, moduleConfig);
1238: assertNotNull("straight sub found", result);
1239: assertEquals("straight sub value", "/mypages/index.jsp", result);
1240: }
1241:
1242: // Second module (default pagePattern)
1243: public void testPageURL2() {
1244: request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
1245: request.setPathElements("/myapp", "/2/action.do", null, null);
1246:
1247: String page = null;
1248: String result = null;
1249:
1250: // Straight substitution
1251: page = "/mypages/index.jsp";
1252: result = tagutils.pageURL(request, page, moduleConfig2);
1253: assertNotNull("straight sub found", result);
1254: assertEquals("straight sub value", "/2/mypages/index.jsp",
1255: result);
1256: }
1257:
1258: // Third module (custom pagePattern)
1259: // TODO finish me
1260: public void testPageURL3a() {
1261: request.setAttribute(Globals.MODULE_KEY, moduleConfig3);
1262: request.setPathElements("/myapp", "/3/action.do", null, null);
1263:
1264: // String page = null;
1265: // String result = null;
1266: }
1267:
1268: // Third module (custom pagePattern)
1269: public void testPageURL3b() {
1270: request.setAttribute(Globals.MODULE_KEY, moduleConfig3);
1271: request.setPathElements("/myapp", "/3/action.do", null, null);
1272:
1273: String page = null;
1274: String result = null;
1275:
1276: // Straight substitution
1277: page = "/mypages/index.jsp";
1278: result = tagutils.pageURL(request, page, moduleConfig3);
1279: assertNotNull("straight sub found", result);
1280: assertEquals("straight sub value", "/3/mypages/index.jsp",
1281: result);
1282: }
1283:
1284: /**
1285: * Helper method that verifies the supplied specifiers.
1286: *
1287: * @param forward The forward specified
1288: * @param href The href specified
1289: * @param pageString The pageString specified
1290: * @param action The action specified
1291: */
1292: private void verifyBadSetOfSpecifiers(String forward, String href,
1293: String pageString, String action) {
1294: String url = null;
1295:
1296: try {
1297: url = tagutils.computeURLWithCharEncoding(pageContext,
1298: forward, href, pageString, action, null, null,
1299: null, false, true, false);
1300: } catch (MalformedURLException e) {
1301: assertNull("url should be null", url);
1302: } catch (UnsupportedOperationException e) {
1303: fail("MalformedURLException not thrown");
1304: }
1305: }
1306:
1307: // -------------------------------------------------------------- encodeURL()
1308: // Default module (default pagePattern)
1309: public void testencodeURL1() {
1310: String encodedURL = null;
1311:
1312: encodedURL = tagutils.encodeURL("foo-bar.baz");
1313: assertEquals("encode url", "foo-bar.baz", encodedURL);
1314: }
1315:
1316: // ------------------------------------------ getActionErrors()
1317: // ActionErrors
1318: public void testGetActionErrors1a() {
1319: ActionMessages actionErrors = new ActionMessages();
1320:
1321: actionErrors.add("prop", new ActionMessage("key.key"));
1322: request.setAttribute("errors", actionErrors);
1323:
1324: try {
1325: ActionMessages errors = tagutils.getActionMessages(
1326: pageContext, "errors");
1327:
1328: assertNotNull("errors should not be null", errors);
1329: assertNotNull("errors prop should not be null", errors
1330: .get("prop"));
1331:
1332: String val = null;
1333: int i = 0;
1334:
1335: for (Iterator iter = errors.get("prop"); iter.hasNext();) {
1336: ActionMessage error = (ActionMessage) iter.next();
1337:
1338: val = error.getKey();
1339: i++;
1340: }
1341:
1342: assertEquals("only 1 error", i, 1);
1343: assertEquals("errors prop should match", val, "key.key");
1344: } catch (JspException e) {
1345: fail(e.getMessage());
1346: }
1347: }
1348:
1349: // String
1350: public void testGetActionErrors1b() {
1351: request.setAttribute("foo", "bar");
1352:
1353: try {
1354: ActionMessages errors = tagutils.getActionMessages(
1355: pageContext, "foo");
1356:
1357: assertNotNull("errors should not be null", errors);
1358: assertNotNull("errors prop should not be null", errors
1359: .get("prop"));
1360:
1361: String key = null;
1362: int i = 0;
1363:
1364: for (Iterator iter = errors
1365: .get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext();) {
1366: ActionMessage error = (ActionMessage) iter.next();
1367:
1368: key = error.getKey();
1369:
1370: Object[] values = error.getValues();
1371:
1372: assertNull(values);
1373: i++;
1374: }
1375:
1376: assertEquals("only 1 error", i, 1);
1377: assertEquals("key should match", key, "bar");
1378: } catch (JspException e) {
1379: fail(e.getMessage());
1380: }
1381: }
1382:
1383: // String Array
1384: public void testGetActionErrors1c() {
1385: String[] vals = new String[] { "bar", "baz" };
1386:
1387: request.setAttribute("foo", vals);
1388:
1389: try {
1390: ActionMessages errors = tagutils.getActionMessages(
1391: pageContext, "foo");
1392:
1393: assertNotNull("errors should not be null", errors);
1394: assertNotNull("errors prop should not be null", errors
1395: .get("prop"));
1396:
1397: String key = null;
1398: int i = 0;
1399:
1400: for (Iterator iter = errors
1401: .get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext();) {
1402: ActionMessage error = (ActionMessage) iter.next();
1403:
1404: key = error.getKey();
1405:
1406: Object[] values = error.getValues();
1407:
1408: assertNull((values));
1409: assertEquals("1st key should match", key, vals[i]);
1410: i++;
1411: }
1412:
1413: assertEquals("only 1 error", i, 2);
1414: } catch (JspException e) {
1415: fail(e.getMessage());
1416: }
1417: }
1418:
1419: // String Array (thrown JspException)
1420: public void testGetActionErrors1d() {
1421: request.setAttribute("foo", new MockFormBean());
1422:
1423: ActionMessages errors = null;
1424:
1425: try {
1426: errors = tagutils.getActionMessages(pageContext, "foo");
1427: fail("should have thrown JspException");
1428: } catch (JspException e) {
1429: assertNull("errors should be null", errors);
1430: }
1431: }
1432:
1433: // ActionErrors (thrown Exception)
1434: // TODO -- currently this does not hit the line for caught Exception
1435: public void testGetActionErrors1e() {
1436: ActionMessages actionErrors = new ActionMessages();
1437:
1438: actionErrors.add("prop", new ActionMessage("key.key"));
1439: request.setAttribute("errors", actionErrors);
1440:
1441: try {
1442: ActionMessages errors = tagutils.getActionMessages(
1443: pageContext, "does-not-exist");
1444:
1445: assertNotNull("errors should not be null", errors);
1446: assertNotNull("errors prop should not be null", errors
1447: .get("prop"));
1448:
1449: for (Iterator iter = errors.get("prop"); iter.hasNext();) {
1450: fail("Should not have any errors for does-not-exist");
1451: }
1452: } catch (JspException e) {
1453: fail(e.getMessage());
1454: }
1455: }
1456:
1457: // ------------------------------------------ getActionMappingName()
1458: public void testGetActionMappingName1() {
1459: String[] paths = { "foo", "foo.do", "foo?foo=bar",
1460: "foo?foo=bar&bar=baz", "foo?foo=bar&bar=baz" };
1461:
1462: String[][] prepends = { { "", "/foo" }, { "/", "/foo" },
1463: { "bar/", "/bar/foo" }, { "/bar/", "/bar/foo" } };
1464:
1465: String[] appends = { "", "#anchor", "?", "?#", "?foo=bar",
1466: "?foo1=bar1&foo2=bar2", "?foo1=bar1&foo2=bar2" };
1467:
1468: String finalResult = null;
1469:
1470: String path = null;
1471: String results = null;
1472: boolean equality = false;
1473: int ct = 0;
1474:
1475: for (int i = 0; i < appends.length; i++) {
1476: for (int j = 0; j < prepends.length; j++) {
1477: finalResult = prepends[j][1];
1478:
1479: for (int k = 0; k < paths.length; k++) {
1480: path = prepends[j][0] + paths[k] + appends[i];
1481: results = tagutils.getActionMappingName(path);
1482: equality = finalResult.equals(results);
1483:
1484: if (!equality) {
1485: fail("Path does not return correct result\n"
1486: + "\nexpected: " + results
1487: + "\nfound: " + path);
1488: }
1489:
1490: assertTrue("Path should translate to result",
1491: equality);
1492: ct++;
1493: }
1494: }
1495: }
1496:
1497: log.debug(ct + " assertions run in this test");
1498: }
1499:
1500: public void testString_getActionMappingURL_String_PageContext() {
1501: ActionConfig actionConfig = new ActionConfig();
1502:
1503: actionConfig.setParameter("/foo");
1504: moduleConfig.addActionConfig(actionConfig);
1505:
1506: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1507: request.setPathElements("/myapp", "/foo.do", null, null);
1508:
1509: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1510: "/foo", pageContext), "/myapp/foo");
1511: }
1512:
1513: // use servlet mapping (extension mapping)
1514: public void testString_getActionMappingURL_String_String_PageContext_boolean1() {
1515: pageContext.getServletContext().setAttribute(
1516: Globals.SERVLET_KEY, "*.do");
1517:
1518: ActionConfig actionConfig = new ActionConfig();
1519:
1520: actionConfig.setParameter("/foo");
1521: moduleConfig.addActionConfig(actionConfig);
1522:
1523: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1524: request.setPathElements("/myapp", "/baz.do", null, null);
1525:
1526: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1527: "/foo", pageContext), "/myapp/foo.do");
1528: }
1529:
1530: // use servlet mapping (extension mapping)
1531: // -- with params
1532: public void testString_getActionMappingURL_String_String_PageContext_boolean2() {
1533: pageContext.getServletContext().setAttribute(
1534: Globals.SERVLET_KEY, "*.do");
1535:
1536: ActionConfig actionConfig = new ActionConfig();
1537:
1538: actionConfig.setParameter("/foo");
1539: moduleConfig.addActionConfig(actionConfig);
1540:
1541: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1542: request
1543: .setPathElements("/myapp", "/baz.do?foo=bar", null,
1544: null);
1545:
1546: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1547: "/foo?foo=bar", pageContext), "/myapp/foo.do?foo=bar");
1548: }
1549:
1550: // use servlet mapping (extension mapping)
1551: // -- path as "/"
1552: // (this is probably not a realistic use case)
1553: public void testString_getActionMappingURL_String_String_PageContext_boolean3() {
1554: pageContext.getServletContext().setAttribute(
1555: Globals.SERVLET_KEY, "*.do");
1556:
1557: ActionConfig actionConfig = new ActionConfig();
1558:
1559: actionConfig.setParameter("/foo");
1560: moduleConfig.addActionConfig(actionConfig);
1561:
1562: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1563: request.setPathElements("/mycontext", "/baz", null, null);
1564:
1565: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1566: "/", pageContext), "/mycontext/.do");
1567: }
1568:
1569: // use servlet mapping (path mapping)
1570: public void testString_getActionMappingURL_String_String_PageContext_boolean4() {
1571: pageContext.getServletContext().setAttribute(
1572: Globals.SERVLET_KEY, "/myapp/*");
1573:
1574: ActionConfig actionConfig = new ActionConfig();
1575:
1576: actionConfig.setParameter("/foo");
1577: moduleConfig.addActionConfig(actionConfig);
1578:
1579: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1580: request.setPathElements("/mycontext", "/baz", null, null);
1581:
1582: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1583: "/foo", pageContext), "/mycontext/myapp/foo");
1584: }
1585:
1586: // use servlet mapping (path mapping)
1587: // -- with params
1588: public void testString_getActionMappingURL_String_String_PageContext_boolean5() {
1589: pageContext.getServletContext().setAttribute(
1590: Globals.SERVLET_KEY, "/myapp/*");
1591:
1592: ActionConfig actionConfig = new ActionConfig();
1593:
1594: actionConfig.setParameter("/foo");
1595: moduleConfig.addActionConfig(actionConfig);
1596:
1597: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1598: request.setPathElements("/mycontext", "/baz?foo=bar", null,
1599: null);
1600:
1601: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1602: "/foo?foo=bar", pageContext),
1603: "/mycontext/myapp/foo?foo=bar");
1604: }
1605:
1606: // use servlet mapping (path mapping)
1607: // -- using "/" as mapping
1608: public void testString_getActionMappingURL_String_String_PageContext_boolean6() {
1609: pageContext.getServletContext().setAttribute(
1610: Globals.SERVLET_KEY, "/");
1611:
1612: ActionConfig actionConfig = new ActionConfig();
1613:
1614: actionConfig.setParameter("/foo");
1615: moduleConfig.addActionConfig(actionConfig);
1616:
1617: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1618: request.setPathElements("/mycontext", "/baz", null, null);
1619:
1620: assertEquals("Check path /foo", tagutils.getActionMappingURL(
1621: "/", pageContext), "/mycontext/");
1622: }
1623:
1624: // ------------------------------------------ getActionMessages()
1625: // -- using ActionMessages
1626: public void testActionMessages_getActionMessages_PageContext_String1() {
1627: ActionMessages actionMessages = new ActionMessages();
1628:
1629: actionMessages.add("prop", new ActionMessage("key.key"));
1630: request.setAttribute("messages", actionMessages);
1631:
1632: try {
1633: ActionMessages messages = tagutils.getActionMessages(
1634: pageContext, "messages");
1635:
1636: assertNotNull("messages should not be null", messages);
1637: assertNotNull("messages prop should not be null", messages
1638: .get("prop"));
1639:
1640: String val = null;
1641: int i = 0;
1642:
1643: for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1644: ActionMessage message = (ActionMessage) iter.next();
1645:
1646: val = message.getKey();
1647: i++;
1648: }
1649:
1650: assertEquals("only 1 message", i, 1);
1651: assertEquals("messages prop should match", val, "key.key");
1652: } catch (JspException e) {
1653: fail(e.getMessage());
1654: }
1655: }
1656:
1657: // -- using ActionErrors
1658: public void testActionMessages_getActionMessages_PageContext_String2() {
1659: ActionMessages actionMessages = new ActionMessages();
1660:
1661: actionMessages.add("prop", new ActionMessage("key.key"));
1662: request.setAttribute("messages", actionMessages);
1663:
1664: try {
1665: ActionMessages messages = tagutils.getActionMessages(
1666: pageContext, "messages");
1667:
1668: assertNotNull("messages should not be null", messages);
1669: assertNotNull("messages prop should not be null", messages
1670: .get("prop"));
1671:
1672: String val = null;
1673: int i = 0;
1674:
1675: for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1676: ActionMessage message = (ActionMessage) iter.next();
1677:
1678: val = message.getKey();
1679: i++;
1680: }
1681:
1682: assertEquals("only 1 message", i, 1);
1683: assertEquals("messages prop should match", val, "key.key");
1684: } catch (JspException e) {
1685: fail(e.getMessage());
1686: }
1687: }
1688:
1689: // -- using String
1690: public void testActionMessages_getActionMessages_PageContext_String3() {
1691: request.setAttribute("foo", "bar");
1692:
1693: try {
1694: ActionMessages messages = tagutils.getActionMessages(
1695: pageContext, "foo");
1696:
1697: assertNotNull("messages should not be null", messages);
1698: assertNotNull("messages prop should not be null", messages
1699: .get("prop"));
1700:
1701: String key = null;
1702: int i = 0;
1703:
1704: for (Iterator iter = messages
1705: .get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext();) {
1706: ActionMessage message = (ActionMessage) iter.next();
1707:
1708: key = message.getKey();
1709:
1710: Object[] values = message.getValues();
1711:
1712: assertNull(values);
1713: i++;
1714: }
1715:
1716: assertEquals("only 1 message", i, 1);
1717: assertEquals("key should match", key, "bar");
1718: } catch (JspException e) {
1719: fail(e.getMessage());
1720: }
1721: }
1722:
1723: // -- using String Array
1724: public void testActionMessages_getActionMessages_PageContext_String4() {
1725: String[] vals = new String[] { "bar", "baz" };
1726:
1727: request.setAttribute("foo", vals);
1728:
1729: try {
1730: ActionMessages messages = tagutils.getActionMessages(
1731: pageContext, "foo");
1732:
1733: assertNotNull("messages should not be null", messages);
1734: assertNotNull("messages prop should not be null", messages
1735: .get("prop"));
1736:
1737: String key = null;
1738: int i = 0;
1739:
1740: for (Iterator iter = messages
1741: .get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext();) {
1742: ActionMessage message = (ActionMessage) iter.next();
1743:
1744: key = message.getKey();
1745:
1746: Object[] values = message.getValues();
1747:
1748: assertNull((values));
1749: assertEquals("1st key should match", key, vals[i]);
1750: i++;
1751: }
1752:
1753: assertEquals("only 1 message", i, 2);
1754: } catch (JspException e) {
1755: fail(e.getMessage());
1756: }
1757: }
1758:
1759: // String Array (thrown JspException)
1760: public void testActionMessages_getActionMessages_PageContext_String5() {
1761: request.setAttribute("foo", new MockFormBean());
1762:
1763: ActionMessages messages = null;
1764:
1765: try {
1766: messages = tagutils.getActionMessages(pageContext, "foo");
1767: fail("should have thrown JspException");
1768: } catch (JspException e) {
1769: assertNull("messages should be null", messages);
1770: }
1771: }
1772:
1773: // ActionMessages (thrown Exception)
1774: // TODO -- currently this does not hit the line for caught Exception
1775: public void testActionMessages_getActionMessages_PageContext_String6() {
1776: ActionMessages actionMessages = new ActionMessages();
1777:
1778: actionMessages.add("prop", new ActionMessage("key.key"));
1779: request.setAttribute("messages", actionMessages);
1780:
1781: try {
1782: ActionMessages messages = tagutils.getActionMessages(
1783: pageContext, "does-not-exist");
1784:
1785: assertNotNull("messages should not be null", messages);
1786: assertNotNull("messages prop should not be null", messages
1787: .get("prop"));
1788:
1789: for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1790: fail("Should not have any messages for does-not-exist");
1791: }
1792: } catch (JspException e) {
1793: fail(e.getMessage());
1794: }
1795: }
1796:
1797: // ----public ModuleConfig getModuleConfig(PageContext pageContext)
1798: public void testModuleConfig_getModuleConfig_PageContext() {
1799: MockServletConfig mockServletConfig = new MockServletConfig();
1800: ModuleConfig moduleConfig = new ModuleConfigImpl("");
1801: MockServletContext mockServletContext = new MockServletContext();
1802: MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
1803: MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
1804:
1805: mockServletConfig.setServletContext(mockServletContext);
1806:
1807: MockPageContext mockPageContext = new MockPageContext(
1808: mockServletConfig, mockHttpServletRequest,
1809: mockHttpServletResponse);
1810:
1811: ModuleConfig foundModuleConfig = null;
1812:
1813: try {
1814: foundModuleConfig = tagutils
1815: .getModuleConfig(mockPageContext);
1816: fail("Expected ModuleConfig to not be found");
1817: } catch (NullPointerException ignore) {
1818: // expected result
1819: }
1820:
1821: mockHttpServletRequest.setAttribute(Globals.MODULE_KEY,
1822: moduleConfig);
1823:
1824: mockPageContext.getServletContext().setAttribute(
1825: Globals.MODULE_KEY, mockPageContext);
1826:
1827: foundModuleConfig = tagutils.getModuleConfig(mockPageContext);
1828: assertNotNull(foundModuleConfig);
1829: }
1830:
1831: // -- public Locale getUserLocale(PageContext pageContext, String locale)
1832: public void testLocale_getUserLocale_PageContext_String() {
1833: request.setLocale(Locale.ENGLISH);
1834: assertEquals(tagutils.getUserLocale(pageContext, ""),
1835: Locale.ENGLISH);
1836:
1837: request.setLocale(Locale.CANADA);
1838: assertEquals(tagutils.getUserLocale(pageContext, ""),
1839: Locale.CANADA);
1840: }
1841:
1842: // -- public boolean isXhtml(PageContext pageContext)
1843: public void test_boolean_isXhtml_PageContext() {
1844: assertFalse(tagutils.isXhtml(pageContext));
1845: pageContext.setAttribute(Globals.XHTML_KEY, "true");
1846:
1847: assertTrue(tagutils.isXhtml(pageContext));
1848: }
1849:
1850: // -- public Object lookup(PageContext pageContext, String name, String scopeName)
1851: // lookup with null scope
1852: public void test_Object_lookup_PageContext_String__String1() {
1853: pageContext.setAttribute("bean", new MockFormBean());
1854:
1855: try {
1856: Object val = tagutils.lookup(pageContext, "bean", null);
1857:
1858: assertNotNull((val));
1859: } catch (JspException e) {
1860: fail("bean not found:" + e.getMessage());
1861: }
1862: }
1863:
1864: // lookup with page scope
1865: public void test_Object_lookup_PageContext_String__String2() {
1866: pageContext.setAttribute("bean", new MockFormBean());
1867:
1868: try {
1869: Object val = tagutils.lookup(pageContext, "bean", "page");
1870:
1871: assertNotNull((val));
1872: } catch (JspException e) {
1873: fail("bean not found:" + e.getMessage());
1874: }
1875: }
1876:
1877: // lookup with invalid scope
1878: // -- (where an exception is thrown)
1879: public void test_Object_lookup_PageContext_String__String3() {
1880: pageContext.setAttribute("bean", new MockFormBean());
1881:
1882: Object val = null;
1883:
1884: try {
1885: val = tagutils.lookup(pageContext, "bean", "invalid");
1886: fail("invalid scope :");
1887: } catch (JspException e) {
1888: assertNull((val));
1889: }
1890: }
1891:
1892: // try to get the call to throw an IllegalAccessException
1893: public void test_Object_lookup_PageContext_String_String_String1() {
1894: // page.setAttribute("bean", new MockFormBean());
1895: // Object val = null;
1896: // try {
1897: // val = tagutils.lookup(page, "bean", "throwIllegalAccessException");
1898: // fail("should have thrown exception");
1899: // } catch (JspException e) {
1900: // assertNull(val);
1901: // }
1902: }
1903:
1904: // try to get the call to throw an IllegalArgumentException
1905: public void test_Object_lookup_PageContext_String_String_String2() {
1906: pageContext.setAttribute("bean", new MockFormBean());
1907:
1908: Object val = null;
1909:
1910: try {
1911: val = tagutils.lookup(pageContext, "bean",
1912: "doesNotExistMethod", "page");
1913: fail("should have thrown exception");
1914: } catch (JspException e) {
1915: assertNull(val);
1916: }
1917: }
1918:
1919: // try to get the call to throw an NoSuchMethodException
1920: public void test_Object_lookup_PageContext_String_String_String3() {
1921: pageContext.setAttribute("bean", new MockFormBean());
1922:
1923: Object val = null;
1924:
1925: try {
1926: val = tagutils.lookup(pageContext, "bean",
1927: "doesNotExistMethod");
1928: fail("should have thrown exception");
1929: } catch (JspException e) {
1930: assertNull(val);
1931: }
1932: }
1933:
1934: /**
1935: * Testing message()
1936: *
1937: * public String message( PageContext pageContext, String bundle, String
1938: * locale, String key) throws JspException
1939: */
1940: public void testMessageBadParams() {
1941: String val = null;
1942:
1943: try {
1944: val = tagutils.message(pageContext, "bundle", "locale",
1945: "key");
1946: fail("val should be null");
1947: } catch (JspException e) {
1948: assertNull(val);
1949: }
1950: }
1951:
1952: // set bundle in page scope
1953: // message() assumes the bundle will never be in page scope
1954: // -- bad key
1955: public void donttestMessagePageBadKey() {
1956: putBundleInScope(PageContext.PAGE_SCOPE, true);
1957:
1958: String val = null;
1959:
1960: try {
1961: val = tagutils.message(pageContext, null, null,
1962: "foo.bar.does.not.exist");
1963: fail("val should be null");
1964: } catch (JspException e) {
1965: assertNull(val);
1966: }
1967: }
1968:
1969: // set bundle in request scope
1970: // -- bad key
1971: public void testMessageRequestBadKey() {
1972: putBundleInScope(PageContext.REQUEST_SCOPE, true);
1973:
1974: String val = null;
1975:
1976: try {
1977: val = tagutils.message(pageContext, null, null,
1978: "foo.bar.does.not.exist");
1979: assertNull(val);
1980: } catch (JspException e) {
1981: fail("val should be null, no exception");
1982: }
1983: }
1984:
1985: // set bundle in session scope
1986: // -- bad key
1987: //
1988: // This represents a use case where the user specifically set the bundle
1989: // in session under Globals.MESSAGES_KEY.
1990: // Perhaps we should check session, and throw/log a rather explicit message
1991: // for why this is a bad idea, instead of ignoring or returning null.
1992: public void testMessageSessionBadKey() {
1993: putBundleInScope(PageContext.SESSION_SCOPE, true);
1994:
1995: String val = null;
1996:
1997: try {
1998: val = tagutils.message(pageContext, null, null,
1999: "foo.bar.does.not.exist");
2000: fail("MessageResources should never be put in session scope.");
2001: } catch (JspException e) {
2002: assertNull(val);
2003: }
2004: }
2005:
2006: // set bundle in application scope
2007: // -- bad key
2008: public void testMessageApplicationBadKey() {
2009: putBundleInScope(PageContext.APPLICATION_SCOPE, true);
2010:
2011: String val = null;
2012:
2013: try {
2014: val = tagutils.message(pageContext, null, null,
2015: "foo.bar.does.not.exist");
2016: assertNull(val);
2017: } catch (JspException e) {
2018: fail("val should be null, no exception");
2019: }
2020: }
2021:
2022: // set bundle in request scope
2023: // -- good key
2024: public void testMessageRequestGoodKey() {
2025: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2026:
2027: String val = null;
2028:
2029: try {
2030: val = tagutils.message(pageContext, null, null, "foo");
2031: assertTrue("Validate message value", "bar".equals(val));
2032: } catch (JspException e) {
2033: fail("val should be \"bar\"");
2034: }
2035: }
2036:
2037: // set bundle in application scope
2038: // -- good key
2039: public void testMessageApplicationGoodKey() {
2040: putBundleInScope(PageContext.APPLICATION_SCOPE, true);
2041:
2042: String val = null;
2043:
2044: try {
2045: val = tagutils.message(pageContext, null, null, "foo");
2046: assertTrue("Validate message value", "bar".equals(val));
2047: } catch (JspException e) {
2048: fail("val should be \"bar\"");
2049: }
2050: }
2051:
2052: /**
2053: * Tests for:
2054: *
2055: * public String message( PageContext pageContext, String bundle, String
2056: * locale, String key, Object args[]) throws JspException
2057: */
2058: public void testMessageRequestGoodKeyWithNullParams() {
2059: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2060:
2061: String[] args = null;
2062:
2063: String val = null;
2064:
2065: try {
2066: val = tagutils
2067: .message(pageContext, null, null, "foo", args);
2068: assertTrue("Validate message value", "bar".equals(val));
2069: } catch (JspException e) {
2070: fail("val should be \"bar\"");
2071: }
2072: }
2073:
2074: public void testMessageApplicationGoodKeyWithNullParams() {
2075: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2076:
2077: String[] args = null;
2078:
2079: String val = null;
2080:
2081: try {
2082: val = tagutils
2083: .message(pageContext, null, null, "foo", args);
2084: assertTrue("Validate message value", "bar".equals(val));
2085: } catch (JspException e) {
2086: fail("val should be \"bar\"");
2087: }
2088: }
2089:
2090: public void testMessageRequestGoodKeyWithParams() {
2091: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2092:
2093: String[] args = { "I love this" };
2094:
2095: String val = null;
2096:
2097: try {
2098: val = tagutils.message(pageContext, null, null, "foo.bar",
2099: args);
2100: assertTrue("Validate message value", "I love this bar"
2101: .equals(val));
2102: } catch (JspException e) {
2103: fail("val should be \"bar\"");
2104: }
2105: }
2106:
2107: public void testMessageApplicationGoodKeyWithParams() {
2108: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2109:
2110: String[] args = { "I love this" };
2111:
2112: String val = null;
2113:
2114: try {
2115: val = tagutils.message(pageContext, null, null, "foo.bar",
2116: args);
2117: assertTrue("Validate message value", "I love this bar"
2118: .equals(val));
2119: } catch (JspException e) {
2120: fail("val should be \"bar\"");
2121: }
2122: }
2123:
2124: /**
2125: * Tests for: public boolean present( PageContext pageContext, String
2126: * bundle, String locale, String key) throws JspException {
2127: */
2128: public void testPresentNulls() {
2129: boolean result = false;
2130:
2131: try {
2132: result = tagutils.present(null, null, null, null);
2133: fail("An exception should have been thrown");
2134: } catch (JspException e) {
2135: fail("An npe should have been thrown");
2136: } catch (NullPointerException e) {
2137: assertFalse("Correct behaviour", result);
2138: }
2139: }
2140:
2141: public void testPresentBadKey() {
2142: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2143:
2144: boolean result = false;
2145:
2146: try {
2147: result = tagutils.present(pageContext, null, null,
2148: "foo.bar.not.exist");
2149: assertFalse("Value should be null", result);
2150: } catch (JspException e) {
2151: fail("An npe should have been thrown");
2152: } catch (NullPointerException e) {
2153: assertFalse("Correct behaviour", result);
2154: }
2155: }
2156:
2157: public void testPresentGoodKey() {
2158: putBundleInScope(PageContext.REQUEST_SCOPE, true);
2159:
2160: boolean result = false;
2161:
2162: try {
2163: result = tagutils.present(pageContext, null, null, "foo");
2164: assertTrue("Key should have been found", result);
2165: } catch (JspException e) {
2166: fail("An exception should have been thrown");
2167: }
2168: }
2169:
2170: /**
2171: * public void write(PageContext pageContext, String text) throws
2172: * JspException {
2173: */
2174: public void testWriteNullParams() {
2175: try {
2176: tagutils.write(null, null);
2177: fail("NullPointerException should have been thrown");
2178: } catch (JspException e) {
2179: fail("NullPointerException should have been thrown");
2180: } catch (NullPointerException e) {
2181: // pass
2182: }
2183: }
2184:
2185: public void testWrite() {
2186: MockPageContext pg = new MockPageContext(false, false);
2187:
2188: try {
2189: tagutils.write(pg, null);
2190: } catch (JspException e) {
2191: fail("JspException should not have been thrown");
2192: }
2193: }
2194:
2195: public void testWriteThrowException() {
2196: MockPageContext pg = new MockPageContext(true, false);
2197:
2198: try {
2199: tagutils.write(pg, null);
2200: fail("JspException should have been thrown");
2201: } catch (JspException e) {
2202: // success
2203: }
2204: }
2205:
2206: public void testWritePrevious() {
2207: MockPageContext pg = new MockPageContext(false, false);
2208:
2209: try {
2210: tagutils.writePrevious(pg, null);
2211: } catch (JspException e) {
2212: fail("JspException should not have been thrown");
2213: }
2214: }
2215:
2216: public void testWritePreviousThrowException() {
2217: MockPageContext pg = new MockPageContext(true, false);
2218:
2219: try {
2220: tagutils.writePrevious(pg, null);
2221: fail("JspException should have been thrown");
2222: } catch (JspException e) {
2223: // success
2224: }
2225: }
2226:
2227: public void testWritePreviousBody() {
2228: MockPageContext pg = new MockPageContext(false, true);
2229:
2230: try {
2231: tagutils.writePrevious(pg, null);
2232: } catch (JspException e) {
2233: fail("JspException should not have been thrown");
2234: }
2235: }
2236:
2237: public void testOverrideInstance() {
2238:
2239: class CustomTagUtils extends TagUtils {
2240: public String filter(String value) {
2241: return "I HAVE BEEN OVERRIDDEN!";
2242: }
2243: }
2244: // verify original logic
2245: assertNull("Filter Test", TagUtils.getInstance().filter(null));
2246:
2247: // set the custom instance
2248: TagUtils.setInstance(new CustomTagUtils());
2249: assertEquals("Custom Instance Test", TagUtils.getInstance()
2250: .filter(null), "I HAVE BEEN OVERRIDDEN!");
2251:
2252: // reset back to the cached instance
2253: TagUtils.setInstance(tagutils);
2254: assertNull("Filter Test", TagUtils.getInstance().filter(null));
2255:
2256: }
2257: }
|