// Task_t.cxx #include "dial_task/Task.h" #include #include #include #include #include #include "dataset_util/getcwd.h" #include "dataset_util/mkdir.h" #include "dataset_util/copy_file.h" #include "dataset_util/FileName.h" #include "dataset_util/FileStatus.h" #include "dataset_util/XmlElement.h" #include "dataset_util/DtdRegistry.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" using std::string; using std::cout; using std::endl; #if ( __GNUC__ != 2 ) using namespace std::rel_ops; #endif using dset::CredentialSelectionCatalog; using dset::GssCredentialManager; using dial::Task; namespace { void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } } // end unnamed namespace int Task_t() { msg("Begin testing Task"); system("rm -rf mycode.cxx config.dat tskdir"); string pwd = getcwd(); assert( getcwd() == pwd ); msg("Fetch default instance of CSC."); assert( CredentialSelectionCatalog::create_default_instance() == 0 ); CredentialSelectionCatalog& sc = CredentialSelectionCatalog::default_instance(); cout << sc << endl; assert( sc.is_valid() ); string owner = "me"; assert( GssCredentialManager::set_default() == 0 ); assert( GssCredentialManager::set_owner("me") == 0 ); msg("Create code"); Text code("mycode.cxx"); code.append("int myfun() {"); code.append(" return 123;"); code.append("}"); cout << code << endl; code.write(); msg("Create config"); Text conf("config.dat"); conf.append("min = -20.0"); conf.append("max = 40.0"); conf.append("nbin = 150"); cout << conf << endl; conf.write(); msg("Create task from files"); Task::NameList files; files.push_back("mycode.cxx"); files.push_back("config.dat"); Task tsk(files); cout << tsk << endl; assert( tsk.is_valid() ); assert( tsk.owner() == "me" ); assert( tsk.create_time() > 0 ); assert( tsk.files() == files ); assert( tsk.texts().size() == files.size() ); assert( getcwd() == pwd ); msg("Check file string"); cout << "mycode.cxx config.dat" << endl; cout << tsk.file_string() << endl; assert( tsk.file_string() == "mycode.cxx config.dat" ); msg("Check text for nosuchfile"); cout << tsk.text("nosuchfile") << endl; assert( tsk.text("nosuchfile").size() == 0 ); assert( tsk.text("nosuchfile").name() == "" ); msg("Check text for code"); cout << tsk.text("mycode.cxx") << endl; assert( tsk.text("mycode.cxx") == code ); assert( tsk.text("mycode.cxx").name() == "mycode.cxx" ); msg("Create task from file string in directory"); mkdir("tskdir"); assert( copy_file("mycode.cxx", "tskdir/mycode2.cxx") == 0 ); assert( copy_file("config.dat", "tskdir/config2.dat") == 0 ); Task tsk1("mycode2.cxx config2.dat", "tskdir"); cout << tsk1 << endl; assert( tsk1.is_valid() ); assert( tsk1.owner() == "me" ); assert( tsk1.create_time() > 0 ); Task::NameList files2; files2.push_back("mycode2.cxx"); files2.push_back("config2.dat"); assert( tsk1.files() == files2 ); assert( tsk1.texts().size() == files2.size() ); assert( tsk1.has("mycode2.cxx") ); assert( ! tsk1.has("mycode3.cxx") ); assert( getcwd() == pwd ); msg("Create task from *"); Task atsk("*", "tskdir"); assert( atsk.is_valid() ); assert( atsk.owner() == "me" ); assert( atsk.create_time() > 0 ); Task::NameList afiles; afiles.push_back("config2.dat"); afiles.push_back("mycode2.cxx"); assert( atsk.files() == afiles ); assert( atsk.texts().size() == atsk.files().size() ); assert( getcwd() == pwd ); msg("Create task from texts"); Task::TextList texts; texts.push_back(code); texts.push_back(conf); Task ttsk(texts); cout << ttsk << endl; assert( ttsk.is_valid() ); assert( ttsk.owner() == "me" ); assert( ttsk.create_time() > 0 ); assert( ttsk.files() == files ); assert( ttsk.texts().size() == files.size() ); assert( getcwd() == pwd ); msg("Copy task"); { Task tsk2 = tsk; cout << tsk2 << endl; assert( tsk2.is_valid() ); assert( tsk2.owner() == tsk.owner() ); assert( tsk2.create_time() == tsk.create_time() ); assert( tsk2.files() == files ); assert( tsk2.texts() == tsk.texts() ); } msg("Assign task"); { Task tsk2; assert( ! tsk2.is_valid() ); tsk2 = tsk; cout << tsk2 << endl; assert( tsk2.is_valid() ); assert( tsk2.owner() == tsk.owner() ); assert( tsk2.create_time() == tsk.create_time() ); assert( tsk2.files() == files ); assert( tsk2.texts() == tsk.texts() ); } msg("Try to write files without create flag"); FileStatus dstat("tskdir2"); FileStatus fstat("tskdir2/config.dat"); assert( ! dstat.exists() ); assert( ! fstat.exists() ); assert( tsk.write_files("tskdir2", false) != 0 ); dstat.update(); fstat.update(); assert( ! dstat.exists() ); msg("Write files with create flag"); assert( tsk.write_files("tskdir2", true) == 0 ); fstat.update(); assert( fstat.exists() ); msg("Try to overwrite files without overwrite flag"); unlink("tskdir2/config.dat"); fstat.update(); assert( ! fstat.exists() ); assert( tsk.write_files("tskdir2", false, false) != 0 ); fstat.update(); assert( ! fstat.exists() ); msg("Try to overwrite files with overwrite flag"); assert( tsk.write_files("tskdir2", false, true) == 0 ); fstat.update(); assert( fstat.exists() ); msg("Write to XML"); { const XmlElement* pele = tsk.xml(false); assert ( pele != 0 ); cout << *pele << endl; msg("Read from XML"); Task tsk2(*pele); cout << tsk2 << endl; assert( tsk2.is_valid() ); assert( tsk2.owner() == tsk.owner() ); assert( tsk2.create_time() == tsk.create_time() ); assert( tsk2.files() == files ); assert( tsk2.texts() == tsk.texts() ); delete pele; } msg("DTD"); cout << Task::dtd() << endl; assert( Task::dtd().size() != 0 ); assert( DtdRegistry::instance("dial").has_type("Task") ); msg("Display DTD."); DtdRegistry::display(cout) << endl; string fname = DtdRegistry::instance("dial").write(); assert( fname == "dial.dtd" ); cout << FileName(fname).fullpath() << endl; msg("Check test task"); cout << Task::test_instance() << endl; assert( Task::test_instance().is_valid() ); msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return Task_t(); } #endif