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
|
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Elements/Stanza.h>
#include <Swiften/Parser/GenericPayloadParser.h>
#include <Swiften/Parser/PayloadParserFactory.h>
#include <Swiften/Parser/PayloadParserFactoryCollection.h>
#include <Swiften/Parser/StanzaParser.h>
using namespace Swift;
class StanzaParserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(StanzaParserTest);
CPPUNIT_TEST(testHandleEndElement_OnePayload);
CPPUNIT_TEST(testHandleEndElement_MultiplePayloads);
CPPUNIT_TEST(testHandleEndElement_StrayCharacterData);
CPPUNIT_TEST(testHandleEndElement_UnknownPayload);
CPPUNIT_TEST(testHandleParse_BasicAttributes);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
factoryCollection_ = new PayloadParserFactoryCollection();
factoryCollection_->addFactory(&factory1_);
factoryCollection_->addFactory(&factory2_);
}
void tearDown() {
delete factoryCollection_;
}
void testHandleEndElement_OnePayload() {
MyStanzaParser testling(factoryCollection_);
AttributeMap attributes;
attributes.addAttribute("foo", "", "fum");
attributes.addAttribute("bar", "", "baz");
testling.handleStartElement("mystanza", "", attributes);
testling.handleStartElement("mypayload1", "", attributes);
testling.handleStartElement("child", "", attributes);
testling.handleEndElement("child", "");
testling.handleEndElement("mypayload1", "");
testling.handleEndElement("mystanza", "");
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload1>());
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload1>()->hasChild);
}
void testHandleEndElement_MultiplePayloads() {
MyStanzaParser testling(factoryCollection_);
AttributeMap attributes;
testling.handleStartElement("mystanza", "", attributes);
testling.handleStartElement("mypayload1", "", attributes);
testling.handleEndElement("mypayload1", "");
testling.handleStartElement("mypayload2", "", attributes);
testling.handleEndElement("mypayload2", "");
testling.handleEndElement("mystanza", "");
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload1>());
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload2>());
}
void testHandleEndElement_StrayCharacterData() {
MyStanzaParser testling(factoryCollection_);
AttributeMap attributes;
testling.handleStartElement("mystanza", "", attributes);
testling.handleStartElement("mypayload1", "", attributes);
testling.handleEndElement("mypayload1", "");
testling.handleCharacterData("bla");
testling.handleStartElement("mypayload2", "", attributes);
testling.handleEndElement("mypayload2", "");
testling.handleEndElement("mystanza", "");
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload1>());
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload2>());
}
void testHandleEndElement_UnknownPayload() {
MyStanzaParser testling(factoryCollection_);
AttributeMap attributes;
testling.handleStartElement("mystanza", "", attributes);
testling.handleStartElement("mypayload1", "", attributes);
testling.handleEndElement("mypayload1", "");
testling.handleStartElement("unknown-payload", "", attributes);
testling.handleStartElement("unknown-payload-child", "", attributes);
testling.handleEndElement("unknown-payload-child", "");
testling.handleEndElement("unknown-payload", "");
testling.handleStartElement("mypayload2", "", attributes);
testling.handleEndElement("mypayload2", "");
testling.handleEndElement("mystanza", "");
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload1>());
CPPUNIT_ASSERT(testling.getStanza()->getPayload<MyPayload2>());
}
void testHandleParse_BasicAttributes() {
MyStanzaParser testling(factoryCollection_);
AttributeMap attributes;
attributes.addAttribute("to", "", "foo@example.com/blo");
attributes.addAttribute("from", "", "bar@example.com/baz");
attributes.addAttribute("id", "", "id-123");
testling.handleStartElement("mystanza", "", attributes);
testling.handleEndElement("mypayload1", "");
CPPUNIT_ASSERT_EQUAL(JID("foo@example.com/blo"), testling.getStanza()->getTo());
CPPUNIT_ASSERT_EQUAL(JID("bar@example.com/baz"), testling.getStanza()->getFrom());
CPPUNIT_ASSERT_EQUAL(std::string("id-123"), testling.getStanza()->getID());
}
private:
class MyPayload1 : public Payload
{
public:
MyPayload1() : hasChild(false) {}
bool hasChild;
};
class MyPayload1Parser : public GenericPayloadParser<MyPayload1>
{
public:
MyPayload1Parser() {}
virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap&) {
if (element != "mypayload1") {
getPayloadInternal()->hasChild = true;
}
}
virtual void handleEndElement(const std::string&, const std::string&) {}
virtual void handleCharacterData(const std::string&) {}
};
class MyPayload1ParserFactory : public PayloadParserFactory
{
public:
MyPayload1ParserFactory() {}
PayloadParser* createPayloadParser() { return new MyPayload1Parser(); }
bool canParse(const std::string& element, const std::string&, const AttributeMap&) const {
return element == "mypayload1";
}
};
class MyPayload2 : public Payload
{
public:
MyPayload2() {}
};
class MyPayload2Parser : public GenericPayloadParser<MyPayload2>
{
public:
MyPayload2Parser() {}
virtual void handleStartElement(const std::string&, const std::string&, const AttributeMap&) {}
virtual void handleEndElement(const std::string&, const std::string&) {}
virtual void handleCharacterData(const std::string&) {}
};
class MyPayload2ParserFactory : public PayloadParserFactory
{
public:
MyPayload2ParserFactory() {}
PayloadParser* createPayloadParser() { return new MyPayload2Parser(); }
bool canParse(const std::string& element, const std::string&, const AttributeMap&) const {
return element == "mypayload2";
}
};
class MyStanza : public Stanza
{
public:
MyStanza() {}
};
class MyStanzaParser : public StanzaParser
{
public:
MyStanzaParser(PayloadParserFactoryCollection* collection) : StanzaParser(collection)
{
stanza_ = std::make_shared<MyStanza>();
}
virtual std::shared_ptr<ToplevelElement> getElement() const {
return stanza_;
}
private:
std::shared_ptr<MyStanza> stanza_;
};
MyPayload1ParserFactory factory1_;
MyPayload2ParserFactory factory2_;
PayloadParserFactoryCollection* factoryCollection_;
};
CPPUNIT_TEST_SUITE_REGISTRATION(StanzaParserTest);
|