cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1481168 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ test/java/org/apache/cxf/jaxrs/impl/
Date Fri, 10 May 2013 20:01:29 GMT
Author: sergeyb
Date: Fri May 10 20:01:25 2013
New Revision: 1481168

URL: http://svn.apache.org/r1481168
Log:
[CXF-5007] More updates to ResponseImpl, ResponseBuilderImpl, LinkBuilderImpl, UriBuilderImpl,
etc

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProvider.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProvider.java
Fri May 10 20:01:25 2013
@@ -115,6 +115,9 @@ public class CacheControlHeaderProvider 
     }
 
     private String[] getTokens(String c) {
+        if (c == null) {
+            throw new IllegalArgumentException();
+        }
         if (c.contains("\"")) {
             List<String> values = new ArrayList<String>(4);
             Matcher m = COMPLEX_HEADER_PATTERN.matcher(c);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
Fri May 10 20:01:25 2013
@@ -93,7 +93,9 @@ public class LinkBuilderImpl implements 
 
     @Override
     public Builder rel(String rel) {
-        return param(Link.REL, rel);
+        String exisingRel = params.get(Link.REL);
+        String newRel = exisingRel == null ? rel : exisingRel + " " + rel; 
+        return param(Link.REL, newRel);
     }
 
     @Override

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java Fri
May 10 20:01:25 2013
@@ -121,7 +121,7 @@ public class MetadataMap<K, V> implement
     
     public V getFirst(K key) {
         List<V> data = this.get(key);
-        return data == null ? null : data.get(0);
+        return data == null || data.isEmpty() ? null : data.get(0);
     }
 
     public void putSingle(K key, V value) {
@@ -150,7 +150,7 @@ public class MetadataMap<K, V> implement
     }
 
     public List<V> get(Object key) {
-        if (!caseInsensitive) {
+        if (!caseInsensitive || key == null) {
             return m.get(key);
         }
         K realKey = getMatchingKey(key);
@@ -159,7 +159,8 @@ public class MetadataMap<K, V> implement
     
     private K getMatchingKey(Object key) {
         for (K entry : m.keySet()) {
-            if (entry.toString().equalsIgnoreCase(key.toString())) {
+            if (entry != null && entry.toString().equalsIgnoreCase(key.toString())
+                || entry == null && key == null) {
                 return entry;
             }
         }
@@ -181,7 +182,7 @@ public class MetadataMap<K, V> implement
     }
 
     public List<V> put(K key, List<V> value) {
-        K realKey = !caseInsensitive ? key : getMatchingKey(key);
+        K realKey = !caseInsensitive || key == null ? key : getMatchingKey(key);
         return m.put(realKey == null ? key : realKey, value);
     }
 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
Fri May 10 20:01:25 2013
@@ -187,17 +187,17 @@ public final class ResponseBuilderImpl e
             MediaType mt = v.getMediaType();
             if (mt != null) {
                 acceptVary = HttpHeaders.ACCEPT;
-                addHeader(HttpHeaders.ACCEPT, mt);
+                addHeader(HttpHeaders.CONTENT_TYPE, mt);
             }
             Locale l = v.getLanguage();
             if (l != null) {
                 acceptLangVary = HttpHeaders.ACCEPT_LANGUAGE;
-                addHeader(HttpHeaders.ACCEPT_LANGUAGE, l);
+                addHeader(HttpHeaders.CONTENT_LANGUAGE, l);
             }
             String enc = v.getEncoding();
             if (enc != null) {
                 acceptEncVary = HttpHeaders.ACCEPT_ENCODING;
-                addHeader(HttpHeaders.ACCEPT_ENCODING, enc);
+                addHeader(HttpHeaders.CONTENT_ENCODING, enc);
             }
         }
         handleVaryValue(acceptVary, acceptLangVary, acceptEncVary);
@@ -272,7 +272,11 @@ public final class ResponseBuilderImpl e
 
     @Override
     public ResponseBuilder allow(Set<String> methods) {
-        return allow(methods.toArray(new String[methods.size()]));
+        if (methods == null) {
+            return allow();
+        } else {
+            return allow(methods.toArray(new String[methods.size()]));
+        }
     }
 
     @Override

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
Fri May 10 20:01:25 2013
@@ -125,11 +125,12 @@ public final class ResponseImpl extends 
     }
     
     public Object getEntity() {
+        checkEntityIsClosed();
         return entity;
     }
 
     public boolean hasEntity() {
-        return entity != null;
+        return getEntity() != null;
     }
     
     public MultivaluedMap<String, Object> getMetadata() {
@@ -250,7 +251,8 @@ public final class ResponseImpl extends 
     }
 
     public Link.Builder getLinkBuilder(String relation) {
-        return Link.fromLink(getLink(relation));
+        Link link = getLink(relation);
+        return link == null ? null : Link.fromLink(link);
     }
 
     public Set<Link> getLinks() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
Fri May 10 20:01:25 2013
@@ -469,22 +469,14 @@ public class UriBuilderImpl extends UriB
         if (method == null) {
             throw new IllegalArgumentException("method is null");
         }
-        Path foundAnn = null;
         for (Method meth : resource.getMethods()) {
             if (meth.getName().equals(method)) {
                 Path ann = meth.getAnnotation(Path.class);
-                if (foundAnn != null && ann != null) {
-                    throw new IllegalArgumentException("Multiple Path annotations for '"
+ method
-                                                       + "' overloaded method");
-                }
-                foundAnn = ann;
+                String path = ann == null ? "" : ann.value();
+                return path(resource).path(path);
             }
         }
-        if (foundAnn == null) {
-            throw new IllegalArgumentException("No Path annotation for '" + method + "' method");
-        }
-        // path(String) decomposes multi-segment path when necessary
-        return path(foundAnn.value());
+        throw new IllegalArgumentException("No Path annotation for '" + method + "' method");
     }
 
     @Override
@@ -516,6 +508,9 @@ public class UriBuilderImpl extends UriB
                 this.originalPathEmpty = StringUtils.isEmpty(uri.getPath());
                 uri(uri);
             } catch (IllegalArgumentException ex) {
+                if (!new URITemplate(path).getVariables().isEmpty()) {
+                    return uriAsTemplate(path);
+                }
                 String pathEncoded = HttpUtils.pathEncode(path);
                 // Bad hack to bypass the TCK usage of bogus URI with empty paths containing
matrix parameters, 
                 // which even URI class chokes upon; cheaper to do the following than try
to challenge,
@@ -874,10 +869,40 @@ public class UriBuilderImpl extends UriB
         try {
             return uri(URI.create(uriTemplate));
         } catch (Exception ex) {
-            throw new IllegalArgumentException(ex);
+            if (new URITemplate(uriTemplate).getVariables().isEmpty()) {
+                throw new IllegalArgumentException(ex);    
+            } else {
+                return uriAsTemplate(uriTemplate);
+            }
+            
         }
     }
 
+    private UriBuilder uriAsTemplate(String uri) {
+        // This can be a start of replacing URI class Parser completely
+        // but it can be too complicated, the following code is needed for now 
+        // to deal with URIs containing template variables. 
+        int index = uri.indexOf(":");
+        if (index != -1) {
+            this.scheme = uri.substring(0, index);
+            uri = uri.substring(index + 1);
+            if (uri.indexOf("//") == 0) {
+                uri = uri.substring(2);
+                index = uri.indexOf("/");
+                if (index != -1) {
+                    String[] schemePair = uri.substring(0, index).split(":");
+                    this.host = schemePair[0];
+                    this.port = schemePair.length == 2 ? Integer.valueOf(schemePair[1]) :
-1;
+                    
+                }
+                uri = uri.substring(index);
+            }
+            
+        }
+        setPathAndMatrix(uri);
+        return this;
+    }
+    
     //the clarified rules for encoding values of uri templates are:
     //  - encode each value contextually based on the URI component containing the template
     //  - in path templates, by default, encode also slashes (i.e. treat all path templates
as 

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProviderTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/CacheControlHeaderProviderTest.java
Fri May 10 20:01:25 2013
@@ -26,11 +26,17 @@ import javax.ws.rs.core.CacheControl;
 
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 public class CacheControlHeaderProviderTest extends Assert {
     
+    @Test(expected = IllegalArgumentException.class)
+    public void testValueOfNull() {
+        CacheControl.valueOf(null);
+    }
+    
     @Test
     public void testFromSimpleString() {
         CacheControl c = CacheControl.valueOf(

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
Fri May 10 20:01:25 2013
@@ -18,26 +18,55 @@
  */
 package org.apache.cxf.jaxrs.impl;
 
+import java.io.ByteArrayInputStream;
 import java.net.URI;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
 import javax.ws.rs.core.Link;
+import javax.ws.rs.core.MediaType;
 
+import org.junit.Assert;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-
-public class LinkBuilderImplTest {
+public class LinkBuilderImplTest extends Assert {
     
 
     @Test
-    public void build() throws Exception {
+    public void testBuild() throws Exception {
         Link.Builder linkBuilder = new LinkBuilderImpl();
         Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").build();
         assertEquals("<http://example.com/page1>;rel=\"previous\"", prevLink.toString());
     }
+    
+    @Test
+    public void testbBuildObjects() throws Exception {
+        StringBuilder path1 = new StringBuilder().append("p1");
+        ByteArrayInputStream path2 = new ByteArrayInputStream("p2".getBytes()) {
+            @Override
+            public String toString() {
+                return "p2";
+            }
+        };
+        URI path3 = new URI("p3");
+        
+        String expected = "<" + "http://host.com:888/" + "p1/p2/p3" + ">";
+        Link.Builder builder = Link.fromUri("http://host.com:888/" + "{x1}/{x2}/{x3}");
+        Link link = builder.build(path1, path2, path3);
+        assertNotNull(link);
+        assertEquals(link.toString(), expected);
+    }
+    
+    @Test
+    public void testBuildManyRels() throws Exception {
+        Link.Builder linkBuilder = new LinkBuilderImpl();
+        Link prevLink = linkBuilder.uri("http://example.com/page1").rel("1").rel("2").build();
+        assertEquals("<http://example.com/page1>;rel=\"1 2\"", prevLink.toString());
+    }
 
     @Test
-    public void relativeBuild() throws Exception {
+    public void testRelativeBuild() throws Exception {
         Link.Builder linkBuilder = new LinkBuilderImpl();
         URI base = URI.create("http://example.com/page2");
         Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").buildRelativized(base);
@@ -45,32 +74,27 @@ public class LinkBuilderImplTest {
     }
 
     @Test
-    public void severalAttributes() throws Exception {
+    public void testSeveralAttributes() throws Exception {
         Link.Builder linkBuilder = new LinkBuilderImpl();
         Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").title("A
title").build();
         assertEquals("<http://example.com/page1>;rel=\"previous\";title=\"A title\"",
prevLink.toString());
     }
-
+    
     @Test
-    public void copyOnBuild() throws Exception {
-        Link.Builder linkBuilder = new LinkBuilderImpl();
-        Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").build();
-        Link nextLink = linkBuilder.uri("http://example.com/page3").rel("next").build();
-
-        // Previously built link should not be affected by reuse of link builder
-        assertEquals("<http://example.com/page1>;rel=\"previous\"", prevLink.toString());
-        assertEquals("<http://example.com/page3>;rel=\"next\"", nextLink.toString());
+    public void testCreateFromMethod() throws Exception {
+        Link.Builder linkBuilder = Link.fromMethod(TestResource.class, "consumesAppJson");
+        Link link = linkBuilder.build();
+        String resource = link.toString();
+        assertTrue(resource.contains("<resource/consumesappjson>"));
     }
-
-    @Test
-    public void copyOnRelativeBuild() throws Exception {
-        Link.Builder linkBuilder = new LinkBuilderImpl();
-        URI base = URI.create("http://example.com/page2");
-        Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").buildRelativized(base);
-        Link nextLink = linkBuilder.uri("http://example.com/page3").rel("next").buildRelativized(base);
-
-        // Previously built link should not be affected by reuse of link builder
-        assertEquals("<page1>;rel=\"previous\"", prevLink.toString());
-        assertEquals("<page3>;rel=\"next\"", nextLink.toString());
+    
+    @Path("resource")
+    public static class TestResource {
+        @POST
+        @Consumes(MediaType.APPLICATION_JSON)
+        @Path("consumesappjson")
+        public String consumesAppJson() {
+            return MediaType.APPLICATION_JSON;
+        }
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
Fri May 10 20:01:25 2013
@@ -52,6 +52,39 @@ public class MetadataMapTest extends Ass
     }
     
     @Test
+    public void testPutSingleNullKey() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.putSingle(null, "null");
+        m.putSingle(null, "null2");
+        assertEquals(1, m.get(null).size());
+        assertEquals("null2", m.getFirst(null));
+    }
+    
+    @Test
+    public void testPutSingleNullKeyCaseSensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>(false,
true);
+        m.putSingle(null, "null");
+        m.putSingle(null, "null2");
+        assertEquals(1, m.get(null).size());
+        assertEquals("null2", m.getFirst(null));
+    }
+    
+    @Test
+    public void testPutSingleNullKeyCaseSensitive2() {
+        MetadataMap<String, Object> map = new MetadataMap<String, Object>(false,
true);
+        Object obj1 = new Object();
+        Object obj2 = new Object();
+        map.putSingle("key", obj1);
+        map.putSingle("key", obj2);
+        map.putSingle(null, obj2);
+        map.putSingle(null, obj1);
+        assertEquals(2, map.size());
+        assertEquals(1, map.get(null).size());
+        assertSame(map.getFirst("key"), obj2);
+        assertSame(map.getFirst(null), obj1);
+    }
+    
+    @Test
     public void testAddFirst() {
         MetadataMap<String, Object> m = new MetadataMap<String, Object>();
         m.addFirst("baz", "foo");
@@ -299,6 +332,15 @@ public class MetadataMapTest extends Ass
     }
     
     @Test
+    public void testGetFirstEmptyMap() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        assertNull(m.getFirst("key"));
+        m.add("key", "1");
+        m.get("key").clear();
+        assertNull(m.getFirst("key"));
+    }
+    
+    @Test
     public void testCompareIgnoreValueOrder() {
         MetadataMap<String, String> m = new MetadataMap<String, String>();
         m.add("baz", "bar1");

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
Fri May 10 20:01:25 2013
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.impl;
 import java.lang.annotation.Annotation;
 import java.net.URI;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -35,9 +36,11 @@ import javax.ws.rs.core.Link;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.NewCookie;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Variant;
 
 import org.apache.cxf.jaxrs.utils.HttpUtils;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -129,6 +132,12 @@ public class ResponseBuilderImplTest ext
         methods.add("GET");
         checkBuild(Response.ok().allow(methods).build(), 200, null, m);
     }
+    
+    @Test
+    public void testAllowReSet() throws Exception {
+        Response r = Response.ok().allow("GET").allow((Set<String>)null).build();
+        assertNull(r.getMetadata().getFirst("Allow"));
+    }
 
     @Test
     public void testValidStatus() {
@@ -327,6 +336,28 @@ public class ResponseBuilderImplTest ext
                    200, null, m);
     }
     
+    @Test
+    public void testVariant2() throws Exception {
+        List<String> encoding = Arrays.asList("gzip", "compress");
+        MediaType mt = MediaType.APPLICATION_JSON_TYPE;
+        ResponseBuilder rb = Response.ok();
+        rb = rb.variants(getVariantList(encoding, mt).toArray(new Variant[0]));
+        Response response = rb.build();
+        List<Object> enc = response.getHeaders().get(HttpHeaders.CONTENT_ENCODING);
+        assertTrue(encoding.containsAll(enc));
+        List<Object> ct = response.getHeaders().get(HttpHeaders.CONTENT_TYPE);
+        assertTrue(ct.contains(mt.toString()));
+    }
+    
+    protected static List<Variant> getVariantList(List<String> encoding,
+                                                  MediaType... mt) {
+        return Variant.VariantListBuilder.newInstance()
+            .mediaTypes(mt)
+            .languages(new Locale("en", "US"), new Locale("en", "GB"), new Locale("zh", "CN"))
+            .encodings(encoding.toArray(new String[]{}))
+            .add()
+            .build();
+    }
 
     @Test
     public void testCreatedNoEntity() throws Exception {
@@ -353,12 +384,14 @@ public class ResponseBuilderImplTest ext
     public void testVariantsArray() throws Exception {
         
         MetadataMap<String, Object> m = new MetadataMap<String, Object>();
-        m.add("Accept", "application/json");
-        m.add("Accept", "application/xml");
+        m.add("Content-Type", "application/json");
+        m.add("Content-Language", "en_uk");
+        m.add("Content-Language", "en_gb");
         m.add("Vary", "Accept");
-
-        Variant json = new Variant(MediaType.APPLICATION_JSON_TYPE, (String)null, null);
-        Variant xml = new Variant(MediaType.APPLICATION_XML_TYPE, (String)null, null);
+        m.add("Vary", "Accept-Language");
+        
+        Variant json = new Variant(MediaType.APPLICATION_JSON_TYPE, "en_uk", null);
+        Variant xml = new Variant(MediaType.APPLICATION_JSON_TYPE, "en_gb", null);
 
         checkBuild(Response.ok().variants(json, xml).build(), 200, null, m);
     }
@@ -366,17 +399,17 @@ public class ResponseBuilderImplTest ext
     @Test
     public void testVariantsList() throws Exception {
         MetadataMap<String, Object> m = new MetadataMap<String, Object>();
-        m.add("Accept", "text/xml");
-        m.add("Accept", "application/xml");
-        m.add("Accept-Language", "en_UK");
-        m.add("Accept-Language", "en_GB");
-        m.add("Accept-Encoding", "compress");
-        m.add("Accept-Encoding", "gzip");
+        m.add("Content-Type", "text/xml");
+        m.add("Content-Language", "en_UK");
+        m.add("Content-Language", "en_GB");
+        m.add("Content-Encoding", "compress");
+        m.add("Content-Encoding", "gzip");
         m.add("Vary", "Accept");
         m.add("Vary", "Accept-Language");
         m.add("Vary", "Accept-Encoding");
+        
         List<Variant> vts = Variant.VariantListBuilder.newInstance()
-            .mediaTypes(MediaType.TEXT_XML_TYPE, MediaType.APPLICATION_XML_TYPE).
+            .mediaTypes(MediaType.TEXT_XML_TYPE).
             languages(new Locale("en", "UK"), new Locale("en", "GB")).encodings("compress",
"gzip").
                       add().build();
 

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
Fri May 10 20:01:25 2013
@@ -20,20 +20,32 @@
 package org.apache.cxf.jaxrs.impl;
 
 import java.io.ByteArrayInputStream;
+import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Link;
+import javax.ws.rs.core.Link.Builder;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.NewCookie;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.Response.StatusType;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.Variant;
+import javax.ws.rs.core.Variant.VariantListBuilder;
+import javax.ws.rs.ext.RuntimeDelegate;
+import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
 
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 
@@ -54,11 +66,34 @@ public class ResponseImplTest extends As
     }
     
     @Test
+    @Ignore
+    public void testGetHeaderStringUsingHeaderDelegate() throws Exception {
+        StringBean bean = new StringBean("s3");
+        RuntimeDelegate original = RuntimeDelegate.getInstance();
+        RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(original));
+        try {
+            Response response = Response.ok().header(bean.get(), bean).build();
+            String header = response.getHeaderString(bean.get());
+            assertTrue(header.contains(bean.get()));
+        } finally {
+            RuntimeDelegate.setInstance(original);
+            StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate();
+        }
+    }
+    
+    @Test
     public void testHasEntity() {
         assertTrue(new ResponseImpl(200, "").hasEntity());
         assertFalse(new ResponseImpl(200).hasEntity());
     }
     
+    @Test(expected = IllegalStateException.class)
+    public void testGetEntityAfterClose() {
+        Response response = Response.ok("entity").build();
+        response.close();
+        response.getEntity();
+    }
+    
     @Test
     public void testStatuInfoForOKStatus() {
         StatusType si = new ResponseImpl(200, "").getStatusInfo();
@@ -86,12 +121,12 @@ public class ResponseImplTest extends As
         assertEquals("", si.getReasonPhrase());
     }
     
-    @Test
+    @Test(expected = IllegalStateException.class)
     public void testHasEntityAfterClose() {
         Response r = new ResponseImpl(200, new ByteArrayInputStream("data".getBytes()));

         assertTrue(r.hasEntity());
         r.close();
-        assertFalse(r.hasEntity());
+        r.hasEntity();
     }
     
     
@@ -204,6 +239,23 @@ public class ResponseImplTest extends As
     }
     
     @Test
+    public void testGetNoLinkBuilder() throws Exception {
+        Response response = Response.ok().build();
+        Builder builder = response.getLinkBuilder("anyrelation");
+        assertNull(builder);
+    }
+    
+    protected static List<Variant> getVariantList(List<String> encoding,
+                                                  MediaType... mt) {
+        return Variant.VariantListBuilder.newInstance()
+            .mediaTypes(mt)
+            .languages(new Locale("en", "US"), new Locale("en", "GB"), new Locale("zh", "CN"))
+            .encodings(encoding.toArray(new String[]{}))
+            .add()
+            .build();
+    }
+    
+    @Test
     public void testGetLinks() {
         ResponseImpl ri = new ResponseImpl(200);
         MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
@@ -232,4 +284,106 @@ public class ResponseImplTest extends As
         assertEquals("http://prev", prev.getUri().toString());
         assertEquals("prev", prev.getRel());
     }
+    
+    public static class StringBean {
+        private String header;
+
+        public StringBean(String header) {
+            super();
+            this.header = header;
+        }
+        
+        public String get() {
+            return header;
+        }
+
+        public void set(String h) {
+            this.header = h;
+        }
+        
+        @Override
+        public String toString() {
+            return "StringBean. To get a value, use rather #get() method.";
+        }
+    }
+    
+    public static class StringBeanRuntimeDelegate extends RuntimeDelegate {
+        private RuntimeDelegate original;
+        public StringBeanRuntimeDelegate(RuntimeDelegate orig) {
+            super();
+            this.original = orig;
+            assertNotStringBeanRuntimeDelegate(orig);
+        }
+
+        @Override
+        public <T> T createEndpoint(Application arg0, Class<T> arg1)
+            throws IllegalArgumentException, UnsupportedOperationException {
+            return original.createEndpoint(arg0, arg1);
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> HeaderDelegate<T> createHeaderDelegate(Class<T> arg0)
+            throws IllegalArgumentException {
+            if (arg0 == StringBean.class) {
+                return (HeaderDelegate<T>) new StringBeanHeaderDelegate();
+            } else {
+                return original.createHeaderDelegate(arg0);
+            }
+        }
+
+        @Override
+        public ResponseBuilder createResponseBuilder() {
+            return original.createResponseBuilder();
+        }
+
+        @Override
+        public UriBuilder createUriBuilder() {
+            return original.createUriBuilder();
+        }
+
+        @Override
+        public VariantListBuilder createVariantListBuilder() {
+            return original.createVariantListBuilder();
+        }
+
+        public RuntimeDelegate getOriginal() {
+            return original;
+        }
+
+        public static final void assertNotStringBeanRuntimeDelegate() {
+            RuntimeDelegate delegate = RuntimeDelegate.getInstance();
+            assertNotStringBeanRuntimeDelegate(delegate);
+        }
+
+        public static final void assertNotStringBeanRuntimeDelegate(RuntimeDelegate delegate)
{
+            if (delegate instanceof StringBeanRuntimeDelegate) {
+                StringBeanRuntimeDelegate sbrd = (StringBeanRuntimeDelegate) delegate;
+                if (sbrd.getOriginal() != null) {
+                    RuntimeDelegate.setInstance(sbrd.getOriginal());
+                    throw new RuntimeException(
+                        "RuntimeDelegate.getInstance() is StringBeanRuntimeDelegate");
+                }
+            }
+        }
+
+        @Override
+        public Builder createLinkBuilder() {
+            return original.createLinkBuilder();
+        }
+    }
+    
+    public static class StringBeanHeaderDelegate implements HeaderDelegate<StringBean>
{
+
+        @Override
+        public StringBean fromString(String string) throws IllegalArgumentException {
+            return new StringBean(string);
+        }
+
+        @Override
+        public String toString(StringBean bean) throws IllegalArgumentException {
+            return bean.get();
+        }
+
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=1481168&r1=1481167&r2=1481168&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Fri May 10 20:01:25 2013
@@ -40,6 +40,20 @@ import org.junit.Test;
 public class UriBuilderImplTest extends Assert {
 
     @Test
+    public void testUriTemplate() throws Exception {
+        UriBuilder builder = UriBuilder.fromUri("http://localhost:8080/{a}/{b}");
+        URI uri = builder.build("1", "2");
+        assertEquals("http://localhost:8080/1/2", uri.toString());
+    }
+    
+    @Test
+    public void testUriTemplate2() throws Exception {
+        UriBuilder builder = UriBuilder.fromUri("http://localhost/{a}/{b}");
+        URI uri = builder.build("1", "2");
+        assertEquals("http://localhost/1/2", uri.toString());
+    }
+    
+    @Test
     public void testBuildWithNonEncodedSubstitutionValue() {
         URI uri;
         uri = UriBuilder.fromPath("/{a}").build("{}");
@@ -765,7 +779,7 @@ public class UriBuilderImplTest extends 
     public void testAddPathClassMethod() throws Exception {
         URI uri = new URI("http://foo/");
         URI newUri = new UriBuilderImpl().uri(uri).path(BookStore.class, "updateBook").path("bar").build();
-        assertEquals("URI is not built correctly", new URI("http://foo/books/bar"), newUri);
+        assertEquals("URI is not built correctly", new URI("http://foo/bookstore/books/bar"),
newUri);
     }
 
     @Test(expected = IllegalArgumentException.class)



Mime
View raw message