cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r797117 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/
Date Thu, 23 Jul 2009 15:54:00 GMT
Author: sergeyb
Date: Thu Jul 23 15:53:59 2009
New Revision: 797117

URL: http://svn.apache.org/viewvc?rev=797117&view=rev
Log:
JAXRS: adding AegisJSONProvider plus few more jaxb/json tests

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book_aegis.txt

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractAegisProvider.java
Thu Jul 23 15:53:59 2009
@@ -41,8 +41,18 @@
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
     private static Map<Class<?>, AegisContext> classContexts = new WeakHashMap<Class<?>,
AegisContext>();
+    protected boolean writeXsiType = true;
+    protected boolean readXsiType = true;
+    @Context 
+    protected ContextResolver<AegisContext> resolver;
     
-    @Context protected ContextResolver<AegisContext> resolver;
+    public void setWriteXsiType(boolean write) {
+        writeXsiType = write;
+    }
+    
+    public void setReadXsiType(boolean read) {
+        readXsiType = read;
+    }
     
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns,
MediaType mt) {
         return isSupported(type, genericType, anns);
@@ -94,8 +104,8 @@
             AegisContext context = classContexts.get(type);
             if (context == null) {
                 context = new AegisContext();
-                context.setWriteXsiTypes(true); // needed, since we know no element/type
maps.
-                context.setReadXsiTypes(true);
+                context.setWriteXsiTypes(writeXsiType); 
+                context.setReadXsiTypes(readXsiType);
                 Set<Class<?>> rootClasses = new HashSet<Class<?>>();
                 rootClasses.add(type);
                 if (!(genericType instanceof Class)) {
@@ -119,4 +129,8 @@
     protected boolean isSupported(Class<?> type, Type genericType, Annotation[] annotations)
{
         return true;
     }
+    
+    void clearContexts() {
+        classContexts.clear();
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu Jul 23 15:53:59 2009
@@ -87,6 +87,23 @@
     private Schema schema;
     private String collectionWrapperName;
     private Map<String, String> collectionWrapperMap;
+    private List<String> jaxbElementClassNames;
+    
+    public void setJaxbElementClassNames(List<String> names) {
+        jaxbElementClassNames = names;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected Object convertToJaxbElementIfNeeded(Object obj, Class<?> cls, Type genericType)

+        throws Exception {
+        if (jaxbElementClassNames != null && jaxbElementClassNames.contains(cls.getName()))
{
+            QName name = getJaxbQName(cls, genericType, obj, false);
+            if (name != null) {
+                return new JAXBElement(name, cls, null, obj);
+            }
+        }
+        return obj;
+    }
     
     public void setCollectionWrapperName(String wName) {
         collectionWrapperName = wName;

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisElementProvider.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisElementProvider.java
Thu Jul 23 15:53:59 2009
@@ -44,21 +44,24 @@
 @Provider
 @Produces({"application/xml", "application/*+xml", "text/xml" })
 @Consumes({"application/xml", "application/*+xml", "text/xml" })
-public final class AegisElementProvider extends AbstractAegisProvider  {
+public class AegisElementProvider extends AbstractAegisProvider  {
     
     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType m, 
         MultivaluedMap<String, String> headers, InputStream is) 
         throws IOException {
         AegisContext context = getAegisContext(type, genericType);
         AegisReader<XMLStreamReader> aegisReader = context.createXMLStreamReader();
-        XMLStreamReader xmlStreamReader = StaxUtils.createXMLStreamReader(is);
         try {
+            XMLStreamReader xmlStreamReader = createStreamReader(type, is);
             return aegisReader.read(xmlStreamReader);
         } catch (Exception e) {
             throw new WebApplicationException(e);
         }
     }
 
+    protected XMLStreamReader createStreamReader(Class<?> type, InputStream is) throws
Exception {
+        return StaxUtils.createXMLStreamReader(is);
+    }
     
     public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] anns,
 
         MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) 
@@ -69,13 +72,19 @@
         AegisContext context = getAegisContext(type, genericType);
         org.apache.cxf.aegis.type.Type aegisType = TypeUtil.getWriteTypeStandalone(context,
obj, null);
         AegisWriter<XMLStreamWriter> aegisWriter = context.createXMLStreamWriter();
-        XMLStreamWriter xmlStreamWriter = StaxUtils.createXMLStreamWriter(os);
         try {
+            XMLStreamWriter xmlStreamWriter = createStreamWriter(type, os);
             // use type qname as element qname?
+            xmlStreamWriter.writeStartDocument();
             aegisWriter.write(obj, aegisType.getSchemaType(), false, xmlStreamWriter, aegisType);
+            xmlStreamWriter.writeEndDocument();
             xmlStreamWriter.close();
         } catch (Exception e) {
             throw new WebApplicationException(e);
         }
     }
+    
+    protected XMLStreamWriter createStreamWriter(Class<?> type, OutputStream os) throws
Exception {
+        return StaxUtils.createXMLStreamWriter(os);
+    }
 }

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java?rev=797117&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
Thu Jul 23 15:53:59 2009
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.Provider;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.cxf.common.util.PackageUtils;
+import org.codehaus.jettison.AbstractXMLStreamWriter;
+import org.codehaus.jettison.mapped.Configuration;
+import org.codehaus.jettison.mapped.MappedNamespaceConvention;
+import org.codehaus.jettison.mapped.MappedXMLInputFactory;
+import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
+
+@Provider
+@Produces({"application/json" })
+@Consumes({"application/json" })
+public final class AegisJSONProvider extends AegisElementProvider  {
+    
+    private List<String> arrayKeys;
+    private boolean serializeAsArray;
+    private ConcurrentHashMap<String, String> namespaceMap = new ConcurrentHashMap<String,
String>();
+    
+    public AegisJSONProvider() {
+    }
+    
+    public void setArrayKeys(List<String> keys) {
+        this.arrayKeys = keys;
+    }
+    
+    public void setSerializeAsArray(boolean asArray) {
+        this.serializeAsArray = asArray;
+    }
+    
+    @Override
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+        return false;
+    }
+    
+    public void setNamespaceMap(Map<String, String> nsMap) {
+        this.namespaceMap = new ConcurrentHashMap<String, String>(nsMap);
+    }
+    
+    @Override
+    protected XMLStreamWriter createStreamWriter(Class<?> type, OutputStream os) throws
Exception {
+        QName qname = getQName(type);
+        if (writeXsiType) {
+            namespaceMap.putIfAbsent("http://www.w3.org/2001/XMLSchema-instance", "xsins");
+        }
+        Configuration c = new Configuration(namespaceMap);
+        MappedNamespaceConvention convention = new MappedNamespaceConvention(c);
+        AbstractXMLStreamWriter xsw = new MappedXMLStreamWriter(
+                                            convention, 
+                                            new OutputStreamWriter(os, "UTF-8"));
+        if (serializeAsArray) {
+            if (arrayKeys != null) {
+                for (String key : arrayKeys) {
+                    xsw.seriliazeAsArray(key);
+                }
+            } else {
+                String key = getKey(convention, qname);
+                xsw.seriliazeAsArray(key);
+            }
+        }
+        return xsw;
+    }
+    
+    @Override
+    protected XMLStreamReader createStreamReader(Class<?> type, InputStream is) throws
Exception {
+        MappedXMLInputFactory factory = new MappedXMLInputFactory(namespaceMap);
+        return factory.createXMLStreamReader(is);
+    }
+    
+    private QName getQName(Class<?> type) {
+        String nsURI = PackageUtils.getNamespace(PackageUtils.getPackageName(type));
+        if (nsURI.endsWith("/")) {
+            nsURI = nsURI.substring(0, nsURI.length() - 1);
+        }
+        QName qname = new QName(nsURI, type.getSimpleName(), "ns1"); 
+        namespaceMap.putIfAbsent(nsURI, "ns1");
+        return qname;
+    }
+    
+    private String getKey(MappedNamespaceConvention convention, QName qname) throws Exception
{
+        return convention.createKey(qname.getPrefix(), 
+                                    qname.getNamespaceURI(),
+                                    qname.getLocalPart());
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Jul 23 15:53:59 2009
@@ -72,7 +72,6 @@
     private Map<String, Object> mProperties = new HashMap<String, Object>();
     private boolean enableStreaming;
     private ValidationEventHandler eventHandler;
-    private List<String> jaxbElementClassNames;
     
     @Override
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] anns, MediaType
mt) {
@@ -92,10 +91,6 @@
         super.setContext(mc);
     }
     
-    public void setJaxbElementClassNames(List<String> names) {
-        jaxbElementClassNames = names;
-    }
-    
     public void setValidationHandler(ValidationEventHandler handler) {
         eventHandler = handler;
     }
@@ -282,15 +277,9 @@
     
     protected void marshal(Object obj, Class<?> cls, Type genericType, 
                            String enc, OutputStream os, MediaType mt) throws Exception {
-        
-        if (jaxbElementClassNames != null && jaxbElementClassNames.contains(cls.getName()))
{
-            QName name = getJaxbQName(cls, genericType, obj, false);
-            if (name != null) {
-                @SuppressWarnings("unchecked")
-                JAXBElement el = new JAXBElement(name, cls, null, obj);
-                obj = el;
-                cls = JAXBElement.class;
-            }
+        obj = convertToJaxbElementIfNeeded(obj, cls, genericType);
+        if (obj instanceof JAXBElement && cls != JAXBElement.class) {
+            cls = JAXBElement.class;
         }
         
         Marshaller ms = createMarshaller(obj, cls, genericType, enc);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Thu Jul 23 15:53:59 2009
@@ -328,6 +328,12 @@
     
     protected void marshal(Object actualObject, Class<?> actualClass, 
                            Type genericType, String enc, OutputStream os) throws Exception
{
+        
+        actualObject = convertToJaxbElementIfNeeded(actualObject, actualClass, genericType);
+        if (actualObject instanceof JAXBElement && actualClass != JAXBElement.class)
{
+            actualClass = JAXBElement.class;
+        }
+        
         Marshaller ms = createMarshaller(actualObject, actualClass, genericType, enc);
         marshal(ms, actualObject, actualClass, genericType, enc, os, false);
     }
@@ -388,7 +394,10 @@
         }
 
         public void writeCharacters(String text) throws XMLStreamException {
-            if (!lastWriteChars) {
+            if (!lastWriteChars || text.trim().length() > 0) {
+                if (lastWriteChars) {
+                    text = text.trim();
+                }
                 super.writeCharacters(text);
                 lastWriteChars = true;
             }

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java?rev=797117&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
Thu Jul 23 15:53:59 2009
@@ -0,0 +1,145 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.cxf.jaxrs.fortest.AegisTestBean;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.resources.ManyTags;
+import org.apache.cxf.jaxrs.resources.TagVO;
+import org.apache.cxf.jaxrs.resources.Tags;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class AegisJSONProviderTest extends Assert {
+    
+    @Test
+    public void testIsWriteable() {
+        MessageBodyWriter<Object> p = new AegisJSONProvider();
+        assertTrue(p.isWriteable(AegisTestBean.class, null, null, null));
+    }
+    
+    @Test
+    public void testIsReadable() {
+        MessageBodyReader<Object> p = new AegisJSONProvider();
+        assertFalse(p.isReadable(AegisTestBean.class, null, null, null));
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    @Ignore
+    public void testReadFrom() throws Exception {
+        AegisJSONProvider p = new AegisJSONProvider();
+        Map<String, String> namespaceMap = new HashMap<String, String>();
+        namespaceMap.put("http://fortest.jaxrs.cxf.apache.org", "ns1");
+        namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "ns2");
+        String data = "{\"ns1.AegisTestBean\":{\"@xsins.type\":\"ns1:AegisTestBean\","
+            + "\"ns1.boolValue\":true,\"ns1.strValue\":\"hovercraft\"}}";
+        
+        byte[] simpleBytes = data.getBytes("utf-8");
+        Object beanObject = p.readFrom((Class)AegisTestBean.class, null, null, 
+                                          null, null, new ByteArrayInputStream(simpleBytes));
+        AegisTestBean bean = (AegisTestBean) beanObject;
+        assertEquals("hovercraft", bean.getStrValue());
+        assertEquals(Boolean.TRUE, bean.getBoolValue());
+    }
+    
+    @Test
+    public void testWriteToWithXsiType() throws Exception {
+        String data = "{\"ns1.AegisTestBean\":{\"@ns2.type\":\"ns1:AegisTestBean\","
+            + "\"ns1.boolValue\":true,\"ns1.strValue\":\"hovercraft\"}}";
+        doTestWriteTo(data, true, true);
+    }
+    
+    @Test
+    public void testWriteToWithXsiTypeNoNamespaces() throws Exception {
+        String data = "{\"ns1.AegisTestBean\":{\"@xsins.type\":\"ns1:AegisTestBean\","
+            + "\"ns1.boolValue\":true,\"ns1.strValue\":\"hovercraft\"}}";
+        doTestWriteTo(data, true, false);
+    }
+    
+    @Test
+    public void testWriteToWithoutXsiType() throws Exception {
+        String data = "{\"ns1.AegisTestBean\":{"
+            + "\"ns1.boolValue\":true,\"ns1.strValue\":\"hovercraft\"}}";
+        doTestWriteTo(data, false, true);
+    }
+    
+    
+    private void doTestWriteTo(String data, boolean writeXsi, boolean setNsMap) throws Exception
{
+        AegisJSONProvider p = new AegisJSONProvider();
+        p.clearContexts();
+        p.setWriteXsiType(writeXsi);
+        if (setNsMap) {
+            Map<String, String> namespaceMap = new HashMap<String, String>();
+            namespaceMap.put("http://fortest.jaxrs.cxf.apache.org", "ns1");
+            namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "ns2");
+            p.setNamespaceMap(namespaceMap);
+        }    
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        AegisTestBean bean = new AegisTestBean();
+        bean.setBoolValue(Boolean.TRUE);
+        bean.setStrValue("hovercraft");
+        p.writeTo(bean, (Class)AegisTestBean.class, AegisTestBean.class, 
+                  AegisTestBean.class.getAnnotations(), 
+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        byte[] bytes = os.toByteArray();
+        String json = new String(bytes, "utf-8");
+        assertEquals(data, json);
+        
+    }
+    
+    @Test
+    public void testManyTags() throws Exception {
+        AegisJSONProvider p = new AegisJSONProvider();
+        p.clearContexts();
+        p.setWriteXsiType(false);
+        p.setSerializeAsArray(true);
+        
+        Tags tags = new Tags();
+        tags.addTag(createTag("a", "b"));
+        ManyTags many = new ManyTags();
+        many.setTags(tags);
+        
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        
+        p.writeTo(many, (Class)ManyTags.class, ManyTags.class, ManyTags.class.getAnnotations(),

+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        
+        String s = os.toString();
+        String data = "{\"ns1.ManyTags\":[{\"ns1.tags\":[{},{\"ns1.TagVO\""
+            + ":{\"ns1.group\":\"b\",\"ns1.name\":\"a\"}}]}]}";
+        assertEquals(data, s);
+    }
+    
+    private TagVO createTag(String name, String group) {
+        return new TagVO(name, group);
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisJSONProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisProviderTest.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/AegisProviderTest.java
Thu Jul 23 15:53:59 2009
@@ -37,7 +37,8 @@
 public class AegisProviderTest extends Assert {
     
     private static final String SIMPLE_BEAN_XML 
-        = "<ns1:AegisTestBean xmlns:ns1=\"http://fortest.jaxrs.cxf.apache.org\" "
+        = "<?xml version='1.0' encoding='UTF-8'?><ns1:AegisTestBean "
+            + "xmlns:ns1=\"http://fortest.jaxrs.cxf.apache.org\" "
             + "xmlns:ns2=\"http://www.w3.org/2001/XMLSchema-instance\" ns2:type=\"ns1:AegisTestBean\">"
             + "<ns1:boolValue>true</ns1:boolValue><ns1:strValue>hovercraft</ns1:strValue>"
             + "</ns1:AegisTestBean>";

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Thu Jul 23 15:53:59 2009
@@ -44,6 +44,7 @@
 import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.CollectionsResource;
 import org.apache.cxf.jaxrs.resources.ManyTags;
+import org.apache.cxf.jaxrs.resources.SuperBook;
 import org.apache.cxf.jaxrs.resources.TagVO;
 import org.apache.cxf.jaxrs.resources.TagVO2;
 import org.apache.cxf.jaxrs.resources.Tags;
@@ -298,16 +299,24 @@
     
     @Test
     public void testWriteIgnoreMixedContent() throws Exception {
-        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125}}", true);
+        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125}}",
+                           true, "book.xml");
+    }
+    
+    @Test
+    public void testWriteIgnoreMixedContent2() throws Exception {
+        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125,\"$\":\"books\"}}",
+                           true, "book2.xml");
     }
     
     @Test
     public void testWriteMixedContent() throws Exception {
-        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125,\"$\":\"\\n     \\n\"}}",
false);
+        doTestMixedContent("{\"Book\":{\"name\":\"CXF\",\"id\":125,\"$\":\"\\n     \\n\"}}",
+                           false, "book.xml");
     }
     
-    private void doTestMixedContent(String data, boolean ignore) throws Exception {
-        InputStream is = getClass().getResourceAsStream("book.xml");
+    private void doTestMixedContent(String data, boolean ignore, String fileName) throws
Exception {
+        InputStream is = getClass().getResourceAsStream(fileName);
         JAXBContext context = JAXBContext.newInstance(new Class[]{Books.class, Book.class});
         Unmarshaller um = context.createUnmarshaller();
         JAXBElement jaxbEl = um.unmarshal(new StreamSource(is), Books.class);
@@ -322,6 +331,44 @@
     }
     
     @Test
+    public void testWriteListOfDerivedTypes() throws Exception {
+        JSONProvider p = new JSONProvider();
+        Map<String, String> namespaceMap = new HashMap<String, String>();
+        namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
+        p.setNamespaceMap(namespaceMap);
+        Books2 books2 = new Books2();
+        books2.setBooks(Collections.singletonList(
+                            new SuperBook("CXF Rocks", 123L, 124L)));
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        
+        p.writeTo(books2, (Class)Books2.class, Books2.class, Books2.class.getAnnotations(),

+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        String s = os.toString();
+        String data = "{\"books2\":{\"books\":{\"@xsins.type\":\"superBook\",\"id\":123,"
+            + "\"name\":\"CXF Rocks\",\"superId\":124}}}";
+        assertEquals(data, s);
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadListOfDerivedTypes() throws Exception {
+        JSONProvider p = new JSONProvider();
+        Map<String, String> namespaceMap = new HashMap<String, String>();
+        namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
+        p.setNamespaceMap(namespaceMap);
+        String data = "{\"books2\":{\"books\":{\"@xsins.type\":\"superBook\",\"id\":123,"
+            + "\"name\":\"CXF Rocks\",\"superId\":124}}}";
+        byte[] bytes = data.getBytes();
+        Object books2Object = p.readFrom((Class)Books2.class, null, null, 
+                                          null, null, new ByteArrayInputStream(bytes));
+        Books2 books = (Books2)books2Object;
+        List<? extends Book> list = books.getBooks();
+        assertEquals(1, list.size());
+        SuperBook book = (SuperBook)list.get(0);
+        assertEquals(124L, book.getSuperId());
+    }
+    
+    @Test
     public void testWriteToListWithManyValues() throws Exception {
         JSONProvider p = new JSONProvider();
         Tags tags = new Tags();
@@ -394,4 +441,17 @@
         @XmlAnyElement(lax = true)
         protected List<Object> books;
     }
+    
+    @XmlRootElement()
+    public static class Books2 {
+        protected List<? extends Book> books;
+        
+        public void setBooks(List<? extends Book> list) {
+            books = list;
+        }
+        
+        public List<? extends Book> getBooks() {
+            return books;
+        }
+    }
 }

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml?rev=797117&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml Thu
Jul 23 15:53:59 2009
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
+<Book>
+ 
+     <name>CXF</name>
+     <id>125</id>
+     books
+</Book>

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/book2.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book_aegis.txt
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book_aegis.txt?rev=797117&r1=797116&r2=797117&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book_aegis.txt
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_add_book_aegis.txt
Thu Jul 23 15:53:59 2009
@@ -1 +1 @@
-<ns1:Book xmlns:ns1="http://jaxrs.systest.cxf.apache.org" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance"
ns2:type="ns1:Book"><ns1:id>124</ns1:id><ns1:name>CXF in Action - 2</ns1:name></ns1:Book>
\ No newline at end of file
+<?xml version='1.0' encoding='UTF-8'?><ns1:Book xmlns:ns1="http://jaxrs.systest.cxf.apache.org"
xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:type="ns1:Book"><ns1:id>124</ns1:id><ns1:name>CXF
in Action - 2</ns1:name></ns1:Book>
\ No newline at end of file



Mime
View raw message