forked from objectionary/eo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objectionary#2638): added ShakeMojoTest + todo
- Loading branch information
Showing
3 changed files
with
239 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
234 changes: 234 additions & 0 deletions
234
eo-maven-plugin/src/test/java/org/eolang/maven/ShakeMojoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2023 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.XMLDocument; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import org.cactoos.io.ResourceOf; | ||
import org.cactoos.text.TextOf; | ||
import org.eolang.maven.util.HmBase; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.hamcrest.io.FileMatchers; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
/** | ||
* Test case for {@link ShakeMojo}. | ||
* | ||
* @since 0.35.0 | ||
*/ | ||
final class ShakeMojoTest { | ||
|
||
/** | ||
* The key for testing. | ||
*/ | ||
private String key; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
this.key = "target/%s/foo/x/main.%s"; | ||
} | ||
|
||
@Test | ||
void shakesSuccessfully(@TempDir final Path temp) throws IOException { | ||
final FakeMaven maven = new FakeMaven(temp); | ||
final Map<String, Path> res = maven | ||
.withHelloWorld() | ||
.with("trackOptimizationSteps", true) | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class) | ||
.result(); | ||
MatcherAssert.assertThat( | ||
res, | ||
Matchers.hasKey( | ||
String.format("target/%s/foo/x/main/01-remove-refs.xml", ShakeMojo.STEPS) | ||
) | ||
); | ||
MatcherAssert.assertThat( | ||
res, | ||
Matchers.hasKey( | ||
String.format(this.key, ShakeMojo.DIR, TranspileMojo.EXT) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void getsAlreadyShakenResultsFromCache(@TempDir final Path temp) throws Exception { | ||
final TextOf cached = new TextOf( | ||
new ResourceOf("org/eolang/maven/optimize/main.xml") | ||
); | ||
final Path cache = temp.resolve("cache"); | ||
final String hash = "abcdef1"; | ||
new HmBase(cache).save( | ||
cached, | ||
Paths.get(ShakeMojo.SHAKEN) | ||
.resolve(hash) | ||
.resolve("foo/x/main.xmir") | ||
); | ||
new FakeMaven(temp) | ||
.withHelloWorld() | ||
.with("cache", cache) | ||
.allTojosWithHash(() -> hash) | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class); | ||
MatcherAssert.assertThat( | ||
new XMLDocument( | ||
new HmBase(temp).load( | ||
Paths.get( | ||
String.format( | ||
this.key, | ||
ShakeMojo.DIR, | ||
TranspileMojo.EXT | ||
) | ||
) | ||
).asBytes() | ||
), | ||
Matchers.is(new XMLDocument(cached.asString())) | ||
); | ||
} | ||
|
||
@Test | ||
void skipsAlreadyShaken(@TempDir final Path temp) throws IOException { | ||
final FakeMaven maven = new FakeMaven(temp) | ||
.withHelloWorld() | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class); | ||
final Path path = maven.result().get( | ||
String.format(this.key, ShakeMojo.DIR, TranspileMojo.EXT) | ||
); | ||
final long mtime = path.toFile().lastModified(); | ||
maven.execute(ShakeMojo.class); | ||
MatcherAssert.assertThat( | ||
path.toFile().lastModified(), | ||
Matchers.is(mtime) | ||
); | ||
} | ||
|
||
@Test | ||
void shakesIfExpired(@TempDir final Path temp) throws Exception { | ||
final FakeMaven maven = new FakeMaven(temp); | ||
final Path tgt = maven | ||
.withHelloWorld() | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class) | ||
.result() | ||
.get( | ||
String.format(this.key, ShakeMojo.DIR, TranspileMojo.EXT) | ||
); | ||
final long old = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(10L); | ||
if (!tgt.toFile().setLastModified(old)) { | ||
Assertions.fail(String.format("The last modified attribute can't be set for %s", tgt)); | ||
} | ||
maven.execute(ShakeMojo.class); | ||
MatcherAssert.assertThat( | ||
"We expect that already shaken xmir will be replaced by a new shaken xmir, because the first xmir is outdated and should be updated", | ||
tgt.toFile().lastModified(), | ||
Matchers.greaterThan(old) | ||
); | ||
} | ||
|
||
@Test | ||
void savesShakenResultsToCache(@TempDir final Path temp) throws IOException { | ||
final Path cache = temp.resolve("cache"); | ||
final String hash = "abcdef1"; | ||
new FakeMaven(temp) | ||
.withHelloWorld() | ||
.with("cache", cache) | ||
.allTojosWithHash(() -> hash) | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class); | ||
MatcherAssert.assertThat( | ||
cache.resolve(ShakeMojo.SHAKEN) | ||
.resolve(hash) | ||
.resolve("foo/x/main.xmir").toFile(), | ||
FileMatchers.anExistingFile() | ||
); | ||
} | ||
|
||
/** | ||
* The test with high number of eo programs reveals concurrency problems of the OptimizeMojo. | ||
* Since other tests works only with single program - it's hard to find concurrency mistakes. | ||
* @param temp Test directory. | ||
* @throws java.io.IOException If problem with filesystem happened. | ||
*/ | ||
@Test | ||
void shakesConcurrentlyWithLotsOfPrograms(@TempDir final Path temp) throws IOException { | ||
final FakeMaven maven = new FakeMaven(temp); | ||
final int total = 20; | ||
for (int program = 0; program < total; ++program) { | ||
maven.withHelloWorld(); | ||
} | ||
final Map<String, Path> res = maven | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class) | ||
.result(); | ||
for (int program = 0; program < total; ++program) { | ||
MatcherAssert.assertThat( | ||
res, | ||
Matchers.hasKey( | ||
String.format( | ||
"target/%s/foo/x/main%s.%s", | ||
ShakeMojo.DIR, | ||
FakeMaven.suffix(program), | ||
TranspileMojo.EXT | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
@Test | ||
void doesNotCrashesOnError(@TempDir final Path temp) throws Exception { | ||
MatcherAssert.assertThat( | ||
new FakeMaven(temp) | ||
.withProgram( | ||
"+package f\n", | ||
"[args] > main", | ||
" seq > @", | ||
" TRUE > x", | ||
" FALSE > x" | ||
).with("trackOptimizationSteps", true) | ||
.execute(ParseMojo.class) | ||
.execute(OptimizeMojo.class) | ||
.execute(ShakeMojo.class) | ||
.result(), | ||
Matchers.hasKey( | ||
String.format(this.key, ShakeMojo.DIR, TranspileMojo.EXT) | ||
) | ||
); | ||
} | ||
} |