|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery.functions.request; |
| 23 | + |
| 24 | +import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream; |
| 25 | +import org.exist.http.servlets.RequestWrapper; |
| 26 | +import org.exist.storage.BrokerPool; |
| 27 | +import org.exist.storage.DBBroker; |
| 28 | +import org.exist.util.Configuration; |
| 29 | +import org.exist.util.XMLReaderObjectFactory; |
| 30 | +import org.exist.util.XMLReaderPool; |
| 31 | +import org.exist.xquery.XPathException; |
| 32 | +import org.exist.xquery.XQueryContext; |
| 33 | +import org.exist.xquery.value.Sequence; |
| 34 | +import org.junit.Test; |
| 35 | +import org.w3c.dom.Document; |
| 36 | +import org.xml.sax.SAXException; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.io.StringWriter; |
| 41 | +import java.nio.charset.StandardCharsets; |
| 42 | +import java.util.Properties; |
| 43 | + |
| 44 | +import static org.easymock.EasyMock.*; |
| 45 | +import static org.junit.Assert.*; |
| 46 | + |
| 47 | +/** |
| 48 | + * Unlike {@link GetDataTest} this test tries to test the code of |
| 49 | + * the {@link GetData} class directly. |
| 50 | + * |
| 51 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 52 | + */ |
| 53 | +public class GetData2Test { |
| 54 | + |
| 55 | + @Test |
| 56 | + public void xmlChunkedTransferNonBlockingAvailable() throws XPathException, IOException, SAXException { |
| 57 | + final String content = "<hello>world</hello>"; |
| 58 | + try (final InputStream is = new UnsynchronizedByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { |
| 59 | + |
| 60 | + final RequestWrapper mockRequestWrapper = createNiceMock(RequestWrapper.class); |
| 61 | + final Configuration mockConfiguration = createNiceMock(Configuration.class); |
| 62 | + final BrokerPool mockBrokerPool = createNiceMock(BrokerPool.class); |
| 63 | + final DBBroker mockBroker = createNiceMock(DBBroker.class); |
| 64 | + final XMLReaderObjectFactory xmlReaderObjectFactory = new XMLReaderObjectFactory(); |
| 65 | + final XMLReaderPool xmlReaderPool = new XMLReaderPool(xmlReaderObjectFactory, 20, 0); |
| 66 | + |
| 67 | + expect(mockRequestWrapper.getSession(false)).andReturn(null); |
| 68 | + expect(mockRequestWrapper.getContentLength()).andReturn(-1l); |
| 69 | + expect(mockRequestWrapper.getHeader("Transfer-Encoding")).andReturn("chunked"); |
| 70 | + expect(mockRequestWrapper.getInputStream()).andReturn(is); |
| 71 | + expect(mockRequestWrapper.getContentType()).andReturn("application/xml"); |
| 72 | + |
| 73 | + expect(mockConfiguration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY)).andReturn("org.exist.util.io.FileFilterInputStreamCache"); |
| 74 | + expect(mockConfiguration.getProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, Boolean.FALSE)).andReturn(Boolean.FALSE); |
| 75 | + |
| 76 | + expect(mockBrokerPool.getConfiguration()).andReturn(mockConfiguration); |
| 77 | + expect(mockBrokerPool.getActiveBroker()).andReturn(mockBroker).anyTimes(); |
| 78 | + expect(mockBrokerPool.getParserPool()).andReturn(xmlReaderPool).anyTimes(); |
| 79 | + expect(mockBroker.getBrokerPool()).andReturn(mockBrokerPool).anyTimes(); |
| 80 | + |
| 81 | + replay(mockRequestWrapper, mockConfiguration, mockBrokerPool, mockBroker); |
| 82 | + |
| 83 | + xmlReaderObjectFactory.configure(mockConfiguration); |
| 84 | + |
| 85 | + final XQueryContext context = new XQueryContext(mockBrokerPool, mockConfiguration, null); |
| 86 | + context.setHttpContext(new XQueryContext.HttpContext(mockRequestWrapper, null)); |
| 87 | + |
| 88 | + final GetData getData = new GetData(context); |
| 89 | + final Sequence result = getData.eval((Sequence[]) null, (Sequence) null); |
| 90 | + assertNotNull(result); |
| 91 | + assertFalse(result.isEmpty()); |
| 92 | + assertEquals(1, result.getItemCount()); |
| 93 | + assertTrue(result.itemAt(0) instanceof Document); |
| 94 | + assertEquals("hello", ((Document) result.itemAt(0)).getDocumentElement().getLocalName()); |
| 95 | + assertEquals("world", ((Document) result.itemAt(0)).getDocumentElement().getTextContent()); |
| 96 | + |
| 97 | + verify(mockRequestWrapper, mockConfiguration, mockBrokerPool, mockBroker); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void xmlChunkedTransferNonBlockingNoneAvailable() throws XPathException, IOException { |
| 103 | + final String content = "<hello>world</hello>"; |
| 104 | + try (final InputStream is = new ZeroAvailableInputStream(content.getBytes(StandardCharsets.UTF_8))) { |
| 105 | + |
| 106 | + final RequestWrapper mockRequestWrapper = createNiceMock(RequestWrapper.class); |
| 107 | + final Configuration mockConfiguration = createNiceMock(Configuration.class); |
| 108 | + final BrokerPool mockBrokerPool = createNiceMock(BrokerPool.class); |
| 109 | + final DBBroker mockBroker = createNiceMock(DBBroker.class); |
| 110 | + final XMLReaderObjectFactory xmlReaderObjectFactory = new XMLReaderObjectFactory(); |
| 111 | + final XMLReaderPool xmlReaderPool = new XMLReaderPool(xmlReaderObjectFactory, 20, 0); |
| 112 | + |
| 113 | + expect(mockRequestWrapper.getSession(false)).andReturn(null); |
| 114 | + expect(mockRequestWrapper.getContentLength()).andReturn(-1l); |
| 115 | + expect(mockRequestWrapper.getHeader("Transfer-Encoding")).andReturn("chunked"); |
| 116 | + expect(mockRequestWrapper.getInputStream()).andReturn(is); |
| 117 | + expect(mockRequestWrapper.getContentType()).andReturn("application/xml"); |
| 118 | + |
| 119 | + expect(mockConfiguration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY)).andReturn("org.exist.util.io.FileFilterInputStreamCache"); |
| 120 | + expect(mockConfiguration.getProperty(XQueryContext.PROPERTY_XQUERY_RAISE_ERROR_ON_FAILED_RETRIEVAL, Boolean.FALSE)).andReturn(Boolean.FALSE); |
| 121 | + |
| 122 | + expect(mockBrokerPool.getConfiguration()).andReturn(mockConfiguration); |
| 123 | + expect(mockBrokerPool.getActiveBroker()).andReturn(mockBroker).anyTimes(); |
| 124 | + expect(mockBrokerPool.getParserPool()).andReturn(xmlReaderPool).anyTimes(); |
| 125 | + expect(mockBroker.getBrokerPool()).andReturn(mockBrokerPool).anyTimes(); |
| 126 | + |
| 127 | + replay(mockRequestWrapper, mockConfiguration, mockBrokerPool, mockBroker); |
| 128 | + |
| 129 | + xmlReaderObjectFactory.configure(mockConfiguration); |
| 130 | + |
| 131 | + final XQueryContext context = new XQueryContext(mockBrokerPool, mockConfiguration, null); |
| 132 | + context.setHttpContext(new XQueryContext.HttpContext(mockRequestWrapper, null)); |
| 133 | + |
| 134 | + final GetData getData = new GetData(context); |
| 135 | + final Sequence result = getData.eval((Sequence[]) null, (Sequence) null); |
| 136 | + assertNotNull(result); |
| 137 | + assertFalse(result.isEmpty()); |
| 138 | + assertEquals(1, result.getItemCount()); |
| 139 | + assertTrue(result.itemAt(0) instanceof Document); |
| 140 | + assertEquals("hello", ((Document) result.itemAt(0)).getDocumentElement().getLocalName()); |
| 141 | + assertEquals("world", ((Document) result.itemAt(0)).getDocumentElement().getTextContent()); |
| 142 | + |
| 143 | + verify(mockRequestWrapper, mockConfiguration, mockBrokerPool, mockBroker); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public static class ZeroAvailableInputStream extends UnsynchronizedByteArrayInputStream { |
| 148 | + |
| 149 | + public ZeroAvailableInputStream(final byte[] data) { |
| 150 | + super(data); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public int available() { |
| 155 | + return 0; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments