-
Notifications
You must be signed in to change notification settings - Fork 17
/
EquivalenceTestExecutable.xtend
248 lines (217 loc) · 9.17 KB
/
EquivalenceTestExecutable.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
package tools.vitruv.applications.cbs.testutils.equivalencetest
import java.io.IOException
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.util.Collection
import java.util.LinkedHashSet
import java.util.List
import java.util.Map
import java.util.Optional
import java.util.Set
import java.util.function.Predicate
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Delegate
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
import org.junit.jupiter.api.function.Executable
import tools.vitruv.applications.cbs.testutils.MetamodelDescriptor
import tools.vitruv.applications.cbs.testutils.ModelComparisonSettings
import tools.vitruv.change.propagation.ChangePropagationSpecification
import tools.vitruv.framework.vsum.VirtualModelBuilder
import tools.vitruv.testutils.TestProjectManager
import tools.vitruv.testutils.TestUserInteraction
import tools.vitruv.testutils.printing.DefaultPrintIdProvider
import tools.vitruv.testutils.printing.ModelPrinting
import tools.vitruv.testutils.printing.PrintIdProvider
import tools.vitruv.testutils.printing.UriReplacingPrinter
import tools.vitruv.testutils.views.BasicTestView
import tools.vitruv.testutils.views.ChangePublishingTestView
import tools.vitruv.testutils.views.TestView
import tools.vitruv.testutils.views.UriMode
import static com.google.common.base.Preconditions.checkNotNull
import static java.nio.file.FileVisitResult.*
import static org.hamcrest.MatcherAssert.assertThat
import static tools.vitruv.testutils.matchers.ModelMatchers.containsModelOf
import static tools.vitruv.testutils.printing.PrintMode.*
import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*
import static extension java.nio.file.Files.walkFileTree
import static extension tools.vitruv.testutils.printing.ModelPrinting.*
@FinalFieldsConstructor
package class EquivalenceTestExecutable implements Executable, AutoCloseable {
static val testProjectManager = new TestProjectManager()
val MetamodelStep testStep
val Map<MetamodelDescriptor, List<MetamodelStep>> dependencySteps
val Map<MetamodelDescriptor, MetamodelStep> referenceSteps
val Collection<ChangePropagationSpecification> changePropagationSpecifications
val UriMode uriMode
val ModelComparisonSettings comparisonSettings
val EquivalenceTestExtensionContext extensionContext
override execute() throws Throwable {
try (
val testView = setupTestView();
val referenceView = setupReferenceView();
val printerChange = installViewDirectoryUriReplacement(testView, referenceView)
) {
checkNotNull(printerChange) // Suppress warning for variable not being used
executeDependencies(testView, referenceView)
testStep.executeIn(testView)
referenceSteps.values.forEach[executeIn(referenceView)]
verifyTestViewResults()
} catch (Throwable t) {
extensionContext.executionException = Optional.of(t)
throw t
} finally {
close()
}
}
def private DirectoryTestView setupTestView() {
val viewDirectory = testProjectManager.getProject("", extensionContext)
val vsumDirectory = testProjectManager.getProject("vsum", extensionContext)
val changePropagationSpecifications = this.changePropagationSpecifications
val userInteraction = new TestUserInteraction
val vsum = new VirtualModelBuilder().withStorageFolder(vsumDirectory).withUserInteractorForResultProvider(
new TestUserInteraction.ResultProvider(userInteraction)).
withChangePropagationSpecifications(changePropagationSpecifications).buildAndInitialize()
new DirectoryTestView(
new ChangePublishingTestView(viewDirectory, userInteraction, uriMode, vsum, vsum.uuidResolver) [
vsum.getModelInstance(it)?.resource
],
viewDirectory
)
}
def private DirectoryTestView setupReferenceView() {
newBasicView(testProjectManager.getProject("reference", extensionContext))
}
def private DirectoryTestView setupReadOnlyTestView() {
newBasicView(testProjectManager.getProject("", extensionContext))
}
def private executeDependencies(TestView testView, TestView referenceView) {
for (dependencyTestStep : dependencySteps.getOrDefault(testStep.targetMetamodel, emptyList)) {
dependencyTestStep.executeIn(testView)
}
for (referenceMetamodel : referenceMetamodels) {
for (dependencyReferenceStep : this.dependencySteps.getOrDefault(referenceMetamodel, emptyList)) {
dependencyReferenceStep.executeIn(referenceView)
}
}
verifyTestViewResults()
}
def private void verifyTestViewResults() throws Throwable {
// use new views to reload the actual resources from disk
try(
val testView = setupReadOnlyTestView();
val referenceView = setupReferenceView()
) {
val referenceFiles = referenceView.directory.dataFiles [ file |
referenceMetamodels.exists[file.belongsTo(it)]
]
assertThat(testView, containsExactlyResources(referenceView, referenceFiles))
referenceFiles.forEach [ model |
val referenceResource = referenceView.resourceAt(model)
val referenceMetamodel = checkNotNull(
referenceMetamodels.findFirst[referenceResource.belongsTo(it)],
'''Cannot find metamodel of «referenceResource»!'''
)
val filters = comparisonSettings.getEqualityOptionsForMetamodel(referenceMetamodel)
assertThat(testView.resourceAt(model), containsModelOf(referenceResource, filters))
]
}
}
def private AutoCloseable installViewDirectoryUriReplacement(TestView testView, TestView referenceView) {
ModelPrinting.prepend(new UriReplacingPrinter(List.of(
// mind the order, the test view is a prefix of the reference view!
referenceView.getUri(Path.of('.')).appendSegment('') -> URI.createFileURI("[reference view]/"),
testView.getUri(Path.of('.')).appendSegment('') -> URI.createFileURI("[test view]/")
)))
}
override close() {
testProjectManager.afterEach(extensionContext)
extensionContext.close()
}
def private static dataFiles(Path path, Predicate<Path> predicate) {
val result = new LinkedHashSet()
path.walkFileTree(new SimpleFileVisitor<Path> {
override preVisitDirectory(Path dir, BasicFileAttributes attrs) {
val dirName = dir.fileName.toString
if (dirName.startsWith('[') && dirName.endsWith(']') && dir != path)
SKIP_SUBTREE
else
CONTINUE
}
override visitFile(Path file, BasicFileAttributes attrs) throws IOException {
val relativized = path.relativize(file)
if (predicate.test(relativized)) {
result.add(relativized)
}
CONTINUE
}
})
return result
}
def static private belongsTo(Path path, MetamodelDescriptor metamodel) {
metamodel.fileExtensions.exists[path.toString.endsWith('''.«it»''')]
}
def static private belongsTo(Resource resource, MetamodelDescriptor metamodel) {
metamodel.fileExtensions.contains(resource.URI.fileExtension)
}
def private Matcher<? super DirectoryTestView> containsExactlyResources(TestView referenceView,
Set<Path> referenceFiles) {
new ModelFilesMatcher(referenceFiles, referenceView, referenceMetamodels)
}
def private getReferenceMetamodels() {
referenceSteps.keySet
}
def private newBasicView(Path viewDirectory) {
new DirectoryTestView(new BasicTestView(viewDirectory, uriMode), viewDirectory)
}
@FinalFieldsConstructor
private static class DirectoryTestView implements TestView {
@Delegate
val TestView delegate
@Accessors
val Path directory
}
@FinalFieldsConstructor
private static class ModelFilesMatcher extends TypeSafeMatcher<DirectoryTestView> {
val Set<Path> referenceFiles
val TestView referenceView
val Set<MetamodelDescriptor> referenceMetamodels
var Set<Path> testFiles
val PrintIdProvider idProvider = new DefaultPrintIdProvider()
override describeTo(Description description) {
description.appendText("exactly these resource paths to exist in the test view: ").appendPrintResult [
printSet(referenceFiles, MULTI_LINE_LIST) [ subTarget, path |
subTarget.print(path.toString)
]
]
}
override matchesSafely(DirectoryTestView testView) {
testFiles = testView.directory.dataFiles [ file |
referenceMetamodels.exists[file.belongsTo(it)]
]
return testFiles == referenceFiles
}
override describeMismatchSafely(DirectoryTestView testView, Description mismatchDescription) {
val missingResources = (new LinkedHashSet(referenceFiles) => [removeAll(testFiles)]).mapFixedTo(
new LinkedHashSet)[referenceView.resourceAt(it)]
val unexpectedResources = (new LinkedHashSet(testFiles) => [removeAll(referenceFiles)]).mapFixedTo(
new LinkedHashSet)[referenceView.resourceAt(it)]
if (!missingResources.isEmpty) {
mismatchDescription.appendText("the following resources are missing in the test view: ").
appendModelValueSet(missingResources, MULTI_LINE_LIST, idProvider)
}
if (!unexpectedResources.isEmpty) {
if (!missingResources.isEmpty) {
mismatchDescription.appendText(System.lineSeparator).appendText(' and ')
}
mismatchDescription.appendText("the test view contains the following unexpected resources: ").
appendModelValueSet(unexpectedResources, MULTI_LINE_LIST, idProvider)
}
}
}
}