Torc  0.1
torcxsdtest.cpp
Go to the documentation of this file.
1 /* Class TorcXSDTest
2 *
3 * Copyright (C) Mark Kendall 2016-18
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 // Qt
21 #include <QDir>
22 #include <QFile>
23 
24 // Torc
25 #include "torclocalcontext.h"
26 #include "torcexitcodes.h"
27 #include "torclogging.h"
28 #include "torcdirectories.h"
29 #include "torccentral.h"
30 #include "torcxsdtest.h"
31 
32 #ifdef USING_XMLPATTERNS
33 #include "torcxmlvalidator.h"
34 #elif USING_LIBXML2
35 #include "torclibxmlvalidator.h"
36 #endif
37 
91 {
92 #if defined(USING_XMLPATTERNS) || defined(USING_LIBXML2)
93  if (!CommandLine)
95 
96  if (int error = TorcLocalContext::Create(CommandLine, false))
97  return error;
98 
99  QString directory = CommandLine->GetValue(QStringLiteral("xsdtest")).toString();
100  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Starting XSD test from %1").arg(directory));
101 
102  QString basexsd = QStringLiteral("%1/html/torc.xsd").arg(GetTorcShareDir());
103  QByteArray fullxsd = TorcCentral::GetCustomisedXSD(basexsd);
104  if (fullxsd.isEmpty())
105  {
107  return TORC_EXIT_UNKOWN_ERROR;
108  }
109 
110  QDir dir(directory);
111  QStringList filters;
112  filters << QStringLiteral("*.xml");
113 
114  QStringList testfiles = dir.entryList(filters, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable | QDir::CaseSensitive, QDir::Name);
115 
116  if (testfiles.isEmpty())
117  {
118  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Failed to find any xml files in %1").arg(dir.path()));
120  return TORC_EXIT_UNKOWN_ERROR;
121  }
122 
123  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Found %1 files.").arg(testfiles.size()));
124  int passcount = 0;
125  foreach (const QString &file, testfiles)
126  {
127  // these should all FAIL!
128  QString path = directory + "/" + file;
129  LOG(VB_GENERAL, LOG_INFO, path);
130  TorcXmlValidator validator(path, fullxsd, true);
131  if (validator.Validated())
132  {
133  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Unexpected pass: %1").arg(path));
134  passcount++;
135  }
136  }
137 
138  if (!passcount)
139  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("All test files failed as expected"));
140  else
141  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("%1 unexpected passes.").arg(passcount));
142 
144 
145 #else
146  (void) CommandLine;
147  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("XML validation not available."));
148 #endif
149 
150  return TORC_EXIT_OK;
151 }
152 
static qint16 Create(TorcCommandLine *CommandLine, bool Init=true)
static QByteArray GetCustomisedXSD(const QString &BaseXSDFile)
#define TORC_EXIT_INVALID_CMDLINE
Definition: torcexitcodes.h:7
QString GetTorcShareDir(void)
Return the path to the installed Torc shared resources.
Torc command line handler.
#define TORC_EXIT_UNKOWN_ERROR
Definition: torcexitcodes.h:6
static int RunXSDTestSuite(TorcCommandLine *CommandLine)
Perform XSD test validation of XML files.
Definition: torcxsdtest.cpp:90
static void TearDown(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
#define TORC_EXIT_OK
Definition: torcexitcodes.h:4
QVariant GetValue(const QString &Key)
Return the value associated with Key or an invalid QVariant if the option is not present.
bool Validated(void) const