diff options
| author | Xisco Fauli <xiscofauli@libreoffice.org> | 2024-09-16 17:32:43 +0200 |
|---|---|---|
| committer | Xisco Fauli <xiscofauli@libreoffice.org> | 2024-09-17 17:15:25 +0200 |
| commit | b9a70d822ce54fc6976542a48ff938a1adc6661d (patch) | |
| tree | 25ffe1a80c54359120caab0b04e11931e9796eb0 /unoxml | |
| parent | c600065569bba3c2bbbaceba0b7998f8ad9ec8f9 (diff) | |
unoxml: move checkSPARQL from Java to CppUnittest
Change-Id: Id8adaec00b9920966c91471fdd32720337a8c414
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/173462
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Diffstat (limited to 'unoxml')
| -rw-r--r-- | unoxml/qa/complex/unoxml/RDFRepositoryTest.java | 207 | ||||
| -rw-r--r-- | unoxml/qa/unit/data/example.rdf | 44 | ||||
| -rw-r--r-- | unoxml/qa/unit/rdftest.cxx | 139 |
3 files changed, 183 insertions, 207 deletions
diff --git a/unoxml/qa/complex/unoxml/RDFRepositoryTest.java b/unoxml/qa/complex/unoxml/RDFRepositoryTest.java index 987e8dc5f5f7..d6b4ec2c06ec 100644 --- a/unoxml/qa/complex/unoxml/RDFRepositoryTest.java +++ b/unoxml/qa/complex/unoxml/RDFRepositoryTest.java @@ -63,8 +63,6 @@ public class RDFRepositoryTest XURI baz; XURI uint; XURI rdfslabel; - XURI manifest; - XURI uuid; XURI base; XBlankNode blank; XLiteral lit; @@ -113,11 +111,6 @@ public class RDFRepositoryTest rdfslabel = URI.create(xContext, rdfs + "label"); assertNotNull("rdfslabel", rdfslabel); - manifest = URI.create(xContext, "manifest:manifest"); //FIXME - assertNotNull("manifest", manifest); - uuid = URI.create(xContext, - "urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"); - assertNotNull("uuid", uuid); base = URI.create(xContext, "base-uri:"); //FIXME assertNotNull("base", base); } catch (Exception e) { @@ -344,98 +337,6 @@ public class RDFRepositoryTest } } - @Test - public void checkSPARQL() - { - try { - - System.out.println("Checking SPARQL queries..."); - - XInputStream xIn = new StreamSimulator(TestDocument.getUrl("example.rdf"), true, param); - xRep.importGraph(FileFormat.RDF_XML, xIn, manifest, base); - - String query; - query = "SELECT ?p WHERE { ?p rdf:type pkg:Package . }"; - XQuerySelectResult result = xRep.querySelect(mkNss() + query); - assertTrue("query: package-id\n" + query, - eq(result, new String[] { "p" }, - new XNode[][] { { uuid } })); - - query = "SELECT ?part ?path FROM <" + manifest + - "> WHERE { ?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part ." - + " ?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; - result = xRep.querySelect(mkNss() + query); - assertTrue("query: contentfile", - eq(result, new String[] { "part", "path" }, - new XNode[][] { { BlankNode.create(xContext, "whatever"), - Literal.create(xContext, "content.xml") } })); - - query = "SELECT ?pkg ?path FROM <" + toS(manifest) + "> WHERE { " - + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " - + "?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; - result = xRep.querySelect(mkNss() + query); - assertTrue("query: contentfile\n" + query, - eq(result, new String[] { "pkg", "path" }, - new XNode[][] { { uuid , - Literal.create(xContext, "content.xml") } })); - - query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " - + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " - + "?part pkg:path ?path . ?part rdf:type odf:StylesFile. }"; - result = xRep.querySelect(mkNss() + query); - assertTrue("query: stylesfile\n" + query, - eq(result, new String[] { "part", "path" }, - new XNode[][] { })); - - query = "SELECT ?part ?path FROM <" + toS(manifest) + "> WHERE { " - + "?pkg rdf:type pkg:Package . ?pkg pkg:hasPart ?part . " - + "?part pkg:path ?path . ?part rdf:type odf:MetadataFile. }"; - result = xRep.querySelect(mkNss() + query); - assertTrue("query: metadatafile\n" + query, - eq(result, new String[] { "part", "path" }, - new XNode[][] { { - URI.create(xContext, "http://hospital-employee/doctor"), - Literal.create(xContext, - "meta/hospital/doctor.rdf") } })); - -//FIXME redland BUG - String uri = "uri:example-element-2"; - query = "SELECT ?path ?idref FROM <" + toS(manifest) + "> WHERE { " - + "<" + toS(uuid) + "> pkg:hasPart ?part . " - + "?part pkg:path ?path ; " - + " rdf:type ?type ; " - + " pkg:hasPart <" + uri + "> . " - + "<" + uri + "> " - + " pkg:idref ?idref . " - + " FILTER (?type = odf:ContentFile || ?type = odf:StylesFile)" - + " }"; - result = xRep.querySelect(mkNss() + query); - assertTrue("query: example-element-2\n" + query, - eq(result, new String[] { "path", "idref" }, - new XNode[][] { { - Literal.create(xContext, "content.xml"), - Literal.create(xContext, "ID_B") } })); - - // CONSTRUCT result triples have no graph! - Statement x_PkgFooLit = new Statement(uuid, foo, lit, null); - query = "CONSTRUCT { ?pkg <" + toS(foo) + "> \"" + - lit.getStringValue() + "\" } FROM <" + toS(manifest) + - "> WHERE { ?pkg rdf:type pkg:Package . } "; - XEnumeration xResultEnum = xRep.queryConstruct(mkNss() + query); - assertTrue("query: construct\n" + query, - eq(xResultEnum, new Statement[] { x_PkgFooLit })); - - query = "ASK { ?pkg rdf:type pkg:Package . }"; - boolean bResult = xRep.queryAsk(mkNss() + query); - assertTrue("query: ask\n" + query, bResult); - - System.out.println("...done"); - - } catch (Exception e) { - report(e); - } - } - // utilities ------------------------------------------------------------- public void report(Exception e) { @@ -468,32 +369,6 @@ public class RDFRepositoryTest return c.toArray(new Statement[c.size()]); } - static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception - { - java.util.Collection<XNode[]> c = new java.util.ArrayList<XNode[]>(); - while (i_Enum.hasMoreElements()) { - XNode[] s = (XNode[]) i_Enum.nextElement(); - c.add(s); - } - return c.toArray(new XNode[c.size()][]); - } - - private static class BindingComp implements java.util.Comparator<XNode[]> - { - public int compare(XNode[] left, XNode[] right) - { - if (left.length != right.length) - { - throw new RuntimeException(); - } - for (int i = 0; i < left.length; ++i) { - int eq = left[i].getStringValue().compareTo(right[i].getStringValue()); - if (eq != 0) return eq; - } - return 0; - } - } - private static class StmtComp implements java.util.Comparator<Statement> { public int compare(Statement left, Statement right) @@ -599,88 +474,6 @@ public class RDFRepositoryTest } } - static boolean eq(XQuerySelectResult i_Result, - String[] i_Vars, XNode[][] i_Bindings) throws Exception - { - String[] vars = i_Result.getBindingNames(); - XEnumeration iter = i_Result; - XNode[][] bindings = toSeqs(iter); - if (bindings.length != i_Bindings.length) { - System.out.println("binding lengths differ: " + i_Bindings.length + - " vs " + bindings.length ); - return false; - } - if (vars.length != i_Vars.length) { - // ignore for empty result: it is unclear to me whether SPARQL - // spec requires returning the variables in this case, - // and evidently newer rasqal versions don't - if (0 != i_Bindings.length || 0 != vars.length) - { - System.out.println("var lengths differ: expected " - + i_Vars.length + " but got " + vars.length); - return false; - } - } else { - for (int i = 0; i < i_Vars.length; ++i) { - if (!vars[i].equals(i_Vars[i])) { - System.out.println("variable names differ: " + - vars[i] + " != " + i_Vars[i]); - return false; - } - } - } - java.util.Arrays.sort(bindings, new BindingComp()); - java.util.Arrays.sort(i_Bindings, new BindingComp()); - for (int i = 0; i < i_Bindings.length; ++i) { - if (i_Bindings[i].length != i_Vars.length) { - System.out.println("TEST ERROR!"); - throw new Exception(); - } - if (bindings[i].length != i_Vars.length) { - System.out.println("binding length and var length differ"); - return false; - } - for (int j = 0; j < i_Vars.length; ++j) { - if (!eq(bindings[i][j], i_Bindings[i][j])) { - System.out.println("bindings differ: " + - toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j])); - return false; - } - } - } - return true; - } - - static String mkNamespace(String i_prefix, String i_namespace) - { - return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n"; - } - - static String mkNss() - { - String namespaces = mkNamespace("rdf", - "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); - namespaces += mkNamespace("pkg", - "http://docs.oasis-open.org/opendocument/meta/package/common#"); - namespaces += mkNamespace("odf", - "http://docs.oasis-open.org/opendocument/meta/package/odf#"); - return namespaces; - } - - // useful when debugging - static void dumpRepo(XDocumentRepository xRep) throws Exception - { - XEnumeration xEnum = xRep.getStatements(null, null, null); - while (xEnum.hasMoreElements()) - { - Statement s = (Statement) xEnum.nextElement(); - System.out.println("STATEMENT IN: " + toS(s.Graph) - + "\n S: " + toS(s.Subject) - + "\n P: " + toS(s.Predicate) - + "\n O: " + toS(s.Object)); - } - } - private XMultiServiceFactory getMSF() { return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); diff --git a/unoxml/qa/unit/data/example.rdf b/unoxml/qa/unit/data/example.rdf new file mode 100644 index 000000000000..d20a75ba2b10 --- /dev/null +++ b/unoxml/qa/unit/data/example.rdf @@ -0,0 +1,44 @@ +<?xml version="1.0"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<rdf:RDF + xmlns:pkg="http://docs.oasis-open.org/opendocument/meta/package/common#" + xmlns:odf="http://docs.oasis-open.org/opendocument/meta/package/odf#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> +<pkg:Package rdf:about="urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"> + <pkg:hasPart> + <odf:ContentFile pkg:path="content.xml"> + <pkg:hasPart> + <odf:Element rdf:about="uri:example-element-1" + pkg:idref="ID_A"/> + </pkg:hasPart> + <pkg:hasPart> + <odf:Element rdf:about="uri:example-element-2" + pkg:idref="ID_B"/> + </pkg:hasPart> + </odf:ContentFile> + </pkg:hasPart> + <pkg:hasPart> + <odf:MetadataFile rdf:about="http://hospital-employee/doctor" + pkg:path="meta/hospital/doctor.rdf"> + <rdf:type rdf:resource="http://medical-employee/data"/> + <rdf:type rdf:resource="http://www.w3.org/2006/vcard/ns#"/> + </odf:MetadataFile> + </pkg:hasPart> +</pkg:Package> +</rdf:RDF> diff --git a/unoxml/qa/unit/rdftest.cxx b/unoxml/qa/unit/rdftest.cxx index 7e3b93e60875..b20b914f28d5 100644 --- a/unoxml/qa/unit/rdftest.cxx +++ b/unoxml/qa/unit/rdftest.cxx @@ -190,6 +190,145 @@ CPPUNIT_TEST_FIXTURE(RDFStreamTest, testRDFa) CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), xResult.First.size()); CPPUNIT_ASSERT(!xResult.Second); } + +CPPUNIT_TEST_FIXTURE(RDFStreamTest, testSPARQL) +{ + const uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext(), + css::uno::UNO_SET_THROW); + const uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess( + xContext->getServiceManager()->createInstanceWithContext( + u"com.sun.star.ucb.SimpleFileAccess"_ustr, xContext), + uno::UNO_QUERY_THROW); + const uno::Reference<io::XInputStream> xInputStream( + xFileAccess->openFileRead(m_directories.getURLFromSrc(u"/unoxml/qa/unit/data/example.rdf")), + uno::UNO_SET_THROW); + uno::Reference<rdf::XRepository> xRepo = rdf::Repository::create(xContext); + uno::Reference<rdf::XDocumentRepository> xDocRepo(xRepo, uno::UNO_QUERY); + CPPUNIT_ASSERT(xDocRepo); + + uno::Reference<css::rdf::XURI> xManifest = rdf::URI::create(xContext, "manifest:manifest"); + uno::Reference<css::rdf::XURI> xBase = rdf::URI::create(xContext, "base-uri:"); + uno::Reference<css::rdf::XURI> xFoo = rdf::URI::create(xContext, "uri:foo"); + uno::Reference<css::rdf::XURI> xBar = rdf::URI::create(xContext, "uri:bar"); + + xDocRepo->importGraph(rdf::FileFormat::RDF_XML, xInputStream, xManifest, xBase); + + OUString sNss("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX pkg: <http://docs.oasis-open.org/opendocument/meta/package/common#>\n" + "PREFIX odf: <http://docs.oasis-open.org/opendocument/meta/package/odf#>\n"); + + // 1. query: package-id + OUString sQuery("SELECT ?p WHERE { ?p rdf:type pkg:Package . }"); + uno::Reference<rdf::XQuerySelectResult> aResult = xDocRepo->querySelect(sNss + sQuery); + uno::Sequence<OUString> aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("p"), aBindings[0]); + + uno::Sequence<uno::Reference<rdf::XNode>> aNode; + css::uno::fromAny(aResult->nextElement(), &aNode); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aNode.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"), + aNode[0]->getStringValue()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + // 2. query: contentfile + sQuery = "SELECT ?part ?path FROM <manifest:manifest> WHERE { ?pkg rdf:type pkg:Package . ?pkg " + "pkg:hasPart ?part . ?part " + "pkg:path ?path . ?part rdf:type odf:ContentFile. }"; + aResult = xDocRepo->querySelect(sNss + sQuery); + aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("part"), aBindings[0]); + CPPUNIT_ASSERT_EQUAL(OUString("path"), aBindings[1]); + + css::uno::fromAny(aResult->nextElement(), &aNode); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aNode.getLength()); + CPPUNIT_ASSERT(!aNode[0]->getStringValue().isEmpty()); + CPPUNIT_ASSERT_EQUAL(OUString("content.xml"), aNode[1]->getStringValue()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + // 3. query: contentfile + sQuery = "SELECT ?pkg ?path FROM <manifest:manifest> WHERE { ?pkg rdf:type pkg:Package . ?pkg " + "pkg:hasPart ?part . ?part pkg:path ?path . ?part rdf:type odf:ContentFile. }"; + aResult = xDocRepo->querySelect(sNss + sQuery); + aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("pkg"), aBindings[0]); + CPPUNIT_ASSERT_EQUAL(OUString("path"), aBindings[1]); + + css::uno::fromAny(aResult->nextElement(), &aNode); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aNode.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"), + aNode[0]->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("content.xml"), aNode[1]->getStringValue()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + // 4. query: stylesfile + sQuery = "SELECT ?part ?path FROM <manifest:manifest> WHERE { ?pkg rdf:type pkg:Package . ?pkg " + "pkg:hasPart ?part . ?part pkg:path ?path . ?part rdf:type odf:StylesFile. }"; + aResult = xDocRepo->querySelect(sNss + sQuery); + aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aBindings.getLength()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + // 5. query: metadatafile + sQuery = "SELECT ?part ?path FROM <manifest:manifest> WHERE { ?pkg rdf:type pkg:Package . ?pkg " + "pkg:hasPart ?part . ?part pkg:path ?path . ?part rdf:type odf:MetadataFile. }"; + aResult = xDocRepo->querySelect(sNss + sQuery); + aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("part"), aBindings[0]); + CPPUNIT_ASSERT_EQUAL(OUString("path"), aBindings[1]); + + css::uno::fromAny(aResult->nextElement(), &aNode); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aNode.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("http://hospital-employee/doctor"), aNode[0]->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("meta/hospital/doctor.rdf"), aNode[1]->getStringValue()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + //FIXME redland BUG + // 6. query: metadatafile + sQuery = "SELECT ?path ?idref FROM <manifest:manifest> WHERE { " + "<urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3> pkg:hasPart ?part . ?part pkg:path " + "?path ; rdf:type ?type ; pkg:hasPart <uri:example-element-2> . " + "<uri:example-element-2> pkg:idref ?idref . FILTER (?type = odf:ContentFile || " + "?type = odf:StylesFile) }"; + aResult = xDocRepo->querySelect(sNss + sQuery); + aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("path"), aBindings[0]); + CPPUNIT_ASSERT_EQUAL(OUString("idref"), aBindings[1]); + + css::uno::fromAny(aResult->nextElement(), &aNode); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aNode.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("content.xml"), aNode[0]->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("ID_B"), aNode[1]->getStringValue()); + + CPPUNIT_ASSERT(!aResult->hasMoreElements()); + + // 7. query: construct + sQuery = "CONSTRUCT { ?pkg <uri:foo> \"I am the literal\" } FROM <manifest:manifest> WHERE { " + "?pkg rdf:type pkg:Package . } "; + uno::Reference<container::XEnumeration> aResultEnum = xDocRepo->queryConstruct(sNss + sQuery); + + rdf::Statement aStatement = aResultEnum->nextElement().get<rdf::Statement>(); + + CPPUNIT_ASSERT_EQUAL(OUString("urn:uuid:224ab023-77b8-4396-a75a-8cecd85b81e3"), + aStatement.Subject->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("uri:foo"), aStatement.Predicate->getStringValue()); + CPPUNIT_ASSERT_EQUAL(OUString("I am the literal"), aStatement.Object->getStringValue()); + + CPPUNIT_ASSERT(!aResultEnum->hasMoreElements()); + + // 8. query: ask + sQuery = "ASK { ?pkg rdf:type pkg:Package . }"; + CPPUNIT_ASSERT(xDocRepo->queryAsk(sNss + sQuery)); +} } CPPUNIT_PLUGIN_IMPLEMENT(); |
