1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import javax.servlet.http.Cookie ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 24 import org.apache.hivemind.test.HiveMindTestCase; 25 import org.easymock.MockControl; 26 27 33 public class TestCookieSource extends HiveMindTestCase 34 { 35 private static class ComparableCookie extends Cookie 36 { 37 public ComparableCookie(String name, String value) 38 { 39 super(name, value); 40 } 41 42 public boolean equals(Object obj) 43 { 44 Cookie c = (Cookie ) obj; 45 46 return equals(getName(), c.getName()) && equals(getValue(), c.getValue()) 47 && equals(getPath(), c.getPath()); 48 } 49 50 private boolean equals(Object value, Object other) 51 { 52 return value == other || (value != null && value.equals(other)); 53 } 54 } 55 56 private HttpServletRequest setupRequest(String [] nameValues) 57 { 58 Cookie [] cookies = null; 59 60 if (nameValues != null) 61 { 62 63 List l = new ArrayList (); 64 65 for (int i = 0; i < nameValues.length; i += 2) 66 { 67 String name = nameValues[i]; 68 String value = nameValues[i + 1]; 69 70 Cookie c = new Cookie (name, value); 71 72 l.add(c); 73 } 74 75 cookies = (Cookie []) l.toArray(new Cookie [l.size()]); 76 } 77 78 MockControl control = newControl(HttpServletRequest .class); 79 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 80 81 request.getCookies(); 82 control.setReturnValue(cookies); 83 84 return request; 85 } 86 87 private void attempt(String name, String expected, String [] nameValues) 88 { 89 HttpServletRequest request = setupRequest(nameValues); 90 91 CookieSourceImpl cs = new CookieSourceImpl(); 92 93 cs.setRequest(request); 94 95 replayControls(); 96 97 String actual = cs.readCookieValue(name); 98 99 assertEquals(expected, actual); 100 101 verifyControls(); 102 } 103 104 public void testNoCookies() 105 { 106 attempt("foo", null, null); 107 } 108 109 public void testMatch() 110 { 111 attempt("fred", "flintstone", new String [] 112 { "barney", "rubble", "fred", "flintstone" }); 113 } 114 115 public void testNoMatch() 116 { 117 attempt("foo", null, new String [] 118 { "bar", "baz" }); 119 } 120 121 public void testWriteCookie() 122 { 123 MockControl requestControl = newControl(HttpServletRequest .class); 124 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 125 126 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 127 128 130 request.getContextPath(); 131 requestControl.setReturnValue("/context"); 132 133 Cookie cookie = new ComparableCookie("foo", "bar"); 134 cookie.setPath("/context"); 135 136 response.addCookie(cookie); 137 138 replayControls(); 139 140 CookieSourceImpl cs = new CookieSourceImpl(); 141 cs.setRequest(request); 142 cs.setResponse(response); 143 144 cs.writeCookieValue("foo", "bar"); 145 146 verifyControls(); 147 } 148 149 public void testRemoveCookie() 150 { 151 MockControl requestControl = newControl(HttpServletRequest .class); 152 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 153 154 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 155 156 158 request.getContextPath(); 159 requestControl.setReturnValue("/context"); 160 161 Cookie cookie = new ComparableCookie("foo", null); 162 cookie.setPath("/context"); 163 164 response.addCookie(cookie); 165 166 replayControls(); 167 168 CookieSourceImpl cs = new CookieSourceImpl(); 169 cs.setRequest(request); 170 cs.setResponse(response); 171 172 cs.removeCookieValue("foo"); 173 174 verifyControls(); 175 } 176 } | Popular Tags |