Merge branch '2.2.x'

This commit is contained in:
Marcin Grzejszczak
2020-07-13 11:13:58 +02:00
12 changed files with 148 additions and 38 deletions

View File

@@ -287,7 +287,7 @@ run the following command:
./scripts/parallelBuild.sh
```
To use eight 8 cores, run thke following command:
To use eight 8 cores, run the following command:
```
CORES=8 ./scripts/parallelBuild.sh

View File

@@ -82,6 +82,7 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -129,6 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath

View File

@@ -84,6 +84,7 @@ set CMD_LINE_ARGS=%*
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

View File

@@ -193,9 +193,8 @@ public class ContractDownloader {
}
private String wrapWithAntPattern(String path) {
String changedPath = path.replace(File.separator, "/");
return "**" + surroundWithSeparator(changedPath).replace(File.separator, "/")
+ "**/";
String changedPath = surroundWithSeparator(path).replace(File.separator, "/");
return "**" + changedPath.replace(File.separator, "/") + "**/";
}
private String groupArtifactToPattern(File contractsDirectory) {

View File

@@ -20,7 +20,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -76,7 +75,27 @@ public class FileStubDownloader implements StubDownloaderBuilder {
if (StringUtils.isEmpty(location) || !isProtocolAccepted(location)) {
return null;
}
return new StubsResource(location);
// Can be resolving a resource for Classpath as fallback
if (!location.startsWith("stubs://file://")) {
return new StubsResource(location);
}
// Convert any windows file format path to a uri
String correctlyFormattedLocation = convertLocationToUriFormat(location);
return new StubsResource(correctlyFormattedLocation);
}
private String convertLocationToUriFormat(String location) {
final String correctlyFormattedLocation = separatorsToUnix(location);
final String rawPath = correctlyFormattedLocation.replace("stubs://file://", "");
if (rawPath.charAt(0) != '/') {
return "stubs://file:///" + rawPath;
}
return correctlyFormattedLocation;
}
private String separatorsToUnix(String location) {
return location != null && location.indexOf(92) != -1
? location.replace('\\', '/') : location;
}
}
@@ -161,7 +180,7 @@ class StubsStubDownloader implements StubDownloader {
Resource resource = ResourceResolver.resource(schemeSpecificPart);
if (resource != null) {
try {
return Paths.get(resource.getURI()).toString();
return resource.getURL().getFile();
}
catch (IOException ex) {
return schemeSpecificPart;

View File

@@ -55,10 +55,10 @@ import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.ResourceUtils;
/**
@@ -221,7 +221,7 @@ class GitRepo {
return git;
}
catch (GitAPIException | URISyntaxException e) {
deleteBaseDirIfExists();
deleteBaseDirIfExists("Failed to initialize base directory");
throw new IllegalStateException(e);
}
}
@@ -236,7 +236,7 @@ class GitRepo {
return command.call();
}
catch (GitAPIException e) {
deleteBaseDirIfExists();
deleteBaseDirIfExists("Failed to delete base directory");
throw e;
}
finally {
@@ -290,13 +290,10 @@ class GitRepo {
return false;
}
private void deleteBaseDirIfExists() {
private void deleteBaseDirIfExists(String errorMessage) {
if (this.basedir.exists()) {
try {
FileUtils.delete(this.basedir, FileUtils.RECURSIVE);
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize base directory", e);
if (!FileSystemUtils.deleteRecursively(this.basedir)) {
throw new IllegalStateException(errorMessage);
}
}
}

View File

@@ -59,7 +59,7 @@ class AetherStubDownloaderSpec extends Specification {
and:
String localRepo = AetherFactories.localRepositoryDirectory(true)
new File(localRepo, "org/springframework/cloud/spring-cloud-contract-spec"
.replaceAll("/", File.separator)).list().size() > 0
.replace("/", File.separator)).list().size() > 0
and:
AetherStubDownloader aetherStubDownloader = new AetherStubDownloader(stubRunnerOptions)

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.contract.stubrunner;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.core.io.Resource;
/**
* @author Matty A
*/
public class FileStubDownloaderTests {
@Test
public void resolve() {
final FileStubDownloader fileStubDownloader = new FileStubDownloader();
Resource expectedUnixResource = new StubsResource("stubs://file:///User/A/B/C");
Resource expectedWindowsResource = new StubsResource(
"stubs://file:///C:/Users/A/B/C");
String unixFileFormat = "stubs://file:///User/A/B/C";
String windowsFileFormat = "stubs://file://C:\\Users\\A\\B\\C";
String windowsFileFormatCorrectPathStart = "stubs://file:///C:\\Users\\A\\B\\C";
Assertions.assertThat(expectedUnixResource)
.isEqualTo(fileStubDownloader.resolve(unixFileFormat, null));
Assertions.assertThat(expectedWindowsResource)
.isEqualTo(fileStubDownloader.resolve(windowsFileFormat, null));
Assertions.assertThat(expectedWindowsResource).isEqualTo(
fileStubDownloader.resolve(windowsFileFormatCorrectPathStart, null));
}
}

View File

@@ -86,12 +86,12 @@ public class GitStubDownloaderTests {
public void should_pick_stubs_for_group_and_artifact_with_version_from_a_git_repo()
throws Exception {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file("/git_samples/contract-git/")
.getAbsolutePath() + "/").replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-git/").getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
Map.Entry<StubConfiguration, File> entry = stubDownloader
@@ -107,12 +107,12 @@ public class GitStubDownloaderTests {
public void should_pick_latest_build_snapshot_stubs_when_latest_version_set()
throws URISyntaxException {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file("/git_samples/contract-git/")
.getAbsolutePath() + "/").replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-git/").getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
Map.Entry<StubConfiguration, File> entry = stubDownloader
@@ -149,12 +149,12 @@ public class GitStubDownloaderTests {
public void should_pick_latest_release_stubs_when_release_version_set()
throws URISyntaxException {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file("/git_samples/contract-git/")
.getAbsolutePath() + "/").replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-git/").getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
Map.Entry<StubConfiguration, File> entry = stubDownloader
@@ -177,13 +177,14 @@ public class GitStubDownloaderTests {
public void should_pick_latest_build_snapshot_stubs_when_latest_version_set_and_latest_folder_exists()
throws URISyntaxException {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file(
"/git_samples/contract-predefined-names-git/").getAbsolutePath()
.replace("/", File.separator)
+ "/").replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-predefined-names-git/")
.getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
Map.Entry<StubConfiguration, File> entry = stubDownloader
@@ -213,13 +214,13 @@ public class GitStubDownloaderTests {
public void should_pick_release_folder_when_release_version_set()
throws URISyntaxException {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file(
"/git_samples/contract-predefined-names-git/").getAbsolutePath() + "/")
.replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-predefined-names-git/")
.getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
Map.Entry<StubConfiguration, File> entry = stubDownloader
@@ -242,12 +243,12 @@ public class GitStubDownloaderTests {
public void should_fail_to_fetch_stubs_when_concrete_version_was_not_specified()
throws URISyntaxException {
StubDownloaderBuilder stubDownloaderBuilder = new ScmStubDownloaderBuilder();
String contractFolderLocation = (file("/git_samples/contract-git/")
.getAbsolutePath() + "/").replace(File.separator, "/");
StubDownloader stubDownloader = stubDownloaderBuilder
.build(new StubRunnerOptionsBuilder()
.withStubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withStubRepositoryRoot("git://"
+ file("/git_samples/contract-git").getAbsolutePath()
+ "/")
.withStubRepositoryRoot("git://" + contractFolderLocation)
.withProperties(props()).build());
try {

View File

@@ -16,7 +16,9 @@
package org.springframework.cloud.contract.verifier.util.xml
import com.sun.org.apache.xml.internal.security.utils.DOMNamespaceContext
import javax.xml.namespace.NamespaceContext
import java.util.stream.IntStream
import javax.xml.parsers.DocumentBuilder
@@ -87,10 +89,13 @@ class XmlToXPathsConverter {
private static String getNodeValue(String path, Object body) {
XPath xPath = XPathFactory.newInstance().newXPath()
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance()
builderFactory.setNamespaceAware(true)
DocumentBuilder documentBuilder = builderFactory
.newDocumentBuilder()
Document parsedXml = documentBuilder.
parse(new InputSource(new StringReader(body as String)))
xPath.setNamespaceContext(new DOMNamespaceContext(parsedXml.documentElement))
return xPath.evaluate(path, parsedXml.documentElement)
}

View File

@@ -0,0 +1,40 @@
package org.springframework.cloud.contract.verifier.util.xml
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
import javax.xml.xpath.XPathExpressionException
class XmlToXPathsConverterSpec extends Specification {
@Shared
String namedXml = '''<ns1:customer xmlns:ns1="http://demo.com/testns">
<email>customer@test.com</email>
</ns1:customer>
'''
@Shared
String unnamedXml = '''<customer>
<email>customer@test.com</email>
</customer>
'''
@Unroll
def "should generate [#expectedValue] for xPath [#value]"() {
expect:
value == expectedValue
where:
value || expectedValue
XmlToXPathsConverter.retrieveValueFromBody("/ns1:customer/email/text()", namedXml) || '''customer@test.com'''
XmlToXPathsConverter.retrieveValueFromBody("/customer/email/text()", namedXml) || ''''''
XmlToXPathsConverter.retrieveValueFromBody("/customer/email/text()", unnamedXml) || '''customer@test.com'''
}
@Unroll
def "should throw exception when searching for inexistent name space"() {
when:
XmlToXPathsConverter.retrieveValueFromBody("/ns1:customer/email/text()", unnamedXml)
then:
def e = thrown(XPathExpressionException)
e.message.contains('Prefix must resolve to a namespace: ns1')
}
}