| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "proton/message.hpp" |
| 21 |
#include "proton/scalar.hpp" |
| 22 |
#include "test_bits.hpp" |
| 23 |
#include <string> |
| 24 |
#include <fstream> |
| 25 |
#include <streambuf> |
| 26 |
#include <iosfwd> |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
using namespace std; |
| 31 |
using namespace proton; |
| 32 |
|
| 33 |
#define CHECK_STR(ATTR) \ |
| 34 |
m.ATTR(#ATTR); \ |
| 35 |
ASSERT_EQUAL(std::string(#ATTR), m.ATTR()) |
| 36 |
|
| 37 |
#define CHECK_MESSAGE_ID(ATTR) \ |
| 38 |
m.ATTR(#ATTR); \ |
| 39 |
ASSERT_EQUAL(scalar(#ATTR), m.ATTR()) |
| 40 |
|
| 41 |
void test_message_defaults() { |
| 42 |
message m; |
| 43 |
ASSERT(m.body().empty()); |
| 44 |
ASSERT(m.id().empty()); |
| 45 |
ASSERT(m.user().empty()); |
| 46 |
ASSERT(m.to().empty()); |
| 47 |
ASSERT(m.subject().empty()); |
| 48 |
ASSERT(m.reply_to().empty()); |
| 49 |
ASSERT(m.correlation_id().empty()); |
| 50 |
ASSERT(m.content_type().empty()); |
| 51 |
ASSERT(m.content_encoding().empty()); |
| 52 |
ASSERT(m.group_id().empty()); |
| 53 |
ASSERT(m.reply_to_group_id().empty()); |
| 54 |
ASSERT_EQUAL(0, m.expiry_time().milliseconds()); |
| 55 |
ASSERT_EQUAL(0, m.creation_time().milliseconds()); |
| 56 |
|
| 57 |
ASSERT_EQUAL(false, m.inferred()); |
| 58 |
ASSERT_EQUAL(false, m.durable()); |
| 59 |
ASSERT_EQUAL(0, m.ttl().milliseconds()); |
| 60 |
ASSERT_EQUAL(message::default_priority, m.priority()); |
| 61 |
ASSERT_EQUAL(false, m.first_acquirer()); |
| 62 |
ASSERT_EQUAL(0u, m.delivery_count()); |
| 63 |
} |
| 64 |
|
| 65 |
void test_message_properties() { |
| 66 |
message m("hello"); |
| 67 |
std::string s = get<std::string>(m.body()); |
| 68 |
ASSERT_EQUAL("hello", s); |
| 69 |
|
| 70 |
CHECK_MESSAGE_ID(id); |
| 71 |
CHECK_STR(user); |
| 72 |
CHECK_STR(to); |
| 73 |
CHECK_STR(subject); |
| 74 |
CHECK_STR(reply_to); |
| 75 |
CHECK_MESSAGE_ID(correlation_id); |
| 76 |
CHECK_STR(content_type); |
| 77 |
CHECK_STR(content_encoding); |
| 78 |
CHECK_STR(group_id); |
| 79 |
CHECK_STR(reply_to_group_id); |
| 80 |
m.expiry_time(timestamp(42)); |
| 81 |
ASSERT_EQUAL(m.expiry_time().milliseconds(), 42); |
| 82 |
m.creation_time(timestamp(4242)); |
| 83 |
ASSERT_EQUAL(m.creation_time().milliseconds(), 4242); |
| 84 |
m.ttl(duration(30)); |
| 85 |
ASSERT_EQUAL(m.ttl().milliseconds(), 30); |
| 86 |
m.priority(3); |
| 87 |
ASSERT_EQUAL(m.priority(), 3); |
| 88 |
|
| 89 |
message m2(m); |
| 90 |
ASSERT_EQUAL("hello", get<std::string>(m2.body())); |
| 91 |
ASSERT_EQUAL(message_id("id"), m2.id()); |
| 92 |
ASSERT_EQUAL("user", m2.user()); |
| 93 |
ASSERT_EQUAL("to", m2.to()); |
| 94 |
ASSERT_EQUAL("subject", m2.subject()); |
| 95 |
ASSERT_EQUAL("reply_to", m2.reply_to()); |
| 96 |
ASSERT_EQUAL(message_id("correlation_id"), m2.correlation_id()); |
| 97 |
ASSERT_EQUAL("content_type", m2.content_type()); |
| 98 |
ASSERT_EQUAL("content_encoding", m2.content_encoding()); |
| 99 |
ASSERT_EQUAL("group_id", m2.group_id()); |
| 100 |
ASSERT_EQUAL("reply_to_group_id", m2.reply_to_group_id()); |
| 101 |
ASSERT_EQUAL(42, m2.expiry_time().milliseconds()); |
| 102 |
ASSERT_EQUAL(4242, m.creation_time().milliseconds()); |
| 103 |
|
| 104 |
m2 = m; |
| 105 |
ASSERT_EQUAL("hello", get<std::string>(m2.body())); |
| 106 |
ASSERT_EQUAL(message_id("id"), m2.id()); |
| 107 |
ASSERT_EQUAL("user", m2.user()); |
| 108 |
ASSERT_EQUAL("to", m2.to()); |
| 109 |
ASSERT_EQUAL("subject", m2.subject()); |
| 110 |
ASSERT_EQUAL("reply_to", m2.reply_to()); |
| 111 |
ASSERT_EQUAL(message_id("correlation_id"), m2.correlation_id()); |
| 112 |
ASSERT_EQUAL("content_type", m2.content_type()); |
| 113 |
ASSERT_EQUAL("content_encoding", m2.content_encoding()); |
| 114 |
ASSERT_EQUAL("group_id", m2.group_id()); |
| 115 |
ASSERT_EQUAL("reply_to_group_id", m2.reply_to_group_id()); |
| 116 |
ASSERT_EQUAL(42, m2.expiry_time().milliseconds()); |
| 117 |
ASSERT_EQUAL(4242, m.creation_time().milliseconds()); |
| 118 |
} |
| 119 |
|
| 120 |
void test_message_body() { |
| 121 |
std::string s("hello"); |
| 122 |
message m1(s.c_str()); |
| 123 |
ASSERT_EQUAL(s, get<std::string>(m1.body())); |
| 124 |
message m2(s); |
| 125 |
ASSERT_EQUAL(s, coerce<std::string>(m2.body())); |
| 126 |
message m3; |
| 127 |
m3.body(s); |
| 128 |
ASSERT_EQUAL(s, coerce<std::string>(m3.body())); |
| 129 |
ASSERT_EQUAL(5, coerce<int64_t>(message(5).body())); |
| 130 |
ASSERT_EQUAL(3.1, coerce<double>(message(3.1).body())); |
| 131 |
} |
| 132 |
|
| 133 |
void test_message_maps() { |
| 134 |
message m; |
| 135 |
|
| 136 |
ASSERT(m.properties().empty()); |
| 137 |
ASSERT(m.message_annotations().empty()); |
| 138 |
ASSERT(m.delivery_annotations().empty()); |
| 139 |
|
| 140 |
m.properties().put("foo", 12); |
| 141 |
m.delivery_annotations().put("bar", "xyz"); |
| 142 |
m.message_annotations().put(23, int8_t(42)); |
| 143 |
|
| 144 |
ASSERT_EQUAL(m.properties().get("foo"), scalar(12)); |
| 145 |
ASSERT_EQUAL(m.delivery_annotations().get("bar"), scalar("xyz")); |
| 146 |
|
| 147 |
ASSERT_EQUAL(m.message_annotations().get(23), scalar(int8_t(42))); |
| 148 |
ASSERT_EQUAL(proton::get<int8_t>(m.message_annotations().get(23)), 42); |
| 149 |
ASSERT_EQUAL(m.message_annotations().get(23).get<int8_t>(), 42); |
| 150 |
|
| 151 |
message m2(m); |
| 152 |
|
| 153 |
ASSERT_EQUAL(m.properties().get("foo"), scalar(12)); // Decoding shouldn't change it |
| 154 |
|
| 155 |
ASSERT_EQUAL(m2.properties().get("foo"), scalar(12)); |
| 156 |
ASSERT_EQUAL(m2.delivery_annotations().get("bar"), scalar("xyz")); |
| 157 |
ASSERT_EQUAL(m2.message_annotations().get(23), scalar(int8_t(42))); |
| 158 |
|
| 159 |
m.properties().put("foo","newfoo"); |
| 160 |
m.delivery_annotations().put(24, 1000); |
| 161 |
m.message_annotations().erase(23); |
| 162 |
|
| 163 |
message m3 = m; |
| 164 |
size_t size = m3.properties().size(); |
| 165 |
ASSERT_EQUAL(1u, size); |
| 166 |
ASSERT_EQUAL(m3.properties().get("foo"), scalar("newfoo")); |
| 167 |
ASSERT_EQUAL(2u, m3.delivery_annotations().size()); |
| 168 |
ASSERT_EQUAL(m3.delivery_annotations().get("bar"), scalar("xyz")); |
| 169 |
ASSERT_EQUAL(m3.delivery_annotations().get(24), scalar(1000)); |
| 170 |
ASSERT(m3.message_annotations().empty()); |
| 171 |
|
| 172 |
// PROTON-1498 |
| 173 |
message msg; |
| 174 |
msg.message_annotations().put("x-opt-jms-msg-type", int8_t(1)); |
| 175 |
|
| 176 |
proton::message::annotation_map& am_ref = msg.message_annotations(); |
| 177 |
uint8_t t = am_ref.get(proton::symbol("x-opt-jms-msg-type")).get<int8_t>(); |
| 178 |
ASSERT_EQUAL(1, t); |
| 179 |
|
| 180 |
proton::message::annotation_map am_val = msg.message_annotations(); |
| 181 |
t = am_val.get(proton::symbol("x-opt-jms-msg-type")).get<int8_t>(); |
| 182 |
ASSERT_EQUAL(1, t); |
| 183 |
} |
| 184 |
|
| 185 |
void test_message_reuse() { |
| 186 |
message m1("one"); |
| 187 |
m1.properties().put("x", "y"); |
| 188 |
|
| 189 |
message m2("two"); |
| 190 |
m2.properties().put("a", "b"); |
| 191 |
|
| 192 |
m1.decode(m2.encode()); // Use m1 for a newly decoded message |
| 193 |
ASSERT_EQUAL(value("two"), m1.body()); |
| 194 |
ASSERT_EQUAL(value("b"), m1.properties().get("a")); |
| 195 |
} |
| 196 |
|
| 197 |
void test_message_print() { |
| 198 |
message m("hello"); |
| 199 |
m.to("to"); |
| 200 |
m.user("user"); |
| 201 |
m.subject("subject"); |
| 202 |
m.content_type("weird"); |
| 203 |
m.correlation_id(23); |
| 204 |
m.properties().put("foo", "bar"); |
| 205 |
m.properties().put("num", 9); |
| 206 |
m.delivery_annotations().put("deliverme", "please"); |
| 207 |
ASSERT_EQUAL("Message{address=\"to\", user_id=\"user\", subject=\"subject\", correlation_id=23, content_type=\"weird\", body=\"hello\"}", to_string(m)); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
int main(int, char**) { |
| 213 |
int failed = 0; |
| 214 |
RUN_TEST(failed, test_message_properties()); |
| 215 |
RUN_TEST(failed, test_message_defaults()); |
| 216 |
RUN_TEST(failed, test_message_body()); |
| 217 |
RUN_TEST(failed, test_message_maps()); |
| 218 |
RUN_TEST(failed, test_message_reuse()); |
| 219 |
RUN_TEST(failed, test_message_print()); |
| 220 |
return failed; |
| 221 |
} |