Skip to content

Commit

Permalink
feat: create the test structure (unit and devmode test) when using `c…
Browse files Browse the repository at this point in the history
…reate-extension` Maven Mojo

Fixes #3585
  • Loading branch information
machi1990 committed Nov 13, 2019
1 parent 6ac8aae commit 39c0fd8
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ public class CreateExtensionMojo extends AbstractMojo {
@Parameter(property = "quarkus.itestParentPath")
Path itestParentPath;

/**
* Indicates whether to generate a unit test class for the extension
*/
@Parameter(property = "quarkus.generateUnitTest", defaultValue = "true")
boolean generateUnitTest;

/**
* Indicates whether to generate a dev mode unit test class for the extension
*/
@Parameter(property = "quarkus.generateDevModeTest", defaultValue = "true")
boolean generateDevModeTest;

@Parameter(defaultValue = "${project}", readonly = true)
MavenProject project;

Expand Down Expand Up @@ -498,6 +510,14 @@ void addModules(Path basePomXml, Model basePom, Charset charset)
+ "/deployment/" + model.artifactIdBaseCamelCase + "Processor.java");
evalTemplate(cfg, "Processor.java", processorPath, charset, model);

if (generateUnitTest) {
generateUnitTestClass(charset, cfg, model);
}

if (generateDevModeTest) {
generateDevModeTestClass(charset, cfg, model);
}

if (!basePom.getModules().contains(model.artifactIdBase)) {
getLog().info(String.format("Adding module [%s] to [%s]", model.artifactIdBase, basePomXml));
new PomTransformer(basePomXml, charset).transform(Transformation.addModule(model.artifactIdBase));
Expand All @@ -521,46 +541,67 @@ void addModules(Path basePomXml, Model basePom, Charset charset)
.transform(Transformation.addManagedDependency(model.groupId, aId, model.bomEntryVersion));
}
if (itestParentPath != null) {
generateItest(cfg, charset, model);
generateIntegrationTest(cfg, charset, model);
}

}

void generateItest(Configuration cfg, Charset charset, TemplateParams model)
private void generateUnitTestClass(Charset charset, Configuration cfg, TemplateParams model)
throws IOException, TemplateException {
final Path unitTest = basedir
.resolve(model.artifactIdBase + "/deployment/src/test/java/" + model.javaPackageBase.replace('.', '/')
+ "/test/" + model.artifactIdBaseCamelCase + "UnitTest.java");

evalTemplate(cfg, "UnitTest.java", unitTest, charset, model);
}

private void generateDevModeTestClass(Charset charset, Configuration cfg, TemplateParams model)
throws IOException, TemplateException {

final Path devModeTest = basedir
.resolve(model.artifactIdBase + "/deployment/src/test/java/" + model.javaPackageBase.replace('.', '/')
+ "/test/" + model.artifactIdBaseCamelCase + "DevModeTest.java");

evalTemplate(cfg, "DevModeTest.java", devModeTest, charset, model);

}

void generateIntegrationTest(Configuration cfg, Charset charset, TemplateParams model)
throws MojoFailureException, MojoExecutionException, TemplateException {
final Path itestParentAbsPath = basedir.resolve(itestParentPath).toAbsolutePath();
try (Reader r = Files.newBufferedReader(itestParentAbsPath, charset)) {
final Model itestParent = new MavenXpp3Reader().read(r);
if (!"pom".equals(itestParent.getPackaging())) {
final Path integrationTestsParentAbsPath = basedir.resolve(itestParentPath).toAbsolutePath();
try (Reader r = Files.newBufferedReader(integrationTestsParentAbsPath, charset)) {
final Model integrationTestParent = new MavenXpp3Reader().read(r);
if (!"pom".equals(integrationTestParent.getPackaging())) {
throw new MojoFailureException(
"Can add an extension integration test only under a project with packagin 'pom'; found: "
+ itestParent.getPackaging() + " in " + itestParentAbsPath);
"Can add an extension integration test only under a project with packaging 'pom'; found: "
+ integrationTestParent.getPackaging() + " in " + integrationTestsParentAbsPath);
}
model.itestParentGroupId = getGroupId(itestParent);
model.itestParentArtifactId = itestParent.getArtifactId();
model.itestParentVersion = getVersion(itestParent);
model.itestParentGroupId = getGroupId(integrationTestParent);
model.itestParentArtifactId = integrationTestParent.getArtifactId();
model.itestParentVersion = getVersion(integrationTestParent);
model.itestParentRelativePath = "../pom.xml";

final Path itestDir = itestParentAbsPath.getParent().resolve(model.artifactIdBase);
evalTemplate(cfg, "integration-test-pom.xml", itestDir.resolve("pom.xml"), charset, model);
final Path integrationTestDir = integrationTestsParentAbsPath.getParent().resolve(model.artifactIdBase);
evalTemplate(cfg, "integration-test-pom.xml", integrationTestDir.resolve("pom.xml"), charset, model);

final Path testResourcePath = itestDir.resolve("src/main/java/" + model.javaPackageBase.replace('.', '/')
final Path testResourcePath = integrationTestDir.resolve("src/main/java/" + model.javaPackageBase.replace('.', '/')
+ "/it/" + model.artifactIdBaseCamelCase + "Resource.java");
evalTemplate(cfg, "TestResource.java", testResourcePath, charset, model);
final Path testClassDir = itestDir
final Path testClassDir = integrationTestDir
.resolve("src/test/java/" + model.javaPackageBase.replace('.', '/') + "/it");
evalTemplate(cfg, "Test.java", testClassDir.resolve(model.artifactIdBaseCamelCase + "Test.java"), charset,
model);
evalTemplate(cfg, "IT.java", testClassDir.resolve(model.artifactIdBaseCamelCase + "IT.java"), charset,
model);

getLog().info(String.format("Adding module [%s] to [%s]", model.artifactIdBase, itestParentAbsPath));
new PomTransformer(itestParentAbsPath, charset).transform(Transformation.addModule(model.artifactIdBase));
getLog().info(String.format("Adding module [%s] to [%s]", model.artifactIdBase, integrationTestsParentAbsPath));
new PomTransformer(integrationTestsParentAbsPath, charset)
.transform(Transformation.addModule(model.artifactIdBase));

} catch (IOException e) {
throw new MojoExecutionException(String.format("Could not read %s", itestParentAbsPath), e);
throw new MojoExecutionException(String.format("Could not read %s", integrationTestsParentAbsPath), e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException(String.format("Could not parse %s", itestParentAbsPath), e);
throw new MojoExecutionException(String.format("Could not parse %s", integrationTestsParentAbsPath), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package [=javaPackageBase].test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

class [=artifactIdBaseCamelCase]DevModeTest {
@RegisterExtension
static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest() // Start hot reload (DevMode) test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information
Assertions.fail("Add dev mode assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package [=javaPackageBase].test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

class [=artifactIdBaseCamelCase]UnitTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() // Start unit test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information
Assertions.fail("Add some assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
[#if !assumeManaged ] <version>[=r"$"]{project.version}</version>
[/#if]
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

public class CreateExtensionMojoTest {

static CreateExtensionMojo createMojo(String testProjectName) throws IllegalArgumentException,
IllegalAccessException, IOException, NoSuchFieldException, SecurityException {
static CreateExtensionMojo createMojo(String testProjectName)
throws IllegalArgumentException, IOException, SecurityException {
final Path srcDir = Paths.get("src/test/resources/projects/" + testProjectName);
/*
* We want to run on the same project multiple times with different args so let's create a copy with a random
Expand All @@ -44,6 +44,8 @@ static CreateExtensionMojo createMojo(String testProjectName) throws IllegalArgu
mojo.quarkusVersion = CreateExtensionMojo.DEFAULT_QUARKUS_VERSION;
mojo.bomEntryVersion = CreateExtensionMojo.DEFAULT_BOM_ENTRY_VERSION;
mojo.assumeManaged = true;
mojo.generateDevModeTest = true;
mojo.generateUnitTest = true;
mojo.nameSegmentDelimiter = CreateExtensionMojo.DEFAULT_NAME_SEGMENT_DELIMITER;
return mojo;
}
Expand All @@ -64,7 +66,7 @@ private static Path getCopyDir(String testProjectName) {

@Test
void createExtensionUnderExistingPomMinimal() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
IllegalArgumentException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-pom");
mojo.artifactId = "my-project-(minimal-extension)";
mojo.assumeManaged = false;
Expand All @@ -76,7 +78,7 @@ void createExtensionUnderExistingPomMinimal() throws MojoExecutionException, Moj

@Test
void createExtensionUnderExistingPomWithAdditionalRuntimeDependencies() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
IllegalArgumentException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-pom");
mojo.artifactId = "my-project-(add-to-bom)";
mojo.assumeManaged = false;
Expand All @@ -90,8 +92,8 @@ void createExtensionUnderExistingPomWithAdditionalRuntimeDependencies() throws M
}

@Test
void createExtensionUnderExistingPomWithItest() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
void createExtensionUnderExistingPomWithIntegrationTest() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-pom");
mojo.artifactId = "my-project-(itest)";
mojo.assumeManaged = false;
Expand All @@ -104,7 +106,7 @@ void createExtensionUnderExistingPomWithItest() throws MojoExecutionException, M

@Test
void createExtensionUnderExistingPomCustomGrandParent() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
IllegalArgumentException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-pom");
mojo.artifactId = "myproject-(with-grand-parent)";
mojo.grandParentArtifactId = "build-bom";
Expand Down Expand Up @@ -148,16 +150,15 @@ static void assertTreesMatch(Path expected, Path actual) throws IOException {
}

@Test
void getPackage() throws IOException {
void getPackage() {
assertEquals("org.apache.camel.quarkus.aws.sns.deployment", CreateExtensionMojo
.getJavaPackage("org.apache.camel.quarkus", null, "camel-quarkus-aws-sns-deployment"));
assertEquals("org.apache.camel.quarkus.component.aws.sns.deployment", CreateExtensionMojo
.getJavaPackage("org.apache.camel.quarkus", "component", "camel-quarkus-aws-sns-deployment"));
}

@Test
void toCapCamelCase() throws IOException {
void toCapCamelCase() {
assertEquals("FooBarBaz", CreateExtensionMojo.toCapCamelCase("foo-bar-baz"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>my-project-add-to-bom</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme.my.project.add.to.bom.test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

class AddToBomDevModeTest {
@RegisterExtension
static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest() // Start hot reload (DevMode) test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information
Assertions.fail("Add dev mode assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.acme.my.project.add.to.bom.test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

class AddToBomUnitTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() // Start unit test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information
Assertions.fail("Add some assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>my-project-itest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme.my.project.itest.test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

class ItestDevModeTest {
@RegisterExtension
static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest() // Start hot reload (DevMode) test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information
Assertions.fail("Add dev mode assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.acme.my.project.itest.test;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

class ItestUnitTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() // Start unit test with your extension loaded
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void test() {
// Write your tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information
Assertions.fail("Add some assertions to " + getClass().getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>my-project-minimal-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit 39c0fd8

Please sign in to comment.