Skip to content

Commit

Permalink
feat(objectionary#2638): added ShakeMojoTest + todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanich96 committed Dec 20, 2023
1 parent f519353 commit 21043d5
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 11 deletions.
4 changes: 0 additions & 4 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@

/**
* Shake (prepare) XML files after optimizations for translation to java.
*
* @todo #2577:30min Add tests for ShakeMojo. ShakeMojo was created in order to split two
* optimization stages: optimizations themselves and preparations for translation to java
* Need to create tests for {@link ShakeMojo} in order to be sure that it does only it's job.
* @since 0.33.0
*/
@Mojo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@

/**
* Test case for {@link OptimizeMojo}.
*
* @todo #2638:15min This test has the error:
* Here "Matchers.hasKey(String.format("target/%s/foo/x/main.%s",
* ParseMojo.DIR, TranspileMojo.EXT)".
* But it should be "Matchers.hasKey(String.format("target/%s/foo/x/main.%s",
* OptimizeMojo.DIR, TranspileMojo.EXT)".
* @since 0.1
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
Expand Down Expand Up @@ -187,12 +191,6 @@ void optimizesSuccessfully(@TempDir final Path temp) throws IOException {
);
}

/**
* 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 optimizesConcurrentlyWithLotsOfPrograms(@TempDir final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
Expand Down
234 changes: 234 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/ShakeMojoTest.java
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)
)
);
}
}

0 comments on commit 21043d5

Please sign in to comment.