Skip to content

Commit

Permalink
Merge pull request #1354 from Bertk/coverage
Browse files Browse the repository at this point in the history
add unit test WildcardPatternFileProviderTest
  • Loading branch information
guwirth authored Dec 17, 2017
2 parents d878c09 + 2518a6d commit eb620d0
Showing 1 changed file with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.sensors.tests.dotnet;
//origin https://github.com/SonarSource/sonar-dotnet-tests-library/

import com.google.common.base.Joiner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

public class WildcardPatternFileProviderTest {

@Rule
public TemporaryFolder tmp = new TemporaryFolder();

@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void init() throws Exception {
tmp.newFile("foo.txt");
tmp.newFile("bar.txt");

tmp.newFolder("a");
tmp.newFile(path("a", "foo.txt"));
tmp.newFolder("a", "a21");

tmp.newFolder("b");

tmp.newFolder("c");
tmp.newFolder("c", "c21");
tmp.newFile(path("c", "c21", "foo.txt"));
tmp.newFolder("c", "c22");
tmp.newFolder("c", "c22", "c31");
tmp.newFile(path("c", "c22", "c31", "foo.txt"));
tmp.newFile(path("c", "c22", "c31", "bar.txt"));
}

@Test
public void absolute_paths() {
assertThat(listFiles(new File(tmp.getRoot(), "foo.txt").getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")).getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(new File(tmp.getRoot(), "nonexisting.txt").getAbsolutePath())).isEmpty();
}

@Test
public void absolute_paths_with_current_and_parent_folder_access() {
assertThat(listFiles(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt")).getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt")));
}

@Test
public void absolute_paths_with_wildcards() {
assertThat(listFiles(new File(tmp.getRoot(), "*.txt").getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"), new File(tmp.getRoot(), "bar.txt"));

assertThat(listFiles(new File(tmp.getRoot(), "f*").getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles(new File(tmp.getRoot(), "fo?.txt").getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles(new File(tmp.getRoot(), "foo?.txt").getAbsolutePath())).isEmpty();

assertThat(listFiles(new File(tmp.getRoot(), path("**", "foo.txt")).getAbsolutePath()))
.containsOnly(
new File(tmp.getRoot(), "foo.txt"),
new File(tmp.getRoot(), path("a", "foo.txt")),
new File(tmp.getRoot(), path("c", "c21", "foo.txt")),
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(new File(tmp.getRoot(), path("**", "c31", "foo.txt")).getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(new File(tmp.getRoot(), path("**", "c?1", "foo.txt")).getAbsolutePath()))
.containsOnly(new File(tmp.getRoot(), path("c", "c21", "foo.txt")), new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(new File(tmp.getRoot(), path("?", "**", "foo.txt")).getAbsolutePath()))
.containsOnly(
new File(tmp.getRoot(), path("a", "foo.txt")),
new File(tmp.getRoot(), path("c", "c21", "foo.txt")),
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));
}

@Test
public void relative_paths() {
assertThat(listFiles("foo.txt", tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles(path("c", "c22", "c31", "foo.txt"), tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles("nonexisting.txt", tmp.getRoot())).isEmpty();
}

@Test
public void relative_paths_with_current_and_parent_folder_access() {
assertThat(listFiles(path("..", "foo.txt"), new File(tmp.getRoot(), "a")))
.containsOnly(new File(new File(tmp.getRoot(), "a"), path("..", "foo.txt")));

assertThat(listFiles(path(".", "foo.txt"), tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), path(".", "foo.txt")));

assertThat(listFiles(path("a", "..", "foo.txt"), tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), path("a", "..", "foo.txt")));
}

@Test
public void relative_paths_with_wildcards() {
assertThat(listFiles("*.txt", tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"), new File(tmp.getRoot(), "bar.txt"));

assertThat(listFiles("f*", tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles("fo?.txt", tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), "foo.txt"));

assertThat(listFiles("foo?.txt", tmp.getRoot())).isEmpty();

assertThat(listFiles(path("**", "foo.txt"), tmp.getRoot()))
.containsOnly(
new File(tmp.getRoot(), "foo.txt"),
new File(tmp.getRoot(), path("a", "foo.txt")),
new File(tmp.getRoot(), path("c", "c21", "foo.txt")),
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(path("**", "c31", "foo.txt"), tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(path("**", "c?1", "foo.txt"), tmp.getRoot()))
.containsOnly(new File(tmp.getRoot(), path("c", "c21", "foo.txt")), new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));

assertThat(listFiles(path("?", "**", "foo.txt"), tmp.getRoot()))
.containsOnly(
new File(tmp.getRoot(), path("a", "foo.txt")),
new File(tmp.getRoot(), path("c", "c21", "foo.txt")),
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")));
}

@Test
public void should_fail_with_current_folder_access_after_wildcard() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Cannot contain '.' or '..' after the first wildcard.");

listFiles(new File(tmp.getRoot(), path("?", ".", "foo.txt")).getAbsolutePath());
}

@Test
public void should_fail_with_parent_folder_access_after_wildcard() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Cannot contain '.' or '..' after the first wildcard.");

listFiles(path("*", "..", "foo.txt"), tmp.getRoot());
}

private static String path(String... elements) {
return Joiner.on(File.separator).join(elements);
}

private static Set<File> listFiles(String pattern) {
return listFiles(pattern, null);
}

private static Set<File> listFiles(String pattern, File baseDir) {
return new WildcardPatternFileProvider(baseDir, File.separator).listFiles(pattern);
}

}

0 comments on commit eb620d0

Please sign in to comment.