Skip to content

Commit

Permalink
Merge pull request #2 from manusa/review/webapp
Browse files Browse the repository at this point in the history
review: refactor: AppServerAutoDetectionTest uses parameters
  • Loading branch information
anurag-rajawat committed Oct 3, 2022
2 parents a757ba4 + 9b573e9 commit 02867c5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 120 deletions.
5 changes: 5 additions & 0 deletions jkube-kit/generator/webapp/pom.xml
Expand Up @@ -44,6 +44,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Expand Up @@ -13,17 +13,22 @@
*/
package org.eclipse.jkube.generator.webapp;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.stream.Stream;

import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.generator.webapp.handler.TomcatAppSeverHandler;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;
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;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -36,136 +41,118 @@ class AppServerAutoDetectionTest {
@TempDir
Path folder;

@Test
void isWildFly() throws Exception {
Object[] descriptorNames = new Object[] {
"WEB-INF/jboss-deployment-structure.xml", true,
"META-INF/jboss-deployment-structure.xml", true,
"WEB-INF/jboss-web.xml", true,
"WEB-INF/ejb-jar.xml", true,
"WEB-INF/jboss-ejb3.xml", true,
"META-INF/persistence.xml", true,
"META-INF/foo-jms.xml", true,
"WEB-INF/bar-jms.xml", true,
"META-INF/my-datasource-ds.xml", true,
"WEB-INF/my-datasource-ds.xml", true,
"WEB-INF/jboss-ejb-client.xml", true,
"META-INF/jbosscmp-jdbc.xml", true,
"WEB-INF/jboss-webservices.xml", true,
"META-INF/non-existent-descriptor.xml", false,
};

Object[] pluginNames = new Object[] {
"org.jboss.as.plugins:jboss-as-maven-plugin", true,
"org.wildfly.plugins:wildfly-maven-plugin", true,
"org.wildfly.swarm:wildfly-swarm-plugin", false,
"io.thorntail:thorntail-maven-plugin", false,
"org.wildfly.plugins:wildfly-jar-maven-plugin", false,
};

assertAppServerDescriptorApplicability(descriptorNames);
assertPluginApplicability(pluginNames);
}

@Test
void isTomcat() throws Exception {
Object[] descriptorNames = new Object[] {
"META-INF/context.xml", true,
"META-INF/non-existent-descriptor.xml", false,
};

Object[] pluginNames = new Object[] {
"org.apache.tomcat.maven:tomcat6-maven-plugin", true,
"org.apache.tomcat.maven:tomcat7-maven-plugin", true,
};

assertAppServerDescriptorApplicability(descriptorNames);
assertPluginApplicability(pluginNames);
}
@ParameterizedTest(name = "{index}: with ''{1}'' descriptor file should return ''{0}'' handler")
@MethodSource("descriptorFiles")
void detect_withDescriptorFiles_shouldReturnApplicableHandler(String handlerName, Path relativeDescriptor) throws IOException {
final Path descriptor = folder.resolve("webapp").resolve(relativeDescriptor);
final Path xxxInf = descriptor.getParent();
Files.createDirectories(descriptor.getParent());
Files.createFile(descriptor);

GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(xxxInf.toFile())
.build()).build();

AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);
assertThat(appServerHandler)
.hasFieldOrPropertyWithValue("name", handlerName)
.returns(true, AppServerHandler::isApplicable);
}

static Stream<Arguments> descriptorFiles() {
return Stream.of(
Arguments.arguments("wildfly", Paths.get("WEB-INF", "jboss-deployment-structure.xml")),
Arguments.arguments("wildfly", Paths.get("META-INF", "jboss-deployment-structure.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "jboss-web.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "ejb-jar.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "jboss-ejb3.xml")),
Arguments.arguments("wildfly", Paths.get("META-INF", "persistence.xml")),
Arguments.arguments("wildfly", Paths.get("META-INF", "foo-jms.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "bar-jms.xml")),
Arguments.arguments("wildfly", Paths.get("META-INF", "my-datasource-ds.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "my-datasource-ds.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "jboss-ejb-client.xml")),
Arguments.arguments("wildfly", Paths.get("META-INF", "jbosscmp-jdbc.xml")),
Arguments.arguments("wildfly", Paths.get("WEB-INF", "jboss-webservices.xml")),
Arguments.arguments("tomcat", Paths.get("META-INF", "context.xml")),
Arguments.arguments("jetty", Paths.get("META-INF", "jetty-logging.properties")),
Arguments.arguments("jetty", Paths.get("WEB-INF", "jetty-web.xml"))
);
}

@ParameterizedTest(name = "{index}: with ''{1}:{2}:1.0.0'' plugin should return ''{0}'' handler")
@MethodSource("plugins")
void detect_withPlugin_shouldReturnApplicableHandler(String handlerName, String groupId, String artifactId) {
GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(folder.toFile())
.plugins(Collections.singletonList(Plugin.builder()
.groupId(groupId).artifactId(artifactId).version("1.0.0")
.build()))
.build()).build();
AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);
assertThat(appServerHandler)
.hasFieldOrPropertyWithValue("name", handlerName)
.returns(true, AppServerHandler::isApplicable);
}

static Stream<Arguments> plugins() {
return Stream.of(
Arguments.arguments("wildfly", "org.jboss.as.plugins", "jboss-as-maven-plugin"),
Arguments.arguments("wildfly", "org.wildfly.plugins", "wildfly-maven-plugin"),
Arguments.arguments("tomcat", "org.apache.tomcat.maven", "tomcat6-maven-plugin"),
Arguments.arguments("tomcat", "org.apache.tomcat.maven", "tomcat7-maven-plugin"),
Arguments.arguments("jetty", "org.mortbay.jetty", "jetty-maven-plugin"),
Arguments.arguments("jetty", "org.eclipse.jetty", "jetty-maven-plugin")
);
}

@Test
void detect_withSpecifiedServer_shouldReturnSpecifiedServer() {
GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder().build()).build();

AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect("tomcat");
assertThat(appServerHandler.getName()).isEqualTo("tomcat");
AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect("wildfly");
assertThat(appServerHandler.getName()).isEqualTo("wildfly");
}


@Test
void isJetty() throws Exception {
Object[] descriptorNames = new Object[] {
"META-INF/jetty-logging.properties", true,
"WEB-INF/jetty-web.xml", true,
"META-INF/non-existent-descriptor.xml", false,
};

Object[] pluginNames = new Object[] {
"org.mortbay.jetty:jetty-maven-plugin", true,
"org.eclipse.jetty:jetty-maven-plugin", true,
};

assertAppServerDescriptorApplicability(descriptorNames);
assertPluginApplicability(pluginNames);
}
void detect_withNotApplicableDescriptor_shouldReturnDefaultServer() throws IOException {
final Path descriptor = folder.resolve("webapp").resolve("META-INF").resolve("not-valid-descriptor.xml");
final Path xxxInf = descriptor.getParent();
Files.createDirectories(descriptor.getParent());
Files.createFile(descriptor);

@Test
void detect_withDefaultServer_shouldReturnTomcat() throws IOException {
File appDir = Files.createDirectory(folder.resolve("webapp")).toFile();
GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(appDir)
.plugins(Collections.singletonList(Plugin.builder()
.groupId("org.apache.tomcat.maven")
.artifactId("org.apache.tomcat.maven")
.version("testversion")
.configuration(Collections.emptyMap()).build()))
.build()).build();
GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(xxxInf.toFile())
.build()).build();

AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);
assertThat(appServerHandler.getName()).isEqualTo("tomcat");
assertThat(appServerHandler)
.isInstanceOf(TomcatAppSeverHandler.class)
.hasFieldOrPropertyWithValue("name", "tomcat")
.returns(false, AppServerHandler::isApplicable);
}

private void assertAppServerDescriptorApplicability(Object[] descriptors) throws IOException {
for (int i = 0; i < descriptors.length; i += 2) {
String[] descriptor = descriptors[i].toString().split("/");
String appDir = descriptor[0];
String file = descriptor[1];
boolean expected = (boolean) descriptors[i + 1];

Path webInf = Files.createDirectories(folder.resolve("webapp" + i).resolve(appDir));
Files.createFile(webInf.resolve(file));

GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(webInf.toFile())
.plugins(Collections.emptyList())
.build()).build();

AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);

String message = String.format("Expected descriptor %s to make isApplicable() return %s", descriptor, expected);
assertThat(appServerHandler.isApplicable()).as(message).isEqualTo(expected);
}
}

private void assertPluginApplicability(Object[] plugins) {
for (int i = 0; i < plugins.length; i += 2) {
String[] pluginCoordinate = plugins[i].toString().split(":");
String groupId = pluginCoordinate[0];
String artifactId = pluginCoordinate[1];
boolean expected = (boolean) plugins[i + 1];

GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(folder.toFile())
.plugins(Collections.singletonList(Plugin.builder()
.groupId(groupId).artifactId(artifactId)
.version("testversion")
.configuration(Collections.emptyMap()).build()))
.build()).build();

AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);

String message = String.format("Expected plugin %s to make isApplicable() return %s", pluginCoordinate, expected);
assertThat(appServerHandler.isApplicable()).as(message).isEqualTo(expected);
}
}
@ParameterizedTest(name = "{index}: with ''{0}:{1}:1.0.0'' plugin should return tomcat handler")
@MethodSource("notApplicablePlugins")
void detect_withNotApplicablePlugin_shouldReturnDefaultServer(String groupId, String artifactId) {
GeneratorContext generatorContext = GeneratorContext.builder().project(JavaProject.builder()
.buildDirectory(folder.toFile())
.plugins(Collections.singletonList(Plugin.builder()
.groupId(groupId).artifactId(artifactId).version("1.0.0")
.build()))
.build()).build();
AppServerHandler appServerHandler = new AppServerDetector(generatorContext).detect(null);
assertThat(appServerHandler)
.isInstanceOf(TomcatAppSeverHandler.class)
.hasFieldOrPropertyWithValue("name", "tomcat")
.returns(false, AppServerHandler::isApplicable);
}

static Stream<Arguments> notApplicablePlugins() {
return Stream.of(
Arguments.arguments("org.wildfly.swarm", "wildfly-swarm-plugin"),
Arguments.arguments("io.thorntail", "thorntail-maven-plugin"),
Arguments.arguments("org.wildfly.plugins", "wildfly-jar-maven-plugin")
);
}
}

0 comments on commit 02867c5

Please sign in to comment.