1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // This file contains the unit tests for the ExpatHandlerNs class.
27 
28 #include "kml/base/expat_handler_ns.h"
29 #include "boost/scoped_ptr.hpp"
30 #include "kml/base/attributes.h"
31 #include "kml/base/expat_parser.h"
32 #include "kml/base/xmlns.h"
33 #include "gtest/gtest.h"
34 
35 using kmlbase::Attributes;
36 using kmlbase::Xmlns;
37 
38 namespace kmlbase {
39 
40 class ExpatHandlerNsTest : public testing::Test {
41 };
42 
43 // Note: no namespace can use the kExpatNsSeparator (see base/expat_parser.h).
44 static const char kDefaultNamespace[] = "this:is:the:default:namespace";
45 static const char kDefaultPrefix[] = "default";
46 static const char kOtherNamespace[] = "this:is:another:namespace";
47 static const char kOtherPrefix[] = "other";
48 
49 typedef std::vector<string> StringVector;
50 
51 class TestHandler : public ExpatHandler {
52  public:
TestHandler(StringVector * log)53   TestHandler(StringVector* log) : log_(log) {}
StartElement(const string & name,const StringVector & atts)54   virtual void StartElement(const string& name,
55                             const StringVector& atts) {
56     // TODO: handle attributes
57     log_->push_back(name);
58   }
EndElement(const string & name)59   virtual void EndElement(const string& name) {
60     log_->push_back(name);
61   }
CharData(const string & s)62   virtual void CharData(const string& s) {
63     // Nothing namespace/prefix related here.  Just ignore.
64   }
65 
66  private:
67   StringVector* log_;
68 };
69 
70 // This verifies ExpatHandlerNs's StartElement() and EndElement() on XML with
71 // the default namespace.
TEST_F(ExpatHandlerNsTest,TestExpatParserDefault)72 TEST_F(ExpatHandlerNsTest, TestExpatParserDefault) {
73   Attributes attributes;
74   attributes.SetString("xmlns", kDefaultNamespace);
75   boost::scoped_ptr<Xmlns> xmlns(Xmlns::Create(attributes));
76   std::vector<string> log;
77   TestHandler test_handler(&log);
78   ExpatHandlerNs expat_handler_ns(&test_handler, xmlns.get());
79   const string kXml(
80       string("<hi xmlns=\"") + kDefaultNamespace + "\"/>");
81   string errors;
82   ASSERT_TRUE(ExpatParser::ParseString(kXml, &expat_handler_ns, &errors, true));
83   ASSERT_EQ(static_cast<size_t>(2), log.size());
84   ASSERT_EQ(string("hi"), log[0]);
85   ASSERT_EQ(string("hi"), log[1]);
86 }
87 
88 // This verifies ExpatHandlerNs's StartElement() and EndElement() on XML with
89 // no default namespace: all elements are prefixed to a namespace mapping
90 // known to the TestHandler.
TEST_F(ExpatHandlerNsTest,TestExpatParserPrefixed)91 TEST_F(ExpatHandlerNsTest, TestExpatParserPrefixed) {
92   Attributes attributes;
93   attributes.SetString(
94       string("xmlns:") + kDefaultPrefix, kDefaultNamespace);
95   boost::scoped_ptr<Xmlns> xmlns(Xmlns::Create(attributes));
96   std::vector<string> log;
97   TestHandler test_handler(&log);
98   ExpatHandlerNs expat_handler_ns(&test_handler, xmlns.get());
99   const string kTag("hi");
100   const string kXml(
101       string("<") + kDefaultPrefix + ":" + kTag + " " +
102                   "xmlns:" + kDefaultPrefix + "=\"" + kDefaultNamespace +
103                    "\"" + "/>");
104   string errors;
105   ASSERT_TRUE(ExpatParser::ParseString(kXml, &expat_handler_ns, &errors, true));
106   ASSERT_EQ(static_cast<size_t>(2), log.size());
107   ASSERT_EQ(string(kDefaultPrefix) + ":" + kTag, log[0]);
108   ASSERT_EQ(string(kDefaultPrefix) + ":" + kTag, log[1]);
109 }
110 
111 // This verifies ExpatHandlerNs's StartElement() and EndElement() on XML with
112 // elements in the default namespace and elements in another namespace.
TEST_F(ExpatHandlerNsTest,TestExpatParserDefaultAndPrefixed)113 TEST_F(ExpatHandlerNsTest, TestExpatParserDefaultAndPrefixed) {
114   Attributes attributes;
115   attributes.SetString("xmlns", kDefaultNamespace);
116   attributes.SetString(
117       string("xmlns:") + kOtherPrefix, kOtherNamespace);
118   boost::scoped_ptr<Xmlns> xmlns(Xmlns::Create(attributes));
119   std::vector<string> log;
120   TestHandler test_handler(&log);
121   ExpatHandlerNs expat_handler_ns(&test_handler, xmlns.get());
122   const string kHi("hi");
123   const string kThere("there");
124   const string kXml(
125       string("<") + kHi + " " +
126                   "xmlns=\"" + kDefaultNamespace + "\" " +
127                   "xmlns:" + kOtherPrefix + "=\"" + kOtherNamespace + "\">" +
128                   "<" + kOtherPrefix + ":" + kThere + "/>" +
129                   "</" + kHi + ">");
130   string errors;
131   ASSERT_TRUE(ExpatParser::ParseString(kXml, &expat_handler_ns, &errors, true));
132   ASSERT_EQ(static_cast<size_t>(4), log.size());
133   ASSERT_EQ(kHi, log[0]);
134   ASSERT_EQ(string(kOtherPrefix) + ":" + kThere, log[1]);
135   ASSERT_EQ(string(kOtherPrefix) + ":" + kThere, log[2]);
136   ASSERT_EQ(kHi, log[3]);
137 }
138 
139 }  // end namespace kmlbase
140