Skip to content

Commit f262519

Browse files
authored
Merge pull request #2376 from marcingrzejszczak/issues_#2362
Injects Maven's repositorySystem to Aether downloader
2 parents 66dbe1b + c5a09d0 commit f262519

File tree

31 files changed

+811
-263
lines changed

31 files changed

+811
-263
lines changed

spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class AetherStubDownloader implements StubDownloader {
7070
// Preloading class for the shutdown hook not to throw ClassNotFound
7171
private static final Class CLAZZ = TemporaryFileStorage.class;
7272

73+
// In order to allow to pass the injected values from Maven
74+
private static RepositorySystem repositorySystemFromMaven;
75+
7376
private final List<RemoteRepository> remoteRepos;
7477

7578
private final RepositorySystem repositorySystem;
@@ -105,7 +108,7 @@ public AetherStubDownloader(StubRunnerOptions stubRunnerOptions) {
105108
throw new UnsupportedOperationException(
106109
"You can't use Aether downloader when you use classpath to find stubs");
107110
}
108-
this.repositorySystem = AetherFactories.repositorySystemOr(null);
111+
this.repositorySystem = AetherFactories.repositorySystemOr(repositorySystemFromMaven);
109112
this.workOffline = stubRunnerOptions.stubsMode == StubRunnerProperties.StubsMode.LOCAL;
110113
this.session = newSession(this.repositorySystem, this.workOffline);
111114
registerShutdownHook();
@@ -278,4 +281,13 @@ private void registerShutdownHook() {
278281
new Thread(() -> TemporaryFileStorage.cleanup(AetherStubDownloader.this.deleteStubsAfterTest)));
279282
}
280283

284+
/**
285+
* Use this if you're in Maven and have the {@link RepositorySystem} injected to you
286+
* through the {@code Component} annotation.
287+
* @param fromMaven system injected to you by Maven
288+
*/
289+
public static void setRepositorySystemFromMaven(RepositorySystem fromMaven) {
290+
repositorySystemFromMaven = fromMaven;
291+
}
292+
281293
}

spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/pom.xml

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,6 @@
5757
</excludes>
5858
</configuration>
5959
</plugin>
60-
<!--<plugin>
61-
<groupId>org.apache.maven.plugins</groupId>
62-
<artifactId>maven-failsafe-plugin</artifactId>
63-
<dependencies>
64-
&lt;!&ndash; let JUnit vintage engine run JUnit 3 or JUnit 4 tests &ndash;&gt;
65-
<dependency>
66-
<groupId>org.junit.jupiter</groupId>
67-
<artifactId>junit-jupiter-engine</artifactId>
68-
<version>${junit-jupiter.version}</version>
69-
</dependency>
70-
<dependency>
71-
<groupId>org.junit.vintage</groupId>
72-
<artifactId>junit-vintage-engine</artifactId>
73-
<version>${junit-vintage.version}</version>
74-
</dependency>
75-
</dependencies>
76-
<configuration>
77-
<reportFormat>plain</reportFormat>
78-
<failIfNoTests>true</failIfNoTests>
79-
<junitPlatformArtifactName>
80-
org.junit.jupiter:junit-vintage-engine
81-
</junitPlatformArtifactName>
82-
<includes>
83-
<include>**/PluginUnitTest.java</include>
84-
</includes>
85-
</configuration>
86-
<executions>
87-
<execution>
88-
<goals>
89-
&lt;!&ndash; <goal>integration-test</goal>&ndash;&gt;
90-
<goal>verify</goal>
91-
</goals>
92-
</execution>
93-
</executions>
94-
</plugin>-->
9560
<plugin>
9661
<groupId>org.jacoco</groupId>
9762
<artifactId>jacoco-maven-plugin</artifactId>
@@ -394,10 +359,9 @@
394359
<version>${maven.version}</version>
395360
<scope>test</scope>
396361
</dependency>
397-
<!-- TODO: Migrate to junit5 for maven tests -->
398362
<dependency>
399-
<groupId>junit</groupId>
400-
<artifactId>junit</artifactId>
363+
<groupId>org.apache.maven.resolver</groupId>
364+
<artifactId>maven-resolver-supplier</artifactId>
401365
<scope>test</scope>
402366
</dependency>
403367
<dependency>

spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/ConvertMojo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.apache.maven.plugins.annotations.Parameter;
3232
import org.apache.maven.project.MavenProject;
3333
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
34+
import org.eclipse.aether.RepositorySystem;
3435
import org.eclipse.aether.RepositorySystemSession;
3536

37+
import org.springframework.cloud.contract.stubrunner.AetherStubDownloader;
3638
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
3739
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
3840
import org.springframework.cloud.contract.verifier.converter.RecursiveFilesConverter;
@@ -56,6 +58,9 @@ public class ConvertMojo extends AbstractMojo {
5658
static final String CONTRACTS_PATH = "/contracts";
5759
static final String ORIGINAL_PATH = "/original";
5860

61+
@Component
62+
private RepositorySystem repositorySystem;
63+
5964
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
6065
private RepositorySystemSession repoSession;
6166

@@ -209,6 +214,8 @@ public void execute() throws MojoExecutionException {
209214
// download contracts, unzip them and pass as output directory
210215
ContractVerifierConfigProperties config = new ContractVerifierConfigProperties();
211216
config.setExcludeBuildFolders(this.excludeBuildFolders);
217+
// download contracts, unzip them and pass as output directory
218+
AetherStubDownloader.setRepositorySystemFromMaven(repositorySystem);
212219
File contractsDirectory = locationOfContracts(config);
213220
contractsDirectory = contractSubfolderIfPresent(contractsDirectory);
214221

spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/GenerateTestsMojo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
import org.apache.maven.plugin.MojoExecution;
2929
import org.apache.maven.plugin.MojoExecutionException;
3030
import org.apache.maven.plugin.MojoFailureException;
31+
import org.apache.maven.plugins.annotations.Component;
3132
import org.apache.maven.plugins.annotations.LifecyclePhase;
3233
import org.apache.maven.plugins.annotations.Mojo;
3334
import org.apache.maven.plugins.annotations.Parameter;
3435
import org.apache.maven.plugins.annotations.ResolutionScope;
3536
import org.apache.maven.project.MavenProject;
37+
import org.eclipse.aether.RepositorySystem;
3638
import org.eclipse.aether.RepositorySystemSession;
3739

3840
import org.springframework.cloud.contract.spec.ContractVerifierException;
41+
import org.springframework.cloud.contract.stubrunner.AetherStubDownloader;
3942
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
4043
import org.springframework.cloud.contract.verifier.TestGenerator;
4144
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
@@ -53,6 +56,11 @@
5356
requiresDependencyResolution = ResolutionScope.TEST)
5457
public class GenerateTestsMojo extends AbstractMojo {
5558

59+
@Component
60+
private RepositorySystem repositorySystem;
61+
62+
// Required for the case where there is no configuration (check the basic project for
63+
// tests)
5664
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
5765
private RepositorySystemSession repoSession;
5866

@@ -263,6 +271,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
263271
final ContractVerifierConfigProperties config = new ContractVerifierConfigProperties();
264272
config.setFailOnInProgress(this.failOnInProgress);
265273
// download contracts, unzip them and pass as output directory
274+
AetherStubDownloader.setRepositorySystemFromMaven(repositorySystem);
266275
File contractsDirectory = new MavenContractsDownloader(this.project, this.contractDependency,
267276
this.contractsPath, this.contractsRepositoryUrl, this.contractsMode, getLog(),
268277
this.contractsRepositoryUsername, this.contractsRepositoryPassword, this.contractsRepositoryProxyHost,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.contract.maven.verifier;
18+
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
22+
import org.apache.maven.api.plugin.testing.MojoExtension;
23+
import org.apache.maven.plugin.AbstractMojo;
24+
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
25+
import org.eclipse.aether.DefaultRepositorySystemSession;
26+
import org.eclipse.aether.RepositorySystem;
27+
import org.eclipse.aether.repository.LocalRepository;
28+
import org.eclipse.aether.supplier.RepositorySystemSupplier;
29+
import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.BeforeEach;
32+
33+
import static org.assertj.core.api.BDDAssertions.then;
34+
35+
/**
36+
* Small helper for the project-per-folder integration tests.
37+
*/
38+
abstract class AbstractProjectIntegrationTests extends AbstractMojoIntegrationTests {
39+
40+
private Path targetDir;
41+
42+
@BeforeEach
43+
void setUpTargetDir() throws Exception {
44+
Path projectDir = Path.of(MojoExtension.getBasedir());
45+
this.targetDir = projectDir.resolve("target");
46+
deleteDirectoryIfExists(this.targetDir);
47+
Files.createDirectories(this.targetDir);
48+
}
49+
50+
@AfterEach
51+
void cleanTargetDir() throws Exception {
52+
deleteDirectoryIfExists(this.targetDir);
53+
}
54+
55+
void runConvertMojo(ConvertMojo mojo) throws Exception {
56+
configureRepositorySystem(mojo);
57+
setupBuildPaths(mojo, this.targetDir);
58+
Path stubsDir = this.targetDir.resolve("stubs");
59+
MojoExtension.setVariableValueToObject(mojo, "stubsDirectory", stubsDir.toFile());
60+
MojoExtension.setVariableValueToObject(mojo, "incrementalContractStubs", false);
61+
mojo.execute();
62+
then(Files.exists(stubsDir)).as("generated stubs directory should exist at %s", stubsDir).isTrue();
63+
}
64+
65+
void runGenerateTestsMojo(GenerateTestsMojo mojo) throws Exception {
66+
configureRepositorySystem(mojo);
67+
setupBuildPaths(mojo, this.targetDir);
68+
Path generatedSources = this.targetDir.resolve("generated-test-sources/contracts");
69+
Path generatedResources = this.targetDir.resolve("generated-test-resources/contracts");
70+
MojoExtension.setVariableValueToObject(mojo, "generatedTestSourcesDir", generatedSources.toFile());
71+
MojoExtension.setVariableValueToObject(mojo, "generatedTestResourcesDir", generatedResources.toFile());
72+
MojoExtension.setVariableValueToObject(mojo, "incrementalContractTests", false);
73+
mojo.execute();
74+
then(Files.exists(generatedSources)).as("generated test sources should exist at %s", generatedSources).isTrue();
75+
then(Files.exists(generatedResources)).as("generated test resources should exist at %s", generatedResources)
76+
.isTrue();
77+
}
78+
79+
void configureRemoteRepo(AbstractMojo mojo, WireMockRepositoryServer server) throws Exception {
80+
MojoExtension.setVariableValueToObject(mojo, "contractsRepositoryUrl", server.baseUrl());
81+
}
82+
83+
Path repositoryRoot() {
84+
return Path.of("src/test/resources/m2repo/repository").toAbsolutePath();
85+
}
86+
87+
private void configureRepositorySystem(AbstractMojo mojo) throws Exception {
88+
RepositorySystem repoSystem = new RepositorySystemSupplier().get();
89+
DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
90+
LocalRepository localRepo = new LocalRepository(this.targetDir.resolve("local-repo").toFile());
91+
repoSession.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(repoSession, localRepo));
92+
repoSession.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
93+
MojoExtension.setVariableValueToObject(mojo, "repositorySystem", repoSystem);
94+
MojoExtension.setVariableValueToObject(mojo, "repoSession", repoSession);
95+
}
96+
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.contract.maven.verifier;
18+
19+
import org.apache.maven.api.plugin.testing.Basedir;
20+
import org.apache.maven.api.plugin.testing.InjectMojo;
21+
import org.junit.jupiter.api.Test;
22+
23+
class BasicBaseclassFromMappingsIntegrationTests extends AbstractProjectIntegrationTests {
24+
25+
@Test
26+
@InjectMojo(goal = "convert", pom = "pom.xml")
27+
@Basedir("src/test/projects/basic-baseclass-from-mappings")
28+
void shouldHandleBaseClassMappings(ConvertMojo mojo) throws Exception {
29+
runConvertMojo(mojo);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.contract.maven.verifier;
18+
19+
import org.apache.maven.api.plugin.testing.Basedir;
20+
import org.apache.maven.api.plugin.testing.InjectMojo;
21+
import org.junit.jupiter.api.Test;
22+
23+
class BasicGeneratedBaseclassIntegrationTests extends AbstractProjectIntegrationTests {
24+
25+
@Test
26+
@InjectMojo(goal = "convert", pom = "pom.xml")
27+
@Basedir("src/test/projects/basic-generated-baseclass")
28+
void shouldGenerateBaseClassFromConvention(ConvertMojo mojo) throws Exception {
29+
runConvertMojo(mojo);
30+
}
31+
32+
}

spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/test/projects/basic/${project.build.directory}/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/contracts/Sample.groovy renamed to spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/test/java/org/springframework/cloud/contract/maven/verifier/BasicIntegrationTests.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/ org.springframework.cloud.contract.spec.Contract.make {
16-
request {
17-
method 'POST'
18-
url('/users') {
15+
*/
1916

20-
}
21-
headers {
22-
header 'Content-Type': 'application/json'
23-
}
24-
body '''{ "login" : "john", "name": "John The Contract" }'''
25-
}
26-
response {
27-
status OK()
28-
headers {
29-
header 'Location': '/users/john'
30-
}
17+
package org.springframework.cloud.contract.maven.verifier;
18+
19+
import org.apache.maven.api.plugin.testing.Basedir;
20+
import org.apache.maven.api.plugin.testing.InjectMojo;
21+
import org.junit.jupiter.api.Test;
22+
23+
class BasicIntegrationTests extends AbstractProjectIntegrationTests {
24+
25+
@Test
26+
@InjectMojo(goal = "convert", pom = "pom.xml")
27+
@Basedir("src/test/projects/basic")
28+
void shouldConvertContracts(ConvertMojo mojo) throws Exception {
29+
runConvertMojo(mojo);
3130
}
31+
3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.contract.maven.verifier;
18+
19+
import org.apache.maven.api.plugin.testing.Basedir;
20+
import org.apache.maven.api.plugin.testing.InjectMojo;
21+
import org.junit.jupiter.api.Test;
22+
23+
class BasicRemoteContractsIntegrationTests extends AbstractProjectIntegrationTests {
24+
25+
@Test
26+
@InjectMojo(goal = "convert", pom = "pom.xml")
27+
@Basedir("src/test/projects/basic-remote-contracts")
28+
void shouldDownloadContractsFromRemoteRepo(ConvertMojo mojo) throws Exception {
29+
try (WireMockRepositoryServer server = WireMockRepositoryServer.start(repositoryRoot())) {
30+
configureRemoteRepo(mojo, server);
31+
runConvertMojo(mojo);
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)