testsuite/src/c++/TestKeys.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestKeys.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Demo code for a client using xmlBlaster
00006 Version:   $Id: TestKeys.cpp 14955 2006-03-20 12:40:31Z goetzger $
00007 -----------------------------------------------------------------------------*/
00008 #include "TestSuite.h"
00009 #include <util/key/MsgKeyFactory.h>
00010 #include <iostream>
00011 
00017 using namespace std;
00018 using namespace org::xmlBlaster::util;
00019 using namespace org::xmlBlaster::util::thread;
00020 using namespace org::xmlBlaster::util::key;
00021 using namespace org::xmlBlaster::client;
00022 using namespace org::xmlBlaster::client::key;
00023 
00024 namespace org { namespace xmlBlaster { namespace test {
00025 
00026 class TestKeys: public TestSuite
00027 {
00028 private:
00029 
00036  public:
00037    TestKeys(int args, char *argc[]) : TestSuite(args, argc, "TestKeys")
00038    {
00039    }
00040 
00041    virtual ~TestKeys() 
00042    {
00043    }
00044 
00049    void setUp() 
00050    {
00051       TestSuite::setUp();
00052    }
00053 
00054 
00059    void tearDown() 
00060    {
00061       log_.info(ME, "Going to tear down.");
00062       TestSuite::tearDown();
00063    }
00064 
00065 
00070    void testPublishKey() 
00071    {
00072       if (log_.trace()) log_.trace(ME, "TestPublishKey");
00073       PublishKey pubKey(global_);
00074       pubKey.setOid("someOid");
00075       pubKey.setContentMime("text/plain");
00076       pubKey.setContentMimeExtended("3.124");
00077       string xmlKey = string("") +
00078          "  <TestKeys-AGENT id='192.168.124.10' subId='1' type='generic' >\n" +
00079          "    <TestKeys-DRIVER id='FileProof' pollingFreq='10' >\n" +
00080          "      <![CDATA[ this is a simple cdata text ]]>\n" + 
00081          "    </TestKeys-DRIVER>\n"+
00082          "  </TestKeys-AGENT>\n";
00083       pubKey.setClientTags(xmlKey);
00084 
00085      string mime = pubKey.getContentMime();
00086      assertEquals(log_, ME, "text/plain", mime, "checking mime type");
00087      string mimeExtended = pubKey.getContentMimeExtended();
00088      assertEquals(log_, ME, "3.124", mimeExtended, "checking mime extended type");
00089      string xmlKeyRet =  pubKey.getData().getClientTags();
00090      assertEquals(log_, ME, xmlKey, xmlKeyRet, "checking client tags");
00091 
00092      string literal = pubKey.toXml();
00093      if (log_.trace()) log_.trace(ME, "testPublishKey the literal is: " + literal);
00094      
00095      MsgKeyFactory factory(global_);
00096      MsgKeyData keyData = factory.readObject(literal);
00097 
00098      mime = keyData.getContentMime();
00099      assertEquals(log_, ME, "text/plain", mime, "msgKeyData: checking mime type");
00100      mimeExtended = keyData.getContentMimeExtended();
00101      assertEquals(log_, ME, "3.124", mimeExtended, "msgKeyData: checking mime extended type");
00102      xmlKeyRet =  keyData.getClientTags();
00103      assertEquals(log_, ME, xmlKey, xmlKeyRet, "msgKeyData: checking client tags");
00104 
00105    }
00106 
00107 
00108    void usage() const
00109    {
00110       TestSuite::usage();
00111       log_.plain(ME, "----------------------------------------------------------");
00112       XmlBlasterAccess::usage();
00113       log_.usage();
00114       log_.plain(ME, "Example:");
00115       log_.plain(ME, "   TestKeys -bootstrapHostname myHost.myCompany.com -bootstrapPort 3412 -trace true");
00116       log_.plain(ME, "----------------------------------------------------------");
00117    }
00118 };
00119 
00120 }}} // namespace
00121 
00122 using namespace org::xmlBlaster::test;
00123 
00124 int main(int args, char *argc[]) 
00125 {
00126    try {
00127       org::xmlBlaster::util::Object_Lifetime_Manager::init();
00128       TestKeys testSub(args, argc);
00129       testSub.setUp();
00130       testSub.testPublishKey();
00131       testSub.tearDown();
00132       Thread::sleepSecs(1);
00133    }
00134    catch (XmlBlasterException& ex) {
00135       std::cout << ex.toXml() << std::endl;
00136    }
00137    catch (bad_exception& ex) {
00138       cout << "bad_exception: " << ex.what() << endl;
00139    }
00140    catch (exception& ex) {
00141       cout << " exception: " << ex.what() << endl;
00142    }
00143    catch (string& ex) {
00144       cout << "string: " << ex << endl;
00145    }
00146    catch (char* ex) {
00147       cout << "char* :  " << ex << endl;
00148    }
00149 
00150    catch (...)
00151    {
00152       cout << "unknown exception occured" << endl;
00153       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
00154       cout << e.toXml() << endl;
00155    }
00156 
00157    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00158    return 0;
00159 }
00160