Qt implementation of SODEP.

svn path=/branches/work/~ervin/sodep/; revision=806026
This commit is contained in:
Kevin Ottens 2008-05-09 21:11:35 +00:00
parent c0279bc3e2
commit d712c2d2c9
16 changed files with 1299 additions and 0 deletions

View File

@ -0,0 +1,9 @@
find_package(KDE4 REQUIRED)
include(KDE4Defaults)
include(MacroLibrary)
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES})
add_subdirectory(sodep)
add_subdirectory(tests)

View File

@ -0,0 +1,4 @@
/** @mainpage SODEP
*/
// DOXYGEN_SET_PROJECT_NAME = Sodep

View File

@ -0,0 +1,22 @@
project(sodep)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${QT_INCLUDE_DIR})
set(sodep_LIB_SRCS sodepvalue.cpp sodepfault.cpp sodepmessage.cpp)
kde4_add_library(sodep SHARED ${sodep_LIB_SRCS})
target_link_libraries(sodep ${QT_QTCORE_LIBRARY})
install(TARGETS sodep
DESTINATION ${LIB_INSTALL_DIR})
set_target_properties(sodep PROPERTIES VERSION 1.0.0 SOVERSION 1)
install(FILES
sodepvalue.h
sodepfault.h
sodepmessage.h
DESTINATION ${INCLUDE_INSTALL_DIR})

View File

@ -0,0 +1,108 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "sodepfault.h"
#include <QtCore/QString>
#include "sodephelpers_p.h"
class SodepFaultPrivate
{
public:
QString name;
SodepValue data;
};
SodepFault::SodepFault()
: d(new SodepFaultPrivate)
{
}
SodepFault::SodepFault(const QString &name, const SodepValue &data)
: d(new SodepFaultPrivate)
{
d->name = name;
d->data = data;
}
SodepFault::SodepFault(const SodepFault &other)
: d(new SodepFaultPrivate)
{
*d = *other.d;
}
SodepFault::~SodepFault()
{
delete d;
}
SodepFault &SodepFault::operator=(const SodepFault &other)
{
*d = *other.d;
return *this;
}
QString SodepFault::name() const
{
return d->name;
}
SodepValue SodepFault::data() const
{
return d->data;
}
bool SodepFault::isValid() const
{
return !d->name.isEmpty();
}
void SodepFault::writeTo(QIODevice &io) const
{
if (!isValid()) {
io.putChar(0);
return;
}
io.putChar(1);
sodepWrite(io, name());
d->data.writeTo(io);
}
SodepFault SodepFault::readFrom(QIODevice &io)
{
char code;
io.getChar(&code);
if (code!=1) {
return SodepFault();
}
SodepFault result;
result.d->name = sodepReadString(io);
result.d->data = SodepValue::readFrom(io);
return result;
}

View File

@ -0,0 +1,52 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SODEPFAULT_H
#define SODEPFAULT_H
#include <sodepvalue.h>
class SodepFaultPrivate;
class Q_DECL_EXPORT SodepFault
{
public:
SodepFault();
explicit SodepFault(const QString &name, const SodepValue &data = SodepValue());
SodepFault(const SodepFault &other);
~SodepFault();
SodepFault &operator=(const SodepFault &other);
QString name() const;
SodepValue data() const;
bool isValid() const;
void writeTo(QIODevice &io) const;
static SodepFault readFrom(QIODevice &io);
private:
SodepFaultPrivate * const d;
};
#endif

View File

@ -0,0 +1,95 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SODEPHELPERS_P_H
#define SODEPHELPERS_P_H
#include <QtCore/QIODevice>
#include <QtCore/QString>
inline void sodepWrite(QIODevice &io, double value)
{
const char *in = (const char*)&value;
char out[8];
for (int i=0; i<8; ++i) {
out[i] = in[7-i];
}
io.write(out, 8);
}
inline void sodepWrite(QIODevice &io, qint32 value)
{
const char *in = (const char*)&value;
char out[4];
for (int i=0; i<4; ++i) {
out[i] = in[3-i];
}
io.write(out, 4);
}
inline void sodepWrite(QIODevice &io, const QString &value)
{
QByteArray data = value.toUtf8();
sodepWrite(io, data.size());
io.write(data);
}
inline double sodepReadDouble(QIODevice &io)
{
double d;
char *out = (char*)&d;
char in[8];
io.read(in, 8);
for (int i=0; i<8; ++i) {
out[i] = in[7-i];
}
return d;
}
inline qint32 sodepReadInt(QIODevice &io)
{
qint32 i;
char *out = (char*)&i;
char in[4];
io.read(in, 4);
for (int j=0; j<4; ++j) {
out[j] = in[3-j];
}
return i;
}
inline QString sodepReadString(QIODevice &io)
{
qint32 length = sodepReadInt(io);
QByteArray data = io.read(length);
return QString::fromUtf8(data);
}
#endif

View File

@ -0,0 +1,120 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "sodepmessage.h"
#include <QtCore/QString>
#include "sodephelpers_p.h"
class SodepMessagePrivate
{
public:
QString resourcePath;
QString operationName;
SodepFault fault;
SodepValue data;
};
SodepMessage::SodepMessage()
: d(new SodepMessagePrivate)
{
}
SodepMessage::SodepMessage(const QString &resourcePath, const QString &operationName)
: d(new SodepMessagePrivate)
{
d->resourcePath = resourcePath;
d->operationName = operationName;
}
SodepMessage::SodepMessage(const SodepMessage &other)
: d(new SodepMessagePrivate)
{
*d = *other.d;
}
SodepMessage::~SodepMessage()
{
delete d;
}
SodepMessage &SodepMessage::operator=(const SodepMessage &other)
{
*d = *other.d;
return *this;
}
QString SodepMessage::resourcePath() const
{
return d->resourcePath;
}
QString SodepMessage::operationName() const
{
return d->operationName;
}
SodepFault SodepMessage::fault() const
{
return d->fault;
}
void SodepMessage::setFault(const SodepFault &fault)
{
d->fault = fault;
}
SodepValue SodepMessage::data() const
{
return d->data;
}
void SodepMessage::setData(const SodepValue &data)
{
d->data = data;
}
bool SodepMessage::isValid()
{
return !d->resourcePath.isEmpty() && !d->operationName.isEmpty();
}
void SodepMessage::writeTo(QIODevice &io) const
{
sodepWrite(io, d->resourcePath);
sodepWrite(io, d->operationName);
d->fault.writeTo(io);
d->data.writeTo(io);
}
SodepMessage SodepMessage::readFrom(QIODevice &io)
{
SodepMessage result;
result.d->resourcePath = sodepReadString(io);
result.d->operationName = sodepReadString(io);
result.d->fault = SodepFault::readFrom(io);
result.d->data = SodepValue::readFrom(io);
return result;
}

View File

@ -0,0 +1,58 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SODEPMESSAGE_H
#define SODEPMESSAGE_H
#include <sodepvalue.h>
#include <sodepfault.h>
class SodepMessagePrivate;
class Q_DECL_EXPORT SodepMessage
{
public:
SodepMessage();
explicit SodepMessage(const QString &resourcePath,
const QString &operationName);
SodepMessage(const SodepMessage &other);
~SodepMessage();
SodepMessage &operator=(const SodepMessage &other);
QString resourcePath() const;
QString operationName() const;
SodepFault fault() const;
void setFault(const SodepFault &fault);
SodepValue data() const;
void setData(const SodepValue &data);
bool isValid();
void writeTo(QIODevice &io) const;
static SodepMessage readFrom(QIODevice &io);
private:
SodepMessagePrivate * const d;
};
#endif

View File

@ -0,0 +1,206 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "sodepvalue.h"
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include "sodephelpers_p.h"
class SodepValuePrivate
{
public:
QVariant content;
QMap<QString, QList<SodepValue> > children;
};
SodepValue::SodepValue()
: d(new SodepValuePrivate)
{
}
SodepValue::SodepValue(const QString &content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(qint32 content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(double content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(const SodepValue &other)
: d(new SodepValuePrivate)
{
*d = *other.d;
}
SodepValue::~SodepValue()
{
delete d;
}
SodepValue &SodepValue::operator=(const SodepValue &other)
{
*d = *other.d;
return *this;
}
QStringList SodepValue::childrenNames() const
{
return d->children.keys();
}
QList<SodepValue> & SodepValue::children(const QString &name)
{
return d->children[name];
}
const QList<SodepValue> & SodepValue::children(const QString &name) const
{
return d->children[name];
}
QString SodepValue::toString() const
{
if (isString()) {
return d->content.toString();
} else {
return QString();
}
}
qint32 SodepValue::toInt() const
{
if (isInt()) {
return d->content.toInt();
} else {
return 0;
}
}
double SodepValue::toDouble() const
{
if (isDouble()) {
return d->content.toDouble();
} else {
return 0.0;
}
}
bool SodepValue::isString() const
{
return d->content.type()==QVariant::String;
}
bool SodepValue::isInt() const
{
return d->content.type()==QVariant::Int;
}
bool SodepValue::isDouble() const
{
return d->content.type()==QVariant::Double;
}
bool SodepValue::isValid() const
{
return isString() || isInt() || isDouble();
}
void SodepValue::writeTo(QIODevice &io) const
{
if (isDouble()) {
io.putChar(3);
sodepWrite(io, toDouble());
} else if (isInt()) {
io.putChar(2);
sodepWrite(io, toInt());
} else if (isString()) {
io.putChar(1);
sodepWrite(io, toString());
} else {
io.putChar(0);
}
sodepWrite(io, d->children.size());
foreach (const QString &name, d->children.keys()) {
sodepWrite(io, name);
QList<SodepValue> values = d->children[name];
qint32 valueCount = values.size();
sodepWrite(io, valueCount);
for (int j=0; j<valueCount; ++j) {
values[j].writeTo(io);
}
}
}
SodepValue SodepValue::readFrom(QIODevice &io)
{
SodepValue result;
char code;
io.getChar(&code);
switch(code) {
case 3: {
result = SodepValue(sodepReadDouble(io));
break;
}
case 2: {
result = SodepValue(sodepReadInt(io));
break;
}
case 1: {
result = SodepValue(sodepReadString(io));
break;
}
default:
break;
}
qint32 childrenCount = sodepReadInt(io);
for (int i=0; i<childrenCount; ++i) {
QString name = sodepReadString(io);
QList<SodepValue> values;
qint32 valueCount = sodepReadInt(io);
for (int j=0; j<valueCount; ++j) {
values << readFrom(io);
}
result.d->children[name] = values;
}
return result;
}

View File

@ -0,0 +1,65 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SODEPVALUE_H
#define SODEPVALUE_H
#include <QtCore/QIODevice>
#include <QtCore/QList>
class SodepValuePrivate;
class Q_DECL_EXPORT SodepValue
{
public:
SodepValue();
explicit SodepValue(const QString &content);
explicit SodepValue(qint32 content);
explicit SodepValue(double content);
SodepValue(const SodepValue &other);
~SodepValue();
SodepValue &operator=(const SodepValue &other);
QStringList childrenNames() const;
QList<SodepValue> &children(const QString &name);
const QList<SodepValue> &children(const QString &name) const;
QString toString() const;
qint32 toInt() const;
double toDouble() const;
bool isString() const;
bool isInt() const;
bool isDouble() const;
bool isValid() const;
void writeTo(QIODevice &io) const;
static SodepValue readFrom(QIODevice &io);
private:
SodepValuePrivate * const d;
};
#endif

View File

@ -0,0 +1,25 @@
include_directories(${KDE4_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../sodep)
MACRO(SODEP_UNIT_TESTS)
FOREACH(_testname ${ARGN})
kde4_add_unit_test(${_testname} NOGUI ${_testname}.cpp)
target_link_libraries(${_testname} ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY} sodep)
ENDFOREACH(_testname)
ENDMACRO(SODEP_UNIT_TESTS)
MACRO(SODEP_EXECUTABLE_TESTS)
FOREACH(_testname ${ARGN})
kde4_add_executable(${_testname} NOGUI TEST ${_testname}.cpp)
target_link_libraries(${_testname} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} sodep)
ENDFOREACH(_testname)
ENDMACRO(SODEP_EXECUTABLE_TESTS)
SODEP_UNIT_TESTS(
sodepvaluetest
sodepfaulttest
sodepmessagetest
)
SODEP_EXECUTABLE_TESTS(
sodepprintertest
)

View File

@ -0,0 +1,100 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QObject>
#include <QtTest/QtTest>
#include <sodepfault.h>
#include "sodeptesthelpers.h"
class SodepFaultTest : public QObject
{
Q_OBJECT
private slots:
void shouldHandleInvalids()
{
SodepFault f;
QCOMPARE(f.name(), QString());
QVERIFY(!f.data().isValid());
QVERIFY(!f.isValid());
}
void shouldVerifyInitialState()
{
SodepFault f1("blup"), f2("blop", SodepValue(42));
QCOMPARE(f1.name(), QString("blup"));
QVERIFY(!f1.data().isValid());
QCOMPARE(f2.name(), QString("blop"));
QVERIFY(f2.data().isValid());
QCOMPARE(f2.data().toInt(), 42);
f1 = f2;
QCOMPARE(f1.name(), QString("blop"));
QVERIFY(f1.data().isValid());
QCOMPARE(f1.data().toInt(), 42);
}
void shouldBeSerializable_data()
{
SodepValue v(42);
QByteArray vSerial = QByteArray::fromHex("020000002A00000000");
QTest::addColumn<SodepFault>("original");
QTest::addColumn<QByteArray>("serialized");
QTest::newRow("empty fault") << SodepFault()
<< QByteArray::fromHex("00");
QTest::newRow("no value fault") << SodepFault("foo")
<< QByteArray::fromHex("0100000003")+QByteArray("foo")
+ QByteArray::fromHex("0000000000");
QTest::newRow("value fault") << SodepFault("bar", v)
<< QByteArray::fromHex("0100000003")+QByteArray("bar")
+ vSerial;
}
void shouldBeSerializable()
{
QBuffer buffer;
QFETCH(SodepFault, original);
QFETCH(QByteArray, serialized);
SodepFault result;
buffer.open(QIODevice::WriteOnly);
original.writeTo(buffer);
buffer.close();
buffer.open(QIODevice::ReadOnly);
result = SodepFault::readFrom(buffer);
buffer.close();
sodepCompare(result, original);
QCOMPARE(buffer.data(), serialized);
}
};
QTEST_MAIN(SodepFaultTest)
#include "sodepfaulttest.moc"

View File

@ -0,0 +1,98 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QObject>
#include <QtTest/QtTest>
#include <sodepmessage.h>
#include "sodeptesthelpers.h"
class SodepMessageTest : public QObject
{
Q_OBJECT
private slots:
void shouldVerifyInitialState()
{
SodepMessage m1("/foo", "bar");
SodepMessage m2("/pata/pata", "pon");
QCOMPARE(m1.resourcePath(), QString("/foo"));
QCOMPARE(m1.operationName(), QString("bar"));
QCOMPARE(m2.resourcePath(), QString("/pata/pata"));
QCOMPARE(m2.operationName(), QString("pon"));
m1 = m2;
QCOMPARE(m1.resourcePath(), QString("/pata/pata"));
QCOMPARE(m1.operationName(), QString("pon"));
}
void shouldBeSerializable_data()
{
SodepValue v(42);
QByteArray vSerial = QByteArray::fromHex("020000002A00000000");
SodepFault f("foo");
QByteArray fSerial = QByteArray::fromHex("0100000003")+QByteArray("foo")
+ QByteArray::fromHex("0000000000");
QTest::addColumn<SodepMessage>("original");
QTest::addColumn<QByteArray>("serialized");
QTest::newRow("no payload message") << SodepMessage("/pata", "pon")
<< QByteArray::fromHex("00000005")+QByteArray("/pata")
+ QByteArray::fromHex("00000003")+QByteArray("pon")
+ QByteArray::fromHex("00")
+ QByteArray::fromHex("0000000000");
SodepMessage payload("/pata", "pon");
payload.setFault(f);
payload.setData(v);
QTest::newRow("payload message") << payload
<< QByteArray::fromHex("00000005")+QByteArray("/pata")
+ QByteArray::fromHex("00000003")+QByteArray("pon")
+ fSerial
+ vSerial;
}
void shouldBeSerializable()
{
QBuffer buffer;
QFETCH(SodepMessage, original);
QFETCH(QByteArray, serialized);
SodepMessage result;
buffer.open(QIODevice::WriteOnly);
original.writeTo(buffer);
buffer.close();
buffer.open(QIODevice::ReadOnly);
result = SodepMessage::readFrom(buffer);
buffer.close();
sodepCompare(result, original);
QCOMPARE(buffer.data(), serialized);
}
};
QTEST_MAIN(SodepMessageTest)
#include "sodepmessagetest.moc"

View File

@ -0,0 +1,84 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QBuffer>
#include <QtGui/QApplication>
#include <QtGui/QBoxLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtNetwork/QTcpSocket>
#include <sodepmessage.h>
#include <sodepvalue.h>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow()
{
setLayout(new QHBoxLayout());
m_lineEdit = new QLineEdit(this);
layout()->addWidget(m_lineEdit);
QPushButton *button = new QPushButton(this);
button->setText("SEND");
layout()->addWidget(button);
connect(button, SIGNAL(clicked()),
this, SLOT(sendMessage()));
m_socket.connectToHost("localhost", 10000);
if (!m_socket.waitForConnected(10000)) {
qDebug("Failed to connect!");
return;
}
qDebug("Connected!");
}
private slots:
void sendMessage()
{
SodepMessage message("/", "printInput");
message.setData(SodepValue(m_lineEdit->text()));
message.writeTo(m_socket);
qDebug("Message sent:");
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
message.writeTo(buffer);
buffer.close();
qDebug(buffer.data().toHex());
}
private:
QLineEdit *m_lineEdit;
QTcpSocket m_socket;
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
#include "sodepprintertest.moc"

View File

@ -0,0 +1,79 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SODEPTESTHELPERS_H
#define SODEPTESTHELPERS_H
#include <QtTest/QtTest>
#include <sodepfault.h>
#include <sodepmessage.h>
#include <sodepvalue.h>
Q_DECLARE_METATYPE(SodepValue);
inline void sodepCompare(const SodepValue &v1, const SodepValue &v2)
{
QCOMPARE(v1.isValid(), v2.isValid());
QCOMPARE(v1.isString(), v2.isString());
QCOMPARE(v1.isInt(), v2.isInt());
QCOMPARE(v1.isDouble(), v2.isDouble());
QCOMPARE(v1.toString(), v2.toString());
QCOMPARE(v1.toInt(), v2.toInt());
QCOMPARE(v1.toDouble(), v2.toDouble());
QStringList v1Names = v1.childrenNames();
QStringList v2Names = v2.childrenNames();
QCOMPARE(v1Names, v2Names);
foreach (const QString &name, v1Names) {
QList<SodepValue> v1Values = v1.children(name);
QList<SodepValue> v2Values = v2.children(name);
QCOMPARE(v1Values.size(), v2Values.size());
for (int i=0; i<v1Values.size(); ++i) {
sodepCompare(v1Values[i], v2Values[i]);
}
}
}
Q_DECLARE_METATYPE(SodepFault);
inline void sodepCompare(const SodepFault &f1, const SodepFault &f2)
{
QCOMPARE(f1.isValid(), f2.isValid());
QCOMPARE(f1.name(), f2.name());
sodepCompare(f1.data(), f2.data());
}
Q_DECLARE_METATYPE(SodepMessage);
inline void sodepCompare(const SodepMessage &m1, const SodepMessage &m2)
{
QCOMPARE(m1.resourcePath(), m2.resourcePath());
QCOMPARE(m1.operationName(), m2.operationName());
sodepCompare(m1.fault(), m2.fault());
sodepCompare(m1.data(), m2.data());
}
#endif

View File

@ -0,0 +1,174 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QObject>
#include <QtTest/QtTest>
#include <sodepvalue.h>
#include "sodeptesthelpers.h"
class SodepValueTest : public QObject
{
Q_OBJECT
private slots:
void shouldHandleInvalids()
{
SodepValue v;
QCOMPARE(v.toInt(), 0);
QCOMPARE(v.toDouble(), 0.0);
QCOMPARE(v.toString(), QString());
QVERIFY(!v.isValid());
QVERIFY(!v.isString());
QVERIFY(!v.isInt());
QVERIFY(!v.isDouble());
}
void shouldRespectIntValues()
{
SodepValue v1(42), v2;
QCOMPARE(v1.toInt(), 42);
QCOMPARE(v2.toInt(), 0);
QVERIFY(v1.isInt());
QVERIFY(!v2.isInt());
v2 = v1;
QCOMPARE(v2.toInt(), 42);
QVERIFY(v2.isInt());
QCOMPARE(v2.toDouble(), 0.0);
QCOMPARE(v2.toString(), QString());
}
void shouldRespectDoubleValues()
{
SodepValue v1(0.42), v2;
QCOMPARE(v1.toDouble(), 0.42);
QCOMPARE(v2.toDouble(), 0.0);
QVERIFY(v1.isDouble());
QVERIFY(!v2.isDouble());
v2 = v1;
QCOMPARE(v2.toDouble(), 0.42);
QVERIFY(v2.isDouble());
QCOMPARE(v2.toInt(), 0);
QCOMPARE(v2.toString(), QString());
}
void shouldRespectStringValues()
{
SodepValue v1("42"), v2;
QCOMPARE(v1.toString(), QString("42"));
QCOMPARE(v2.toString(), QString());
QVERIFY(v1.isString());
QVERIFY(!v2.isString());
v2 = v1;
QCOMPARE(v2.toString(), QString("42"));
QVERIFY(v2.isString());
QCOMPARE(v2.toInt(), 0);
QCOMPARE(v2.toDouble(), 0.0);
}
void shouldHandleChildren()
{
SodepValue v;
v.children("first") << SodepValue(7) << SodepValue(8);
v.children("second") << SodepValue(42);
QCOMPARE(v.children("second").size(), 1);
QCOMPARE(v.children("second")[0].toInt(), 42);
QCOMPARE(v.children("first").size(), 2);
QCOMPARE(v.children("first")[0].toInt(), 7);
QCOMPARE(v.children("first")[1].toInt(), 8);
QCOMPARE(v.children("bwaaaaah!").size(), 0);
}
void shouldBeSerializable_data()
{
QTest::addColumn<SodepValue>("original");
QTest::addColumn<QByteArray>("serialized");
QTest::newRow("empty value") << SodepValue()
<< QByteArray::fromHex("0000000000");
QTest::newRow("double value") << SodepValue(0.42)
<< QByteArray::fromHex("033FDAE147AE147AE100000000");
QTest::newRow("int value") << SodepValue(42)
<< QByteArray::fromHex("020000002A00000000");
QTest::newRow("string value") << SodepValue("foo")
<< QByteArray::fromHex("0100000003")+QByteArray("foo")
+ QByteArray::fromHex("00000000");
SodepValue complex("complex");
complex.children("foo") << SodepValue(42);
complex.children("bar") << SodepValue(0.42);
QTest::newRow("complex value") << complex
<< QByteArray::fromHex("0100000007")+QByteArray("complex")
+ QByteArray::fromHex("00000002") // two children
+ QByteArray::fromHex("00000003")+QByteArray("bar")
+ QByteArray::fromHex("00000001") // one value
+ QByteArray::fromHex("033FDAE147AE147AE100000000")
+ QByteArray::fromHex("00000003")+QByteArray("foo")
+ QByteArray::fromHex("00000001") // one value
+ QByteArray::fromHex("020000002A00000000");
}
void shouldBeSerializable()
{
QBuffer buffer;
QFETCH(SodepValue, original);
QFETCH(QByteArray, serialized);
SodepValue result;
buffer.open(QIODevice::WriteOnly);
original.writeTo(buffer);
buffer.close();
buffer.open(QIODevice::ReadOnly);
result = SodepValue::readFrom(buffer);
buffer.close();
sodepCompare(result, original);
QCOMPARE(buffer.data(), serialized);
}
};
QTEST_MAIN(SodepValueTest)
#include "sodepvaluetest.moc"