00001
00002
00003 #include <iostream>
00004 #include <fstream>
00005
00006 using std::cerr ;
00007 using std::cout ;
00008 using std::endl ;
00009 using std::ifstream ;
00010
00011 #include "uncompressT.h"
00012 #include "BESUncompressManager.h"
00013 #include "BESCache.h"
00014 #include "BESException.h"
00015 #include <test_config.h>
00016
00017 #define BES_CACHE_CHAR '#'
00018
00019 int
00020 uncompressT::run(void)
00021 {
00022 cout << endl << "*****************************************" << endl;
00023 cout << "Entered uncompressT::run" << endl;
00024 int retVal = 0;
00025
00026 string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00027 string src_file = cache_dir + "/testfile.txt.gz" ;
00028
00029
00030
00031 string target ;
00032 try
00033 {
00034 BESCache cache( cache_dir, "gz_cache", 1 ) ;
00035
00036 if( cache.is_cached( src_file, target ) )
00037 {
00038 if( remove( target.c_str() ) != 0 )
00039 {
00040 cerr << "Unable to remove target file " << target
00041 << " , initializing test" << endl ;
00042 return 1 ;
00043 }
00044 }
00045
00046 cout << endl << "*****************************************" << endl;
00047 cout << "uncompress a test gz file" << endl;
00048 try
00049 {
00050 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00051 cout << "Uncompression succeeded" << endl ;
00052 if( result == target )
00053 {
00054 cout << "result is correct" << endl ;
00055 }
00056 else
00057 {
00058 cerr << "Resulting file " << result << " is not correct, "
00059 << "should be " << target << endl ;
00060 return 1 ;
00061 }
00062 ifstream strm( target.c_str() ) ;
00063 if( !strm )
00064 {
00065 cerr << "Resulting file " << result << " doesn't exist" << endl;
00066 return 1 ;
00067 }
00068 char line[80] ;
00069 strm.getline( (char *)line, 80 ) ;
00070 string sline = line ;
00071 if( sline != "This is a test of a compression method." )
00072 {
00073 cerr << "Contents of file not correct" << endl ;
00074 cerr << "Actual: " << sline << endl ;
00075 cerr << "Should be: This is a test of a compression method."
00076 << endl ;
00077 return 1 ;
00078 }
00079 else
00080 {
00081 cout << "Contents of file correct" << endl ;
00082 }
00083 }
00084 catch( BESException &e )
00085 {
00086 cerr << "Failed to uncompress the file" << endl ;
00087 cerr << e.get_message() << endl ;
00088 return 1 ;
00089 }
00090 catch( ... )
00091 {
00092 cerr << "Failed to uncompress the file" << endl ;
00093 cerr << "Unknown exception thrown" << endl ;
00094 return 1 ;
00095 }
00096
00097 string tmp ;
00098 if( cache.is_cached( src_file, tmp ) )
00099 {
00100 cout << "File is now cached" << endl ;
00101 }
00102 else
00103 {
00104 cerr << "File should be cached" << endl ;
00105 return 1 ;
00106 }
00107
00108 cout << endl << "*****************************************" << endl;
00109 cout << "uncompress a test gz file, should be cached" << endl;
00110 try
00111 {
00112 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00113 cout << "Uncompression succeeded" << endl ;
00114 if( result == target )
00115 {
00116 cout << "result is correct" << endl ;
00117 }
00118 else
00119 {
00120 cerr << "Resulting file " << result << " is not correct, "
00121 << "should be " << target << endl ;
00122 return 1 ;
00123 }
00124 ifstream strm( target.c_str() ) ;
00125 if( !strm )
00126 {
00127 cerr << "Resulting file " << result << " doesn't exist" << endl;
00128 return 1 ;
00129 }
00130 char line[80] ;
00131 strm.getline( (char *)line, 80 ) ;
00132 string sline = line ;
00133 if( sline != "This is a test of a compression method." )
00134 {
00135 cerr << "Contents of file not correct" << endl ;
00136 cerr << "Actual: " << sline << endl ;
00137 cerr << "Should be: This is a test of a compression method."
00138 << endl ;
00139 return 1 ;
00140 }
00141 else
00142 {
00143 cout << "Contents of file correct" << endl ;
00144 }
00145 }
00146 catch( BESException &e )
00147 {
00148 cerr << "Failed to uncompress the file" << endl ;
00149 cerr << e.get_message() << endl ;
00150 return 1 ;
00151 }
00152 catch( ... )
00153 {
00154 cerr << "Failed to uncompress the file" << endl ;
00155 cerr << "Unknown exception thrown" << endl ;
00156 return 1 ;
00157 }
00158
00159 if( cache.is_cached( src_file, tmp ) )
00160 {
00161 cout << "File is still cached" << endl ;
00162 }
00163 else
00164 {
00165 cerr << "File should be cached" << endl ;
00166 return 1 ;
00167 }
00168 }
00169 catch( BESException &e )
00170 {
00171 cerr << "Unable to create the gz cache object" << endl ;
00172 cerr << e.get_message() << endl ;
00173 return 1 ;
00174 }
00175 catch( ... )
00176 {
00177 cerr << "Unable to create the gz cache object" << endl ;
00178 cerr << "Unknown exception thrown" << endl ;
00179 return 1 ;
00180 }
00181
00182 src_file = cache_dir + "/testfile.txt.bz2" ;
00183 try
00184 {
00185 BESCache cache( cache_dir, "bz2_cache", 1 ) ;
00186
00187 if( cache.is_cached( src_file, target ) )
00188 {
00189 if( remove( target.c_str() ) != 0 )
00190 {
00191 cerr << "Unable to remove target file " << target
00192 << " , initializing test" << endl ;
00193 return 1 ;
00194 }
00195 }
00196
00197 cout << endl << "*****************************************" << endl;
00198 cout << "uncompress a test bz2 file" << endl;
00199 try
00200 {
00201 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00202 cout << "Uncompression succeeded" << endl ;
00203 if( result == target )
00204 {
00205 cout << "result is correct" << endl ;
00206 }
00207 else
00208 {
00209 cerr << "Resulting file " << result << " is not correct, "
00210 << "should be " << target << endl ;
00211 return 1 ;
00212 }
00213 ifstream strm( target.c_str() ) ;
00214 if( !strm )
00215 {
00216 cerr << "Resulting file " << result << " doesn't exist" << endl;
00217 return 1 ;
00218 }
00219 char line[80] ;
00220 strm.getline( (char *)line, 80 ) ;
00221 string sline = line ;
00222 if( sline != "This is a test of a compression method." )
00223 {
00224 cerr << "Contents of file not correct" << endl ;
00225 cerr << "Actual: " << sline << endl ;
00226 cerr << "Should be: This is a test of a compression method."
00227 << endl ;
00228 return 1 ;
00229 }
00230 else
00231 {
00232 cout << "Contents of file correct" << endl ;
00233 }
00234 }
00235 catch( BESException &e )
00236 {
00237 cerr << "Failed to uncompress the file" << endl ;
00238 cerr << e.get_message() << endl ;
00239 return 1 ;
00240 }
00241 catch( ... )
00242 {
00243 cerr << "Failed to uncompress the file" << endl ;
00244 cerr << "Unknown exception thrown" << endl ;
00245 return 1 ;
00246 }
00247
00248 string tmp ;
00249 if( cache.is_cached( src_file, tmp ) )
00250 {
00251 cout << "File is now cached" << endl ;
00252 }
00253 else
00254 {
00255 cerr << "File should be cached" << endl ;
00256 return 1 ;
00257 }
00258
00259 cout << endl << "*****************************************" << endl;
00260 cout << "uncompress a test bz2 file, should be cached" << endl;
00261 try
00262 {
00263 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00264 cout << "Uncompression succeeded" << endl ;
00265 if( result == target )
00266 {
00267 cout << "result is correct" << endl ;
00268 }
00269 else
00270 {
00271 cerr << "Resulting file " << result << " is not correct, "
00272 << "should be " << target << endl ;
00273 return 1 ;
00274 }
00275 ifstream strm( target.c_str() ) ;
00276 if( !strm )
00277 {
00278 cerr << "Resulting file " << result << " doesn't exist" << endl;
00279 return 1 ;
00280 }
00281 char line[80] ;
00282 strm.getline( (char *)line, 80 ) ;
00283 string sline = line ;
00284 if( sline != "This is a test of a compression method." )
00285 {
00286 cerr << "Contents of file not correct" << endl ;
00287 cerr << "Actual: " << sline << endl ;
00288 cerr << "Should be: This is a test of a compression method."
00289 << endl ;
00290 return 1 ;
00291 }
00292 else
00293 {
00294 cout << "Contents of file correct" << endl ;
00295 }
00296 }
00297 catch( BESException &e )
00298 {
00299 cerr << "Failed to uncompress the file" << endl ;
00300 cerr << e.get_message() << endl ;
00301 return 1 ;
00302 }
00303 catch( ... )
00304 {
00305 cerr << "Failed to uncompress the file" << endl ;
00306 cerr << "Unknown exception thrown" << endl ;
00307 return 1 ;
00308 }
00309
00310 }
00311 catch( BESException &e )
00312 {
00313 cerr << "Unable to create the bz2 cache object" << endl ;
00314 cerr << e.get_message() << endl ;
00315 return 1 ;
00316 }
00317 catch( ... )
00318 {
00319 cerr << "Unable to create the bz2 cache object" << endl ;
00320 cerr << "Unknown exception thrown" << endl ;
00321 return 1 ;
00322 }
00323
00324 src_file = cache_dir + "/testfile.txt.Z" ;
00325 try
00326 {
00327 BESCache cache( cache_dir, "z_cache", 1 ) ;
00328
00329 if( cache.is_cached( src_file, target ) )
00330 {
00331 if( remove( target.c_str() ) != 0 )
00332 {
00333 cerr << "Unable to remove target file " << target
00334 << " , initializing test" << endl ;
00335 return 1 ;
00336 }
00337 }
00338
00339 cout << endl << "*****************************************" << endl;
00340 cout << "uncompress a test z file" << endl;
00341 try
00342 {
00343 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00344 cout << "Uncompression succeeded" << endl ;
00345 if( result == target )
00346 {
00347 cout << "result is correct" << endl ;
00348 }
00349 else
00350 {
00351 cerr << "Resulting file " << result << " is not correct, "
00352 << "should be " << target << endl ;
00353 return 1 ;
00354 }
00355 ifstream strm( target.c_str() ) ;
00356 if( !strm )
00357 {
00358 cerr << "Resulting file " << result << " doesn't exist" << endl;
00359 return 1 ;
00360 }
00361 char line[80] ;
00362 strm.getline( (char *)line, 80 ) ;
00363 string sline = line ;
00364 if( sline != "This is a test of a compression method." )
00365 {
00366 cerr << "Contents of file not correct" << endl ;
00367 cerr << "Actual: " << sline << endl ;
00368 cerr << "Should be: This is a test of a compression method."
00369 << endl ;
00370 return 1 ;
00371 }
00372 else
00373 {
00374 cout << "Contents of file correct" << endl ;
00375 }
00376 }
00377 catch( BESException &e )
00378 {
00379 cerr << "Failed to uncompress the file" << endl ;
00380 cerr << e.get_message() << endl ;
00381 return 1 ;
00382 }
00383 catch( ... )
00384 {
00385 cerr << "Failed to uncompress the file" << endl ;
00386 cerr << "Unknown exception thrown" << endl ;
00387 return 1 ;
00388 }
00389
00390 string tmp ;
00391 if( cache.is_cached( src_file, tmp ) )
00392 {
00393 cout << "File is now cached" << endl ;
00394 }
00395 else
00396 {
00397 cerr << "File should be cached" << endl ;
00398 return 1 ;
00399 }
00400
00401 cout << endl << "*****************************************" << endl;
00402 cout << "uncompress a test z file, should be cached" << endl;
00403 try
00404 {
00405 string result = BESUncompressManager::TheManager()->uncompress( src_file, cache ) ;
00406 cout << "Uncompression succeeded" << endl ;
00407 if( result == target )
00408 {
00409 cout << "result is correct" << endl ;
00410 }
00411 else
00412 {
00413 cerr << "Resulting file " << result << " is not correct, "
00414 << "should be " << target << endl ;
00415 return 1 ;
00416 }
00417 ifstream strm( target.c_str() ) ;
00418 if( !strm )
00419 {
00420 cerr << "Resulting file " << result << " doesn't exist" << endl;
00421 return 1 ;
00422 }
00423 char line[80] ;
00424 strm.getline( (char *)line, 80 ) ;
00425 string sline = line ;
00426 if( sline != "This is a test of a compression method." )
00427 {
00428 cerr << "Contents of file not correct" << endl ;
00429 cerr << "Actual: " << sline << endl ;
00430 cerr << "Should be: This is a test of a compression method."
00431 << endl ;
00432 return 1 ;
00433 }
00434 else
00435 {
00436 cout << "Contents of file correct" << endl ;
00437 }
00438 }
00439 catch( BESException &e )
00440 {
00441 cerr << "Failed to uncompress the file" << endl ;
00442 cerr << e.get_message() << endl ;
00443 return 1 ;
00444 }
00445 catch( ... )
00446 {
00447 cerr << "Failed to uncompress the file" << endl ;
00448 cerr << "Unknown exception thrown" << endl ;
00449 return 1 ;
00450 }
00451
00452 }
00453 catch( BESException &e )
00454 {
00455 cerr << "Unable to create the z cache object" << endl ;
00456 cerr << e.get_message() << endl ;
00457 return 1 ;
00458 }
00459 catch( ... )
00460 {
00461 cerr << "Unable to create the z cache object" << endl ;
00462 cerr << "Unknown exception thrown" << endl ;
00463 return 1 ;
00464 }
00465
00466 cout << endl << "*****************************************" << endl;
00467 cout << "Returning from uncompressT::run" << endl;
00468
00469 return retVal;
00470 }
00471
00472 int
00473 main(int argC, char **argV) {
00474 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/bes.conf" ;
00475 putenv( (char *)env_var.c_str() ) ;
00476 Application *app = new uncompressT();
00477 return app->main(argC, argV);
00478 }
00479