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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.junit.Before;
  22. import org.junit.Test;
  23. import java.io.File;
  24. import java.util.Collection;
  25. import static org.fest.assertions.Assertions.assertThat;
  26. public class FilePatternTest {
  27. File basedir, rootFile, subFile;
  28. @Before
  29. public void init() throws Exception {
  30. rootFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/root.txt").toURI());
  31. subFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt").toURI());
  32. basedir = rootFile.getParentFile();
  33. }
  34. @Test
  35. public void should_list_files_by_relative_path() throws Exception {
  36. assertThat(new FilePattern().listFiles(basedir, "subdir/*.txt")).containsOnly(subFile);
  37. assertThat(new FilePattern().listFiles(basedir, "*.txt")).containsOnly(rootFile);
  38. assertThat(new FilePattern().listFiles(basedir, "root.txt")).containsOnly(rootFile);
  39. assertThat(new FilePattern().listFiles(basedir, "ro*t.txt")).containsOnly(rootFile);
  40. assertThat(new FilePattern().listFiles(basedir, "ro?t.txt")).containsOnly(rootFile);
  41. assertThat(new FilePattern().listFiles(basedir, "r?t.txt")).isEmpty();
  42. assertThat(new FilePattern().listFiles(basedir, "*")).containsOnly(rootFile);
  43. assertThat(new FilePattern().listFiles(basedir, "**/*")).containsOnly(rootFile, subFile);
  44. assertThat(new FilePattern().listFiles(basedir, "**/*.txt")).containsOnly(subFile, rootFile);
  45. assertThat(new FilePattern().listFiles(basedir, "**/*.jar")).isEmpty();
  46. assertThat(new FilePattern().listFiles(basedir, "elsewhere/root.txt")).isEmpty();
  47. assertThat(new FilePattern().listFiles(basedir, "elsewhere/subfile.txt")).isEmpty();
  48. }
  49. @Test
  50. public void should_list_files_by_absolute_path() throws Exception {
  51. assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/*.txt"), rootFile);
  52. assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/**/subdir/*"), subFile);
  53. assertOnly(new FilePattern().listFiles(basedir, rootFile.getCanonicalPath()), rootFile);
  54. assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/*/subfile.txt"), subFile);
  55. assertThat(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/**/*.txt")).containsOnly(subFile, rootFile);
  56. assertThat(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/ElseWhere/**/*.txt")).isEmpty();
  57. assertThat(new FilePattern().listFiles(basedir, "/ElseWhere/**/*.txt")).isEmpty();
  58. }
  59. private void assertOnly(Collection<File> files, File file) throws Exception {
  60. assertThat(files).hasSize(1);
  61. assertThat(files.iterator().next().getCanonicalPath()).isEqualTo(file.getCanonicalPath());
  62. }
  63. }