BtRegistryTest.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "BtRegistry.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Exception.h"
  4. #include "MockBtContext.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(testGetBtContext);
  15. CPPUNIT_TEST(testGetAllBtContext);
  16. CPPUNIT_TEST(testRemove);
  17. CPPUNIT_TEST(testRemoveAll);
  18. CPPUNIT_TEST_SUITE_END();
  19. private:
  20. public:
  21. void testGetBtContext();
  22. void testGetAllBtContext();
  23. void testRemove();
  24. void testRemoveAll();
  25. };
  26. CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest );
  27. void BtRegistryTest::testGetBtContext()
  28. {
  29. BtRegistry btRegistry;
  30. CPPUNIT_ASSERT(btRegistry.getBtContext("test").isNull());
  31. SharedHandle<BtContext> btContext(new MockBtContext());
  32. BtObject btObject;
  33. btObject._btContext = btContext;
  34. btRegistry.put("test", btObject);
  35. CPPUNIT_ASSERT_EQUAL(btContext.get(),
  36. btRegistry.getBtContext("test").get());
  37. }
  38. static void addTwoBtContext(BtRegistry& btRegistry)
  39. {
  40. SharedHandle<BtContext> btContext1(new MockBtContext());
  41. SharedHandle<BtContext> btContext2(new MockBtContext());
  42. BtObject btObject1;
  43. btObject1._btContext = btContext1;
  44. BtObject btObject2;
  45. btObject2._btContext = btContext2;
  46. btRegistry.put("ctx1", btObject1);
  47. btRegistry.put("ctx2", btObject2);
  48. }
  49. void BtRegistryTest::testGetAllBtContext()
  50. {
  51. BtRegistry btRegistry;
  52. addTwoBtContext(btRegistry);
  53. std::vector<SharedHandle<BtContext> > result;
  54. btRegistry.getAllBtContext(std::back_inserter(result));
  55. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  56. }
  57. void BtRegistryTest::testRemove()
  58. {
  59. BtRegistry btRegistry;
  60. addTwoBtContext(btRegistry);
  61. CPPUNIT_ASSERT(btRegistry.remove("ctx1"));
  62. CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
  63. CPPUNIT_ASSERT(!btRegistry.get("ctx2").isNull());
  64. }
  65. void BtRegistryTest::testRemoveAll()
  66. {
  67. BtRegistry btRegistry;
  68. addTwoBtContext(btRegistry);
  69. btRegistry.removeAll();
  70. CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull());
  71. CPPUNIT_ASSERT(btRegistry.get("ctx2").isNull());
  72. }
  73. } // namespace aria2