001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.server.rpc;
017:
018: import com.google.gwt.user.client.rpc.IsSerializable;
019: import com.google.gwt.user.client.rpc.SerializationException;
020:
021: import junit.framework.TestCase;
022:
023: import java.io.BufferedReader;
024: import java.io.ByteArrayInputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.Serializable;
028: import java.io.UnsupportedEncodingException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.security.Principal;
032: import java.util.Enumeration;
033: import java.util.Locale;
034: import java.util.Map;
035: import java.util.Set;
036:
037: import javax.servlet.RequestDispatcher;
038: import javax.servlet.Servlet;
039: import javax.servlet.ServletConfig;
040: import javax.servlet.ServletContext;
041: import javax.servlet.ServletException;
042: import javax.servlet.ServletInputStream;
043: import javax.servlet.http.Cookie;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpSession;
046:
047: /**
048: * Test some of the failure modes associated with
049: * {@link RemoteServiceServlet#doGetSerializationPolicy(HttpServletRequest, String, String)}.
050: *
051: * TODO: test caching of policies?
052: */
053: public class RemoteServiceServletTest extends TestCase {
054:
055: private static class Bar implements Serializable {
056: }
057:
058: private static class Baz {
059: }
060:
061: private static class Foo implements IsSerializable {
062: }
063:
064: private static class MockHttpServletRequest implements
065: HttpServletRequest {
066: private String contextPath;
067:
068: public Object getAttribute(String arg0) {
069: throw new UnsupportedOperationException();
070: }
071:
072: public Enumeration getAttributeNames() {
073: throw new UnsupportedOperationException();
074: }
075:
076: public String getAuthType() {
077: throw new UnsupportedOperationException();
078: }
079:
080: public String getCharacterEncoding() {
081: throw new UnsupportedOperationException();
082: }
083:
084: public int getContentLength() {
085: throw new UnsupportedOperationException();
086: }
087:
088: public String getContentType() {
089: throw new UnsupportedOperationException();
090: }
091:
092: public String getContextPath() {
093: return contextPath;
094: }
095:
096: public Cookie[] getCookies() {
097: throw new UnsupportedOperationException();
098: }
099:
100: public long getDateHeader(String arg0) {
101: throw new UnsupportedOperationException();
102: }
103:
104: public String getHeader(String arg0) {
105: throw new UnsupportedOperationException();
106: }
107:
108: public Enumeration getHeaderNames() {
109: throw new UnsupportedOperationException();
110: }
111:
112: public Enumeration getHeaders(String arg0) {
113: throw new UnsupportedOperationException();
114: }
115:
116: public ServletInputStream getInputStream() throws IOException {
117: throw new UnsupportedOperationException();
118: }
119:
120: public int getIntHeader(String arg0) {
121: throw new UnsupportedOperationException();
122: }
123:
124: public String getLocalAddr() {
125: throw new UnsupportedOperationException();
126: }
127:
128: public Locale getLocale() {
129: throw new UnsupportedOperationException();
130: }
131:
132: public Enumeration getLocales() {
133: throw new UnsupportedOperationException();
134: }
135:
136: public String getLocalName() {
137: throw new UnsupportedOperationException();
138: }
139:
140: public int getLocalPort() {
141: throw new UnsupportedOperationException();
142: }
143:
144: public String getMethod() {
145: throw new UnsupportedOperationException();
146: }
147:
148: public String getParameter(String arg0) {
149: throw new UnsupportedOperationException();
150: }
151:
152: public Map getParameterMap() {
153: throw new UnsupportedOperationException();
154: }
155:
156: public Enumeration getParameterNames() {
157: throw new UnsupportedOperationException();
158: }
159:
160: public String[] getParameterValues(String arg0) {
161: throw new UnsupportedOperationException();
162: }
163:
164: public String getPathInfo() {
165: throw new UnsupportedOperationException();
166: }
167:
168: public String getPathTranslated() {
169: throw new UnsupportedOperationException();
170: }
171:
172: public String getProtocol() {
173: throw new UnsupportedOperationException();
174: }
175:
176: public String getQueryString() {
177: throw new UnsupportedOperationException();
178: }
179:
180: public BufferedReader getReader() throws IOException {
181: throw new UnsupportedOperationException();
182: }
183:
184: public String getRealPath(String arg0) {
185: throw new UnsupportedOperationException();
186: }
187:
188: public String getRemoteAddr() {
189: throw new UnsupportedOperationException();
190: }
191:
192: public String getRemoteHost() {
193: throw new UnsupportedOperationException();
194: }
195:
196: public int getRemotePort() {
197: throw new UnsupportedOperationException();
198: }
199:
200: public String getRemoteUser() {
201: throw new UnsupportedOperationException();
202: }
203:
204: public RequestDispatcher getRequestDispatcher(String arg0) {
205: throw new UnsupportedOperationException();
206: }
207:
208: public String getRequestedSessionId() {
209: throw new UnsupportedOperationException();
210: }
211:
212: public String getRequestURI() {
213: throw new UnsupportedOperationException();
214: }
215:
216: public StringBuffer getRequestURL() {
217: throw new UnsupportedOperationException();
218: }
219:
220: public String getScheme() {
221: throw new UnsupportedOperationException();
222: }
223:
224: public String getServerName() {
225: throw new UnsupportedOperationException();
226: }
227:
228: public int getServerPort() {
229: throw new UnsupportedOperationException();
230: }
231:
232: public String getServletPath() {
233: throw new UnsupportedOperationException();
234: }
235:
236: public HttpSession getSession() {
237: throw new UnsupportedOperationException();
238: }
239:
240: public HttpSession getSession(boolean arg0) {
241: throw new UnsupportedOperationException();
242: }
243:
244: public Principal getUserPrincipal() {
245: throw new UnsupportedOperationException();
246: }
247:
248: public boolean isRequestedSessionIdFromCookie() {
249: throw new UnsupportedOperationException();
250: }
251:
252: public boolean isRequestedSessionIdFromUrl() {
253: throw new UnsupportedOperationException();
254: }
255:
256: public boolean isRequestedSessionIdFromURL() {
257: throw new UnsupportedOperationException();
258: }
259:
260: public boolean isRequestedSessionIdValid() {
261: throw new UnsupportedOperationException();
262: }
263:
264: public boolean isSecure() {
265: throw new UnsupportedOperationException();
266: }
267:
268: public boolean isUserInRole(String arg0) {
269: throw new UnsupportedOperationException();
270: }
271:
272: public void removeAttribute(String arg0) {
273: throw new UnsupportedOperationException();
274: }
275:
276: public void setAttribute(String arg0, Object arg1) {
277: throw new UnsupportedOperationException();
278: }
279:
280: public void setCharacterEncoding(String arg0) {
281: throw new UnsupportedOperationException();
282: }
283: }
284:
285: private class MockServletConfig implements ServletConfig {
286: private ServletContext context;
287:
288: public String getInitParameter(String arg0) {
289: throw new UnsupportedOperationException();
290: }
291:
292: public Enumeration getInitParameterNames() {
293: throw new UnsupportedOperationException();
294: }
295:
296: public ServletContext getServletContext() {
297: return context;
298: }
299:
300: public String getServletName() {
301: throw new UnsupportedOperationException();
302: }
303:
304: void setContext(ServletContext context) {
305: this .context = context;
306: }
307: }
308:
309: private class MockServletContext implements ServletContext {
310: private ServletConfig config;
311: private Throwable exLogged;
312: private String messageLogged;
313:
314: public MockServletContext() {
315: }
316:
317: public Object getAttribute(String arg0) {
318: throw new UnsupportedOperationException();
319: }
320:
321: public Enumeration getAttributeNames() {
322: throw new UnsupportedOperationException();
323: }
324:
325: public ServletContext getContext(String arg0) {
326: throw new UnsupportedOperationException();
327: }
328:
329: public String getInitParameter(String arg0) {
330: throw new UnsupportedOperationException();
331: }
332:
333: public Enumeration getInitParameterNames() {
334: throw new UnsupportedOperationException();
335: }
336:
337: public int getMajorVersion() {
338: throw new UnsupportedOperationException();
339: }
340:
341: public String getMimeType(String arg0) {
342: throw new UnsupportedOperationException();
343: }
344:
345: public int getMinorVersion() {
346: throw new UnsupportedOperationException();
347: }
348:
349: public RequestDispatcher getNamedDispatcher(String arg0) {
350: throw new UnsupportedOperationException();
351: }
352:
353: public String getRealPath(String arg0) {
354: throw new UnsupportedOperationException();
355: }
356:
357: public RequestDispatcher getRequestDispatcher(String arg0) {
358: throw new UnsupportedOperationException();
359: }
360:
361: public URL getResource(String arg0)
362: throws MalformedURLException {
363: throw new UnsupportedOperationException();
364: }
365:
366: public InputStream getResourceAsStream(String arg0) {
367: throw new UnsupportedOperationException();
368: }
369:
370: public Set getResourcePaths(String arg0) {
371: throw new UnsupportedOperationException();
372: }
373:
374: public String getServerInfo() {
375: throw new UnsupportedOperationException();
376: }
377:
378: public Servlet getServlet(String arg0) throws ServletException {
379: throw new UnsupportedOperationException();
380: }
381:
382: public String getServletContextName() {
383: throw new UnsupportedOperationException();
384: }
385:
386: public Enumeration getServletNames() {
387: throw new UnsupportedOperationException();
388: }
389:
390: public Enumeration getServlets() {
391: throw new UnsupportedOperationException();
392: }
393:
394: public void log(Exception arg0, String arg1) {
395: log(arg1, arg0);
396: }
397:
398: public void log(String arg0) {
399: log(arg0, null);
400: }
401:
402: public void log(String arg0, Throwable arg1) {
403: messageLogged = arg0;
404: exLogged = arg1;
405: }
406:
407: public void removeAttribute(String arg0) {
408: }
409:
410: public void setAttribute(String arg0, Object arg1) {
411: throw new UnsupportedOperationException();
412: }
413:
414: void setConfig(ServletConfig config) {
415: this .config = config;
416: }
417: }
418:
419: public void testDoGetSerializationPolicy_FailToOpenMD5Resource()
420: throws ServletException {
421: MockServletConfig mockConfig = new MockServletConfig();
422: MockServletContext mockContext = new MockServletContext() {
423: public InputStream getResourceAsStream(String resource) {
424: return null;
425: }
426: };
427: mockConfig.context = mockContext;
428: mockContext.config = mockConfig;
429:
430: RemoteServiceServlet rss = new RemoteServiceServlet();
431:
432: MockHttpServletRequest mockRequest = new MockHttpServletRequest();
433: rss.init(mockConfig);
434:
435: mockRequest.contextPath = "/MyModule";
436:
437: SerializationPolicy serializationPolicy = rss
438: .doGetSerializationPolicy(mockRequest,
439: "http://www.google.com/MyModule", "12345");
440: assertNull(serializationPolicy);
441: assertNotNull(mockContext.messageLogged);
442: }
443:
444: /**
445: * Test method for
446: * {@link com.google.gwt.user.server.rpc.RemoteServiceServlet#doGetSerializationPolicy(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)}.
447: *
448: * This method tests that if the module path is in a different context than
449: * the RemoteServiceServlet which is processing the request, a message will be
450: * logged and null is returned for the SerializationPolicy.
451: */
452: public void testDoGetSerializationPolicy_ModuleInSeparateServlet()
453: throws ServletException {
454: MockServletConfig mockConfig = new MockServletConfig();
455: MockServletContext mockContext = new MockServletContext();
456: mockConfig.context = mockContext;
457: mockContext.config = mockConfig;
458:
459: RemoteServiceServlet rss = new RemoteServiceServlet();
460:
461: MockHttpServletRequest mockRequest = new MockHttpServletRequest();
462: rss.init(mockConfig);
463:
464: mockRequest.contextPath = "/foo";
465: SerializationPolicy serializationPolicy = rss
466: .doGetSerializationPolicy(mockRequest,
467: "http://www.google.com/MyModule", "");
468: assertNotNull(mockContext.messageLogged);
469: assertNull(serializationPolicy);
470: }
471:
472: /**
473: * Test method for
474: * {@link com.google.gwt.user.server.rpc.RemoteServiceServlet#doGetSerializationPolicy(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)}.
475: *
476: * This method tests the success case. The resource exists and is in the same
477: * path at the web application.
478: */
479: public void testDoGetSerializationPolicy_Success()
480: throws ServletException, SerializationException {
481: final String resourceHash = "12345";
482: final String resourcePath = SerializationPolicyLoader
483: .getSerializationPolicyFileName(resourceHash);
484: MockServletConfig mockConfig = new MockServletConfig();
485: MockServletContext mockContext = new MockServletContext() {
486: public InputStream getResourceAsStream(String resource) {
487: if (resourcePath.equals(resource)) {
488: try {
489: String payLoad = Foo.class.getName()
490: + ",true\n" + Bar.class.getName()
491: + ",false\n";
492: return new ByteArrayInputStream(
493: payLoad
494: .getBytes(SerializationPolicyLoader.SERIALIZATION_POLICY_FILE_ENCODING));
495: } catch (UnsupportedEncodingException e) {
496: return null;
497: }
498: }
499:
500: return null;
501: }
502: };
503: mockConfig.context = mockContext;
504: mockContext.config = mockConfig;
505:
506: RemoteServiceServlet rss = new RemoteServiceServlet();
507:
508: MockHttpServletRequest mockRequest = new MockHttpServletRequest();
509: rss.init(mockConfig);
510:
511: mockRequest.contextPath = "/MyModule";
512:
513: SerializationPolicy serializationPolicy = rss
514: .doGetSerializationPolicy(mockRequest,
515: "http://www.google.com/MyModule", resourceHash);
516: assertNotNull(serializationPolicy);
517:
518: assertDeserializeFields(serializationPolicy, Foo.class);
519: assertValidDeserialize(serializationPolicy, Foo.class);
520:
521: assertDeserializeFields(serializationPolicy, Bar.class);
522: assertNotValidDeserialize(serializationPolicy, Bar.class);
523:
524: assertNotDeserializeFields(serializationPolicy, Baz.class);
525: assertNotValidDeserialize(serializationPolicy, Baz.class);
526: }
527:
528: private void assertDeserializeFields(SerializationPolicy policy,
529: Class clazz) {
530: assertTrue(policy.shouldDeserializeFields(clazz));
531: }
532:
533: private void assertNotDeserializeFields(SerializationPolicy policy,
534: Class clazz) {
535: assertFalse(policy.shouldDeserializeFields(clazz));
536: }
537:
538: private void assertNotValidDeserialize(SerializationPolicy policy,
539: Class clazz) {
540: try {
541: policy.validateDeserialize(clazz);
542: fail("assertNotValidDeserialize: " + clazz.getName()
543: + " failed to throw an exception");
544: } catch (SerializationException e) {
545: // expected
546: }
547: }
548:
549: private void assertValidDeserialize(SerializationPolicy policy,
550: Class clazz) throws SerializationException {
551: policy.validateDeserialize(clazz);
552: }
553: }
|