RequestGroupManTest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "RequestGroupMan.h"
  2. #include <fstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "prefs.h"
  5. #include "SingleFileDownloadContext.h"
  6. #include "RequestGroup.h"
  7. #include "Option.h"
  8. #include "DownloadResult.h"
  9. #include "FileEntry.h"
  10. #include "ServerStatMan.h"
  11. #include "ServerStat.h"
  12. #include "File.h"
  13. namespace aria2 {
  14. class RequestGroupManTest : public CppUnit::TestFixture {
  15. CPPUNIT_TEST_SUITE(RequestGroupManTest);
  16. CPPUNIT_TEST(testIsSameFileBeingDownloaded);
  17. CPPUNIT_TEST(testGetInitialCommands);
  18. CPPUNIT_TEST(testLoadServerStat);
  19. CPPUNIT_TEST(testSaveServerStat);
  20. CPPUNIT_TEST_SUITE_END();
  21. private:
  22. SharedHandle<Option> _option;
  23. public:
  24. void setUp()
  25. {
  26. _option.reset(new Option());
  27. }
  28. void testIsSameFileBeingDownloaded();
  29. void testGetInitialCommands();
  30. void testLoadServerStat();
  31. void testSaveServerStat();
  32. };
  33. CPPUNIT_TEST_SUITE_REGISTRATION( RequestGroupManTest );
  34. void RequestGroupManTest::testIsSameFileBeingDownloaded()
  35. {
  36. std::deque<std::string> uris;
  37. uris.push_back("http://localhost/aria2.tar.bz2");
  38. SharedHandle<RequestGroup> rg1(new RequestGroup(_option, uris));
  39. SharedHandle<RequestGroup> rg2(new RequestGroup(_option, uris));
  40. SharedHandle<SingleFileDownloadContext> dctx1
  41. (new SingleFileDownloadContext(0, 0, "aria2.tar.bz2"));
  42. SharedHandle<SingleFileDownloadContext> dctx2
  43. (new SingleFileDownloadContext(0, 0, "aria2.tar.bz2"));
  44. rg1->setDownloadContext(dctx1);
  45. rg2->setDownloadContext(dctx2);
  46. RequestGroups rgs;
  47. rgs.push_back(rg1);
  48. rgs.push_back(rg2);
  49. RequestGroupMan gm(rgs, 1, _option.get());
  50. CPPUNIT_ASSERT(gm.isSameFileBeingDownloaded(rg1.get()));
  51. dctx2->setFilename("aria2.tar.gz");
  52. CPPUNIT_ASSERT(!gm.isSameFileBeingDownloaded(rg1.get()));
  53. }
  54. void RequestGroupManTest::testGetInitialCommands()
  55. {
  56. // TODO implement later
  57. }
  58. void RequestGroupManTest::testSaveServerStat()
  59. {
  60. RequestGroupMan rm(std::deque<SharedHandle<RequestGroup> >(),0,_option.get());
  61. SharedHandle<ServerStat> ss_localhost(new ServerStat("localhost", "http"));
  62. rm.addServerStat(ss_localhost);
  63. File f("/tmp/aria2_RequestGroupManTest_testSaveServerStat");
  64. if(f.exists()) {
  65. f.remove();
  66. }
  67. CPPUNIT_ASSERT(rm.saveServerStat(f.getPath()));
  68. CPPUNIT_ASSERT(f.isFile());
  69. f.remove();
  70. CPPUNIT_ASSERT(f.mkdirs());
  71. CPPUNIT_ASSERT(!rm.saveServerStat(f.getPath()));
  72. }
  73. void RequestGroupManTest::testLoadServerStat()
  74. {
  75. File f("/tmp/aria2_RequestGroupManTest_testLoadServerStat");
  76. std::ofstream o(f.getPath().c_str(), std::ios::binary);
  77. o << "host=localhost, protocol=http, dl_speed=0, last_updated=1219505257,"
  78. << "status=OK";
  79. o.close();
  80. RequestGroupMan rm(std::deque<SharedHandle<RequestGroup> >(),0,_option.get());
  81. std::cerr << "testLoadServerStat" << std::endl;
  82. CPPUNIT_ASSERT(rm.loadServerStat(f.getPath()));
  83. SharedHandle<ServerStat> ss_localhost = rm.findServerStat("localhost",
  84. "http");
  85. CPPUNIT_ASSERT(!ss_localhost.isNull());
  86. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), ss_localhost->getHostname());
  87. }
  88. } // namespace aria2