/** * Returns the file pattern. * @return the file pattern */ public FilePattern asFilePattern() { if (segments.isEmpty()) { throw new IllegalStateException(); } else { return FilePattern.compile(getPathString()); } }
/** * Extracts a base path from the given path. * @param path the given path * @return the base path of the given path */ public static BasePath headOf(String path) { if (isEmpty(path)) { return BasePath.EMPTY; } FilePattern pattern = FilePattern.compile(path); return headOf(pattern); }
/** * An invalid character. */ @Test(expected = IllegalArgumentException.class) public void invalid_character() { FilePattern.compile("\\data.csv"); }
/** * An unclosed variable. */ @Test(expected = IllegalArgumentException.class) public void variable_unclosed() { FilePattern.compile("${csv"); }
/** * An invalid wildcard. */ @Test(expected = IllegalArgumentException.class) public void consecutive_wildcard() { FilePattern.compile("**.csv"); }
/** * An invalid character in selection. */ @Test(expected = IllegalArgumentException.class) public void selection_invalid_character() { FilePattern.compile("{*}"); }
/** * A doller. */ @Test(expected = IllegalArgumentException.class) public void doller() { FilePattern.compile("$.csv"); }
/** * An unclosed selection. */ @Test(expected = IllegalArgumentException.class) public void selection_unclosed() { FilePattern.compile("{data"); }
private List<String> collect() throws IOException { List<FileStatus> all = HadoopDataSourceUtil.search(getTempFileSystem(), getBase(), FilePattern.compile("**")); List<FileStatus> files = new ArrayList<>(); for (FileStatus stat : all) { if (stat.isDirectory() == false) { files.add(stat); } } return normalize(files); }
/** * A traverse pattern. */ @Test public void traverse() { FilePattern compiled = FilePattern.compile("**"); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(true)); assertThat(segments.get(0).getElements(), is(kind())); }
/** * A contains wildcard pattern. */ @Test public void containsWildcard() { FilePattern compiled = FilePattern.compile("data-*.csv"); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(TOKEN, WILDCARD, TOKEN))); assertThat(segments.get(0).getElements(), is(token("data-", "*", ".csv"))); }
/** * An empty variable pattern. */ @Test public void variable_empty() { FilePattern compiled = FilePattern.compile("${}"); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(VARIABLE))); assertThat(segments.get(0).getElements(), is(token("${}"))); }
/** * A variable pattern. */ @Test public void variable() { FilePattern compiled = FilePattern.compile("${v}"); assertThat(compiled.containsVariables(), is(true)); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(VARIABLE))); assertThat(segments.get(0).getElements(), is(token("${v}"))); }
/** * A token. */ @Test public void token() { FilePattern compiled = FilePattern.compile("a"); assertThat(compiled.containsVariables(), is(false)); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(TOKEN))); assertThat(segments.get(0).getElements(), is(token("a"))); }
/** * A selection pattern. */ @Test public void selection_empty() { FilePattern compiled = FilePattern.compile("{}"); assertThat(compiled.containsVariables(), is(false)); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(SELECTION))); assertThat(segments.get(0).getElements(), is(token("{}"))); }
/** * A selection pattern. */ @Test public void selection_containsEmpty() { FilePattern compiled = FilePattern.compile("{alpha||gamma}"); assertThat(compiled.containsVariables(), is(false)); List<Segment> segments = compiled.getSegments(); assertThat(segments.size(), is(1)); assertThat(segments.get(0).isTraverse(), is(false)); assertThat(segments.get(0).getElements(), is(kind(SELECTION))); assertThat(segments.get(0).getElements(), is(token("{alpha||gamma}"))); }
/** * single file does not cover anything. * @throws Exception if failed */ @Test public void minimalCovered_trivial() throws Exception { touch("a.csv"); FileSystem fs = getTempFileSystem(); List<FileStatus> raw = HadoopDataSourceUtil.search(fs, getBase(), FilePattern.compile("**/*.csv")); assertThat(raw.size(), is(1)); List<FileStatus> results = HadoopDataSourceUtil.onlyMinimalCovered(raw); assertThat(normalize(results), is(path("a.csv"))); }
/** * search by token. * @throws Exception if failed */ @Test public void search_direct() throws Exception { touch("a.csv"); FileSystem fs = getTempFileSystem(); List<FileStatus> results = HadoopDataSourceUtil.search(fs, getBase(), FilePattern.compile("a.csv")); assertThat(normalize(results), is(path("a.csv"))); }
/** * search by wildcard for directories. * @throws Exception if failed */ @Test public void search_wildcard_dir() throws Exception { touch("a/a.csv"); touch("b/b/b.csv"); touch("c/c.csv"); FileSystem fs = getTempFileSystem(); List<FileStatus> results = HadoopDataSourceUtil.search(fs, getBase(), FilePattern.compile("*/*.csv")); assertThat(normalize(results), is(path("a/a.csv", "c/c.csv"))); }
/** * search by traverse. * @throws Exception if failed */ @Test public void search_traverse() throws Exception { touch("a/a.csv"); touch("b/b.csv"); touch("c/c.csv"); FileSystem fs = getTempFileSystem(); List<FileStatus> results = HadoopDataSourceUtil.search(fs, getBase(), FilePattern.compile("**")); assertThat(normalize(results), is(path("", "a", "b", "c", "a/a.csv", "b/b.csv", "c/c.csv"))); }