DRDE/AusRegCliever/server/mdJSON.cpp

166 lines
4.4 KiB
C++
Raw Normal View History

2014-01-28 18:16:48 +00:00
/*
* mdJSON.cpp
*
* Created on: Jan 28, 2014
* Author: jdaugherty
*/
2014-01-29 00:02:24 +00:00
#define MD_JSON
2014-01-28 18:16:48 +00:00
#include "cliever-md.h"
2014-01-28 19:23:02 +00:00
#include <algorithm> // sort
#include <json/json.h>
#include <stdio.h>
2014-01-29 18:10:26 +00:00
#include "mdJSON.hpp"
2014-01-28 19:23:02 +00:00
2014-01-30 23:27:18 +00:00
namespace ACPRODINOTE {
extern testCases theseCases;
extern testFuncs theseFuncs;
2014-01-29 00:02:24 +00:00
2014-01-30 23:27:18 +00:00
}
2014-01-29 00:02:24 +00:00
using namespace std;
2014-01-28 19:23:02 +00:00
static std::string
readInputTestFile( const char *path )
{
FILE *file = fopen( path, "rb" );
if ( !file )
return std::string("");
fseek( file, 0, SEEK_END );
long size = ftell( file );
fseek( file, 0, SEEK_SET );
std::string text;
char *buffer = new char[size+1];
buffer[size] = 0;
if ( fread( buffer, 1, size, file ) == (unsigned long)size )
text = buffer;
fclose( file );
delete[] buffer;
return text;
}
2014-01-29 18:10:26 +00:00
bool mdJSON::run()
{
2014-01-29 23:57:01 +00:00
bool done=false;
int i=0, nCases =0, nThings = 0, debug=1000;
2014-01-29 18:10:26 +00:00
const Json::Value suite = root["testSuite00"];
if (!suite) {
theseLogs->logN(0,"No 'testSuite00' suite root, can't run.");
return true;
}
2014-01-29 23:57:01 +00:00
if (!suite.isObject()) {
theseLogs->logN(0,"'testSuite00' isn't an object, can't run.");
return true;
}
if (suite.empty()) {
theseLogs->logN(0,"'testSuite00' contains nothing, can't run.");
return true;
}
theseLogs->logN(1,"'testSuite00' - binding %d fields and case objects.",suite.size());
Json::Value::Members itemNames = suite.getMemberNames();
for ( i = 0; i < itemNames.size(); ++i ) { const char *thisItem;
2014-01-30 02:30:49 +00:00
try {
2014-01-29 23:57:01 +00:00
thisItem = itemNames[i].c_str();
2014-01-30 15:25:00 +00:00
if (debug > 100000)
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"item %s.",thisItem);
2014-01-30 15:25:00 +00:00
if (!stricmp(thisItem,"registry")) {
}
2014-01-29 18:10:26 +00:00
if (strncmp(thisItem,"case",4)) continue;
if (strlen(thisItem) != 6) continue;
2014-01-30 15:25:00 +00:00
if (debug > 100000)
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"case %s.",thisItem);
2014-01-30 23:27:18 +00:00
if (!ACPRODINOTE::theseFuncs[thisItem]) {
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"No logic to bind to '%s', need it.",thisItem);
2014-01-29 18:10:26 +00:00
return false;
}
2014-01-30 15:25:00 +00:00
//if (!suite[i].isObject()) {
// theseLogs->logN(1,"'%s' isn't an object, can't use.",thisItem);
// continue;
//}
2014-01-30 23:27:18 +00:00
ACPRODINOTE::theseCases[nCases].parms = NULL;
ACPRODINOTE::theseCases[nCases].fBody = ACPRODINOTE::theseFuncs[thisItem];
ACPRODINOTE::theseCases[nCases++].caseName = thisItem;
2014-01-30 02:30:49 +00:00
}
catch (...)
{
theseLogs->logN(1,"Test case binding exception: %s ",thisItem );
}
2014-01-29 18:10:26 +00:00
}
2014-01-30 02:30:49 +00:00
theseLogs->logN(1,"%d cases bound, beginning execution.",nCases);
2014-01-29 18:10:26 +00:00
2014-01-30 23:27:18 +00:00
for (i=0;i<ACPRODINOTE::theseCases.size();i++) {
2014-01-29 23:57:01 +00:00
try{
2014-01-30 23:27:18 +00:00
theseLogs->logN(2,"%d Begin setup of %s ",i+1,ACPRODINOTE::theseCases[i].caseName );
ACPRODINOTE::theseCases[i].fBody();
theseLogs->logN(2,"%d End setup of %s ",i+1,ACPRODINOTE::theseCases[i].caseName );
2014-01-29 18:10:26 +00:00
}
2014-01-30 23:27:18 +00:00
catch (exception e)
{
theseLogs->logN(2,"Case %s fault: %s ",ACPRODINOTE::theseCases[i].caseName, e.what());
}
2014-01-29 23:57:01 +00:00
catch (...)
{
2014-01-30 23:27:18 +00:00
theseLogs->logN(1,"Unknown test case fault in %s ",ACPRODINOTE::theseCases[i].caseName );
2014-01-29 23:57:01 +00:00
}
}
theseLogs->logN(0,"Suite 'testSuite00' end execution.");
2014-01-29 18:10:26 +00:00
}
2014-01-28 19:23:02 +00:00
static bool
2014-01-29 00:02:24 +00:00
parseValueTree( const std::string &input, const std::string &kind, Json::Value &root, const Json::Features &features)
2014-01-28 19:23:02 +00:00
{
Json::Reader reader( features );
bool parsingSuccessful = reader.parse( input, root );
if ( !parsingSuccessful )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(2, "Failed to parse %s file: %s",
2014-01-28 19:23:02 +00:00
kind.c_str(),
reader.getFormattedErrorMessages().c_str() );
return true;
}
return false;
2014-01-28 18:16:48 +00:00
}
2014-01-29 00:02:24 +00:00
bool mdJSON::parse()
2014-01-28 19:23:02 +00:00
{
bool value = false;
Json::Features features;
try
{
std::string input = readInputTestFile( path.c_str() );
if ( input.empty() )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Failed to read input or empty input: %s", path.c_str() );
2014-01-28 19:23:02 +00:00
return 3;
}
2014-01-29 00:02:24 +00:00
return parseValueTree( input, "input", root, features );
2014-01-28 19:23:02 +00:00
}
catch ( const std::exception &e )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Unhandled exception: %s", e.what() );
2014-01-28 19:23:02 +00:00
value = true;
}
return value;
2014-01-28 18:16:48 +00:00
}
2014-01-29 18:10:26 +00:00
void mdJSON::setPath(char *fileName) {
2014-01-28 19:23:02 +00:00
2014-01-29 18:10:26 +00:00
path = string(fileName);
2014-01-28 18:16:48 +00:00
}