00001
00002
00003 #include <stdlib.h>
00004 #include <iostream>
00005
00006 using std::cerr ;
00007 using std::cout ;
00008 using std::endl ;
00009
00010 #include "keysT.h"
00011 #include "TheBESKeys.h"
00012 #include "BESException.h"
00013 #include <test_config.h>
00014
00015 int keysT::
00016 initialize( int argC, char **argV )
00017 {
00018 if( argC == 2 )
00019 {
00020 _keyFile = argV[1] ;
00021 }
00022
00023 return baseApp::initialize( argC, argV ) ;
00024 }
00025
00026 int keysT::
00027 run(void)
00028 {
00029 cout << endl << "*****************************************" << endl;
00030 cout << "Entered keysT::run" << endl;
00031 int retVal = 0;
00032
00033 if( _keyFile != "" )
00034 {
00035 char envVal[256] ;
00036 sprintf( envVal, "BES_CONF=%s", _keyFile.c_str() ) ;
00037 putenv( envVal ) ;
00038 try
00039 {
00040 TheBESKeys::TheKeys()->show_keys() ;
00041 }
00042 catch( BESException &e )
00043 {
00044 cout << "unable to create BESKeys:" << endl ;
00045 cout << e.get_message() << endl ;
00046 }
00047 catch( ... )
00048 {
00049 cout << "unable to create BESKeys: unkown exception caught"
00050 << endl ;
00051 }
00052
00053 return 0 ;
00054 }
00055
00056 cout << endl << "*****************************************" << endl;
00057 cout << "no file set" << endl;
00058 putenv( "BES_CONF=" ) ;
00059 try
00060 {
00061 TheBESKeys::TheKeys() ;
00062 cerr << "created, should have not been created" << endl ;
00063 return 1 ;
00064 }
00065 catch( BESException &e )
00066 {
00067 cout << "unable to create BESKeys, good, because:" << endl ;
00068
00069
00070
00071 }
00072
00073 cout << endl << "*****************************************" << endl;
00074 cout << "notfound file set" << endl;
00075 putenv( "BES_CONF=notfound.ini" ) ;
00076 try
00077 {
00078 TheBESKeys::TheKeys() ;
00079 cerr << "created, should have not been created" << endl ;
00080 return 1 ;
00081 }
00082 catch( BESException &e )
00083 {
00084 cout << "unable to create BESKeys, good, because:" << endl ;
00085
00086
00087
00088 }
00089
00090 cout << endl << "*****************************************" << endl;
00091 cout << "bad keys, not enough equal signs" << endl;
00092 char *pwd = getenv( "PWD" ) ;
00093 string pwd_s ;
00094 if( !pwd )
00095 pwd_s = "." ;
00096 else
00097 pwd_s = pwd ;
00098 string env_s = (string)"BES_CONF=" + TEST_SRC_DIR + "/bad_keys1.ini" ;
00099 char env1[1024] ;
00100 sprintf( env1, "%s", env_s.c_str() ) ;
00101 putenv( env1 ) ;
00102 try
00103 {
00104 TheBESKeys::TheKeys() ;
00105 cerr << "created, should have not been created" << endl ;
00106 return 1 ;
00107 }
00108 catch( BESException &e )
00109 {
00110 cout << "unable to create BESKeys, good, because:" << endl ;
00111 cout << e.get_message() << endl ;
00112 }
00113
00114 cout << endl << "*****************************************" << endl;
00115 cout << "bad keys, too many equal signs" << endl;
00116 env_s = (string)"BES_CONF=" + TEST_SRC_DIR + "/bad_keys2.ini" ;
00117 char env2[1024] ;
00118 sprintf( env2, "%s", env_s.c_str() ) ;
00119 putenv( env2 ) ;
00120 try
00121 {
00122 TheBESKeys::TheKeys() ;
00123 cerr << "created, should have not been created" << endl ;
00124 return 1 ;
00125 }
00126 catch( BESException &e )
00127 {
00128 cout << "unable to create BESKeys, good, because:" << endl ;
00129 cout << e.get_message() << endl ;
00130 }
00131
00132 cout << endl << "*****************************************" << endl;
00133 cout << "good keys file, should load" << endl;
00134 env_s = (string)"BES_CONF=" + TEST_SRC_DIR + "/keys_test.ini" ;
00135 char env3[1024] ;
00136 sprintf( env3, "%s", env_s.c_str() ) ;
00137 putenv( env3 ) ;
00138 try
00139 {
00140 TheBESKeys::TheKeys() ;
00141 cout << "created, good" << endl ;
00142 }
00143 catch( BESException &e )
00144 {
00145 cerr << "unable to create BESKeys, because:" << endl ;
00146 cerr << e.get_message() << endl ;
00147 return 1 ;
00148 }
00149
00150 cout << endl << "*****************************************" << endl;
00151 cout << "get keys" << endl;
00152 bool found = false ;
00153 string ret = "" ;
00154 for( int i = 1; i < 4; i++ )
00155 {
00156 char key[32] ;
00157 sprintf( key, "BES.KEY%d", i ) ;
00158 char val[32] ;
00159 sprintf( val, "val%d", i ) ;
00160 cout << "looking for " << key << endl ;
00161 ret = "" ;
00162 ret = TheBESKeys::TheKeys()->get_key( key, found ) ;
00163 if( found == false )
00164 {
00165 cerr << key << " not found" << endl ;
00166 return 1 ;
00167 }
00168 if( ret == "" )
00169 {
00170 cerr << key << " set to \"\"" << endl ;
00171 return 1 ;
00172 }
00173 if( ret != val )
00174 {
00175 cerr << key << " = " << ret << ", but should = " << val << endl ;
00176 return 1 ;
00177 }
00178 else
00179 {
00180 cout << key << " = " << ret << endl ;
00181 }
00182 }
00183
00184 cout << endl << "*****************************************" << endl;
00185 cout << "look for non existant key" << endl;
00186 ret = TheBESKeys::TheKeys()->get_key( "BES.NOTFOUND", found ) ;
00187 if( found == true )
00188 {
00189 cerr << "found BES.NOTFOUND = \"" << ret << "\"" << endl ;
00190 return 1 ;
00191 }
00192 else
00193 {
00194 cout << "did not find BES.NOTFOUND" << endl ;
00195 }
00196
00197 cout << endl << "*****************************************" << endl;
00198 cout << "look for key with empty value" << endl;
00199 ret = TheBESKeys::TheKeys()->get_key( "BES.KEY4", found ) ;
00200 if( found == true )
00201 {
00202 if( ret == "" )
00203 {
00204 cout << "found and is empty" << endl ;
00205 }
00206 else
00207 {
00208 cerr << "found BES.NOTFOUND = \"" << ret << "\"" << endl ;
00209 return 1 ;
00210 }
00211 }
00212 else
00213 {
00214 cerr << "did not find BES.KEY4" << endl ;
00215 return 1 ;
00216 }
00217
00218 cout << endl << "*****************************************" << endl;
00219 cout << "set bad key, 0 = characters" << endl;
00220 try
00221 {
00222 ret = TheBESKeys::TheKeys()->set_key( "BES.NOEQS" ) ;
00223 cerr << "set_key successful with value \"" << ret << "\"" << endl ;
00224 return 1 ;
00225 }
00226 catch( BESException &e )
00227 {
00228 cout << "unable to set the key, good, because:" << endl ;
00229 cout << e.get_message() ;
00230 }
00231
00232 cout << endl << "*****************************************" << endl;
00233 cout << "set bad key, 2 = characters" << endl;
00234 try
00235 {
00236 ret = TheBESKeys::TheKeys()->set_key( "BES.2EQS=val1=val2" ) ;
00237 cerr << "set_key successful with value \"" << ret << "\"" << endl ;
00238 return 1 ;
00239 }
00240 catch( BESException &e )
00241 {
00242 cout << "unable to set the key, good, because:" << endl ;
00243 cout << e.get_message() ;
00244 }
00245
00246 cout << endl << "*****************************************" << endl;
00247 cout << "set BES.KEY5 to val5" << endl;
00248 try
00249 {
00250 ret = TheBESKeys::TheKeys()->set_key( "BES.KEY5=val5" ) ;
00251 if( ret == "val5" )
00252 {
00253 cout << "set_key successful" << endl ;
00254 }
00255 else
00256 {
00257 cerr << "set successfully, but incorrect val = \""
00258 << ret << "\"" << endl ;
00259 return 1 ;
00260 }
00261 }
00262 catch( BESException &e )
00263 {
00264 cerr << "unable to set the key, because:" << endl ;
00265 cerr << e.get_message() ;
00266 return 1 ;
00267 }
00268
00269 cout << endl << "*****************************************" << endl;
00270 cout << "set BES.KEY6 to val6" << endl;
00271 try
00272 {
00273 ret = TheBESKeys::TheKeys()->set_key( "BES.KEY6", "val6" ) ;
00274 if( ret == "val6" )
00275 {
00276 cout << "set_key successful" << endl ;
00277 }
00278 else
00279 {
00280 cerr << "set successfully, but incorrect val = \""
00281 << ret << "\"" << endl ;
00282 return 1 ;
00283 }
00284 }
00285 catch( BESException &e )
00286 {
00287 cerr << "unable to set the key, because:" << endl ;
00288 cerr << e.get_message() ;
00289 return 1 ;
00290 }
00291
00292 cout << endl << "*****************************************" << endl;
00293 cout << "get keys" << endl;
00294 for( int i = 1; i < 7; i++ )
00295 {
00296 char key[32] ;
00297 sprintf( key, "BES.KEY%d", i ) ;
00298 char val[32] ;
00299 if( i == 4 ) sprintf( val, "" ) ;
00300 else sprintf( val, "val%d", i ) ;
00301 cout << "looking for " << key << endl ;
00302 ret = "" ;
00303 ret = TheBESKeys::TheKeys()->get_key( key, found ) ;
00304 if( found == false )
00305 {
00306 cerr << key << " not found" << endl ;
00307 return 1 ;
00308 }
00309 if( ret != val )
00310 {
00311 cerr << key << " = " << ret << ", but should = " << val << endl ;
00312 return 1 ;
00313 }
00314 else
00315 {
00316 cout << key << " = " << ret << endl ;
00317 }
00318 }
00319
00320 cout << endl << "*****************************************" << endl;
00321 cout << "Returning from keysT::run" << endl;
00322
00323 return retVal;
00324 }
00325
00326 int
00327 main(int argC, char **argV) {
00328 Application *app = new keysT();
00329 return app->main(argC, argV);
00330 }
00331