/
SearchIndexTests.scala
239 lines (196 loc) · 8.49 KB
/
SearchIndexTests.scala
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package scala.tools.colladoc.model
import tools.nsc.doc.model._
import org.specs2.SpecificationWithJUnit
import tools.nsc.doc.model.MemberEntity
import org.apache.lucene.store.{RAMDirectory, Directory}
import tools.nsc.doc.model.Package
import org.apache.lucene.document.Document
import org.apache.lucene.index.IndexReader
object SearchIndexTests extends SpecificationWithJUnit with EntityMemberMock with DocTemplateEntityMock {
var directory: Directory = _
"Search Index" should {
doBefore {
directory = new RAMDirectory
construct()
}
"Use the FSDirectory that is given to it on creation" in {
expect {
expectationsForEmptyPackage
}
val index = new SearchIndex(mockPackage, directory, commentMapper)
index.directory must beEqualTo(directory)
}
"Index the root package" in {
expect {
expectationsForEmptyPackage
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqualTo(1)
docs(0).get(SearchIndex.nameField) mustEqual packageName.toLowerCase
docs(0).get(SearchIndex.commentField) mustEqual defaultUserComment
}
"Index Entity: name, entityId and user-defined comment" in {
val mockEntity = mock[MemberEntity]
expect {
expectationsForPackageWithEntity(mockEntity)
expectationsForAnyMemberEntityWithUserComment(mockEntity)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.nameField) mustEqual entityName.toLowerCase
docs(1).get(SearchIndex.commentField) mustEqual defaultUserComment
docs(1).get(SearchIndex.entityLookupField) mustEqual mockEntity.hashCode.toString
}
"Index Entities without comments" in {
val mockEntity = mock[MemberEntity]
expect {
expectationsForPackageWithEntity(mockEntity)
expectationsForAnyMemberEntityWithoutComment(mockEntity)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.nameField) mustEqual entityName.toLowerCase
docs(1).get(SearchIndex.commentField) mustEqual ""
docs(1).get(SearchIndex.entityLookupField) mustEqual mockEntity.hashCode.toString
}
"Index Entities with default comments" in {
val mockEntity = mock[MemberEntity]
expect {
expectationsForPackageWithEntity(mockEntity)
expectationsForAnyMemberEntityWithComment(mockEntity)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.nameField) mustEqual entityName.toLowerCase
docs(1).get(SearchIndex.commentField) mustEqual defaultComment
docs(1).get(SearchIndex.entityLookupField) mustEqual mockEntity.hashCode.toString
}
// TODO
/*"Index class: visibility, parentClass, traits that extends" in {
expect {
defaultExpectationsForPackage()
one(mockPackage).members.willReturn(List[MemberEntity](mockClass))
one(mockClass).linearizationTemplates.willReturn(List[TemplateEntity](traitTemplateEntity, notTraitTemplateEntity))
expectationsForDocTemplateEntity(mockClass)
expectationsForAnyMemberEntityWithUserComment(mockClass)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.visibilityField) mustEqual classVisibility
docs(1).get(SearchIndex.withsField) mustEqual traitTemplateEntityName.toLowerCase
docs(1).get(SearchIndex.typeField) mustEqual SearchIndex.classField
}
"Index trait: visibility, parentClass, traits that extends" in {
expect {
defaultExpectationsForPackage()
one(mockPackage).members.willReturn(List[MemberEntity](mockTrait))
one(mockTrait).linearizationTemplates.willReturn(List[TemplateEntity](traitTemplateEntity, notTraitTemplateEntity))
expectationsForDocTemplateEntity(mockTrait)
expectationsForAnyMemberEntityWithUserComment(mockTrait)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.visibilityField) mustEqual classVisibility
docs(1).get(SearchIndex.withsField) mustEqual traitTemplateEntityName.toLowerCase
docs(1).get(SearchIndex.typeField) mustEqual SearchIndex.traitField
}
"Index object: visibility, parentClass, traits that extends" in {
expect {
defaultExpectationsForPackage()
one(mockPackage).members.willReturn(List[MemberEntity](mockObject))
one(mockObject).linearizationTemplates.willReturn(List[TemplateEntity](traitTemplateEntity, notTraitTemplateEntity))
expectationsForDocTemplateEntity(mockObject)
expectationsForAnyMemberEntityWithUserComment(mockObject)
}
new SearchIndex(mockPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.visibilityField) mustEqual classVisibility
docs(1).get(SearchIndex.withsField) mustEqual traitTemplateEntityName.toLowerCase
docs(1).get(SearchIndex.typeField) mustEqual SearchIndex.objectField
}
"Index Def and stores its number of parameters, visibility, return value" in {
val mockDef = mock[Def]
val mockVisibility = mock[Visibility]
val mockReturnParam = mock[TypeParam]
val mockParam = mock[TypeParam]
val returnParamName = "Return Param Name"
val defVisibility = "public"
expect {
expectationsForPackageWithEntity(mockDef)
one(mockDef).typeParams willReturn(List[TypeParam](mockParam))
one(mockDef).visibility willReturn mockVisibility
one(mockDef).resultType willReturn mockReturnParam
one(mockReturnParam).name willReturn returnParamName
expectationsForAnyMemberEntity(mockEntity)
}
val index = new SearchIndex(mockPackage, directory)
val docs = getAllDocs(directory)
docs(0).get(SearchIndex.returnsField) mustEqual returnParamName
docs(0).get(SearchIndex.typeParamsCountField) mustEqual(1)
docs(0).get(SearchIndex.visibilityField) mustEqual defVisibility
}*/
"Only index each entity once" in {
val directory = new RAMDirectory
val packageName = "foo"
val mockRootPackage = mock[Package]
expect {
exactly(1).of(mockRootPackage).name willReturn (packageName.toLowerCase)
one(commentMapper).latestToString(mockRootPackage) willReturn (Some(defaultUserComment))
// This entity returns itself as a member, putting us in a situation where
// we could index it twice (or infinitely) if we are not careful.
one(mockRootPackage).members willReturn (List[MemberEntity](mockRootPackage))
}
new SearchIndex(mockRootPackage, directory, commentMapper)
val docs = getAllDocs(directory)
docs.length must beEqual(1)
}
// "Index all inherited classes and their parents" in {
// // TODO: Implement this...
// assert(false)
// }
"Reindex document when the comment is updated" in {
// 1. check the initial comment for the entity
val mockEntity = mock[MemberEntity]
expect {
expectationsForPackageWithEntity(mockEntity)
expectationsForAnyMemberEntityWithUserComment(mockEntity)
}
val index = new SearchIndex(mockPackage, directory, commentMapper)
var docs = getAllDocs(directory)
docs.length must beEqual(2)
docs(1).get(SearchIndex.commentField) mustEqual defaultUserComment
// 2. Change the comment
expect {
one(commentMapper).latestToString(mockEntity) willReturn (Some(updatedUserComment))
}
index.reindexEntityComment(mockEntity)
// 3. Verify that the document exist
// 4. And the new comment is indexed
docs = getAllDocs(directory)
docs(1).get(SearchIndex.commentField) mustEqual updatedUserComment
}
}
private def getAllDocs(dir: Directory) = {
var docs = List[Document]()
var reader: IndexReader = null
try {
reader = IndexReader.open(dir, true)
for (i <- 0 until reader.numDocs()) {
docs = docs ::: List[Document](reader.document(i))
}
}
finally {
if (reader != null) {
reader.close()
}
}
docs
}
}