diff options
author | Godin <mandrikov@gmail.com> | 2010-11-03 16:34:10 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-11-03 16:34:10 +0000 |
commit | 72864ce12eeb02dc9d199ee0edb4d62a4bb33240 (patch) | |
tree | 0c2471e8ff624193c9100a3b4339c5df146e6e2e /plugins | |
parent | db1aa08abd0210d57d9f412eb1532f98d03cc08a (diff) | |
download | sonarqube-72864ce12eeb02dc9d199ee0edb4d62a4bb33240.tar.gz sonarqube-72864ce12eeb02dc9d199ee0edb4d62a4bb33240.zip |
Improve routines for patterns, add unit tests
Diffstat (limited to 'plugins')
4 files changed, 39 insertions, 41 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/PatternUtils.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/PatternUtils.java index 9f2d927677f..54ec9ed07bd 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/PatternUtils.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/PatternUtils.java @@ -3,34 +3,13 @@ package org.sonar.java; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.WildcardPattern; -import com.google.common.collect.Lists; - -import java.util.List; - public final class PatternUtils { private PatternUtils() { } - public static WildcardPattern[] createMatchers(String pattern) { - List<WildcardPattern> matchers = Lists.newArrayList(); - if (StringUtils.isNotEmpty(pattern)) { - String[] patterns = pattern.split(","); - for (String p : patterns) { - p = StringUtils.replace(p, ".", "/"); - matchers.add(WildcardPattern.create(p)); - } - } - return matchers.toArray(new WildcardPattern[matchers.size()]); - } - - public static boolean matches(String text, WildcardPattern[] matchers) { - for (WildcardPattern matcher : matchers) { - if (matcher.match(text)) { - return true; - } - } - return false; + public static WildcardPattern[] createPatterns(String patterns) { + return WildcardPattern.create(StringUtils.split(StringUtils.replace(patterns, ".", "/"), ',')); } } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/UndocumentedApiCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/UndocumentedApiCheck.java index 2661a204dd2..0440e6f1123 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/UndocumentedApiCheck.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/UndocumentedApiCheck.java @@ -27,7 +27,7 @@ public class UndocumentedApiCheck extends JavaAstCheck { @RuleProperty(description = "Optional. If this property is not defined, all classes should adhere to this constraint. Ex : **.api.**") private String forClasses = new String(); - private WildcardPattern[] matchers; + private WildcardPattern[] patterns; @Override public List<Integer> getWantedTokens() { @@ -38,7 +38,7 @@ public class UndocumentedApiCheck extends JavaAstCheck { public void visitToken(DetailAST ast) { SourceCode currentResource = peekSourceCode(); SourceClass sourceClass = peekParentClass(); - if (PatternUtils.matches(sourceClass.getKey(), getMatchers())) { + if (WildcardPattern.match(getPatterns(), sourceClass.getKey())) { if (PublicApiVisitor.isPublicApi(ast) && !PublicApiVisitor.isDocumentedApi(ast, getFileContents())) { SourceFile sourceFile = currentResource.getParent(SourceFile.class); CheckMessage message = new CheckMessage(this, "Avoid undocumented API"); @@ -48,11 +48,11 @@ public class UndocumentedApiCheck extends JavaAstCheck { } } - private WildcardPattern[] getMatchers() { - if (matchers == null) { - matchers = PatternUtils.createMatchers(StringUtils.defaultIfEmpty(forClasses, "**")); + private WildcardPattern[] getPatterns() { + if (patterns == null) { + patterns = PatternUtils.createPatterns(StringUtils.defaultIfEmpty(forClasses, "**")); } - return matchers; + return patterns; } public String getForClasses() { diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/ArchitectureCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/ArchitectureCheck.java index c4a165d07ed..58b8e60bf09 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/ArchitectureCheck.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/ArchitectureCheck.java @@ -48,8 +48,8 @@ public class ArchitectureCheck extends BytecodeCheck { @RuleProperty(description = "Mandatory. Ex : java.util.Vector, java.util.Hashtable, java.util.Enumeration") private String toClasses = new String(); - private WildcardPattern[] fromMatchers; - private WildcardPattern[] toMatchers; + private WildcardPattern[] fromPatterns; + private WildcardPattern[] toPatterns; private AsmClass asmClass; private Map<String, CheckMessage> internalNames; @@ -72,7 +72,7 @@ public class ArchitectureCheck extends BytecodeCheck { @Override public void visitClass(AsmClass asmClass) { String nameAsmClass = asmClass.getInternalName(); - if (PatternUtils.matches(nameAsmClass, getFromMatchers())) { + if (WildcardPattern.match(getFromPatterns(), nameAsmClass)) { this.asmClass = asmClass; this.internalNames = Maps.newHashMap(); } else { @@ -95,7 +95,7 @@ public class ArchitectureCheck extends BytecodeCheck { if (asmClass != null && edge != null) { String internalNameTargetClass = edge.getTargetAsmClass().getInternalName(); if ( !internalNames.containsKey(internalNameTargetClass)) { - if (PatternUtils.matches(internalNameTargetClass, getToMatchers())) { + if (WildcardPattern.match(getToPatterns(), internalNameTargetClass)) { int sourceLineNumber = getSourceLineNumber(edge); logMessage(asmClass.getInternalName(), internalNameTargetClass, sourceLineNumber); } @@ -127,18 +127,18 @@ public class ArchitectureCheck extends BytecodeCheck { internalNames.put(toClass, message); } - private WildcardPattern[] getFromMatchers() { - if (fromMatchers == null) { - fromMatchers = PatternUtils.createMatchers(StringUtils.defaultIfEmpty(fromClasses, "**")); + private WildcardPattern[] getFromPatterns() { + if (fromPatterns == null) { + fromPatterns = PatternUtils.createPatterns(StringUtils.defaultIfEmpty(fromClasses, "**")); } - return fromMatchers; + return fromPatterns; } - private WildcardPattern[] getToMatchers() { - if (toMatchers == null) { - toMatchers = PatternUtils.createMatchers(toClasses); + private WildcardPattern[] getToPatterns() { + if (toPatterns == null) { + toPatterns = PatternUtils.createPatterns(toClasses); } - return toMatchers; + return toPatterns; } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/PatternUtilsTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/PatternUtilsTest.java new file mode 100644 index 00000000000..ce506cc909a --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/PatternUtilsTest.java @@ -0,0 +1,19 @@ +package org.sonar.java; + +import org.junit.Test; +import org.sonar.api.utils.WildcardPattern; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class PatternUtilsTest { + + @Test + public void shouldConvertJavaPackagesToPatterns() { + WildcardPattern[] patterns = PatternUtils.createPatterns("org.sonar.Foo,javax.**"); + + assertThat(patterns.length, is(2)); + assertThat(patterns[0].match("org/sonar/Foo"), is(true)); + assertThat(patterns[1].match("javax.Bar"), is(true)); + } +} |