Removing old files from output mappings folder; Related to #355
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.contract.stubrunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
@@ -77,14 +78,12 @@ public class StubRunner implements StubRunning {
|
||||
this.stubsConfiguration.artifactId + "_"
|
||||
+ stubs.getPort(this.stubsConfiguration.toColonSeparatedDependencyNotation()));
|
||||
try {
|
||||
if (outputMappings.exists()) {
|
||||
outputMappings.delete();
|
||||
}
|
||||
outputMappings.getParentFile().mkdirs();
|
||||
clearOldFiles(outputMappings.getParentFile(), this.stubsConfiguration.artifactId);
|
||||
outputMappings.createNewFile();
|
||||
Files.write(Paths.get(outputMappings.toURI()), registeredMappings.getBytes());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Stored the mappings for artifactid [" + this.stubsConfiguration.artifactId + " at [" + outputMappings + "] location");
|
||||
log.debug("Stored the mappings for artifactid [" + this.stubsConfiguration.artifactId + "] at [" + outputMappings + "] location");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
@@ -96,6 +95,27 @@ public class StubRunner implements StubRunning {
|
||||
return stubs;
|
||||
}
|
||||
|
||||
private void clearOldFiles(File outputFolder, final String filename) {
|
||||
File[] files = outputFolder.listFiles(new FilenameFilter() {
|
||||
@Override public boolean accept(final File dir, final String name) {
|
||||
return name.startsWith(filename);
|
||||
}
|
||||
});
|
||||
if (files == null) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.debug("Failed to retrieve any mappings");
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (final File file : files) {
|
||||
if (!file.delete()) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.debug("Exception occurred while trying to remove [" + file.getAbsolutePath() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL findStubUrl(String groupId, String artifactId) {
|
||||
return this.localStubRunner.findStubUrl(groupId, artifactId);
|
||||
|
||||
@@ -39,7 +39,7 @@ class StubRunnerRuleSpec extends Specification {
|
||||
.repoRoot(StubRunnerRuleSpec.getResource("/m2repo/repository").toURI().toString())
|
||||
.downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance")
|
||||
.downloadStub("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")
|
||||
.withMappingsOutputFolder("target/outputmappingsforule")
|
||||
.withMappingsOutputFolder("target/outputmappingsforrule")
|
||||
|
||||
|
||||
def 'should start WireMock servers'() {
|
||||
@@ -61,7 +61,7 @@ class StubRunnerRuleSpec extends Specification {
|
||||
when:
|
||||
def url = rule.findStubUrl('fraudDetectionServer')
|
||||
then:
|
||||
new File("target/outputmappingsforule", "fraudDetectionServer_${url.port}").exists()
|
||||
new File("target/outputmappingsforrule", "fraudDetectionServer_${url.port}").exists()
|
||||
}
|
||||
// end::classrule[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user