cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1080228 - in /cxf/branches/2.3.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Date Thu, 10 Mar 2011 13:59:37 GMT
Author: sergeyb
Date: Thu Mar 10 13:59:36 2011
New Revision: 1080228

URL: http://svn.apache.org/viewvc?rev=1080228&view=rev
Log:
Merged revisions 1080225 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1080225 | sergeyb | 2011-03-10 13:56:38 +0000 (Thu, 10 Mar 2011) | 1 line
  
  [CXF-3391] Better support for extra JAXB classes
........

Modified:
    cxf/branches/2.3.x-fixes/   (props changed)
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Mar 10 13:59:36 2011
@@ -1 +1 @@
-/cxf/trunk:1079780
+/cxf/trunk:1079780,1080225

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1080228&r1=1080227&r2=1080228&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu Mar 10 13:59:36 2011
@@ -385,7 +385,16 @@ public abstract class AbstractJAXBProvid
         synchronized (classContexts) {
             JAXBContext context = classContexts.get(type);
             if (context == null) {
-                context = JAXBContext.newInstance(new Class[]{type}, cProperties);
+                Class[] classes = null;
+                if (extraClass != null) {
+                    classes = new Class[extraClass.length + 1];
+                    classes[0] = type;
+                    System.arraycopy(extraClass, 0, classes, 1, extraClass.length);
+                } else {
+                    classes = new Class[] {type};    
+                }
+                
+                context = JAXBContext.newInstance(classes, cProperties);
                 classContexts.put(type, context);
             }
             return context;

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1080228&r1=1080227&r2=1080228&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Thu Mar 10 13:59:36 2011
@@ -50,6 +50,7 @@ import javax.xml.bind.ValidationEventHan
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.attachment.AttachmentMarshaller;
 import javax.xml.namespace.QName;
@@ -110,6 +111,66 @@ public class JAXBElementProviderTest ext
     }
     
     @Test
+    public void testExtraClassWithoutSingleContext() throws Exception {
+        ClassResourceInfo cri = 
+            ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true,
true);
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setExtraClass(new Class[]{SuperBook.class});
+        provider.init(Collections.singletonList(cri));
+        JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);
+        
+        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        bookContext.createMarshaller().marshal(new SuperBook("name", 1L, 2L), os);
+        SuperBook book = (SuperBook)bookContext.createUnmarshaller()
+                             .unmarshal(new ByteArrayInputStream(os.toByteArray()));
+        assertEquals(2L, book.getSuperId());        
+    }
+    
+    @Test
+    public void testExtraClassWithGenerics() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setExtraClass(new Class[]{XmlObject.class});
+        testXmlList(provider);
+    }
+    
+    @Test
+    public void testExtraClassWithGenericsAndSingleContext() throws Exception {
+        ClassResourceInfo cri = 
+            ResourceUtils.createClassResourceInfo(XmlListResource.class, XmlListResource.class,
true, true);
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setExtraClass(new Class[]{XmlObject.class});
+        provider.init(Collections.singletonList(cri));
+        testXmlList(provider);
+        
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void testXmlList(JAXBElementProvider provider) throws Exception {
+        
+        List<XmlObject> list = new ArrayList<XmlObject>();
+        for (int i = 0; i < 10; i++) {
+            MyObject o = new MyObject();
+            o.setName("name #" + i);
+            list.add(new XmlObject(o));
+        }
+        XmlList<XmlObject> xmlList = new XmlList<XmlObject>(list);
+        
+        JAXBContext context = provider.getJAXBContext(XmlList.class, XmlList.class);
+        
+        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        context.createMarshaller().marshal(xmlList, os);
+        XmlList<XmlObject> list2 = (XmlList<XmlObject>)context.createUnmarshaller()
+            .unmarshal(new ByteArrayInputStream(os.toByteArray()));
+        
+        List<XmlObject> actualList = list2.getList();
+        assertEquals(10, actualList.size());
+        for (int i = 0; i < 10; i++) {
+            XmlObject object = actualList.get(i);
+            assertEquals("name #" + i, object.getAttribute().getName());
+        }
+    }
+    
+    @Test
     public void testIsWriteableList() throws Exception {
         testIsWriteableCollection("getBooks");
     }
@@ -1144,4 +1205,69 @@ public class JAXBElementProviderTest ext
             return null;
         }
     }
+    
+    @XmlRootElement(name = "list")
+    public static class XmlList<A> {
+        private List<A> list;
+
+        public XmlList() {
+            // no-op
+        }
+
+        public XmlList(List<A> l) {
+            list = l;
+        }
+
+        public List<A> getList() {
+            return list;
+        }
+
+        public void setList(List<A> l) {
+            list = l;
+        }
+    }
+
+    @XmlType
+    public static class XmlObject {
+        private MyObject attribute;
+
+        public XmlObject() {
+            // no-op
+        }
+
+        public XmlObject(MyObject a) {
+            attribute = a;
+        }
+
+        @XmlElement(name = "attribute")
+        public MyObject getAttribute() {
+            return attribute;
+        }
+
+        public void setAttribute(MyObject a) {
+            attribute = a;
+        }
+    }
+
+    public static class MyObject {
+        private String name;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String n) {
+            name = n;
+        }
+    }
+
+    
+    @Path("/test")
+    public class XmlListResource {
+        @GET
+        @Path("/jaxb")
+        public XmlList<XmlObject> testJaxb() {
+            return null;
+        }
+    }
 }



Mime
View raw message