testsuite/src/c++/TestClientProperty.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestClientProperty.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Testing the ClientProperty Features
00006 -----------------------------------------------------------------------------*/
00007 #include "TestSuite.h"
00008 #include <iostream>
00009 #include <util/qos/ClientProperty.h>
00010 #include <util/qos/QosData.h>
00011 #include <util/qos/MsgQosData.h>
00012 
00013 using namespace std;
00014 using namespace org::xmlBlaster::util;
00015 using namespace org::xmlBlaster::util::qos;
00016 
00022 namespace org { namespace xmlBlaster { namespace test {
00023 
00024 class TestClientProperty : public TestSuite {
00025    
00026 private:
00027    string ME;
00028    Global& global_;
00029    I_Log&  log_;
00030    int     count_;
00031 public:
00032    TestClientProperty(Global& global, const string& name) 
00033       : TestSuite(global.getArgs(), global.getArgc(), name, false),
00034         ME(name), 
00035         global_(global),
00036         log_(global.getLog("test")) 
00037    {
00038       count_ = 0;
00039    }
00040 
00041    virtual ~TestClientProperty()
00042    {
00043    }
00044 
00045    void testQosData()
00046    {
00047       log_.info(ME, "testQosData(): Starting tests ...");
00048       {
00049          MsgQosData msgQosData(global_);
00050          msgQosData.addClientProperty("key1", "value1", Constants::TYPE_STRING, Constants::ENCODING_BASE64);
00051 
00052          string res = msgQosData.getClientProperty("key1", string("dummy"));
00053          assertEquals(log_, ME, "value1", res, "bla");
00054       
00055          const QosData::ClientPropertyMap& map = msgQosData.getClientProperties();
00056          const QosData::ClientPropertyMap::const_iterator it = map.find("key1");
00057          if (it==map.end())
00058             log_.error(ME, "NO key1 found");
00059          const ClientProperty cp = (*it).second;
00060          log_.info(ME, cp.toXml(""));
00061          assertEquals(log_, ME, "key1", cp.getName(), "key1");
00062          assertEquals(log_, ME, "value1", cp.getStringValue(), "");
00063          assertEquals(log_, ME, Constants::TYPE_STRING, cp.getType(), "type");
00064          assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
00065          log_.info(ME, cp.toXml(""));
00066       }
00067       {
00068          MsgQosData msgQosData(global_);
00069          msgQosData.addClientProperty("key2", string("value2"));
00070 
00071          string res = msgQosData.getClientProperty("key2", string("dummy"));
00072          assertEquals(log_, ME, "value2", res, "bla");
00073       
00074          const QosData::ClientPropertyMap& map = msgQosData.getClientProperties();
00075          const QosData::ClientPropertyMap::const_iterator it = map.find("key2");
00076          if (it==map.end())
00077             log_.error(ME, "NO key2 found");
00078          const ClientProperty cp = (*it).second;
00079          log_.info(ME, cp.toXml(""));
00080          assertEquals(log_, ME, "key2", cp.getName(), "key2");
00081          assertEquals(log_, ME, "value2", cp.getStringValue(), "");
00082          assertEquals(log_, ME, ""/*Constants::TYPE_STRING*/, cp.getType(), "type");
00083          assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00084          log_.info(ME, cp.toXml(""));
00085       }
00086    }
00087 
00088    void testClientProperty() 
00089    {
00090       log_.info(ME, "testClientProperty(): Starting tests ...");
00091       try {
00092          {
00093             ClientProperty cp("key", string("s tring"));
00094             cout << "name=" << cp.getName() 
00095                  << ", valueB64=" << cp.getValueRaw()
00096                  << ", value=" << cp.getStringValue()
00097                  << ", type=" << cp.getType()
00098                  << ", charset=" << cp.getCharset()
00099                  << ", isBase64=" << cp.isBase64()
00100                  << cp.toXml("")
00101                  << endl << endl;
00102             assertEquals(log_, ME, "key", cp.getName(), "name");
00103             assertEquals(log_, ME, "s tring", cp.getStringValue(), "value");
00104             assertEquals(log_, ME, "s tring", cp.getValueRaw(), "encoded value");
00105             assertEquals(log_, ME, "", cp.getType(), "type");
00106             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00107             assertEquals(log_, ME, "", cp.getCharset(), "charset");
00108             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00109          }
00110          {
00111             ClientProperty cp("key", string("102 304 506 "));
00112             cp.setCharset("windows-1252");
00113             cout << "name=" << cp.getName() 
00114                  << ", valueB64=" << cp.getValueRaw()
00115                  << ", value=" << cp.getStringValue()
00116                  << ", type=" << cp.getType()
00117                  << ", charset=" << cp.getCharset()
00118                  << ", isBase64=" << cp.isBase64()
00119                  << cp.toXml("")
00120                  << endl << endl;
00121             assertEquals(log_, ME, "key", cp.getName(), "name");
00122             assertEquals(log_, ME, "102 304 506 ", cp.getStringValue(), "value");
00123             assertEquals(log_, ME, "102 304 506 ", cp.getValueRaw(), "encoded value");
00124             assertEquals(log_, ME, "", cp.getType(), "type");
00125             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00126             assertEquals(log_, ME, "windows-1252", cp.getCharset(), "charset");
00127             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00128          }
00129          {
00130             ClientProperty cp("key", string("str]]>ing"));
00131             cout << "name=" << cp.getName() 
00132                  << ", valueB64=" << cp.getValueRaw()
00133                  << ", value=" << cp.getStringValue()
00134                  << ", type=" << cp.getType()
00135                  << ", isBase64=" << cp.isBase64()
00136                  << cp.toXml("")
00137                  << endl << endl;
00138             assertEquals(log_, ME, "key", cp.getName(), "name");
00139             assertEquals(log_, ME, "str]]>ing", cp.getStringValue(), "value");
00140             assertEquals(log_, ME, "c3RyXV0+aW5n", cp.getValueRaw(), "encoded value");
00141             assertEquals(log_, ME, "", cp.getType(), "type");
00142             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
00143             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
00144          }
00145          {
00146             ClientProperty cp("key", string("str&ing"));
00147             cout << "name=" << cp.getName() 
00148                  << ", valueB64=" << cp.getValueRaw()
00149                  << ", value=" << cp.getStringValue()
00150                  << ", type=" << cp.getType()
00151                  << ", isBase64=" << cp.isBase64()
00152                  << cp.toXml("")
00153                  << endl << endl;
00154             assertEquals(log_, ME, "key", cp.getName(), "name");
00155             assertEquals(log_, ME, "str&ing", cp.getStringValue(), "value");
00156             assertEquals(log_, ME, "c3RyJmluZw==", cp.getValueRaw(), "encoded value");
00157             assertEquals(log_, ME, "", cp.getType(), "type");
00158             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
00159             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
00160          }
00161          {
00162             ClientProperty cp("transactionID", string("x2004062008 4423489478000"));
00163             cout << "name=" << cp.getName() 
00164                  << ", valueB64=" << cp.getValueRaw()
00165                  << ", value=" << cp.getStringValue()
00166                  << ", type=" << cp.getType()
00167                  << ", isBase64=" << cp.isBase64()
00168                  << cp.toXml("")
00169                  << endl << endl;
00170             assertEquals(log_, ME, "transactionID", cp.getName(), "name");
00171             assertEquals(log_, ME, "x2004062008 4423489478000", cp.getStringValue(), "value");
00172             assertEquals(log_, ME, "", cp.getType(), "type");
00173             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00174          }
00175          {
00176             ClientProperty cp("key", string("str<<<ing"));
00177             cout << "name=" << cp.getName() 
00178                  << ", valueB64=" << cp.getValueRaw()
00179                  << ", value=" << cp.getStringValue()
00180                  << ", type=" << cp.getType()
00181                  << ", isBase64=" << cp.isBase64()
00182                  << cp.toXml("")
00183                  << endl << endl;
00184             assertEquals(log_, ME, "key", cp.getName(), "name");
00185             assertEquals(log_, ME, "str<<<ing", cp.getStringValue(), "value");
00186             assertEquals(log_, ME, "", cp.getType(), "type");
00187             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
00188             assertEquals(log_, ME, "c3RyPDw8aW5n", cp.getValueRaw(), "encoded value");
00189             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
00190          }
00191          {
00192             ClientProperty cp("key", "const char *");
00193             cout << "name=" << cp.getName() 
00194                  << ", valueB64=" << cp.getValueRaw()
00195                  << ", value=" << cp.getStringValue()
00196                  << ", type=" << cp.getType()
00197                  << ", isBase64=" << cp.isBase64()
00198                  << cp.toXml("")
00199                  << endl << endl;
00200             assertEquals(log_, ME, "key", cp.getName(), "name");
00201             assertEquals(log_, ME, "const char *", cp.getStringValue(), "value");
00202             assertEquals(log_, ME, "const char *", cp.getValueRaw(), "encoded value");
00203             assertEquals(log_, ME, "", cp.getType(), "type");
00204             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00205             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00206          }
00207          {
00208             long f=10L;
00209             ClientProperty cp("key", f);
00210             cout << "name=" << cp.getName() 
00211                  << ", valueB64=" << cp.getValueRaw()
00212                  << ", value=" << cp.getStringValue()
00213                  << ", type=" << cp.getType()
00214                  << ", isBase64=" << cp.isBase64()
00215                  << cp.toXml("")
00216                  << endl << endl;
00217             long ret;
00218             cp.getValue(ret);
00219             assertEquals(log_, ME, "key", cp.getName(), "name");
00220             assertEquals(log_, ME, f, ret, "value");
00221             assertEquals(log_, ME, "10", cp.getValueRaw(), "encoded value");
00222             assertEquals(log_, ME, "long", cp.getType(), "type");
00223             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00224             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00225          }
00226          {
00227             float f=10.5;
00228             ClientProperty cp("key", f);
00229             cout << "name=" << cp.getName() 
00230                  << ", valueB64=" << cp.getValueRaw()
00231                  << ", value=" << cp.getStringValue()
00232                  << ", type=" << cp.getType()
00233                  << ", isBase64=" << cp.isBase64()
00234                  << cp.toXml("")
00235                  << endl << endl;
00236             float ret;
00237             cp.getValue(ret);
00238             assertEquals(log_, ME, "key", cp.getName(), "name");
00239             assertEquals(log_, ME, f, ret, "value");
00240             assertEquals(log_, ME, "10.5", cp.getValueRaw(), "encoded value");
00241             assertEquals(log_, ME, "float", cp.getType(), "type");
00242             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00243             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00244          }
00245          {
00246             double f=20.63452879L;
00247             ClientProperty cp("key", f);
00248             cout << "name=" << cp.getName() 
00249                  << ", valueB64=" << cp.getValueRaw()
00250                  << ", value=" << cp.getStringValue()
00251                  << ", type=" << cp.getType()
00252                  << ", encoding=" << cp.getEncoding()
00253                  << ", isBase64=" << cp.isBase64()
00254                  << cp.toXml("")
00255                  << endl;
00256             double ret;
00257             cp.getValue(ret);
00258             assertEquals(log_, ME, "key", cp.getName(), "name");
00259             //assertEquals(log_, ME, f, ret, "value");
00260             assertEquals(log_, ME, "20.63452879", cp.getValueRaw(), "encoded value");
00261             assertEquals(log_, ME, "double", cp.getType(), "type");
00262             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00263             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00264          }
00265          {
00266             vector<unsigned char> v;
00267             v.push_back('H');
00268             v.push_back('a');
00269             v.push_back('l');
00270             v.push_back('l');
00271             v.push_back('o');
00272             ClientProperty cp("key", v);
00273             cout << "name=" << cp.getName() 
00274                  << ", valueB64=" << cp.getValueRaw()
00275                  << ", value=" << cp.getStringValue()
00276                  << ", type=" << cp.getType()
00277                  << ", encoding=" << cp.getEncoding()
00278                  << ", isBase64=" << cp.isBase64()
00279                  << cp.toXml("")
00280                  << endl;
00281             {
00282                std::vector<unsigned char> ret = cp.getValue();
00283                std::string str;
00284 #if defined(__sun)
00285                // No assign
00286                cout << "NEW=";
00287                for (int i=0; i<ret.size(); i++) {
00288                   cout << ret.at(i);
00289                }
00290                cout << endl;
00291 #else
00292                str.assign(ret.begin(),ret.end());
00293                cout << "NEW=" << str << endl;
00294 #endif
00295             }
00296             assertEquals(log_, ME, "key", cp.getName(), "name");
00297             assertEquals(log_, ME, "Hallo", cp.getStringValue(), "value");
00298             assertEquals(log_, ME, "byte[]", cp.getType(), "type");
00299             assertEquals(log_, ME, "base64", cp.getEncoding(), "encoding");
00300             assertEquals(log_, ME, true, cp.isBase64(), "isBase64");
00301             assertEquals(log_, ME, "SGFsbG8=", cp.getValueRaw(), "encoded value");
00302          }
00303          {
00304             bool b=true;
00305             ClientProperty cp("key", b);
00306             cout << "name=" << cp.getName() 
00307                  << ", valueB64=" << cp.getValueRaw()
00308                  << ", value=" << cp.getStringValue()
00309                  << ", type=" << cp.getType()
00310                  << ", encoding=" << cp.getEncoding()
00311                  << ", isBase64=" << cp.isBase64()
00312                  << cp.toXml("")
00313                  << endl;
00314             bool ret;
00315             cp.getValue(ret);
00316             assertEquals(log_, ME, "key", cp.getName(), "name");
00317             //assertEquals(log_, ME, f, ret, "value");
00318             assertEquals(log_, ME, "true", cp.getValueRaw(), "encoded value");
00319             assertEquals(log_, ME, "boolean", cp.getType(), "type");
00320             assertEquals(log_, ME, "", cp.getEncoding(), "encoding");
00321             assertEquals(log_, ME, false, cp.isBase64(), "isBase64");
00322          }
00323       }
00324       catch(bad_cast b) {
00325          cout << "EXCEPTION: " << b.what() << endl;
00326          assert(0);
00327       }
00328    }
00329 
00330    void setUp() {
00331    }
00332 
00333    void tearDown() {
00334    }
00335 };
00336    
00337 }}} // namespace
00338 
00339 using namespace org::xmlBlaster::test;
00340 
00341 int main(int args, char *argv[]) 
00342 {
00343    Global& glob = Global::getInstance();
00344    glob.initialize(args, argv);
00345 
00346    TestClientProperty testObj(glob, "TestClientProperty");
00347 
00348    testObj.setUp();
00349    testObj.testQosData();
00350    testObj.testClientProperty();
00351    testObj.tearDown();
00352    return 0;
00353 }
00354 
00355