diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-08-30 22:48:17 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-08-30 22:48:17 +0200 |
commit | 8a1c7bb53923ba9fa01c3c6ceba2046be0530d11 (patch) | |
tree | 24830adc8d42e2d9735eefeffa23ae3eb113f62a /config-model/src/main/java/com/yahoo/config | |
parent | 6250e2b6c78e5ee27690b5071f68ae510e8c113f (diff) |
Revert "Merge pull request #6742 from vespa-engine/revert-6732-bratseth/generate-rank-profiles-for-all-models-part-10-2"
This reverts commit a294ef166c59c795f9e6fd31fbd6914c502d559a, reversing
changes made to cef4c0f9d7c084f320e77abb2a93522acd7f3f53.
Diffstat (limited to 'config-model/src/main/java/com/yahoo/config')
-rw-r--r-- | config-model/src/main/java/com/yahoo/config/model/test/MockApplicationPackage.java | 126 |
1 files changed, 125 insertions, 1 deletions
diff --git a/config-model/src/main/java/com/yahoo/config/model/test/MockApplicationPackage.java b/config-model/src/main/java/com/yahoo/config/model/test/MockApplicationPackage.java index 7404ae14a5d..757dab4cbf3 100644 --- a/config-model/src/main/java/com/yahoo/config/model/test/MockApplicationPackage.java +++ b/config-model/src/main/java/com/yahoo/config/model/test/MockApplicationPackage.java @@ -16,11 +16,17 @@ import com.yahoo.searchdefinition.parser.ParseException; import com.yahoo.vespa.config.ConfigDefinitionKey; import com.yahoo.config.application.api.ApplicationPackage; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.Reader; import java.io.StringReader; +import java.io.UncheckedIOException; import java.util.*; +import java.util.stream.Collectors; /** * For testing purposes only @@ -113,7 +119,7 @@ public class MockApplicationPackage implements ApplicationPackage { @Override public ApplicationFile getFile(Path file) { - throw new UnsupportedOperationException(); + return new MockApplicationFile(file, Path.fromString(root.toString())); } @Override @@ -300,4 +306,122 @@ public class MockApplicationPackage implements ApplicationPackage { return xmlStringWithIdAttribute.substring(idStart + 4, idEnd - 1); } + public static class MockApplicationFile extends ApplicationFile { + + /** The path to the application package root */ + private final Path root; + + /** The File pointing to the actual file represented by this */ + private final File file; + + public MockApplicationFile(Path filePath, Path applicationPackagePath) { + super(filePath); + this.root = applicationPackagePath; + file = applicationPackagePath.append(filePath).toFile(); + } + + @Override + public boolean isDirectory() { + return file.isDirectory(); + } + + @Override + public boolean exists() { + return file.exists(); + } + + @Override + public Reader createReader() throws FileNotFoundException { + try { + if ( ! exists()) throw new FileNotFoundException("File '" + file + "' does not exist"); + return IOUtils.createReader(file, "UTF-8"); + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public InputStream createInputStream() throws FileNotFoundException { + try { + if ( ! exists()) throw new FileNotFoundException("File '" + file + "' does not exist"); + return new BufferedInputStream(new FileInputStream(file)); + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public ApplicationFile createDirectory() { + file.mkdirs(); + return this; + } + + @Override + public ApplicationFile writeFile(Reader input) { + try { + IOUtils.writeFile(file, IOUtils.readAll(input), false); + return this; + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public ApplicationFile appendFile(String value) { + try { + IOUtils.writeFile(file, value, true); + return this; + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public List<ApplicationFile> listFiles(PathFilter filter) { + if ( ! isDirectory()) return Collections.emptyList(); + return Arrays.stream(file.listFiles()).filter(f -> filter.accept(Path.fromString(f.toString()))) + .map(f -> new MockApplicationFile(asApplicationRelativePath(f), root)) + .collect(Collectors.toList()); + } + + @Override + public ApplicationFile delete() { + file.delete(); + return this; + } + + @Override + public MetaData getMetaData() { + throw new UnsupportedOperationException(); + } + + @Override + public int compareTo(ApplicationFile other) { + return this.getPath().getName().compareTo((other).getPath().getName()); + } + + /** Strips the application package root path prefix from the path of the given file */ + private Path asApplicationRelativePath(File file) { + Path path = Path.fromString(file.toString()); + + Iterator<String> pathIterator = path.iterator(); + // Skip the path elements this shares with the root + for (Iterator<String> rootIterator = root.iterator(); rootIterator.hasNext(); ) { + String rootElement = rootIterator.next(); + String pathElement = pathIterator.next(); + if ( ! rootElement.equals(pathElement)) throw new RuntimeException("Assumption broken"); + } + // Build a path from the remaining + Path relative = Path.fromString(""); + while (pathIterator.hasNext()) + relative = relative.append(pathIterator.next()); + return relative; + } + + } + } |