use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XQueryTest method doubleDocNode_2078755.
@Test
public void doubleDocNode_2078755() throws XMLDBException {
Collection testCollection = getTestCollection();
Resource doc = testCollection.createResource(MODULE8_NAME, "BinaryResource");
doc.setContent(module8);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
service.setProperty(OutputKeys.INDENT, "no");
String query = "import module namespace dr = \"double-root2\" at \"" + URI + "/test/" + MODULE8_NAME + "\";\n" + "let $doc1 := dr:documentIn() \n" + "let $count1 := count($doc1/element()) \n" + "let $doc2 := dr:documentIn() \n" + "let $count2 := count($doc2/element()) \n" + "return ($count1, $count2) \n";
ResourceSet result = service.query(query);
assertEquals(2, result.getSize());
assertEquals("1", result.getResource(0).getContent().toString());
assertEquals("1", result.getResource(1).getContent().toString());
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XQueryTest method globalVars.
@Test
public void globalVars() throws XMLDBException {
Collection testCollection = getTestCollection();
Resource doc = testCollection.createResource(MODULE5_NAME, "BinaryResource");
doc.setContent(module5);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
doc = testCollection.createResource(MODULE6_NAME, "BinaryResource");
doc.setContent(module6);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
XQueryService service = (XQueryService) testCollection.getService("XPathQueryService", "1.0");
String query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE5_NAME + "\";\n" + "$foo:bar";
ResourceSet result = service.query(query);
assertEquals(result.getSize(), 1);
assertEquals(result.getResource(0).getContent(), "bar");
query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "$local:a";
result = service.query(query);
assertEquals(result.getSize(), 1);
assertEquals(result.getResource(0).getContent(), "abc");
boolean gotException = false;
try {
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE6_NAME + "\";\n" + "$foo:bar";
result = service.query(query);
} catch (XMLDBException e) {
assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
gotException = true;
}
assertTrue("Duplicate global variable should generate error", gotException);
gotException = false;
try {
query = "xquery version \"1.0\";\n" + "declare variable $local:a := 'abc';" + "declare variable $local:a := 'abc';" + "$local:a";
result = service.query(query);
} catch (XMLDBException e) {
assertTrue("Test should generate err:XQST0049, got: " + e.getMessage(), e.getMessage().indexOf("err:XQST0049") > -1);
gotException = true;
}
assertTrue("Duplicate global variable should generate error", gotException);
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XQueryTest method modulesAndNS.
@Test
public void modulesAndNS() throws XMLDBException, IOException, SAXException {
Collection testCollection = getTestCollection();
Resource doc = testCollection.createResource(MODULE7_NAME, "BinaryResource");
doc.setContent(module7);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
service.setProperty(OutputKeys.INDENT, "no");
String query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE7_NAME + "\";\n" + "<div xmlns='http://www.w3.org/1999/xhtml'>" + "{ foo:link() }" + "</div>";
ResourceSet result = service.query(query);
assertEquals(1, result.getSize());
result.getResource(0).getContent();
assertXMLEqual("<div xmlns='http://www.w3.org/1999/xhtml'><a xmlns=\"\" href='#'>Link</a></div>", result.getResource(0).getContent().toString());
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + MODULE7_NAME + "\";\n" + "<div xmlns='http://www.w3.org/1999/xhtml'>" + "{ foo:copy(<a>Link</a>) }" + "</div>";
result = service.query(query);
assertEquals(1, result.getSize());
result.getResource(0).getContent();
assertXMLEqual("<div xmlns='http://www.w3.org/1999/xhtml'><a>Link</a></div>", result.getResource(0).getContent().toString());
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XQueryTest method userEscalationForInMemoryNodes.
@Test
public void userEscalationForInMemoryNodes() throws XMLDBException {
String query = "xmldb:login(\"xmldb:exist:///db\", \"guest\", \"guest\"), sm:id()/sm:id/sm:effective/sm:username/text(), let $node := <node id=\"1\">value</node>, $null := $node[@id eq '1'] return sm:id()/sm:id/sm:effective/sm:username/text()";
XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
ResourceSet result = service.query(query);
Resource loggedIn = result.getResource(0);
Resource currentUser = result.getResource(1);
Resource currentUserAfterInMemoryOp = result.getResource(2);
// check the login as guest worked
assertEquals("Logged in as quest: " + loggedIn.getContent().toString(), "true", loggedIn.getContent().toString());
// check that we are guest
assertEquals("After Login as guest, User should be guest and is: " + currentUser.getContent().toString(), "guest", currentUser.getContent().toString());
// check that we are still guest
assertEquals("After Query, User should still be guest and is: " + currentUserAfterInMemoryOp.getContent().toString(), "guest", currentUserAfterInMemoryOp.getContent().toString());
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XQueryTest method importExternalClasspathLibraryModule.
@Test
public void importExternalClasspathLibraryModule() throws XMLDBException {
final long timestamp = System.currentTimeMillis();
final Collection testCollection = getTestCollection();
final Resource doc = testCollection.createResource("import-external-classpath.xq", "BinaryResource");
doc.setContent("import module namespace ext1 = \"http://import-external-classpath-library-module-test.com\" at \"resource:org/exist/xquery/external-classpath-library-module.xqm\";\n" + "ext1:echo(" + timestamp + ")");
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
final EXistXPathQueryService service = (EXistXPathQueryService) testCollection.getService("XPathQueryService", "1.0");
final ResourceSet resourceSet = service.executeStoredQuery("/db/test/import-external-classpath.xq");
assertEquals(1, resourceSet.getSize());
final Resource resource = resourceSet.getResource(0);
final Source expected = Input.fromString("<echo>" + timestamp + "</echo>").build();
final Source actual = Input.fromString(resource.getContent().toString()).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
Aggregations