-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmfParsleyFixturesAndUtilitiesTestRule.xtend
337 lines (267 loc) · 8.14 KB
/
EmfParsleyFixturesAndUtilitiesTestRule.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
* Copyright (c) 2015 RCP Vision (http://www.rcp-vision.com) 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:
* Lorenzo Bettini - Initial contribution and API
*******************************************************************************/
package org.eclipse.emf.parsley.tests.util
import com.google.inject.Injector
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceImpl
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain
import org.eclipse.emf.edit.domain.EditingDomain
import org.eclipse.emf.parsley.examples.library.Book
import org.eclipse.emf.parsley.examples.library.EXTLibraryFactory
import org.eclipse.emf.parsley.examples.library.EXTLibraryPackage
import org.eclipse.emf.parsley.examples.library.Library
import org.eclipse.emf.parsley.examples.library.Writer
import org.eclipse.emf.parsley.tests.models.testmodels.ClassForControls
import org.eclipse.emf.parsley.tests.models.testmodels.TestContainer
import org.eclipse.emf.parsley.tests.models.testmodels.TestEClass
import org.eclipse.emf.parsley.tests.models.testmodels.TestmodelsFactory
import org.eclipse.emf.parsley.tests.models.testmodels.TestmodelsPackage
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import static org.eclipse.emf.parsley.examples.library.EXTLibraryFactory.*
import static extension org.junit.Assert.*
import org.eclipse.emf.parsley.tests.models.testmodels.DerivedClass
/**
* Objects, utility methods and other elements used in tests.
*
* @author Lorenzo Bettini - Initial contribution and API
*
*/
class EmfParsleyFixturesAndUtilitiesTestRule implements TestRule {
protected Library lib = null
protected Writer wr = null
protected Book b = null
var protected ResourceSet resourceSet;
val protected testPackage = TestmodelsPackage.eINSTANCE
val protected testFactory = TestmodelsFactory.eINSTANCE
val protected libraryFactory = EXTLibraryFactory.eINSTANCE
val protected libraryPackage = EXTLibraryPackage.eINSTANCE
val protected baseClass = testPackage.baseClass
val protected derivedClass = testPackage.derivedClass
var protected TestContainer testContainer
/**
* An instance to use for testing the creation of a Control
* using an AbstractControlFactory
*/
var protected ClassForControls classForControlsInstance
/**
* An instance to use for testing Ecore related mechanisms
*/
var protected TestEClass testEClassInstance
/**
* An instance to use for testing Ecore related mechanisms
*/
var protected DerivedClass derivedClassInstance
val static TEST_RESOURCE_URI = "resources/TestResource.xmi"
val static TEST_LIBRARY_RESOURCE_URI = "resources/TestLibraryResource.xmi"
override apply(Statement base, Description description) {
return [
setup();
base.evaluate();
]
}
def void setup() {
resourceSet = createResourceSet
lib = createTestLibrary
wr = lib.writers.head
b = lib.books.head
testContainer = createTestContainer
classForControlsInstance = createClassForControls
derivedClassInstance = createDerivedClassInstance
testEClassInstance = testFactory.createTestEClass
}
def getTestPackage() {
testPackage
}
def getTestContainer() {
testContainer
}
def setTestContainer(TestContainer testContainer) {
this.testContainer = testContainer
}
def getClassForControlsInstance() {
classForControlsInstance
}
def getDerivedClassInstance() {
derivedClassInstance
}
def getLibrary() {
lib
}
def getLibraryPackage() {
libraryPackage
}
def getWriter() {
wr
}
def getBook() {
b
}
def getLibraryFactory() {
libraryFactory
}
def getDerivedClass() {
derivedClass
}
def getBaseClass() {
baseClass
}
def getTestFactory() {
testFactory
}
def ResourceSet createResourceSet() {
new ResourceSetImpl
}
def createResourceInResouceSet() {
createResource => [
resourceSet.resources += it
]
}
def createResourceInResouceSet(ResourceSet resourceSet) {
createResource => [
resourceSet.resources += it
]
}
def createResource() {
new ResourceImpl
}
def EditingDomain getEditingDomain() {
return null
}
def createAdapterFactoryEditingDomain(Injector injector) {
injector.getInstance(AdapterFactoryEditingDomain)
}
def assertBooks(Writer w, int expectedSize) {
expectedSize.assertEquals(w.books.size)
}
def booksByReflection(Writer w) {
w.eGet(EXTLibraryPackage.eINSTANCE.writer_Books) as List<Book>
}
def addBooksByReflection(Writer w, List<Book> books) {
w.eSet(EXTLibraryPackage.eINSTANCE.writer_Books, books)
}
def createTestLibrary() {
eINSTANCE.createLibrary() => [
name = "TEST"
val writer = createTestWriter("Writer")
val book = createTestBook("Book")
writers += writer
books += book
writer.books += book
]
}
def createTestWriter(String t) {
eINSTANCE.createWriter() => [
firstName = t
]
}
def createTestBook(String t) {
eINSTANCE.createBook() => [ title = t ]
}
def addTestBook(String t) {
lib.books += createTestBook(t)
}
def createClassWithName(Resource res, String n) {
createClassWithName(n) => [
res.contents += it
]
}
def createClassWithName(String n) {
testFactory.createClassWithName => [name = n]
}
def createClassForTable(String n) {
testFactory.createClassForTable => [
classWithName1 = createClassWithName(n)
classWithName2 = createClassWithName(n)
]
}
def createTestContainerInResource() {
testFactory.createTestContainer => [
createResourceInResouceSet.contents += it
]
}
def createTestContainer(Resource res, String name) {
createTestContainer(res) => [
it.name = name
]
}
def createTestContainer(Resource res) {
testFactory.createTestContainer => [
res.contents += it
]
}
def createTestContainer() {
testFactory.createTestContainer
}
def createClassForControls() {
testFactory.createClassForControls
}
def createDerivedClassInstance() {
testFactory.createDerivedClass
}
def createClassForFeatureMapEntry1(String n) {
testFactory.createClassForFeatureMapEntry1 => [name = n]
}
def createClassForFeatureMapEntry2(String n) {
testFactory.createClassForFeatureMapEntry2 => [name = n]
}
/**
* Registers XMI resource factory and registers TestmodelsPackage
* so that testable resources can be loaded with
* URI.createURI("http:///My.testmodels")
*/
def setupResouceFactory(ResourceSet resourceSet) {
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
resourceSet.getPackageRegistry().put
(TestmodelsPackage.eNS_URI,
TestmodelsPackage.eINSTANCE);
resourceSet.getPackageRegistry().put
(EXTLibraryPackage.eNS_URI,
EXTLibraryPackage.eINSTANCE);
return resourceSet
}
def createAndSetupResourceSet() {
createResourceSet.setupResouceFactory
}
def createTestResource() {
val resourceSet = createAndSetupResourceSet
val resource = resourceSet.createResource(URI.createURI(TEST_RESOURCE_URI))
return resource
}
def loadTestResource() {
val resourceSet = createAndSetupResourceSet
val resource = resourceSet.getResource(URI.createURI(TEST_RESOURCE_URI), true)
return resource
}
def createTestLibrayResource() {
val resourceSet = createAndSetupResourceSet
val resource = resourceSet.createResource(URI.createURI(TEST_LIBRARY_RESOURCE_URI))
return resource
}
def createTestLibrayResourceAndInitialize() {
return createTestLibrayResource => [
contents += createTestLibrary
]
}
def loadTestLibraryResource() {
val resourceSet = createAndSetupResourceSet
val resource = resourceSet.getResource(URI.createURI(TEST_LIBRARY_RESOURCE_URI), true)
return resource
}
}