commit | author | age
|
d896e6
|
1 |
/*
|
JM |
2 |
* Copyright 2012 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.tests;
|
|
17 |
|
|
18 |
import static org.junit.Assert.assertEquals;
|
|
19 |
|
|
20 |
import java.util.ArrayList;
|
|
21 |
import java.util.List;
|
|
22 |
|
|
23 |
import org.eclipse.jgit.lib.Repository;
|
|
24 |
import org.junit.Test;
|
|
25 |
|
|
26 |
import com.gitblit.LuceneExecutor;
|
40ca5c
|
27 |
import com.gitblit.models.RefModel;
|
JM |
28 |
import com.gitblit.models.RepositoryModel;
|
d896e6
|
29 |
import com.gitblit.models.SearchResult;
|
40ca5c
|
30 |
import com.gitblit.utils.JGitUtils;
|
d896e6
|
31 |
import com.gitblit.utils.StringUtils;
|
JM |
32 |
|
|
33 |
/**
|
|
34 |
* Tests Lucene indexing and querying.
|
|
35 |
*
|
|
36 |
* @author James Moger
|
|
37 |
*
|
|
38 |
*/
|
|
39 |
public class LuceneExecutorTest {
|
|
40 |
|
|
41 |
private LuceneExecutor newLuceneExecutor() {
|
|
42 |
return new LuceneExecutor(null, GitBlitSuite.REPOSITORIES);
|
|
43 |
}
|
|
44 |
|
40ca5c
|
45 |
private RepositoryModel newRepositoryModel(Repository repository) {
|
JM |
46 |
RepositoryModel model = new RepositoryModel();
|
|
47 |
model.name = StringUtils.getRelativePath(GitBlitSuite.REPOSITORIES.getAbsolutePath(),
|
d896e6
|
48 |
repository.getDirectory().getAbsolutePath());
|
40ca5c
|
49 |
model.hasCommits = JGitUtils.hasCommits(repository);
|
JM |
50 |
|
|
51 |
// index all local branches
|
|
52 |
model.indexedBranches = new ArrayList<String>();
|
|
53 |
for (RefModel ref : JGitUtils.getLocalBranches(repository, true, -1)) {
|
|
54 |
model.indexedBranches.add(ref.getName());
|
|
55 |
}
|
|
56 |
return model;
|
d896e6
|
57 |
}
|
JM |
58 |
|
|
59 |
@Test
|
|
60 |
public void testIndex() throws Exception {
|
|
61 |
LuceneExecutor lucene = newLuceneExecutor();
|
|
62 |
|
|
63 |
// reindex helloworld
|
|
64 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
40ca5c
|
65 |
RepositoryModel model = newRepositoryModel(repository);
|
JM |
66 |
lucene.reindex(model, repository);
|
d896e6
|
67 |
repository.close();
|
JM |
68 |
|
d04009
|
69 |
SearchResult result = lucene.search("type:blob AND path:bit.bit", 1, 1, model.name).get(0);
|
d896e6
|
70 |
assertEquals("Mike Donaghy", result.author);
|
d04009
|
71 |
result = lucene.search("type:blob AND path:clipper.prg", 1, 1, model.name).get(0);
|
d896e6
|
72 |
assertEquals("tinogomes", result.author);
|
JM |
73 |
|
|
74 |
// reindex theoretical physics
|
|
75 |
repository = GitBlitSuite.getTheoreticalPhysicsRepository();
|
40ca5c
|
76 |
model = newRepositoryModel(repository);
|
JM |
77 |
lucene.reindex(model, repository);
|
d896e6
|
78 |
repository.close();
|
JM |
79 |
|
|
80 |
// reindex JGit
|
|
81 |
repository = GitBlitSuite.getJGitRepository();
|
40ca5c
|
82 |
model = newRepositoryModel(repository);
|
JM |
83 |
lucene.reindex(model, repository);
|
d896e6
|
84 |
repository.close();
|
JM |
85 |
|
|
86 |
lucene.close();
|
|
87 |
}
|
|
88 |
|
|
89 |
@Test
|
|
90 |
public void testQuery() throws Exception {
|
|
91 |
LuceneExecutor lucene = new LuceneExecutor(null, GitBlitSuite.REPOSITORIES);
|
|
92 |
|
|
93 |
// 2 occurrences on the master branch
|
|
94 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
40ca5c
|
95 |
RepositoryModel model = newRepositoryModel(repository);
|
d896e6
|
96 |
repository.close();
|
JM |
97 |
|
d04009
|
98 |
List<SearchResult> results = lucene.search("ada", 1, 10, model.name);
|
d896e6
|
99 |
assertEquals(2, results.size());
|
JM |
100 |
for (SearchResult res : results) {
|
|
101 |
assertEquals("refs/heads/master", res.branch);
|
|
102 |
}
|
|
103 |
|
|
104 |
// author test
|
d04009
|
105 |
results = lucene.search("author: tinogomes AND type:commit", 1, 10, model.name);
|
d896e6
|
106 |
assertEquals(2, results.size());
|
JM |
107 |
|
|
108 |
// blob test
|
d04009
|
109 |
results = lucene.search("type: blob AND \"import std.stdio\"", 1, 10, model.name);
|
d896e6
|
110 |
assertEquals(1, results.size());
|
JM |
111 |
assertEquals("d.D", results.get(0).path);
|
|
112 |
|
|
113 |
// 1 occurrence on the gh-pages branch
|
|
114 |
repository = GitBlitSuite.getTheoreticalPhysicsRepository();
|
40ca5c
|
115 |
model = newRepositoryModel(repository);
|
d896e6
|
116 |
repository.close();
|
JM |
117 |
|
d04009
|
118 |
results = lucene.search("\"add the .nojekyll file\"", 1, 10, model.name);
|
d896e6
|
119 |
assertEquals(1, results.size());
|
JM |
120 |
assertEquals("Ondrej Certik", results.get(0).author);
|
|
121 |
assertEquals("2648c0c98f2101180715b4d432fc58d0e21a51d7", results.get(0).commitId);
|
|
122 |
assertEquals("refs/heads/gh-pages", results.get(0).branch);
|
|
123 |
|
d04009
|
124 |
results = lucene.search("type:blob AND \"src/intro.rst\"", 1, 10, model.name);
|
d896e6
|
125 |
assertEquals(4, results.size());
|
JM |
126 |
|
|
127 |
// hash id tests
|
d04009
|
128 |
results = lucene.search("commit:57c4f26f157ece24b02f4f10f5f68db1d2ce7ff5", 1, 10, model.name);
|
d896e6
|
129 |
assertEquals(1, results.size());
|
JM |
130 |
|
d04009
|
131 |
results = lucene.search("commit:57c4f26f157*", 1, 10, model.name);
|
d896e6
|
132 |
assertEquals(1, results.size());
|
JM |
133 |
|
|
134 |
// annotated tag test
|
|
135 |
repository = GitBlitSuite.getJGitRepository();
|
40ca5c
|
136 |
model = newRepositoryModel(repository);
|
d896e6
|
137 |
repository.close();
|
JM |
138 |
|
d04009
|
139 |
results = lucene.search("I663208919f297836a9c16bf458e4a43ffaca4c12", 1, 10, model.name);
|
d896e6
|
140 |
assertEquals(1, results.size());
|
JM |
141 |
assertEquals("[v1.3.0.201202151440-r]", results.get(0).tags.toString());
|
|
142 |
|
|
143 |
lucene.close();
|
|
144 |
}
|
|
145 |
|
|
146 |
@Test
|
|
147 |
public void testMultiSearch() throws Exception {
|
|
148 |
LuceneExecutor lucene = newLuceneExecutor();
|
|
149 |
List<String> list = new ArrayList<String>();
|
|
150 |
Repository repository = GitBlitSuite.getHelloworldRepository();
|
40ca5c
|
151 |
list.add(newRepositoryModel(repository).name);
|
d896e6
|
152 |
repository.close();
|
JM |
153 |
|
|
154 |
repository = GitBlitSuite.getJGitRepository();
|
40ca5c
|
155 |
list.add(newRepositoryModel(repository).name);
|
d896e6
|
156 |
repository.close();
|
JM |
157 |
|
d04009
|
158 |
List<SearchResult> results = lucene.search("test", 1, 10, list);
|
d896e6
|
159 |
lucene.close();
|
JM |
160 |
assertEquals(10, results.size());
|
|
161 |
}
|
|
162 |
} |