TNG / junit-dataprovider

A TestNG like dataprovider runner for JUnit with many additional features
Apache License 2.0
246 stars 164 forks source link

Since v1.9.0 the dollar character ($) in values is leading to exceptions #34

Closed janschaefer closed 10 years ago

janschaefer commented 10 years ago

The following test fails with v1.9.0:

@Test
@DataProvider( "$" ) 
public void test( String s ) {
}

Exception:

java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
    at com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder.process(BasePlaceholder.java:61)
    at com.tngtech.java.junit.dataprovider.DataProviderFrameworkMethod.getName(DataProviderFrameworkMethod.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.testName(BlockJUnit4ClassRunner.java:203)
    at org.junit.runners.BlockJUnit4ClassRunner.describeChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.describeChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:373)
    at org.junit.runners.ParentRunner.filter(ParentRunner.java:326)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.filter(DataProviderRunner.java:213)
    at org.junit.runner.manipulation.Filter.apply(Filter.java:97)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
janschaefer commented 10 years ago

I guess the problem comes from the appendReplacement call in BasePlaceholder.java:61

From the JavaDoc of appendReplacement:

     * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
     * the replacement string may cause the results to be different than if it
     * were being treated as a literal replacement string. Dollar signs may be
     * treated as references to captured subsequences as described above, and
     * backslashes are used to escape literal characters in the replacement
     * string.
janschaefer commented 10 years ago

I guess the solution would be to pass the argument to the quoteReplacement method of the Matcher class

aaschmid commented 10 years ago

Hi @janschaefer,

you are totally right, I have overseen that case completely :-( Sorry for the inconvenience.

Do you need a new release immediately? I can also provide a new version as snapshot which you can test your code with?

Cheers, Andreas

aaschmid commented 10 years ago

The only possibility we loose now, is to have a matcher group in the regular expression and refer this regular expression then placeholder processing by $1 etc. But I think the other case is more important such that I will cope with this small flexibility loose for now ...

janschaefer commented 10 years ago

As I currently do not need the new features of the DataProvider I can live with version 1.8.0. The only problem is that, in addition, JGiven does not work with v1.9.0 anymore because of the @BeforeClass support. This, however, is more a JGiven bug and can easily be fixed in JGiven. With 1.9.0, however, some JGiven tests are broken because of the $ problem :-).

aaschmid commented 10 years ago

Ok, I see. You can use the just published version 1.9.1-SNAPSHOT from

       <repository>
         <id>snapshots-repo</id>
         <url>https://oss.sonatype.org/content/repositories/snapshots</url>
         <releases><enabled>false</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>

(see also http://stackoverflow.com/questions/7715321/how-to-download-snapshot-version-from-maven-snapshot-repository)

to fix your bug in JGiven and give me feedback of some other findings. If you don't have some, I will publish a new official version :-) Ok?

janschaefer commented 10 years ago

I have checked it and actually it wasn't a JGiven issue, but rather a problem with the dataprovider runner that does not execute ClassRules as far as I see. I have created a new Issue #35 for that