|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.test; |
| 8 | + |
| 9 | +import org.hamcrest.CustomMatcher; |
| 10 | +import org.hamcrest.Description; |
| 11 | +import org.hamcrest.Matcher; |
| 12 | + |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.LinkOption; |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +public class FileMatchers { |
| 18 | + public static Matcher<Path> pathExists(LinkOption... options) { |
| 19 | + return new CustomMatcher<>("Path exists") { |
| 20 | + @Override |
| 21 | + public boolean matches(Object item) { |
| 22 | + if (item instanceof Path) { |
| 23 | + Path path = (Path) item; |
| 24 | + return Files.exists(path, options); |
| 25 | + } else { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + } |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + public static Matcher<Path> isDirectory(LinkOption... options) { |
| 34 | + return new FileTypeMatcher("directory", options) { |
| 35 | + @Override |
| 36 | + protected boolean matchPath(Path path) { |
| 37 | + return Files.isDirectory(path, options); |
| 38 | + } |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + public static Matcher<Path> isRegularFile(LinkOption... options) { |
| 43 | + return new FileTypeMatcher("regular file", options) { |
| 44 | + @Override |
| 45 | + protected boolean matchPath(Path path) { |
| 46 | + return Files.isRegularFile(path, options); |
| 47 | + } |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + private abstract static class FileTypeMatcher extends CustomMatcher<Path> { |
| 52 | + private final LinkOption[] options; |
| 53 | + |
| 54 | + FileTypeMatcher(String typeName, LinkOption... options) { |
| 55 | + super("Path is " + typeName); |
| 56 | + this.options = options; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean matches(Object item) { |
| 61 | + if (item instanceof Path) { |
| 62 | + Path path = (Path) item; |
| 63 | + return matchPath(path); |
| 64 | + } else { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected abstract boolean matchPath(Path path); |
| 70 | + |
| 71 | + @Override |
| 72 | + public void describeMismatch(Object item, Description description) { |
| 73 | + super.describeMismatch(item, description); |
| 74 | + if (item instanceof Path) { |
| 75 | + Path path = (Path) item; |
| 76 | + if (Files.exists(path, options) == false) { |
| 77 | + description.appendText(" (file not found)"); |
| 78 | + } else if (Files.isDirectory(path, options)) { |
| 79 | + description.appendText(" (directory)"); |
| 80 | + } else if (Files.isSymbolicLink(path)) { |
| 81 | + description.appendText(" (symlink)"); |
| 82 | + } else if (Files.isRegularFile(path, options)) { |
| 83 | + description.appendText(" (regular file)"); |
| 84 | + } else { |
| 85 | + description.appendText(" (unknown file type)"); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments