Skip to content

Commit c04e37b

Browse files
committed
[test] Improve coverage for fn:transform test
Tweak transformation’s CompileTimeURIResolver class to allow easier subclassing, to let unit test exercise it more easily.
1 parent d136666 commit c04e37b

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/URIResolution.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue base
6969
}
7070
}
7171

72-
static public class CompileTimeURIResolver implements URIResolver {
72+
public static class CompileTimeURIResolver implements URIResolver {
7373

7474
private final XQueryContext xQueryContext;
7575
private final Expression containingExpression;
@@ -86,7 +86,7 @@ public Source resolve(final String href, final String base) throws TransformerEx
8686
final AnyURIValue baseURI = new AnyURIValue(base);
8787
final AnyURIValue hrefURI = new AnyURIValue(href);
8888
var resolved = resolveURI(hrefURI, baseURI);
89-
return resolveDocument(resolved.getStringValue(), xQueryContext, containingExpression);
89+
return resolveDocument(resolved.getStringValue());
9090
} catch (URISyntaxException e) {
9191
throw new TransformerException("Failed to resolve " +
9292
href +
@@ -99,6 +99,10 @@ public Source resolve(final String href, final String base) throws TransformerEx
9999
base, e);
100100
}
101101
}
102+
103+
protected Source resolveDocument(final String location) throws XPathException {
104+
return URIResolution.resolveDocument(location, xQueryContext, containingExpression);
105+
}
102106
}
103107

104108
/**

exist-core/src/test/java/org/exist/xquery/functions/fn/transform/FunTransformTest.java

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
package org.exist.xquery.functions.fn.transform;
2323

2424
import org.exist.xquery.XPathException;
25+
import org.exist.xquery.XQueryContext;
2526
import org.exist.xquery.value.AnyURIValue;
2627
import org.junit.jupiter.api.Test;
2728

29+
import javax.xml.transform.Source;
30+
import javax.xml.transform.TransformerException;
2831
import java.math.BigDecimal;
2932
import java.net.URISyntaxException;
3033

@@ -49,20 +52,23 @@ void versionNumbers() throws Transform.PendingException {
4952
assertEquals(Options.XSLTVersion.fromDecimal(new BigDecimal("3.1")), Options.XSLTVersion.fromDecimal(new BigDecimal("3.1")));
5053
}
5154

52-
@Test void badVersionNumber() throws Transform.PendingException {
55+
@Test
56+
void badVersionNumber() throws Transform.PendingException {
5357

5458
assertThrows(Transform.PendingException.class, () -> {
5559
Options.XSLTVersion version311 = Options.XSLTVersion.fromDecimal(new BigDecimal("3.11"));
5660
});
5761
}
5862

59-
@Test public void emptyResolution() throws XPathException, URISyntaxException {
63+
@Test
64+
public void emptyResolution() throws XPathException, URISyntaxException {
6065
var base = new AnyURIValue("");
6166
var relative = new AnyURIValue("path/to/functions1.xsl");
6267
assertEquals(new AnyURIValue("path/to/functions1.xsl"), URIResolution.resolveURI(relative, base));
6368
}
6469

65-
@Test public void resolution() throws XPathException, URISyntaxException {
70+
@Test
71+
public void resolution() throws XPathException, URISyntaxException {
6672
var base = new AnyURIValue("xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl");
6773
var relative = new AnyURIValue("functions1.xsl");
6874
assertEquals(new AnyURIValue("xmldb:exist:/db/apps/fn_transform/functions1.xsl"),
@@ -107,4 +113,67 @@ void versionNumbers() throws Transform.PendingException {
107113
assertEquals(new AnyURIValue("https://127.0.0.1:8088/a/b/c/functions1.xsl"),
108114
URIResolution.resolveURI(relative5, base5));
109115
}
116+
117+
/**
118+
* Create some UT coverage of the CompileTimeURIResolver
119+
* This is more significantly exercised by XQTS tests
120+
* Results are the same as above, wrapped in a {@code Source}
121+
*
122+
* @throws TransformerException
123+
*/
124+
@Test
125+
public void resolverObject() throws TransformerException {
126+
var resolver = new URIResolution.CompileTimeURIResolver(new XQueryContext(), null) {
127+
@Override protected SourceWithLocation resolveDocument(final String location) {
128+
return new SourceWithLocation("RESOLVED::" + location);
129+
}
130+
};
131+
132+
assertEquals("RESOLVED::xmldb:exist:/db/apps/fn_transform/functions1.xsl",
133+
((SourceWithLocation)resolver.resolve("functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
134+
assertEquals("RESOLVED::xmldb:exist:/functions1.xsl",
135+
((SourceWithLocation)resolver.resolve("/functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
136+
137+
assertEquals("RESOLVED::xmldb:exist:/fn_transform/functions1.xsl",
138+
((SourceWithLocation)resolver.resolve("/fn_transform/functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
139+
140+
assertEquals("RESOLVED::xmldb:exist:/fn_transform/functions1.xsl",
141+
((SourceWithLocation)resolver.resolve("/fn_transform/functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
142+
143+
assertEquals("RESOLVED::https://127.0.0.1:8088/db/apps/fn_transform/functions1.xsl",
144+
((SourceWithLocation)resolver.resolve("functions1.xsl", "https://127.0.0.1:8088/db/apps/fn_transform/tei-toc2.xsl")).location);
145+
146+
assertEquals("RESOLVED::https://127.0.0.1:8088/db/apps/fn_transform/functions1.xsl",
147+
((SourceWithLocation)resolver.resolve("functions1.xsl", "https://127.0.0.1:8088/db/apps/fn_transform/")).location);
148+
149+
assertEquals("RESOLVED::https://127.0.0.1:8088/functions1.xsl",
150+
((SourceWithLocation)resolver.resolve("/functions1.xsl", "https://127.0.0.1:8088/db/apps/fn_transform/")).location);
151+
152+
assertEquals("RESOLVED::xmldb:exist:///a/b/c/functions1.xsl",
153+
((SourceWithLocation)resolver.resolve("xmldb:exist:///a/b/c/functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
154+
155+
assertEquals("RESOLVED::https://127.0.0.1:8088/a/b/c/functions1.xsl",
156+
((SourceWithLocation)resolver.resolve("https://127.0.0.1:8088/a/b/c/functions1.xsl", "xmldb:exist:///db/apps/fn_transform/tei-toc2.xsl")).location);
157+
}
158+
159+
/**
160+
* Skeleton implementation for test
161+
*/
162+
private static class SourceWithLocation implements Source {
163+
164+
final String location;
165+
SourceWithLocation(final String location) {
166+
this.location = location;
167+
}
168+
169+
@Override
170+
public void setSystemId(String systemId) {
171+
172+
}
173+
174+
@Override
175+
public String getSystemId() {
176+
return null;
177+
}
178+
}
110179
}

0 commit comments

Comments
 (0)