BtRegistryTest.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. namespace aria2 {
  12. class BtRegistryTest:public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(BtRegistryTest);
  14. CPPUNIT_TEST(testGetDownloadContext);
  15. CPPUNIT_TEST(testGetAllDownloadContext);
  16. CPPUNIT_TEST(testRemove);
  17. CPPUNIT_TEST(testRemoveAll);
  18. CPPUNIT_TEST_SUITE_END();
  19. private:
  20. public:
  21. void testGetDownloadContext();
  22. void testGetAllDownloadContext();
  23. void testRemove();
  24. void testRemoveAll();
  25. };
  26. CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest );
  27. void BtRegistryTest::testGetDownloadContext()
  28. {
  29. BtRegistry btRegistry;
  30. CPPUNIT_ASSERT(btRegistry.getDownloadContext("test").isNull());
  31. SharedHandle<DownloadContext> dctx(new DownloadContext());
  32. BtObject btObject;
  33. btObject._downloadContext = dctx;
  34. btRegistry.put("test", btObject);
  35. CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext("test").get());
  36. }
  37. static void addTwoDownloadContext(BtRegistry& btRegistry)
  38. {
  39. SharedHandle<DownloadContext> dctx1(new DownloadContext());
  40. SharedHandle<DownloadContext> dctx2(new DownloadContext());
  41. BtObject btObject1;
  42. btObject1._downloadContext = dctx1;
  43. BtObject btObject2;
  44. btObject2._downloadContext = dctx2;
  45. btRegistry.put("ctx1", btObject1);
  46. btRegistry.put("ctx2", btObject2);
  47. }
  48. void BtRegistryTest::testGetAllDownloadContext()
  49. {
  50. BtRegistry btRegistry;
  51. addTwoDownloadContext(btRegistry);
  52. std::vector<SharedHandle<DownloadContext> > result;
  53. btRegistry.getAllDownloadContext(std::back_inserter(result));
  54. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  55. }
  56. void BtRegistryTest::testRemove()
  57. {
  58. BtRegistry btRegistry;
  59. addTwoDownloadContext(btRegistry);
  60. CPPUNIT_ASSERT(btRegistry.remove("ctx1"));
  61. CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
  62. CPPUNIT_ASSERT(!btRegistry.get("ctx2").isNull());
  63. }
  64. void BtRegistryTest::testRemoveAll()
  65. {
  66. BtRegistry btRegistry;
  67. addTwoDownloadContext(btRegistry);
  68. btRegistry.removeAll();
  69. CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
  70. CPPUNIT_ASSERT(btRegistry.get("ctx2").isNull());
  71. }
  72. } // namespace aria2