Skip to content

Commit

Permalink
Make test more robust (not dependent on hash order)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1724433 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jan 13, 2016
1 parent c500f9a commit c99f0dc
Showing 1 changed file with 87 additions and 22 deletions.
109 changes: 87 additions & 22 deletions test/org/apache/catalina/core/TestApplicationHttpRequest.java
Expand Up @@ -18,6 +18,7 @@


import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;


Expand All @@ -41,95 +42,129 @@ public class TestApplicationHttpRequest extends TomcatBaseTest {
*/ */
@Test @Test
public void testForwardQueryString01() throws Exception { public void testForwardQueryString01() throws Exception {
doQueryStringTest(null, "a=b", "a:(b)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b" });
doQueryStringTest(null, "a=b", expected);
} }




@Test @Test
public void testForwardQueryString02() throws Exception { public void testForwardQueryString02() throws Exception {
doQueryStringTest(null, "a=b&a=c", "a:(b),(c)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "c" });
doQueryStringTest(null, "a=b&a=c", expected);
} }




@Test @Test
public void testForwardQueryString03() throws Exception { public void testForwardQueryString03() throws Exception {
doQueryStringTest(null, "a=b&c=d", "a:(b);c:(d)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b" });
expected.put("c", new String[] { "d" });
doQueryStringTest(null, "a=b&c=d", expected);
} }




@Test @Test
public void testForwardQueryString04() throws Exception { public void testForwardQueryString04() throws Exception {
doQueryStringTest(null, "a=b&c=d&a=e", "a:(b),(e);c:(d)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "e" });
expected.put("c", new String[] { "d" });
doQueryStringTest(null, "a=b&c=d&a=e", expected);
} }




@Test @Test
public void testForwardQueryString05() throws Exception { public void testForwardQueryString05() throws Exception {
// Parameters with no value are assigned a vale of the empty string // Parameters with no value are assigned a vale of the empty string
doQueryStringTest(null, "a=b&c&a=e", "a:(b),(e);c:()"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "e" });
expected.put("c", new String[] { "" });
doQueryStringTest(null, "a=b&c&a=e", expected);
} }




@Test @Test
public void testOriginalQueryString01() throws Exception { public void testOriginalQueryString01() throws Exception {
doQueryStringTest("a=b", null, "a:(b)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b" });
doQueryStringTest("a=b", null, expected);
} }




@Test @Test
public void testOriginalQueryString02() throws Exception { public void testOriginalQueryString02() throws Exception {
doQueryStringTest("a=b&a=c", null, "a:(b),(c)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "c" });
doQueryStringTest("a=b&a=c", null, expected);
} }




@Test @Test
public void testOriginalQueryString03() throws Exception { public void testOriginalQueryString03() throws Exception {
doQueryStringTest("a=b&c=d", null, "a:(b);c:(d)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b" });
expected.put("c", new String[] { "d" });
doQueryStringTest("a=b&c=d", null, expected);
} }




@Test @Test
public void testOriginalQueryString04() throws Exception { public void testOriginalQueryString04() throws Exception {
doQueryStringTest("a=b&c=d&a=e", null, "a:(b),(e);c:(d)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "e" });
expected.put("c", new String[] { "d" });
doQueryStringTest("a=b&c=d&a=e", null, expected);
} }




@Test @Test
public void testOriginalQueryString05() throws Exception { public void testOriginalQueryString05() throws Exception {
// Parameters with no value are assigned a vale of the empty string // Parameters with no value are assigned a vale of the empty string
doQueryStringTest("a=b&c&a=e", null, "a:(b),(e);c:()"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "b", "e" });
expected.put("c", new String[] { "" });
doQueryStringTest("a=b&c&a=e", null, expected);
} }




@Test @Test
public void testMergeQueryString01() throws Exception { public void testMergeQueryString01() throws Exception {
doQueryStringTest("a=b", "a=z", "a:(z),(b)"); Map<String,String[]> expected = new HashMap<>();
expected.put("a", new String[] { "z", "b" });
doQueryStringTest("a=b", "a=z", expected);
} }




@Test @Test
public void testMergeQueryString02() throws Exception { public void testMergeQueryString02() throws Exception {
// Parameters with no value are assigned a vale of the empty string Map<String,String[]> expected = new HashMap<>();
doQueryStringTest("a=b&c&a=e", "a=z", "a:(z),(b),(e);c:()"); expected.put("a", new String[] { "z", "b", "e" });
expected.put("c", new String[] { "" });
doQueryStringTest("a=b&c&a=e", "a=z", expected);
} }




@Test @Test
public void testMergeQueryString03() throws Exception { public void testMergeQueryString03() throws Exception {
// Parameters with no value are assigned a vale of the empty string Map<String,String[]> expected = new HashMap<>();
doQueryStringTest("a=b&c&a=e", "c=z", "a:(b),(e);c:(z),()"); expected.put("a", new String[] { "b", "e" });
expected.put("c", new String[] { "z", "" });
doQueryStringTest("a=b&c&a=e", "c=z", expected);
} }




@Test @Test
public void testMergeQueryString04() throws Exception { public void testMergeQueryString04() throws Exception {
// Parameters with no value are assigned a vale of the empty string Map<String,String[]> expected = new HashMap<>();
doQueryStringTest("a=b&c&a=e", "a", "a:(),(b),(e);c:()"); expected.put("a", new String[] { "", "b", "e" });
expected.put("c", new String[] { "" });
doQueryStringTest("a=b&c&a=e", "a", expected);
} }




private void doQueryStringTest(String originalQueryString, String forwardQueryString, private void doQueryStringTest(String originalQueryString, String forwardQueryString,
String expected) throws Exception { Map<String,String[]> expected) throws Exception {
Tomcat tomcat = getTomcatInstance(); Tomcat tomcat = getTomcatInstance();


// No file system docBase required // No file system docBase required
Expand All @@ -142,7 +177,7 @@ private void doQueryStringTest(String originalQueryString, String forwardQuerySt
} }
ctx.addServletMapping("/forward", "forward"); ctx.addServletMapping("/forward", "forward");


Tomcat.addServlet(ctx, "display", new DisplayParameterServlet()); Tomcat.addServlet(ctx, "display", new DisplayParameterServlet(expected));
ctx.addServletMapping("/display", "display"); ctx.addServletMapping("/display", "display");


tomcat.start(); tomcat.start();
Expand All @@ -158,7 +193,7 @@ private void doQueryStringTest(String originalQueryString, String forwardQuerySt
int rc = getUrl(target.toString(), response, null); int rc = getUrl(target.toString(), response, null);


Assert.assertEquals(200, rc); Assert.assertEquals(200, rc);
Assert.assertEquals(expected, response.toString()); Assert.assertEquals("OK", response.toString());
} }




Expand All @@ -184,15 +219,45 @@ private static class DisplayParameterServlet extends HttpServlet {


private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


private Map<String,String[]> expected;

public DisplayParameterServlet(Map<String,String[]> expected) {
this.expected = expected;
}

@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { throws ServletException, IOException {
resp.setContentType("text/plain"); resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
PrintWriter w = resp.getWriter(); PrintWriter w = resp.getWriter();
Map<String,String[]> params = req.getParameterMap(); Map<String,String[]> actual = req.getParameterMap();

boolean ok = true;
for (Entry<String,String[]> entry : actual.entrySet()) {
String[] expectedValue = expected.get(entry.getKey());
if (expectedValue == null ||
expectedValue.length != entry.getValue().length) {
ok = false;
break;
}
for (int i = 0; i < expectedValue.length; i++) {
if (!expectedValue[i].equals(entry.getValue()[i])) {
ok = false;
break;
}
}
if (!ok) {
break;
}
}

if (ok) {
w.print("OK");
return;
}
boolean firstParam = true; boolean firstParam = true;
for (Entry<String,String[]> param : params.entrySet()) { for (Entry<String,String[]> param : actual.entrySet()) {
if (firstParam) { if (firstParam) {
firstParam = false; firstParam = false;
} else { } else {
Expand Down

0 comments on commit c99f0dc

Please sign in to comment.