From: Bill L. <bi...@ji...> - 2003-07-17 13:38:17
|
Guys, I took Steve's advice and created my own extention to MockHttpServletRequest - my first attempt is below: private class MyMockHttpServletRequest extends MockHttpServletRequest { private Hashtable attributes; public JiveMockHttpServletRequest() { attributes = new Hashtable(); } public Object getAttribute(String s) { return attributes.get(s); } public Enumeration getAttributeNames() { return attributes.elements(); } public void setAttribute(String s, Object o) { attributes.put(s,o); } } After doing this and reading Jeff's mail I implemented it with a ReturnObjectBag - however, I don't see what that buys me: private class MyMockHttpServletRequest extends MockHttpServletRequest { private ReturnObjectBag attributes; public MyMockHttpServletRequest() { attributes = new ReturnObjectBag("attributes"); } public Object getAttribute(String s) { return attributes.getHashTable().get(s); } public Enumeration getAttributeNames() { return attributes.getHashTable().elements(); } public void setAttribute(String s, Object o) { attributes.getHashTable().put(s,o); } } Is there a benefit to either approach? Both work perfectly in my test case and I'm more inclined to go with the first approach just because it's simpler (and the ReturnObjectBag does nothing for me in this case). Cheers, --Bill Jeff Martin wrote: > Should probably be a ReturnObjectBag as this lets you setup a list of > return values for a key. > > > On Thu, 2003-07-17 at 08:47, Steve Freeman wrote: > >>This implementation /is/ a little hazy. >> >>The idea is that you should preload your attributes in the order in >>which they are requested, and then they'll just be returned as the code >>runs. It's not actually doing a lookup. Checking which attributes are >>asked for is actually a separate concept and handled by a different >>expectation. There are some arguments for doing this but it does look a >>little clumsy. >> >>If you don't like this, I suggest you replace this part of the mock >>request with an ExpectationMap which might be closer to the behaviour >>you want. >> >>S. >> >>Bill Lynch wrote: >> >>>All, >>> >>>I haven't been able to figure out how to set attributes in a >>>MockHttpServletRequest. I searched the mailing list archives for clues, >>>but didn't find any (and there's no javadocs for that class!). >>> >>>The method that looks like it might work only takes one param: >>> >>>request.setupGetAttribute(Object) >>> >>>I'd expect that to be >>> >>>request.setupGetAttribute(String, Object) >>> >>>I've also tried passing in a vector of name/value pairs to that method, >>>but when I call getAttribute(String) on that request object I get this >>>error: >>> >>>There was 1 failure: >>>1) >>>testGetAttribute(com.foo.util.MyTest)junit.framework.AssertionFailedError: >>>attributes has run out of objects. >>> at >>>com.mockobjects.ReturnObjectList.nextReturnObject(ReturnObjectList.java:61) >>> at >>>com.mockobjects.servlet.MockHttpServletRequest.getAttribute(MockHttpServletRequest.java:58) |