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 "bz2T.h"
00012 #include "BESUncompressBZ2.h"
00013 #include "BESCache.h"
00014 #include "BESException.h"
00015 #include <test_config.h>
00016
00017 #define BES_CACHE_CHAR '#'
00018
00019 int
00020 bz2T::run(void)
00021 {
00022 cout << endl << "*****************************************" << endl;
00023 cout << "Entered bz2T::run" << endl;
00024 int retVal = 0;
00025
00026 #ifdef HAVE_BZLIB_H
00027 string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00028 string src_file = cache_dir + "/testfile.txt.bz2" ;
00029
00030
00031
00032 string target ;
00033 try
00034 {
00035 BESCache cache( cache_dir, "bz2_cache", 1 ) ;
00036
00037 if( cache.is_cached( src_file, target ) )
00038 {
00039 if( remove( target.c_str() ) != 0 )
00040 {
00041 cerr << "Unable to remove target file " << target
00042 << " , initializing test" << endl ;
00043 return 1 ;
00044 }
00045 }
00046
00047 cout << endl << "*****************************************" << endl;
00048 cout << "uncompress a test file" << endl;
00049 try
00050 {
00051 string result = BESUncompressBZ2::uncompress( src_file, target ) ;
00052 cout << "Uncompression succeeded" << endl ;
00053 if( result == target )
00054 {
00055 cout << "result is correct" << endl ;
00056 }
00057 else
00058 {
00059 cerr << "Resulting file " << result << " is not correct, "
00060 << "should be " << target << endl ;
00061 return 1 ;
00062 }
00063 ifstream strm( target.c_str() ) ;
00064 if( !strm )
00065 {
00066 cerr << "Resulting file " << result << " doesn't exist" << endl;
00067 return 1 ;
00068 }
00069 char line[80] ;
00070 strm.getline( (char *)line, 80 ) ;
00071 string sline = line ;
00072 if( sline != "This is a test of a compression method." )
00073 {
00074 cerr << "Contents of file not correct" << endl ;
00075 cerr << "Actual: " << sline << endl ;
00076 cerr << "Should be: This is a test of a compression method."
00077 << endl ;
00078 return 1 ;
00079 }
00080 else
00081 {
00082 cout << "Contents of file correct" << endl ;
00083 }
00084 }
00085 catch( BESException &e )
00086 {
00087 cerr << "Failed to uncompress the file" << endl ;
00088 cerr << e.get_message() << endl ;
00089 return 1 ;
00090 }
00091 catch( ... )
00092 {
00093 cerr << "Failed to uncompress the file" << endl ;
00094 cerr << "Unknown exception thrown" << endl ;
00095 return 1 ;
00096 }
00097
00098 string tmp ;
00099 if( cache.is_cached( src_file, tmp ) )
00100 {
00101 cout << "File is now cached" << endl ;
00102 }
00103 else
00104 {
00105 cerr << "File should be cached" << endl ;
00106 return 1 ;
00107 }
00108
00109 cout << endl << "*****************************************" << endl;
00110 cout << "uncompress a test file that is already cached" << endl;
00111 try
00112 {
00113 string result = BESUncompressBZ2::uncompress( src_file, target ) ;
00114 cout << "Uncompression succeeded" << endl ;
00115 if( result == target )
00116 {
00117 cout << "result is correct" << endl ;
00118 }
00119 else
00120 {
00121 cerr << "Resulting file " << result << " is not correct, "
00122 << "should be " << target << endl ;
00123 return 1 ;
00124 }
00125 ifstream strm( target.c_str() ) ;
00126 if( !strm )
00127 {
00128 cerr << "Resulting file " << result << " doesn't exist" << endl;
00129 return 1 ;
00130 }
00131 char line[80] ;
00132 strm.getline( (char *)line, 80 ) ;
00133 string sline = line ;
00134 if( sline != "This is a test of a compression method." )
00135 {
00136 cerr << "Contents of file not correct" << endl ;
00137 cerr << "Actual: " << sline << endl ;
00138 cerr << "Should be: This is a test of a compression method."
00139 << endl ;
00140 return 1 ;
00141 }
00142 else
00143 {
00144 cout << "Contents of file correct" << endl ;
00145 }
00146 }
00147 catch( BESException &e )
00148 {
00149 cerr << "Failed to uncompress the file" << endl ;
00150 cerr << e.get_message() << endl ;
00151 return 1 ;
00152 }
00153 catch( ... )
00154 {
00155 cerr << "Failed to uncompress the file" << endl ;
00156 cerr << "Unknown exception thrown" << endl ;
00157 return 1 ;
00158 }
00159
00160 if( cache.is_cached( src_file, tmp ) )
00161 {
00162 cout << "File is still cached" << endl ;
00163 }
00164 else
00165 {
00166 cerr << "File should be cached" << endl ;
00167 return 1 ;
00168 }
00169
00170 }
00171 catch( BESException &e )
00172 {
00173 cerr << "Unable to create the cache object" << endl ;
00174 cerr << e.get_message() << endl ;
00175 return 1 ;
00176 }
00177 catch( ... )
00178 {
00179 cerr << "Unable to create the cache object" << endl ;
00180 cerr << "Unknown exception thrown" << endl ;
00181 return 1 ;
00182 }
00183 #else
00184 cout << endl << "*****************************************" << endl;
00185 cout << "BZ2 not compiled in, not running test" << endl;
00186 #endif
00187
00188 cout << endl << "*****************************************" << endl;
00189 cout << "Returning from bz2T::run" << endl;
00190
00191 return retVal;
00192 }
00193
00194 int
00195 main(int argC, char **argV) {
00196 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/bes.conf" ;
00197 putenv( (char *)env_var.c_str() ) ;
00198 Application *app = new bz2T();
00199 return app->main(argC, argV);
00200 }
00201