Package com.googlecode.gwt.test.internal.patchers

Source Code of com.googlecode.gwt.test.internal.patchers.AbstractRemoteServiceServletPatcher

package com.googlecode.gwt.test.internal.patchers;

import java.lang.reflect.Method;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet;
import com.googlecode.gwt.test.exceptions.GwtTestRpcException;
import com.googlecode.gwt.test.internal.GwtConfig;
import com.googlecode.gwt.test.patchers.InitMethod;
import com.googlecode.gwt.test.patchers.PatchClass;
import com.googlecode.gwt.test.patchers.PatchMethod;
import com.googlecode.gwt.test.rpc.ServletMockProvider;

@PatchClass(AbstractRemoteServiceServlet.class)
public class AbstractRemoteServiceServletPatcher {

   public static Method currentCalledMethod;

   public static ServletMockProvider ensureServletMockProvider(
            AbstractRemoteServiceServlet servlet, String methodName) {
      ServletMockProvider servletMockProvider = GwtConfig.get().getModuleRunner().getServletMockProvider();
      if (servletMockProvider == null) {
         throw new GwtTestRpcException("Illegal call to " + servlet.getClass().getName() + "."
                  + methodName + " : You have to set a valid "
                  + ServletMockProvider.class.getSimpleName() + " instance through "
                  + GwtConfig.get().getModuleRunner().getClass().getSimpleName()
                  + ".setServletMockProvider(..) method");
      }

      return servletMockProvider;
   }

   @InitMethod
   static void addMockedGetServletConfigMethod(CtClass c) throws CannotCompileException {

      StringBuilder sb = new StringBuilder();
      sb.append("public javax.servlet.ServletConfig getServletConfig() { return ");
      sb.append(AbstractRemoteServiceServletPatcher.class.getName()).append(
               ".ensureServletMockProvider(this, \"getServletConfig()\").getMockedConfig(this); }");

      CtMethod m = CtMethod.make(sb.toString(), c);
      c.addMethod(m);
   }

   @PatchMethod
   static HttpServletRequest getThreadLocalRequest(AbstractRemoteServiceServlet servlet) {

      return ensureServletMockProvider(servlet, "getThreadLocalRequest()").getMockedRequest(
               servlet, currentCalledMethod);
   }

   @PatchMethod
   static HttpServletResponse getThreadLocalResponse(AbstractRemoteServiceServlet servlet) {

      return ensureServletMockProvider(servlet, "getThreadLocalResponse()").getMockedResponse(
               servlet, currentCalledMethod);
   }

}
TOP

Related Classes of com.googlecode.gwt.test.internal.patchers.AbstractRemoteServiceServletPatcher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.