|
| 1 | +/* |
| 2 | + * SonarQube Text Plugin |
| 3 | + * Copyright (C) 2021-2023 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 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 License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.plugins.common; |
| 21 | + |
| 22 | +import java.nio.file.Path; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.sonar.api.batch.fs.InputFile; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +class NotBinaryFilePredicateTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void all_values() { |
| 32 | + NotBinaryFilePredicate predicate = new NotBinaryFilePredicate(".foo", ".foo.bar", "_my_suffix", "", "oo."); |
| 33 | + assertThat(predicate.apply(inputFile("foo.foo"))).isFalse(); |
| 34 | + assertThat(predicate.apply(inputFile("foo.exe"))).isFalse(); |
| 35 | + assertThat(predicate.apply(inputFile("foo.EXE"))).isFalse(); |
| 36 | + assertThat(predicate.apply(inputFile("foo.UNKNOWN"))).isTrue(); |
| 37 | + assertThat(predicate.apply(inputFile("foo.txt"))).isTrue(); |
| 38 | + assertThat(predicate.apply(inputFile("boom.foo.bar"))).isFalse(); |
| 39 | + assertThat(predicate.apply(inputFile("boom.foo.other"))).isTrue(); |
| 40 | + assertThat(predicate.apply(inputFile("boom_suffix"))).isTrue(); |
| 41 | + assertThat(predicate.apply(inputFile("boom_my_suffix"))).isFalse(); |
| 42 | + assertThat(predicate.apply(inputFile("_"))).isTrue(); |
| 43 | + assertThat(predicate.apply(inputFile("foo."))).isFalse(); |
| 44 | + assertThat(predicate.apply(inputFile("bar."))).isTrue(); |
| 45 | + assertThat(predicate.apply(inputFile(""))).isTrue(); |
| 46 | + assertThat(predicate.apply(inputFile("cacerts"))).isFalse(); |
| 47 | + assertThat(predicate.apply(inputFile("ec54a0d82c5938c"))).isTrue(); |
| 48 | + assertThat(predicate.apply(inputFile("EC54A0D82C5938C"))).isTrue(); |
| 49 | + assertThat(predicate.apply(inputFile("9f17e505386ec54a0d82c5938ca15805"))).isFalse(); |
| 50 | + assertThat(predicate.apply(inputFile("9F17E505386EC54A0D82C5938CA15805"))).isFalse(); |
| 51 | + assertThat(predicate.apply(inputFile("abac0000111122222233333444455555"))).isTrue(); |
| 52 | + assertThat(predicate.apply(inputFile("Boum123Boum456Boum789Boum123Boum"))).isTrue(); |
| 53 | + assertThat(predicate.apply(inputFile("9f17e505386ec54a0d82c5938ca15805.txt"))).isTrue(); |
| 54 | + assertThat(predicate.apply(inputFile("71c686d48275f9b552946aedf50d67e218064cbd"))).isFalse(); |
| 55 | + assertThat(predicate.apply(inputFile("9dc59c1853a762fb0fe420b0e18b7861b0045f291f8dd75ab9e9d578a31e729a"))).isFalse(); |
| 56 | + assertThat(predicate.apply(inputFile("faccb33a9627c7269e7ce4e4a37beb2809410a90612977b61944b1680411f39eb2b773adf539d320c3b48c1a0ddfc9b01fc0673a1d893cdb30878b7432f0ebc6"))) |
| 57 | + .isFalse(); |
| 58 | + predicate.addBinaryFileExtension("txt"); |
| 59 | + assertThat(predicate.apply(inputFile("foo.txt"))).isFalse(); |
| 60 | + } |
| 61 | + |
| 62 | + private static InputFile inputFile(String filename) { |
| 63 | + return TestUtils.inputFile(Path.of(filename), "// empty content"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void extensions() { |
| 68 | + assertThat(NotBinaryFilePredicate.extension("")).isNull(); |
| 69 | + assertThat(NotBinaryFilePredicate.extension("foo.txt")).isEqualTo("txt"); |
| 70 | + assertThat(NotBinaryFilePredicate.extension("foo")).isNull(); |
| 71 | + assertThat(NotBinaryFilePredicate.extension(".txt")).isEqualTo("txt"); |
| 72 | + assertThat(NotBinaryFilePredicate.extension("foo.bar.c")).isEqualTo("c"); |
| 73 | + assertThat(NotBinaryFilePredicate.extension("...c")).isEqualTo("c"); |
| 74 | + assertThat(NotBinaryFilePredicate.extension("foo.")).isNull(); |
| 75 | + assertThat(NotBinaryFilePredicate.extension("foo.bar.")).isNull(); |
| 76 | + ; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments