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 static org.junit.Assert.assertThat;
24 import org.hamcrest.core.Is;
25 import org.junit.Test;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 public class FindbugsAntConverterTest {
33 public void convertToJavaRegexFormat() {
34 assertAntPatternEqualsToFindBugsRegExp("foo", "~foo", "foo");
35 assertAntPatternEqualsToFindBugsRegExp("**/*Test.java", "~(.*\\.)?[^\\\\^\\s]*Test", "Test");
36 assertAntPatternEqualsToFindBugsRegExp("**/*Test.java", "~(.*\\.)?[^\\\\^\\s]*Test", "foo.bar.Test");
40 public void shouldConvertAntToJavaRegexp() {
42 assertAntPatternEqualsToFindBugsRegExp("?", "~.", "g");
43 assertAntPatternEqualsToFindBugsRegExp("*/myClass.JaVa", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
44 assertAntPatternEqualsToFindBugsRegExp("*/myClass.java", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
45 assertAntPatternEqualsToFindBugsRegExp("*/myClass2.jav", "~([^\\\\^\\s]*\\.)?myClass2", "foo.bar.test.myClass2");
46 assertAntPatternEqualsToFindBugsRegExp("*/myOtherClass", "~([^\\\\^\\s]*\\.)?myOtherClass", "foo.bar.test.myOtherClass");
47 assertAntPatternEqualsToFindBugsRegExp("*", "~[^\\\\^\\s]*", "ga.%#123_(*");
48 assertAntPatternEqualsToFindBugsRegExp("**", "~.*", "gd.3reqg.3151];9#@!");
49 assertAntPatternEqualsToFindBugsRegExp("**/generated/**", "~(.*\\.)?generated\\..*", "!@$Rq/32T$).generated.##TR.e#@!$");
50 assertAntPatternEqualsToFindBugsRegExp("**/cl*nt/*", "~(.*\\.)?cl[^\\\\^\\s]*nt\\.[^\\\\^\\s]*", "!#$_.clr31r#!$(nt.!#$QRW)(.");
51 assertAntPatternEqualsToFindBugsRegExp("**/org/apache/commons/**", "~(.*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
52 assertAntPatternEqualsToFindBugsRegExp("*/org/apache/commons/**", "~([^\\\\^\\s]*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
53 assertAntPatternEqualsToFindBugsRegExp("org/apache/commons/**", "~org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
57 public void shouldntMatchThoseClassPattern() {
59 assertJavaRegexpResult("[^\\\\^\\s]", "fad f.ate 12#)", false);
62 private void assertAntPatternEqualsToFindBugsRegExp(String antPattern, String regExp, String example) {
63 assertThat(FindbugsAntConverter.antToJavaRegexpConvertor(antPattern), Is.is(regExp));
64 String javaRegexp = regExp.substring(1, regExp.length());
65 assertJavaRegexpResult(javaRegexp, example, true);
68 private void assertJavaRegexpResult(String javaRegexp, String example, boolean expectedResult) {
69 Pattern pattern = Pattern.compile(javaRegexp);
70 Matcher matcher = pattern.matcher(example);
71 assertThat(example + " tested with pattern " + javaRegexp, matcher.matches(), Is.is(expectedResult));