| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.test; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.ioc.annotations.Inject; |
| 18 | |
import org.apache.tapestry5.ioc.internal.util.CollectionFactory; |
| 19 | |
import org.apache.tapestry5.ioc.internal.util.InternalUtils; |
| 20 | |
import org.apache.tapestry5.services.Session; |
| 21 | |
|
| 22 | |
import java.util.*; |
| 23 | |
|
| 24 | |
public class TestableRequestImpl implements TestableRequest |
| 25 | |
{ |
| 26 | |
private final String contextPath; |
| 27 | |
|
| 28 | 54 | private final Map<String, Object> parameters = CollectionFactory.newMap(); |
| 29 | |
|
| 30 | 54 | private final Map<String, Object> attributes = CollectionFactory.newMap(); |
| 31 | |
|
| 32 | |
private Session session; |
| 33 | |
|
| 34 | 54 | private String path = "/"; |
| 35 | |
|
| 36 | 54 | private Locale locale = Locale.getDefault(); |
| 37 | |
|
| 38 | |
@Inject |
| 39 | |
public TestableRequestImpl() |
| 40 | |
{ |
| 41 | 46 | this("/foo"); |
| 42 | 46 | } |
| 43 | |
|
| 44 | |
public TestableRequestImpl(String contextPath) |
| 45 | 54 | { |
| 46 | 54 | this.contextPath = contextPath; |
| 47 | 54 | } |
| 48 | |
|
| 49 | |
public TestableRequest clear() |
| 50 | |
{ |
| 51 | 72 | parameters.clear(); |
| 52 | |
|
| 53 | 72 | return this; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public TestableRequest setPath(String path) |
| 57 | |
{ |
| 58 | 72 | this.path = path; |
| 59 | |
|
| 60 | 72 | return this; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public TestableRequest setLocale(Locale locale) |
| 64 | |
{ |
| 65 | 52 | this.locale = locale; |
| 66 | |
|
| 67 | 52 | return this; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public TestableRequest loadParameter(String parameterName, String parameterValue) |
| 71 | |
{ |
| 72 | 8 | Object existing = parameters.get(parameterName); |
| 73 | |
|
| 74 | 8 | if (existing == null) |
| 75 | |
{ |
| 76 | 8 | parameters.put(parameterName, parameterValue); |
| 77 | 8 | return this; |
| 78 | |
} |
| 79 | |
|
| 80 | 0 | if (existing instanceof List) |
| 81 | |
{ |
| 82 | 0 | ((List) existing).add(parameterValue); |
| 83 | 0 | return this; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | 0 | List list = new ArrayList(); |
| 89 | 0 | list.add(existing); |
| 90 | 0 | list.add(parameterValue); |
| 91 | |
|
| 92 | 0 | parameters.put(parameterName, list); |
| 93 | |
|
| 94 | 0 | return this; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public TestableRequest overrideParameter(String parameterName, String parameterValue) |
| 98 | |
{ |
| 99 | 10 | parameters.put(parameterName, parameterValue); |
| 100 | |
|
| 101 | 10 | return this; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public long getDateHeader(String name) |
| 105 | |
{ |
| 106 | 0 | return 0; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public String getHeader(String name) |
| 113 | |
{ |
| 114 | 0 | return null; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public List<String> getHeaderNames() |
| 121 | |
{ |
| 122 | 0 | return Collections.emptyList(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public Locale getLocale() |
| 126 | |
{ |
| 127 | 72 | return locale; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public List<String> getParameterNames() |
| 131 | |
{ |
| 132 | 0 | return InternalUtils.sortedKeys(parameters); |
| 133 | |
} |
| 134 | |
|
| 135 | |
public String[] getParameters(String name) |
| 136 | |
{ |
| 137 | 8 | Object value = parameters.get(name); |
| 138 | |
|
| 139 | 8 | if (value == null) return null; |
| 140 | |
|
| 141 | 8 | if (value instanceof String) |
| 142 | 8 | return new String[] { (String) value }; |
| 143 | |
|
| 144 | 0 | List list = (List) value; |
| 145 | |
|
| 146 | 0 | return (String[]) list.toArray(new String[list.size()]); |
| 147 | |
} |
| 148 | |
|
| 149 | |
public String getPath() |
| 150 | |
{ |
| 151 | 422 | return path; |
| 152 | |
} |
| 153 | |
|
| 154 | |
public String getContextPath() |
| 155 | |
{ |
| 156 | 334 | return contextPath; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public String getParameter(String name) |
| 160 | |
{ |
| 161 | 60 | Object value = parameters.get(name); |
| 162 | |
|
| 163 | 60 | if (value == null || value instanceof String) return (String) value; |
| 164 | |
|
| 165 | 0 | List<String> list = (List<String>) value; |
| 166 | |
|
| 167 | 0 | return list.get(0); |
| 168 | |
} |
| 169 | |
|
| 170 | |
public Session getSession(boolean create) |
| 171 | |
{ |
| 172 | 118 | if (!create) return session; |
| 173 | |
|
| 174 | 62 | if (session == null) session = new PageTesterSession(); |
| 175 | |
|
| 176 | 62 | return session; |
| 177 | |
} |
| 178 | |
|
| 179 | |
public void setEncoding(String requestEncoding) |
| 180 | |
{ |
| 181 | 0 | } |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public boolean isXHR() |
| 187 | |
{ |
| 188 | 58 | return false; |
| 189 | |
} |
| 190 | |
|
| 191 | |
public boolean isSecure() |
| 192 | |
{ |
| 193 | 146 | return false; |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public boolean isRequestedSessionIdValid() |
| 200 | |
{ |
| 201 | 0 | return true; |
| 202 | |
} |
| 203 | |
|
| 204 | |
public Object getAttribute(String name) |
| 205 | |
{ |
| 206 | 60 | return attributes.get(name); |
| 207 | |
} |
| 208 | |
|
| 209 | |
public void setAttribute(String name, Object value) |
| 210 | |
{ |
| 211 | 0 | attributes.put(name, value); |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public String getServerName() |
| 218 | |
{ |
| 219 | 0 | return "localhost"; |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public String getMethod() |
| 226 | |
{ |
| 227 | 8 | return "POST"; |
| 228 | |
} |
| 229 | |
} |