BtRegistryTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "BtRegistry.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Exception.h"
  4. #include "DownloadContext.h"
  5. #include "MockPeerStorage.h"
  6. #include "MockPieceStorage.h"
  7. #include "MockBtAnnounce.h"
  8. #include "MockBtProgressInfoFile.h"
  9. #include "BtRuntime.h"
  10. #include "FileEntry.h"
  11. #include "bittorrent_helper.h"
  12. namespace aria2 {
  13. class BtRegistryTest:public CppUnit::TestFixture {
  14. CPPUNIT_TEST_SUITE(BtRegistryTest);
  15. CPPUNIT_TEST(testGetDownloadContext);
  16. CPPUNIT_TEST(testGetDownloadContext_infoHash);
  17. CPPUNIT_TEST(testGetAllDownloadContext);
  18. CPPUNIT_TEST(testRemove);
  19. CPPUNIT_TEST(testRemoveAll);
  20. CPPUNIT_TEST_SUITE_END();
  21. private:
  22. public:
  23. void testGetDownloadContext();
  24. void testGetDownloadContext_infoHash();
  25. void testGetAllDownloadContext();
  26. void testRemove();
  27. void testRemoveAll();
  28. };
  29. CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest );
  30. void BtRegistryTest::testGetDownloadContext()
  31. {
  32. BtRegistry btRegistry;
  33. CPPUNIT_ASSERT(!btRegistry.getDownloadContext(1));
  34. SharedHandle<DownloadContext> dctx(new DownloadContext());
  35. SharedHandle<BtObject> btObject(new BtObject());
  36. btObject->downloadContext = dctx;
  37. btRegistry.put(1, btObject);
  38. CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
  39. }
  40. namespace {
  41. void addTwoDownloadContext(BtRegistry& btRegistry)
  42. {
  43. SharedHandle<DownloadContext> dctx1(new DownloadContext());
  44. SharedHandle<DownloadContext> dctx2(new DownloadContext());
  45. SharedHandle<BtObject> btObject1(new BtObject());
  46. btObject1->downloadContext = dctx1;
  47. SharedHandle<BtObject> btObject2(new BtObject());
  48. btObject2->downloadContext = dctx2;
  49. btRegistry.put(1, btObject1);
  50. btRegistry.put(2, btObject2);
  51. }
  52. } // namespace
  53. void BtRegistryTest::testGetDownloadContext_infoHash()
  54. {
  55. BtRegistry btRegistry;
  56. addTwoDownloadContext(btRegistry);
  57. SharedHandle<TorrentAttribute> attrs1(new TorrentAttribute());
  58. attrs1->infoHash = "hash1";
  59. SharedHandle<TorrentAttribute> attrs2(new TorrentAttribute());
  60. attrs2->infoHash = "hash2";
  61. btRegistry.getDownloadContext(1)->setAttribute
  62. (bittorrent::BITTORRENT, attrs1);
  63. btRegistry.getDownloadContext(2)->setAttribute
  64. (bittorrent::BITTORRENT, attrs2);
  65. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1"));
  66. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==
  67. btRegistry.getDownloadContext(1).get());
  68. CPPUNIT_ASSERT(!btRegistry.getDownloadContext("not exists"));
  69. }
  70. void BtRegistryTest::testGetAllDownloadContext()
  71. {
  72. BtRegistry btRegistry;
  73. addTwoDownloadContext(btRegistry);
  74. std::vector<SharedHandle<DownloadContext> > result;
  75. btRegistry.getAllDownloadContext(std::back_inserter(result));
  76. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  77. }
  78. void BtRegistryTest::testRemove()
  79. {
  80. BtRegistry btRegistry;
  81. addTwoDownloadContext(btRegistry);
  82. CPPUNIT_ASSERT(btRegistry.remove(1));
  83. CPPUNIT_ASSERT(!btRegistry.get(1));
  84. CPPUNIT_ASSERT(btRegistry.get(2));
  85. }
  86. void BtRegistryTest::testRemoveAll()
  87. {
  88. BtRegistry btRegistry;
  89. addTwoDownloadContext(btRegistry);
  90. btRegistry.removeAll();
  91. CPPUNIT_ASSERT(!btRegistry.get(1));
  92. CPPUNIT_ASSERT(!btRegistry.get(2));
  93. }
  94. } // namespace aria2