You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/04/26 02:32:21 UTC

[cxf] 01/01: CXF-8516: Fixing jaxrs.spec.provider.jaxbcontext readWriteProviderTest

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch CXF-8516
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 385eea347ad622a3666688544a120758b37c5f58
Author: reta <dr...@gmail.com>
AuthorDate: Thu Apr 22 21:53:47 2021 -0400

    CXF-8516: Fixing jaxrs.spec.provider.jaxbcontext readWriteProviderTest
---
 .../apache/cxf/jaxrs/provider/ProviderFactory.java | 12 ++--
 .../cxf/jaxrs/provider/CustomJaxbProvider.java     | 70 ++++++++++++++++++++++
 .../cxf/jaxrs/provider/ProviderFactoryTest.java    | 18 ++++++
 .../cxf/systest/jaxrs/JAXRSAsyncClientTest.java    |  2 +
 .../jaxrs/provider/CXFJaxbElementProvider.java     | 70 ++++++++++++++++++++++
 .../jaxrs/provider/JAXBContextResolverTest.java    |  2 +
 6 files changed, 169 insertions(+), 5 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 673243d..917bfaa 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -890,19 +890,21 @@ public abstract class ProviderFactory {
             MessageBodyWriter<?> e1 = p1.getProvider();
             MessageBodyWriter<?> e2 = p2.getProvider();
 
-            int result = compareClasses(e1, e2);
-            if (result != 0) {
-                return result;
-            }
             List<MediaType> types1 =
                 JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e1), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
             List<MediaType> types2 =
                 JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e2), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
 
-            result = JAXRSUtils.compareSortedMediaTypes(types1, types2, JAXRSUtils.MEDIA_TYPE_QS_PARAM);
+            int result = JAXRSUtils.compareSortedMediaTypes(types1, types2, JAXRSUtils.MEDIA_TYPE_QS_PARAM);
+            if (result != 0) {
+                return result;
+            }
+            
+            result = compareClasses(e1, e2);
             if (result != 0) {
                 return result;
             }
+
             result = compareCustomStatus(p1, p2);
             if (result != 0) {
                 return result;
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/CustomJaxbProvider.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/CustomJaxbProvider.java
new file mode 100644
index 0000000..e8b343d
--- /dev/null
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/CustomJaxbProvider.java
@@ -0,0 +1,70 @@
+/**
+ * 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.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBElement;
+
+@Provider
+public class CustomJaxbProvider implements MessageBodyReader<JAXBElement<String>>, 
+        MessageBodyWriter<JAXBElement<String>> {
+
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        return JAXBElement.class.isAssignableFrom(type);
+    }
+
+    @Override
+    public long getSize(JAXBElement<String> t, Class<?> type, Type genericType, Annotation[] annotations,
+                        MediaType mediaType) {
+        return t.getValue().toString().length() + 2;
+    }
+
+    @Override
+    public void writeTo(JAXBElement<String> t, Class<?> type, Type genericType, Annotation[] annotations,
+                        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
+                        OutputStream entityStream) throws IOException, WebApplicationException {
+        entityStream.write("WriteInCXFJaxbProvider".getBytes());
+    }
+
+    @Override
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        return isWriteable(type, genericType, annotations, mediaType);
+    }
+
+    @Override
+    public JAXBElement<String> readFrom(Class<JAXBElement<String>> type, Type genericType,
+                                        Annotation[] annotations, MediaType mediaType,
+                                        MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
+        throws IOException, WebApplicationException {
+        return null;
+    }
+
+}
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index c9796ad..b88103b 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -56,6 +56,7 @@ import javax.ws.rs.ext.ReaderInterceptor;
 import javax.ws.rs.ext.WriterInterceptor;
 import javax.ws.rs.ext.WriterInterceptorContext;
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.validation.Schema;
 
@@ -82,12 +83,14 @@ import org.easymock.EasyMock;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public class ProviderFactoryTest {
@@ -425,6 +428,21 @@ public class ProviderFactoryTest {
                                                               MediaType.TEXT_XML_TYPE, new MessageImpl());
         assertSame(customJaxbWriter, provider);
     }
+    
+    @Test
+    public void testCustomProviderAndJaxbProvider() {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        CustomJaxbProvider provider = new CustomJaxbProvider();
+        pf.registerUserProvider(provider);
+        
+        MessageBodyReader<JAXBElement> customJaxbReader = pf.createMessageBodyReader(JAXBElement.class, 
+            String.class, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
+        assertThat(customJaxbReader, instanceOf(JAXBElementTypedProvider.class));
+
+        MessageBodyWriter<JAXBElement> customJaxbWriter = pf.createMessageBodyWriter(JAXBElement.class, 
+            String.class, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
+        assertThat(customJaxbWriter, instanceOf(JAXBElementTypedProvider.class));
+    }
 
     @Test
     public void testDataSourceReader() {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
index c8341c9..3cbf337 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
@@ -35,6 +35,7 @@ import javax.annotation.Priority;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.NotFoundException;
 import javax.ws.rs.ProcessingException;
+import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
@@ -493,6 +494,7 @@ public class JAXRSAsyncClientTest extends AbstractBusClientServerTestBase {
         };
     }
 
+    @Produces("application/xml")
     private static class FaultyBookWriter implements MessageBodyWriter<Book> {
 
         @Override
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbElementProvider.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbElementProvider.java
new file mode 100644
index 0000000..3f2520d
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbElementProvider.java
@@ -0,0 +1,70 @@
+/**
+ * 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.systest.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBElement;
+
+@Provider
+public class CXFJaxbElementProvider implements MessageBodyReader<JAXBElement<String>>, 
+        MessageBodyWriter<JAXBElement<String>> {
+
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        return JAXBElement.class.isAssignableFrom(type);
+    }
+
+    @Override
+    public long getSize(JAXBElement<String> t, Class<?> type, Type genericType, Annotation[] annotations,
+                        MediaType mediaType) {
+        return t.getValue().toString().length() + 2;
+    }
+
+    @Override
+    public void writeTo(JAXBElement<String> t, Class<?> type, Type genericType, Annotation[] annotations,
+                        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
+                        OutputStream entityStream) throws IOException, WebApplicationException {
+        entityStream.write("WriteInCXFJaxbProvider".getBytes());
+    }
+
+    @Override
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        return isWriteable(type, genericType, annotations, mediaType);
+    }
+
+    @Override
+    public JAXBElement<String> readFrom(Class<JAXBElement<String>> type, Type genericType,
+                                        Annotation[] annotations, MediaType mediaType,
+                                        MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
+        throws IOException, WebApplicationException {
+        return null;
+    }
+
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JAXBContextResolverTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JAXBContextResolverTest.java
index 2046e37..24cb5d6 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JAXBContextResolverTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JAXBContextResolverTest.java
@@ -48,7 +48,9 @@ public class JAXBContextResolverTest extends AbstractClientServerTestBase {
         protected org.apache.cxf.endpoint.Server createServer(Bus bus) throws Exception {
             final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
             sf.setResourceClasses(CXFResource.class);
+            sf.setProvider(new CXFJaxbElementProvider());
             sf.setProvider(new CXFJaxbContextResolver());
+            sf.setProvider(new CXFJaxbProvider());
             sf.setAddress("http://localhost:" + PORT + "/");
             return sf.create();
         }