Move exporter to separate class.

This commit is contained in:
Florian Geyer 2014-05-16 11:48:30 +02:00
parent c2940a8f18
commit 204cd8d971
10 changed files with 189 additions and 62 deletions

View File

@ -48,6 +48,7 @@ set(keepassx_SOURCES
core/SignalMultiplexer.cpp
core/TimeDelta.cpp
core/TimeInfo.cpp
core/ToDbExporter.cpp
core/Tools.cpp
core/Uuid.cpp
core/qcommandlineoption.cpp

View File

@ -500,22 +500,6 @@ void Group::copyDataFrom(const Group* other)
m_lastTopVisibleEntry = other->m_lastTopVisibleEntry;
}
Database* Group::exportToDb()
{
Q_ASSERT(database());
Database* db = new Database();
Group* clonedGroup = clone(Entry::CloneNewUuid | Entry::CloneIncludeHistory);
clonedGroup->setParent(db->rootGroup());
QSet<Uuid> customIcons = customIconsRecursive();
db->metadata()->copyCustomIcons(customIcons, database()->metadata());
db->copyAttributesFrom(database());
return db;
}
void Group::addEntry(Entry* entry)
{
Q_ASSERT(entry);

View File

@ -111,7 +111,6 @@ public:
*/
Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const;
void copyDataFrom(const Group* other);
Database* exportToDb();
Q_SIGNALS:
void dataChanged(Group* group);

39
src/core/ToDbExporter.cpp Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ToDbExporter.h"
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
Database* ToDbExporter::exportToDb(Group* group)
{
Database* oldDb = group->database();
Q_ASSERT(oldDb);
Database* db = new Database();
Group* clonedGroup = group->clone(Entry::CloneNewUuid | Entry::CloneIncludeHistory);
clonedGroup->setParent(db->rootGroup());
QSet<Uuid> customIcons = group->customIconsRecursive();
db->metadata()->copyCustomIcons(customIcons, oldDb->metadata());
db->copyAttributesFrom(oldDb);
return db;
}

31
src/core/ToDbExporter.h Normal file
View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_TODBEXPORTER_H
#define KEEPASSX_TODBEXPORTER_H
class Database;
class Group;
class ToDbExporter
{
public:
Database* exportToDb(Group* group);
};
#endif // KEEPASSX_TODBEXPORTER_H

View File

@ -168,6 +168,9 @@ add_unit_test(NAME testrandom SOURCES TestRandom.cpp MOCS TestRandom.h
add_unit_test(NAME testentrysearcher SOURCES TestEntrySearcher.cpp MOCS TestEntrySearcher.h
LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testexporter SOURCES TestExporter.cpp MOCS TestExporter.h
LIBS ${TEST_LIBRARIES})
if(WITH_GUI_TESTS)
add_subdirectory(gui)
endif(WITH_GUI_TESTS)

82
tests/TestExporter.cpp Normal file
View File

@ -0,0 +1,82 @@
/*
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestExporter.h"
#include <QTest>
#include "tests.h"
#include "core/ToDbExporter.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
QTEST_GUILESS_MAIN(TestExporter)
void TestExporter::initTestCase()
{
Crypto::init();
}
void TestExporter::testToDbExporter()
{
QImage iconImage(1, 1, QImage::Format_RGB32);
iconImage.setPixel(0, 0, qRgb(1, 2, 3));
Uuid iconUuid = Uuid::random();
QImage iconUnusedImage(1, 1, QImage::Format_RGB32);
iconUnusedImage.setPixel(0, 0, qRgb(1, 2, 3));
Uuid iconUnusedUuid = Uuid::random();
Database* dbOrg = new Database();
Group* groupOrg = new Group();
groupOrg->setParent(dbOrg->rootGroup());
groupOrg->setName("GTEST");
Entry* entryOrg = new Entry();
entryOrg->setGroup(groupOrg);
entryOrg->setTitle("ETEST");
dbOrg->metadata()->addCustomIcon(iconUuid, iconImage);
dbOrg->metadata()->addCustomIcon(iconUnusedUuid, iconUnusedImage);
entryOrg->setIcon(iconUuid);
entryOrg->beginUpdate();
entryOrg->setIcon(Entry::DefaultIconNumber);
entryOrg->endUpdate();
Database* dbExp = ToDbExporter().exportToDb(groupOrg);
QCOMPARE(dbExp->rootGroup()->children().size(), 1);
Group* groupExp = dbExp->rootGroup()->children().first();
QVERIFY(groupExp != groupOrg);
QCOMPARE(groupExp->name(), groupOrg->name());
QCOMPARE(groupExp->entries().size(), 1);
Entry* entryExp = groupExp->entries().first();
QCOMPARE(entryExp->title(), entryOrg->title());
QCOMPARE(dbExp->metadata()->customIcons().size(), 1);
QVERIFY(dbExp->metadata()->containsCustomIcon(iconUuid));
QCOMPARE(entryExp->iconNumber(), entryOrg->iconNumber());
QCOMPARE(entryExp->historyItems().size(), 1);
QCOMPARE(entryExp->historyItems().first()->iconUuid(), iconUuid);
delete dbOrg;
delete dbExp;
}

33
tests/TestExporter.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_TESTEXPORTER_H
#define KEEPASSX_TESTEXPORTER_H
#include <QObject>
class TestExporter : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testToDbExporter();
};
#endif // KEEPASSX_TESTEXPORTER_H

View File

@ -441,48 +441,4 @@ void TestGroup::testCopyCustomIcons()
QCOMPARE(metaTarget->customIcon(group2Icon).pixel(0, 0), qRgb(4, 5, 6));
}
void TestGroup::testExportToDb()
{
QImage iconImage(1, 1, QImage::Format_RGB32);
iconImage.setPixel(0, 0, qRgb(1, 2, 3));
Uuid iconUuid = Uuid::random();
QImage iconUnusedImage(1, 1, QImage::Format_RGB32);
iconUnusedImage.setPixel(0, 0, qRgb(1, 2, 3));
Uuid iconUnusedUuid = Uuid::random();
Database* dbOrg = new Database();
Group* groupOrg = new Group();
groupOrg->setParent(dbOrg->rootGroup());
groupOrg->setName("GTEST");
Entry* entryOrg = new Entry();
entryOrg->setGroup(groupOrg);
entryOrg->setTitle("ETEST");
dbOrg->metadata()->addCustomIcon(iconUuid, iconImage);
dbOrg->metadata()->addCustomIcon(iconUnusedUuid, iconUnusedImage);
entryOrg->setIcon(iconUuid);
entryOrg->beginUpdate();
entryOrg->setIcon(Entry::DefaultIconNumber);
entryOrg->endUpdate();
Database* dbExp = groupOrg->exportToDb();
QCOMPARE(dbExp->rootGroup()->children().size(), 1);
Group* groupExp = dbExp->rootGroup()->children().first();
QVERIFY(groupExp != groupOrg);
QCOMPARE(groupExp->name(), groupOrg->name());
QCOMPARE(groupExp->entries().size(), 1);
Entry* entryExp = groupExp->entries().first();
QCOMPARE(entryExp->title(), entryOrg->title());
QCOMPARE(dbExp->metadata()->customIcons().size(), 1);
QVERIFY(dbExp->metadata()->containsCustomIcon(iconUuid));
QCOMPARE(entryExp->iconNumber(), entryOrg->iconNumber());
QCOMPARE(entryExp->historyItems().size(), 1);
QCOMPARE(entryExp->historyItems().first()->iconUuid(), iconUuid);
delete dbOrg;
delete dbExp;
}
QTEST_GUILESS_MAIN(TestGroup)

View File

@ -33,7 +33,6 @@ private Q_SLOTS:
void testCopyCustomIcon();
void testClone();
void testCopyCustomIcons();
void testExportToDb();
};
#endif // KEEPASSX_TESTGROUP_H