summaryrefslogtreecommitdiffstats
path: root/src/orcus_test_xml_mapped.cpp
blob: 16d19ec4a3fe6597d27ee1ee41b3b99c294a398f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <orcus/orcus_xml.hpp>
#include <orcus/sax_ns_parser.hpp>
#include <orcus/xml_namespace.hpp>
#include <orcus/stream.hpp>
#include <orcus/dom_tree.hpp>

#include <orcus/spreadsheet/factory.hpp>
#include <orcus/spreadsheet/document.hpp>

#include "orcus_test_global.hpp"

#include <cstdlib>
#include <cassert>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

#include "filesystem_env.hpp"

using namespace orcus;

const fs::path test_base_dir(SRCDIR"/test/xml-mapped");

namespace {

void test_mapped_xml_import()
{
    ORCUS_TEST_FUNC_SCOPE;

    struct test_case
    {
        const char* base_dir;
        bool output_equals_input;
    };

    const std::vector<test_case> tests =
    {
        { SRCDIR"/test/xml-mapped/attribute-basic", true },
        { SRCDIR"/test/xml-mapped/attribute-namespace", true },
        { SRCDIR"/test/xml-mapped/attribute-namespace-2", false },
        { SRCDIR"/test/xml-mapped/attribute-range-self-close", true },
        { SRCDIR"/test/xml-mapped/attribute-single-element", true },
        { SRCDIR"/test/xml-mapped/attribute-single-element-2", true },
        { SRCDIR"/test/xml-mapped/content-basic", true },
        { SRCDIR"/test/xml-mapped/content-namespace", false },
        { SRCDIR"/test/xml-mapped/content-namespace-2", true },
        { SRCDIR"/test/xml-mapped/content-namespace-3", false },
        { SRCDIR"/test/xml-mapped/custom-labels", true },
        { SRCDIR"/test/xml-mapped/custom-labels-2", true },
        { SRCDIR"/test/xml-mapped/fuel-economy", true },
        { SRCDIR"/test/xml-mapped/nested-repeats", false },
        { SRCDIR"/test/xml-mapped/nested-repeats-2", false },
        { SRCDIR"/test/xml-mapped/nested-repeats-3", false },
        { SRCDIR"/test/xml-mapped/nested-repeats-4", false },
    };

    auto dump_xml_structure = [](const file_content& content, xmlns_context& cxt)
    {
        dom::document_tree tree(cxt);
        tree.load(content.str());
        std::ostringstream os;
        tree.dump_compact(os);
        return os.str();
    };

    const fs::path temp_output_xml = fs::temp_directory_path() / "orcus-output.xml";

    std::string strm;

    for (const test_case& tc : tests)
    {
        fs::path base_dir(tc.base_dir);
        fs::path data_file = base_dir / "input.xml";
        fs::path map_file = base_dir / "map.xml";
        fs::path check_file = base_dir / "check.txt";

        // Load the data file content.
        std::cout << "reading " << data_file.string() << std::endl;
        file_content content(data_file.string().data());
        std::string data_strm{content.str()};

        spreadsheet::range_size_t ss{1048576, 16384};
        spreadsheet::document doc{ss};
        spreadsheet::import_factory import_fact(doc);
        spreadsheet::export_factory export_fact(doc);

        xmlns_repository repo;
        xmlns_context cxt = repo.create_context();

        // Parse the map file to define map rules, and parse the data file.
        orcus_xml app(repo, &import_fact, &export_fact);
        file_content map_content(map_file.string().data());
        app.read_map_definition(map_content.str());
        app.read_stream(data_strm);

        // Zero the source data stream to make sure it's completely erased off
        // memory.
        std::for_each(data_strm.begin(), data_strm.end(), [](char& c) { c = '\0'; });
        assert(data_strm[0] == '\0');
        assert(data_strm[data_strm.size()-1] == '\0');

        // Check the content of the document against static check file.
        std::ostringstream os;
        doc.dump_check(os);
        std::string loaded = os.str();
        content.load(check_file.string().data());
        strm = content.str();

        assert(!loaded.empty());
        assert(!strm.empty());

        std::string_view p1(loaded.data(), loaded.size()), p2(strm.data(), strm.size());

        p1 = trim(p1);
        p2 = trim(p2);
        assert(p1 == p2);

        if (tc.output_equals_input)
        {
            // Output to xml file with the linked values coming from the document.
            std::cout << "writing to " << temp_output_xml << std::endl;
            {
                // Create a duplicate source XML stream.
                content.load(data_file.string());
                std::string data_strm_dup{content.str()};
                std::ofstream file(temp_output_xml.string(), std::ios::out | std::ios::trunc);
                assert(file);
                app.write(data_strm_dup, file);
            }

            // Compare the logical xml content of the output xml with the
            // input one. They should be identical.

            // Hold the stream content in memory while the namespace context is being used.
            file_content strm_data_file(data_file.string());
            file_content strm_out_file(temp_output_xml.string());
            std::string dump_input = dump_xml_structure(strm_data_file, cxt);
            std::string dump_output = dump_xml_structure(strm_out_file, cxt);
            assert(!dump_input.empty() && !dump_output.empty());

            std::cout << dump_input << std::endl;
            std::cout << "--" << std::endl;
            std::cout << dump_output << std::endl;
            assert(dump_input == dump_output);
        }
    }

    // Delete the temporary xml output.
    fs::remove(temp_output_xml);
}

void test_mapped_xml_import_no_map_definition()
{
    ORCUS_TEST_FUNC_SCOPE;

    const std::vector<fs::path> tests = {
        test_base_dir / "attribute-basic",
        test_base_dir / "attribute-namespace",
        test_base_dir / "attribute-namespace-2",
        test_base_dir / "attribute-range-self-close",
        test_base_dir / "content-basic",
        test_base_dir / "content-one-column",
        test_base_dir / "content-namespace",
        test_base_dir / "content-namespace-2",
        test_base_dir / "content-namespace-3",
        test_base_dir / "fuel-economy",
        test_base_dir / "nested-repeats",
        test_base_dir / "nested-repeats-2",
        test_base_dir / "nested-repeats-3",
        test_base_dir / "nested-repeats-4",
    };

    for (const fs::path& base_dir : tests)
    {
        fs::path input_file = base_dir / "input.xml";
        fs::path check_file = base_dir / "check-nomap.txt";

        std::cout << "reading " << input_file.string() << std::endl;

        file_content content(input_file.string().data());
        file_content expected(check_file.string().data());

        xmlns_repository repo;

        {
            // Automatically detect map definition without a map file.
            spreadsheet::range_size_t ss{1048576, 16384};
            spreadsheet::document doc{ss};
            spreadsheet::import_factory import_fact(doc);

            orcus_xml app(repo, &import_fact, nullptr);

            app.detect_map_definition(content.str());
            app.read_stream(content.str());

            test::verify_content(__FILE__, __LINE__, doc, expected.str());
        }

        {
            // Generate a map file and use it to import the XML document.
            spreadsheet::range_size_t ss{1048576, 16384};
            spreadsheet::document doc{ss};
            spreadsheet::import_factory import_fact(doc);

            orcus_xml app(repo, &import_fact, nullptr);

            std::ostringstream os;
            app.write_map_definition(content.str(), os);
            std::string map_def = os.str();
            app.read_map_definition(map_def);
            app.read_stream(content.str());

            test::verify_content(__FILE__, __LINE__, doc, expected.str());
        }
    }
}

void test_invalid_map_definition()
{
    ORCUS_TEST_FUNC_SCOPE;

    fs::path invalids_dir = test_base_dir / "invalids" / "map-defs";

    const std::vector<fs::path> tests = {
        invalids_dir / "not-xml.xml",
        invalids_dir / "non-leaf-element-linked.xml",
    };

    xmlns_repository repo;

    spreadsheet::range_size_t ss{1048576, 16384};
    spreadsheet::document doc{ss};
    spreadsheet::import_factory import_fact(doc);
    orcus_xml app(repo, &import_fact, nullptr);

    for (const fs::path& test : tests)
    {
        std::cout << test.string() << std::endl;
        file_content content(test.string().data());
        doc.clear();

        try
        {
            app.read_map_definition(content.str());
            assert(!"We were expecting an exception, but didn't get one.");
        }
        catch (const invalid_map_error& e)
        {
            // Success!
            std::cout << std::endl
                << "Exception received as expected, with the following message:" << std::endl
                << std::endl
                << test::prefix_multiline_string(e.what(), "  ") << std::endl
                << std::endl;
        }
        catch (const std::exception& e)
        {
            std::cerr << e.what() << std::endl;
            assert(!"Wrong exception thrown.");
        }
    }
}

void test_encoding()
{
    ORCUS_TEST_FUNC_SCOPE;

    const fs::path test_dir = test_base_dir / "encoding";

    struct test_case
    {
        const fs::path path;
        character_set_t charset;
    };

    const test_case tests[] = {
        { test_dir / "utf-8.xml", character_set_t::utf_8 },
        { test_dir / "gbk.xml", character_set_t::gbk },
        { test_dir / "euc-jp.xml", character_set_t::euc_jp },
    };

    for (const auto& test : tests)
    {
        std::cout << "reading " << test.path.string() << std::endl;

        file_content content(test.path.string());

        xmlns_repository repo;

        spreadsheet::range_size_t ss{1048576, 16384};
        spreadsheet::document doc{ss};
        spreadsheet::import_factory import_fact(doc);
        orcus_xml app(repo, &import_fact, nullptr);

        app.read_stream(content.str());

        assert(import_fact.get_character_set() == test.charset);
    }
}

} // anonymous namespace

int main()
{
    test_mapped_xml_import();
    test_mapped_xml_import_no_map_definition();
    test_invalid_map_definition();
    test_encoding();

    return EXIT_SUCCESS;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */