Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / tests / saxparser_chunk_parsing_inconsistent_state / main.cc
1 /* Copyright (C) 2015  The libxml++ development team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #include <config.h>
19 #include <libxml++/libxml++.h>
20
21 #include <cstdlib>
22 #include <glibmm.h>
23 #include <sstream>
24 #include <stdexcept>
25
26 class MySaxParser : public xmlpp::SaxParser
27 {
28 protected:
29   void on_start_document() override
30   {
31     throw std::runtime_error("some custom runtime exception");
32   }
33   void on_error(const Glib::ustring& /* text */) override
34   {
35     throw std::runtime_error("on_error() called");
36   }
37 };
38
39 int main()
40 {
41   Glib::init();
42
43   {
44     MySaxParser parser;
45
46     bool exceptionThrown = false;
47     try
48     {
49       parser.parse_chunk("<?");
50       parser.finish_chunk_parsing();
51     }
52 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
53     catch(const std::runtime_error& e)
54 #else
55     catch(const xmlpp::exception& e)
56 #endif
57     {
58       exceptionThrown = true;
59       g_assert_cmpstr(e.what(), ==, "on_error() called");
60     }
61     g_assert_true(exceptionThrown);
62
63     // Try parsing a stream now.
64     exceptionThrown = false;
65     try
66     {
67       std::stringstream ss("<root></root>");
68       parser.parse_stream(ss);
69     }
70     catch(const xmlpp::parse_error& e)
71     {
72       // An "Attempt to start a second parse while a parse is in progress." parse
73       // error should not have been thrown.
74       g_assert_not_reached();
75     }
76 #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR
77     catch(const std::runtime_error& e)
78 #else
79     catch(const xmlpp::exception& e)
80 #endif
81     {
82       exceptionThrown = true;
83       g_assert_cmpstr(e.what(), ==, "some custom runtime exception");
84     }
85     g_assert_true(exceptionThrown);
86   }
87
88   return EXIT_SUCCESS;
89 }