From d1ad42a857559523c087492837255a491592172b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haindrich=20Zolt=C3=A1n=20=28kirk=29?= Date: Tue, 24 May 2016 22:28:11 +0200 Subject: [PATCH] SUREFIRE-1250: add exceptions to describe usage rules of %regex --- .../maven/surefire/testset/ResolvedTest.java | 23 ++++++ .../testset/TestListResolverTest.java | 77 +++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java b/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java index cf3af6f42a..50dc5c3302 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java @@ -81,11 +81,13 @@ public ResolvedTest( String classPattern, String methodPattern, boolean isRegex if ( isRegex && classPattern != null ) { classPattern = wrapRegex( classPattern ); + regexSanityCheck( classPattern ); } if ( isRegex && methodPattern != null ) { methodPattern = wrapRegex( methodPattern ); + regexSanityCheck( methodPattern ); } this.classPattern = reformatClassPattern( classPattern, isRegex ); @@ -105,6 +107,7 @@ public ResolvedTest( Type type, String pattern, boolean isRegex ) if ( isRegex && pattern != null ) { pattern = wrapRegex( pattern ); + regexSanityCheck( pattern ); } classPattern = isClass ? reformatClassPattern( pattern, isRegex ) : null; methodPattern = !isClass ? pattern : null; @@ -391,4 +394,24 @@ else if ( !cls.contains( "/" ) ) return cls; } } + + private static void regexSanityCheck( String methodNameExpr ) + { + try + { + // this will emit regex exceptions + from( methodNameExpr ); + if ( methodNameExpr.indexOf( "#" ) != -1 ) + { + throw new IllegalArgumentException( "Extra # in regex: " + methodNameExpr ); + } + } + catch ( IllegalArgumentException pse ) + { + throw new IllegalArgumentException( "%regex[] usage rule violation, valid regex rules:\n" + + " * # - where both regex can be individually evaluated as a regex\n" + + " * you may use at most 1 '#' to in one regex filter.", pse ); + } + } + } diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java index 81e876be12..da47884c0e 100644 --- a/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java +++ b/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java @@ -411,4 +411,81 @@ private static Set resolveClass( String patterns ) } return resolved; } + + public void testRegexRuleViolationQuotedHashMark() + { + try + { + new TestListResolver( "%regex[.\\Q#\\E.]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + + public void testRegexRuleViolationEnclosedMethodSeparator() + { + try + { + new TestListResolver( "%regex[(.|.#.)]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + + public void testRegexRuleViolationMultipleHashmarkWithClassConstraint() + { + try + { + new TestListResolver( "%regex[.*#.|#.]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + + public void testRegexRuleViolationMultipleHashmarkForMethods() + { + try + { + new TestListResolver( "%regex[#.|#.]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + public void testRegexRuleViolationInvalidClassPattern() + { + try + { + new TestListResolver( "%regex[.(.]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + public void testRegexRuleViolationInvalidMethodPattern() + { + try + { + new TestListResolver( "%regex[#.(.]" ); + fail( "IllegalArgumentException is expected" ); + } + catch ( IllegalArgumentException iea ) + { + // expected + } + } + }