From 11515bf5f3a5971714f2cba9b03dfdafb63e9b15 Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 29 Jul 2009 15:05:07 +0000 Subject: Added testsrcipts to the transactionparser. Still missing the documentation though. --- server/src/Makefile.am | 8 +++ server/src/transactionparser.cc | 128 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 4 deletions(-) (limited to 'server/src') diff --git a/server/src/Makefile.am b/server/src/Makefile.am index f970117..baecd73 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -103,6 +103,7 @@ EXTRA_DIST = \ TESTFILES = \ test_saxparser \ + test_transactionparser \ test_versionstr \ test_macrolist \ test_queryhandlerpentominos \ @@ -139,6 +140,13 @@ TEST_SAXPARSER_FILES = \ test_saxparser: $(TEST_SAXPARSER_FILES) @../../tools/test $(TEST_SAXPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) +TEST_TRANSACTIONPARSER_FILES = \ + transactionparser.cc \ + $(BASICFILES) \ + $(PARSERFILES) +test_transactionparser: $(TEST_TRANSACTIONPARSER_FILES) + @../../tools/test $(TEST_TRANSACTIONPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) + TEST_VERSIONSTR_FILES = \ versionstr.cc \ $(BASICFILES) diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc index 9e3d952..08c0a42 100644 --- a/server/src/transactionparser.cc +++ b/server/src/transactionparser.cc @@ -33,6 +33,34 @@ #include #include +#include "debug.h" +#include "exception.h" + +static void error(const char* fmt, ...) +{ + PRACRO_ERR_LOG(transactionparser, "Error in TransactionParser: "); + + { + va_list argp; + va_start(argp, fmt); + PRACRO_ERR_LOG_VA(macro, fmt, argp); + va_end(argp); + + fprintf(stderr, "\n"); + } + + { + char *p; + va_list argp; + va_start(argp, fmt); + if(vasprintf(&p, fmt, argp) != -1) { + throw Exception("Error in TransactionParser: " + std::string(p)); + free(p); + } + va_end(argp); + } +} + TransactionParser::TransactionParser(Transaction *transaction) { this->transaction = transaction; @@ -68,15 +96,107 @@ void TransactionParser::startTag(std::string name, std::map< std::string, std::s } if(name == "field") { + if(!transaction->commits.size()) error("Field without a commit tag!"); + if(attributes.find("name") == attributes.end()) error("Field is missing 'name' attribute"); + if(attributes.find("value") == attributes.end()) error("Field is missing 'value' attribute"); transaction->commits.back().fields[attributes["name"]] = attributes["value"]; } } void TransactionParser::parseError(char *buf, size_t len, std::string error, int lineno) { - fprintf(stderr, "TransactionParser error at line %d: %s\n", lineno, error.c_str()); - fprintf(stderr, "\tBuffer %u bytes: [", len); + PRACRO_ERR_LOG(transactionparser, "TransactionParser error at line %d: %s\n", + lineno, error.c_str()); + PRACRO_ERR_LOG(transactionparser, "\tBuffer %u bytes: [", len); if(fwrite(buf, len, 1, stderr) != len) {} - fprintf(stderr, "]\n"); - fflush(stderr); + PRACRO_ERR_LOG(transactionparser, "]\n"); + + char *slineno; + if(asprintf(&slineno, " at line %d\n", lineno) != -1) { + throw Exception(error + slineno); + free(slineno); + } } + +#ifdef TEST_TRANSACTIONPARSER + +static char xml_minimal[] = +"\n" +"\n" + ; + +static char xml_request[] = +"\n" +"\n" +" \n" +"\n" + ; + +static char xml_commit[] = +"\n" +"\n" +" \n" +" \n" +" \n" +" \n" +" \n" +"\n" + ; + +static char xml_fail[] = +"\n" +"\n" +" \n" +"\n" + ; + +int main() +{ + // Test minimal + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_minimal, strlen(xml_minimal)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test request + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_request, strlen(xml_request)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test commit + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_commit, strlen(xml_commit)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test parse error (should throw an exception) + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_fail, strlen(xml_fail)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + goto onandon; + } + printf("We should fail here...\n"); + return 1; + + onandon: + + return 0; +} + +#endif/*TEST_TRANSACTIONPARSER*/ -- cgit v1.2.3