1 /*-----------------------------------------------------------------------------
  2 Name:      TestProperty.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the property class
  6 -----------------------------------------------------------------------------*/
  7 #include "TestSuite.h"
  8 #include <util/Property.h>
  9 #include <iostream>
 10 #include <stdlib.h> // setenv()
 11 
 12 using namespace std;
 13 using namespace org::xmlBlaster::util;
 14 
 15 namespace org { namespace xmlBlaster { namespace test {
 16 
 17 static int dosetenv(const char *key, const char *value);
 18 
 19 /**
 20  * @return 0 on success
 21  */
 22 static int dosetenv(const char *key, const char *value)
 23 {
 24 #  ifdef _WINDOWS
 25       string str = string(key) + "=" + value;
 26       return _putenv(str.c_str());
 27 #  else
 28 
 29 # ifdef __sun__
 30       string str = string(key) + "=" + value;
 31       return putenv((char*)str.c_str()); // for SOLARIS
 32 # else
 33       return setenv(key, value, 1); // for UNIX
 34 #endif
 35 
 36 #  endif
 37 }
 38 
 39 
 40 /**
 41  * Tests the property class. 
 42  */
 43 class TestProperty: public TestSuite
 44 {
 45 private:
 46    //Global& glob;
 47 
 48 public:
 49 
 50    /**
 51     * Constructs the TestProperty object.
 52     * <p />
 53     * @param testName  The name used in the test suite
 54     * @param loginName The name to login to the xmlBlaster
 55     */
 56    TestProperty(int args, char *argc[]) : TestSuite(args, argc, "TestProperty", false)
 57                //, glob(Global::getInstance())
 58    {
 59    }
 60 
 61    virtual ~TestProperty() 
 62    {
 63    }
 64 
 65    /**
 66     * TEST: Construct a message and publish it. <p />
 67     * The returned publishOid is checked
 68     */
 69    void testDefault() 
 70    {
 71       if (log_.trace()) log_.trace(ME, "testDefault");
 72       string me = ME+".testDefault()";
 73       {
 74          Property prop;
 75          std::cout << "Property.testDefault()" << prop.toXml() << std::endl;
 76          assertEquals(log_, me, true, prop.propertyExists("user.home"), "user.home check");
 77          assertEquals(log_, me, true, prop.propertyExists("user.name"), "user.name check");
 78          assertEquals(log_, me, true, prop.propertyExists("file.separator"), "file.separator check");
 79          assertEquals(log_, me, true, prop.propertyExists("path.separator"), "path.separator check");
 80       }
 81       {  // Manipulate env ...
 82 
 83          const char *HOME = getenv("HOME");  // remember
 84          const char *HOMEDRIVE = getenv("HOMEDRIVE");
 85          const char *HOMEPATH = getenv("HOMEPATH");
 86          const char *USER = getenv("USER");
 87 
 88          assertEquals(log_, me, 0, dosetenv("HOME", "D:/BLA"), "setenv"); // for UNIX
 89          assertEquals(log_, me, 0, dosetenv("HOMEDRIVE", "D:"), "setenv"); // for Windows
 90          assertEquals(log_, me, 0, dosetenv("HOMEPATH", "/BLA"), "setenv"); // for Windows
 91 
 92          assertEquals(log_, me, 0, dosetenv("USER", "OIOI"), "setenv");
 93          
 94          Property prop;
 95          std::cout << "Property.testDefault()" << prop.toXml() << std::endl;
 96          assertEquals(log_, me, "D:/BLA", prop.get("user.home", ""), "user.home check");
 97          assertEquals(log_, me, "OIOI", prop.get("user.name", ""), "user.name check");
 98 
 99          // restore env
100          if (HOME) dosetenv("HOME", HOME);
101          if (HOMEDRIVE) dosetenv("HOMEDRIVE", HOMEDRIVE);
102          if (HOMEPATH) dosetenv("HOMEPATH", HOMEPATH);
103          if (USER) dosetenv("USER", USER);
104       }
105    }
106 
107    /**
108     * TEST: Construct a message and publish it. <p />
109     * The returned publishOid is checked
110     */
111    void testReplace() 
112    {
113       string me = ME+".testReplace()";
114       if (log_.trace()) log_.trace(me, "");
115       Property prop;
116       prop.setProperty("A", "aaa");
117       prop.setProperty("B", "bValue-${A}-bValue");
118       std::cout << "Property.testReplace()" << prop.toXml() << std::endl;
119       assertEquals(log_, me, "bValue-aaa-bValue", prop.get("B", ""), "${} check");
120    }
121 
122    /**
123     * TEST: Construct a message and publish it. <p />
124     * The returned publishOid is checked
125     */
126    void testLoadPropertyFile() 
127    {
128       string me = ME+".testLoadPropertyFile()";
129       if (log_.trace()) log_.trace(me, "");
130       Property prop;
131       prop.loadPropertyFile();
132       std::cout << "Property.testLoadPropertyFile()" << prop.toXml() << std::endl;
133       // TODO: How to test?
134    }
135 
136    /**
137     * Sets up the fixture. 
138     */
139    void setUp() 
140    {
141       TestSuite::setUp();
142    }
143 
144    /**
145     * Tears down the fixture. 
146     */
147    void tearDown() 
148    {
149       log_.info(ME, "Going to tear down.");
150       TestSuite::tearDown();
151    }
152 
153    void usage() const
154    {
155       TestSuite::usage();
156       log_.plain(ME, "Example:");
157       log_.plain(ME, "   TestProperty -A aValue -B bValue-${A}-bValue"); // TODO: support command line manual checks
158       log_.plain(ME, "----------------------------------------------------------");
159    }
160 };
161 
162 }}} // namespace
163 
164 using namespace org::xmlBlaster::test;
165 
166 int main(int args, char *argc[]) 
167 {
168    try {
169       org::xmlBlaster::util::Object_Lifetime_Manager::init();
170       TestProperty test(args, argc);
171 
172       test.setUp();
173       test.testDefault();
174       test.tearDown();
175 
176       test.setUp();
177       test.testReplace();
178       test.tearDown();
179 
180       test.setUp();
181       test.testLoadPropertyFile();
182       test.tearDown();
183    }
184    catch (XmlBlasterException& ex) {
185       std::cout << ex.toXml() << std::endl;
186    }
187    catch (bad_exception& ex) {
188       cout << "bad_exception: " << ex.what() << endl;
189    }
190    catch (exception& ex) {
191       cout << " exception: " << ex.what() << endl;
192    }
193    catch (string& ex) {
194       cout << "string: " << ex << endl;
195    }
196    catch (char* ex) {
197       cout << "char* :  " << ex << endl;
198    }
199 
200    catch (...)
201    {
202       cout << "unknown exception occured" << endl;
203       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
204       cout << e.toXml() << endl;
205    }
206 
207    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
208    return 0;
209 }


syntax highlighted by Code2HTML, v. 0.9.1