-
Notifications
You must be signed in to change notification settings - Fork 122
/
OptimizeMojoTest.java
404 lines (393 loc) 路 15.2 KB
/
OptimizeMojoTest.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StXSL;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Xsline;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Test case for {@link OptimizeMojo}.
*
* @since 0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class OptimizeMojoTest {
@Test
void skipsAlreadyOptimized(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print > @\n",
temp.relativize(src)
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("cache", temp.resolve("cache/parsed"))
.with("foreignFormat", "csv")
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.execute();
final Path tgt = target.resolve(
String.format("%s/foo/main.%s", OptimizeMojo.DIR, TranspileMojo.EXT)
);
final long mtime = tgt.toFile().lastModified();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.execute();
MatcherAssert.assertThat(
tgt.toFile().lastModified(),
Matchers.is(mtime)
);
}
@Test
void optimizesIfExpired(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print > @\n",
temp.relativize(src)
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("cache", temp.resolve("cache/parsed"))
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.execute();
final Path tgt = target.resolve(
String.format("%s/foo/main.%s", OptimizeMojo.DIR, TranspileMojo.EXT)
);
final long start = System.currentTimeMillis();
tgt.toFile().setLastModified(start - TimeUnit.SECONDS.toMillis(10L));
src.toFile().setLastModified(start - TimeUnit.SECONDS.toMillis(1L));
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.execute();
MatcherAssert.assertThat(
tgt.toFile().lastModified(),
Matchers.greaterThan(start)
);
}
/**
* Test case for #1223.
*
* @param temp Temporary test directory.
* @throws Exception if unexpected error happened.
* @todo #1223:90min Implement caching for optimization step. After implementation don't forget
* to remove '@Disabled' annotation from the next tests:
* - 'getsAlreadyOptimizedResultsFromCache'
* - 'savesOptimizedResultsToCache'
*/
@Test
@Disabled
void getsAlreadyOptimizedResultsFromCache(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print > @\n",
src
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.json");
final Path cache = temp.resolve("cache");
final TextOf cached = new TextOf(
new ResourceOf("org/eolang/maven/optimize/main.xml")
);
final String hash = "abcdef1";
new Home(cache).save(
cached,
Paths.get(OptimizeMojo.OPTIMIZED)
.resolve(hash)
.resolve("foo/main.xmir")
);
Catalogs.INSTANCE.make(foreign, "json")
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_HASH, hash)
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "json")
.with("cache", temp.resolve("cache/parsed"))
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("trackOptimizationSteps", true)
.with("foreignFormat", "json")
.with("cache", cache)
.execute();
MatcherAssert.assertThat(
new XMLDocument(
new Home(target).load(
Paths.get(
String.format("%s/foo/main.%s", OptimizeMojo.DIR, TranspileMojo.EXT)
)
).asBytes()
),
Matchers.is(new XMLDocument(cached.asString()))
);
}
@Test
@Disabled
void savesOptimizedResultsToCache(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print > @\n",
src
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.json");
final Path cache = temp.resolve("cache");
final String hash = "abcdef1";
Catalogs.INSTANCE.make(foreign, "json")
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_HASH, hash)
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "json")
.with("cache", temp.resolve("cache/parsed"))
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("trackOptimizationSteps", true)
.with("foreignFormat", "json")
.with("cache", cache)
.execute();
MatcherAssert.assertThat(
cache.resolve(OptimizeMojo.OPTIMIZED)
.resolve(hash)
.resolve("foo/main.xmir").toFile(),
FileMatchers.anExistingFile()
);
}
@Test
void testSimpleOptimize(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
"+package f\n\n[args] > main\n (stdout \"Hello!\").print > @\n",
temp.relativize(src)
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("cache", temp.resolve("cache/parsed"))
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("trackOptimizationSteps", true)
.with("foreignFormat", "csv")
.execute();
MatcherAssert.assertThat(
new Home(target).exists(
Paths.get(
String.format("%s/foo/main/00-not-empty-atoms.xml", OptimizeMojo.STEPS)
)
),
Matchers.is(true)
);
MatcherAssert.assertThat(
new Home(target).exists(
Paths.get(
String.format("%s/foo/main.%s", OptimizeMojo.DIR, TranspileMojo.EXT)
)
),
Matchers.is(true)
);
}
@Test
void testOptimizeWithFailOnErrorFlag(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
String.join(
"\n",
"+package f",
"\n+alias THIS-IS-WRONG org.eolang.io.stdout",
"[args] > main",
" (stdout \"Hello!\").print > @\n"
),
temp.relativize(src)
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("cache", temp.resolve("cache/parsed"))
.execute();
new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("failOnError", false)
.execute();
MatcherAssert.assertThat(
Files.notExists(
target.resolve(
String.format("%s/foo/main.%s", OptimizeMojo.DIR, TranspileMojo.EXT)
)
),
Matchers.is(true)
);
}
@Test
void testOptimizedFail(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
String.join(
"\n",
"+package f",
"\n+alias THIS-IS-WRONG org.eolang.io.stdout",
"[args] > main",
" (stdout \"Hello!\").print > @\n"
),
src
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.csv");
Catalogs.INSTANCE.make(foreign)
.add("foo.main")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("cache", temp.resolve("cache/parsed"))
.execute();
Assertions.assertThrows(
IllegalArgumentException.class,
() -> new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.execute()
);
}
@Test
void testFailOnWarning(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo.src.eo");
new Home(temp).save(
new ResourceOf("org/eolang/maven/withwarning.eo"),
temp.relativize(src)
);
final Path target = temp.resolve("target");
final Path foreign = temp.resolve("eo-foreign.json");
Catalogs.INSTANCE.make(foreign)
.add("foo.src")
.set(AssembleMojo.ATTR_SCOPE, "compile")
.set(AssembleMojo.ATTR_EO, src.toString());
new Moja<>(ParseMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("cache", temp.resolve("cache/parsed"))
.with("foreignFormat", "csv")
.execute();
this.applyXsl(
"org/eolang/maven/set-warning-severity.xsl",
target.resolve("01-parse/foo/src.xmir")
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> new Moja<>(OptimizeMojo.class)
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("failOnError", false)
.with("failOnWarning", true)
.execute()
);
}
/**
* Apply XSL transformation.
* @param xsl Path to XSL within classpath
* @param xml Path to XML to be tranformed
*/
private void applyXsl(final String xsl, final Path xml) throws Exception {
final XML output = new Xsline(
new TrDefault<Shift>()
.with(
new StXSL(
new XSLDocument(
new ResourceOf(xsl).stream()
)))
).pass(new XMLDocument(xml));
new Home(xml.getParent()).save(output.toString(), xml.getParent().relativize(xml));
}
}