Skip to content

Commit 5d519af

Browse files
Test function result caching (#399)
* Build some tests for function result caching. * Adjusted focus handling to properly honor focus independence.
1 parent 0bf1dd7 commit 5d519af

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/impl/AbstractFunction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ protected static ISequence<?> convertSequence(
219219
return ISequence.of(stream);
220220
}
221221

222+
@Nullable
222223
private IItem getContextItem(@NonNull ISequence<?> focus) {
223-
IItem contextItem = focus.getFirstItem(true);
224+
IItem contextItem = isFocusDependent()
225+
? focus.getFirstItem(true)
226+
: null;
224227
if (isFocusDependent() && contextItem == null) {
225228
throw new DynamicMetapathException(DynamicMetapathException.DYNAMIC_CONTEXT_ABSENT, "The context is empty");
226229
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* SPDX-FileCopyrightText: none
3+
* SPDX-License-Identifier: CC0-1.0
4+
*/
5+
6+
package gov.nist.secauto.metaschema.core.metapath.function;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import static org.junit.jupiter.api.Assertions.assertSame;
10+
11+
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
12+
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
13+
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
14+
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
15+
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
class DefaultFunctionTest {
20+
21+
/**
22+
* Ensure that the same call to the fn:boolean function produces the exact same
23+
* result instances.
24+
*/
25+
@Test
26+
void testSameResultCurrentDateTime() {
27+
StaticContext staticContext = StaticContext.builder()
28+
.build();
29+
30+
IFunction function = staticContext.lookupFunction("current-dateTime", 0);
31+
assertNotNull(function);
32+
33+
DynamicContext dynamicContext = new DynamicContext(staticContext);
34+
35+
ISequence<?> result1 = function.execute(
36+
CollectionUtil.emptyList(),
37+
dynamicContext,
38+
ISequence.empty());
39+
ISequence<?> result2 = function.execute(
40+
CollectionUtil.emptyList(),
41+
dynamicContext,
42+
ISequence.empty());
43+
44+
assertSame(result1, result2);
45+
}
46+
47+
/**
48+
* Ensure that the same call to the fn:boolean function with the same argument
49+
* produces the exact same result instances.
50+
*/
51+
@Test
52+
void testSameResultBoolean() {
53+
StaticContext staticContext = StaticContext.builder()
54+
.build();
55+
56+
IFunction function = staticContext.lookupFunction("boolean", 1);
57+
assertNotNull(function);
58+
59+
DynamicContext dynamicContext = new DynamicContext(staticContext);
60+
61+
ISequence<?> result1 = function.execute(
62+
CollectionUtil.singletonList(ISequence.of(IBooleanItem.valueOf(true))),
63+
dynamicContext,
64+
ISequence.empty());
65+
ISequence<?> result2 = function.execute(
66+
CollectionUtil.singletonList(ISequence.of(IBooleanItem.valueOf(true))),
67+
dynamicContext,
68+
ISequence.empty());
69+
70+
assertSame(result1, result2);
71+
}
72+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SPDX-FileCopyrightText: none
3+
* SPDX-License-Identifier: CC0-1.0
4+
*/
5+
6+
package gov.nist.secauto.metaschema.databind;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import static org.junit.jupiter.api.Assertions.assertSame;
10+
11+
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
12+
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
13+
import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
14+
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
15+
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
16+
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.nio.file.Paths;
21+
22+
class FnDocTest {
23+
24+
/**
25+
* Ensure that the same document loaded twice produces the exact same node
26+
* instances.
27+
*/
28+
@Test
29+
void testSameNode() {
30+
IBindingContext bindingContext = IBindingContext.newInstance();
31+
32+
StaticContext staticContext = StaticContext.builder()
33+
.baseUri(Paths.get(".").toUri())
34+
.build();
35+
DynamicContext dynamicContext = new DynamicContext(staticContext);
36+
dynamicContext.setDocumentLoader(bindingContext.newBoundLoader());
37+
38+
IFunction function = staticContext.lookupFunction("doc", 1);
39+
assertNotNull(function);
40+
41+
ISequence<?> result1 = function.execute(
42+
CollectionUtil
43+
.singletonList(ISequence.of(IStringItem.valueOf("src/test/resources/metaschema/simple/metaschema.xml"))),
44+
dynamicContext,
45+
ISequence.empty());
46+
47+
ISequence<?> result2 = function.execute(
48+
CollectionUtil
49+
.singletonList(ISequence.of(IStringItem.valueOf("src/test/resources/metaschema/simple/metaschema.xml"))),
50+
dynamicContext,
51+
ISequence.empty());
52+
assertSame(result1, result2);
53+
}
54+
}

0 commit comments

Comments
 (0)