cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1468490 - in /cxf/trunk/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
Date Tue, 16 Apr 2013 16:22:36 GMT
Author: sergeyb
Date: Tue Apr 16 16:22:36 2013
New Revision: 1468490

URL: http://svn.apache.org/r1468490
Log:
[CXF-4967] Fixing NPE in JSONProvider

Modified:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java?rev=1468490&r1=1468489&r2=1468490&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/JSONProvider.java
Tue Apr 16 16:22:36 2013
@@ -491,7 +491,7 @@ public class JSONProvider<T> extends Abs
         Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception
{
         
         QName qname = actualClass == Document.class ? null : getQName(actualClass, genericType,
actualObject);
-        if (ignoreNamespaces && (isCollection  || dropRootElement)) {        
+        if (qname != null && ignoreNamespaces && (isCollection  || dropRootElement))
{        
             qname = new QName(qname.getLocalPart());
         }
         if (BADGER_FISH_CONVENTION.equals(convention)) {

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java?rev=1468490&r1=1468489&r2=1468490&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
Tue Apr 16 16:22:36 2013
@@ -30,6 +30,7 @@ import org.apache.cxf.jaxrs.impl.Metadat
 import org.apache.cxf.jaxrs.impl.ProvidersImpl;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
+import org.apache.cxf.jaxrs.provider.json.JSONProvider;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
@@ -48,9 +49,10 @@ public class DOM4JProviderTest extends A
         // starts with the xml PI
         assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
     }
-    
     private org.dom4j.Document readXML() throws Exception {
-        final String xml = "<a/>";
+        return readXML("<a/>");
+    }
+    private org.dom4j.Document readXML(final String xml) throws Exception {
         DOM4JProvider p = new DOM4JProvider();
         p.setProviders(new ProvidersImpl(createMessage()));
         org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class,

@@ -102,6 +104,21 @@ public class DOM4JProviderTest extends A
         assertEquals("{\"a\":\"\"}", str);
     }
     
+    @Test
+    public void testWriteJSONDropRoot() throws Exception {
+        org.dom4j.Document dom = readXML("<root><a/></root>");
+        DOM4JProvider p = new DOM4JProvider();
+        p.setProviders(new ProvidersImpl(createMessageWithJSONProvider()));
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
+                   new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String,
Object>(),
+                   bos);
+        String str = bos.toString();
+        // starts with the xml PI
+        assertEquals("{\"a\":\"\"}", str);
+    }
+    
     private Message createMessage() {
         ProviderFactory factory = ServerProviderFactory.getInstance();
         Message m = new MessageImpl();
@@ -124,4 +141,31 @@ public class DOM4JProviderTest extends A
         e.put(Endpoint.class, endpoint);
         return m;
     }
+    
+    private Message createMessageWithJSONProvider() {
+        ProviderFactory factory = ServerProviderFactory.getInstance();
+        JSONProvider<Object> provider = new JSONProvider<Object>();
+        provider.setDropRootElement(true);
+        provider.setIgnoreNamespaces(true);
+        factory.registerUserProvider(provider);
+        Message m = new MessageImpl();
+        m.put("org.apache.cxf.http.case_insensitive_queries", false);
+        Exchange e = new ExchangeImpl();
+        m.setExchange(e);
+        e.setInMessage(m);
+        Endpoint endpoint = EasyMock.createMock(Endpoint.class);
+        endpoint.getEndpointInfo();
+        EasyMock.expectLastCall().andReturn(null).anyTimes();
+        endpoint.get(Application.class.getName());
+        EasyMock.expectLastCall().andReturn(null);
+        endpoint.size();
+        EasyMock.expectLastCall().andReturn(0).anyTimes();
+        endpoint.isEmpty();
+        EasyMock.expectLastCall().andReturn(true).anyTimes();
+        endpoint.get(ServerProviderFactory.class.getName());
+        EasyMock.expectLastCall().andReturn(factory).anyTimes();
+        EasyMock.replay(endpoint);
+        e.put(Endpoint.class, endpoint);
+        return m;
+    }
 }



Mime
View raw message