1 /*-----------------------------------------------------------------------------
  2 Name:      TestClientProperty.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the ClientProperty Features
  6 -----------------------------------------------------------------------------*/
  7 #include "TestSuite.h"
  8 #include <iostream>
  9 #include <util/qos/ClientProperty.h>
 10 #include <util/qos/QosData.h>
 11 #include <util/qos/MsgQosData.h>
 12 
 13 using namespace std;
 14 using namespace org::xmlBlaster::util;
 15 using namespace org::xmlBlaster::util::qos;
 16 
 17 /**
 18  * Class test of ClientProperty.cpp
 19  * @see org::xmlBlaster::util::qos::ClientProperty 
 20  */
 21 
 22 namespace org { namespace xmlBlaster { namespace test {
 23 
 24 class TestClientProperty : public TestSuite {
 25    
 26 private:
 27    string ME;
 28    Global& global_;
 29    I_Log&  log_;
 30    int     count_;
 31 public:
 32    TestClientProperty(Global& global, const string& name) 
 33       : TestSuite(global.getArgs(), global.getArgc(), name, false),
 34         ME(name), 
 35         global_(global),
 36         log_(global.getLog("test")) 
 37    {
 38       count_ = 0;
 39    }
 40 
 41    virtual ~TestClientProperty()
 42    {
 43    }
 44 
 45    void testQosData()
 46    {
 47       log_.info(ME, "testQosData(): Starting tests ...");
 48       {
 49          MsgQosData msgQosData(global_);
 50          msgQosData.addClientProperty("key1", "value1", Constants::TYPE_STRING, Constants::ENCODING_BASE64);
 51 
 52          string res = msgQosData.getClientProperty("key1", string("dummy"));
 53          assertEquals(log_, ME, "value1", res, "bla");
 54       
 55          const QosData::ClientPropertyMap& map = msgQosData.getClientProperties();
 56          const QosData::ClientPropertyMap::const_iterator it = map.find("key1");
 57          if (it==map.end())
 58             log_.error(ME, "NO key1 found");
 59          const ClientProperty cp = (*it).second;
 60          log_.info(ME, cp.toXml(""));
 61          assertEquals(log_, ME, "key1", cp.getName(), "key1");
 62          assertEquals(log_, ME, "value1", cp.getStringValue(), "");
 63          assertEquals(log_, ME, Constants::TYPE_STRING, cp.getType(), "type");
 64          assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
 65          log_.info(ME, cp.toXml(""));
 66       }
 67       {
 68          MsgQosData msgQosData(global_);
 69          msgQosData.addClientProperty("key2", string("value2"));
 70 
 71          string res = msgQosData.getClientProperty("key2", string("dummy"));
 72          assertEquals(log_, ME, "value2", res, "bla");
 73       
 74          const QosData::ClientPropertyMap& map = msgQosData.getClientProperties();
 75          const QosData::ClientPropertyMap::const_iterator it = map.find("key2");
 76          if (it==map.end())
 77             log_.error(ME, "NO key2 found");
 78          const ClientProperty cp = (*it).second;
 79          log_.info(ME, cp.toXml(""));
 80          assertEquals(log_, ME, "key2", cp.getName(), "key2");
 81          assertEquals(log_, ME, "value2", cp.getStringValue(), "");
 82          assertEquals(log_, ME, ""/*Constants::TYPE_STRING*/, cp.getType(), "type");
 83          assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
 84          log_.info(ME, cp.toXml(""));
 85       }
 86    }
 87 
 88    void testClientProperty() 
 89    {
 90       log_.info(ME, "testClientProperty(): Starting tests ...");
 91       try {
 92          {
 93             ClientProperty cp("key", string("s tring"));
 94             cout << "name=" << cp.getName() 
 95                  << ", valueB64=" << cp.getValueRaw()
 96                  << ", value=" << cp.getStringValue()
 97                  << ", type=" << cp.getType()
 98                  << ", charset=" << cp.getCharset()
 99                  << ", isBase64=" << cp.isBase64()
100                  << cp.toXml("")
101                  << endl << endl;
102             assertEquals(log_, ME, "key", cp.getName(), "name");
103             assertEquals(log_, ME, "s tring", cp.getStringValue(), "value");
104             assertEquals(log_, ME, "s tring", cp.getValueRaw(), "encoded value");
105             assertEquals(log_, ME, "", cp.getType(), "type");
106             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
107             assertEquals(log_, ME, "", cp.getCharset(), "charset");
108             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
109          }
110          {
111             ClientProperty cp("key", string("102 304 506 "));
112             cp.setCharset("windows-1252");
113             cout << "name=" << cp.getName() 
114                  << ", valueB64=" << cp.getValueRaw()
115                  << ", value=" << cp.getStringValue()
116                  << ", type=" << cp.getType()
117                  << ", charset=" << cp.getCharset()
118                  << ", isBase64=" << cp.isBase64()
119                  << cp.toXml("")
120                  << endl << endl;
121             assertEquals(log_, ME, "key", cp.getName(), "name");
122             assertEquals(log_, ME, "102 304 506 ", cp.getStringValue(), "value");
123             assertEquals(log_, ME, "102 304 506 ", cp.getValueRaw(), "encoded value");
124             assertEquals(log_, ME, "", cp.getType(), "type");
125             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
126             assertEquals(log_, ME, "windows-1252", cp.getCharset(), "charset");
127             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
128          }
129          {
130             ClientProperty cp("key", string("str]]>ing"));
131             cout << "name=" << cp.getName() 
132                  << ", valueB64=" << cp.getValueRaw()
133                  << ", value=" << cp.getStringValue()
134                  << ", type=" << cp.getType()
135                  << ", isBase64=" << cp.isBase64()
136                  << cp.toXml("")
137                  << endl << endl;
138             assertEquals(log_, ME, "key", cp.getName(), "name");
139             assertEquals(log_, ME, "str]]>ing", cp.getStringValue(), "value");
140             assertEquals(log_, ME, "c3RyXV0+aW5n", cp.getValueRaw(), "encoded value");
141             assertEquals(log_, ME, "", cp.getType(), "type");
142             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
143             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
144          }
145          {
146             ClientProperty cp("key", string("str&ing"));
147             cout << "name=" << cp.getName() 
148                  << ", valueB64=" << cp.getValueRaw()
149                  << ", value=" << cp.getStringValue()
150                  << ", type=" << cp.getType()
151                  << ", isBase64=" << cp.isBase64()
152                  << cp.toXml("")
153                  << endl << endl;
154             assertEquals(log_, ME, "key", cp.getName(), "name");
155             assertEquals(log_, ME, "str&ing", cp.getStringValue(), "value");
156             assertEquals(log_, ME, "c3RyJmluZw==", cp.getValueRaw(), "encoded value");
157             assertEquals(log_, ME, "", cp.getType(), "type");
158             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
159             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
160          }
161          {
162             ClientProperty cp("transactionID", string("x2004062008 4423489478000"));
163             cout << "name=" << cp.getName() 
164                  << ", valueB64=" << cp.getValueRaw()
165                  << ", value=" << cp.getStringValue()
166                  << ", type=" << cp.getType()
167                  << ", isBase64=" << cp.isBase64()
168                  << cp.toXml("")
169                  << endl << endl;
170             assertEquals(log_, ME, "transactionID", cp.getName(), "name");
171             assertEquals(log_, ME, "x2004062008 4423489478000", cp.getStringValue(), "value");
172             assertEquals(log_, ME, "", cp.getType(), "type");
173             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
174          }
175          {
176             ClientProperty cp("key", string("str<<<ing"));
177             cout << "name=" << cp.getName() 
178                  << ", valueB64=" << cp.getValueRaw()
179                  << ", value=" << cp.getStringValue()
180                  << ", type=" << cp.getType()
181                  << ", isBase64=" << cp.isBase64()
182                  << cp.toXml("")
183                  << endl << endl;
184             assertEquals(log_, ME, "key", cp.getName(), "name");
185             assertEquals(log_, ME, "str<<<ing", cp.getStringValue(), "value");
186             assertEquals(log_, ME, "", cp.getType(), "type");
187             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
188             assertEquals(log_, ME, "c3RyPDw8aW5n", cp.getValueRaw(), "encoded value");
189             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
190          }
191          {
192             ClientProperty cp("key", "const char *");
193             cout << "name=" << cp.getName() 
194                  << ", valueB64=" << cp.getValueRaw()
195                  << ", value=" << cp.getStringValue()
196                  << ", type=" << cp.getType()
197                  << ", isBase64=" << cp.isBase64()
198                  << cp.toXml("")
199                  << endl << endl;
200             assertEquals(log_, ME, "key", cp.getName(), "name");
201             assertEquals(log_, ME, "const char *", cp.getStringValue(), "value");
202             assertEquals(log_, ME, "const char *", cp.getValueRaw(), "encoded value");
203             assertEquals(log_, ME, "", cp.getType(), "type");
204             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
205             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
206          }
207          {
208             long f=10L;
209             ClientProperty cp("key", f);
210             cout << "name=" << cp.getName() 
211                  << ", valueB64=" << cp.getValueRaw()
212                  << ", value=" << cp.getStringValue()
213                  << ", type=" << cp.getType()
214                  << ", isBase64=" << cp.isBase64()
215                  << cp.toXml("")
216                  << endl << endl;
217             long ret;
218             cp.getValue(ret);
219             assertEquals(log_, ME, "key", cp.getName(), "name");
220             assertEquals(log_, ME, f, ret, "value");
221             assertEquals(log_, ME, "10", cp.getValueRaw(), "encoded value");
222             assertEquals(log_, ME, "long", cp.getType(), "type");
223             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
224             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
225          }
226          {
227             float f=10.5;
228             ClientProperty cp("key", f);
229             cout << "name=" << cp.getName() 
230                  << ", valueB64=" << cp.getValueRaw()
231                  << ", value=" << cp.getStringValue()
232                  << ", type=" << cp.getType()
233                  << ", isBase64=" << cp.isBase64()
234                  << cp.toXml("")
235                  << endl << endl;
236             float ret;
237             cp.getValue(ret);
238             assertEquals(log_, ME, "key", cp.getName(), "name");
239             assertEquals(log_, ME, f, ret, "value");
240             assertEquals(log_, ME, "10.5", cp.getValueRaw(), "encoded value");
241             assertEquals(log_, ME, "float", cp.getType(), "type");
242             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
243             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
244          }
245          {
246             double f=20.63452879L;
247             ClientProperty cp("key", f);
248             cout << "name=" << cp.getName() 
249                  << ", valueB64=" << cp.getValueRaw()
250                  << ", value=" << cp.getStringValue()
251                  << ", type=" << cp.getType()
252                  << ", encoding=" << cp.getEncoding()
253                  << ", isBase64=" << cp.isBase64()
254                  << cp.toXml("")
255                  << endl;
256             double ret;
257             cp.getValue(ret);
258             assertEquals(log_, ME, "key", cp.getName(), "name");
259             //assertEquals(log_, ME, f, ret, "value");
260             assertEquals(log_, ME, "20.63452879", cp.getValueRaw(), "encoded value");
261             assertEquals(log_, ME, "double", cp.getType(), "type");
262             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
263             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
264          }
265          {
266             vector<unsigned char> v;
267             v.push_back('H');
268             v.push_back('a');
269             v.push_back('l');
270             v.push_back('l');
271             v.push_back('o');
272             ClientProperty cp("key", v);
273             cout << "name=" << cp.getName() 
274                  << ", valueB64=" << cp.getValueRaw()
275                  << ", value=" << cp.getStringValue()
276                  << ", type=" << cp.getType()
277                  << ", encoding=" << cp.getEncoding()
278                  << ", isBase64=" << cp.isBase64()
279                  << cp.toXml("")
280                  << endl;
281             {
282                std::vector<unsigned char> ret = cp.getValue();
283                std::string str;
284 #if defined(__sun)
285                // No assign
286                cout << "NEW=";
287                for (int i=0; i<ret.size(); i++) {
288                   cout << ret.at(i);
289                }
290                cout << endl;
291 #else
292                str.assign(ret.begin(),ret.end());
293                cout << "NEW=" << str << endl;
294 #endif
295             }
296             assertEquals(log_, ME, "key", cp.getName(), "name");
297             assertEquals(log_, ME, "Hallo", cp.getStringValue(), "value");
298             assertEquals(log_, ME, "byte[]", cp.getType(), "type");
299             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
300             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
301             assertEquals(log_, ME, "SGFsbG8=", cp.getValueRaw(), "encoded value");
302          }
303          {
304             bool b=true;
305             ClientProperty cp("key", b);
306             cout << "name=" << cp.getName() 
307                  << ", valueB64=" << cp.getValueRaw()
308                  << ", value=" << cp.getStringValue()
309                  << ", type=" << cp.getType()
310                  << ", encoding=" << cp.getEncoding()
311                  << ", isBase64=" << cp.isBase64()
312                  << cp.toXml("")
313                  << endl;
314             bool ret;
315             cp.getValue(ret);
316             assertEquals(log_, ME, "key", cp.getName(), "name");
317             //assertEquals(log_, ME, f, ret, "value");
318             assertEquals(log_, ME, "true", cp.getValueRaw(), "encoded value");
319             assertEquals(log_, ME, "boolean", cp.getType(), "type");
320             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
321             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
322          }
323       }
324       catch(bad_cast b) {
325          cout << "EXCEPTION: " << b.what() << endl;
326          assert(0);
327       }
328    }
329 
330    void setUp() {
331    }
332 
333    void tearDown() {
334    }
335 };
336    
337 }}} // namespace
338 
339 using namespace org::xmlBlaster::test;
340 
341 int main(int args, char *argv[]) 
342 {
343    Global& glob = Global::getInstance();
344    glob.initialize(args, argv);
345 
346    TestClientProperty testObj(glob, "TestClientProperty");
347 
348    testObj.setUp();
349    testObj.testQosData();
350    testObj.testClientProperty();
351    testObj.tearDown();
352    return 0;
353 }


syntax highlighted by Code2HTML, v. 0.9.1