Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-scratch2 into lp:zorba.
Requested reviews: Markos Zaharioudakis (markos-za) For more details, see: https://code.launchpad.net/~zorba-coders/zorba/markos-scratch2/+merge/110609 renamed file test/unit/static_context.cpp to avoid conflict with src/context/static_contectx.cpp during debugging -- https://code.launchpad.net/~zorba-coders/zorba/markos-scratch2/+merge/110609 Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'test/unit/CMakeLists.txt' --- test/unit/CMakeLists.txt 2012-06-14 22:50:21 +0000 +++ test/unit/CMakeLists.txt 2012-06-15 20:03:19 +0000 @@ -97,7 +97,7 @@ xquery_functions.cpp xmldatamanager.cpp staticcollectionmanager.cpp - static_context.cpp + test_static_context.cpp ) # multithread_simple.cpp === renamed file 'test/unit/static_context.cpp' => 'test/unit/static_context.cpp.THIS' === added file 'test/unit/test_static_context.cpp' --- test/unit/test_static_context.cpp 1970-01-01 00:00:00 +0000 +++ test/unit/test_static_context.cpp 2012-06-15 20:03:19 +0000 @@ -0,0 +1,358 @@ +/* + * Copyright 2006-2012 The FLWOR Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <cassert> +#include <iostream> +#include <fstream> +#include <list> +#include <map> + +#include <sstream> +#include <zorba/store_manager.h> +#include <zorba/zorba.h> +#include <zorba/zorba_exception.h> +#include <zorba/diagnostic_list.h> + +using namespace std; +using namespace zorba; +#ifndef ZORBA_NO_FULL_TEXT +using namespace zorba::locale; +#endif /* ZORBA_NO_FULL_TEXT */ + +bool +sctx_test_1(Zorba* const zorba) +{ + StaticContext_t lSctx = zorba->createStaticContext(); + + Zorba_CompilerHints_t lHints; + + std::stringstream lProlog; + lProlog << "declare namespace foo = 'http://www.example.com';"; + + lSctx->loadProlog(lProlog.str(), lHints); + + NsBindings lBindings; + lSctx->getNamespaceBindings(lBindings); + + bool lFooFound = false; + + for (NsBindings::const_iterator lIter = lBindings.begin(); + lIter != lBindings.end(); ++lIter) + { + std::cout << "prefix: " << lIter->first << " bound to " + << lIter->second << std::endl; + + if (lIter->first.compare("foo") == 0) + { + lFooFound = true; + } + } + + return lFooFound && lBindings.size() == 6; +} + +bool +sctx_test_2(Zorba* const zorba) +{ + StaticContext_t lSctx = zorba->createStaticContext(); + + Zorba_CompilerHints_t lHints; + + try + { + Item lFetched = lSctx->fetch("http://www.zorba-xquery.com/modules/fetch", "MODULE"); + + return !lFetched.isNull(); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + } + return false; +} + + +bool sctx_test_3(Zorba* zorba) +{ + StaticContext_t sctx = zorba->createStaticContext(); + + try + { + Zorba_CompilerHints_t hints; + std::stringstream prolog; + prolog << "declare variable $prologVariable := <hello>World!</hello>;"; + sctx->loadProlog(prolog.str(), hints); + + // compile the main query using the populated static context + XQuery_t query = zorba->compileQuery("declare variable $queryVar := <queryVar>foo</queryVar>; $prologVariable ", sctx); + + // execute the query and make sure that the result is correct + Zorba_SerializerOptions lSerOptions; + lSerOptions.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES; + std::stringstream result; + query->execute(result, &lSerOptions); + std::cout << "Print prolog variable: " << result.str() << std::endl; + + if (result.str() != "<hello>World!</hello>") + return false; + } + catch (XQueryException &e) + { + std::cerr << e << std::endl; + return false; + } + + return true; +} + + +bool +sctx_test_4(Zorba* const zorba) +{ + StaticContext_t lSctx = zorba->createStaticContext(); + + Zorba_CompilerHints_t lHints; + + try + { + Item lFetched = lSctx->fetchBinary("http://www.zorba-xquery.com/modules/fetch", "MODULE"); + + size_t s; + return !lFetched.isNull() && lFetched.getBase64BinaryValue(s); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + } + return false; +} + + +bool +sctx_test_5(Zorba* zorba) +{ + std::stringstream queryString1; + std::stringstream queryString2; + + queryString1 + << "import module namespace ddl = " + << "\"http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl\";" + << std::endl + << "ddl:create(xs:QName(\"ddl:coll1\"));" + << std::endl; + + queryString2 + << "import module namespace ddl = " + << "\"http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl\";" + << std::endl + << "ddl:create(xs:QName(\"ddl:coll1\"), <a/>);" + << std::endl; + + ItemFactory* factory = zorba->getItemFactory(); + + Item fname = factory-> + createQName("http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl", + "create"); + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 1); + sctx->disableFunction(fname, 2); + + XQuery_t query = zorba->compileQuery(queryString1, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + if (e.diagnostic() != err::XPST0017) + return false; + } + catch (...) + { + return false; + } + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 1); + sctx->disableFunction(fname, 2); + + queryString2.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString2, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + if (e.diagnostic() != err::XPST0017) + return false; + } + catch (...) + { + return false; + } + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 1); + + queryString1.clear(); + queryString1.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString1, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + if (e.diagnostic() != err::XPST0017) + return false; + } + catch (...) + { + return false; + } + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 1); + + queryString2.clear(); + queryString2.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString2, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + return false; + } + catch (...) + { + return false; + } + + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 2); + + queryString1.clear(); + queryString1.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString1, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + return false; + } + catch (...) + { + return false; + } + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 2); + + queryString2.clear(); + queryString2.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString2, sctx); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + if (e.diagnostic() != err::XPST0017) + return false; + } + catch (...) + { + return false; + } + + + try + { + StaticContext_t sctx = zorba->createStaticContext(); + sctx->disableFunction(fname, 2); + + queryString1.clear(); + queryString1.seekg(0, std::ios::beg); + + XQuery_t query = zorba->compileQuery(queryString1, sctx); + + std::ofstream planFile("out.plan"); + assert(planFile.good()); + + query->saveExecutionPlan(planFile); + } + catch (ZorbaException& e) + { + std::cerr << e << std::endl; + + return false; + } + catch (...) + { + std::cerr << "unknown exception" << std::endl; + + return false; + } + + return true; +} + + + +int test_static_context( int argc, char *argv[] ) +{ + void* zstore = StoreManager::getStore(); + Zorba* zorba = Zorba::getInstance(zstore); + + if (!sctx_test_1(zorba)) + return 1; + + if (!sctx_test_2(zorba)) + return 2; + + if (!sctx_test_3(zorba)) + return 3; + + if (!sctx_test_4(zorba)) + return 4; + + if (!sctx_test_5(zorba)) + return 5; + + zorba->shutdown(); + StoreManager::shutdownStore( zstore ); + return 0; +} +/* vim:set et sw=2 ts=2: */ +
-- Mailing list: https://launchpad.net/~zorba-coders Post to : [email protected] Unsubscribe : https://launchpad.net/~zorba-coders More help : https://help.launchpad.net/ListHelp

