Sync docs from master to gh-pages

This commit is contained in:
buildmaster
2016-12-13 08:45:08 +00:00
parent 9c62dd731a
commit 67c020fe8e

View File

@@ -797,6 +797,7 @@ $(addBlockSwitches);
<li><a href="#_custom_test_generator">Custom test generator</a></li>
<li><a href="#_custom_stub_generator">Custom stub generator</a></li>
<li><a href="#_custom_stub_runner">Custom Stub Runner</a></li>
<li><a href="#_custom_stub_downloader">Custom Stub Downloader</a></li>
</ul>
</li>
<li><a href="#_links">Links</a></li>
@@ -7684,8 +7685,7 @@ class MocoHttpServerStub implements HttpServerStub {
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>org.springframework.cloud.contract.stubrunner.HttpServerStub=\
org.springframework.cloud.contract.stubrunner.provider.moco.MocoHttpServerStub</code></pre>
<pre class="highlight"><code># Example of a custom HTTP Server Stub</code></pre>
</div>
</div>
<div class="paragraph">
@@ -7705,6 +7705,142 @@ will be picked. If you provide more than one then the first one on the list will
</table>
</div>
</div>
<div class="sect3">
<h4 id="_custom_stub_downloader">Custom Stub Downloader</h4>
<div class="paragraph">
<p>You can customize the way your stubs are downloaded. If you don&#8217;t want to download the JARs
from Nexus / Artifactory in the way we do by default you can set your own implementation.
Below you can find an example of a Stub Downloader Provider that takes <code>json</code> files from the test resources
from classpath, copies them to a temp file and then passes that temporary folder
as a root for the stubs.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java" data-lang="java">/*
* Copyright 2013-2016 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
*
* 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 org.springframework.cloud.contract.stubrunner.provider.moco
import org.springframework.cloud.contract.stubrunner.StubConfiguration
import org.springframework.cloud.contract.stubrunner.StubDownloader
import org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder
import org.springframework.cloud.contract.stubrunner.StubRunnerOptions
import org.springframework.core.io.DefaultResourceLoader
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import java.nio.file.Files
/**
* Poor man's version of taking stubs from classpath. It needs much more
* love and attention to go to the main sources.
*
* @author Marcin Grzejszczak
*/
class ClasspathStubProvider implements StubDownloaderBuilder {
private static final int TEMP_DIR_ATTEMPTS = 10000
@Override
public StubDownloader build(StubRunnerOptions stubRunnerOptions) {
final StubConfiguration configuration = stubRunnerOptions.getDependencies().first()
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
new DefaultResourceLoader())
try {
String rootFolder = repoRoot(stubRunnerOptions) ?: "**/" + separatedArtifact(configuration) + "/**/*.json"
Resource[] resources = resolver.getResources(rootFolder)
final File tmp = createTempDir()
tmp.deleteOnExit()
// you'd have to write an impl to maintain the folder structure
// this is just for demo
resources.each { Resource resource -&gt;
Files.copy(resource.getInputStream(), new File(tmp, resource.getFile().getName()).toPath())
}
return new StubDownloader() {
@Override
public Map.Entry&lt;StubConfiguration, File&gt; downloadAndUnpackStubJar(
StubConfiguration stubConfiguration) {
return new AbstractMap.SimpleEntry(configuration, tmp)
}
}
} catch (IOException e) {
throw new IllegalStateException(e)
}
}
private String repoRoot(StubRunnerOptions stubRunnerOptions) {
switch (stubRunnerOptions.stubRepositoryRoot) {
case { !it }:
return ""
case { String root -&gt; root.endsWith("**/*.json") }:
return stubRunnerOptions.stubRepositoryRoot
default:
return stubRunnerOptions.stubRepositoryRoot + "/**/*.json"
}
}
private String separatedArtifact(StubConfiguration configuration) {
return configuration.getGroupId().replace(".", File.separator) +
File.separator + configuration.getArtifactId()
}
// Taken from Guava
private File createTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"))
String baseName = System.currentTimeMillis() + "-"
for (int counter = 0; counter &lt; TEMP_DIR_ATTEMPTS; counter++) {
File tempDir = new File(baseDir, baseName + counter)
if (tempDir.mkdir()) {
return tempDir
}
}
throw new IllegalStateException(
"Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (
TEMP_DIR_ATTEMPTS - 1) + ")")
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>and just register it in your <code>spring.factories</code> file</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code># Example of a custom Stub Downloader Provider
org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder=\
org.springframework.cloud.contract.stubrunner.provider.moco.ClasspathStubProvider</code></pre>
</div>
</div>
<div class="paragraph">
<p>that way you&#8217;ll be able to pick a folder with the source of your stubs.</p>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<div class="title">Important</div>
</td>
<td class="content">
If you don&#8217;t provide any implementation then the default one - Aether based that will download stubs from a remote repo
will be picked. If you provide more than one then the first one on the list will be picked.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_links">Links</h3>