Skip to content

Commit

Permalink
Adding tests for Maven plugin
Browse files Browse the repository at this point in the history
* Adding complete test infrastructure for Maven plugin.
* Plugin is able to work with DI maven container.

Refers to issue #9
  • Loading branch information
cardil committed Jan 11, 2019
1 parent ef1eb50 commit c41f89a
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 30 deletions.
4 changes: 4 additions & 0 deletions plugs-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<name>Plugs :: Plugs Core</name>

<dependencies>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
</dependency>
<dependency>
<groupId>pl.wavesoftware</groupId>
<artifactId>eid-exceptions</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions plugs-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<name>Plugs :: Plugs Maven Plugin</name>

<dependencies>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand All @@ -42,11 +46,21 @@
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<scope>provided</scope>
</dependency>

<!-- Tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package pl.wavesoftware.plugs.maven.generator;

import org.apiguardian.api.API;

import javax.inject.Named;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 2019-01-10
* @since 0.1.0
*/
@API(status = API.Status.EXPERIMENTAL)
@Named
final class DefaultWorker implements Worker {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@

package pl.wavesoftware.plugs.maven.generator;

import com.google.common.annotations.VisibleForTesting;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import javax.inject.Inject;

/**
* A main mojo to generate plug modules
*
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 0.1.0
*/
@Mojo(
name = "package-plug",
name = PackagePlugMojo.GOAL,
defaultPhase = LifecyclePhase.PACKAGE,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME
)
final class PackagePlugMojo extends AbstractMojo {
public final class PackagePlugMojo extends AbstractMojo {

@SuppressWarnings("WeakerAccess")
@API(status = Status.STABLE)
public static final String GOAL = "package-plug";

private final Worker worker;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package pl.wavesoftware.plugs.maven.generator;

import org.apiguardian.api.API;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 2019-01-10
* @since 0.1.0
*/
@API(status = API.Status.EXPERIMENTAL)
interface Worker {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019 Wave Software
*
* 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 pl.wavesoftware.plugs.maven.generator;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;

import java.nio.file.Path;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 0.1.0
*/
final class DefaultMojoConfigurator implements MojoConfigurator {
@Override
public MavenSession getMavenSession(MojoRule rule, Path pomDirectory) throws Exception {
// setup with pom
MavenProject project = rule.readMavenProject(pomDirectory.toFile());

// Generate session
return rule.newMavenSession(project);
}

@Override
public MojoExecution getMojoExecution(MojoRule rule, String goal) {
// Generate Execution and Mojo for testing
return rule.newMojoExecution(goal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 Wave Software
*
* 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 pl.wavesoftware.plugs.maven.generator;

import org.apache.maven.plugin.AbstractMojo;

import java.nio.file.Path;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 0.1.0
*/
public interface MojoBuilder<T extends AbstractMojo> {
MojoBuilder<T> withPomDirectory(Path pomDirectory);
MojoBuilder<T> withUsingResources(boolean setting);
T build(String goal);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2019 Wave Software
*
* 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 pl.wavesoftware.plugs.maven.generator;

import org.apache.maven.plugin.AbstractMojo;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 0.1.0
*/
public interface MojoBuilderFactory {
MojoBuilderFactory configurator(MojoConfigurator configurator);
<T extends AbstractMojo> MojoBuilder<T> builder(Class<T> mojoType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2019 Wave Software
*
* 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 pl.wavesoftware.plugs.maven.generator;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.testing.MojoRule;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 2019-01-11
*/
final class MojoBuilderFactoryImpl implements MojoBuilderFactory {

private final MojoRule rule;

private MojoConfigurator configurator = new DefaultMojoConfigurator();

MojoBuilderFactoryImpl(MojoRule rule) {
this.rule = rule;
}

@Override
public MojoBuilderFactory configurator(MojoConfigurator configurator) {
this.configurator = configurator;
return this;
}

@Override
public <T extends AbstractMojo> MojoBuilder<T> builder(Class<T> mojoType) {
return new MojoBuilderImpl<>(rule, mojoType, configurator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2019 Wave Software
*
* 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 pl.wavesoftware.plugs.maven.generator;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.MojoRule;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import static pl.wavesoftware.eid.utils.EidExecutions.tryToExecute;
import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull;

/**
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
* @since 0.1.0
*/
final class MojoBuilderImpl<T extends AbstractMojo>
implements MojoBuilder<T> {

private final MojoRule mojoRule;
private final Class<T> type;
private final MojoConfigurator configurator;

private Path pomDirectory = Paths.get(".");
private boolean usingResources = true;

MojoBuilderImpl(MojoRule mojoRule, Class<T> type, MojoConfigurator configurator) {
this.mojoRule = mojoRule;
this.type = type;
this.configurator = configurator;
}

@Override
public MojoBuilder<T> withPomDirectory(Path pomDirectory) {
this.pomDirectory = pomDirectory;
return this;
}

@Override
public MojoBuilder<T> withUsingResources(boolean setting) {
this.usingResources = setting;
return this;
}

@Override
public T build(String goal) {
return tryToExecute(() -> {
MavenSession session = configurator.getMavenSession(
mojoRule, getPomDirectory()
);
MojoExecution execution = configurator.getMojoExecution(
mojoRule,
goal
);
org.apache.maven.plugin.Mojo mojo =
mojoRule.lookupConfiguredMojo(session, execution);
return type.cast(mojo);
}, "20190111:223034");
}

private Path getPomDirectory() throws URISyntaxException {
if (usingResources) {
URL url = checkNotNull(
type.getClassLoader().getResource(pomDirectory.toString()),
"20190111:224609"
);
File file = new File(url.toURI());
return file.toPath();
}
return pomDirectory;
}
}
Loading

0 comments on commit c41f89a

Please sign in to comment.