File: diranalyzertester.cpp

package info (click to toggle)
strigi 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,808 kB
  • ctags: 7,071
  • sloc: cpp: 44,468; ansic: 9,764; perl: 515; java: 367; python: 345; xml: 316; yacc: 176; sh: 153; makefile: 38
file content (167 lines) | stat: -rw-r--r-- 5,198 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "diranalyzertester.h"
#include "analyzerconfiguration.h"
#include "diranalyzer.h"
#include "indexmanager.h"
#include "indexreader.h"
#include "indexpluginloader.h"
#include "unittestfunctions.h"

#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstring>

using namespace std;
using namespace strigiunittest;

#ifdef _WIN32
    const string DirAnalyzerTester::separator = "\\";
#else
    const string DirAnalyzerTester::separator = "/";
#endif

void DirAnalyzerTester::setUp() {
    char buff[13];
    char* dirname;

    // generate index dir name
    strcpy(buff, "strigiXXXXXX");
    dirname = mkdtemp(buff);

    if (dirname == NULL) {
        cerr << "Error creating temporary directory for index because of: "
            << strerror(errno) << endl;
        return;
    } else {
        //cout << "created index dir: " << dirname << endl;
        indexdir.assign(dirname);
    }

    // generate indexed docs name
    strcpy(buff, "strigiXXXXXX");
    dirname = mkdtemp(buff);

    if (dirname == NULL) {
        cerr << "Error creating temporary directory for indexed docs: "
            << strerror(errno) << endl;
        return;
    } else {
        //cout << "created dir for testing documents: " << dirname << endl;
        filedir.assign(dirname);
    }

    // prepare files to be indexed
    string filename;
    string filecontents;

    filename = "testfile01";
    filecontents = "this is a simple test file";
    indexedFiles.insert (make_pair<string, string> (filename, filecontents));

    filename = "testfile02";
    filecontents = "unit testing example";
    indexedFiles.insert (make_pair<string, string> (filename, filecontents));

    // create files on file system
    for (map<string,string>::iterator iter = indexedFiles.begin();
         iter != indexedFiles.end(); iter++)
    {
        string fullpath = filedir + separator + iter->first;

        ofstream file;
        file.open(fullpath.c_str());
        if (file.is_open()) {
            file << iter->second;
            file.close();
        } else {
            cerr << "error during creation of file " << fullpath;
        }
    }

    manager = getIndexManager(backend, indexdir);
}

void DirAnalyzerTester::tearDown()
{
    if (manager) {
        Strigi::IndexPluginLoader::deleteIndexManager(manager);
    }
    manager = NULL;

    // clean up data
    string cmd = "rm -r ";
    cmd += indexdir;
    cmd += " ";
    cmd += filedir;
    int r = system(cmd.c_str());
    CPPUNIT_ASSERT_MESSAGE("cleanup failed", r == 0);
}

void DirAnalyzerTester::testVariables()
{
    CPPUNIT_ASSERT_MESSAGE ("manager == NULL", manager);
    CPPUNIT_ASSERT_MESSAGE ("backend empty", !backend.empty());
    CPPUNIT_ASSERT_MESSAGE ("indexdir empty", !indexdir.empty());

    // THIS TEST IS BROKEN: you cannot expect documents without indexing them
    unsigned int indexedFilesSize = manager->indexReader()->countDocuments();
    if (indexedFilesSize != indexedFiles.size()) {
        ostringstream msg;
        msg << "There are " << indexedFilesSize << " indexed files instead of "
            << indexedFiles.size();
        //CPPUNIT_FAIL(msg.str());
    }
}

void DirAnalyzerTester::testCreateIndex()
{
    CPPUNIT_ASSERT_MESSAGE ("manager == null", manager);

    Strigi::AnalyzerConfiguration config;
    Strigi::DirAnalyzer* analyzer = new Strigi::DirAnalyzer(*manager, config);

    for (map<string,string>::iterator iter = indexedFiles.begin();
         iter != indexedFiles.end(); iter++)
    {
        string path = filedir + separator + iter->first;
        // cerr << "going to index " << path << endl;
        int retval = analyzer->analyzeDir(path, 1);
        CPPUNIT_ASSERT_MESSAGE("Error indexing "+path, retval == 0);
    }
    delete analyzer;

    CPPUNIT_ASSERT_MESSAGE("Not all documents were indexed.",
        manager->indexReader()->countDocuments()
            == (int32_t)indexedFiles.size());
    unsigned int indexedFilesSize = manager->indexReader()->countDocuments();
    if (indexedFilesSize != indexedFiles.size()) {
        ostringstream msg;
        msg << "There are " << indexedFilesSize << " indexed files instead of "
            << indexedFiles.size();
        CPPUNIT_FAIL(msg.str());
    }
}