users@jersey.java.net

[Jersey] Re: Using ApacheHttpClient in a JerseyTest

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Wed, 09 Feb 2011 10:22:09 +0100

That would be a solution, but I'd like it to be more consistent with
existing code.. currently I'm inclining to create ClientFactory
interface/abstract class - something similar to TestContainerFactory..

I should be able to resolve this issue
(http://java.net/jira/browse/JERSEY-643) by end of the week, so stay
tuned. Fix should be available in 1.6-ea03.

Pavel

On 02/08/2011 09:19 PM, Ryan Stewart wrote:
> At a minimum, can't we just get getClient() to be protected instead of
> private and static? There's no need for it to be static, and if it's
> protected, then it can be very simply overridden to return the proper
> type of client.
>
> On Mon, Feb 7, 2011 at 9:28 AM, Pavel Bucek <pavel.bucek_at_oracle.com
> <mailto:pavel.bucek_at_oracle.com>> wrote:
>
> Hello,
>
> excellent question, thanks for it.
>
> Unfortunately, there is no simple way how to do it now. I tried it
> but I ended with almost all overridden methods from JerseyTest,
> which is certainly not good solution. Main problem here is that
> webresource() method doesn't use client(), but internally saved
> instance in private field, to which you don't have access.
>
> There is definitely room (and need) for improvement, please file
> RFE issue for this..
>
> What I'd recommend to do now is just copy JerseyTest class source
> [1], rename it to something like ApacheClientJerseyTest and modify
> getClient to something like this:
>
> private static Client getClient(TestContainer tc, AppDescriptor
> ad) {
> Client c = ApacheHttpClient.create(ad.getClientConfig());
>
> //check if logging is required
> boolean enableLogging =
> (System.getProperty("enableLogging") != null)
> ? true
> : false;
>
> if (enableLogging) {
> c.addFilter(new LoggingFilter());
> }
>
> return c;
> }
>
>
> And your tests should extend this newly created class..
>
> Pavel
>
> [1]
> http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-test-framework/jersey-test-framework-core/src/main/java/com/sun/jersey/test/framework/JerseyTest.java?rev=4574
>
>
>
>
> On 02/07/2011 02:08 PM, Damiano ALBANI wrote:
>
> Hello,
>
> I'd like to use ApacheHttpClient in my JerseyTest-based test
> suite, to leverage its cookie management capabilities in
> particular.
> The Java code for my test class looks like this:
>
> protected AppDescriptor configure() {
> return
> new WebAppDescriptor.Builder("fr.univNantes.spin")
> .clientConfig(
> new DefaultApacheHttpClientConfig() {
> {
>
> this.getClasses().add(JAXBContextResolver.class);
>
> this.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES,
> true);
>
> this.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
> }
> }
> )
> .build();
> }
>
> protected TestContainerFactory getTestContainerFactory() {
> return new ExternalTestContainerFactory();
> }
>
> @Test
> public void test_login() {
> ClientResponse response =
> resource().path("login").get(ClientResponse.class);
> }
>
> But that doesn't make it use an ApacheHttpClient at all.
> It looks like the culprit is
> "JerseyTest::getClient(TestContainer, AppDescriptor)", which
> always returns a client based on URLConnectionClientHandler...
>
> So where/how can I plug in an Apache client in there?
>
> Thanks a lot.
>
>
>