开发者

How do you get JUnit to model multiple tabs/windows opened but only one session

I'm writing unit tests for Spring Framework 2.5 using JUnit. More specifically, I'm using the MockHttpServletRequest to test my controllers. I'm running into a problem, testing a particular scenario.

An app was editing objects, persisted to Hibernate. The objects work something like this:

Class Workflow {
  ... various properties and methods
  Long workflowId;  // primary key for this object
  Long subclassId;  // foreign key to another class
  String subclassType; // what kind of other class?
}

Class Request {
  .. various other properties and methods
  Long subclassId; // primary key for this object
  Long workflowId; // foreign key to Workflow
}

Workflow is always attached to one, and only one, Request and a Request is always attached to one, and only one Workflow. However, there are multiple objects/tables which Workflow can link to, so the subclassType has to keep track of what other table to look in. So, this is a little bit like Request inheriting from/implementing Workflow, but the database stores them in separate tables so the Java was implemented as separate classes.

I didn't write this mess. I'm maintaining it. And it's in wide use, with several dif开发者_StackOverflowferent applications and subclassTypes so there are limits to how much "cleanup" I can do.

The web app in question was putting the Workflow object into HttpSession but editing the Request in a form. When someone opened up multiple windows/tabs, with a different Request in each window, the Workflow object from the last one opened was overwriting any prior-opened ones (sessions are attached to cookies, only one set of cookies for the domain, shared by ALL windows in the web browser). If they submitted the form for the first Request, the Workflow object from the last one (pulled from the HttpSession) was written with the submitted Request (NOT the correct one), corrupting the data in the database.

We ended up with Requests which had multiple Workflows connecting to them, and Requests which had NO Workflows connecting to them.

The obvious fix was to rewrite the app so it never stores the Workflow in the HttpSession. workflowId is one of the fields in the Request; we persisted that to the form via a hidden field. Yes, we hit the database more often, but we aren't cross-connecting our records.

That's the situation. Now, the question: how do you model this in JUnit? So that no one else, in the future, runs afoul of this situation again?

We use a MockHttpServletRequest, plugging in form values and submitting them by using a AnnotationMethodHandlerAdapter and a Controller object. As an example:

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    AnnotationMethodHandlerAdapter handler = new AnnotationMethodHandlerAdaptor();
    RequestFormController controller = new RequestFormController();

    request.setMethod("GET");
    request.setRequestURI("/EditRequest.do"); // handled by the RequestFormController

    request.setParameter("wfId", Long.toString(wfid)); // wfId is the workflow ID of a valid Workflow with an attached Request
    ModelAndView mv = handler.handle(request, response, controller);

    request.setParameter("wfId", mv.getModel().get("wfId");
    request.setParameter("requestText", "blah blah blah");

    request.setMethod("POST");
    request.setRequestURI("/UpdateRequest.do"); // handled by the RequestFormController
    mv = handler.handle(request, response, controller);

This is all pretty straightforward, Spring/JUnit stuff. The problem is that, to open something in two different tabs/windows, you have to have two different requests, each of which has its own HttpSession. With a web browser, though, those two different requests share the same cookies, which means they share the same session information. How do you get JUnit to model that?

I can copy the cookies from one to the other, but that doesn't seem to expose the error. MockHttpServletRequest is still keeping the HttpSession information separate. I'm having a hard time copying the HttpSession Attributes from one to the other. Insomuch as I've done that, I can't seem to get that to expose the error, either.

Any suggestions? This is a very real problem, and MockHttpServletRequest and all the other JUnit-related things we're doing aren't, accurately, modeling the real world of Tomcat and web browsers.

Peace.


When writing your test you can explicity create a session and set all requests to use the same session.

MockHttpSession session = new MockHttpSession();
request.setSession(session);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜