summaryrefslogtreecommitdiff
path: root/src/LocalFileCache_test.cc
blob: 2052a687ad252386d696f7a8f0caaa2ebba40ef7 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * vdr-vodcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2007 - 2009 Tobias Grimm <vdr@e-tobi.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <time.h>
#include <sstream>
#include <cxxtest/TestSuite.h>
#include "LocalFileCache.h"
#include "SdbmHashCalculator.h"
#include "values.h"
#include <stdlib.h>

using namespace std;

namespace
{

const string CacheDir("test.cache");

class An_empty_LocalFileCache: public CxxTest::TestSuite
{
private:
    RefPtr<LocalFileCache> _localFileCache;

private:
    void CreateCachedFile(string fileName, long age)
    {
        string cachedFileName = SdbmHashCalculator::Calculate(fileName);

        time_t fileTime = time(NULL) - age * 60;
        struct tm *timeComponents = localtime(&fileTime);

        char formattedTime[11];
        strftime(formattedTime, 11, "%y%m%d%H%M", timeComponents);

        string touchCommand = string("touch -t ")  + formattedTime + " " +
          CacheDir + "/" + cachedFileName;

        system(touchCommand.c_str());
    }

public:
    void setUp()
    {
        system(("rm -rf " + CacheDir).c_str());
        system(("mkdir " + CacheDir).c_str());
        _localFileCache = new LocalFileCache(CacheDir);
    }

    void tearDown()
    {
        system(("rm -rf " + CacheDir).c_str());
    }

    void Should_not_return_any_data()
    {
        TS_ASSERT(NULL == _localFileCache->CreateStreamByUrl("foo://bar").get());
    }

    void Should_return_the_data_put_into_the_cache()
    {
        stringstream input("foo");
        _localFileCache->Put(input, "http://foo");

        RefPtr<istream> output = _localFileCache->CreateStreamByUrl("http://foo");
        TS_ASSERT(NULL != output.get());

        stringstream outputStr;
        outputStr << output->rdbuf();
        TS_ASSERT_EQUALS(input.str(), outputStr.str());
    }

    void Should_return_different_data_by_different_urls()
    {
        stringstream input1("foo");
        stringstream input2("bar");

        _localFileCache->Put(input1, "http://foo/baz");
        _localFileCache->Put(input2, "http://bar/baz");

        stringstream output1;
        stringstream output2;

        output1 << _localFileCache->CreateStreamByUrl("http://foo/baz")->rdbuf();
        output2 << _localFileCache->CreateStreamByUrl("http://bar/baz")->rdbuf();

        TS_ASSERT_EQUALS(input1.str(), output1.str());
        TS_ASSERT_EQUALS(input2.str(), output2.str());
    }

    void Should_return_the_age_of_an_existing_entry_in_minutes()
    {
        CreateCachedFile("testFile", 5);
        TS_ASSERT_EQUALS(5, _localFileCache->GetAgeInMinutes("testFile"));
    }

    void Should_return_the_maximum_age_for_a_non_existing_entry_in_minutes()
    {
        TS_ASSERT_EQUALS(MAXLONG, _localFileCache->GetAgeInMinutes("testFile"));
    }

    void Should_tell_if_a_file_is_cached()
    {
        TS_ASSERT(!_localFileCache->IsCached("testFile"));

        CreateCachedFile("testFile", 0);
        TS_ASSERT(_localFileCache->IsCached("testFile"));
    }

    void Should_not_clean_up_younger_files()
    {
        CreateCachedFile("testFile1", 5); // 5 minutes
        CreateCachedFile("testFile2", 5 * 60 * 24); // 5 days
        TS_ASSERT(_localFileCache->IsCached("testFile1"));
        TS_ASSERT(_localFileCache->IsCached("testFile2"));

        _localFileCache->CleanUp(10);

        TS_ASSERT(_localFileCache->IsCached("testFile1"));
        TS_ASSERT(_localFileCache->IsCached("testFile2"));
    }

    void Should_clean_up_older_files()
    {
        CreateCachedFile("testFile1", 5 * 60 * 24); // 5 days
        CreateCachedFile("testFile2", 10 * 60 * 24); // 10 days
        TS_ASSERT(_localFileCache->IsCached("testFile1"));
        TS_ASSERT(_localFileCache->IsCached("testFile2"));

        _localFileCache->CleanUp(10);

        TS_ASSERT(_localFileCache->IsCached("testFile1"));
        TS_ASSERT(!_localFileCache->IsCached("testFile2"));
    }

    void Should_delete_a_file_if_it_can_not_be_written_successfully()
    {
        // TODO: Any ideas how to test this?
    }
};

};