1 /*-----------------------------------------------------------------------------
  2 Name:      TestKeys.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a client using xmlBlaster
  6 Version:   $Id: TestKeys.cpp 14955 2006-03-20 12:40:31Z goetzger $
  7 -----------------------------------------------------------------------------*/
  8 #include "TestSuite.h"
  9 #include <util/key/MsgKeyFactory.h>
 10 #include <iostream>
 11 
 12 /**
 13  * This client tests the Key objects.<br />
 14  * <p>
 15  */
 16 
 17 using namespace std;
 18 using namespace org::xmlBlaster::util;
 19 using namespace org::xmlBlaster::util::thread;
 20 using namespace org::xmlBlaster::util::key;
 21 using namespace org::xmlBlaster::client;
 22 using namespace org::xmlBlaster::client::key;
 23 
 24 namespace org { namespace xmlBlaster { namespace test {
 25 
 26 class TestKeys: public TestSuite
 27 {
 28 private:
 29 
 30    /**
 31     * Constructs the TestKeys object.
 32     * <p />
 33     * @param testName  The name used in the test suite
 34     * @param loginName The name to login to the xmlBlaster
 35     */
 36  public:
 37    TestKeys(int args, char *argc[]) : TestSuite(args, argc, "TestKeys")
 38    {
 39    }
 40 
 41    virtual ~TestKeys() 
 42    {
 43    }
 44 
 45    /**
 46     * Sets up the fixture. <p />
 47     * Connect to xmlBlaster and login
 48     */
 49    void setUp() 
 50    {
 51       TestSuite::setUp();
 52    }
 53 
 54 
 55    /**
 56     * Tears down the fixture. <p />
 57     * cleaning up .... erase() the previous message OID and logout
 58     */
 59    void tearDown() 
 60    {
 61       log_.info(ME, "Going to tear down.");
 62       TestSuite::tearDown();
 63    }
 64 
 65 
 66    /**
 67     * TEST: Construct a message and publish it. <p />
 68     * The returned publishOid is checked
 69     */
 70    void testPublishKey() 
 71    {
 72       if (log_.trace()) log_.trace(ME, "TestPublishKey");
 73       PublishKey pubKey(global_);
 74       pubKey.setOid("someOid");
 75       pubKey.setContentMime("text/plain");
 76       pubKey.setContentMimeExtended("3.124");
 77       string xmlKey = string("") +
 78          "  <TestKeys-AGENT id='192.168.124.10' subId='1' type='generic' >\n" +
 79          "    <TestKeys-DRIVER id='FileProof' pollingFreq='10' >\n" +
 80          "      <![CDATA[ this is a simple cdata text ]]>\n" + 
 81          "    </TestKeys-DRIVER>\n"+
 82          "  </TestKeys-AGENT>\n";
 83       pubKey.setClientTags(xmlKey);
 84 
 85      string mime = pubKey.getContentMime();
 86      assertEquals(log_, ME, "text/plain", mime, "checking mime type");
 87      string mimeExtended = pubKey.getContentMimeExtended();
 88      assertEquals(log_, ME, "3.124", mimeExtended, "checking mime extended type");
 89      string xmlKeyRet =  pubKey.getData().getClientTags();
 90      assertEquals(log_, ME, xmlKey, xmlKeyRet, "checking client tags");
 91 
 92      string literal = pubKey.toXml();
 93      if (log_.trace()) log_.trace(ME, "testPublishKey the literal is: " + literal);
 94      
 95      MsgKeyFactory factory(global_);
 96      MsgKeyData keyData = factory.readObject(literal);
 97 
 98      mime = keyData.getContentMime();
 99      assertEquals(log_, ME, "text/plain", mime, "msgKeyData: checking mime type");
100      mimeExtended = keyData.getContentMimeExtended();
101      assertEquals(log_, ME, "3.124", mimeExtended, "msgKeyData: checking mime extended type");
102      xmlKeyRet =  keyData.getClientTags();
103      assertEquals(log_, ME, xmlKey, xmlKeyRet, "msgKeyData: checking client tags");
104 
105    }
106 
107 
108    void usage() const
109    {
110       TestSuite::usage();
111       log_.plain(ME, "----------------------------------------------------------");
112       XmlBlasterAccess::usage();
113       log_.usage();
114       log_.plain(ME, "Example:");
115       log_.plain(ME, "   TestKeys -bootstrapHostname myHost.myCompany.com -bootstrapPort 3412 -trace true");
116       log_.plain(ME, "----------------------------------------------------------");
117    }
118 };
119 
120 }}} // namespace
121 
122 using namespace org::xmlBlaster::test;
123 
124 int main(int args, char *argc[]) 
125 {
126    try {
127       org::xmlBlaster::util::Object_Lifetime_Manager::init();
128       TestKeys testSub(args, argc);
129       testSub.setUp();
130       testSub.testPublishKey();
131       testSub.tearDown();
132       Thread::sleepSecs(1);
133    }
134    catch (XmlBlasterException& ex) {
135       std::cout << ex.toXml() << std::endl;
136    }
137    catch (bad_exception& ex) {
138       cout << "bad_exception: " << ex.what() << endl;
139    }
140    catch (exception& ex) {
141       cout << " exception: " << ex.what() << endl;
142    }
143    catch (string& ex) {
144       cout << "string: " << ex << endl;
145    }
146    catch (char* ex) {
147       cout << "char* :  " << ex << endl;
148    }
149 
150    catch (...)
151    {
152       cout << "unknown exception occured" << endl;
153       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
154       cout << e.toXml() << endl;
155    }
156 
157    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
158    return 0;
159 }


syntax highlighted by Code2HTML, v. 0.9.1