// FileGenericRepository_t.cxx #include "dataset_catalog/FileGenericRepository.h" #include "dataset_util/Environment.h" #include "dataset_id/UniqueIdGenerator.h" #include #include using std::string; using std::ostream; using std::cout; using std::endl; using dset::GenericRepository; using dset::FileGenericRepository; namespace { void msg(const char* msg) { cout << "--- "; cout << msg; cout << " ---" << endl; } } // end of unnamed namespace int FileGenericRepository_t() { msg("Begin testing FileGenericRepository"); msg("Try to open an invalid repository"); { FileGenericRepository filegr("abc", "junk", false); assert( ! filegr.is_valid() ); assert( filegr.error() != 0 ); } // Copy the repository files. string dir = "dat/"; int stat = 999; for ( int itry=0; itry<10; ++itry ) { string com = "test -d "; com += dir; stat = system(com.c_str()); if ( stat == 0 ) break; dir = "../" + dir; } if ( stat != 0 ) { cout << "Unable to find dat directory" << endl; assert(false); } system("rm -rf UniqueId"); string com = "cp -r " + dir + "/UniqueId ."; assert( system(com.c_str()) == 0 ); // Remove old repository. system("rm -rf mydb"); string sxml = "Test XML string"; string id = "123-456"; msg("Open the repository"); { FileGenericRepository filegr("dataset", "mydb", true); msg("Check validity"); if( !filegr.is_valid() ) { cout << "Invalid repository - Error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; assert(false); } cout << filegr << endl; msg("Write to repository"); if( filegr.insert(id, sxml) != 0 ) { cout << "Insert operation failed with error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } msg("Testing valid file "); if( !filegr.has(id) ) { cout << "Error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } msg("Read from repository"); string res = filegr.get(id); if( res.empty() ) { cout << "Read operation failed with error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } else cout << res; msg("Close the repository"); } msg("Open a new repository"); { FileGenericRepository filegr("dataset", "mydb"); if ( ! filegr.is_valid() ) { cout << "Invalid repository - Error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; assert(false); } cout << filegr << endl; msg("Check type"); cout << filegr.type() << endl; assert( FileGenericRepository::is_valid_type(filegr.type()) ); assert( ! FileGenericRepository::is_valid_type("xxx") ); msg("Read from repository"); string res = filegr.get(id); if( res.empty() ) { cout << "Read operation failed with error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } else cout << res ; msg("Retrieve all filenames from repository"); GenericRepository::IdList lst = filegr.get_ids(10); if(lst.empty()) { cout << "Retrieve failed with error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } else { for( GenericRepository::IdList::const_iterator it = lst.begin(); it != lst.end(); ++it ) cout << *it << endl; } msg("Check repository type"); if(filegr.repository_type().empty()) { cout << "Error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } else cout << filegr.repository_type() << endl; msg("Remove from repository"); if( filegr.remove(id) != 0 ) { cout << "Delete operation failed with error: " << filegr.error() << endl; cout << filegr.error_string(filegr.error()) << endl; } msg("Close the repository"); } msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return FileGenericRepository_t(); } #endif