com.github.tomakehurst.wiremock.common.ClasspathFileSource Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.common.ClasspathFileSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourcesFileSource.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private static FileSource[] toSources(Resource[] resources) {
	FileSource[] sources = new FileSource[resources.length];
	for (int i = 0; i < resources.length; i++) {
		Resource resource = resources[i];
		if (resource instanceof ClassPathResource) {
			ClassPathResource classes = (ClassPathResource) resource;
			sources[i] = new ClasspathFileSource(classes.getPath());
		}
		else if (resource instanceof FileSystemResource) {
			FileSystemResource files = (FileSystemResource) resource;
			sources[i] = new SingleRootFileSource(files.getFile());
		}
		else if (resource instanceof UrlResource) {
			sources[i] = fileOrFallbackToClasspath(resource);
		}
		else {
			throw new IllegalArgumentException(
					"Unsupported resource type for file source: "
							+ resource.getClass());
		}
	}
	return sources;
}
 
Example #2
Source File: WireMockAutoConfiguration.java    From restdocs-wiremock with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public WireMockConfiguration wireMockConfiguration(WireMockProperties properties) {
	WireMockConfiguration config = WireMockConfiguration.options();
	if(properties.getPort() > 0) {
		log.info("Starting WireMock on port " + properties.getPort());
		config.port(properties.getPort());
	} else {
		log.info("Starting WireMock on dynamic port");
		config.dynamicPort();
	}
	if(properties.getStubPath() != null) {
		config.fileSource(new ClasspathFileSource(properties.getStubPath()));
	}
	config.extensions(new ResponseTemplateTransformer(false));
	return config;
}
 
Example #3
Source File: WireMockListener.java    From restdocs-wiremock with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
	if(wireMockAnnotation == null) {
		return;
	}
	WireMockTest methodAnnotation = testContext.getTestMethod().getAnnotation(WireMockTest.class);
	
	String stubPath = "";
	if(this.wireMockAnnotation.stubPath() != null) {
		stubPath = this.wireMockAnnotation.stubPath();
	}
	if (methodAnnotation != null && methodAnnotation.stubPath() != null) {
		stubPath += "/" + methodAnnotation.stubPath();
	}

	ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) testContext
			.getApplicationContext();

	WireMockServer server = applicationContext.getBean(WireMockServer.class);
	server.resetMappings();
	if(! stubPath.isEmpty()) {
		server.loadMappingsUsing(new JsonFileMappingsSource(new ClasspathFileSource(stubPath)));
	}
}
 
Example #4
Source File: ResourcesFileSource.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private static FileSource fileOrFallbackToClasspath(Resource resource) {
	UrlResource file = (UrlResource) resource;
	try {
		URI uri = file.getURI();
		if (compressedResource(uri)) {
			return new ClasspathFileSource(pathFromCompressed(uri));
		}
		return new SingleRootFileSource(getFile(file));
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}