Skip to content

Commit dca4d9a

Browse files
authored
Merge pull request #2196 from adamretter/hotfix/subsequence-mixed-types
Fixes an issue with mixed types in a subsequence
2 parents 4393aea + a75e748 commit dca4d9a

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/org/exist/xquery/value/SubSequence.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void add(final Item item) throws XPathException {
8686

8787
@Override
8888
public int getItemType() {
89-
return Type.ITEM;
89+
return sequence.getItemType();
9090
}
9191

9292
@Override
@@ -361,7 +361,26 @@ public Iterator<Collection> getCollectionIterator() {
361361

362362
@Override
363363
public boolean isPersistentSet() {
364-
return false;
364+
final SequenceIterator iterator;
365+
try {
366+
iterator = iterate();
367+
} catch (final XPathException e) {
368+
throw new RuntimeException(e); // should never happen!
369+
}
370+
371+
// needed to guard against returning true for an empty-sequence below
372+
if (!iterator.hasNext()) {
373+
return false;
374+
}
375+
376+
while (iterator.hasNext()) {
377+
final NodeValue item = (NodeValue) iterator.nextItem();
378+
if (item.getImplementationType() != NodeValue.PERSISTENT_NODE) {
379+
return false;
380+
}
381+
}
382+
// else, all items were persistent
383+
return true;
365384
}
366385

367386
@Override

test/src/org/exist/xquery/functions/fn/FunSubSequenceTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,30 @@
2222

2323
import com.googlecode.junittoolbox.ParallelRunner;
2424
import org.exist.test.ExistXmldbEmbeddedServer;
25+
import org.exist.test.TestConstants;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
2528
import org.junit.ClassRule;
2629
import org.junit.Test;
2730
import org.junit.runner.RunWith;
31+
import org.xmldb.api.base.Collection;
32+
import org.xmldb.api.base.Resource;
2833
import org.xmldb.api.base.ResourceSet;
2934
import org.xmldb.api.base.XMLDBException;
35+
import org.xmldb.api.modules.CollectionManagementService;
36+
import org.xmldb.api.modules.XMLResource;
3037

3138
import static org.junit.Assert.assertEquals;
3239

3340
@RunWith(ParallelRunner.class)
3441
public class FunSubSequenceTest {
3542

3643
@ClassRule
37-
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(true, true);
44+
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true);
45+
46+
private static Collection test = null;
47+
private static final String SIMPLE_XML_FILENAME = "simple.xml";
48+
private static final String SIMPLE_XML = "<nums><i>1</i><i>2</i><i>3</i><i>4</i></nums>";
3849

3950
@Test
4051
public void all_arity2() throws XMLDBException {
@@ -168,6 +179,26 @@ public void largeRange_arity3() throws XMLDBException {
168179
assertEquals("(3000000000)", asSequenceStr(result));
169180
}
170181

182+
@Test
183+
public void persistentSupsequence_toInMemory() throws XMLDBException {
184+
final ResourceSet result = existEmbeddedServer.executeQuery("fn:subsequence(doc('" + TestConstants.TEST_COLLECTION_URI.getCollectionPath() + "/" + SIMPLE_XML_FILENAME + "')/nums/i, 2, 2)//text()");
185+
assertEquals("(2,3)", asSequenceStr(result));
186+
}
187+
188+
@BeforeClass
189+
public static void setup() throws XMLDBException {
190+
test = existEmbeddedServer.createCollection(existEmbeddedServer.getRoot(), TestConstants.TEST_COLLECTION_URI.lastSegment().toString());
191+
final Resource resource = test.createResource(SIMPLE_XML_FILENAME, XMLResource.RESOURCE_TYPE);
192+
resource.setContent(SIMPLE_XML);
193+
test.storeResource(resource);
194+
}
195+
196+
@AfterClass
197+
public static void cleanup() throws XMLDBException {
198+
final CollectionManagementService collectionManagementService = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
199+
collectionManagementService.removeCollection(test.getName());
200+
}
201+
171202
private static String asSequenceStr(final ResourceSet result) throws XMLDBException {
172203
final StringBuilder builder = new StringBuilder();
173204
builder.append('(');

0 commit comments

Comments
 (0)