// // See the file LICENSE for redistribution information. // // Copyright (c) 2002,2009 Oracle. All rights reserved. // // // Test program for iterative methods in XmlResults (extension of 11.3.1/11.4.1) // System includes #include #include #include // XQilla includes #include // Xerces includes #include #include #include // DB XML includes (external) #include "dbxml/DbXml.hpp" // test code includes #include "../util/TestLogging.hpp" using namespace DbXmlTest; using namespace DbXml; using namespace std; // function prototypes (so that main() appears first...) void doTest(TestLogger &log, const std::string &testid, bool cdb, bool transacted, bool nodeLevelStorage, bool indexNodes, const std::string &path2DbEnv); void usage(const std::string &progname, int exitCode); int main(int argc, char **argv) { string testid; // arbitrary id for test (used for file names) bool transacted(false); // true iff a transacted environment is used bool cdb(false); // true iff a CDS environment is used bool nodeLevelStorage(false); // node level storage (whole docs otherwise) bool indexNodes(false); // use the DBXML_INDEX_NODES flag bool verifyLog(false); // true iff log file is to be verified string path2DbEnv; // path to DB XML env (must exist) string logDir("./"); // path to dir to contain log file int err(1); for(int i=1; iremove(tenv, path2DbEnv.c_str(), 0); // prepare - database, container, document u_int32_t flags = (DB_CREATE|DB_INIT_MPOOL); if (cdb) { flags |= DB_INIT_CDB; } else if (transacted) { flags |=( DB_INIT_TXN|DB_INIT_LOG|DB_INIT_LOCK); } DB_ENV *env; (void) db_env_create(&env, 0); if (flags & DB_INIT_CDB) env->set_flags(env, DB_CDB_ALLDB, 1); int err = env->open(env, path2DbEnv.c_str(), flags, 0); if (err) { cerr << "Failed to open DB_ENV: " << err << endl; exit(-1); } XmlManager db(env, DBXML_ADOPT_DBENV); db.setDefaultContainerType(nodeLevelStorage ? XmlContainer::NodeContainer : XmlContainer::WholedocContainer); db.setDefaultContainerFlags(indexNodes ? DBXML_INDEX_NODES : DBXML_NO_INDEX_NODES); XmlDocument doc = db.createDocument(); string docContent("cef"); doc.setContent(docContent); doc.setName("foo"); XmlUpdateContext uc = db.createUpdateContext(); XmlTransaction *txn = 0; if(transacted) { txn = new XmlTransaction(db.createTransaction()); } XmlContainer *container = 0; string containerName = testid + ".dbxml"; if(transacted) { container = new XmlContainer(db.createContainer(*txn, containerName)); container->putDocument(*txn, doc, uc); } else { container = new XmlContainer(db.createContainer(containerName)); container->putDocument(doc, uc); } ostringstream os; os << "Created the container '" << container->getName() << "'"; os << " ("; switch (container->getContainerType()) { case XmlContainer::WholedocContainer: os << "whole document storage"; break; case XmlContainer::NodeContainer: os << "node level storage"; break; default: os << "unknown storage"; break; } os << ")"; TEST_MSG(log, os.str()); ///////////////////////////////////////////////////////////////////////// // iterative methods on XmlResults (eager evaluation) { TEST_MSG(log, "Using eager evaluation"); // execute a simple query that gives a single result XmlQueryContext qc = db.createQueryContext(); qc.setEvaluationType(XmlQueryContext::Eager); string queryString("collection('" + container->getName() + "')/a/b"); XmlResults *xr = 0; if(transacted) { xr = new XmlResults(db.query(*txn, queryString, qc)); } else { xr = new XmlResults(db.query(queryString, qc)); } if(xr->size() != 1) { ERROR_MSG(log, "Wrong number of results, expected 1 but got %1") << (unsigned int) xr->size(); throw std::exception(); } // test that the document content contains the value as a substring, wheras // the document as a string should match the document content // TEST next(XmlValue) TEST_MSG(log, "Testing XmlResults::next(XmlValue)..."); XmlValue xv; int err = 0; int count = 0; while(xr->next(xv)) { ++count; if(docContent.find(xv.asString()) == string::npos) { ERROR_MSG(log, "result failed, expected to find '%1' within '%2'") << xv.asString() << docContent; ++err; } } if(count != 1) { ERROR_MSG(log, "Wrong number of results, expected 1 but got %1") << count; ++err; } if(!err) TEST_MSG(log, "...OK"); // TEST next(XmlDocument) TEST_MSG(log, "Testing XmlResults::next(XmlDocument)..."); xr->reset(); XmlDocument xd = db.createDocument();; count = err = 0; string s; while(xr->next(xd)) { ++count; if(docContent.compare(xd.getContent(s))) { ERROR_MSG(log, "result failed, expected '%1' but got '%2'") << docContent << xd.getContent(s); ++err; } } if(count != 1) { ERROR_MSG(log, "Wrong number of results, expected 1 but got %1") << count; ++err; } if(!err) TEST_MSG(log, "...OK"); if(transacted) { txn->commit(); } delete xr; delete txn; } ///////////////////////////////////////////////////////////////////////// // iterative methods on XmlResults (lazy evaluation) { TEST_MSG(log, "Using lazy evaluation"); if(transacted) { txn = new XmlTransaction(db.createTransaction()); } // execute a simple query that gives a single result XmlQueryContext qc = db.createQueryContext(); qc.setEvaluationType(XmlQueryContext::Lazy); string queryString("collection('" + container->getName() + "')/a/b"); // test that the value contains the document content as a substring, wheras // the document as a string should match the document content // Note that transaction is kept open for the iterations XmlResults *xr = 0; if(transacted) { xr = new XmlResults(db.query(*txn, queryString, qc)); } else { xr = new XmlResults(db.query(queryString, qc)); } // TEST next(XmlValue) TEST_MSG(log, "Testing XmlResults::next(XmlValue)..."); XmlValue xv; int err = 0; int count = 0; while(xr->next(xv)) { ++count; if(docContent.find(xv.asString()) == string::npos) { ERROR_MSG(log, "result failed, expected to find '%1' within '%2'") << xv.asString() << docContent; ++err; } } if(count != 1) { ERROR_MSG(log, "Wrong number of results, expected 1 but got %1") << count; ++err; } if(!err) TEST_MSG(log, "...OK"); // TEST next(XmlDocument) TEST_MSG(log, "Testing XmlResults::next(XmlDocument)..."); xr->reset(); XmlDocument xd = db.createDocument();; count = err = 0; string s; while(xr->next(xd)) { ++count; if(docContent.compare(xd.getContent(s))) { ERROR_MSG(log, "result failed, expected '%1' but got '%2'") << docContent << xd.getContent(s); ++err; } } if(count != 1) { ERROR_MSG(log, "Wrong number of results, expected 1 but got %1") << count; ++err; } if(!err) TEST_MSG(log, "...OK"); if(transacted) { txn->commit(); } delete xr; delete txn; } // clean up delete container; ///////////////////////////////////////////////////////////////////// TEST_MSG(log, "Completed test"); }