00001
00002
00003 #include <iostream>
00004
00005 using std::cerr ;
00006 using std::cout ;
00007 using std::endl ;
00008
00009 #include "containerT.h"
00010 #include "TheBESKeys.h"
00011 #include "BESContainerStorageList.h"
00012 #include "BESContainer.h"
00013 #include "BESContainerStorage.h"
00014 #include "BESContainerStorageFile.h"
00015 #include "BESCache.h"
00016 #include "BESException.h"
00017 #include <test_config.h>
00018
00019 int containerT::
00020 run(void)
00021 {
00022 cout << endl << "*****************************************" << endl;
00023 cout << "Entered containerT::run" << endl;
00024 int retVal = 0;
00025
00026
00027
00028
00029 try
00030 {
00031 string key = (string)"BES.Container.Persistence.File.TheFile=" +
00032 TEST_SRC_DIR + "/container01.file" ;
00033 TheBESKeys::TheKeys()->set_key( key ) ;
00034 BESContainerStorageList::TheList()->add_persistence( new BESContainerStorageFile( "TheFile" ) ) ;
00035 }
00036 catch( BESException &e )
00037 {
00038 cerr << "couldn't add storage to storage list:" << endl ;
00039 cerr << e.get_message() << endl ;
00040 return 1 ;
00041 }
00042
00043 cout << endl << "*****************************************" << endl;
00044 cout << "try to find symbolic name that doesn't exist, default" << endl;
00045 try
00046 {
00047 BESContainer c( "nosym" ) ;
00048 BESContainerStorageList::TheList()->look_for( c ) ;
00049 cerr << "Found nosym, shouldn't have" << endl ;
00050 if( c.is_valid() == true )
00051 cerr << "container is valid, should not be" << endl ;
00052 cerr << " real_name = " << c.get_real_name() << endl ;
00053 cerr << " constraint = " << c.get_constraint() << endl ;
00054 cerr << " sym_name = " << c.get_symbolic_name() << endl ;
00055 cerr << " container type = " << c.get_container_type() << endl ;
00056 return 1 ;
00057 }
00058 catch( BESException &e )
00059 {
00060 cout << "caught exception, didn't find nosym, good" << endl ;
00061 cout << e.get_message() << endl ;
00062 }
00063
00064 cout << endl << "*****************************************" << endl;
00065 cout << "try to find symbolic name that does exist, default" << endl;
00066 try
00067 {
00068 BESContainer c( "sym1" ) ;
00069 BESContainerStorageList::TheList()->look_for( c ) ;
00070 cout << "found sym1" << endl ;
00071 if( c.is_valid() == false )
00072 {
00073 cerr << "is not valid though" << endl ;
00074 return 1 ;
00075 }
00076 if( c.get_symbolic_name() != "sym1" )
00077 {
00078 cerr << "symbolic name != sym1, " << c.get_symbolic_name()
00079 << endl ;
00080 return 1 ;
00081 }
00082 if( c.get_real_name() != "real1" )
00083 {
00084 cerr << "real name != real1, " << c.get_real_name()
00085 << endl ;
00086 return 1 ;
00087 }
00088 if( c.get_container_type() != "type1" )
00089 {
00090 cerr << "real name != type1, " << c.get_container_type()
00091 << endl ;
00092 return 1 ;
00093 }
00094 }
00095 catch( BESException &e )
00096 {
00097 cerr << "didn't find sym1, should have" << endl ;
00098 return 1 ;
00099 }
00100
00101 cout << endl << "*****************************************" << endl;
00102 cout << "set to strict" << endl;
00103 TheBESKeys::TheKeys()->set_key( "BES.Container.Persistence=strict" ) ;
00104
00105 cout << endl << "*****************************************" << endl;
00106 cout << "try to find symbolic name that doesn't exist, strict" << endl;
00107 try
00108 {
00109 BESContainer c( "nosym" ) ;
00110 BESContainerStorageList::TheList()->look_for( c ) ;
00111 cerr << "Found nosym, shouldn't have" << endl ;
00112 if( c.is_valid() == true )
00113 cerr << "container is valid, should not be" << endl ;
00114 cerr << " real_name = " << c.get_real_name() << endl ;
00115 cerr << " constraint = " << c.get_constraint() << endl ;
00116 cerr << " sym_name = " << c.get_symbolic_name() << endl ;
00117 cerr << " container type = " << c.get_container_type() << endl ;
00118 return 1 ;
00119 }
00120 catch( BESException &e )
00121 {
00122 cout << "caught exception, didn't find nosym, good" << endl ;
00123 cout << e.get_message() << endl ;
00124 }
00125
00126 cout << endl << "*****************************************" << endl;
00127 cout << "try to find symbolic name that does exist, strict" << endl;
00128 try
00129 {
00130 BESContainer c( "sym1" ) ;
00131 BESContainerStorageList::TheList()->look_for( c ) ;
00132 cout << "found sym1" << endl ;
00133 if( c.is_valid() == false )
00134 {
00135 cerr << "is not valid though" << endl ;
00136 return 1 ;
00137 }
00138 if( c.get_symbolic_name() != "sym1" )
00139 {
00140 cerr << "symbolic name != sym1, " << c.get_symbolic_name()
00141 << endl ;
00142 return 1 ;
00143 }
00144 if( c.get_real_name() != "real1" )
00145 {
00146 cerr << "real name != real1, " << c.get_real_name()
00147 << endl ;
00148 return 1 ;
00149 }
00150 if( c.get_container_type() != "type1" )
00151 {
00152 cerr << "real name != type1, " << c.get_container_type()
00153 << endl ;
00154 return 1 ;
00155 }
00156 }
00157 catch( BESException &e )
00158 {
00159 cerr << "didn't find sym1, should have" << endl ;
00160 return 1 ;
00161 }
00162
00163 cout << endl << "*****************************************" << endl;
00164 cout << "set to nice" << endl;
00165 TheBESKeys::TheKeys()->set_key( "BES.Container.Persistence=nice" ) ;
00166
00167 cout << endl << "*****************************************" << endl;
00168 cout << "try to find symbolic name that doesn't exist, nice" << endl;
00169 try
00170 {
00171 BESContainer c( "nosym" ) ;
00172 BESContainerStorageList::TheList()->look_for( c ) ;
00173 if( c.is_valid() == true )
00174 {
00175 cerr << "Found nosym, shouldn't have" << endl ;
00176 cerr << " real_name = " << c.get_real_name() << endl ;
00177 cerr << " constraint = " << c.get_constraint() << endl ;
00178 cerr << " sym_name = " << c.get_symbolic_name() << endl ;
00179 cerr << " container type = " << c.get_container_type() << endl ;
00180 return 1 ;
00181 }
00182 else
00183 {
00184 cout << "didn't find nosym, didn't throw exception, good" << endl ;
00185 }
00186 }
00187 catch( BESException &e )
00188 {
00189 cerr << "caught exception, shouldn't have" << endl ;
00190 cerr << e.get_message() << endl ;
00191 return 1 ;
00192 }
00193
00194 cout << endl << "*****************************************" << endl;
00195 cout << "try to find symbolic name that does exist, nice" << endl;
00196 try
00197 {
00198 BESContainer c( "sym1" ) ;
00199 BESContainerStorageList::TheList()->look_for( c ) ;
00200 if( c.is_valid() == false )
00201 {
00202 cerr << "is not valid though" << endl ;
00203 return 1 ;
00204 }
00205 if( c.get_symbolic_name() != "sym1" )
00206 {
00207 cerr << "symbolic name != sym1, " << c.get_symbolic_name()
00208 << endl ;
00209 return 1 ;
00210 }
00211 if( c.get_real_name() != "real1" )
00212 {
00213 cerr << "real name != real1, " << c.get_real_name()
00214 << endl ;
00215 return 1 ;
00216 }
00217 if( c.get_container_type() != "type1" )
00218 {
00219 cerr << "real name != type1, " << c.get_container_type()
00220 << endl ;
00221 return 1 ;
00222 }
00223 }
00224 catch( BESException &e )
00225 {
00226 cerr << "didn't find sym1, should have" << endl ;
00227 return 1 ;
00228 }
00229
00230 string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00231 string src_file = cache_dir + "/testfile.txt" ;
00232 string com_file = cache_dir + "/testfile.txt.gz" ;
00233
00234 TheBESKeys::TheKeys()->set_key( "BES.CacheDir", cache_dir ) ;
00235 TheBESKeys::TheKeys()->set_key( "BES.CachePrefix", "cont_cache" ) ;
00236 TheBESKeys::TheKeys()->set_key( "BES.CacheSize", "1" ) ;
00237
00238 string chmod = (string)"chmod a+w " + TEST_SRC_DIR + "/cache" ;
00239 system( chmod.c_str() ) ;
00240
00241 cout << endl << "*****************************************" << endl;
00242 cout << "access a non compressed file" << endl;
00243 try
00244 {
00245 BESContainer c( "sym" ) ;
00246 c.set_real_name( src_file ) ;
00247 c.set_container_type( "txt" ) ;
00248
00249 string result = c.access() ;
00250 if( result != src_file )
00251 {
00252 cerr << "result " << result << " does not match src "
00253 << src_file << endl ;
00254 return 1 ;
00255 }
00256 else
00257 {
00258 cout << "result matches src" << endl ;
00259 }
00260 }
00261 catch( BESException &e )
00262 {
00263 cerr << "Failed to access non compressed file" << endl ;
00264 cerr << e.get_message() << endl ;
00265 return 1 ;
00266 }
00267 catch( ... )
00268 {
00269 cerr << "Failed to access non compressed file" << endl ;
00270 cerr << "Unknown error" << endl ;
00271 return 1 ;
00272 }
00273
00274 cout << endl << "*****************************************" << endl;
00275 cout << "access a compressed file" << endl;
00276 try
00277 {
00278 BESCache cache( *(TheBESKeys::TheKeys()),
00279 "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00280 string target ;
00281 bool is_it = cache.is_cached( com_file, target ) ;
00282 if( is_it )
00283 {
00284 if( remove( target.c_str() ) != 0 )
00285 {
00286 cerr << "Unable to remove target file " << target
00287 << " , initializing test" << endl ;
00288 return 1 ;
00289 }
00290 }
00291
00292 BESContainer c( "sym" ) ;
00293 c.set_real_name( com_file ) ;
00294 c.set_container_type( "txt" ) ;
00295
00296 string result = c.access() ;
00297 if( result != target )
00298 {
00299 cerr << "result " << result << " does not match target "
00300 << target << endl ;
00301 return 1 ;
00302 }
00303 else
00304 {
00305 cout << "result matches src" << endl ;
00306 }
00307
00308 if( cache.is_cached( com_file, target ) )
00309 {
00310 cout << "file is now cached" << endl ;
00311 }
00312 else
00313 {
00314 cerr << "file should be cached in " << target << endl ;
00315 return 1 ;
00316 }
00317 }
00318 catch( BESException &e )
00319 {
00320 cerr << "Failed to access compressed file" << endl ;
00321 cerr << e.get_message() << endl ;
00322 return 1 ;
00323 }
00324 catch( ... )
00325 {
00326 cerr << "Failed to access compressed file" << endl ;
00327 cerr << "Unknown error" << endl ;
00328 return 1 ;
00329 }
00330
00331 cout << endl << "*****************************************" << endl;
00332 cout << "Returning from containerT::run" << endl;
00333
00334 return retVal;
00335 }
00336
00337 int
00338 main(int argC, char **argV) {
00339 Application *app = new containerT();
00340 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/empty.ini" ;
00341 putenv( (char *)env_var.c_str() ) ;
00342 return app->main(argC, argV);
00343 }
00344