/
DotReferenceFinderTest.xtend
315 lines (264 loc) · 8.79 KB
/
DotReferenceFinderTest.xtend
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*******************************************************************************
* Copyright (c) 2018, 2020 itemis AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tamas Miklossy (itemis AG) - initial API and implementation (bug #531049)
*******************************************************************************/
package org.eclipse.gef.dot.tests
import com.google.inject.Inject
import java.util.Collections
import java.util.List
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.NullProgressMonitor
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.gef.dot.internal.language.dot.DotAst
import org.eclipse.gef.dot.internal.language.dot.EdgeRhsNode
import org.eclipse.gef.dot.internal.language.dot.EdgeStmtNode
import org.eclipse.gef.dot.internal.language.dot.NodeStmt
import org.eclipse.gef.dot.tests.ui.DotUiInjectorProvider
import org.eclipse.search.ui.IQueryListener
import org.eclipse.search.ui.ISearchQuery
import org.eclipse.search.ui.ISearchResult
import org.eclipse.search.ui.NewSearchUI
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.ui.editor.findrefs.ReferenceQueryExecutor
import org.eclipse.xtext.ui.editor.findrefs.ReferenceSearchResult
import org.eclipse.xtext.ui.refactoring.ui.SyncUtil
import org.eclipse.xtext.ui.resource.IResourceSetProvider
import org.eclipse.xtext.ui.testing.AbstractEditorTest
import org.eclipse.xtext.xbase.lib.Functions.Function1
import org.junit.AfterClass
import org.junit.Test
import org.junit.runner.RunWith
import static org.eclipse.gef.dot.tests.DotTestUtils.createTestProjectWithXtextNature
import static extension org.eclipse.gef.dot.tests.DotTestUtils.createTestFile
@RunWith(XtextRunner)
@InjectWith(DotUiInjectorProvider)
class DotReferenceFinderTest extends AbstractEditorTest {
@Inject extension SyncUtil
@Inject IResourceSetProvider resourceSetProvider
@Inject ReferenceQueryExecutor referenceQueryExecutor
ISearchResult searchResult = null
override setUp() {
super.setUp
createTestProjectWithXtextNature
}
@Test def finding_references_001() {
'''
graph {
1
}
'''.testFindingReferences([firstNode], "DOT References to node '1' (/dottestproject/test.dot)", list(
[firstNode]
))
}
@Test def finding_references_002() {
'''
graph {
1
2
}
'''.testFindingReferences([secondNode], "DOT References to node '2' (/dottestproject/test.dot)", list(
[secondNode]
))
}
@Test def finding_references_003() {
'''
graph {
1
1--2
}
'''.testFindingReferences([firstNode], "DOT References to node '1' (/dottestproject/test.dot)", list(
[firstNode],
[sourceNodeOfFirstEdge]
))
}
@Test def finding_references_004() {
'''
graph {
1
1--2
}
'''.testFindingReferences([sourceNodeOfFirstEdge], "DOT References to node '1' (/dottestproject/test.dot)", list(
[firstNode],
[sourceNodeOfFirstEdge]
))
}
@Test def finding_references_005() {
'''
graph {
1--2
3--4
}
'''.testFindingReferences([sourceNodeOfSecondEdge], "DOT References to node '3' (/dottestproject/test.dot)", list(
[sourceNodeOfSecondEdge]
))
}
@Test def finding_references_006() {
'''
graph {
1--2
3--4
}
'''.testFindingReferences([targetNodeOfFirstEdge], "DOT References to node '2' (/dottestproject/test.dot)", list(
[targetNodeOfFirstEdge]
))
}
@Test def finding_references_007() {
'''
graph {
1--2
3--4
}
'''.testFindingReferences([targetNodeOfSecondEdge], "DOT References to node '4' (/dottestproject/test.dot)", list(
[targetNodeOfSecondEdge]
))
}
@Test def finding_references_008() {
'''
graph {
1--2
2--2
}
'''.testFindingReferences([targetNodeOfFirstEdge], "DOT References to node '2' (/dottestproject/test.dot)", list(
[targetNodeOfFirstEdge],
[sourceNodeOfSecondEdge],
[targetNodeOfSecondEdge]
))
}
@Test def finding_references_009() {
'''
graph {
1--2
2--2
}
'''.testFindingReferences([sourceNodeOfSecondEdge], "DOT References to node '2' (/dottestproject/test.dot)", list(
[targetNodeOfFirstEdge],
[sourceNodeOfSecondEdge],
[targetNodeOfSecondEdge]
))
}
@Test def finding_references_010() {
'''
graph {
1--2
2--2
}
'''.testFindingReferences([targetNodeOfSecondEdge], "DOT References to node '2' (/dottestproject/test.dot)", list(
[targetNodeOfFirstEdge],
[sourceNodeOfSecondEdge],
[targetNodeOfSecondEdge]
))
}
private def testFindingReferences(CharSequence it, (DotAst)=>EObject element, String label, List<(DotAst)=>EObject> elements) {
// given
dslFile.
// when
searchingReferencesOn(element).
// then
searchViewDisplays(label).
// and
searchViewContains(elements)
}
private def dslFile(CharSequence it) {
val testFile = toString.createTestFile
val project = testFile.project
val resourceSet = resourceSetProvider.get(project)
val projectFullPath = project.fullPath.toString
val uri = URI.createPlatformResourceURI(projectFullPath + "/" + testFile.name , true)
val resource = resourceSet.createResource(uri)
resource.load(newHashMap)
resource
}
private def searchingReferencesOn(Resource resource, (DotAst)=>EObject elementProvider) {
val dotAst = resource.contents.head as DotAst
val element = elementProvider.apply(dotAst)
waitForBuild(new NullProgressMonitor)
referenceQueryExecutor.init(element)
NewSearchUI.addQueryListener( new IQueryListener() {
override queryAdded(ISearchQuery query) {
}
override queryFinished(ISearchQuery query) {
searchResult = query.searchResult
}
override queryRemoved(ISearchQuery query) {
}
override queryStarting(ISearchQuery query) {
}
});
referenceQueryExecutor.execute
while(searchResult===null) {
Thread.sleep(100)
}
element
}
private def searchViewDisplays(EObject element, String label) {
val actualLabel = referenceQueryExecutor.getLabel(element)
label.assertEquals(actualLabel)
element
}
private def searchViewContains(EObject element, List<(DotAst)=>EObject> expectedReferences) {
val matchingReferences = (searchResult as ReferenceSearchResult).matchingReferences
val resource = element.eResource
val dotAst = resource.contents.head as DotAst
val actual = matchingReferences.map[sourceEObjectUri.fragment].sort.join(System.lineSeparator)
val expected = expectedReferences.map[resource.getURIFragment(apply(dotAst))].sort.join(System.lineSeparator)
expected.assertEquals(actual)
}
private def getFirstNode(DotAst it) {
nodeStmts.head.node
}
private def getSecondNode(DotAst it) {
nodeStmts.get(1).node
}
private def getSourceNodeOfFirstEdge(DotAst it) {
edgeStmtNodes.head.node
}
private def getSourceNodeOfSecondEdge(DotAst it) {
edgeStmtNodes.get(1).node
}
private def getTargetNodeOfFirstEdge(DotAst it) {
edgeStmtNodes.head.targetNode
}
private def getTargetNodeOfSecondEdge(DotAst it) {
edgeStmtNodes.get(1).targetNode
}
private def nodeStmts(DotAst it) {
stmts.filter(NodeStmt)
}
private def edgeStmtNodes(DotAst it) {
stmts.filter(EdgeStmtNode)
}
private def stmts(DotAst it) {
graphs.head.stmts
}
private def targetNode(EdgeStmtNode it) {
(edgeRHS.head as EdgeRhsNode).node
}
/**
* This workaround is necessary when using the xtend compiler in a version less than 2.8.0, otherwise, the generated code cannot be compiled. The error messages:
* The method testFindingReferences(CharSequence, Functions.Function1<? super DotAst,? extends EObject>, String, List<Functions.Function1<? super DotAst,? extends EObject>>) in the type DotReferenceFinderTests is not applicable for the arguments
* (StringConcatenation, Functions.Function1<DotAst,EObject>, String, List<Functions.Function1<DotAst,EObject>>)
* TODO: remove this workaround as soon as at least the xtend-maven plugin 2.8.0 is used in the build process.
*/
private def list(Function1<? super DotAst, ? extends EObject>... initial) {
Collections.<Function1<? super DotAst, ? extends EObject>>unmodifiableList(CollectionLiterals.<Function1<? super DotAst, ? extends EObject>>newArrayList(initial))
}
@AfterClass def static void cleanup() {
/**
* The Eclipse workspace needs to be explicitly saved after the test execution
* otherwise, the test case executions are resulting in a NullPointerException.
* For more information, see
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=460996
*/
ResourcesPlugin.workspace.save(true, null)
}
}