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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* This file is part of the libvisio project.
*
* 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 <libvisio/libvisio.h>
#include <libxml/xpath.h>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "xmldrawinggenerator.h"
namespace
{
/// Allows using CPPUNIT_ASSERT_EQUAL() on librevenge::RVNGString instances.
std::ostream &operator <<(std::ostream &s, const librevenge::RVNGString &string)
{
return s << string.cstr();
}
/// Caller must call xmlXPathFreeObject.
xmlXPathObjectPtr getXPathNode(xmlDocPtr doc, const librevenge::RVNGString &xpath)
{
xmlXPathContextPtr xpathContext = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObject = xmlXPathEvalExpression(BAD_CAST(xpath.cstr()), xpathContext);
xmlXPathFreeContext(xpathContext);
return xpathObject;
}
/// Same as the assertXPath(), but don't assert: return the string instead.
librevenge::RVNGString getXPath(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &attribute)
{
xmlXPathObjectPtr xpathobject = getXPathNode(doc, xpath);
xmlNodeSetPtr nodeset = xpathobject->nodesetval;
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': number of nodes is incorrect.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), 1, xmlXPathNodeSetGetLength(nodeset));
if (attribute.empty())
return librevenge::RVNGString();
xmlNodePtr node = nodeset->nodeTab[0];
xmlChar *prop = xmlGetProp(node, BAD_CAST(attribute.cstr()));
librevenge::RVNGString s((const char *)prop);
xmlFree(prop);
xmlXPathFreeObject(xpathobject);
return s;
}
/**
* Assert that xpath exists, and xpath returns exactly one node.
* xpath's attribute's value must equal to the expectedValue value.
*/
void assertXPath(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &attribute, const librevenge::RVNGString &expectedValue)
{
librevenge::RVNGString actualValue = getXPath(doc, xpath, attribute);
librevenge::RVNGString message("Attribute '");
message.append(attribute);
message.append("' of '");
message.append(xpath);
message.append("': incorrect value.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), expectedValue, actualValue);
}
/// Same as the assertXPathContent(), but don't assert: return the string instead.
librevenge::RVNGString getXPathContent(xmlDocPtr doc, const librevenge::RVNGString &xpath)
{
xmlXPathObjectPtr xpathObject = getXPathNode(doc, xpath);
xmlNodeSetPtr nodeset = xpathObject->nodesetval;
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': not found.");
CPPUNIT_ASSERT_MESSAGE(message.cstr(), xmlXPathNodeSetGetLength(nodeset) > 0);
xmlNodePtr node = nodeset->nodeTab[0];
librevenge::RVNGString s(reinterpret_cast<char *>((node->children[0]).content));
xmlXPathFreeObject(xpathObject);
return s;
}
/// Assert that xpath exists, and its content equals to content.
void assertXPathContent(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &content)
{
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': contents of child does not match.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), content, getXPathContent(doc, xpath));
}
/// Paints an XML representation of filename into buffer, then returns the parsed buffer content.
xmlDocPtr parse(const char *filename, xmlBufferPtr buffer)
{
librevenge::RVNGString path(TDOC "/");
path.append(filename);
librevenge::RVNGFileStream input(path.cstr());
CPPUNIT_ASSERT(libvisio::VisioDocument::isSupported(&input));
xmlTextWriterPtr writer = xmlNewTextWriterMemory(buffer, 0);
CPPUNIT_ASSERT(writer);
xmlTextWriterStartDocument(writer, 0, 0, 0);
libvisio::XmlDrawingGenerator painter(writer);
CPPUNIT_ASSERT(libvisio::VisioDocument::parse(&input, &painter));
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
//std::cerr << "XML is '" << (const char *)xmlBufferContent(buffer) << "'" << std::endl;
return xmlParseMemory((const char *)xmlBufferContent(buffer), xmlBufferLength(buffer));
}
}
class ImportTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(ImportTest);
CPPUNIT_TEST(testVsdxMetadataTitle);
CPPUNIT_TEST(testVsdMetadataTitleMs1252);
CPPUNIT_TEST(testVsdMetadataTitleUtf8);
CPPUNIT_TEST_SUITE_END();
void testVsdxMetadataTitle();
void testVsdMetadataTitleMs1252();
void testVsdMetadataTitleUtf8();
xmlBufferPtr m_buffer;
xmlDocPtr m_doc;
public:
ImportTest();
virtual void setUp();
virtual void tearDown();
};
ImportTest::ImportTest()
: m_buffer(0),
m_doc(0)
{
}
void ImportTest::setUp()
{
CPPUNIT_ASSERT(!m_buffer);
m_buffer = xmlBufferCreate();
CPPUNIT_ASSERT(m_buffer);
CPPUNIT_ASSERT(!m_doc);
}
void ImportTest::tearDown()
{
xmlFreeDoc(m_doc);
m_doc = 0;
xmlBufferFree(m_buffer);
m_buffer = 0;
}
void ImportTest::testVsdxMetadataTitle()
{
m_doc = parse("fdo86664.vsdx", m_buffer);
// The setDocumentMetaData() call was missing, so the node did not exist.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle");
assertXPath(m_doc, "/document/setDocumentMetaData", "subject", "mysubject");
assertXPath(m_doc, "/document/setDocumentMetaData", "initial-creator", "vmiklos creator");
// Test <dcterms:created> and <dcterms:modified>.
assertXPath(m_doc, "/document/setDocumentMetaData", "creation-date", "2014-11-24T10:35:17Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "date", "2014-11-24T10:41:22Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "keyword", "mytag");
assertXPath(m_doc, "/document/setDocumentMetaData", "description", "mycomment");
assertXPath(m_doc, "/document/setDocumentMetaData", "creator", "vmiklos modifier");
assertXPathContent(m_doc, "/document/setDocumentMetaData/user-defined[@name='category']", "mycategory");
}
void ImportTest::testVsdMetadataTitleMs1252()
{
m_doc = parse("fdo86729-ms1252.vsd", m_buffer);
// Test windows-1252 -> UTF-8 conversion, provided by ICU.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle\xC3\xA9\xC3\xA1");
assertXPath(m_doc, "/document/setDocumentMetaData", "subject", "mysubject");
assertXPath(m_doc, "/document/setDocumentMetaData", "initial-creator", "vmiklos creator");
// There is only one author/last-modifier field in the file, so make sure creator is the same as initial-creator.
assertXPath(m_doc, "/document/setDocumentMetaData", "creator", "vmiklos creator");
assertXPath(m_doc, "/document/setDocumentMetaData", "keyword", "mytag");
assertXPath(m_doc, "/document/setDocumentMetaData", "description", "mycomment");
}
void ImportTest::testVsdMetadataTitleUtf8()
{
m_doc = parse("fdo86729-utf8.vsd", m_buffer);
// Test the case when the string is UTF-8 encoded already in the file.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle\xC3\xA9\xC3\xA1\xC5\x91\xC5\xB1");
// Test <dcterms:created> and <dcterms:modified>.
assertXPath(m_doc, "/document/setDocumentMetaData", "creation-date", "2014-11-26T09:24:56Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "date", "2014-11-26T09:24:56Z");
}
CPPUNIT_TEST_SUITE_REGISTRATION(ImportTest);
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|