File indexing completed on 2024-05-12 05:10:03

0001 /***************************************************************************
0002     Copyright (C) 2015 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include "modstest.h"
0028 
0029 #include "../translators/xsltimporter.h"
0030 #include "../translators/xslthandler.h"
0031 #include "../collections/bookcollection.h"
0032 #include "../collectionfactory.h"
0033 #include "../fieldformat.h"
0034 #include "../utils/datafileregistry.h"
0035 
0036 #include <KLocalizedString>
0037 
0038 #include <QTest>
0039 #include <QDomDocument>
0040 #include <QTextCodec>
0041 
0042 QTEST_APPLESS_MAIN( ModsTest )
0043 
0044 void ModsTest::initTestCase() {
0045   KLocalizedString::setApplicationDomain("tellico");
0046   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0047   Tellico::DataFileRegistry::self()->addDataLocation(QFINDTESTDATA("../../xslt/mods2tellico.xsl"));
0048 }
0049 
0050 void ModsTest::testBook() {
0051   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/example_mods.xml"));
0052   Tellico::Import::XSLTImporter importer(url);
0053   importer.setXSLTURL(QUrl::fromLocalFile(QFINDTESTDATA("../../xslt/mods2tellico.xsl")));
0054 
0055   Tellico::Data::CollPtr coll = importer.collection();
0056 
0057   QVERIFY(coll);
0058   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0059   QCOMPARE(coll->entryCount(), 1);
0060   QCOMPARE(coll->title(), QStringLiteral("MODS Import"));
0061 
0062   Tellico::Data::EntryPtr entry = coll->entryById(1);
0063   QVERIFY(entry);
0064   QCOMPARE(entry->field("title"), QStringLiteral("Sound and fury"));
0065   QCOMPARE(entry->field("author"), QStringLiteral("Alterman, Eric"));
0066   QCOMPARE(entry->field("genre"), QStringLiteral("bibliography"));
0067   QCOMPARE(entry->field("pub_year"), QStringLiteral("1999"));
0068   QCOMPARE(entry->field("isbn"), QStringLiteral("0-8014-8639-4"));
0069   QCOMPARE(entry->field("lccn"), QStringLiteral("99042030"));
0070 }
0071 
0072 // The Deutsche Nationalbibliothek SRU server returns MARCXML encapsulated in SRW
0073 // The LoC MARC21 to MODS stylesheet has to be modified to accept multiple mods:record elements
0074 // outside of the marc:collection element
0075 // http://www.dnb.de/DE/Service/DigitaleDienste/SRU/sru_node.html
0076 void ModsTest::testDNBMARCXML() {
0077   Tellico::XSLTHandler marcHandler(QUrl::fromLocalFile(QFINDTESTDATA("../../xslt/MARC21slim2MODS3.xsl")));
0078   QVERIFY(marcHandler.isValid());
0079 
0080   QFile f(QFINDTESTDATA("data/dnb-marcxml.xml"));
0081   QVERIFY(f.exists());
0082   QVERIFY(f.open(QIODevice::ReadOnly | QIODevice::Text));
0083   QTextStream stream(&f);
0084   const QString mods = marcHandler.applyStylesheet(stream.readAll());
0085 
0086   Tellico::Import::XSLTImporter importer(mods);
0087   importer.setXSLTURL(QUrl::fromLocalFile(QFINDTESTDATA("../../xslt/mods2tellico.xsl")));
0088 
0089   Tellico::Data::CollPtr coll = importer.collection();
0090   QVERIFY(coll);
0091   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0092   QCOMPARE(coll->entryCount(), 25);
0093 }
0094 
0095 void ModsTest::testLocaleEncoding() {
0096   const auto xslt = QByteArray( \
0097     "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" \
0098     "  <xsl:output method=\"xml\" version=\"1.0\" encoding=\"windows-1251\"/>" \
0099     "  <xsl:template match=\"/\"/>" \
0100     "</xsl:stylesheet>" \
0101   );
0102   QDomDocument dom;
0103   dom.setContent(xslt);
0104   QVERIFY(!dom.isNull());
0105   QVERIFY(dom.toString().contains(QLatin1String("windows-1251")));
0106   Tellico::XSLTHandler::setLocaleEncoding(dom);
0107   QVERIFY(dom.toString().contains(QTextCodec::codecForLocale()->name()));
0108 }