-
Notifications
You must be signed in to change notification settings - Fork 316
/
StandaloneBuilderTest.java
332 lines (273 loc) · 11.6 KB
/
StandaloneBuilderTest.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
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.builder.standalone;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.OutputConfiguration;
import org.eclipse.xtext.generator.OutputConfiguration.SourceMapping;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.util.Files;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
/**
* @author Stefan Oehme - Initial contribution and API
*/
@RunWith(XtextRunner.class)
@InjectWith(StandaloneBuilderInjectorProvider.class)
public class StandaloneBuilderTest {
private static final File PROJECT_DIR = new File("test-data/standalone");
private static final File TMP_DIR = new File(PROJECT_DIR, "tmp");
@Inject
private TestableStandaloneBuilder testBuilder;
@Before
public void setUp() {
testBuilder.resetCallStatistic();
testBuilder.resetTestSetup();
}
@After
public void cleanup() throws IOException {
deleteFolder("src-gen");
deleteFolder("src2-gen");
if (TMP_DIR.exists()) {
Files.sweepFolder(TMP_DIR);
TMP_DIR.delete();
}
}
@Test
public void testDifferentOutputFolders() {
initBuilder(new TestLanguageConfiguration(true));
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/Foo.txt");
assertTrue(generatedFile.exists());
generatedFile = getFile("src2-gen/Bar.txt");
assertTrue(generatedFile.exists());
File unexpectedFile = getFile("src-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Foo.txt");
assertFalse(unexpectedFile.exists());
}
@Test
public void testWriteStorageResource() {
initBuilder(new TestLanguageConfiguration(true));
testBuilder.setWriteStorageResources(true);
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/.Foo.buildertestlanguagebin");
assertTrue(generatedFile.exists());
}
@Test
public void testNoWriteStorageResource() {
initBuilder(new TestLanguageConfiguration(true));
testBuilder.setWriteStorageResources(false);
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/.Foo.buildertestlanguagebin");
assertFalse(generatedFile.exists());
}
@Test
public void testSameOutputFolder() {
initBuilder(new TestLanguageConfiguration(false));
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/Foo.txt");
assertTrue(generatedFile.exists());
generatedFile = getFile("src-gen/Bar.txt");
assertTrue(generatedFile.exists());
File unexpectedFile = getFile("src2-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Foo.txt");
assertFalse(unexpectedFile.exists());
}
@Test
public void testOnlyOneSourceFolder() {
initBuilder(new TestLanguageConfiguration(false));
testBuilder.setSourceDirs(ImmutableList.of(new File(PROJECT_DIR, "src").getAbsolutePath()));
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/Foo.txt");
assertTrue(generatedFile.exists());
File unexpectedFile = getFile("src-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Foo.txt");
assertFalse(unexpectedFile.exists());
}
@Test
public void testRelativeSourceFolder() {
initBuilder(new TestLanguageConfiguration(false));
testBuilder.setSourceDirs(ImmutableList.of("test-data/standalone/src"));
assertTrue(testBuilder.launch());
File generatedFile = getFile("src-gen/Foo.txt");
assertTrue(generatedFile.exists());
File unexpectedFile = getFile("src-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Bar.txt");
assertFalse(unexpectedFile.exists());
unexpectedFile = getFile("src2-gen/Foo.txt");
assertFalse(unexpectedFile.exists());
}
@Test
public void testJarToPlatformMapping() {
initBuilder(new TestLanguageConfiguration(false));
testBuilder.setSourceDirs(ImmutableList.of("test-data/standalone.with.reference/model"));
testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/",
"test-data/model.in.eclipse.project.jar"));
assertTrue("Builder launch returned false", testBuilder.launch());
URI uri = EcorePlugin.getPlatformResourceMap().get("model.in.eclipse.project");
assertNotNull("No platform mapping found for 'model.in.eclipse.project'", uri);
assertTrue("Platform mapping is archive", uri.toString().startsWith("archive:file:/"));
assertTrue("Platform mapping points to jared project",
uri.toString().endsWith("test-data/model.in.eclipse.project.jar!/"));
}
@Test
public void testWriteClassPathConfiguration() throws IOException {
initBuilder(new TestLanguageConfiguration(false));
testBuilder.setSourceDirs(ImmutableList.of("test-data/standalone.with.reference/model"));
testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/",
"test-data/model.in.eclipse.project.jar"));
testBuilder.setTempDir(TMP_DIR);
TMP_DIR.mkdir();
File configFile = new File(TMP_DIR, "classpath.config");
assertFalse(configFile.exists());
testBuilder.setClasspathConfigurationLocation(configFile.getAbsolutePath(), "prod", "prod-out");
assertTrue("Builder launch returned false", testBuilder.launch());
assertTrue(configFile.exists());
Properties onlyProd = new Properties();
try(FileReader reader = new FileReader(configFile, StandardCharsets.UTF_8)) {
onlyProd.load(reader);
}
assertEquals(7, onlyProd.size());
testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/",
"test-data/missing.jar"));
testBuilder.setClasspathConfigurationLocation(configFile.getAbsolutePath(), "test", "test-out");
assertFalse("Builder launch returned true", testBuilder.launch());
assertTrue(configFile.exists());
Properties alsoTest = new Properties();
try(FileReader reader = new FileReader(configFile, StandardCharsets.UTF_8)) {
alsoTest.load(reader);
}
assertTrue(alsoTest.entrySet().containsAll(onlyProd.entrySet()));
assertEquals(14, alsoTest.size());
testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/"));
testBuilder.setClasspathConfigurationLocation(configFile.getAbsolutePath(), "prod", "prod-out");
assertFalse("Builder launch returned true", testBuilder.launch());
alsoTest = new Properties();
try(FileReader reader = new FileReader(configFile, StandardCharsets.UTF_8)) {
alsoTest.load(reader);
}
assertEquals(12, alsoTest.size());
}
@Test
public void testDuplicateSourceEntries() {
TestLanguageConfiguration config = new TestLanguageConfiguration(false);
config.setJavaSupport(true);
initBuilder(config);
testBuilder.setJavaSourceDirs(ImmutableList.of(new File(PROJECT_DIR, "src2").getPath()));
testBuilder.setTempDir(TMP_DIR);
testBuilder.setDebugLog(true);
assertTrue("Builder launch returned false", testBuilder.launch());
File compiledClazz = getFile("tmp/stub-classes/JavaClass.class");
assertTrue("java compilation failed", compiledClazz.exists());
}
@Test
public void testValidateMultipleResources() {
TestLanguageConfiguration config = new TestLanguageConfiguration(false);
initBuilder(config, "src", "src-error");
testBuilder.setTempDir(TMP_DIR);
testBuilder.setDebugLog(true);
testBuilder.setMockGeneration(true);
assertFalse("Build should return false, but returned -success-", testBuilder.launch());
assertEquals("Build should fail early, but validate all resources", 2, testBuilder.getValidateCalled());
assertEquals("Build should fail early", 0, testBuilder.getGenerateCalled());
// revert resource process order - https://bugs.eclipse.org/bugs/show_bug.cgi?id=464663
initBuilder(config, "src-error", "src");
assertFalse("Build should fail, but returned -success-", testBuilder.launch());
assertEquals("Build should fail early, but validation was executed", 2, testBuilder.getValidateCalled());
assertEquals("Build should fail early", 0, testBuilder.getGenerateCalled());
// allow errors, generator should run in spite validation errors, but launch should return "validation error was found"
initBuilder(config, "src-error", "src");
testBuilder.setFailOnValidationError(false);
assertFalse("Build should fail, but returned -success-", testBuilder.launch());
assertEquals("Validation was executed", 2, testBuilder.getValidateCalled());
assertEquals("Generator was executed in spite of validation errors", 1, testBuilder.getGenerateCalled());
}
private File getFile(String projectRelativePath) {
return new File(PROJECT_DIR, projectRelativePath);
}
private void deleteFolder(String projectRelativePath) throws FileNotFoundException {
File folder = getFile(projectRelativePath);
if (folder.exists()) {
Files.sweepFolder(folder);
folder.delete();
}
}
private StandaloneBuilder initBuilder(ILanguageConfiguration config) {
return initBuilder(config, "src", "src2");
}
private StandaloneBuilder initBuilder(ILanguageConfiguration config, String... srcDirs) {
List<String> pathes = new ArrayList<String>();
for (String srcDir : srcDirs) {
pathes.add(new File(PROJECT_DIR, srcDir).getAbsolutePath());
}
testBuilder.setSourceDirs(pathes);
testBuilder.resetCallStatistic();
Map<String, LanguageAccess> languages = new LanguageAccessFactory()
.createLanguageAccess(ImmutableList.of(config), getClass().getClassLoader());
testBuilder.setBaseDir(PROJECT_DIR.getAbsolutePath());
testBuilder.setLanguages(languages);
testBuilder.setClassPathEntries(ImmutableList.<String>of());
return testBuilder;
}
public static class TestLanguageConfiguration implements ILanguageConfiguration {
private boolean useOutputPerSource;
private boolean javaSupport = false;
public TestLanguageConfiguration(boolean useOutputPerSource) {
this.useOutputPerSource = useOutputPerSource;
}
/* @NonNull */
@Override
public String getSetup() {
return "org.eclipse.xtext.builder.tests.BuilderTestLanguageStandaloneSetup";
}
@Override
public Set<OutputConfiguration> getOutputConfigurations() {
OutputConfiguration config = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
config.setOutputDirectory("src-gen");
if (useOutputPerSource) {
SourceMapping sourceMapping = new OutputConfiguration.SourceMapping("src2");
sourceMapping.setOutputDirectory("src2-gen");
config.getSourceMappings().add(sourceMapping);
config.setUseOutputPerSourceFolder(true);
}
return ImmutableSet.of(config);
}
public void setJavaSupport(boolean javaSupport) {
this.javaSupport = javaSupport;
}
@Override
public boolean isJavaSupport() {
return javaSupport;
}
}
}