Skip to content

Commit

Permalink
objectionary#2863 get rid of ugly method using parametrized test
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Mar 4, 2024
1 parent 67606b9 commit 34f68ad
Showing 1 changed file with 47 additions and 99 deletions.
146 changes: 47 additions & 99 deletions eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
import org.cactoos.text.TextOf;
import org.eolang.maven.tojos.PlacedTojo;
import org.eolang.maven.tojos.PlacedTojos;
Expand All @@ -41,6 +42,9 @@
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test case for {@link UnplaceMojo}.
Expand Down Expand Up @@ -124,61 +128,42 @@ void keepsJarBecauseItIsStillInUse(@TempDir final Path temp) throws IOException
);
}

@Test
void unplacesWithKeepAndRemoveBinariesParamTogether(@TempDir final Path temp) throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = UnplaceMojoTest.fakeResult(
temp, placed, KeepRemoveBinaries.KEEP_REMOVE
);
MatcherAssert.assertThat(
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}

@Test
void unplacesWithRemoveBinariesParam(@TempDir final Path temp) throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = UnplaceMojoTest.fakeResult(
temp, placed, KeepRemoveBinaries.REMOVE
);
MatcherAssert.assertThat(
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}

@Test
void unplacesWithKeepBinariesParam(@TempDir final Path temp) throws Exception {
@ParameterizedTest
@MethodSource("testArgsProvider")
void unplacesWithKeepOrRemoveBinariesParam(final String[] params, @TempDir final Path temp)
throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = UnplaceMojoTest.fakeResult(
temp, placed, KeepRemoveBinaries.KEEP
);
MatcherAssert.assertThat(
res.values().stream().anyMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.containsString("false"),
Matchers.not(Matchers.containsString("true"))
)
);
final FakeMaven maven = new FakeMaven(temp)
.with("placed", placed.toFile());
for (final String param : params) {
maven.with(param, UnplaceMojoTest.GLOB_PATTERN);
}
final Map<String, Path> res = maven.execute(UnplaceMojo.class).result();
if (params.length == 1 && "keepBinaries".equals(params[0])) {
MatcherAssert.assertThat(
res.values().stream().anyMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.containsString("false"),
Matchers.not(Matchers.containsString("true"))
)
);
} else {
MatcherAssert.assertThat(
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}
}

@Test
Expand Down Expand Up @@ -291,53 +276,16 @@ private static boolean isClass(final Path path) {
}

/**
* Set of parameters for {@link FakeMaven}.
*/
private enum KeepRemoveBinaries {
/**
* {@code "keepBinaries"} param.
*/
KEEP,

/**
* {@code "removeBinaries"} param.
*/
REMOVE,

/**
* {@code "keepBinaries"} and {@code "removeBinaries"} params.
*/
KEEP_REMOVE
}

/**
* Instantiates {@link FakeMaven} with given parameters, executes it and return result.
* Input arguments for unit tests.
*
* @param temp Test temporary directory.
* @param placed Path to the placed tojos file.
* @param action Action to be performed with binaries.
* @return The result map with all files and folders that was created or compiled during fake
* mojo execution.
* @return Stream of arguments.
*/
private static Map<String, Path> fakeResult(
final Path temp, final Path placed, final KeepRemoveBinaries action) throws IOException {
final FakeMaven maven = new FakeMaven(temp)
.with("placed", placed.toFile());
switch (action) {
case KEEP:
maven.with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN);
break;
case REMOVE:
maven.with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN);
break;
case KEEP_REMOVE:
maven.with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN)
.with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN);
break;
default:
throw new IllegalArgumentException("No such option");
}
return maven.execute(UnplaceMojo.class).result();
private static Stream<Arguments> testArgsProvider() {
return Stream.of(
Arguments.of((Object) new String[]{"keepBinaries"}),
Arguments.of((Object) new String[]{"removeBinaries"}),
Arguments.of((Object) new String[]{"keepBinaries", "removeBinaries"})
);
}
}

0 comments on commit 34f68ad

Please sign in to comment.