From ced74e18895716d8a7326dfac62061a021ddcb3f Mon Sep 17 00:00:00 2001 From: Imran M Yousuf Date: Tue, 17 Feb 2009 15:07:52 +0600 Subject: [PATCH] Get test to work with authentication Signed-off-by: Imran M Yousuf --- .../ws/server/BookStoreResourcesTest.java | 25 +++++++++++++------ 1 files changed, 17 insertions(+), 8 deletions(-) diff --git a/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/BookStoreResourcesTest.java b/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/BookStoreResourcesTest.java index b3ad4b8..d9b6f52 100644 --- a/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/BookStoreResourcesTest.java +++ b/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/BookStoreResourcesTest.java @@ -18,6 +18,7 @@ import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.WebResource.Builder; import com.sun.jersey.client.apache.ApacheHttpClient; +import com.sun.jersey.client.apache.config.ApacheHttpClientConfig; import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig; import com.sun.jersey.core.util.MultivaluedMapImpl; import java.io.File; @@ -73,10 +74,6 @@ public class BookStoreResourcesTest new File("src/test/webapp/WEB-INF/unitTestWeb.xml"), Collections.singleton(new File("target/classes").toURI().toURL())); glassfish.deploy(war); - DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig(); - clientConfig.getState().setCredentials(null, null, -1, "modhu", "modhu"); - client = ApacheHttpClient.create(clientConfig); - webResource = client.resource(BASE_URI); } @Override @@ -123,12 +120,12 @@ public class BookStoreResourcesTest for (int j = 0; j < count; ++j) { for (int i = 0; i < 2; ++i) { final Builder type = - webResource.path("book").type("application/xml"); + getWebResource().path("book").type("application/xml"); type.post(bookElement); } try { final Builder type = - webResource.path("book").type("application/xml"); + getWebResource().path("book").type("application/xml"); type.post(bookElement); fail("Should not succeed!"); } catch (Exception ex) { @@ -265,14 +262,16 @@ public class BookStoreResourcesTest houseElement.setPublicationHouse(house); for (int j = 0; j < count; ++j) { + System.out.println("Iteration: " + j); for (int i = 0; i < 2; ++i) { + System.out.println("Iteration(" + j + ") " + i); final Builder type = - webResource.path("pub-house").type("application/xml"); + getWebResource().path("pub-house").type("application/xml"); type.post(houseElement); } try { final Builder type = - webResource.path("pub-house").type("application/xml"); + getWebResource().path("pub-house").type("application/xml"); type.post(houseElement); fail("Should not succeed!"); } catch (Exception ex) { @@ -294,6 +293,16 @@ public class BookStoreResourcesTest type.put(element); } + private WebResource getWebResource() { + DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig(); + clientConfig.getState().setCredentials(null, null, -1, "modhu", "modhu"); + clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_PREEMPTIVE_AUTHENTICATION, + Boolean.TRUE); + client = ApacheHttpClient.create(clientConfig); + webResource = client.resource(BASE_URI); + return webResource; + } + private void verifyBookElements(final BookElements elements, final String name, final String isbn) { assertTrue(elements.getBooks().size() > 0); for (Book book : elements.getBooks()) { -- 1.5.6