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
|
/*
*
* Licensed to the Apachef Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "unit_test.h"
#include "test_tools.h"
#include "BrokerFixture.h"
#include "qpid/sys/Shlib.h"
#include "qpid/sys/Monitor.h"
#include "qpid/sys/Thread.h"
#include "qpid/sys/Runnable.h"
#include "qpid/client/Message.h"
#include "qpid/framing/reply_exceptions.h"
#include "qpid/client/Connection.h"
#include "qpid/client/SubscriptionManager.h"
#include "qpid/client/LocalQueue.h"
#include "qpid/client/Session.h"
#include "qpid/client/SubscriptionManager.h"
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
#include <vector>
namespace qpid {
namespace tests {
QPID_AUTO_TEST_SUITE(XmlClientSessionTest)
using namespace qpid::client;
using namespace qpid::client::arg;
using namespace qpid::framing;
using namespace qpid;
using qpid::sys::Shlib;
using qpid::sys::Monitor;
using std::string;
using std::cout;
using std::endl;
Shlib shlib(getLibPath("XML_LIB"));
class SubscribedLocalQueue : public LocalQueue {
private:
SubscriptionManager& subscriptions;
public:
SubscribedLocalQueue(SubscriptionManager& subs) : subscriptions(subs) {}
Message get () { return pop(); }
Message get (sys::Duration timeout) { return pop(timeout); }
virtual ~SubscribedLocalQueue() {}
};
struct SimpleListener : public MessageListener
{
Monitor lock;
std::vector<Message> messages;
void received(Message& msg)
{
Monitor::ScopedLock l(lock);
messages.push_back(msg);
lock.notifyAll();
}
void waitFor(const uint n)
{
Monitor::ScopedLock l(lock);
while (messages.size() < n) {
lock.wait();
}
}
};
struct ClientSessionFixture : public SessionFixture
{
void declareSubscribe(const string& q="odd_blue",
const string& dest="xml")
{
session.queueDeclare(queue=q);
session.messageSubscribe(queue=q, destination=dest, acquireMode=1);
session.messageFlow(destination=dest, unit=0, value=0xFFFFFFFF);//messages
session.messageFlow(destination=dest, unit=1, value=0xFFFFFFFF);//bytes
}
};
// ########### START HERE ####################################
QPID_AUTO_TEST_CASE(testXmlBinding) {
ClientSessionFixture f;
SubscriptionManager subscriptions(f.session);
SubscribedLocalQueue localQueue(subscriptions);
f.session.exchangeDeclare(qpid::client::arg::exchange="xml", qpid::client::arg::type="xml");
f.session.queueDeclare(qpid::client::arg::queue="odd_blue");
subscriptions.subscribe(localQueue, "odd_blue");
FieldTable binding;
binding.setString("xquery", "declare variable $color external;"
"(./message/id mod 2 = 1) and ($color = 'blue')");
f.session.exchangeBind(qpid::client::arg::exchange="xml", qpid::client::arg::queue="odd_blue", qpid::client::arg::bindingKey="query_name", qpid::client::arg::arguments=binding);
Message message;
message.getDeliveryProperties().setRoutingKey("query_name");
message.getHeaders().setString("color", "blue");
string m = "<message><id>1</id></message>";
message.setData(m);
f.session.messageTransfer(qpid::client::arg::content=message, qpid::client::arg::destination="xml");
Message m2 = localQueue.get(1*qpid::sys::TIME_SEC);
BOOST_CHECK_EQUAL(m, m2.getData());
}
/**
* Ensure that multiple queues can be bound using the same routing key
*/
QPID_AUTO_TEST_CASE(testXMLBindMultipleQueues) {
ClientSessionFixture f;
f.session.exchangeDeclare(arg::exchange="xml", arg::type="xml");
f.session.queueDeclare(arg::queue="blue", arg::exclusive=true, arg::autoDelete=true);
f.session.queueDeclare(arg::queue="red", arg::exclusive=true, arg::autoDelete=true);
FieldTable blue;
blue.setString("xquery", "./colour = 'blue'");
f.session.exchangeBind(arg::exchange="xml", arg::queue="blue", arg::bindingKey="by-colour", arg::arguments=blue);
FieldTable red;
red.setString("xquery", "./colour = 'red'");
f.session.exchangeBind(arg::exchange="xml", arg::queue="red", arg::bindingKey="by-colour", arg::arguments=red);
Message sent1("<colour>blue</colour>", "by-colour");
f.session.messageTransfer(arg::content=sent1, arg::destination="xml");
Message sent2("<colour>red</colour>", "by-colour");
f.session.messageTransfer(arg::content=sent2, arg::destination="xml");
Message received;
BOOST_CHECK(f.subs.get(received, "blue"));
BOOST_CHECK_EQUAL(sent1.getData(), received.getData());
BOOST_CHECK(f.subs.get(received, "red"));
BOOST_CHECK_EQUAL(sent2.getData(), received.getData());
}
//### Test: Bad XML does not kill the server - and does not even
// raise an exception, the content is not required to be XML.
QPID_AUTO_TEST_CASE(testXMLSendBadXML) {
ClientSessionFixture f;
f.session.exchangeDeclare(arg::exchange="xml", arg::type="xml");
f.session.queueDeclare(arg::queue="blue", arg::exclusive=true, arg::autoDelete=true)\
;
f.session.queueDeclare(arg::queue="red", arg::exclusive=true, arg::autoDelete=true);
FieldTable blue;
blue.setString("xquery", "./colour = 'blue'");
f.session.exchangeBind(arg::exchange="xml", arg::queue="blue", arg::bindingKey="by-c\
olour", arg::arguments=blue);
FieldTable red;
red.setString("xquery", "./colour = 'red'");
f.session.exchangeBind(arg::exchange="xml", arg::queue="red", arg::bindingKey="by-co\
lour", arg::arguments=red);
Message sent1("<>colour>blue</colour>", "by-colour");
f.session.messageTransfer(arg::content=sent1, arg::destination="xml");
BOOST_CHECK_EQUAL(1, 1);
}
//### Test: Bad XQuery does not kill the server, but does raise an exception
QPID_AUTO_TEST_CASE(testXMLBadXQuery) {
ClientSessionFixture f;
f.session.exchangeDeclare(arg::exchange="xml", arg::type="xml");
f.session.queueDeclare(arg::queue="blue", arg::exclusive=true, arg::autoDelete=true)\
;
try {
ScopedSuppressLogging sl; // Supress logging of error messages for expected error.
FieldTable blue;
blue.setString("xquery", "./colour $=! 'blue'");
f.session.exchangeBind(arg::exchange="xml", arg::queue="blue", arg::bindingKey="by-c\
olour", arg::arguments=blue);
}
catch (const InternalErrorException& e) {
return;
}
BOOST_ERROR("A bad XQuery must raise an exception when used in an XML Binding.");
}
//### Test: double, string, and integer field values can all be bound to queries
QPID_AUTO_TEST_CASE(testXmlBindingUntyped) {
ClientSessionFixture f;
SubscriptionManager subscriptions(f.session);
SubscribedLocalQueue localQueue(subscriptions);
f.session.exchangeDeclare(qpid::client::arg::exchange="xml", qpid::client::arg::type="xml");
f.session.queueDeclare(qpid::client::arg::queue="odd_blue");
subscriptions.subscribe(localQueue, "odd_blue");
FieldTable binding;
binding.setString("xquery",
"declare variable $s external;"
"declare variable $i external;"
"declare variable $d external;"
"$s = 'string' and $i = 1 and $d < 1");
f.session.exchangeBind(qpid::client::arg::exchange="xml", qpid::client::arg::queue="odd_blue", qpid::client::arg::bindingKey="query_name", qpid::client::arg::arguments=binding);
Message message;
message.getDeliveryProperties().setRoutingKey("query_name");
message.getHeaders().setString("s", "string");
message.getHeaders().setInt("i", 1);
message.getHeaders().setDouble("d", 0.5);
string m = "<message>Hi, Mom!</message>";
message.setData(m);
f.session.messageTransfer(qpid::client::arg::content=message, qpid::client::arg::destination="xml");
Message m2 = localQueue.get(1*qpid::sys::TIME_SEC);
BOOST_CHECK_EQUAL(m, m2.getData());
}
//### Test: double, string, and integer field values can all be bound to queries
QPID_AUTO_TEST_CASE(testXmlBindingTyped) {
ClientSessionFixture f;
SubscriptionManager subscriptions(f.session);
SubscribedLocalQueue localQueue(subscriptions);
f.session.exchangeDeclare(qpid::client::arg::exchange="xml", qpid::client::arg::type="xml");
f.session.queueDeclare(qpid::client::arg::queue="odd_blue");
subscriptions.subscribe(localQueue, "odd_blue");
FieldTable binding;
binding.setString("xquery",
"declare variable $s as xs:string external;"
"declare variable $i as xs:integer external;"
"declare variable $d external;" // XQilla bug when declaring xs:float, xs:double types? Fine if untyped, acts as float.
"$s = 'string' and $i = 1 and $d < 1");
f.session.exchangeBind(qpid::client::arg::exchange="xml", qpid::client::arg::queue="odd_blue", qpid::client::arg::bindingKey="query_name", qpid::client::arg::arguments=binding);
Message message;
message.getDeliveryProperties().setRoutingKey("query_name");
message.getHeaders().setString("s", "string");
message.getHeaders().setInt("i", 1);
message.getHeaders().setDouble("d", 0.5);
string m = "<message>Hi, Mom!</message>";
message.setData(m);
f.session.messageTransfer(qpid::client::arg::content=message, qpid::client::arg::destination="xml");
Message m2 = localQueue.get(1*qpid::sys::TIME_SEC);
BOOST_CHECK_EQUAL(m, m2.getData());
}
//### Test: Each session can provide its own definition for a query name
//### Test: Bindings persist, surviving broker restart
QPID_AUTO_TEST_SUITE_END()
}} // namespace qpid::tests
|