From: Bill L. <bi...@ji...> - 2003-07-17 16:50:30
|
Jeff, >>Thanks for the advice - I can change that code (I'm new to the class). I >>have a few more questions: >> >>1) Why should I keep using a ReturnObjectBag as an internal object? In >>my example, does it its use benefit me, or can I use it in a better way? > > Simplest (not best) example is this. How can you tell if your code is > doing this. > > for(int i=0;i<100000;i++){ > att = request.getAttribute("name"); > } > > or this > > att = request.getAttribute("name"); So in the ReturnObjectBag's internal ReturnObjectList remove the object it returns from the list? >>2) If I shouldn't mess with the internal Hashtable, why is it exposed? >>Seems like poor design then. > > Not bad design, just unusual environmental pressures, it's probably > vestigial by now. Gotcha. >>3) Any reason there's no Javadocs for any of these classes? :) Having >>better documentation would have steered me away from using the internal >>hashtable probably. > > cough, > http://mockobjects.sourceforge.net/javadoc/1.4/com/mockobjects/ReturnObjectBag.html Woa, my bad! Clearly I didn't dig deep enough. :) Thanks, --Bill >>Nat Pryce wrote: >> >> >>>Try using the putObjectToReturn and getNextReturnObject methods rather than >>>messing around with the ReturnObjectBag's internal hash table. >>> >>>Cheers, >>> Nat. >> >> > >> >>>----- Original Message ----- >>>From: Bill Lynch >>>To: <moc...@li...> >>>Sent: Thursday, July 17, 2003 2:37 PM >>>Subject: Re: [MO-java-users] setting attributes in a mock request >>> >>> >>>>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:6 >>> >>>1) >>> >>> >>>>>>> at >>>> >>>>>>com.mockobjects.servlet.MockHttpServletRequest.getAttribute(MockHttpServl >>> >>>etRequest.java:58) >>> >> >> >> >>------------------------------------------------------- >>This SF.net email is sponsored by: VM Ware >>With VMware you can run multiple operating systems on a single machine. >>WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the >>same time. Free trial click here: http://www.vmware.com/wl/offer/345/0 >>_______________________________________________ >>Mockobjects-java-users mailing list >>Moc...@li... >>https://lists.sourceforge.net/lists/listinfo/mockobjects-java-users -- ---------------------------------- Bill Lynch, Jive Software bi...@ji... |