Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.shindig.gadgets.servlet;
 
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.isA;
 import static org.easymock.classextension.EasyMock.createMock;
 import static org.easymock.classextension.EasyMock.replay;
 import static org.easymock.classextension.EasyMock.verify;
 
 
 
 
 import  org.json.JSONException;
 import  org.json.JSONObject;

Tests for RpcServlet.
 
 public class RpcServletTest extends TestCase {
   private RpcServlet servlet;
   private JsonRpcHandler handler;
 
   @Override
   public void setUp() throws Exception {
     super.setUp();
      = new RpcServlet();
      = createMock(JsonRpcHandler.class);
   }
 
   public void testDoGetNormal() throws Exception {
     HttpServletRequest request = createGetRequest("{\"gadgets\":[]}",
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._");
     HttpServletResponse response = createHttpResponse("Content-Disposition",
         "attachment;filename=rpc.txt""application/json; charset=utf-8",
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._({\"GADGETS\":[]})",
         .);
     JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}");
     expect(.process(isA(JSONObject.class))).andReturn(handlerResponse);
     replay();
     .doGet(requestresponse);
     verify(response);
   }
 
   public void testDoGetWithHandlerRpcException() throws Exception {
     HttpServletRequest request = createGetRequest("{\"gadgets\":[]}""function");
     HttpServletResponse response = createHttpResponse("rpcExceptionMessage",
     expect(.process(isA(JSONObject.class))).andThrow(
         new RpcException("rpcExceptionMessage"));
     replay();
     .doGet(requestresponse);
     verify(response);
   }
 
   public void testDoGetWithHandlerJsonException() throws Exception {
     HttpServletRequest request = createGetRequest("{\"gadgets\":[]}""function");
     HttpServletResponse response = createHttpResponse("Malformed JSON request.",
     expect(.process(isA(JSONObject.class))).andThrow(new JSONException("json"));
     replay();
     .doGet(requestresponse);
     verify(response);
   }
 
   public void testDoGetWithMissingReqParam() throws Exception {
     HttpServletRequest request = createGetRequest(null"function");
     .doGet(requestresponse);
     verify(response);
   }
 
   public void testDoGetWithMissingCallbackParam() throws Exception {
     HttpServletRequest request = createGetRequest("{\"gadgets\":[]}"null);
     .doGet(requestresponse);
     verify(response);
  }
  public void testDoGetWithBadCallbackParamValue() throws Exception {
    HttpServletRequest request = createGetRequest("{\"gadgets\":[]}""/'!=");
    .doGet(requestresponse);
    verify(response);
  }
  private HttpServletRequest createGetRequest(String reqParamValueString callbackParamValue) {
    expect(result.getMethod()).andReturn("GET").anyTimes();
    expect(result.getCharacterEncoding()).andReturn("UTF-8").anyTimes();
        .andReturn(reqParamValue).anyTimes();
        .andReturn(callbackParamValue).anyTimes();
    replay(result);
    return result;
  }
  private HttpServletResponse createHttpResponse(String responseint httpStatusCode)
    throws IOException {
    return createHttpResponse(nullnullnullresponsehttpStatusCode);
  }
  private HttpServletResponse createHttpResponse(String header1String header2,
      String contentTypeString responseint httpStatusCodethrows IOException {
    PrintWriter writer = createMock(PrintWriter.class);
    if (response != null) {
      expect(result.getWriter()).andReturn(writer);
      writer.write(response);
    }
    if (header1 != null && header2 != null) {
      result.setHeader(header1header2);
    }
    if (contentType != null) {
      result.setContentType(contentType);
    }
    result.setStatus(httpStatusCode);
    replay(resultwriter);
    return result;
  }
New to GrepCode? Check out our FAQ X