users@jersey.java.net

[Jersey] Re: Using ApacheHttpClient in a JerseyTest

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 9 Feb 2011 10:35:13 +0100

On Feb 8, 2011, at 9: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.
>

Good idea.

We should also be careful about the logging triggered by the system
property. It should be done in a separate place with checks to see if
a LoggingFilter is already added (since the implementation may not
create a new instance of Client, or the implementation may add it).

Paul.

> On Mon, Feb 7, 2011 at 9:28 AM, Pavel Bucek <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.
>
>
>