// Application_t.cxx #include "dial_app/Application.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::Application; namespace { void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } } // end unnamed namespace int Application_t() { msg("Begin testing Application"); system("rm -rf build_task run appdir appdir2"); 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 build_task"); Text txt1("build_task"); txt1.append("# build_task"); txt1.append(""); txt1.append("date"); txt1.append("ls"); cout << txt1 << endl; txt1.write(); msg("Create run"); Text txt2("run"); txt2.append("# run"); txt2.append(""); txt2.append("nbin = 150"); cout << txt2 << endl; txt2.write(); msg("Create application from files"); Application::NameList files; files.push_back("build_task"); files.push_back("run"); Application app(files); cout << app << endl; assert( app.is_valid() ); assert( app.files() == files ); assert( app.texts().size() == files.size() ); assert( getcwd() == pwd ); msg("Check owner"); cout << "Owner = " << app.owner() << endl; assert( app.owner() == owner ); msg("Check create time"); cout << "Create time = " << app.owner() << endl; assert( app.create_time() > 0 ); msg("Check file string"); cout << "build_task run" << endl; cout << app.file_string() << endl; assert( app.file_string() == "build_task run" ); assert( app.has("run") ); assert( app.has("build_task") ); assert( ! app.has("run2") ); msg("Check text for nosuchfile"); cout << app.text("nosuchfile") << endl; assert( app.text("nosuchfile").size() == 0 ); assert( app.text("nosuchfile").name() == "" ); msg("Check text for run"); cout << app.text("run") << endl; assert( app.text("run") == txt2 ); assert( app.text("run").name() == "run" ); msg("Create app from file string in directory"); mkdir("appdir"); assert( copy_file("build_task", "appdir/build_task") == 0 ); assert( copy_file("run", "appdir/run") == 0 ); Application app1("build_task run", "appdir"); cout << app1 << endl; assert( app1.is_valid() ); assert( app1.owner() == owner ); assert( app1.create_time() > 0 ); Application::NameList files2; files2.push_back("build_task"); files2.push_back("run"); assert( app1.files() == files2 ); assert( app1.texts().size() == files2.size() ); assert( getcwd() == pwd ); msg("Create app from all files in a directory"); Application app3("*", "appdir"); cout << app3 << endl; assert( app3.is_valid() ); assert( app3.owner() == owner ); assert( app3.create_time() > 0 ); assert( app3.files() == files2 ); assert( app3.texts().size() == files2.size() ); assert( getcwd() == pwd ); msg("Create application from texts"); Application::TextList texts; texts.push_back(txt1); texts.push_back(txt2); Application tapp(texts); cout << tapp << endl; assert( tapp.is_valid() ); assert( tapp.owner() == owner ); assert( tapp.create_time() > 0 ); assert( tapp.files() == files ); assert( tapp.texts().size() == files.size() ); assert( getcwd() == pwd ); msg("Copy application"); { Application app2 = app; cout << app2 << endl; assert( app2.is_valid() ); assert( app2.owner() == app.owner() ); assert( app2.create_time() == app.create_time() ); assert( app2.files() == files ); assert( app2.texts() == app.texts() ); } msg("Assign application"); { Application app2; assert( ! app2.is_valid() ); app2 = app; cout << app2 << endl; assert( app2.is_valid() ); assert( app2.owner() == app.owner() ); assert( app2.create_time() == app.create_time() ); assert( app2.files() == files ); assert( app2.texts() == app.texts() ); } msg("Try to write files without create flag"); FileStatus dstat("appdir2"); FileStatus fstat("appdir2/run"); assert( ! dstat.exists() ); assert( ! fstat.exists() ); assert( app.write_files("appdir2", false) != 0 ); dstat.update(); fstat.update(); assert( ! dstat.exists() ); assert( app.output_directory().size() == 0 ); msg("Write files with create flag"); assert( app.write_files("appdir2", true) == 0 ); fstat.update(); assert( fstat.exists() ); assert( app.output_directory().size() ); msg("Try to overwrite files without overwrite flag"); unlink("appdir2/run"); fstat.update(); assert( ! fstat.exists() ); assert( app.write_files("appdir2", false, false) != 0 ); fstat.update(); assert( ! fstat.exists() ); assert( app.output_directory().size() == 0 ); msg("Try to overwrite files with overwrite flag"); assert( app.write_files("appdir2", false, true) == 0 ); fstat.update(); assert( fstat.exists() ); assert( app.output_directory().size() ); msg("Write to XML"); { const XmlElement* pele = app.xml(false); assert ( pele != 0 ); cout << *pele << endl; msg("Read from XML"); Application app2(*pele); cout << app2 << endl; assert( app2.is_valid() ); assert( app2.id() == app.id() ); assert( app2.owner() == app.owner() ); assert( app2.create_time() == app.create_time() ); assert( app2.files() == files ); assert( app2.texts() == app.texts() ); delete pele; } msg("DTD"); cout << Application::dtd() << endl; assert( Application::dtd().size() != 0 ); assert( DtdRegistry::instance("dial").has_type("Application") ); 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 application"); cout << Application::test_instance() << endl; assert( Application::test_instance().is_valid() ); msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return Application_t(); } #endif