Project

Profile

Help

Bug #3280 ยป TestStreamingQueries.java

Gunther Rademacher, 2017-06-14 10:15

 
import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.saxonica.config.EnterpriseConfiguration;

import net.sf.saxon.Configuration;
import net.sf.saxon.lib.StandardURIResolver;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryEvaluator;
import net.sf.saxon.s9api.XQueryExecutable;
import net.sf.saxon.s9api.XdmSequenceIterator;
import net.sf.saxon.trans.XPathException;

public class TestStreamingQueries {
private XQueryCompiler xqueryCompiler;
@BeforeTest
public void beforeTest() {
Configuration configuration = new EnterpriseConfiguration();
configuration.setURIResolver(new StandardURIResolver() {
@Override
public Source resolve(String href, String base) throws XPathException {
return new StreamSource(strawberryFieldsForever());
}
});
Processor processor = new Processor(configuration);
xqueryCompiler = processor.newXQueryCompiler();
}

@DataProvider
public Object[][] queries() {
return new Object[][] {
{"for $x in saxon:stream(doc('uriresolver:resolve')/*/*) return string($x)"},
{"for $x in saxon:stream(doc('uriresolver:resolve')/*/*) return $x"},
{"for $x in saxon:stream(doc('uriresolver:resolve')/*/*) return $x/*"},
{"for $x in saxon:stream(doc('uriresolver:resolve')/*/*)/* return $x"},
{"saxon:stream(doc('uriresolver:resolve')/*/*)/string()"},
{"saxon:stream(doc('uriresolver:resolve')/*/*)"},
{"saxon:stream(doc('uriresolver:resolve')/*/*)/*"},
};
}

@Test(dataProvider="queries", timeOut = 5000)
public void testGetSomeResults(String query) throws SaxonApiException {
XQueryExecutable xqueryExecutable = xqueryCompiler.compile(query);
XQueryEvaluator xqueryEvaluator = xqueryExecutable.load();
XdmSequenceIterator iterator = xqueryEvaluator.iterator();
for (int i = 0; i < 32768; ++i) {
iterator.next();
}
xqueryEvaluator.close();
}

private InputStream strawberryFieldsForever() {
return new InputStream() {
private static final String intro =
"<song>\n";
private static final String chorus =
" <chorus>\n" +
" <line>Let me take you down</line>\n" +
" <line>Cause I'm going to Strawberry Fields</line>\n" +
" <line>Nothing is real</line>\n" +
" <line>And nothing to get hung about</line>\n" +
" <line>Strawberry Fields forever</line>\n" +
" </chorus>\n";
private int i = -intro.length();

@Override
public int read() throws IOException {
if (i++ < 0) {
return intro.charAt(i + intro.length() - 1);
}
else {
i %= chorus.length();
return chorus.charAt(i);
}
}
};
}

}
    (1-1/1)