// dataset_insert_t.cxx #include #include #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/getcwd.h" #include "dataset_util/mkdir.h" #include "dataset_util/getcwd.h" #include "dataset_id/SimpleUniqueIdGenerator.h" #include "dataset_id/DatasetId.h" #include "dataset_base/DatasetRepository.h" #include "dataset_base/SimpleCompoundDataset.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" #include "dataset_xml/XmlParser.h" using std::string; using std::cout; using std::ifstream; using std::endl; using dset::CredentialSelectionCatalog; using dset::GssCredentialManager; using dset::Content; using dset::DatasetList; using dset::GenericDataset; using dset::SimpleCompoundDataset; using dset::DatasetRepository; //********************************************************************** void msg(string txt) { cout << "----- "; cout << txt; cout << " -----" << endl; } string CONN; // Run command and check the return status. // We capture return status and check it if system returns 0. bool run_command(string exe, string args, int expect =0) { string prefix = "rm -f rstat.dat; "; prefix += "export DIAL_COM=" + CONN + "; "; prefix += "export DIAL_CATALOG_CONF=resolver.dat; "; prefix += "DIAL_CATALOG_CONF=" + getcwd() + "/resolver.dat; " + "export DIAL_CATALOG_CONF; "; string suffix = "; echo $? > rstat.dat"; string com = prefix + exe + " " + args + suffix; cout << com << endl; int sstat = system(com.c_str()); if ( true || sstat == 0 ) { ifstream rstat("rstat.dat"); rstat >> sstat; unlink("rstat.dat"); } if ( sstat != expect ) { cout << exe << " " << args << endl; cout << "Expected: " << expect << endl; cout << " Found: " << sstat << endl; return false; } return true; } // Return XML parser. XmlParser& parser() { static XmlParser pr; return pr; } //********************************************************************** int dataset_insert_t() { msg("Clean up previous runs"); run_command("rm", "-rf UniqueId resolver.dat dr.dat dst1.xml dst2.xml dst3.xml"); run_command("rm", "dataset.dtd"); SimpleUniqueIdGenerator::create_collection("Dataset", 123, 1001); SimpleUniqueIdGenerator::set_as_default(); CONN = UniqueIdGenerator::connection(); cout << CONN << endl; msg("Create default repository"); DatasetRepository::create_default_instance(); DatasetRepository& rep = DatasetRepository::default_instance(); assert( rep.is_valid() ); 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 ("Base command"); string exe = "DIAL_CATALOG_CONF=" + getcwd() + "/resolver.dat"; exe += "; export DIAL_CATALOG_CONF"; exe += "; dataset_insert"; cout << exe << endl; msg("Show help"); string args = "-h"; assert( run_command(exe, args) ); msg("Create test dataset 1"); Content con; GenericDataset dst1(con); dst1.lock(); DatasetId did1 = dst1.id(); assert( did1.is_valid() ); assert( did1.is_global() ); cout << dst1 << endl; assert( dst1.is_valid() ); assert( dst1.is_locked() ); string dst1_name = "dst1.xml"; assert( ! FileStatus(dst1_name).is_readable() ); msg("Write dataset to XML file"); const XmlElement* pele = dst1.xml(); assert( pele != 0 ); cout << *pele << endl; parser().write(dst1_name, *pele); assert( FileStatus(dst1_name).is_readable() ); msg("Put dataset 1"); args = "-c SQLRESULT -o SQLRESULT -f " + dst1_name; cout << args << endl; assert( ! rep.has(did1) ); assert( run_command(exe, args) ); assert( rep.has(did1) ); assert( rep.extract(did1)->id() == did1 ); msg("Attempt second insertion"); assert( ! run_command(exe, args) ); msg("Create test dataset 2"); GenericDataset dst2(con); dst2.lock(); DatasetId did2 = dst2.id(); assert( did2.is_valid() ); assert( did2.is_global() ); cout << dst2 << endl; assert( dst2.is_valid() ); assert( dst2.is_locked() ); string dst2_name = "dst2.xml"; assert( ! FileStatus(dst2_name).is_readable() ); msg("Write dataset to XML file"); pele = dst2.xml(); assert( pele != 0 ); parser().write(dst2_name, *pele); assert( FileStatus(dst2_name).is_readable() ); msg("Put dataset 2"); args = " -f " + dst2_name; cout << args << endl; assert( rep.extract(did2) == 0 ); assert( run_command(exe, args) ); assert( rep.extract(did2) != 0 ); assert( rep.extract(did2)->id() == did2 ); msg("Create compound dataset 3"); SimpleCompoundDataset dst3; dst3.merge(dst1); dst3.merge(dst2); dst3.lock(); cout << dst3 << endl; assert( dst3.is_valid() ); assert( dst3.is_locked() ); DatasetId did3 = dst3.id(); assert( did3.is_valid() ); assert( did3.is_global() ); string dst3_name = "dst3.xml"; assert( ! FileStatus(dst3_name).is_readable() ); msg("Write dataset 3 to XML file"); pele = dst3.xml(); assert( pele != 0 ); parser().write(dst3_name, *pele); assert( FileStatus(dst3_name).is_readable() ); msg("Put dataset 3"); args = " -f " + dst3_name; cout << args << endl; assert( rep.extract(did3) == 0 ); assert( run_command(exe, args) ); assert( rep.extract(did3) != 0 ); assert( rep.extract(did3)->id() == did3 ); return 0; } #ifdef CTEST_MAIN int main() { return dataset_insert_t(); } #endif