Skip to content

Commit

Permalink
chore: upgrade to 1.9.0-M17
Browse files Browse the repository at this point in the history
Motivation:
Interactive & CI envvar detection should be shared in common plugin library.

Modifications:
- Upgraded to 1.9.0-M16
- Deleted interactive() method
- Pull-up buildTool field, requireBatchMode, pluginConfiguration.

Result:
Less logic in maven plugin, more in common !
  • Loading branch information
bastien-gatling committed Apr 9, 2024
1 parent 0fbaab0 commit 5521656
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<maven-plugin-annotations.version>3.6.2</maven-plugin-annotations.version>
<header.basedir>${project.basedir}</header.basedir>
<junit.version>5.10.2</junit.version>
<gatling-enterprise-plugin-commons.version>1.9.0-M14</gatling-enterprise-plugin-commons.version>
<gatling-enterprise-plugin-commons.version>1.9.0-M17</gatling-enterprise-plugin-commons.version>

<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<maven-plugin-plugin.version>3.12.0</maven-plugin-plugin.version>
Expand Down
48 changes: 26 additions & 22 deletions src/main/java/io/gatling/mojo/AbstractEnterprisePluginMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package io.gatling.mojo;

import io.gatling.plugin.*;
import io.gatling.plugin.client.EnterpriseClient;
import io.gatling.plugin.client.HttpEnterpriseClient;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.exceptions.UnsupportedClientException;
import io.gatling.plugin.io.JavaPluginScanner;
import io.gatling.plugin.io.PluginIO;
import io.gatling.plugin.io.PluginLogger;
import io.gatling.plugin.io.PluginScanner;
import io.gatling.plugin.model.BuildTool;
import java.net.URL;
import java.util.Scanner;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -81,16 +81,8 @@ public PluginScanner getScanner() {
}
};

protected BatchEnterprisePlugin initBatchEnterprisePlugin() throws MojoFailureException {
return new BatchEnterprisePluginClient(initEnterpriseClient(), pluginLogger);
}

protected InteractiveEnterprisePlugin initInteractiveEnterprisePlugin()
private PluginConfiguration pluginConfiguration(Boolean forceBatchMode)
throws MojoFailureException {
return new InteractiveEnterprisePluginClient(initEnterpriseClient(), pluginIO);
}

private EnterpriseClient initEnterpriseClient() throws MojoFailureException {
if (apiToken == null) {
final String msg =
"Missing API token\n"
Expand All @@ -103,21 +95,33 @@ private EnterpriseClient initEnterpriseClient() throws MojoFailureException {
"MY_API_TOKEN_VALUE");
throw new MojoFailureException(msg);
}
final String pluginTitle = getClass().getPackage().getImplementationTitle();
final String pluginVersion = getClass().getPackage().getImplementationVersion();
if (pluginTitle == null || pluginVersion == null) {
// Should no happen if the plugin is built and packaged properly
throw new IllegalStateException("Gatling plugin title and version not found");
return new PluginConfiguration(
enterpriseUrl,
apiToken,
controlPlaneUrl,
BuildTool.MAVEN,
pluginVersion(),
forceBatchMode,
pluginIO);
}

protected BatchEnterprisePlugin initBatchEnterprisePlugin() throws MojoFailureException {
try {
return EnterprisePluginProvider.getBatchInstance(pluginConfiguration(true));
} catch (UnsupportedClientException e) {
throw new UnsupportedClientMojoException(e);
} catch (EnterprisePluginException e) {
throw new MojoFailureException(e.getMessage(), e);
}
}

protected EnterprisePlugin initEnterprisePlugin(Boolean forceBatchMode)
throws MojoFailureException {
try {
return new HttpEnterpriseClient(
enterpriseUrl, apiToken, pluginTitle, pluginVersion, controlPlaneUrl);
return EnterprisePluginProvider.getInstance(pluginConfiguration(forceBatchMode));
} catch (UnsupportedClientException e) {
throw new MojoFailureException(
"Please update the Gatling Maven plugin to the latest version for compatibility with Gatling Enterprise. See https://gatling.io/docs/gatling/reference/current/extensions/maven_plugin/ for more information about this plugin.",
e);
} catch (Exception e) {
throw new UnsupportedClientMojoException(e);
} catch (EnterprisePluginException e) {
throw new MojoFailureException(e.getMessage(), e);
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/io/gatling/mojo/AbstractGatlingMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.gatling.mojo;

import io.gatling.plugin.io.PluginLogger;
import io.gatling.plugin.model.BuildTool;
import io.gatling.plugin.util.Fork;
import io.gatling.plugin.util.ForkMain;
import io.gatling.plugin.util.JavaLocator;
Expand Down Expand Up @@ -48,18 +49,19 @@ public abstract class AbstractGatlingMojo extends AbstractMojo {
/** Maven's repository. */
@Component protected RepositorySystem repository;

/**
* Uses 2 different mechanisms to detect if the plugin is in interactive mode:
*
* <ul>
* <li>the kind-of standard CI env var on CI tools
* <li>the standard maven option -B,--batch-mode Run in non-interactive (batch), see mvn:help
* </ul>
*
* @return if the plugin is in interactive mode
*/
protected boolean interactive() {
return session.getRequest().isInteractiveMode() && !Boolean.parseBoolean(System.getenv("CI"));
protected BuildTool buildTool = BuildTool.MAVEN;

protected String pluginVersion() {
final String pluginVersion = getClass().getPackage().getImplementationVersion();
if (pluginVersion == null) {
// Should not happen if the plugin is built and packaged properly
throw new IllegalStateException("Gatling plugin title and version not found");
}
return pluginVersion;
}

protected Boolean requireBatchMode() {
return !session.getRequest().isInteractiveMode();
}

protected List<String> buildTestClasspath() throws Exception {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/gatling/mojo/CommonLogMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public final class CommonLogMessage {
private CommonLogMessage() {}

public static String simulationStartSuccess(URL enterpriseUrl, String reportsPath) {
return "Simulation successfully started; reports are available at " + enterpriseUrl + reportsPath;
return "Simulation successfully started; reports are available at "
+ enterpriseUrl
+ reportsPath;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/io/gatling/mojo/EnterpriseDeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.gatling.plugin.BatchEnterprisePlugin;
import io.gatling.plugin.deployment.DeploymentConfiguration;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.model.BuildTool;
import io.gatling.plugin.model.DeploymentInfo;
import java.io.File;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -45,9 +44,7 @@ public void execute() throws MojoFailureException {
deploymentFile,
packageFile,
mavenProject.getArtifactId(),
isPrivateRepositoryEnabled,
BuildTool.MAVEN,
getClass().getPackage().getImplementationVersion());
isPrivateRepositoryEnabled);

getPluginContext().put(CONTEXT_ENTERPRISE_DEPLOY_INFO, deploymentInfo);
} catch (EnterprisePluginException e) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/io/gatling/mojo/EnterpriseStartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void execute() throws MojoFailureException {
final Map context = getPluginContext();
final DeploymentInfo deploymentInfo =
(DeploymentInfo) context.get(EnterpriseDeployMojo.CONTEXT_ENTERPRISE_DEPLOY_INFO);
final EnterprisePlugin plugin = initEnterprisePlugin(interactive());
final EnterprisePlugin plugin = initEnterprisePlugin(requireBatchMode());

try {
RunSummary runSummary = plugin.startSimulation(simulationName, deploymentInfo);
Expand All @@ -71,10 +71,6 @@ public void execute() throws MojoFailureException {
}
}

private EnterprisePlugin initEnterprisePlugin(boolean isInteractive) throws MojoFailureException {
return isInteractive ? initInteractiveEnterprisePlugin() : initBatchEnterprisePlugin();
}

private void waitForRunEnd(EnterprisePlugin plugin, RunSummary startedRun)
throws MojoFailureException {
if (waitForRunEnd) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/gatling/mojo/GatlingMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.gatling.plugin.GatlingConstants;
import io.gatling.plugin.SimulationSelector;
import io.gatling.plugin.model.BuildPlugin;
import io.gatling.plugin.util.Fork;
import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -354,7 +355,7 @@ private List<String> simulations() throws MojoFailureException {
includes,
excludes,
runMultipleSimulations,
interactive());
BuildPlugin.getInstance(buildTool, pluginVersion(), requireBatchMode()).interactive);

SimulationSelector.Result.Error error = result.error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package io.gatling.mojo;

import io.gatling.plugin.EmptyChoicesException;
import io.gatling.plugin.exceptions.EnterprisePluginException;
import io.gatling.plugin.exceptions.UnsupportedJavaVersionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -46,8 +45,6 @@ static <R> R handle(EnterprisePluginExceptionFunction<R> f, Log log) throws Mojo
+ e.supportedVersion
+ " or lower.";
throw new MojoFailureException(msg);
} catch (EmptyChoicesException e) {
throw new MojoFailureException(e.getMessage(), e);
} catch (EnterprisePluginException e) {
throw new MojoFailureException(
"Unhandled Gatling Enterprise plugin exception: " + e.getMessage(), e);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/gatling/mojo/UnsupportedClientMojoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/*
* Copyright 2011-2022 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.mojo;

import io.gatling.plugin.exceptions.UnsupportedClientException;
import org.apache.maven.plugin.MojoFailureException;

public class UnsupportedClientMojoException extends MojoFailureException {
public UnsupportedClientMojoException(UnsupportedClientException e) {
super(
"Please update the Gatling Maven plugin to the latest version for compatibility with Gatling Enterprise. See https://gatling.io/docs/gatling/reference/current/extensions/maven_plugin/ for more information about this plugin.",
e);
}
}

0 comments on commit 5521656

Please sign in to comment.