1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "ClarkTests.h"
23 
24 #include <QDomElement>
25 #include <QFileInfo>
26 #include <QTextStream>
27 
28 #include <U2Core/U2SafePoints.h>
29 
30 namespace U2 {
31 
32 const QString GTest_CompareClarkDatabaseMetafiles::DATABASE1 = "database1";
33 const QString GTest_CompareClarkDatabaseMetafiles::DATABASE2 = "database2";
34 const QString GTest_CompareClarkDatabaseMetafiles::DATABASE1_PREFIXES = "database1-prefixes";
35 const QString GTest_CompareClarkDatabaseMetafiles::DATABASE2_PREFIXES = "database2-prefixes";
36 
37 const QString GTest_CompareClarkDatabaseMetafiles::DATABASE_PREFIX_PLACEHOLDER = "!@#$%^&*()";
38 const QStringList GTest_CompareClarkDatabaseMetafiles::DATABASE_METAFILES = {".custom",
39                                                                              ".custom.fileToAccssnTaxID",
40                                                                              ".custom.fileToTaxIDs",
41                                                                              ".custom_rejected",
42                                                                              "files_excluded.txt",
43                                                                              "targets.txt"};
44 const QString GTest_CompareClarkDatabaseMetafiles::PREFIXES_SEPARATOR = ";";
45 
init(XMLTestFormat *,const QDomElement & element)46 void GTest_CompareClarkDatabaseMetafiles::init(XMLTestFormat *, const QDomElement &element) {
47     // database 1
48     checkNecessaryAttributeExistence(element, DATABASE1);
49     CHECK_OP(stateInfo, );
50 
51     database1 = element.attribute(DATABASE1);
52     CHECK_EXT(!database1.isEmpty(), setError("Database 1 URL is empty"), );
53 
54     XMLTestUtils::replacePrefix(env, database1);
55 
56     // database 2
57     checkNecessaryAttributeExistence(element, DATABASE2);
58     CHECK_OP(stateInfo, );
59 
60     database2 = element.attribute(DATABASE2);
61     CHECK_EXT(!database2.isEmpty(), setError("Database 2 URL is empty"), );
62 
63     XMLTestUtils::replacePrefix(env, database2);
64 
65     // database 1 prefix
66     checkNecessaryAttributeExistence(element, DATABASE1_PREFIXES);
67     CHECK_OP(stateInfo, );
68 
69     foreach (QString prefix, element.attribute(DATABASE1_PREFIXES).split(PREFIXES_SEPARATOR)) {
70         XMLTestUtils::replacePrefix(env, prefix);
71         database1Prefixes << prefix;
72     }
73 
74     // database 2 prefix
75     checkNecessaryAttributeExistence(element, DATABASE2_PREFIXES);
76     CHECK_OP(stateInfo, );
77 
78     foreach (QString prefix, element.attribute(DATABASE2_PREFIXES).split(PREFIXES_SEPARATOR)) {
79         XMLTestUtils::replacePrefix(env, prefix);
80         database2Prefixes << prefix;
81     }
82 }
83 
report()84 Task::ReportResult GTest_CompareClarkDatabaseMetafiles::report() {
85     CHECK_OP(stateInfo, ReportResult_Finished);
86 
87     CHECK_EXT(QFileInfo::exists(database1), setError(QString("Database 1 doesn't exist: '%1'").arg(database1)), ReportResult_Finished);
88     CHECK_EXT(QFileInfo(database1).isDir(), setError(QString("Database 1 is not a directory: '%1'").arg(database1)), ReportResult_Finished);
89 
90     CHECK_EXT(QFileInfo::exists(database2), setError(QString("Database 2 doesn't exist: '%1'").arg(database2)), ReportResult_Finished);
91     CHECK_EXT(QFileInfo(database2).isDir(), setError(QString("Database 2 is not a directory: '%1'").arg(database2)), ReportResult_Finished);
92 
93     foreach (const QString &metafileName, DATABASE_METAFILES) {
94         const QString metafile1Url = database1 + "/" + metafileName;
95         QFile metafile1(metafile1Url);
96         bool opened = metafile1.open(QIODevice::ReadOnly);
97         CHECK_EXT(opened, setError(QString("Can't open metafile '%1' for reading").arg(metafile1.fileName())), ReportResult_Finished);
98         QTextStream metafileStream1(&metafile1);
99 
100         const QString metafile2Url = database2 + "/" + metafileName;
101         QFile metafile2(metafile2Url);
102         opened = metafile2.open(QIODevice::ReadOnly);
103         CHECK_EXT(opened, setError(QString("Can't open metafile '%1' for reading").arg(metafile2.fileName())), ReportResult_Finished);
104         QTextStream metafileStream2(&metafile2);
105 
106         int counter = 0;
107         while (!metafileStream1.atEnd() && !metafileStream2.atEnd()) {
108             QString metafile1Line = metafileStream1.readLine();
109             foreach (const QString &prefix, database1Prefixes) {
110                 metafile1Line.replace(prefix, DATABASE_PREFIX_PLACEHOLDER);
111             }
112 
113             QString metafile2Line = metafileStream2.readLine();
114             foreach (const QString &prefix, database2Prefixes) {
115                 metafile2Line.replace(prefix, DATABASE_PREFIX_PLACEHOLDER);
116             }
117 
118             CHECK_EXT(metafile1Line == metafile2Line,
119                       setError(QString("Metafiles '%1' and '%2' differs at line %3: '%4' and '%5'")
120                                    .arg(metafile1Url)
121                                    .arg(metafile2Url)
122                                    .arg(++counter)
123                                    .arg(metafile1Line)
124                                    .arg(metafile2Line)),
125                       ReportResult_Finished);
126         }
127 
128         CHECK_EXT(metafileStream1.atEnd() && metafileStream2.atEnd(),
129                   setError(QString("Metafiles '%1' and '%2' have different number of lines")
130                                .arg(metafile1Url)
131                                .arg(metafile2Url)),
132                   ReportResult_Finished);
133     }
134 
135     return ReportResult_Finished;
136 }
137 
createTestFactories()138 QList<XMLTestFactory *> ClarkTests::createTestFactories() {
139     return {GTest_CompareClarkDatabaseMetafiles::createFactory()};
140 }
141 
142 }  // namespace U2
143