2007-08-28 15:48:05 +00:00
|
|
|
#include "RequestGroup.h"
|
2008-11-14 12:32:54 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2007-12-12 13:53:33 +00:00
|
|
|
#include "ServerHost.h"
|
|
|
|
#include "Option.h"
|
2008-11-14 12:32:54 +00:00
|
|
|
#include "SingleFileDownloadContext.h"
|
|
|
|
#include "FileEntry.h"
|
2009-01-12 12:27:34 +00:00
|
|
|
#include "PieceStorage.h"
|
2007-08-28 15:48:05 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-08-28 15:48:05 +00:00
|
|
|
|
|
|
|
class RequestGroupTest : public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(RequestGroupTest);
|
2007-12-12 13:53:33 +00:00
|
|
|
CPPUNIT_TEST(testRegisterSearchRemove);
|
|
|
|
CPPUNIT_TEST(testRemoveURIWhoseHostnameIs);
|
2008-11-14 12:32:54 +00:00
|
|
|
CPPUNIT_TEST(testGetFilePath);
|
2009-01-12 12:27:34 +00:00
|
|
|
CPPUNIT_TEST(testCreateDownloadResult);
|
2007-08-28 15:48:05 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {}
|
2007-12-12 13:53:33 +00:00
|
|
|
|
|
|
|
void testRegisterSearchRemove();
|
|
|
|
void testRemoveURIWhoseHostnameIs();
|
2008-11-14 12:32:54 +00:00
|
|
|
void testGetFilePath();
|
2009-01-12 12:27:34 +00:00
|
|
|
void testCreateDownloadResult();
|
2007-08-28 15:48:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( RequestGroupTest );
|
2007-12-12 13:53:33 +00:00
|
|
|
|
|
|
|
void RequestGroupTest::testRegisterSearchRemove()
|
|
|
|
{
|
|
|
|
Option op;
|
2008-02-08 15:53:45 +00:00
|
|
|
RequestGroup rg(&op, std::deque<std::string>());
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<ServerHost> sv1(new ServerHost(1, "localhost1"));
|
|
|
|
SharedHandle<ServerHost> sv2(new ServerHost(2, "localhost2"));
|
|
|
|
SharedHandle<ServerHost> sv3(new ServerHost(3, "localhost3"));
|
2007-12-12 13:53:33 +00:00
|
|
|
|
|
|
|
rg.registerServerHost(sv3);
|
|
|
|
rg.registerServerHost(sv1);
|
|
|
|
rg.registerServerHost(sv2);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(rg.searchServerHost(0).isNull());
|
|
|
|
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<ServerHost> sv = rg.searchServerHost(1);
|
2007-12-12 13:53:33 +00:00
|
|
|
CPPUNIT_ASSERT(!sv.isNull());
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost1"), sv->getHostname());
|
2007-12-12 13:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rg.removeServerHost(1);
|
|
|
|
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<ServerHost> sv = rg.searchServerHost(1);
|
2007-12-12 13:53:33 +00:00
|
|
|
CPPUNIT_ASSERT(sv.isNull());
|
|
|
|
}
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<ServerHost> sv = rg.searchServerHost(2);
|
2007-12-12 13:53:33 +00:00
|
|
|
CPPUNIT_ASSERT(!sv.isNull());
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost2"), sv->getHostname());
|
2007-12-12 13:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestGroupTest::testRemoveURIWhoseHostnameIs()
|
|
|
|
{
|
|
|
|
const char* uris[] = { "http://localhost/aria2.zip",
|
|
|
|
"ftp://localhost/aria2.zip",
|
|
|
|
"http://mirror/aria2.zip" };
|
|
|
|
Option op;
|
2008-02-08 15:53:45 +00:00
|
|
|
RequestGroup rg(&op, std::deque<std::string>(&uris[0], &uris[3]));
|
2007-12-12 13:53:33 +00:00
|
|
|
rg.removeURIWhoseHostnameIs("localhost");
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, rg.getRemainingUris().size());
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/aria2.zip"),
|
2007-12-12 13:53:33 +00:00
|
|
|
rg.getRemainingUris()[0]);
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-11-14 12:32:54 +00:00
|
|
|
void RequestGroupTest::testGetFilePath()
|
|
|
|
{
|
|
|
|
SharedHandle<SingleFileDownloadContext> ctx
|
|
|
|
(new SingleFileDownloadContext(1024, 1024, "myfile"));
|
|
|
|
ctx->setDir("/tmp");
|
|
|
|
Option op;
|
|
|
|
std::deque<std::string> uris;
|
|
|
|
|
|
|
|
RequestGroup group(&op, uris);
|
|
|
|
group.setDownloadContext(ctx);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFilePath());
|
|
|
|
|
|
|
|
group.markInMemoryDownload();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFilePath());
|
|
|
|
}
|
|
|
|
|
2009-01-12 12:27:34 +00:00
|
|
|
void RequestGroupTest::testCreateDownloadResult()
|
|
|
|
{
|
|
|
|
SharedHandle<SingleFileDownloadContext> ctx
|
|
|
|
(new SingleFileDownloadContext(1024, 1024*1024, "myfile"));
|
|
|
|
ctx->setDir("/tmp");
|
|
|
|
Option op;
|
|
|
|
std::deque<std::string> uris;
|
|
|
|
uris.push_back("http://first/file");
|
|
|
|
uris.push_back("http://second/file");
|
|
|
|
|
|
|
|
RequestGroup group(&op, uris);
|
|
|
|
group.setDownloadContext(ctx);
|
|
|
|
group.initPieceStorage();
|
|
|
|
{
|
|
|
|
SharedHandle<DownloadResult> result = group.createDownloadResult();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), result->filePath);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)1024*1024, result->totalLength);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://first/file"), result->uri);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, result->numUri);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)0, result->sessionDownloadLength);
|
2009-01-15 15:39:05 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)0, result->sessionTime);
|
2009-01-12 12:27:34 +00:00
|
|
|
// result is UNKNOWN_ERROR if download has not completed and no specific
|
|
|
|
// error has been reported
|
|
|
|
CPPUNIT_ASSERT_EQUAL(DownloadResult::UNKNOWN_ERROR, result->result);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
group.addURIResult("http://first/file", DownloadResult::TIME_OUT);
|
|
|
|
group.addURIResult("http://second/file",DownloadResult::RESOURCE_NOT_FOUND);
|
|
|
|
|
|
|
|
SharedHandle<DownloadResult> result = group.createDownloadResult();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(DownloadResult::RESOURCE_NOT_FOUND, result->result);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
group.getPieceStorage()->markAllPiecesDone();
|
|
|
|
|
|
|
|
SharedHandle<DownloadResult> result = group.createDownloadResult();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(DownloadResult::FINISHED, result->result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|