RequestGroupManTest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "RequestGroupMan.h"
  2. #include <fstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "prefs.h"
  5. #include "DownloadContext.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. SharedHandle<RequestGroup> rg1(new RequestGroup(_option));
  37. SharedHandle<RequestGroup> rg2(new RequestGroup(_option));
  38. SharedHandle<DownloadContext> dctx1
  39. (new DownloadContext(0, 0, "aria2.tar.bz2"));
  40. SharedHandle<DownloadContext> dctx2
  41. (new DownloadContext(0, 0, "aria2.tar.bz2"));
  42. rg1->setDownloadContext(dctx1);
  43. rg2->setDownloadContext(dctx2);
  44. RequestGroupMan gm(std::deque<SharedHandle<RequestGroup> >(), 1,
  45. _option.get());
  46. gm.addRequestGroup(rg1);
  47. gm.addRequestGroup(rg2);
  48. CPPUNIT_ASSERT(gm.isSameFileBeingDownloaded(rg1.get()));
  49. dctx2->getFirstFileEntry()->setPath("aria2.tar.gz");
  50. CPPUNIT_ASSERT(!gm.isSameFileBeingDownloaded(rg1.get()));
  51. }
  52. void RequestGroupManTest::testGetInitialCommands()
  53. {
  54. // TODO implement later
  55. }
  56. void RequestGroupManTest::testSaveServerStat()
  57. {
  58. RequestGroupMan rm(std::deque<SharedHandle<RequestGroup> >(),0,_option.get());
  59. SharedHandle<ServerStat> ss_localhost(new ServerStat("localhost", "http"));
  60. rm.addServerStat(ss_localhost);
  61. File f("/tmp/aria2_RequestGroupManTest_testSaveServerStat");
  62. if(f.exists()) {
  63. f.remove();
  64. }
  65. CPPUNIT_ASSERT(rm.saveServerStat(f.getPath()));
  66. CPPUNIT_ASSERT(f.isFile());
  67. f.remove();
  68. CPPUNIT_ASSERT(f.mkdirs());
  69. CPPUNIT_ASSERT(!rm.saveServerStat(f.getPath()));
  70. }
  71. void RequestGroupManTest::testLoadServerStat()
  72. {
  73. File f("/tmp/aria2_RequestGroupManTest_testLoadServerStat");
  74. std::ofstream o(f.getPath().c_str(), std::ios::binary);
  75. o << "host=localhost, protocol=http, dl_speed=0, last_updated=1219505257,"
  76. << "status=OK";
  77. o.close();
  78. RequestGroupMan rm(std::deque<SharedHandle<RequestGroup> >(),0,_option.get());
  79. std::cerr << "testLoadServerStat" << std::endl;
  80. CPPUNIT_ASSERT(rm.loadServerStat(f.getPath()));
  81. SharedHandle<ServerStat> ss_localhost = rm.findServerStat("localhost",
  82. "http");
  83. CPPUNIT_ASSERT(!ss_localhost.isNull());
  84. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), ss_localhost->getHostname());
  85. }
  86. } // namespace aria2