StringIndexOutOfBoundsException in findClassesInPackage - Surefire/Maven - JDK 11 fails, JDK 8 succeeds.
See original GitHub issueTestNG Version
7.1.0
Expected behavior
TestNG reads the package name correctly, succeeds in doing a substring
Actual behavior
TestNG keeps the package name from another class when doing a substring on current class, fails with error.
[ERROR] Caused by: java.lang.StringIndexOutOfBoundsException: begin 35, end 11, length 17
[ERROR] at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
[ERROR] at java.base/java.lang.String.substring(String.java:1874)
[ERROR] at org.testng.internal.PackageUtils.findClassesInPackage(PackageUtils.java:112)
[ERROR] at org.testng.xml.XmlPackage.initializeXmlClasses(XmlPackage.java:68)
[ERROR] at org.testng.xml.XmlPackage.getXmlClasses(XmlPackage.java:59)
Having instrumented a copy of TestNG jar with print statements, I can see that this fails when a Jar has its files enumerated. Specifically jaxb-api-2.3.1.jar, which include a file ‘module-info.class’, which lacks a package. PackageUtils then tries to do a substring based on the wrong package, with a class name that has no package, and fails.
This does not occur during the JDK 8 build, as Java 8 doesn’t cause the JAXB jars to be referenced in this part of the build, but Java 11 does.
My simple print statement: org.testng.internal.PackageUtils#findClassesInPackage
for (URL url : dirs) {
String protocol = url.getProtocol();
if (!matchTestClasspath(url, packageDirName, recursive)) {
continue;
}
System.err.println("url = " + url);
With Java 8:
url = file:/C:/projects/testng-failure-case/target/test-classes/
url = file:/C:/projects/testng-failure-case/target/classes/
With Java 11:
url = file:/C:/projects/testng-failure-case/target/test-classes/
url = file:/C:/projects/testng-failure-case/target/classes/
url = jar:file:/C:/Users/jsnell/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar!/META-INF/versions/9/
url = jar:file:/C:/Users/jsnell/.m2/repository/org/apache/logging/log4j/log4j-core/2.11.2/log4j-core-2.11.2.jar!/META-INF/versions/9/
url = jar:file:/C:/Users/jsnell/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/META-INF/versions/9/
Is the issue reproductible on runner?
Completely reproducible with Maven test case using attached source. See output: testng.failure.output.txt
Test case sample
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7
Top GitHub Comments
The
module-info.class
present in for example the JAXB-API JAR triggers this with Java 11. Only if a recursive package name is set in the TestNG suite (<package name=".*">
). When debugging, it looks like thepackageName
variable is kept from the previous file when reading the JAR file, causing out if index when assuming thatmodule-info.class
is also in a package.build-helper-maven-plugin appears to resolve the issue. A co-worker came up with this variant of the test case, though it appears to be running the tests twice now. testng-failure-case-working.zip