Project

Profile

Help

Bug #3279 ยป TestPositionalFilter.java

Gunther Rademacher, 2017-06-14 09:53

 
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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.XdmItem;
import net.sf.saxon.trans.XPathException;

// Pass: 20, Fail: 12

public class TestPositionalFilter {
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[][] singleResultQueries() {
String queries[] = {
"(for $x in saxon:stream(doc('uriresolver:resolve')/*/*) return $x)[FILTER]",
"for $x in saxon:stream(doc('uriresolver:resolve')/*/*)[FILTER] return $x",
"saxon:stream(doc('uriresolver:resolve')/*/*)[FILTER]",
"(let $x := saxon:stream(doc('uriresolver:resolve')/*/*) return $x)[FILTER]",
};
String filters[] = {
"[1]",
"[position() = 1]",
"[position() <= 1]",
"[position() le 1]",
};
List<Object[]> testCases = new ArrayList<>();
for (String query : queries)
for (String filter : filters)
testCases.add(new Object[]{query.replace("[FILTER]", filter)});
return testCases.toArray(new Object[testCases.size()][]);
}

@Test(dataProvider = "singleResultQueries", timeOut = 1000)
public void testGetOneResult(String query) throws SaxonApiException {
XQueryExecutable xqueryExecutable = xqueryCompiler.compile(query);
XQueryEvaluator xqueryEvaluator = xqueryExecutable.load();
xqueryEvaluator.iterator().next();
xqueryEvaluator.close();
}

@Test(dataProvider="singleResultQueries", timeOut = 1000)
public void testGetAllResults(String query) throws SaxonApiException {
XQueryExecutable xqueryExecutable = xqueryCompiler.compile(query);
XQueryEvaluator xqueryEvaluator = xqueryExecutable.load();
for (@SuppressWarnings("unused") XdmItem item : xqueryEvaluator)
;
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)