RequestGroupManTest.cc 3.1 KB

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