1 package jodd.servlet; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import junit.framework.TestCase; 7 8 public class TestServletUtil extends TestCase { 9 10 public void testMakeUrlParams() { 11 HashMap hm = new HashMap(); 12 hm.put("param1", "value1"); 13 assertEquals("param1=value1", ServletUtil.makeUrlParams(hm)); 14 15 hm.put("param2", "value2"); 16 String param = ServletUtil.makeUrlParams(hm); 17 assertTrue(param.indexOf("param1=value1") != -1); 18 assertTrue(param.indexOf("param2=value2") != -1); 19 assertTrue(param.indexOf("¶m") != -1); 20 21 hm.put("param3", "value3"); 22 param = ServletUtil.makeUrlParams(hm); 23 assertTrue(param.indexOf("param1=value1") != -1); 24 assertTrue(param.indexOf("param2=value2") != -1); 25 assertTrue(param.indexOf("param3=value3") != -1); 26 int i = param.indexOf("¶m"); 27 assertTrue(i != -1); 28 assertTrue(param.indexOf("¶m", i) != -1); 29 30 hm.clear(); 31 hm.put("param1", "1/2?3&4"); 32 assertEquals("param1=1%2F2%3F3%264", ServletUtil.makeUrlParams(hm)); 33 } 34 35 public void testGetUrlParams() { 36 String url = "?param1=value1"; 37 Map map = ServletUtil.getUrlParams(url); 38 assertEquals("value1", map.get("param1")); 39 40 url = "foo.do?param1=value1"; 41 map = ServletUtil.getUrlParams(url); 42 assertEquals("value1", map.get("param1")); 43 assertEquals(1, map.size()); 44 45 url = "?param1=value1¶m2=value2"; 46 map = ServletUtil.getUrlParams(url); 47 assertEquals("value1", map.get("param1")); 48 assertEquals("value2", map.get("param2")); 49 assertEquals(2, map.size()); 50 51 url = "?param1=1%2F2%3F3%264¶m2=value2¶m3=value3"; 52 map = ServletUtil.getUrlParams(url); 53 assertEquals("1/2?3&4", map.get("param1")); 54 assertEquals("value2", map.get("param2")); 55 assertEquals("value3", map.get("param3")); 56 assertNull(map.get("param4")); 57 assertEquals(3, map.size()); 58 59 60 url = "?param1=value1&"; 61 map = ServletUtil.getUrlParams(url); 62 assertEquals("value1", map.get("param1")); 63 assertEquals(1, map.size()); 64 65 url = "?param1=value1&&"; 66 map = ServletUtil.getUrlParams(url); 67 assertEquals("value1", map.get("param1")); 68 assertEquals(1, map.size()); 69 70 url = "?param1=value1&&&&&"; 71 map = ServletUtil.getUrlParams(url); 72 assertEquals("value1", map.get("param1")); 73 assertEquals(1, map.size()); 74 75 url = "?param1="; 76 map = ServletUtil.getUrlParams(url); 77 assertEquals("", map.get("param1")); 78 assertEquals(1, map.size()); 79 80 url = "?param2=value2¶m1="; 81 map = ServletUtil.getUrlParams(url); 82 assertEquals("", map.get("param1")); 83 assertEquals("value2", map.get("param2")); 84 assertEquals(2, map.size()); 85 86 url = "?param1=¶m2=value2"; 87 map = ServletUtil.getUrlParams(url); 88 assertEquals("", map.get("param1")); 89 assertEquals("value2", map.get("param2")); 90 assertEquals(2, map.size()); 91 92 url = "?param1"; 93 map = ServletUtil.getUrlParams(url); 94 assertEquals("", map.get("param1")); 95 assertEquals(1, map.size()); 96 97 url = "?param2=value2¶m1"; 98 map = ServletUtil.getUrlParams(url); 99 assertEquals("", map.get("param1")); 100 assertEquals("value2", map.get("param2")); 101 assertEquals(2, map.size()); 102 103 url = "?param1¶m2=value2"; 104 map = ServletUtil.getUrlParams(url); 105 assertEquals("", map.get("param1")); 106 assertEquals("value2", map.get("param2")); 107 assertEquals(2, map.size()); 108 109 url = ""; 110 map = ServletUtil.getUrlParams(url); 111 assertEquals(0, map.size()); 112 113 url = "action.do?"; 114 map = ServletUtil.getUrlParams(url); 115 assertEquals(0, map.size()); 116 117 url = "action.do"; 118 map = ServletUtil.getUrlParams(url); 119 assertEquals(0, map.size()); 120 121 url = "action.do?&"; 122 map = ServletUtil.getUrlParams(url); 123 assertEquals(0, map.size()); 124 125 url = "action.do?¶m1=value1"; 126 map = ServletUtil.getUrlParams(url); 127 assertEquals("value1", map.get("param1")); 128 assertEquals(1, map.size()); 129 } 130 131 } 132 | Popular Tags |