-
Notifications
You must be signed in to change notification settings - Fork 3
/
AbstractLanguageTestSuite.java
668 lines (596 loc) · 21.3 KB
/
AbstractLanguageTestSuite.java
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*******************************************************************************
* Copyright (c) 2016, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*
*******************************************************************************/
package org.eclipse.acceleo.tests.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.parser.AcceleoAstResult;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.aql.validation.AcceleoValidator;
import org.eclipse.acceleo.aql.validation.IAcceleoValidationResult;
import org.eclipse.acceleo.aql.validation.quickfixes.AcceleoQuickFixesSwitch;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.ast.ASTNode;
import org.eclipse.acceleo.query.parser.quickfixes.IAstQuickFix;
import org.eclipse.acceleo.query.parser.quickfixes.IAstResourceChange;
import org.eclipse.acceleo.query.parser.quickfixes.IAstTextReplacement;
import org.eclipse.acceleo.query.runtime.IValidationMessage;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.namespace.ClassLoaderQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.impl.namespace.JavaLoader;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/**
* Run a folder with templates as a test suite JUnit.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
@RunWith(Parameterized.class)
public abstract class AbstractLanguageTestSuite {
/**
* Copy buffer size.
*/
private static final int BUFFER_SIZE = 8192;
/**
* UTF-8 content.
*/
public static final String UTF_8 = "UTF-8";
/**
* The default EOL character to use.
*/
public static final char DEFAULT_END_OF_LINE_CHARACTER = '\n';
/**
* The {@link MemoryURIHandler} that check we don't have adherence to {@link File}.
*/
private static MemoryURIHandler uriHandler = new MemoryURIHandler();
/**
* The {@link AcceleoAstResult}.
*/
protected final AcceleoAstResult astResult;
/**
* The {@link IQualifiedNameQueryEnvironment}.
*/
protected static IQualifiedNameQueryEnvironment queryEnvironment;
/**
* The {@link ResourceSet} for models.
*/
protected static ResourceSet resourceSetForModels;
/**
* The {@link AcceleoEvaluator}.
*/
protected final AcceleoEvaluator evaluator;
/**
* The memory destination {@link String}.
*/
protected final String memoryDestinationString;
/**
* The memoty destination {@link URI}.
*/
protected final URI memoryDestination;
/**
* The {@link ModuleAstSerializer}.
*/
private final ModuleAstSerializer moduleAstSerializer = new ModuleAstSerializer();
/**
* The test folder path.
*/
private final String testFolderPath;
/**
* The module qualified name.
*/
private final String qualifiedName;
/**
* The {@link IAcceleoValidationResult}.
*/
private final IAcceleoValidationResult validationResult;
/**
* The {@link Module} text.
*/
private final String moduleText;
/**
* Constructor.
*
* @param testFolder
* the test folder path
* @throws IOException
* if the tested template can't be read
*/
public AbstractLanguageTestSuite(String testFolder) throws IOException {
this.memoryDestinationString = "acceleotests://" + testFolder + "/";
this.memoryDestination = URI.createURI(memoryDestinationString);
this.testFolderPath = testFolder;
final File testFolderFile = new File(testFolderPath);
final File moduleFile = getModuleFile(testFolderFile);
try (InputStream is = new FileInputStream(moduleFile)) {
moduleText = AcceleoUtil.getContent(is, StandardCharsets.UTF_8.name());
}
final Path rootPath = testFolderFile.toPath().getName(0);
final URL[] urls = new URL[] {testFolderFile.toPath().getName(0).toUri().toURL() };
final ClassLoader classLoader = new URLClassLoader(urls, getClass().getClassLoader());
final IQualifiedNameResolver resolver = new ClassLoaderQualifiedNameResolver(classLoader,
AcceleoParser.QUALIFIER_SEPARATOR);
// TODO get options form ??? or list all possible options ?
// don't add any options ?
final Map<String, String> options = new LinkedHashMap<>();
final ArrayList<Exception> exceptions = new ArrayList<>();
// the ResourceSet will not be used
resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, this, getResourceSet(),
options);
// TODO report exceptions
queryEnvironment = AcceleoUtil.newAcceleoQueryEnvironment(options, resolver, resourceSetForModels,
true);
evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine());
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(new JavaLoader(AcceleoParser.QUALIFIER_SEPARATOR, false));
String namespace = rootPath.relativize(testFolderFile.toPath()).toString().replace(File.separator,
"::") + "::";
qualifiedName = namespace + moduleFile.getName().substring(0, moduleFile.getName().lastIndexOf('.'));
final Object resolved = resolver.resolve(qualifiedName);
if (resolved instanceof Module) {
astResult = ((Module)resolved).getAst();
} else {
astResult = null;
}
final AcceleoValidator validator = new AcceleoValidator(queryEnvironment);
validationResult = validator.validate(astResult, qualifiedName);
}
@AfterClass
public static void afterClass() {
AQLUtils.cleanResourceSetForModels(queryEnvironment.getLookupEngine().getResolver(),
resourceSetForModels);
AcceleoUtil.cleanServices(queryEnvironment, resourceSetForModels);
}
/**
* Gets the test folder path.
*
* @return the test folder path
*/
protected String getTestFolderPath() {
return testFolderPath;
}
/**
* Gets the {@link ResourceSet}.
*
* @return the {@link ResourceSet}
*/
protected ResourceSet getResourceSet() {
ResourceSetImpl res = new ResourceSetImpl();
res.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
res.getURIConverter().getURIHandlers().add(0, uriHandler);
return res;
}
/**
* Tests the parsing by comparing a textual representation of the AST.
*
* @throws IOException
* if the expected AST file can't be read
* @throws FileNotFoundException
* if the expected AST file can't be found
*/
@Test
public void parsing() throws FileNotFoundException, IOException {
final File expectedASTFile = getExpectedASTFile(new File(testFolderPath));
final String actualAst = moduleAstSerializer.serialize(astResult);
if (!expectedASTFile.exists()) {
final File actualASTFile = getActualASTFile(new File(testFolderPath));
if (!actualASTFile.exists()) {
actualASTFile.createNewFile();
}
setContent(new FileOutputStream(actualASTFile), UTF_8, actualAst);
fail("file doesn't exist.");
}
try (FileInputStream stream = new FileInputStream(expectedASTFile)) {
final String expectedAst = AcceleoUtil.getContent(stream, UTF_8);
assertEquals(expectedAst, actualAst);
stream.close();
}
}
/**
* Tests the validation of a template.
*
* @throws FileNotFoundException
* if the file can't be found
* @throws IOException
* if the given stream can't be written to
*/
@Test
public void validation() throws FileNotFoundException, IOException {
final List<IValidationMessage> messages = validationResult.getValidationMessages();
final String actualContent = getValidationContent(messages);
final File expectedFile = getExpectedValidationFile(new File(testFolderPath));
final File actualFile = getActualValidationFile(new File(testFolderPath));
if (!expectedFile.exists()) {
if (!actualFile.exists() && !expectedFile.exists()) {
actualFile.createNewFile();
}
try (FileOutputStream stream = new FileOutputStream(actualFile);) {
setContent(stream, UTF_8, actualContent);
}
fail("file doesn't exist.");
} else {
String expectedContent = "";
try (FileInputStream stream = new FileInputStream(expectedFile);) {
expectedContent = AcceleoUtil.getContent(stream, UTF_8);
}
assertEquals(expectedContent, actualContent);
}
}
/**
* Tests the quick fixes.
*
* @throws FileNotFoundException
* if the file can't be found
* @throws IOException
* if the given stream can't be written to
*/
@Test
public void quickFixes() throws FileNotFoundException, IOException {
final String actualContent = getQuickFixesContent(validationResult);
final File expectedFile = getExpectedQuickFixesFile(new File(testFolderPath));
final File actualFile = getActualQuickFixesFile(new File(testFolderPath));
if (!expectedFile.exists()) {
if (!actualFile.exists() && !expectedFile.exists()) {
actualFile.createNewFile();
}
try (FileOutputStream stream = new FileOutputStream(actualFile);) {
setContent(stream, UTF_8, actualContent);
}
fail("file doesn't exist.");
} else {
String expectedContent = "";
try (FileInputStream stream = new FileInputStream(expectedFile);) {
expectedContent = AcceleoUtil.getContent(stream, UTF_8);
}
assertEquals(getPortableString(expectedContent), getPortableString(actualContent));
}
}
/**
* Serializes the given {@link List} of {@link IValidationMessage}.
*
* @param messages
* the {@link List} of {@link IValidationMessage}
* @return the string representation of the given {@link List} of {@link IValidationMessage}
*/
private String getValidationContent(List<IValidationMessage> messages) {
final String res;
if (messages.size() != 0) {
final StringBuilder builder = new StringBuilder();
for (IValidationMessage message : messages) {
builder.append(message.getLevel());
builder.append(" ");
builder.append(message.getMessage());
builder.append(" - ");
builder.append(message.getStartPosition());
builder.append(" ");
builder.append(message.getEndPosition());
builder.append(DEFAULT_END_OF_LINE_CHARACTER);
}
res = builder.substring(0, builder.length() - 1);
} else {
res = "";
}
return res;
}
/**
* Serializes the {@link IAstQuickFix} for the given {@link IValidationResult}.
*
* @param result
* the {@link List} of {@link IValidationMessage}
* @return the string representation of the {@link IAstQuickFix} for the given {@link IValidationResult}
*/
private String getQuickFixesContent(IAcceleoValidationResult result) {
final String res;
final List<EObject> eObjects = new ArrayList<>();
final Module module = result.getAcceleoAstResult().getModule();
eObjects.add(module);
final Iterator<EObject> it = module.eAllContents();
while (it.hasNext()) {
eObjects.add(it.next());
}
final AcceleoQuickFixesSwitch quickFixesSwitch = new AcceleoQuickFixesSwitch(queryEnvironment, result,
qualifiedName, moduleText);
final StringBuilder builder = new StringBuilder();
for (EObject eObject : eObjects) {
List<IAstQuickFix> quickFixes = quickFixesSwitch.getQuickFixes((ASTNode)eObject);
if (!quickFixes.isEmpty()) {
builder.append(eObject.eResource().getURIFragment(eObject));
builder.append(DEFAULT_END_OF_LINE_CHARACTER);
for (IAstQuickFix quickFix : quickFixes) {
builder.append(quickFix.getName());
builder.append(DEFAULT_END_OF_LINE_CHARACTER);
for (IAstResourceChange resourceChange : quickFix.getResourceChanges()) {
builder.append(resourceChange.toString());
builder.append(DEFAULT_END_OF_LINE_CHARACTER);
}
for (IAstTextReplacement replacement : quickFix.getTextReplacements()) {
builder.append(replacement.toString());
builder.append(DEFAULT_END_OF_LINE_CHARACTER);
}
}
}
}
if (builder.length() != 0) {
res = builder.substring(0, builder.length() - 1);
} else {
res = builder.toString();
}
return res;
}
/**
* Gets the module file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the module file from the test folder path
*/
protected final File getModuleFile(File testFolder) {
return getTemplateFileInternal(testFolder);
}
/**
* Gets the expected AST file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the expected AST file from the test folder path
*/
protected File getFragmentsFolder(File testFolder) {
return new File(testFolder + File.separator + "fragments");
}
/**
* Gets the expected AST file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the expected AST file from the test folder path
*/
protected File getExpectedASTFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-expected-ast.txt");
}
/**
* Gets the actual AST file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual AST file from the test folder path
*/
protected File getActualASTFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-actual-ast.txt");
}
/**
* Gets the expected validation file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the expected validation file from the test folder path
*/
protected File getExpectedValidationFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-expected-validation.txt");
}
/**
* Gets the actual validation file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual validation file from the test folder path
*/
protected File getActualValidationFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-actual-validation.txt");
}
/**
* Gets the expected quick fixes file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the expected quick fixes file from the test folder path
*/
protected File getExpectedQuickFixesFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-expected-quickFixes.txt");
}
/**
* Gets the actual quick fixes file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual quick fixes file from the test folder path
*/
protected File getActualQuickFixesFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-actual-quickFixes.txt");
}
/**
* Gets the actual validated template file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual template file from the test folder path
*/
protected File getExpectedRuntimeMessageFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-expected-runtimeMessages.txt");
}
/**
* Gets the actual validated template file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual template file from the test folder path
*/
protected File getActualRuntimeMessageFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-actual-runtimeMessages.txt");
}
/**
* Gets the expected generated file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the expected generated file from the test folder path
*/
protected File getExpectedGeneratedFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-expected-generation.txt");
}
/**
* Gets the actual generated file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the actual generated file from the test folder path
*/
protected File getActualGeneratedFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + "-actual-generation.txt");
}
/**
* Gets the model file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the model file from the test folder path
*/
protected File getModelFile(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + ".xmi");
}
/**
* Gets the template file from the test folder path.
*
* @param testFolder
* the test folder path
* @return the template file from the test folder path
*/
private static File getTemplateFileInternal(File testFolder) {
return new File(testFolder + File.separator + testFolder.getName() + ".mtl");
}
/**
* Gets the {@link Collection} of test folders from the given folder path.
*
* @param folderPath
* the folder path
* @return the {@link Collection} of test folders from the given folder path
*/
public static Collection<Object[]> retrieveTestFolders(String folderPath) {
Collection<Object[]> parameters = new ArrayList<Object[]>();
File folder = new File(folderPath);
final File[] children = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
final boolean res;
if (pathname.isDirectory() && pathname.canRead()) {
res = getTemplateFileInternal(pathname).exists();
} else {
res = false;
}
return res;
}
});
Arrays.sort(children);
for (File child : children) {
parameters.add(new Object[] {child.getPath() });
}
return parameters;
}
/**
* Sets the given content to the given {@link OutputStream}.
*
* @param stream
* the {@link OutputStream}
* @param charsetName
* the charset name
* @param content
* the content to write
* @throws UnsupportedEncodingException
* if the given charset is not supported
* @throws IOException
* if the given stream can't be written to
*/
public static void setContent(OutputStream stream, String charsetName, String content)
throws UnsupportedEncodingException, IOException {
stream.write(content.getBytes(charsetName));
stream.flush();
}
/**
* Copies all bytes from a source {@link URI} to a destination {@link URI}.
*
* @param sourceURI
* the source {@link URI}
* @param destURI
* the destination {@link URI}
* @return the number of copied bytes
* @throws IOException
* if the copy can't be done
*/
protected static long copy(URIConverter uriConverter, URI sourceURI, URI destURI) throws IOException {
try (InputStream source = uriConverter.createInputStream(sourceURI);
OutputStream dest = uriConverter.createOutputStream(destURI);) {
long nread = 0L;
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = source.read(buf)) > 0) {
dest.write(buf, 0, n);
nread += n;
}
return nread;
}
}
/**
* Gets the portable version of the given {@link String}.
*
* @param textContent
* the text content
* @return the portable version of the given {@link String}
*/
protected String getPortableString(String textContent) {
String res;
res = textContent.replaceAll("/home/.*/tests", "/home/.../tests"); // remove folder prefix
res = res.replaceAll("file:/.*/tests", "file:/.../tests"); // remove folder prefix
res = res.replaceAll("Aucun fichier ou dossier de ce type", "No such file or directory"); // replace
// localized
// message
res = res.replaceAll("20[^ ]* [^ ]* - Lost", "20...date and time... - Lost"); // strip lost user doc
// date
res = res.replaceAll("@[a-f0-9]{5,8}[, )]", "@00000000 "); // object address in toString()
res = res.replaceAll(
"(\\tat [a-zA-Z0-9$./]+((<|<)init(>|>))?\\((Unknown Source|Native Method|[a-zA-Z0-9$./]+java:[0-9]+)\\)\n?)+",
"...STACK..."); // strip stack traces
res = res.replaceAll("127.0.0.100:12.345", "127.0.0.100:12 345"); // localized port...
res = res.replaceAll("127.0.0.100:12,345", "127.0.0.100:12 345"); // localized port...
return res;
}
}