RequestGroupManTest.cc 2.9 KB

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