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 cout << endl << "*****************************************" << endl;
00325 cout << "Returning from uncompressT::run" << endl;
00326
00327 return retVal;
00328 }
00329
00330 int
00331 main(int argC, char **argV) {
00332 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/bes.conf" ;
00333 putenv( (char *)env_var.c_str() ) ;
00334 Application *app = new uncompressT();
00335 return app->main(argC, argV);
00336 }
00337