Search in sources :

Example 76 with Resource

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());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 77 with Resource

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);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 78 with Resource

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());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 79 with Resource

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 80 with Resource

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());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) Diff(org.xmlunit.diff.Diff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) Source(javax.xml.transform.Source)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9