2
This file is part of the Nepomuk KDE project.
3
Copyright (C) 2011 Sebastian Trueg <trueg@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) version 3, or any
9
later version accepted by the membership of KDE e.V. (or its
10
successor approved by the membership of KDE e.V.), which shall
11
act as a proxy defined in Section 6 of version 3 of the license.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library. If not, see <http://www.gnu.org/licenses/>.
22
#include "removablemediamodeltest.h"
24
#define private public
26
#include "../removablemediamodel.h"
32
#include "qtest_kde.h"
33
#include "qtest_dms.h"
36
#include <Soprano/Soprano>
39
#include <Solid/DeviceNotifier>
40
#include <Solid/DeviceInterface>
41
#include <Solid/Block>
42
#include <Solid/Device>
43
#include <Solid/StorageDrive>
44
#include <Solid/StorageVolume>
45
#include <Solid/StorageAccess>
46
#include <Solid/Predicate>
48
#include <Nepomuk/Vocabulary/NIE>
50
#ifndef FAKE_COMPUTER_XML
51
#error "FAKE_COMPUTER_XML not set. An XML file describing a computer is required for this test"
54
#define SOLID_FAKEHW_SERVICE QDBusConnection::sessionBus().baseService()
55
#define SOLID_FAKEHW_PATH "/org/kde/solid/fakehw"
56
#define SOLID_FAKEHW_INTERFACE "local.qttest.Solid.Backends.Fake.FakeManager"
58
// TODO: also test mounting a different device to the same mount path
60
using namespace Nepomuk;
61
using namespace Nepomuk::Vocabulary;
62
using namespace Soprano;
64
Q_DECLARE_METATYPE(Soprano::Node)
65
Q_DECLARE_METATYPE(Soprano::Statement)
69
/// Plug a device in the fake Solid hw manager.
70
void plugDevice(const QString& udi) {
71
QDBusInterface(SOLID_FAKEHW_SERVICE, SOLID_FAKEHW_PATH, SOLID_FAKEHW_INTERFACE, QDBusConnection::sessionBus())
72
.call(QLatin1String("plug"), udi);
75
/// Unplug a device in the fake Solid hw manager.
76
void unplugDevice(const QString& udi) {
77
QDBusInterface(SOLID_FAKEHW_SERVICE, SOLID_FAKEHW_PATH, SOLID_FAKEHW_INTERFACE, QDBusConnection::sessionBus())
78
.call(QLatin1String("unplug"), udi);
81
const char* s_udiXyz123 = "/org/kde/solid/fakehw/volume_part1_size_993284096";
84
void RemovableMediaModelTest::initTestCase()
86
// make sure Solid uses the fake manager
87
setenv("SOLID_FAKEHW", FAKE_COMPUTER_XML, 1);
89
// we simply need some memory model for now - nothing fancy
90
m_model = Soprano::createModel();
91
m_model->setParent(this);
92
m_rmModel = new RemovableMediaModel(m_model, this);
96
void RemovableMediaModelTest::testConvertFileUrlsInStatement_data()
98
QTest::addColumn<Statement>( "original" );
99
QTest::addColumn<Statement>( "converted" );
101
const Statement randomStatement(QUrl("nepomuk:/res/xyz"), QUrl("onto:someProp"), LiteralValue("foobar"));
102
QTest::newRow("noFileUrls") << randomStatement << randomStatement;
104
const Statement randomFileSubject(QUrl("file:///tmp/test"), QUrl("onto:someProp"), LiteralValue("foobar"));
105
QTest::newRow("randomFileUrlInSubject") << randomFileSubject << randomFileSubject;
107
const Statement convertableFileSubject(QUrl("file:///media/XO-Y4/test.txt"), QUrl("onto:someProp"), LiteralValue("foobar"));
108
QTest::newRow("convertableFileUrlInSubject") << convertableFileSubject << convertableFileSubject;
110
const Statement convertableFileObjectWithoutNieUrl(QUrl("nepomuk:/res/xyz"), QUrl("onto:someProp"), QUrl("file:///media/XO-Y4/test.txt"));
111
QTest::newRow("convertableFileUrlInObjectWithoutNieUrl") << convertableFileObjectWithoutNieUrl << convertableFileObjectWithoutNieUrl;
113
const Statement convertableFileObjectWithNieUrl1_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4/test.txt"));
114
const Statement convertableFileObjectWithNieUrl1_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123/test.txt"));
115
QTest::newRow("convertableFileUrlInObjectWithNieUrl1") << convertableFileObjectWithNieUrl1_original << convertableFileObjectWithNieUrl1_converted;
117
const Statement convertableFileObjectWithNieUrl2_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4"));
118
const Statement convertableFileObjectWithNieUrl2_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123"));
119
QTest::newRow("convertableFileUrlInObjectWithNieUrl2") << convertableFileObjectWithNieUrl2_original << convertableFileObjectWithNieUrl2_converted;
121
const Statement convertableFileObjectWithNieUrl3_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/nfs/test.txt"));
122
const Statement convertableFileObjectWithNieUrl3_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("nfs://thehost/solid-path/test.txt"));
123
QTest::newRow("convertableFileUrlInObjectWithNieUrl3") << convertableFileObjectWithNieUrl3_original << convertableFileObjectWithNieUrl3_converted;
125
const Statement convertableFileObjectWithNieUrl4_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/nfs"));
126
const Statement convertableFileObjectWithNieUrl4_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("nfs://thehost/solid-path"));
127
QTest::newRow("convertableFileUrlInObjectWithNieUrl4") << convertableFileObjectWithNieUrl4_original << convertableFileObjectWithNieUrl4_converted;
129
const Statement convertableFileObjectWithNieUrl5_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4/file with spaces.txt"));
130
const Statement convertableFileObjectWithNieUrl5_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123/file with spaces.txt"));
131
QTest::newRow("convertableFileUrlInObjectWithNieUrl5") << convertableFileObjectWithNieUrl5_original << convertableFileObjectWithNieUrl5_converted;
133
const Statement convertableFileObjectWithNieUrl6_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/whatever with spaces/file with spaces.txt"));
134
const Statement convertableFileObjectWithNieUrl6_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://whatever/file with spaces.txt"));
135
QTest::newRow("convertableFileUrlInObjectWithNieUrl6") << convertableFileObjectWithNieUrl6_original << convertableFileObjectWithNieUrl6_converted;
137
const Statement convertableFileObjectWithNieUrl7_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/dvd/file with spaces.txt"));
138
const Statement convertableFileObjectWithNieUrl7_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("optical://SOLIDMAN_BEGINS/file with spaces.txt"));
139
QTest::newRow("convertableFileUrlInObjectWithNieUrl7") << convertableFileObjectWithNieUrl7_original << convertableFileObjectWithNieUrl7_converted;
143
void RemovableMediaModelTest::testConvertFileUrlsInStatement()
145
QFETCH(Statement, original);
146
QFETCH(Statement, converted);
148
QCOMPARE(m_rmModel->convertFileUrls(original), converted);
151
void RemovableMediaModelTest::testConvertFileUrlsInQuery_data()
153
QTest::addColumn<QString>( "original" );
154
QTest::addColumn<QString>( "converted" );
156
QString query = QString::fromLatin1("select ?r where { ?r ?p <file:///media/foobar/test.txt> . }");
157
QTest::newRow("queryWithNonConvertableFileUrl") << query << query;
159
QTest::newRow("queryWithConvertableFileUrl1")
160
<< QString::fromLatin1("select ?r where { ?r ?p <file:///media/XO-Y4/test.txt> . }")
161
<< QString::fromLatin1("select ?r where { ?r ?p <filex://xyz-123/test.txt> . }");
163
QTest::newRow("queryWithConvertableFileUrl1WeirdFormatting")
164
<< QString::fromLatin1("select ?r where { ?r ?p <file:///media/XO-Y4/test.txt> . }")
165
<< QString::fromLatin1("select ?r where { ?r ?p <filex://xyz-123/test.txt> . }");
167
QTest::newRow("queryWithConvertableFileUrl2")
168
<< QString::fromLatin1("select ?r where { ?r ?p <file:///media/nfs/test.txt> . }")
169
<< QString::fromLatin1("select ?r where { ?r ?p <nfs://thehost/solid-path/test.txt> . }");
171
QTest::newRow("queryWithConvertableRegex1")
172
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^file:///media/XO-Y4/test')) . }")
173
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^filex://xyz-123/test')) . }");
175
QTest::newRow("queryWithConvertableRegex1WithWeirdFormatting")
176
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . filter(reGEx( STR(?u) , 'file:///media/XO-Y4/test' ) ) . }")
177
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . filter(reGEx( STR(?u) , 'filex://xyz-123/test' ) ) . }");
179
QTest::newRow("queryWithConvertableRegex2")
180
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^file:///media/nfs/')) . }")
181
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^nfs://thehost/solid-path/')) . }");
183
QTest::newRow("queryWithConvertableRegex3")
184
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '''^file:///media/nfs/''')) . }")
185
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '''^nfs://thehost/solid-path/''')) . }");
187
// looking for anything in /media includes files from any storage mounted somewhere under /media
188
QTest::newRow("queryWithConvertableRegex4")
189
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^file:///media')) . }")
190
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . "
192
"REGEX(?u, '^file:///media') || "
193
"REGEX(?u, '^optical://solidman_begins/') || "
194
"REGEX(?u, '^filex://whatever/') || "
195
"REGEX(?u, '^nfs://thehost/solid-path/') || "
196
"REGEX(?u, '^filex://xyz-123/'))"
199
QTest::newRow("queryWithConvertableRegex4WithWeirdFormatting")
200
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . filter ( reGEX( str( ?u) , '^file:///media' ) ) . }")
201
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . "
203
"REGEX(str( ?u), '^file:///media') || "
204
"REGEX(str( ?u), '^optical://solidman_begins/') || "
205
"REGEX(str( ?u), '^filex://whatever/') || "
206
"REGEX(str( ?u), '^nfs://thehost/solid-path/') || "
207
"REGEX(str( ?u), '^filex://xyz-123/'))"
210
QTest::newRow("queryWithConvertableRegex4")
211
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^file:///media/nfs')) . }")
212
<< QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER((REGEX(?u, '^file:///media/nfs') || REGEX(?u, '^nfs://thehost/solid-path/'))) . }");
214
QTest::newRow("queryWithNotReallyAFileUrl")
215
<< QString::fromLatin1("select ?r where { ?r ?p ?u . FILTER(REGEX(?u, 'ffile:///media/nfs/')) . }")
216
<< QString::fromLatin1("select ?r where { ?r ?p ?u . FILTER(REGEX(?u, 'ffile:///media/nfs/')) . }");
219
void RemovableMediaModelTest::testConvertFileUrlsInQuery()
221
QFETCH(QString, original);
222
QFETCH(QString, converted);
224
QCOMPARE(m_rmModel->convertFileUrls(original), converted);
227
void RemovableMediaModelTest::testConvertFilxUrl_data()
229
QTest::addColumn<Node>( "original" );
230
QTest::addColumn<Node>( "converted" );
232
const Node nothingToConvertFilex(QUrl("filex://abc-789/hello/world"));
233
QTest::newRow("nothingToConvertFilex") << nothingToConvertFilex << nothingToConvertFilex;
235
const Node convertFilex1(QUrl("filex://xyz-123/hello/world"));
236
QTest::newRow("convertFilex1") << convertFilex1 << Node(QUrl("file:///media/XO-Y4/hello/world"));
238
const Node convertFilex2(QUrl("filex://xyz-123"));
239
QTest::newRow("convertFilex2") << convertFilex2 << Node(QUrl("file:///media/XO-Y4"));
241
const Node convertFilex3(QUrl("filex://xyz-123/hello world"));
242
QTest::newRow("convertFilex3") << convertFilex3 << Node(QUrl("file:///media/XO-Y4/hello world"));
244
const Node convertFilex4(QUrl("filex://whatever/hello world"));
245
QTest::newRow("convertFilex4") << convertFilex4 << Node(QUrl("file:///media/whatever with spaces/hello world"));
247
const Node convertnfs(QUrl("nfs://thehost/solid-path"));
248
QTest::newRow("convertnfs") << convertnfs << Node(QUrl("file:///media/nfs"));
250
const Node convertOptical1(QUrl("optical://SOLIDMAN_BEGINS"));
251
QTest::newRow("convertOptical1") << convertOptical1 << Node(QUrl("file:///media/dvd"));
253
const Node convertOptical2(QUrl("optical://SOLIDMAN_BEGINS/file with spaces.txt"));
254
QTest::newRow("convertOptical2") << convertOptical2 << Node(QUrl("file:///media/dvd/file with spaces.txt"));
257
void RemovableMediaModelTest::testConvertFilxUrl()
259
QFETCH(Node, original);
260
QFETCH(Node, converted);
262
QCOMPARE(m_rmModel->convertFilexUrl(original), converted);
265
void RemovableMediaModelTest::testConvertFilxUrls_data()
267
QTest::addColumn<Statement>( "original" );
268
QTest::addColumn<Statement>( "converted" );
270
const Statement randomStatement(QUrl("nepomuk:/res/xyz"), QUrl("onto:someProp"), LiteralValue("foobar"));
271
QTest::newRow("noFileUrls") << randomStatement << randomStatement;
273
const Statement randomFilexSubject(QUrl("filex://123-123/tmp/test"), QUrl("onto:someProp"), LiteralValue("foobar"));
274
QTest::newRow("randomFilexUrlInSubject") << randomFilexSubject << randomFilexSubject;
276
const Statement convertableFilexSubject(QUrl("filex://xyz-123/test.txt"), QUrl("onto:someProp"), LiteralValue("foobar"));
277
QTest::newRow("convertableFilexUrlInSubject") << convertableFilexSubject << convertableFilexSubject;
279
const Statement convertableFilexObjectWithoutNieUrl(QUrl("nepomuk:/res/xyz"), QUrl("onto:someProp"), QUrl("filex://xyz-123/test.txt"));
280
QTest::newRow("convertableFilexUrlInObjectWithoutNieUrl") << convertableFilexObjectWithoutNieUrl << convertableFilexObjectWithoutNieUrl;
282
const Statement convertableFilexObjectWithNieUrl1_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123/test.txt"));
283
const Statement convertableFilexObjectWithNieUrl1_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4/test.txt"));
284
QTest::newRow("convertableFilexUrlInObjectWithNieUrl1") << convertableFilexObjectWithNieUrl1_original << convertableFilexObjectWithNieUrl1_converted;
286
const Statement convertableFilexObjectWithNieUrl2_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123"));
287
const Statement convertableFilexObjectWithNieUrl2_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4"));
288
QTest::newRow("convertableFilexUrlInObjectWithNieUrl2") << convertableFilexObjectWithNieUrl2_original << convertableFilexObjectWithNieUrl2_converted;
290
const Statement convertableFilexObjectWithNieUrl3_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("nfs://thehost/solid-path/test.txt"));
291
const Statement convertableFilexObjectWithNieUrl3_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/nfs/test.txt"));
292
QTest::newRow("convertableFileUrlInObjectWithNieUrl3") << convertableFilexObjectWithNieUrl3_original << convertableFilexObjectWithNieUrl3_converted;
294
const Statement convertableFilexObjectWithNieUrl4_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("nfs://thehost/solid-path"));
295
const Statement convertableFilexObjectWithNieUrl4_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/nfs"));
296
QTest::newRow("convertableFileUrlInObjectWithNieUrl4") << convertableFilexObjectWithNieUrl4_original << convertableFilexObjectWithNieUrl4_converted;
298
const Statement convertableFilexObjectWithNieUrl5_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://xyz-123/file with spaces.txt"));
299
const Statement convertableFilexObjectWithNieUrl5_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/XO-Y4/file with spaces.txt"));
300
QTest::newRow("convertableFileUrlInObjectWithNieUrl5") << convertableFilexObjectWithNieUrl5_original << convertableFilexObjectWithNieUrl5_converted;
302
const Statement convertableFileObjectWithNieUrl6_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("filex://whatever/file with spaces.txt"));
303
const Statement convertableFileObjectWithNieUrl6_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/whatever with spaces/file with spaces.txt"));
304
QTest::newRow("convertableFileUrlInObjectWithNieUrl6") << convertableFileObjectWithNieUrl6_original << convertableFileObjectWithNieUrl6_converted;
306
const Statement convertableFileObjectWithNieUrl7_original(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("optical://SOLIDMAN_BEGINS/file with spaces.txt"));
307
const Statement convertableFileObjectWithNieUrl7_converted(QUrl("nepomuk:/res/xyz"), NIE::url(), QUrl("file:///media/dvd/file with spaces.txt"));
308
QTest::newRow("convertableFileUrlInObjectWithNieUrl7") << convertableFileObjectWithNieUrl7_original << convertableFileObjectWithNieUrl7_converted;
311
void RemovableMediaModelTest::testConvertFilxUrls()
313
QFETCH(Statement, original);
314
QFETCH(Statement, converted);
316
QCOMPARE(m_rmModel->convertFilexUrls(original), converted);
319
void RemovableMediaModelTest::testConversionWithUnmount()
321
KUrl fileXUrl("filex://xyz-123/hello/world");
322
KUrl fileUrl("file:///media/XO-Y4/hello/world");
325
plugDevice(QLatin1String(s_udiXyz123));
327
// conversion should work
328
QCOMPARE(m_rmModel->convertFileUrl(fileUrl), Soprano::Node(fileXUrl));
329
QCOMPARE(m_rmModel->convertFilexUrl(fileXUrl), Soprano::Node(fileUrl));
332
// add some data to query
333
m_rmModel->addStatement(QUrl("nepomuk:/res/foobar"), NIE::url(), fileUrl);
335
// make sure it is converted when queried
336
QCOMPARE(m_rmModel->listStatements(QUrl("nepomuk:/res/foobar"), NIE::url(), Soprano::Node()).allElements().count(), 1);
337
QCOMPARE(m_rmModel->listStatements(QUrl("nepomuk:/res/foobar"), NIE::url(), Soprano::Node()).allElements().first().object(),
338
Soprano::Node(fileUrl));
339
QCOMPARE(m_rmModel->executeQuery(QString::fromLatin1("select ?u where { <nepomuk:/res/foobar> ?p ?u . }"),
340
Soprano::Query::QueryLanguageSparql).iterateBindings(0).allElements().count(), 1);
341
QCOMPARE(m_rmModel->executeQuery(QString::fromLatin1("select ?u where { <nepomuk:/res/foobar> ?p ?u . }"),
342
Soprano::Query::QueryLanguageSparql).iterateBindings(0).allElements().first(),
343
Soprano::Node(fileUrl));
347
unplugDevice(s_udiXyz123);
349
// now conversion should do noting
350
QCOMPARE(m_rmModel->convertFileUrl(fileUrl), Soprano::Node(fileUrl));
351
QCOMPARE(m_rmModel->convertFilexUrl(fileXUrl), Soprano::Node(fileXUrl));
353
// make sure nothing is converted anymore
354
QCOMPARE(m_rmModel->listStatements(QUrl("nepomuk:/res/foobar"), NIE::url(), Soprano::Node()).allElements().count(), 1);
355
QCOMPARE(m_rmModel->listStatements(QUrl("nepomuk:/res/foobar"), NIE::url(), Soprano::Node()).allElements().first().object(),
356
Soprano::Node(fileXUrl));
357
QCOMPARE(m_rmModel->executeQuery(QString::fromLatin1("select ?u where { <nepomuk:/res/foobar> ?p ?u . }"),
358
Soprano::Query::QueryLanguageSparql).iterateBindings(0).allElements().count(), 1);
359
QCOMPARE(m_rmModel->executeQuery(QString::fromLatin1("select ?u where { <nepomuk:/res/foobar> ?p ?u . }"),
360
Soprano::Query::QueryLanguageSparql).iterateBindings(0).allElements().first(),
361
Soprano::Node(fileXUrl));
364
// re-plug device for other tests
365
plugDevice(QLatin1String(s_udiXyz123));
368
QTEST_KDEMAIN_CORE(RemovableMediaModelTest)
370
#include "removablemediamodeltest.moc"