/apache-cxf-2.4.8-src/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java

# · Java · 176 lines · 129 code · 29 blank · 18 comment · 4 complexity · e7514ba6e9cdbb0e46fafc1c5a840e94 MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.cxf.jaxrs.provider;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.io.StringReader;
  24. import java.lang.annotation.Annotation;
  25. import java.util.Collections;
  26. import javax.ws.rs.core.MediaType;
  27. import javax.ws.rs.ext.MessageBodyReader;
  28. import javax.xml.parsers.DocumentBuilder;
  29. import javax.xml.parsers.DocumentBuilderFactory;
  30. import javax.xml.stream.XMLStreamReader;
  31. import javax.xml.transform.Source;
  32. import javax.xml.transform.TransformerFactory;
  33. import javax.xml.transform.dom.DOMSource;
  34. import javax.xml.transform.sax.SAXSource;
  35. import javax.xml.transform.stream.StreamResult;
  36. import javax.xml.transform.stream.StreamSource;
  37. import org.w3c.dom.Document;
  38. import org.apache.cxf.helpers.DOMUtils;
  39. import org.apache.cxf.jaxrs.ext.MessageContext;
  40. import org.apache.cxf.jaxrs.ext.MessageContextImpl;
  41. import org.apache.cxf.jaxrs.impl.MetadataMap;
  42. import org.apache.cxf.message.Message;
  43. import org.apache.cxf.message.MessageImpl;
  44. import org.apache.cxf.staxutils.StaxSource;
  45. import org.apache.cxf.staxutils.StaxUtils;
  46. import org.apache.cxf.staxutils.transform.InTransformReader;
  47. import org.junit.Assert;
  48. import org.junit.Test;
  49. public class SourceProviderTest extends Assert {
  50. @Test
  51. public void testIsWriteable() {
  52. SourceProvider p = new SourceProvider();
  53. assertTrue(p.isWriteable(StreamSource.class, null, null, null)
  54. && p.isWriteable(DOMSource.class, null, null, null)
  55. && p.isWriteable(Source.class, null, null, null));
  56. }
  57. @Test
  58. public void testIsReadable() {
  59. SourceProvider p = new SourceProvider();
  60. assertTrue(p.isReadable(StreamSource.class, null, null, null)
  61. && p.isReadable(DOMSource.class, null, null, null)
  62. && p.isReadable(Source.class, null, null, null));
  63. }
  64. @Test
  65. public void testReadFrom() throws Exception {
  66. SourceProvider p = new TestSourceProvider();
  67. assertSame(StreamSource.class, verifyRead(p, StreamSource.class).getClass());
  68. assertSame(StaxSource.class, verifyRead(p, Source.class).getClass());
  69. assertSame(StaxSource.class, verifyRead(p, SAXSource.class).getClass());
  70. assertSame(StaxSource.class, verifyRead(p, StaxSource.class).getClass());
  71. assertSame(DOMSource.class, verifyRead(p, DOMSource.class).getClass());
  72. assertTrue(Document.class.isAssignableFrom(verifyRead(p, Document.class).getClass()));
  73. }
  74. @SuppressWarnings("unchecked")
  75. @Test
  76. public void testReadFromStreamReader() throws Exception {
  77. TestSourceProvider p = new TestSourceProvider();
  78. InputStream is = new ByteArrayInputStream("<test xmlns=\"http://bar\"/>".getBytes());
  79. XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
  80. reader = new InTransformReader(reader,
  81. Collections.singletonMap("{http://bar}test", "test2"),
  82. null,
  83. null,
  84. null,
  85. false);
  86. p.getMessage().setContent(XMLStreamReader.class, reader);
  87. Source source = (Source)p.readFrom((Class)Source.class,
  88. null, null, null, null, is);
  89. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  90. TransformerFactory.newInstance().newTransformer()
  91. .transform(source, new StreamResult(bos));
  92. assertTrue(bos.toString().contains("test2"));
  93. }
  94. @Test
  95. public void testWriteToDocument() throws Exception {
  96. SourceProvider p = new SourceProvider();
  97. Document doc = DOMUtils.readXml(new StringReader("<test/>"));
  98. ByteArrayOutputStream os = new ByteArrayOutputStream();
  99. p.writeTo(doc, Document.class, Document.class,
  100. new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
  101. new MetadataMap<String, Object>(), os);
  102. String s = os.toString();
  103. assertEquals("<test/>", s);
  104. }
  105. @Test
  106. public void testReadFromWithPreferredFormat() throws Exception {
  107. TestSourceProvider p = new TestSourceProvider();
  108. p.getMessage().put("source-preferred-format", "sax");
  109. assertSame(StaxSource.class, verifyRead(p, Source.class).getClass());
  110. }
  111. @Test
  112. public void testWriteTo() throws Exception {
  113. SourceProvider p = new TestSourceProvider();
  114. StreamSource s = new StreamSource(new ByteArrayInputStream("<test/>".getBytes()));
  115. ByteArrayOutputStream os = new ByteArrayOutputStream();
  116. p.writeTo(s, null, null, null, MediaType.APPLICATION_XML_TYPE,
  117. new MetadataMap<String, Object>(), os);
  118. assertTrue(os.toString().contains("<test/>"));
  119. os = new ByteArrayOutputStream();
  120. p.writeTo(createDomSource(), null, null, null, MediaType.APPLICATION_XML_TYPE,
  121. new MetadataMap<String, Object>(), os);
  122. assertTrue(os.toString().contains("<test/>"));
  123. }
  124. @SuppressWarnings("unchecked")
  125. private <T> T verifyRead(MessageBodyReader p, Class<T> type) throws Exception {
  126. return (T)p.readFrom(type,
  127. null, null, null, null,
  128. new ByteArrayInputStream("<test/>".getBytes()));
  129. }
  130. private DOMSource createDomSource() throws Exception {
  131. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  132. DocumentBuilder builder;
  133. builder = factory.newDocumentBuilder();
  134. return new DOMSource(builder.parse(new ByteArrayInputStream("<test/>".getBytes())));
  135. }
  136. private static class TestSourceProvider extends SourceProvider {
  137. private Message m = new MessageImpl();
  138. public TestSourceProvider() {
  139. }
  140. public Message getMessage() {
  141. return m;
  142. }
  143. protected MessageContext getContext() {
  144. return new MessageContextImpl(m);
  145. };
  146. }
  147. }