cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r743825 [2/2] - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main...
Date Thu, 12 Feb 2009 18:26:09 GMT
Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookNotFoundFault.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookNotFoundFault.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookNotFoundFault.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookNotFoundFault.java Thu
Feb 12 18:26:07 2009
@@ -25,6 +25,10 @@
 public class BookNotFoundFault extends Exception {
     private BookNotFoundDetails details;
 
+    public BookNotFoundFault(String errorMessage) {
+        super(errorMessage);
+    }
+    
     public BookNotFoundFault(BookNotFoundDetails details) {
         super();
         this.details = details;

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreJaxrsJaxws.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreJaxrsJaxws.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreJaxrsJaxws.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreJaxrsJaxws.java
Thu Feb 12 18:26:07 2009
@@ -22,6 +22,7 @@
 import javax.jws.WebMethod;
 import javax.jws.WebParam;
 import javax.jws.WebService;
+import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -30,16 +31,21 @@
 
 @WebService
 @Path("/bookstore")
+@Consumes("application/xml")
+@Produces("application/xml")
 public interface BookStoreJaxrsJaxws {
     
     @WebMethod
     @GET
     @Path("/{id}")
-    Book getBook(@PathParam("id") @WebParam(name = "id") Long id);
+    Book getBook(@PathParam("id") @WebParam(name = "id") Long id) throws BookNotFoundFault;
 
     @WebMethod
     @POST
     @Path("/books")
-    @Produces("application/xml")
     Book addBook(@WebParam(name = "book") Book book);
+    
+    @Path("/books/{id}")
+    @WebMethod(exclude = true)
+    BookSubresource getBookSubresource(@PathParam("id") String id);
 }

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSoapRestImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSoapRestImpl.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSoapRestImpl.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSoapRestImpl.java
Thu Feb 12 18:26:07 2009
@@ -23,7 +23,10 @@
 import java.util.Map;
 
 import javax.annotation.Resource;
+import javax.jws.WebMethod;
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 import javax.xml.ws.WebServiceContext;
 
 import org.apache.cxf.jaxrs.ext.MessageContext;
@@ -42,6 +45,11 @@
     }
     
     public Book getBook(Long id) {
+        if (books.get(id) == null) {
+            Response r = Response.status(404).header("BOOK-HEADER", 
+                "No Book with id " + id + " is available").build();
+            throw new WebApplicationException(r);
+        }
         System.out.println(getContentType());
         return books.get(id);
     }
@@ -70,5 +78,10 @@
         }
         return request.getContentType();
     }
+
+    @WebMethod(exclude = true)
+    public BookSubresource getBookSubresource(String id) {
+        return new BookSubresourceImpl(Long.valueOf(id));
+    }
     
 }

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java Thu
Feb 12 18:26:07 2009
@@ -19,9 +19,16 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.CookieParam;
+import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 
 import org.apache.cxf.customer.book.BookNotFoundFault;
 
@@ -31,4 +38,22 @@
     @Path("/subresource")
     @Produces("application/xml")
     Book getTheBook() throws BookNotFoundFault;
+    
+    @POST
+    @Path("/subresource2/{n1}")
+    @Consumes("text/plain")
+    @Produces("application/xml")
+    Book getTheBook2(@PathParam("n1") String name1,
+                     @QueryParam("n2") String name2,
+                     @QueryParam("n3") String name3,
+                     @HeaderParam("N4") String name4,
+                     @CookieParam("n5") String name5,
+                     String name6) throws BookNotFoundFault;
+    
+    @POST
+    @Path("/subresource3")
+    Book getTheBook3(@FormParam("id") String id,
+                     @FormParam("name") String name) throws BookNotFoundFault;
+    
 }
+

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresourceImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresourceImpl.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresourceImpl.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresourceImpl.java
Thu Feb 12 18:26:07 2009
@@ -23,11 +23,43 @@
 
 public class BookSubresourceImpl implements BookSubresource {
 
+    private Long id;
+    
+    public BookSubresourceImpl() {
+        id = 123L;
+    }
+    
+    public BookSubresourceImpl(Long id) {
+        this.id = id;
+    }
+    
     public Book getTheBook() throws BookNotFoundFault {
+        
+        if (id == 0) {
+            return null;
+        }
+        
         Book b = new Book();
-        b.setId(123);
+        b.setId(id);
         b.setName("CXF in Action");
         return b;
     }
 
+    public Book getTheBook2(String n1, String n2, String n3, String n4, String n5, String
n6) 
+        throws BookNotFoundFault {
+        
+        Book b = new Book();
+        b.setId(id); 
+        b.setName(n1 + n2 + n3 + n4 + n5 + n6);
+        return b;
+    }
+    
+    public Book getTheBook3(String sid, String name) throws BookNotFoundFault {
+        Book b = new Book();
+        
+        b.setId(Long.valueOf(sid)); 
+        b.setName(name);
+        return b;
+    }
+
 }

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java Thu
Feb 12 18:26:07 2009
@@ -23,7 +23,15 @@
 import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
 
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
 import javax.xml.namespace.QName;
 
 import org.apache.commons.httpclient.HttpClient;
@@ -32,7 +40,14 @@
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.ext.form.Form;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -63,6 +78,196 @@
     }
     
     @Test
+    public void testGetBook123Client() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                                  BookStoreJaxrsJaxws.class);
+        Book b = proxy.getBook(new Long("123"));
+        assertEquals(123, b.getId());
+        assertEquals("CXF in Action", b.getName());
+    }
+    
+    @Test
+    public void testGetBook123WebClient() throws Exception {
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        WebClient client = new WebClient(baseAddress);
+        client.path("/bookstore/123").accept(MediaType.APPLICATION_XML_TYPE);
+        Book b = client.get(Book.class);
+        assertEquals(123, b.getId());
+        assertEquals("CXF in Action", b.getName());
+    }
+    
+    @Test
+    public void testNoBookWebClient() throws Exception {
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        WebClient client = new WebClient(baseAddress);
+        client.path("/bookstore/books/0/subresource").accept(MediaType.APPLICATION_XML_TYPE);
+        Book b = client.get(Book.class);
+        assertNull(b);
+        assertEquals(204, client.getResponse().getStatus());
+    }
+    
+    @Test
+    public void testGetBook123WebClientResponse() throws Exception {
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        WebClient client = new WebClient(baseAddress);
+        client.path("/bookstore/123").accept(MediaType.APPLICATION_XML_TYPE);
+        Book b = readBook((InputStream)client.get().getEntity());
+        assertEquals(123, b.getId());
+        assertEquals("CXF in Action", b.getName());
+    }
+    
+    @Test
+    public void testGetBook356ClientException() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                              BookStoreJaxrsJaxws.class);
+        
+        ProviderFactory.getInstance().registerUserProvider(new TestResponseExceptionMapper());
+        try {
+            proxy.getBook(356L);
+            fail();
+        } catch (BookNotFoundFault ex) {
+            assertEquals("No Book with id 356 is available", ex.getMessage());
+        }
+    }
+    
+    
+    @Test
+    public void testGetBookSubresourceClient() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                                  BookStoreJaxrsJaxws.class);
+        BookSubresource bs = proxy.getBookSubresource("125");
+        Book b = bs.getTheBook();
+        assertEquals(125, b.getId());
+        assertEquals("CXF in Action", b.getName());
+    }
+    
+    @Test
+    public void testGetBookSubresourceClient2() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                                  BookStoreJaxrsJaxws.class);
+        doTestSubresource(proxy);
+    }
+    
+    @Test
+    public void testGetBookSubresourceWebClientProxy() throws Exception {
+        
+        WebClient client = new WebClient("http://localhost:9092/test/services/rest");
+        client.type(MediaType.TEXT_PLAIN_TYPE).accept(MediaType.APPLICATION_XML_TYPE);
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.fromClient(client, BookStoreJaxrsJaxws.class,
true);
+        
+        doTestSubresource(proxy);
+        
+        BookStoreJaxrsJaxws proxy2 = JAXRSClientFactory.fromClient(
+            WebClient.client(proxy), BookStoreJaxrsJaxws.class);
+        doTestSubresource(proxy2);
+        
+    }
+    
+    
+    @Test
+    public void testGetBookSubresourceWebClientProxy2() throws Exception {
+        
+        WebClient client = new WebClient("http://localhost:9092/test/services/rest/bookstore")
+            .path("/books/378");
+        client.type(MediaType.TEXT_PLAIN_TYPE).accept(MediaType.APPLICATION_XML_TYPE);
+        BookSubresource proxy = JAXRSClientFactory.fromClient(client, BookSubresource.class);
+        
+        Book b = proxy.getTheBook2("CXF ", "in ", "Action ", "- 3", "7", "8");
+        assertEquals(378, b.getId());
+        assertEquals("CXF in Action - 378", b.getName());
+        
+    }
+    
+    private void doTestSubresource(BookStoreJaxrsJaxws proxy) throws Exception {
+        BookSubresource bs = proxy.getBookSubresource("378");
+        
+        Book b = bs.getTheBook2("CXF ", "in ", "Action ", "- 3", "7", "8");
+        assertEquals(378, b.getId());
+        assertEquals("CXF in Action - 378", b.getName());
+        
+        WebClient.client(bs).reset().header("N4", "- 4");
+        b = bs.getTheBook2("CXF ", "in ", "Action ", null, "7", "8");
+        assertEquals(378, b.getId());
+        assertEquals("CXF in Action - 478", b.getName());
+    }
+    
+    @Test
+    public void testGetBookWebClientForm() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest/bookstore/books/679/subresource3";
+        WebClient wc = new WebClient(baseAddress);
+        MultivaluedMap<String, Object> map = new MetadataMap<String, Object>();
+        map.putSingle("id", "679");
+        map.putSingle("name", "CXF in Action - 679");
+        Book b = readBook((InputStream)wc.accept("application/xml")
+                          .form((Map<String, List<Object>>)map).getEntity());
+        assertEquals(679, b.getId());
+        assertEquals("CXF in Action - 679", b.getName());
+    }
+    
+    @Test
+    public void testGetBookWebClientForm2() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest/bookstore/books/679/subresource3";
+        WebClient wc = new WebClient(baseAddress);
+        Form f = new Form();
+        f.set("id", "679").set("name", "CXF in Action - 679");
+        Book b = readBook((InputStream)wc.accept("application/xml")
+                          .form(f).getEntity());
+        assertEquals(679, b.getId());
+        assertEquals("CXF in Action - 679", b.getName());
+    }
+    
+    @Test
+    public void testGetBookSubresourceClientFormParam() throws Exception {
+        
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                                  BookStoreJaxrsJaxws.class);
+        BookSubresource bs = proxy.getBookSubresource("679");
+        Book b = bs.getTheBook3("679", "CXF in Action - 679");
+        assertEquals(679, b.getId());
+        assertEquals("CXF in Action - 679", b.getName());
+    }
+    
+    @Test
+    public void testAddGetBook123WebClient() throws Exception {
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        WebClient client = new WebClient(baseAddress);
+        client.path("/bookstore/books").accept(MediaType.APPLICATION_XML_TYPE)
+            .type(MediaType.APPLICATION_XML_TYPE);
+        Book b = new Book();
+        b.setId(124);
+        b.setName("CXF in Action - 2");
+        Book b2 = client.post(b, Book.class);
+        assertNotSame(b, b2);
+        assertEquals(124, b2.getId());
+        assertEquals("CXF in Action - 2", b2.getName());
+    }
+    
+    @Test
+    public void testAddGetBook123Client() throws Exception {
+        String baseAddress = "http://localhost:9092/test/services/rest";
+        BookStoreJaxrsJaxws proxy = JAXRSClientFactory.create(baseAddress,
+                                                                  BookStoreJaxrsJaxws.class);
+        Book b = new Book();
+        b.setId(124);
+        b.setName("CXF in Action - 2");
+        Book b2 = proxy.addBook(b);
+        assertNotSame(b, b2);
+        assertEquals(124, b2.getId());
+        assertEquals("CXF in Action - 2", b2.getName());
+    }
+    
+    @Test
     public void testAddGetBookRest() throws Exception {
         
         String endpointAddress =
@@ -117,4 +322,25 @@
         connect.addRequestProperty("Accept", "application/xml,text/plain");
         return connect.getInputStream();
     }
+    
+    private Book readBook(InputStream is) throws Exception {
+        JAXBContext c = JAXBContext.newInstance(new Class[]{Book.class});
+        Unmarshaller u = c.createUnmarshaller();
+        return (Book)u.unmarshal(is);
+    }
+    
+    public static class TestResponseExceptionMapper implements ResponseExceptionMapper<BookNotFoundFault>
{
+        
+        public TestResponseExceptionMapper() {
+        }
+        
+        public BookNotFoundFault fromResponse(Response r) {
+            Object value = r.getMetadata().getFirst("BOOK-HEADER");
+            if (value != null) {
+                return new BookNotFoundFault(value.toString());
+            }
+            throw new WebApplicationException();
+        }
+        
+    }
 }

Modified: cxf/trunk/systests/src/test/resources/jaxrs_soap_rest/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/resources/jaxrs_soap_rest/WEB-INF/beans.xml?rev=743825&r1=743824&r2=743825&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/resources/jaxrs_soap_rest/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/src/test/resources/jaxrs_soap_rest/WEB-INF/beans.xml Thu Feb 12 18:26:07
2009
@@ -35,7 +35,6 @@
   <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
-
   <jaxws:endpoint xmlns:s="http://books.com"
       serviceName="s:BookService"
       endpointName="s:BookPort"
@@ -49,9 +48,8 @@
       <ref bean="bookstore"/>
     </jaxrs:serviceBeans>		   
   </jaxrs:server>
-  <bean id="bookstore" class="org.apache.cxf.systest.jaxrs.BookStoreSoapRestImpl">
-  </bean>
-  
+  <bean id="bookstore" class="org.apache.cxf.systest.jaxrs.BookStoreSoapRestImpl"/>
+
   <jaxws:endpoint xmlns:s="http://books.com"
       serviceName="s:BookService"
       endpointName="s:BookPort"
@@ -65,8 +63,7 @@
       <ref bean="bookstore2"/>
     </jaxrs:serviceBeans>		   
   </jaxrs:server>
-  <bean id="bookstore2" class="org.apache.cxf.systest.jaxrs.BookStoreSoapRestImpl2">
-  </bean>
+  <bean id="bookstore2" class="org.apache.cxf.systest.jaxrs.BookStoreSoapRestImpl2"/>
 
 </beans>
 <!-- END SNIPPET: beans -->



Mime
View raw message