1 18 19 package org.apache.struts.action; 20 21 import junit.framework.TestSuite; 22 import junit.framework.TestCase; 23 import junit.framework.ComparisonFailure; 24 import junit.framework.AssertionFailedError; 25 26 import java.util.Map ; 27 28 33 public class TestActionRedirect extends TestCase { 34 35 36 public TestActionRedirect(String s) { 37 super(s); 38 } 39 40 41 public static TestSuite getSuite() { 42 return new TestSuite(TestActionRedirect.class); 43 } 44 45 46 public static void main(String [] args) { 47 junit.textui.TestRunner runner = new junit.textui.TestRunner(); 48 runner.doRun(TestActionRedirect.getSuite()); 49 } 50 51 52 53 55 56 59 public void testActionRedirectRedirectFlag() { 60 ActionRedirect ar = new ActionRedirect("/path.do"); 61 assertTrue("Redirect flag should be set to true.",ar.getRedirect()); 62 } 63 64 65 68 public void testActionRedirectAddParameter() { 69 ActionRedirect ar = new ActionRedirect("/path.do"); 70 71 ar.addParameter("st","test"); 72 ar.addParameter("obj",new StringBuffer ("someString")); 73 74 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0); 75 assertHasParameter(ar.parameterValues, "st", "test"); 76 assertHasParameter(ar.parameterValues, "obj", "someString"); 77 } 78 79 80 83 public void testActionRedirectAddSameNameParameter() { 84 ActionRedirect ar = new ActionRedirect("/path.do"); 85 86 ar.addParameter("param","param1"); 87 ar.addParameter("param","param2"); 88 ar.addParameter("param",new StringBuffer ("someString")); 89 90 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0); 91 assertHasParameter(ar.parameterValues, "param", "param1"); 92 assertHasParameter(ar.parameterValues, "param", "param2"); 93 assertHasParameter(ar.parameterValues, "param", "someString"); 94 assertEquals("Incorrect number of parameters", 3, countParameters(ar.parameterValues, "param")); 95 } 96 97 98 102 public void testActionRedirectFromExistingForward() { 103 ActionForward forward = new ActionForward("/path.do?param=param1"); 104 105 ActionRedirect ar = new ActionRedirect(forward); 106 107 ar.addParameter("param","param2"); 108 ar.addParameter("object1",new StringBuffer ("someString")); 109 110 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0); 111 assertHasParameter(ar.parameterValues, "param", "param2"); 112 assertHasParameter(ar.parameterValues, "object1", "someString"); 113 assertEquals("Incorrect original path.",forward.getPath(),ar.getOriginalPath()); 114 } 115 116 117 118 126 static void assertHasParameter( 127 Map parameters, 128 String paramName, 129 String paramValue) { 130 Object value = parameters.get(paramName); 131 if (value == null) { 132 throw new AssertionFailedError("Parameter [" + paramName + "] not found"); 133 } 134 135 if (value instanceof String ) { 136 if (!paramValue.equals(value)) { 137 throw new ComparisonFailure("Incorrect value found", 138 paramValue, (String ) value); 139 } 140 } else if (value instanceof String []) { 141 String [] values = (String []) value; 143 for (int i = 0; i < values.length; i++) { 144 if (paramValue.equals(values[i])) { 145 return; 146 } 147 } 148 throw new AssertionFailedError("Expected value not found for parameter [" + paramName + "]"); 149 } else { 150 throw new AssertionFailedError("Unexpected type found as parameter value for [" + paramName + "]"); 152 } 153 } 154 155 163 static int countParameters(Map parameters, 164 String paramName) { 165 Object value = parameters.get(paramName); 166 if (value == null) { 167 return 0; 168 } 169 170 if (value instanceof String ) { 171 return 1; 172 } else if (value instanceof String []) { 173 String [] values = (String []) value; 174 return values.length; 175 } else { 176 throw new AssertionFailedError("Unexpected type found as parameter value for [" + paramName + "]"); 178 } 179 } 180 181 182 } 183 | Popular Tags |