1 /*-----------------------------------------------------------------------------
  2 Name:      TestStringTrim.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Timeout Features
  6 -----------------------------------------------------------------------------*/
  7 #include <util/StringTrim.h>
  8 #include <util/XmlBlasterException.h>
  9 #include <util/Global.h>
 10 #include "TestSuite.h"
 11 #include <iostream>
 12 
 13 using namespace std;
 14 using namespace org::xmlBlaster::util;
 15 
 16 
 17 namespace org { namespace xmlBlaster { namespace test {
 18 
 19 class TestStringTrim
 20 {
 21 private:
 22    string  ME;
 23    Global& global_;
 24    I_Log&  log_;
 25 
 26 public:
 27    TestStringTrim(Global& glob) 
 28       : ME("TestStringTrim"), 
 29         global_(glob), 
 30         log_(glob.getLog("test"))
 31    {
 32    }
 33 
 34 
 35    void tearDown()
 36    {
 37    }
 38 
 39    virtual ~TestStringTrim()
 40    {
 41    }
 42 
 43    void setUp()
 44    {
 45    }
 46 
 47    void testTrim()
 48    {
 49       string me = ME + "::testConnectQos";
 50       log_.info(me, "testing parsing of a return connect qos: start");
 51       StringTrim trimmer;
 52 
 53       // Test const char * variant
 54       assertEquals(log_, me, string("ab"), StringTrim::trimStart(" \t\n ab"), string("Start test"));
 55       assertEquals(log_, me, string("OK"), trimmer.trimStart("OK"), string("Start test"));
 56       assertEquals(log_, me, string(""), trimmer.trimStart(0), string("Start test"));
 57       assertEquals(log_, me, string(""), trimmer.trimStart(""), string("Start test"));
 58       assertEquals(log_, me, string("A  A  "), trimmer.trimStart("A  A  "), string("Start test"));
 59 
 60       assertEquals(log_, me, string("ab"), trimmer.trimEnd("ab \t\n  "), string("End test"));
 61       assertEquals(log_, me, string("OK"), trimmer.trimEnd("OK"), string("End test"));
 62       assertEquals(log_, me, string(""), trimmer.trimEnd(0), string("End test"));
 63       assertEquals(log_, me, string(""), trimmer.trimEnd(""), string("End test"));
 64       assertEquals(log_, me, string("  A  A"), trimmer.trimEnd("  A  A"), string("End test"));
 65 
 66       assertEquals(log_, me, string("ab"), trimmer.trim(" \t\n ab \t\n  "), string("Trim test"));
 67       assertEquals(log_, me, string("OK"), trimmer.trim("OK"), string("Trim test"));
 68       assertEquals(log_, me, string(""), trimmer.trim(0), string("Trim test"));
 69       assertEquals(log_, me, string(""), trimmer.trim(""), string("Trim test"));
 70       assertEquals(log_, me, string("A  A"), trimmer.trim("  A  A"), string("Trim test"));
 71 
 72       // Test string variant
 73       string result;
 74       result = " \t\n ab";
 75       trimmer.trimStart(result);
 76       assertEquals(log_, me, string("ab"), result, string("string - Start test"));
 77       result = "OK";
 78       trimmer.trimStart(result);
 79       assertEquals(log_, me, string("OK"), result, string("string - Start test"));
 80       result = "";
 81       trimmer.trimStart(result);
 82       assertEquals(log_, me, string(""), result, string("string - Start test"));
 83       result = "A  A  ";
 84       trimmer.trimStart(result);
 85       assertEquals(log_, me, string("A  A  "), result, string("string - Start test"));
 86 
 87       result = "ab \t\n  ";
 88       trimmer.trimEnd(result);
 89       assertEquals(log_, me, string("ab"), result, string("string - End test"));
 90       result = "OK";
 91       trimmer.trimEnd(result);
 92       assertEquals(log_, me, string("OK"), result, string("string - End test"));
 93       result = "";
 94       trimmer.trimEnd(result);
 95       assertEquals(log_, me, string(""), result, string("string - End test"));
 96       result = "  A  A";
 97       trimmer.trimEnd(result);
 98       assertEquals(log_, me, string("  A  A"), result, string("string - End test"));
 99 
100       result = " \t\n ab \t\n  ";
101       trimmer.trim(result);
102       assertEquals(log_, me, string("ab"), result, string("string - Trim test"));
103       result = "OK";
104       trimmer.trim(result);
105       assertEquals(log_, me, string("OK"), result, string("string - Trim test"));
106       result = "";
107       trimmer.trim(result);
108       assertEquals(log_, me, string(""), result, string("string - Trim test"));
109       result = "  A  A";
110       trimmer.trim(result);
111       assertEquals(log_, me, string("A  A"), result, string("string - Trim test"));
112       cout << "DONE" << endl;
113       //   assertEquals(log_, log_, me, string("127.0.0.2"), address.getHostname(), "address hostname check");
114    }
115 };
116 
117 }}} // namespace 
118 
119 
120 using namespace org::xmlBlaster::test;
121 
122 /**
123  * Try
124  * <pre>
125  *   java TestStringTrim -help
126  * </pre>
127  * for usage help
128  */
129 int main(int args, char ** argv)
130 {
131    try {
132       org::xmlBlaster::util::Object_Lifetime_Manager::init();
133       Global& glob = Global::getInstance();
134       glob.initialize(args, argv);
135 
136       TestStringTrim test(glob);
137 
138       test.setUp();
139       test.testTrim();
140       test.tearDown();
141       org::xmlBlaster::util::Object_Lifetime_Manager::fini();
142    }
143    catch (XmlBlasterException& ex) {
144       std::cout << ex.toXml() << std::endl;
145    }
146    catch (bad_exception& ex) {
147       cout << "bad_exception: " << ex.what() << endl;
148    }
149    catch (exception& ex) {
150       cout << " exception: " << ex.what() << endl;
151    }
152    catch (string& ex) {
153       cout << "string: " << ex << endl;
154    }
155    catch (char* ex) {
156       cout << "char* :  " << ex << endl;
157    }
158 
159    catch (...)
160    {
161       cout << "unknown exception occured" << endl;
162       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
163       cout << e.toXml() << endl;
164    }
165    return 0;
166 }


syntax highlighted by Code2HTML, v. 0.9.1