Skip to content

Commit 6970764

Browse files
committed
Add unit test coverage of mojo
Signed-off-by: Robert Young <robertyoungnz@gmail.com>
1 parent 535d6dd commit 6970764

File tree

8 files changed

+139
-8
lines changed

8 files changed

+139
-8
lines changed

.idea/codeInsightSettings.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kroxylicious-krpc-plugin/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<version>${maven-plugin-api.version}</version>
6363
<scope>provided</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.apache.maven</groupId>
67+
<artifactId>maven-model</artifactId>
68+
<scope>provided</scope>
69+
</dependency>
6570
<dependency>
6671
<groupId>org.apache.maven.plugin-tools</groupId>
6772
<artifactId>maven-plugin-annotations</artifactId>
@@ -76,6 +81,11 @@
7681
</dependency>
7782

7883
<!-- third party dependencies - test -->
84+
<dependency>
85+
<groupId>org.apache.maven.plugin-testing</groupId>
86+
<artifactId>maven-plugin-testing-harness</artifactId>
87+
<scope>test</scope>
88+
</dependency>
7989
<dependency>
8090
<groupId>org.assertj</groupId>
8191
<artifactId>assertj-core</artifactId>

kroxylicious-krpc-plugin/src/main/java/io/kroxylicious/krpccodegen/maven/AbstractKrpcGeneratorMojo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@ public void execute() throws MojoExecutionException {
9797
}
9898
}
9999

100+
// visible for testing
101+
public MavenProject project() {
102+
return project;
103+
}
104+
100105
abstract KrpcGenerator.Builder builder();
101106
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.krpccodegen.main;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.util.Objects;
12+
13+
import com.google.common.io.Resources;
14+
15+
import static java.nio.charset.StandardCharsets.UTF_8;
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.fail;
18+
19+
public class Files {
20+
static void assertFileHasExpectedContents(File file, String expectedFile) {
21+
try {
22+
String expected = Resources.asCharSource(
23+
Objects.requireNonNull(com.google.common.io.Files.class.getClassLoader().getResource(expectedFile)), UTF_8).read();
24+
assertThat(file).content().isEqualTo(expected);
25+
}
26+
catch (IOException e) {
27+
// noinspection ResultOfMethodCallIgnored
28+
fail("Failed to read file: " + file.getAbsolutePath(), e);
29+
}
30+
}
31+
}

kroxylicious-krpc-plugin/src/test/java/io/kroxylicious/krpccodegen/main/KrpcGeneratorTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
1515
import java.util.List;
16-
import java.util.Objects;
1716
import java.util.StringJoiner;
1817
import java.util.function.Consumer;
1918
import java.util.stream.Stream;
@@ -26,8 +25,8 @@
2625
import org.junit.jupiter.params.provider.MethodSource;
2726

2827
import com.google.common.io.Files;
29-
import com.google.common.io.Resources;
3028

29+
import static io.kroxylicious.krpccodegen.main.Files.assertFileHasExpectedContents;
3130
import static java.nio.charset.StandardCharsets.UTF_8;
3231
import static java.nio.file.Files.writeString;
3332
import static org.assertj.core.api.Assertions.assertThat;
@@ -252,12 +251,6 @@ void filesEqualInvalidArguments(Consumer<Path> tempDirConsumer, Class<? extends
252251
assertThatThrownBy(() -> tempDirConsumer.accept(path)).isInstanceOf(exceptionType).hasMessageContaining(message);
253252
}
254253

255-
private void assertFileHasExpectedContents(File file, String expectedFile) throws IOException {
256-
String expected = Resources.asCharSource(
257-
Objects.requireNonNull(getClass().getClassLoader().getResource(expectedFile)), UTF_8).read();
258-
assertThat(file).content().isEqualTo(expected);
259-
}
260-
261254
private static void testSingleGeneration(File tempDir, String messageSpec, String template, String expectedContents) throws Exception {
262255
String templateFile = "template.ftl";
263256
String outputFile = "output.txt";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.krpccodegen.main;
8+
9+
import java.nio.file.Path;
10+
11+
import org.apache.maven.api.plugin.testing.InjectMojo;
12+
import org.apache.maven.api.plugin.testing.MojoTest;
13+
import org.apache.maven.plugin.MojoExecutionException;
14+
import org.junit.jupiter.api.Test;
15+
16+
import io.kroxylicious.krpccodegen.maven.KrpcSingleGeneratorMojo;
17+
18+
import static io.kroxylicious.krpccodegen.main.Files.assertFileHasExpectedContents;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
@MojoTest
22+
class KrpcMojoTest {
23+
24+
@Test
25+
void generateSingle(@InjectMojo(goal = "generate-single", pom = "classpath:/test-pom.xml") KrpcSingleGeneratorMojo mojo) throws MojoExecutionException {
26+
assertThat(mojo).isNotNull();
27+
mojo.execute();
28+
Path outdir = Path.of(mojo.project().getBuild().getDirectory()).resolve("outmessage");
29+
assertThat(outdir).exists().isDirectory();
30+
Path packageDir = outdir.resolve("com").resolve("example");
31+
assertThat(packageDir).exists().isDirectory();
32+
Path generatedFile = packageDir.resolve("FetchRequest.java");
33+
assertThat(generatedFile).exists().isRegularFile();
34+
assertFileHasExpectedContents(generatedFile.toFile(), "hello-world/example-expected-FetchRequest.txt");
35+
}
36+
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright Kroxylicious Authors.
5+
6+
Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
7+
8+
-->
9+
<project xmlns="http://maven.apache.org/POM/4.0.0"
10+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<groupId>io.kroxylicious</groupId>
15+
<artifactId>example</artifactId>
16+
<version>0.19.0-SNAPSHOT</version>
17+
<packaging>jar</packaging>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>io.kroxylicious</groupId>
23+
<artifactId>kroxylicious-krpc-plugin</artifactId>
24+
<configuration>
25+
<messageSpecDirectory>${project.build.directory}/message-specs/common/message</messageSpecDirectory>
26+
<messageSpecFilter>FetchRequest.json</messageSpecFilter>
27+
<templateDirectory>${project.basedir}/src/test/resources/</templateDirectory>
28+
<templateNames>hello-world/example.ftl</templateNames>
29+
<outputFilePattern>${messageSpecName}.java</outputFilePattern>
30+
<outputPackage>com.example</outputPackage>
31+
<outputDirectory>${project.build.directory}/outmessage</outputDirectory>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,17 @@
438438
<artifactId>maven-core</artifactId>
439439
<version>${maven.core.version}</version>
440440
</dependency>
441+
<dependency>
442+
<groupId>org.apache.maven</groupId>
443+
<artifactId>maven-model</artifactId>
444+
<version>${maven.core.version}</version>
445+
</dependency>
446+
<dependency>
447+
<groupId>org.apache.maven.plugin-testing</groupId>
448+
<artifactId>maven-plugin-testing-harness</artifactId>
449+
<version>3.5.0</version>
450+
<scope>test</scope>
451+
</dependency>
441452
<dependency>
442453
<groupId>org.awaitility</groupId>
443454
<artifactId>awaitility</artifactId>

0 commit comments

Comments
 (0)