summaryrefslogtreecommitdiff
path: root/test/TrackerWatcherCommandTest.cc
blob: b71a53afe04b288da3132bafe21270e042aab811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "TrackerWatcherCommand.h"
#include "TorrentConsoleDownloadEngine.h"
#include "MetaFileUtil.h"
#include "Exception.h"
#include "prefs.h"
#include "HttpInitiateConnectionCommand.h"
#include "ByteArrayDiskWriter.h"
#include "DefaultBtContext.h"
#include "DefaultBtAnnounce.h"
#include "DefaultPieceStorage.h"
#include "DefaultPeerStorage.h"
#include "BtRegistry.h"
#include <cppunit/extensions/HelperMacros.h>

using namespace std;

class TrackerWatcherCommandTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(TrackerWatcherCommandTest);
  CPPUNIT_TEST(testCreateCommand);
  CPPUNIT_TEST_SUITE_END();
private:

public:
  void setUp() {
  }

  void testCreateCommand();
};


CPPUNIT_TEST_SUITE_REGISTRATION( TrackerWatcherCommandTest );

void TrackerWatcherCommandTest::testCreateCommand() {
  try {
    Option* op = new Option();
    op->put(PREF_TRACKER_MAX_TRIES, "10");
    
    BtContextHandle btContext(new DefaultBtContext());
    btContext->load("test.torrent");
    
    BtRuntimeHandle btRuntime;
    BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime);
    
    PieceStorageHandle pieceStorage(new DefaultPieceStorage(btContext, op));
    BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
				     pieceStorage);

    PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, op));
    BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(),
				    peerStorage);

    BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, op));
    BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
    TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
    te->option = op;
    te->segmentMan = new SegmentMan();
    te->segmentMan->option = op;
    ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
    te->segmentMan->diskWriter = byteArrayDiskWriter;

    TrackerWatcherCommand command(1, te, btContext);

    CPPUNIT_ASSERT(dynamic_cast<HttpInitiateConnectionCommand*>(command.createCommand()));
    //cerr << btAnnounce->getAnnounceUrl() << endl;
    
    btAnnounce->announceSuccess();
    btAnnounce->resetAnnounce();
    te->segmentMan->init();
    
    btRuntime->setHalt(true);
    
    CPPUNIT_ASSERT(dynamic_cast<HttpInitiateConnectionCommand*>(command.createCommand()));
    //cerr << btAnnounce->getAnnounceUrl() << endl;
    
    btAnnounce->announceSuccess();
    btAnnounce->resetAnnounce();
    te->segmentMan->init();
    
    CPPUNIT_ASSERT(btAnnounce->noMoreAnnounce());
    
  } catch(Exception* e) {
    cerr << e->getMsg() << endl;
    delete e;
  }
}