forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserWriterTest.cpp
More file actions
executable file
·171 lines (136 loc) · 4.21 KB
/
ParserWriterTest.cpp
File metadata and controls
executable file
·171 lines (136 loc) · 4.21 KB
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
//
// ParserWriterTest.cpp
//
// $Id: //poco/1.4/XML/testsuite/src/ParserWriterTest.cpp#1 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "ParserWriterTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/DOMWriter.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/AutoPtr.h"
#include "Poco/SAX/InputSource.h"
#include "Poco/XML/XMLWriter.h"
#include <sstream>
using Poco::XML::DOMParser;
using Poco::XML::DOMWriter;
using Poco::XML::XMLReader;
using Poco::XML::XMLWriter;
using Poco::XML::Document;
using Poco::XML::AutoPtr;
using Poco::XML::InputSource;
ParserWriterTest::ParserWriterTest(const std::string& name): CppUnit::TestCase(name)
{
}
ParserWriterTest::~ParserWriterTest()
{
}
void ParserWriterTest::testParseWriteXHTML()
{
std::ostringstream ostr;
DOMParser parser;
parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, false);
DOMWriter writer;
AutoPtr<Document> pDoc = parser.parseString(XHTML);
writer.writeNode(ostr, pDoc);
std::string xml = ostr.str();
assert (xml == XHTML);
}
void ParserWriterTest::testParseWriteXHTML2()
{
std::ostringstream ostr;
DOMParser parser;
parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true);
DOMWriter writer;
AutoPtr<Document> pDoc = parser.parseString(XHTML2);
writer.writeNode(ostr, pDoc);
std::string xml = ostr.str();
assert (xml == XHTML2);
}
void ParserWriterTest::testParseWriteSimple()
{
static const std::string simple =
"<config>\n"
"\t<prop1>value1</prop1>\n"
"\t<prop2>value2</prop2>\n"
"</config>\n";
std::istringstream istr(simple);
std::ostringstream ostr;
DOMParser parser;
parser.setFeature(DOMParser::FEATURE_FILTER_WHITESPACE, true);
parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, false);
DOMWriter writer;
writer.setNewLine("\n");
writer.setOptions(XMLWriter::PRETTY_PRINT);
InputSource source(istr);
AutoPtr<Document> pDoc = parser.parse(&source);
writer.writeNode(ostr, pDoc);
unsigned int numChildren = 0;
Poco::XML::Node* child = pDoc->documentElement()->firstChild();
while (child) {
numChildren++;
child = child->nextSibling();
}
assert (numChildren == 2);
std::string xml = ostr.str();
assert (xml == simple);
}
void ParserWriterTest::setUp()
{
}
void ParserWriterTest::tearDown()
{
}
CppUnit::Test* ParserWriterTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ParserWriterTest");
CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteXHTML);
CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteXHTML2);
CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteSimple);
return pSuite;
}
const std::string ParserWriterTest::XHTML =
"<!--\n"
"\tThis is a comment.\n"
"-->"
"<ns1:html xml:lang=\"en\" xmlns:ns1=\"http://www.w3.org/1999/xhtml\">\n"
"\t<ns1:head>\n"
"\t\t<ns1:link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
"\t\t<?xml-stylesheet href=\"styles.css\" type=\"text/css\"?>\n"
"\t\t<ns1:title>A XHTML Example</ns1:title>\n"
"\t</ns1:head>\n"
"\t<ns1:body>\n"
"\t\t<ns1:h1>XHTML Example</ns1:h1>\n"
"\t\t<ns1:p>This is a XHTML example page.</ns1:p>\n"
"\t\t<ns1:img alt=\"Example Picture\" border=\"0\" height=\"192\" src=\"example.gif\" width=\"256\"/>\n"
"\t\t<![CDATA[\n"
"\t\tThe following <tag attr=\"value\">is inside a CDATA section</tag>.\n"
"\t\t]]>\n"
"\t</ns1:body>\n"
"</ns1:html>";
const std::string ParserWriterTest::XHTML2 =
"<!--\n"
"\tThis is a comment.\n"
"-->"
"<xns:html xml:lang=\"en\" xmlns:xns=\"http://www.w3.org/1999/xhtml\">\n"
"\t<xns:head>\n"
"\t\t<xns:link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
"\t\t<?xml-stylesheet href=\"styles.css\" type=\"text/css\"?>\n"
"\t\t<xns:title>A XHTML Example</xns:title>\n"
"\t</xns:head>\n"
"\t<xns:body>\n"
"\t\t<xns:h1>XHTML Example</xns:h1>\n"
"\t\t<xns:p>This is a XHTML example page.</xns:p>\n"
"\t\t<xns:img alt=\"Example Picture\" border=\"0\" height=\"192\" src=\"example.gif\" width=\"256\"/>\n"
"\t\t<![CDATA[\n"
"\t\tThe following <tag attr=\"value\">is inside a CDATA section</tag>.\n"
"\t\t]]>\n"
"\t</xns:body>\n"
"</xns:html>";