You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilePatternTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Sonar Runner - Batch
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.batch;
  21. import org.apache.commons.io.FilenameUtils;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.Collection;
  28. import static org.fest.assertions.Assertions.assertThat;
  29. public class FilePatternTest {
  30. File basedir, rootFile, subFile;
  31. String basePath;
  32. @Before
  33. public void init() throws Exception {
  34. rootFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/root.txt").toURI());
  35. subFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt").toURI());
  36. basedir = rootFile.getParentFile();
  37. basePath = path(basedir);
  38. }
  39. @Test
  40. public void should_list_files_by_relative_path() throws Exception {
  41. assertThat(new FilePattern().listFiles(basedir, "subdir/*.txt")).containsOnly(subFile);
  42. assertThat(new FilePattern().listFiles(basedir, "*.txt")).containsOnly(rootFile);
  43. assertThat(new FilePattern().listFiles(basedir, "root.txt")).containsOnly(rootFile);
  44. assertThat(new FilePattern().listFiles(basedir, "ro*t.txt")).containsOnly(rootFile);
  45. assertThat(new FilePattern().listFiles(basedir, "ro?t.txt")).containsOnly(rootFile);
  46. assertThat(new FilePattern().listFiles(basedir, "r?t.txt")).isEmpty();
  47. assertThat(new FilePattern().listFiles(basedir, "*")).containsOnly(rootFile);
  48. assertThat(new FilePattern().listFiles(basedir, "**/*")).containsOnly(rootFile, subFile);
  49. assertThat(new FilePattern().listFiles(basedir, "**/*.txt")).containsOnly(subFile, rootFile);
  50. assertThat(new FilePattern().listFiles(basedir, "**/*.jar")).isEmpty();
  51. assertThat(new FilePattern().listFiles(basedir, "elsewhere/root.txt")).isEmpty();
  52. assertThat(new FilePattern().listFiles(basedir, "elsewhere/subfile.txt")).isEmpty();
  53. }
  54. @Test
  55. public void should_list_files_by_absolute_path() throws Exception {
  56. assertOnly(new FilePattern().listFiles(basedir, basePath + "/*.txt"), rootFile);
  57. assertOnly(new FilePattern().listFiles(basedir, basePath + "/**/subdir/*"), subFile);
  58. assertOnly(new FilePattern().listFiles(basedir, path(rootFile)), rootFile);
  59. assertOnly(new FilePattern().listFiles(basedir, path(basedir) + "/*/subfile.txt"), subFile);
  60. assertThat(new FilePattern().listFiles(basedir, path(basedir) + "/**/*.txt")).containsOnly(subFile, rootFile);
  61. assertThat(new FilePattern().listFiles(basedir, path(basedir) + "/ElseWhere/**/*.txt")).isEmpty();
  62. assertThat(new FilePattern().listFiles(basedir, "/ElseWhere/**/*.txt")).isEmpty();
  63. }
  64. private void assertOnly(Collection<File> files, File file) throws Exception {
  65. assertThat(files).hasSize(1);
  66. assertThat(files.iterator().next().getCanonicalPath()).isEqualTo(file.getCanonicalPath());
  67. }
  68. private String path(File f) throws IOException {
  69. return FilenameUtils.separatorsToUnix(f.getCanonicalPath());
  70. }
  71. }