BtRegistryTest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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).isNull());
  34. SharedHandle<DownloadContext> dctx(new DownloadContext());
  35. BtObject btObject;
  36. btObject._downloadContext = dctx;
  37. btRegistry.put(1, btObject);
  38. CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
  39. }
  40. static void addTwoDownloadContext(BtRegistry& btRegistry)
  41. {
  42. SharedHandle<DownloadContext> dctx1(new DownloadContext());
  43. SharedHandle<DownloadContext> dctx2(new DownloadContext());
  44. BtObject btObject1;
  45. btObject1._downloadContext = dctx1;
  46. BtObject btObject2;
  47. btObject2._downloadContext = dctx2;
  48. btRegistry.put(1, btObject1);
  49. btRegistry.put(2, btObject2);
  50. }
  51. void BtRegistryTest::testGetDownloadContext_infoHash()
  52. {
  53. BtRegistry btRegistry;
  54. addTwoDownloadContext(btRegistry);
  55. BDE attrs1 = BDE::dict();
  56. attrs1[bittorrent::INFO_HASH] = std::string("hash1");
  57. BDE attrs2 = BDE::dict();
  58. attrs2[bittorrent::INFO_HASH] = std::string("hash2");
  59. btRegistry.getDownloadContext(1)->setAttribute
  60. (bittorrent::BITTORRENT, attrs1);
  61. btRegistry.getDownloadContext(2)->setAttribute
  62. (bittorrent::BITTORRENT, attrs2);
  63. CPPUNIT_ASSERT(!btRegistry.getDownloadContext("hash1").isNull());
  64. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==
  65. btRegistry.getDownloadContext(1).get());
  66. CPPUNIT_ASSERT(btRegistry.getDownloadContext("not exists").isNull());
  67. }
  68. void BtRegistryTest::testGetAllDownloadContext()
  69. {
  70. BtRegistry btRegistry;
  71. addTwoDownloadContext(btRegistry);
  72. std::vector<SharedHandle<DownloadContext> > result;
  73. btRegistry.getAllDownloadContext(std::back_inserter(result));
  74. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  75. }
  76. void BtRegistryTest::testRemove()
  77. {
  78. BtRegistry btRegistry;
  79. addTwoDownloadContext(btRegistry);
  80. CPPUNIT_ASSERT(btRegistry.remove(1));
  81. CPPUNIT_ASSERT(btRegistry.get(1).isNull());
  82. CPPUNIT_ASSERT(!btRegistry.get(2).isNull());
  83. }
  84. void BtRegistryTest::testRemoveAll()
  85. {
  86. BtRegistry btRegistry;
  87. addTwoDownloadContext(btRegistry);
  88. btRegistry.removeAll();
  89. CPPUNIT_ASSERT(btRegistry.get(1).isNull());
  90. CPPUNIT_ASSERT(btRegistry.get(2).isNull());
  91. }
  92. } // namespace aria2