testsuite/src/c++/TestProperty.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestProperty.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Testing the property class
00006 -----------------------------------------------------------------------------*/
00007 #include "TestSuite.h"
00008 #include <util/Property.h>
00009 #include <iostream>
00010 #include <stdlib.h> // setenv()
00011 
00012 using namespace std;
00013 using namespace org::xmlBlaster::util;
00014 
00015 namespace org { namespace xmlBlaster { namespace test {
00016 
00017 static int dosetenv(const char *key, const char *value);
00018 
00022 static int dosetenv(const char *key, const char *value)
00023 {
00024 #  ifdef _WINDOWS
00025       string str = string(key) + "=" + value;
00026       return _putenv(str.c_str());
00027 #  else
00028 
00029 # ifdef __sun__
00030       string str = string(key) + "=" + value;
00031       return putenv((char*)str.c_str()); // for SOLARIS
00032 # else
00033       return setenv(key, value, 1); // for UNIX
00034 #endif
00035 
00036 #  endif
00037 }
00038 
00039 
00043 class TestProperty: public TestSuite
00044 {
00045 private:
00046    //Global& glob;
00047 
00048 public:
00049 
00056    TestProperty(int args, char *argc[]) : TestSuite(args, argc, "TestProperty", false)
00057                //, glob(Global::getInstance())
00058    {
00059    }
00060 
00061    virtual ~TestProperty() 
00062    {
00063    }
00064 
00069    void testDefault() 
00070    {
00071       if (log_.trace()) log_.trace(ME, "testDefault");
00072       string me = ME+".testDefault()";
00073       {
00074          Property prop;
00075          std::cout << "Property.testDefault()" << prop.toXml() << std::endl;
00076          assertEquals(log_, me, true, prop.propertyExists("user.home"), "user.home check");
00077          assertEquals(log_, me, true, prop.propertyExists("user.name"), "user.name check");
00078          assertEquals(log_, me, true, prop.propertyExists("file.separator"), "file.separator check");
00079          assertEquals(log_, me, true, prop.propertyExists("path.separator"), "path.separator check");
00080       }
00081       {  // Manipulate env ...
00082 
00083          const char *HOME = getenv("HOME");  // remember
00084          const char *HOMEDRIVE = getenv("HOMEDRIVE");
00085          const char *HOMEPATH = getenv("HOMEPATH");
00086          const char *USER = getenv("USER");
00087 
00088          assertEquals(log_, me, 0, dosetenv("HOME", "D:/BLA"), "setenv"); // for UNIX
00089          assertEquals(log_, me, 0, dosetenv("HOMEDRIVE", "D:"), "setenv"); // for Windows
00090          assertEquals(log_, me, 0, dosetenv("HOMEPATH", "/BLA"), "setenv"); // for Windows
00091 
00092          assertEquals(log_, me, 0, dosetenv("USER", "OIOI"), "setenv");
00093          
00094          Property prop;
00095          std::cout << "Property.testDefault()" << prop.toXml() << std::endl;
00096          assertEquals(log_, me, "D:/BLA", prop.get("user.home", ""), "user.home check");
00097          assertEquals(log_, me, "OIOI", prop.get("user.name", ""), "user.name check");
00098 
00099          // restore env
00100          if (HOME) dosetenv("HOME", HOME);
00101          if (HOMEDRIVE) dosetenv("HOMEDRIVE", HOMEDRIVE);
00102          if (HOMEPATH) dosetenv("HOMEPATH", HOMEPATH);
00103          if (USER) dosetenv("USER", USER);
00104       }
00105    }
00106 
00111    void testReplace() 
00112    {
00113       string me = ME+".testReplace()";
00114       if (log_.trace()) log_.trace(me, "");
00115       Property prop;
00116       prop.setProperty("A", "aaa");
00117       prop.setProperty("B", "bValue-${A}-bValue");
00118       std::cout << "Property.testReplace()" << prop.toXml() << std::endl;
00119       assertEquals(log_, me, "bValue-aaa-bValue", prop.get("B", ""), "${} check");
00120    }
00121 
00126    void testLoadPropertyFile() 
00127    {
00128       string me = ME+".testLoadPropertyFile()";
00129       if (log_.trace()) log_.trace(me, "");
00130       Property prop;
00131       prop.loadPropertyFile();
00132       std::cout << "Property.testLoadPropertyFile()" << prop.toXml() << std::endl;
00133       // TODO: How to test?
00134    }
00135 
00139    void setUp() 
00140    {
00141       TestSuite::setUp();
00142    }
00143 
00147    void tearDown() 
00148    {
00149       log_.info(ME, "Going to tear down.");
00150       TestSuite::tearDown();
00151    }
00152 
00153    void usage() const
00154    {
00155       TestSuite::usage();
00156       log_.plain(ME, "Example:");
00157       log_.plain(ME, "   TestProperty -A aValue -B bValue-${A}-bValue"); // TODO: support command line manual checks
00158       log_.plain(ME, "----------------------------------------------------------");
00159    }
00160 };
00161 
00162 }}} // namespace
00163 
00164 using namespace org::xmlBlaster::test;
00165 
00166 int main(int args, char *argc[]) 
00167 {
00168    try {
00169       org::xmlBlaster::util::Object_Lifetime_Manager::init();
00170       TestProperty test(args, argc);
00171 
00172       test.setUp();
00173       test.testDefault();
00174       test.tearDown();
00175 
00176       test.setUp();
00177       test.testReplace();
00178       test.tearDown();
00179 
00180       test.setUp();
00181       test.testLoadPropertyFile();
00182       test.tearDown();
00183    }
00184    catch (XmlBlasterException& ex) {
00185       std::cout << ex.toXml() << std::endl;
00186    }
00187    catch (bad_exception& ex) {
00188       cout << "bad_exception: " << ex.what() << endl;
00189    }
00190    catch (exception& ex) {
00191       cout << " exception: " << ex.what() << endl;
00192    }
00193    catch (string& ex) {
00194       cout << "string: " << ex << endl;
00195    }
00196    catch (char* ex) {
00197       cout << "char* :  " << ex << endl;
00198    }
00199 
00200    catch (...)
00201    {
00202       cout << "unknown exception occured" << endl;
00203       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
00204       cout << e.toXml() << endl;
00205    }
00206 
00207    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00208    return 0;
00209 }
00210