2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar 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.
11 * Sonar 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.findbugs;
22 import org.junit.Test;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import static org.fest.assertions.Assertions.assertThat;
29 public class FindbugsAntConverterTest {
32 public void convertToJavaRegexFormat() {
33 assertAntPatternEqualsToFindBugsRegExp("foo", "~foo", "foo");
34 assertAntPatternEqualsToFindBugsRegExp("**/*Test.java", "~(.*\\.)?[^\\\\^\\s]*Test", "Test");
35 assertAntPatternEqualsToFindBugsRegExp("**/*Test.java", "~(.*\\.)?[^\\\\^\\s]*Test", "foo.bar.Test");
39 public void shouldConvertAntToJavaRegexp() {
41 assertAntPatternEqualsToFindBugsRegExp("?", "~.", "g");
42 assertAntPatternEqualsToFindBugsRegExp("*/myClass.JaVa", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
43 assertAntPatternEqualsToFindBugsRegExp("*/myClass.java", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
44 assertAntPatternEqualsToFindBugsRegExp("*/myClass2.jav", "~([^\\\\^\\s]*\\.)?myClass2", "foo.bar.test.myClass2");
45 assertAntPatternEqualsToFindBugsRegExp("*/myOtherClass", "~([^\\\\^\\s]*\\.)?myOtherClass", "foo.bar.test.myOtherClass");
46 assertAntPatternEqualsToFindBugsRegExp("*", "~[^\\\\^\\s]*", "ga.%#123_(*");
47 assertAntPatternEqualsToFindBugsRegExp("**", "~.*", "gd.3reqg.3151];9#@!");
48 assertAntPatternEqualsToFindBugsRegExp("**/generated/**", "~(.*\\.)?generated\\..*", "!@$Rq/32T$).generated.##TR.e#@!$");
49 assertAntPatternEqualsToFindBugsRegExp("**/cl*nt/*", "~(.*\\.)?cl[^\\\\^\\s]*nt\\.[^\\\\^\\s]*", "!#$_.clr31r#!$(nt.!#$QRW)(.");
50 assertAntPatternEqualsToFindBugsRegExp("**/org/apache/commons/**", "~(.*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
51 assertAntPatternEqualsToFindBugsRegExp("*/org/apache/commons/**", "~([^\\\\^\\s]*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
52 assertAntPatternEqualsToFindBugsRegExp("org/apache/commons/**", "~org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
56 public void shouldntMatchThoseClassPattern() {
58 assertJavaRegexpResult("[^\\\\^\\s]", "fad f.ate 12#)", false);
61 private void assertAntPatternEqualsToFindBugsRegExp(String antPattern, String regExp, String example) {
62 assertThat(FindbugsAntConverter.antToJavaRegexpConvertor(antPattern)).isEqualTo(regExp);
63 String javaRegexp = regExp.substring(1, regExp.length());
64 assertJavaRegexpResult(javaRegexp, example, true);
67 private void assertJavaRegexpResult(String javaRegexp, String example, boolean expectedResult) {
68 Pattern pattern = Pattern.compile(javaRegexp);
69 Matcher matcher = pattern.matcher(example);
70 assertThat(matcher.matches()).as(example + " tested with pattern " + javaRegexp).isEqualTo(expectedResult);