Merge branch '3.1.x'

This commit is contained in:
Marcin Grzejszczak
2022-09-01 12:49:33 +02:00
3 changed files with 40 additions and 2 deletions

View File

@@ -48,7 +48,7 @@ public class ResourcesFileSource implements FileSource {
this(toSources(resources));
}
private ResourcesFileSource(FileSource... sources) {
protected ResourcesFileSource(FileSource... sources) {
this.sources = sources;
}
@@ -110,6 +110,12 @@ public class ResourcesFileSource implements FileSource {
public BinaryFile getBinaryFileNamed(String name) {
for (FileSource resource : this.sources) {
try {
if (resource instanceof ClasspathFileSource) {
ClasspathFileSource classpathFileSource = (ClasspathFileSource) resource;
if (classpathFileSource.exists() && compressedResource(classpathFileSource.getUri())) {
return classpathFileSource.getBinaryFileNamed(name);
}
}
UrlResource uri = new UrlResource(resource.getUri());
if (uri.exists()) {
Resource relativeResource = new UrlResource(uri.getURI() + "/" + name);
@@ -118,8 +124,11 @@ public class ResourcesFileSource implements FileSource {
}
}
}
catch (RuntimeException e) {
// Ignore - find next stub file
}
catch (IOException e) {
// Ignore
// Ignore - find next stub file
}
}
throw new IllegalStateException("Cannot create file for " + name);

View File

@@ -16,6 +16,12 @@
package org.springframework.cloud.contract.wiremock.file;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import com.github.tomakehurst.wiremock.common.BinaryFile;
import com.github.tomakehurst.wiremock.common.ClasspathFileSource;
import com.github.tomakehurst.wiremock.common.FileSource;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@@ -24,6 +30,8 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ResourcesFileSourceTest {
@@ -57,4 +65,25 @@ class ResourcesFileSourceTest {
assertThat(resourcesFileSource.getBinaryFileNamed("response-noticeList-success.json").getStream()).isNotEmpty();
}
@DisplayName("find a stub file in jar")
@Test
void find_stub_file_in_jar() throws URISyntaxException {
// given
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("jarfiles/module-banner-test-fixtures.jar").getFile());
String bannerStubPathInJar = "jar:file:" + file.getAbsolutePath() + "!/wiremock/banner/__files";
String bannerStubFilename = "response-bannerList-success.json";
ClasspathFileSource classpathFileSource1 = mock(ClasspathFileSource.class);
when(classpathFileSource1.getUri()).thenReturn(new URI(bannerStubPathInJar));
when(classpathFileSource1.exists()).thenReturn(true);
when(classpathFileSource1.getBinaryFileNamed(bannerStubFilename))
.thenReturn(new BinaryFile(new URI(bannerStubPathInJar + "/response-bannerList-success.json")));
ResourcesFileSource resourcesFileSource = new ResourcesFileSource(classpathFileSource1);
// when & then
assertThat(resourcesFileSource.getBinaryFileNamed(bannerStubFilename).getStream()).isNotEmpty();
}
}