TrackerWatcherCommandTest.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "TrackerWatcherCommand.h"
  2. #include "TorrentConsoleDownloadEngine.h"
  3. #include "MetaFileUtil.h"
  4. #include "Exception.h"
  5. #include "prefs.h"
  6. #include "HttpInitiateConnectionCommand.h"
  7. #include "ByteArrayDiskWriter.h"
  8. #include "DefaultBtContext.h"
  9. #include "DefaultBtAnnounce.h"
  10. #include "DefaultPieceStorage.h"
  11. #include "DefaultPeerStorage.h"
  12. #include "BtRegistry.h"
  13. #include "RequestFactory.h"
  14. #include <cppunit/extensions/HelperMacros.h>
  15. using namespace std;
  16. class TrackerWatcherCommandTest:public CppUnit::TestFixture {
  17. CPPUNIT_TEST_SUITE(TrackerWatcherCommandTest);
  18. CPPUNIT_TEST(testCreateCommand);
  19. CPPUNIT_TEST_SUITE_END();
  20. private:
  21. Option* op;
  22. public:
  23. TrackerWatcherCommandTest():op(new Option())
  24. {
  25. op->put(PREF_TRACKER_MAX_TRIES, "10");
  26. RequestFactoryHandle requestFactory = new RequestFactory();
  27. requestFactory->setOption(op);
  28. RequestFactorySingletonHolder::instance(requestFactory);
  29. }
  30. void setUp() {}
  31. void testCreateCommand();
  32. };
  33. CPPUNIT_TEST_SUITE_REGISTRATION( TrackerWatcherCommandTest );
  34. void TrackerWatcherCommandTest::testCreateCommand() {
  35. try {
  36. BtContextHandle btContext(new DefaultBtContext());
  37. btContext->load("test.torrent");
  38. BtRuntimeHandle btRuntime;
  39. BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime);
  40. PieceStorageHandle pieceStorage(new DefaultPieceStorage(btContext, op));
  41. BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
  42. pieceStorage);
  43. PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, op));
  44. BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(),
  45. peerStorage);
  46. BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, op));
  47. BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
  48. TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
  49. te->option = op;
  50. te->segmentMan = new SegmentMan();
  51. te->segmentMan->option = op;
  52. ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  53. te->segmentMan->diskWriter = byteArrayDiskWriter;
  54. TrackerWatcherCommand command(1, te, btContext);
  55. CPPUNIT_ASSERT(dynamic_cast<HttpInitiateConnectionCommand*>(command.createCommand()));
  56. //cerr << btAnnounce->getAnnounceUrl() << endl;
  57. btAnnounce->announceSuccess();
  58. btAnnounce->resetAnnounce();
  59. te->segmentMan->init();
  60. btRuntime->setHalt(true);
  61. CPPUNIT_ASSERT(dynamic_cast<HttpInitiateConnectionCommand*>(command.createCommand()));
  62. //cerr << btAnnounce->getAnnounceUrl() << endl;
  63. btAnnounce->announceSuccess();
  64. btAnnounce->resetAnnounce();
  65. te->segmentMan->init();
  66. CPPUNIT_ASSERT(btAnnounce->noMoreAnnounce());
  67. } catch(Exception* e) {
  68. cerr << e->getMsg() << endl;
  69. delete e;
  70. }
  71. }