Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / python / check_xnamedgraph.py
blob84c774bddfa3a3fd0c88ef1e569ac32cf25d7dc1
1 #! /usr/bin/env python
2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import unittest
11 from org.libreoffice.unotest import UnoInProcess
14 class XNamedGraph(unittest.TestCase):
16 @classmethod
17 def setUpClass(cls):
18 cls._uno = UnoInProcess()
19 cls._uno.setUp()
21 @classmethod
22 def tearDownClass(cls):
23 cls._uno.tearDown()
25 def test_getStatements(self):
26 xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
27 xRepo = xDMA.getRDFRepository()
28 xGraphs = xRepo.getGraphNames()
30 all = self.getStatementsCount(xGraphs, xDMA, None, None)
31 self.assertTrue(all > 0)
33 # check that we have some node from the second RDF graph
34 # (but not any from the list of LO standard nodes)
35 DATE_URI_STR = "http://www.wollmux.org/WollMuxMetadata#WollMuxVersion"
36 xUri = self.nameToUri(DATE_URI_STR)
38 onlyWollMux = self.getStatementsCount(xGraphs, xDMA, xUri, None)
39 self.assertTrue(onlyWollMux > 0)
40 self.assertTrue(onlyWollMux < all)
42 xDMA.close(True)
44 def test_Statements_AddRemove(self):
45 # take any first graph
46 xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
47 xGraph = self.getAnyGraph(xDMA)
49 DATE_URI_STR = "http://www.example.com/Metadata#Version"
50 OBJECT_STR = "foo"
52 # not exist => add => remove
53 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
54 self.addStatement(xDMA, xGraph, DATE_URI_STR, OBJECT_STR)
55 self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
56 self.removeStatement(xDMA, xGraph, DATE_URI_STR, None)
57 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
59 xDMA.close(True)
61 def test_Statements_RemoveByObject(self):
62 # take any first graph
63 xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
64 xGraph = self.getAnyGraph(xDMA)
66 DATE_URI_STR = "http://www.example.com/Metadata#Version"
67 OBJECT_STR = "foo"
69 # remove by object
70 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
71 self.addStatement(xDMA, xGraph, DATE_URI_STR, OBJECT_STR)
72 self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
73 self.removeStatement(xDMA, xGraph, None, OBJECT_STR)
74 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
76 xDMA.close(True)
78 def test_Statements_RemoveByObject(self):
79 # take any first graph
80 xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
81 xGraph = self.getAnyGraph(xDMA)
83 DATE_URI_STR_1 = "http://www.example.com/Metadata#Version"
84 DATE_URI_STR_2 = "http://www.example.com/Metadata#Second"
85 OBJECT_STR = "foo"
87 # remove several with one call
88 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
89 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))
90 self.addStatement(xDMA, xGraph, DATE_URI_STR_1, OBJECT_STR)
91 self.addStatement(xDMA, xGraph, DATE_URI_STR_2, OBJECT_STR)
92 self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
93 self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))
94 self.removeStatement(xDMA, xGraph, None, OBJECT_STR)
95 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
96 self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))
98 xDMA.close(True)
100 def getAnyGraph(self, xDMA):
101 xRepo = xDMA.getRDFRepository()
102 xGraphs = xRepo.getGraphNames()
103 self.assertTrue(len(xGraphs) > 0)
104 xGraph = xRepo.getGraph(xGraphs[0])
105 self.assertIsNotNone(xGraph)
106 return xGraph
108 def getStatementsCount(self, xGraphs, xDMA, xPredicate, xObject):
109 count = 0
110 xRepo = xDMA.getRDFRepository()
112 for xGraphUri in xGraphs:
113 xGraph = xRepo.getGraph(xGraphUri)
115 xStatements = xGraph.getStatements(xDMA, xPredicate, xObject)
116 self.assertIsNotNone(xStatements)
118 if xPredicate is None:
119 self.assertTrue(xStatements.hasMoreElements())
121 for xStatement in xStatements:
122 count += 1
124 return count
126 def hasStatement(self, xDMA, xGraph, xPredicateName):
127 xPredicate = self.nameToUri(xPredicateName)
128 self.assertIsNotNone(xPredicate)
130 xStatements = xGraph.getStatements(xDMA, xPredicate, None)
131 self.assertIsNotNone(xStatements)
132 return xStatements.hasMoreElements()
134 def nameToUri(self, xPredicateName):
135 xServiceManager = self.__class__._uno.xContext.ServiceManager
136 xUri = xServiceManager.createInstance("com.sun.star.rdf.URI")
137 xUri.initialize((xPredicateName,))
138 return xUri
140 def stringToLiteral(self, xString):
141 xServiceManager = self.__class__._uno.xContext.ServiceManager
142 xLiteral = xServiceManager.createInstance("com.sun.star.rdf.Literal")
143 xLiteral.initialize((xString,))
144 return xLiteral
146 def addStatement(self, xDMA, xGraph, xPredicateName, xObjectStr):
147 xPredicate = self.nameToUri(xPredicateName)
148 self.assertIsNotNone(xPredicate)
150 xObject = self.stringToLiteral(xObjectStr)
151 self.assertIsNotNone(xObject)
153 xGraph.addStatement(xDMA, xPredicate, xObject)
155 def removeStatement(self, xDMA, xGraph, xPredicateName, xObjectStr):
156 xPredicate = None
157 if xPredicateName is not None:
158 xPredicate = self.nameToUri(xPredicateName)
160 xObject = None
161 if xObjectStr is not None:
162 xObject = self.stringToLiteral(xObjectStr)
164 xGraph.removeStatements(xDMA, xPredicate, xObject)
167 if __name__ == '__main__':
168 unittest.main()
170 # vim: set shiftwidth=4 softtabstop=4 expandtab: