Skip to content

Commit

Permalink
Prevent mock request to fail (testing only)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed May 6, 2013
1 parent 46f613c commit eaf0fec
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static org.atmosphere.cpr.Action.TYPE.SKIP_ATMOSPHEREHANDLER;
import static org.atmosphere.cpr.ApplicationConfig.MAX_INACTIVE;

import static org.atmosphere.cpr.AtmosphereFramework.AtmosphereHandlerWrapper;
Expand Down Expand Up @@ -236,6 +237,7 @@ Action action(AtmosphereRequest req, AtmosphereResponse res) throws IOException,

req.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, resource);
req.setAttribute(FrameworkConfig.ATMOSPHERE_HANDLER, handlerWrapper.atmosphereHandler);
req.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);

// Globally defined
Action a = invokeInterceptors(config.framework().interceptors(), resource);
Expand All @@ -249,7 +251,9 @@ Action action(AtmosphereRequest req, AtmosphereResponse res) throws IOException,
return a;
}

boolean skipAtmosphereHandler = (Boolean) resource.getRequest().getAttribute(Action.TYPE.SKIP_ATMOSPHEREHANDLER.name());
//Unit test mock the request and will throw NPE.
boolean skipAtmosphereHandler = req.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null
? (Boolean) req.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
if (!skipAtmosphereHandler) {
try {
handlerWrapper.atmosphereHandler.onRequest(resource);
Expand Down Expand Up @@ -284,12 +288,10 @@ private Action invokeInterceptors(List<AtmosphereInterceptor> c, AtmosphereResou
a = Action.CANCELLED;
}

boolean skip = a.type() == Action.TYPE.SKIP_ATMOSPHEREHANDLER;
boolean skip = a.type() == SKIP_ATMOSPHEREHANDLER;
if (skip) {
logger.debug("AtmosphereInterceptor {} asked to skip the AtmosphereHandler", arc);
r.getRequest().setAttribute(Action.TYPE.SKIP_ATMOSPHEREHANDLER.name(), Boolean.TRUE);
} else if (r.getRequest().getAttribute(Action.TYPE.SKIP_ATMOSPHEREHANDLER.name()) == null) {
r.getRequest().setAttribute(Action.TYPE.SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
r.getRequest().setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.TRUE);
}

if (a.type() != Action.TYPE.CONTINUE) {
Expand Down Expand Up @@ -427,7 +429,7 @@ public Action resumed(AtmosphereRequest request, AtmosphereResponse response)
* {@link Action}, tells the proprietary Comet {@link Servlet}
* to resume (again), suspended or do nothing with the current {@link AtmosphereResponse}.
*
* @param req the {@link AtmosphereRequest}
* @param req the {@link AtmosphereRequest}
* @param res the {@link AtmosphereResponse}
* @return action the Action operation.
* @throws java.io.IOException
Expand Down Expand Up @@ -468,7 +470,7 @@ protected boolean trackActiveRequest(AtmosphereRequest req) {
/**
* Cancel or times out an {@link AtmosphereResource} by invoking it's associated {@link AtmosphereHandler#onStateChange(AtmosphereResourceEvent)}
*
* @param r an {@link AtmosphereResource}
* @param r an {@link AtmosphereResource}
* @param cancelled true if cancelled, false if timedout
* @return true if the operation was executed.
*/
Expand Down Expand Up @@ -529,6 +531,7 @@ protected boolean completeLifecycle(final AtmosphereResource r, boolean cancelle

/**
* Invoke the associated {@link AtmosphereHandler}. This method must be synchronized on an AtmosphereResource
*
* @param r a {@link AtmosphereResourceImpl}
* @throws IOException
*/
Expand Down

0 comments on commit eaf0fec

Please sign in to comment.