Happy new year
[metaproxy-moved-to-github.git] / src / test_xmlutil.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include <metaproxy/xmlutil.hpp>
24
25 #define BOOST_AUTO_TEST_MAIN
26 #define BOOST_TEST_DYN_LINK
27 #include <boost/test/auto_unit_test.hpp>
28
29 #include <yaz/zgdu.h>
30 #include <yaz/otherinfo.h>
31 #include <yaz/oid_db.h>
32
33 using namespace boost::unit_test;
34 namespace mp = metaproxy_1;
35 namespace mp_xml = metaproxy_1::xml;
36
37 BOOST_AUTO_TEST_CASE( url_recipe )
38 {
39     try 
40     {
41         const char *xml_text = 
42             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
43             "<pz:record xmlns:pz=\"http://www.indexdata.com/pazpar2/1.0\""
44             " xmlns:tmarc=\"http://www.indexdata.com/turbomarc\">\n"
45             "<pz:metadata type=\"id\">   11224466 </pz:metadata>\n"
46             "<pz:metadata type=\"oclc-number\"/>\n"
47             "<pz:metadata type=\"lccn\">   11224466 </pz:metadata>\n"
48             "<pz:metadata type=\"author\">Jack Collins</pz:metadata>\n"
49             "<pz:metadata type=\"author-title\"/>\n"
50             "<pz:metadata type=\"author-date\"/>\n"
51             "<pz:metadata type=\"date\"/>\n"
52             "<pz:metadata type=\"title\">How to program a computer</pz:metadata>\n"
53             "<pz:metadata type=\"publication-place\">Penguin</pz:metadata>\n"
54             "<pz:metadata type=\"has-fulltext\">no</pz:metadata>\n"
55             "</pz:record>\n";
56         xmlDoc *doc = xmlParseMemory(xml_text, strlen(xml_text));
57         BOOST_CHECK(doc);
58         if (doc)
59         {
60             std::string res;
61
62             res = mp_xml::url_recipe_handle(doc, "abc");
63             BOOST_CHECK(!res.compare("abc"));
64
65             res = mp_xml::url_recipe_handle(doc, "${has-fulltext[no/yes]}");
66             std::cout << "res=" << res << std::endl;
67             BOOST_CHECK(!res.compare("yes"));
68
69             res = mp_xml::url_recipe_handle(doc, "${has-fulltext[no]}");
70             std::cout << "res=" << res << std::endl;
71             BOOST_CHECK(!res.compare(""));
72
73             res = mp_xml::url_recipe_handle(doc, "${has-fulltext[no/]}");
74             std::cout << "res=" << res << std::endl;
75             BOOST_CHECK(!res.compare(""));
76
77             res = mp_xml::url_recipe_handle(doc, "${has-fulltext[n/]}");
78             std::cout << "res=" << res << std::endl;
79             BOOST_CHECK(!res.compare("o"));
80
81             res = mp_xml::url_recipe_handle(doc, "${has-fulltext}");
82             std::cout << "res=" << res << std::endl;
83             BOOST_CHECK(!res.compare("no"));
84
85             res = mp_xml::url_recipe_handle(
86                 doc, "http://sever.com?title=${md-title[\\s+/+/g]}");
87             std::cout << "res=" << res << std::endl;
88             BOOST_CHECK(!res.compare("http://sever.com?title=How+to+program+a+computer"));
89
90             res = mp_xml::url_recipe_handle(doc, "${md-id[2/1]}");
91             std::cout << "res=" << res << std::endl;
92             BOOST_CHECK(!res.compare("   11124466 "));
93
94             res = mp_xml::url_recipe_handle(doc, "${md-id[2/1/g]}");
95             std::cout << "res=" << res << std::endl;
96             BOOST_CHECK(!res.compare("   11114466 "));
97
98
99             xmlFreeDoc(doc);
100         }
101     }
102     catch ( std::runtime_error &e) {
103         std::cout << "std::runtime error: " << e.what() << std::endl;
104         BOOST_CHECK(false);
105     }
106     catch ( ... ) {
107         std::cout << "unknown exception" << std::endl;
108         BOOST_CHECK(false);
109     }
110 }
111
112
113 /*
114  * Local variables:
115  * c-basic-offset: 4
116  * c-file-style: "Stroustrup"
117  * indent-tabs-mode: nil
118  * End:
119  * vim: shiftwidth=4 tabstop=8 expandtab
120  */
121