Author: sergeyb
Date: Tue Apr 19 10:14:37 2011
New Revision: 1094992

URL: http://svn.apache.org/viewvc?rev=1094992&view=rev
Log:
[CXF-3460] Updating WebClient with methods simplifying the replacement of query 
and path values

Modified:
    
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java

Modified: 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1094992&r1=1094991&r2=1094992&view=diff
==============================================================================
--- 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
 (original)
+++ 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
 Tue Apr 19 10:14:37 2011
@@ -499,11 +499,48 @@ public class WebClient extends AbstractC
     }
     
     /**
+     * Replaces the current path with the new value.
+     * @param path new path value. If it starts from "/" then all the current
+     * path starting from the base URI will be replaced, otherwise only the 
+     * last path segment will be replaced. Providing a null value is equivalent
+     * to calling back(true)  
+     * @return updated WebClient
+     */
+    public WebClient replacePath(String path) {
+        if (path == null) {
+            return back(true);
+        }
+        back(path.startsWith("/") ? true : false);
+        return path(path);
+    }
+    
+    /**
      * Resets the current query
      * @return updated WebClient
      */
     public WebClient resetQuery() {
-        getCurrentBuilder().replaceQuery(null);
+        return replaceQuery(null);
+    }
+    
+    /**
+     * Replaces the current query with the new value.
+     * @param queryString the new value, providing a null is
+     *        equivalent to calling resetQuery().  
+     * @return updated WebClient
+     */
+    public WebClient replaceQuery(String queryString) {
+        getCurrentBuilder().replaceQuery(queryString);
+        return this;
+    }
+    
+    /**
+     * Replaces the current query with the new value.
+     * @param queryString the new value, providing a null is
+     *        equivalent to calling resetQuery().  
+     * @return updated WebClient
+     */
+    public WebClient replaceQueryParam(String queryParam, Object... value) {
+        getCurrentBuilder().replaceQueryParam(queryParam, value);
         return this;
     }
     

Modified: 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java?rev=1094992&r1=1094991&r2=1094992&view=diff
==============================================================================
--- 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
 (original)
+++ 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
 Tue Apr 19 10:14:37 2011
@@ -164,6 +164,45 @@ public class WebClientTest extends Asser
     }
     
     @Test
+    public void testReplaceQuery() {
+        WebClient wc = WebClient.create(URI.create("http://foo";));
+        wc.path("bar").path("baz").query("foo", "bar");
+        assertEquals(URI.create("http://foo";), wc.getBaseURI());
+        assertEquals(URI.create("http://foo/bar/baz?foo=bar";), 
wc.getCurrentURI());
+        wc.replaceQuery("foo1=bar1");
+        assertEquals(URI.create("http://foo/bar/baz?foo1=bar1";), 
wc.getCurrentURI());
+    }
+    
+    @Test
+    public void testReplaceQueryParam() {
+        WebClient wc = WebClient.create(URI.create("http://foo";));
+        wc.path("bar").path("baz").query("foo", "bar").query("foo1", "bar1");
+        assertEquals(URI.create("http://foo";), wc.getBaseURI());
+        assertEquals(URI.create("http://foo/bar/baz?foo=bar&foo1=bar1";), 
wc.getCurrentURI());
+        wc.replaceQueryParam("foo1", "baz");
+        assertEquals(URI.create("http://foo/bar/baz?foo=bar&foo1=baz";), 
wc.getCurrentURI());
+    }
+    
+    @Test
+    public void testReplacePathAll() {
+        WebClient wc = WebClient.create(URI.create("http://foo";));
+        wc.path("bar").path("baz");
+        assertEquals(URI.create("http://foo";), wc.getBaseURI());
+        assertEquals(URI.create("http://foo/bar/baz";), wc.getCurrentURI());
+        wc.replacePath("/new");
+        assertEquals(URI.create("http://foo/new";), wc.getCurrentURI());
+    }
+    @Test
+    public void testReplacePathLastSegment() {
+        WebClient wc = WebClient.create(URI.create("http://foo";));
+        wc.path("bar").path("baz");
+        assertEquals(URI.create("http://foo";), wc.getBaseURI());
+        assertEquals(URI.create("http://foo/bar/baz";), wc.getCurrentURI());
+        wc.replacePath("new");
+        assertEquals(URI.create("http://foo/bar/new";), wc.getCurrentURI());
+    }
+    
+    @Test
     public void testFragment() {
         WebClient wc = WebClient.create(URI.create("http://foo";));
         wc.path("bar").path("baz").query("foo", "bar").fragment("1");


Reply via email to