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/23 01:55:20 UTC

[cxf] branch 3.3.x-fixes updated: Added test case for ContextResolver

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

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


The following commit(s) were added to refs/heads/3.3.x-fixes by this push:
     new 3fa39fc  Added test case for ContextResolver<JAXBContext>
3fa39fc is described below

commit 3fa39fcbd4dc663dab394dc842d2978f6aa55e69
Author: reta <dr...@gmail.com>
AuthorDate: Thu Apr 22 20:06:45 2021 -0400

    Added test case for ContextResolver<JAXBContext>
    
    (cherry picked from commit e7aad940d85a17252f5500d32cdac18546dbd2b5)
    (cherry picked from commit 3165d7514d65a6da040e0148b23b304c7e4101db)
---
 .../jaxrs/provider/CXFJaxbContextResolver.java     | 332 +++++++++++++++++++++
 .../jaxrs/provider/JAXBContextResolverTest.java    |  82 +++++
 2 files changed, 414 insertions(+)

diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbContextResolver.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbContextResolver.java
new file mode 100644
index 0000000..151e733
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/CXFJaxbContextResolver.java
@@ -0,0 +1,332 @@
+/**
+ * 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.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URL;
+
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.PropertyException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.UnmarshallerHandler;
+import javax.xml.bind.ValidationEventHandler;
+import javax.xml.bind.Validator;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.attachment.AttachmentMarshaller;
+import javax.xml.bind.attachment.AttachmentUnmarshaller;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.validation.Schema;
+
+import org.w3c.dom.Node;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+
+@Provider
+public class CXFJaxbContextResolver implements ContextResolver<JAXBContext> {
+    @SuppressWarnings("rawtypes")
+    public static class SomeUnmarshaller implements Unmarshaller {
+        @Override
+        public <A extends XmlAdapter> A getAdapter(Class<A> type) {
+            return null;
+        }
+
+        @Override
+        public AttachmentUnmarshaller getAttachmentUnmarshaller() {
+            return null;
+        }
+
+        @Override
+        public ValidationEventHandler getEventHandler() throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Listener getListener() {
+            return null;
+        }
+
+        @Override
+        public Object getProperty(String name) throws PropertyException {
+            return null;
+        }
+
+        @Override
+        public Schema getSchema() {
+            return null;
+        }
+
+        @Override
+        public UnmarshallerHandler getUnmarshallerHandler() {
+            return null;
+        }
+
+        @Override
+        public boolean isValidating() throws JAXBException {
+            return false;
+        }
+
+        @Override
+        public void setAdapter(XmlAdapter adapter) {
+        }
+
+        @Override
+        public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
+        }
+
+        @Override
+        public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
+        }
+
+        @Override
+        public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
+        }
+
+        @Override
+        public void setListener(Listener listener) {
+        }
+
+        @Override
+        public void setProperty(String name, Object value) throws PropertyException {
+        }
+
+        @Override
+        public void setSchema(Schema schema) {
+        }
+
+        @Override
+        public void setValidating(boolean validating) throws JAXBException {
+        }
+
+        @Override
+        public Object unmarshal(File f) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(InputStream is) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(Reader reader) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(URL url) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(InputSource source) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(Node node) throws JAXBException {
+            return node.toString();
+        }
+
+        @Override
+        public Object unmarshal(Source source) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object unmarshal(XMLStreamReader reader) throws JAXBException {
+            return getClass().getSimpleName();
+        }
+
+        @Override
+        public Object unmarshal(XMLEventReader reader) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public <T> JAXBElement<T> unmarshal(Node node, Class<T> declaredType) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> declaredType) throws JAXBException {
+            String name = getClass().getSimpleName();
+            @SuppressWarnings("unchecked")
+            JAXBElement<T> el = new JAXBElement<T>(new QName(name), declaredType, (T) name);
+            return el;
+        }
+
+        @Override
+        public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> declaredType) throws JAXBException {
+            return null;
+        }
+    }
+
+      
+    @SuppressWarnings("rawtypes")
+    public static class SomeMarshaller implements Marshaller {
+        @Override
+        public <A extends XmlAdapter> A getAdapter(Class<A> arg0) {
+            return null;
+        }
+
+        @Override
+        public AttachmentMarshaller getAttachmentMarshaller() {
+            return null;
+        }
+
+        @Override
+        public ValidationEventHandler getEventHandler() throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Listener getListener() {
+            return null;
+        }
+
+        @Override
+        public Node getNode(Object arg0) throws JAXBException {
+            return null;
+        }
+
+        @Override
+        public Object getProperty(String arg0) throws PropertyException {
+            return null;
+        }
+
+        @Override
+        public Schema getSchema() {
+            return null;
+        }
+
+        @Override
+        public void marshal(Object arg0, Result arg1) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object arg0, OutputStream arg1) throws JAXBException {
+            try {
+                String value = ((JAXBElement) arg0).getValue().toString();
+                arg1.write(value.getBytes());
+                arg1.write(getClass().getSimpleName().getBytes());
+                arg1.flush();
+            } catch (IOException e) {
+                throw new JAXBException(e);
+            }
+        }
+
+        @Override
+        public void marshal(Object arg0, File arg1) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object jaxbElement, Writer writer) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object jaxbElement, ContentHandler handler) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object jaxbElement, Node node) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object jaxbElement, XMLStreamWriter writer) throws JAXBException {
+        }
+
+        @Override
+        public void marshal(Object jaxbElement, XMLEventWriter writer) throws JAXBException {
+        }
+
+        @Override
+        public void setAdapter(XmlAdapter adapter) {
+        }
+
+        @Override
+        public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
+        }
+
+        @Override
+        public void setAttachmentMarshaller(AttachmentMarshaller am) {
+        }
+
+        @Override
+        public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
+        }
+
+        @Override
+        public void setListener(Listener listener) {
+        }
+
+        @Override
+        public void setProperty(String name, Object value) throws PropertyException {
+        }
+
+        @Override
+        public void setSchema(Schema schema) {
+        }
+    }
+    
+    @SuppressWarnings("deprecation")
+    public static class SomeJaxbContext extends JAXBContext {
+        @Override
+        public Marshaller createMarshaller() throws JAXBException {
+            return new SomeMarshaller();
+        }
+
+        @Override
+        public Unmarshaller createUnmarshaller() throws JAXBException {
+            return new SomeUnmarshaller();
+        }
+
+        @Override
+        public Validator createValidator() throws JAXBException {
+            return null;
+        }
+    }
+    
+    @Override
+    public JAXBContext getContext(Class<?> type) {
+        return  new SomeJaxbContext();
+    }
+}
\ No newline at end of file
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
new file mode 100644
index 0000000..2046e37
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JAXBContextResolverTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBElement;
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.testutil.common.AbstractClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractServerTestServerBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class JAXBContextResolverTest extends AbstractClientServerTestBase {
+    public static final String PORT = allocatePort(JAXBContextResolverTest.class);
+
+    public static class Server extends AbstractServerTestServerBase {
+        @Override
+        protected org.apache.cxf.endpoint.Server createServer(Bus bus) throws Exception {
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            sf.setResourceClasses(CXFResource.class);
+            sf.setProvider(new CXFJaxbContextResolver());
+            sf.setAddress("http://localhost:" + PORT + "/");
+            return sf.create();
+        }
+
+        public static void main(String[] args) throws Exception {
+            new Server().start();
+        }
+    }
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        // keep out of process due to stack traces testing failures
+        assertTrue("server did not launch correctly", launchServer(Server.class, true));
+    }
+
+    @Test
+    public void testNoResultsAreReturned() throws Exception {
+        WebClient client = WebClient.create("http://localhost:" + PORT + "/resource/jaxb");
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(3000000);
+
+        List<String> values = new ArrayList<>();
+        values.add(MediaType.APPLICATION_XML);
+        client.getHeaders().put("content-type", values);
+        JAXBElement<String> test = new JAXBElement<>(new QName("org.apache.cxf", "jaxbelement"),
+                                                           String.class, "test");
+        Response response = client.post(test);
+        String result = response.readEntity(String.class);
+        assertThat(result, equalTo("SomeUnmarshallerSomeMarshaller"));
+    }
+}