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 c = new java.util.ArrayList(); - 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 - { - 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 { 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 @@ + + + + + + + + + + + + + + + + + + + + + + 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 xContext(comphelper::getProcessComponentContext(), + css::uno::UNO_SET_THROW); + const uno::Reference xFileAccess( + xContext->getServiceManager()->createInstanceWithContext( + u"com.sun.star.ucb.SimpleFileAccess"_ustr, xContext), + uno::UNO_QUERY_THROW); + const uno::Reference xInputStream( + xFileAccess->openFileRead(m_directories.getURLFromSrc(u"/unoxml/qa/unit/data/example.rdf")), + uno::UNO_SET_THROW); + uno::Reference xRepo = rdf::Repository::create(xContext); + uno::Reference xDocRepo(xRepo, uno::UNO_QUERY); + CPPUNIT_ASSERT(xDocRepo); + + uno::Reference xManifest = rdf::URI::create(xContext, "manifest:manifest"); + uno::Reference xBase = rdf::URI::create(xContext, "base-uri:"); + uno::Reference xFoo = rdf::URI::create(xContext, "uri:foo"); + uno::Reference xBar = rdf::URI::create(xContext, "uri:bar"); + + xDocRepo->importGraph(rdf::FileFormat::RDF_XML, xInputStream, xManifest, xBase); + + OUString sNss("PREFIX rdf: \n" + "PREFIX pkg: \n" + "PREFIX odf: \n"); + + // 1. query: package-id + OUString sQuery("SELECT ?p WHERE { ?p rdf:type pkg:Package . }"); + uno::Reference aResult = xDocRepo->querySelect(sNss + sQuery); + uno::Sequence aBindings = aResult->getBindingNames(); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aBindings.getLength()); + CPPUNIT_ASSERT_EQUAL(OUString("p"), aBindings[0]); + + uno::Sequence> 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 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 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 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 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 WHERE { " + " pkg:hasPart ?part . ?part pkg:path " + "?path ; rdf:type ?type ; pkg:hasPart . " + " 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 \"I am the literal\" } FROM WHERE { " + "?pkg rdf:type pkg:Package . } "; + uno::Reference aResultEnum = xDocRepo->queryConstruct(sNss + sQuery); + + rdf::Statement aStatement = aResultEnum->nextElement().get(); + + 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();