• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • autotests
  • lib
testbase.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of nepomuk-testlib
3  *
4  * Copyright (C) 2010-12 Vishesh Handa <me@vhanda.in>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include "testbase.h"
23 #include "nepomukservicemanagerinterface.h"
24 #include "resourcemanager.h"
25 
26 #include <QtCore/QFile>
27 #include <QtCore/QDir>
28 
29 #include <QtDBus/QDBusConnection>
30 #include <QtTest>
31 
32 #include <KStandardDirs>
33 #include <KTempDir>
34 #include <KDebug>
35 
36 #include "resourcemanager.h"
37 
38 #include <Soprano/Model>
39 #include <Soprano/QueryResultIterator>
40 #include <Soprano/NodeIterator>
41 
42 #include <cstdlib>
43 
44 class Nepomuk2::TestBase::Private {
45 public:
46  org::kde::nepomuk::ServiceManager* m_serviceManager;
47  KTempDir m_tempDir;
48 };
49 
50 Nepomuk2::TestBase::TestBase(QObject* parent)
51  : QObject( parent ),
52  d( new Nepomuk2::TestBase::Private )
53 {
54  char* value = getenv("NEPOMUK_TESTLIB_RUNNING");
55  if( !value ) {
56  QString message("The Nepomuk Test Environment is not running. Running these tests without the "
57  "test environment will delete your Nepomuk database. Aborting");
58  kError() << message;
59  QFAIL(message.toLatin1().data());
60  }
61 
62  //
63  // QTEST_KDEMAIN_WITH_COMPONENTNAME overwrites the XDG_CONFIG_HOME env variable with a custom value
64  // and that breaks all our unit tests. Therfore we add the actual config directory to the tests
65  //
66  KGlobal::dirs()->addResourceDir("config", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/share/config") ) );
67 
68  d->m_serviceManager = new org::kde::nepomuk::ServiceManager( "org.kde.NepomukServer", "/servicemanager", QDBusConnection::sessionBus() );
69 
70  // Wait for Nepomuk to get initialized
71  ResourceManager* rm = ResourceManager::instance();
72  if( !rm->initialized() ) {
73  QEventLoop loop;
74  connect( rm, SIGNAL(nepomukSystemStarted()), &loop, SLOT(quit()) );
75  kDebug() << "Waiting for Nepomuk to start";
76  loop.exec();
77  }
78 
79  // Stop all the other servies
80  QSet<QString> services = runningServices().toSet();
81  kDebug() << "Running Services : " << services;
82  services.remove( "nepomukstorage" );
83 
84  Q_FOREACH( const QString & service, services )
85  stopService( service );
86 }
87 
88 Nepomuk2::TestBase::~TestBase()
89 {
90  delete d;
91 }
92 
93 void Nepomuk2::TestBase::cleanup()
94 {
95  resetRepository();
96 }
97 
98 void Nepomuk2::TestBase::cleanupTest()
99 {
100  resetRepository();
101 }
102 
103 
104 void Nepomuk2::TestBase::resetRepository()
105 {
106  //kDebug() << "Reseting the repository";
107  QTime timer;
108  timer.start();
109 
110  QString query = QString::fromLatin1("select distinct ?r where { ?r ?p ?o. "
111  "FILTER(regex(str(?r), '^nepomuk')) . }");
112  Soprano::Model * model = Nepomuk2::ResourceManager::instance()->mainModel();
113 
114  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
115  while( it.next() ) {
116  model->removeAllStatements( it[0], Soprano::Node(), Soprano::Node() );
117  model->removeAllStatements( Soprano::Node(), Soprano::Node(), it[0] );
118  }
119 
120  // We destroy the Nepomuk graph above, so we clear the cache in order to reset it
121  // Plus, certain graphs might be in the cache and are now invalid
122  QDBusMessage msg = QDBusMessage::createMethodCall( QLatin1String("org.kde.NepomukStorage"),
123  QLatin1String("/datamanagement"),
124  QLatin1String("org.kde.nepomuk.DataManagement"),
125  QLatin1String("clearCache") );
126  QDBusConnection::sessionBus().call( msg );
127 
128  //kDebug() << "Time Taken: " << timer.elapsed()/1000.0 << " seconds";
129 }
130 
131 
132 void Nepomuk2::TestBase::waitForServiceInitialization(const QString& service)
133 {
134  while( !isServiceRunning( service ) ) {
135  QTest::qSleep( 100 );
136  }
137 
138  while( !isServiceInitialized( service ) ) {
139  QTest::qSleep( 200 );
140  kDebug() << runningServices();
141  }
142 }
143 
144 
145 void Nepomuk2::TestBase::startServiceAndWait(const QString& service)
146 {
147  if( isServiceRunning( service ) )
148  return;
149 
150  kDebug() << "Starting " << service << " ...";
151  startService( service );
152  kDebug() << "Waiting ...";
153  waitForServiceInitialization( service );
154 }
155 
156 
157 
158 //
159 // Service Manager
160 //
161 
162 QStringList Nepomuk2::TestBase::availableServices()
163 {
164  QDBusPendingReply< QStringList > reply = d->m_serviceManager->availableServices();
165  reply.waitForFinished();
166  return reply.value();
167 }
168 
169 bool Nepomuk2::TestBase::isServiceAutostarted(const QString& service)
170 {
171  QDBusPendingReply< bool > reply = d->m_serviceManager->isServiceAutostarted( service );
172  reply.waitForFinished();
173  return reply.value();
174 
175 }
176 
177 bool Nepomuk2::TestBase::isServiceInitialized(const QString& name)
178 {
179  QDBusPendingReply< bool > reply = d->m_serviceManager->isServiceInitialized( name );
180  reply.waitForFinished();
181  return reply.value();
182 }
183 
184 bool Nepomuk2::TestBase::isServiceRunning(const QString& name)
185 {
186  QDBusPendingReply< bool > reply = d->m_serviceManager->isServiceRunning( name );
187  reply.waitForFinished();
188  return reply.value();
189 }
190 
191 QStringList Nepomuk2::TestBase::runningServices()
192 {
193  QDBusPendingReply< QStringList > reply = d->m_serviceManager->runningServices();
194  reply.waitForFinished();
195  return reply.value();
196 }
197 
198 void Nepomuk2::TestBase::setServiceAutostarted(const QString& service, bool autostart)
199 {
200  d->m_serviceManager->setServiceAutostarted( service, autostart );
201 }
202 
203 bool Nepomuk2::TestBase::startService(const QString& name)
204 {
205  QDBusPendingReply< bool > reply = d->m_serviceManager->startService( name );
206  reply.waitForFinished();
207  return reply.value();
208 }
209 
210 bool Nepomuk2::TestBase::stopService(const QString& name)
211 {
212  QDBusPendingReply< bool > reply = d->m_serviceManager->stopService( name );
213  reply.waitForFinished();
214  return reply.value();
215 }
216 
217 #include "testbase.moc"
218 
Nepomuk2::TestBase::setServiceAutostarted
void setServiceAutostarted(const QString &service, bool autostart)
Definition: testbase.cpp:198
Nepomuk2::TestBase::stopService
bool stopService(const QString &name)
Definition: testbase.cpp:210
QObject
startService
void startService(const KService::Ptr &ptr)
Definition: tools/nepomukctl/main.cpp:197
Nepomuk2::TestBase::startServiceAndWait
void startServiceAndWait(const QString &service)
Definition: testbase.cpp:145
Nepomuk2::TestBase::waitForServiceInitialization
void waitForServiceInitialization(const QString &service)
Definition: testbase.cpp:132
Nepomuk2::TestBase::startService
bool startService(const QString &name)
Definition: testbase.cpp:203
Nepomuk2::TestBase::isServiceRunning
bool isServiceRunning(const QString &name)
Definition: testbase.cpp:184
Nepomuk2::ResourceManager::initialized
bool initialized() const
Definition: resourcemanager.cpp:306
Nepomuk2::TestBase::TestBase
TestBase(QObject *parent=0)
Definition: testbase.cpp:50
Nepomuk2::TestBase::isServiceInitialized
bool isServiceInitialized(const QString &name)
Definition: testbase.cpp:177
Nepomuk2::TestBase::availableServices
QStringList availableServices()
Definition: testbase.cpp:162
Nepomuk2::TestBase::runningServices
QStringList runningServices()
Definition: testbase.cpp:191
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::TestBase::resetRepository
void resetRepository()
Definition: testbase.cpp:104
Nepomuk2::TestBase::isServiceAutostarted
bool isServiceAutostarted(const QString &service)
Definition: testbase.cpp:169
isServiceRunning
bool isServiceRunning(const KService::Ptr &ptr)
Definition: tools/nepomukctl/main.cpp:160
Nepomuk2::TestBase
All Nepomuk unit tests are to be derived from this class.
Definition: testbase.h:46
testbase.h
Nepomuk2::ResourceManager
The ResourceManager is the central Nepomuk configuration point.
Definition: resourcemanager.h:55
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
Nepomuk2::TestBase::~TestBase
virtual ~TestBase()
Definition: testbase.cpp:88
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal