1111package org .junit .platform .commons .util ;
1212
1313import static java .util .stream .Collectors .joining ;
14- import static org .junit .platform .commons .util .ClasspathFilters .CLASS_FILE_SUFFIX ;
1514import static org .junit .platform .commons .util .StringUtils .isNotBlank ;
1615
1716import java .io .IOException ;
3231import java .util .function .Supplier ;
3332import java .util .stream .Stream ;
3433
34+ import org .junit .platform .commons .JUnitException ;
3535import org .junit .platform .commons .PreconditionViolationException ;
3636import org .junit .platform .commons .function .Try ;
3737import org .junit .platform .commons .io .Resource ;
@@ -182,10 +182,10 @@ private static void walkFilesForUri(URI baseUri, Predicate<Path> filter, BiConsu
182182 }
183183 }
184184
185- private void processClassFileSafely (Path baseDir , String basePackageName , ClassFilter classFilter , Path classFile ,
185+ private void processClassFileSafely (Path baseDir , String basePackageName , ClassFilter classFilter , Path file ,
186186 Consumer <Class <?>> classConsumer ) {
187187 try {
188- String fullyQualifiedClassName = determineFullyQualifiedClassName (baseDir , basePackageName , classFile );
188+ String fullyQualifiedClassName = determineFullyQualifiedClassName (baseDir , basePackageName , file );
189189 if (classFilter .match (fullyQualifiedClassName )) {
190190 try {
191191 // @formatter:off
@@ -196,12 +196,12 @@ private void processClassFileSafely(Path baseDir, String basePackageName, ClassF
196196 // @formatter:on
197197 }
198198 catch (InternalError internalError ) {
199- handleInternalError (classFile , fullyQualifiedClassName , internalError );
199+ handleInternalError (file , fullyQualifiedClassName , internalError );
200200 }
201201 }
202202 }
203203 catch (Throwable throwable ) {
204- handleThrowable (classFile , throwable );
204+ handleThrowable (file , throwable );
205205 }
206206 }
207207
@@ -221,12 +221,12 @@ private void processResourceFileSafely(Path baseDir, String basePackageName, Res
221221 }
222222 }
223223
224- private String determineFullyQualifiedClassName (Path baseDir , String basePackageName , Path classFile ) {
224+ private String determineFullyQualifiedClassName (Path baseDir , String basePackageName , Path file ) {
225225 // @formatter:off
226226 return Stream .of (
227227 basePackageName ,
228- determineSubpackageName (baseDir , classFile ),
229- determineSimpleClassName (classFile )
228+ determineSubpackageName (baseDir , file ),
229+ determineSimpleClassName (file )
230230 )
231231 .filter (value -> !value .isEmpty ()) // Handle default package appropriately.
232232 .collect (joining (PACKAGE_SEPARATOR_STRING ));
@@ -253,21 +253,29 @@ private String determineFullyQualifiedResourceName(Path baseDir, String basePack
253253 // @formatter:on
254254 }
255255
256- private String determineSimpleClassName (Path classFile ) {
257- String fileName = classFile .getFileName ().toString ();
258- return fileName .substring (0 , fileName .length () - CLASS_FILE_SUFFIX .length ());
256+ private String determineSimpleClassName (Path file ) {
257+ String fileName = file .getFileName ().toString ();
258+ return determineSimpleClassName (fileName );
259+ }
260+
261+ static String determineSimpleClassName (String fileName ) {
262+ int lastDot = fileName .lastIndexOf ('.' );
263+ if (lastDot < 0 ) {
264+ throw new JUnitException ("Expected file name with file extension, but got: " + fileName );
265+ }
266+ return fileName .substring (0 , lastDot );
259267 }
260268
261269 private String determineSimpleResourceName (Path resourceFile ) {
262270 return resourceFile .getFileName ().toString ();
263271 }
264272
265- private String determineSubpackageName (Path baseDir , Path classFile ) {
266- Path relativePath = baseDir .relativize (classFile .getParent ());
273+ private String determineSubpackageName (Path baseDir , Path file ) {
274+ Path relativePath = baseDir .relativize (file .getParent ());
267275 String pathSeparator = baseDir .getFileSystem ().getSeparator ();
268276 String subpackageName = relativePath .toString ().replace (pathSeparator , PACKAGE_SEPARATOR_STRING );
269277 if (subpackageName .endsWith (pathSeparator )) {
270- // Workaround for JDK bug: https://bugs.openjdk.java.net /browse/JDK-8153248
278+ // TODO: Remove workaround for JDK bug: https://bugs.openjdk.org /browse/JDK-8153248
271279 subpackageName = subpackageName .substring (0 , subpackageName .length () - pathSeparator .length ());
272280 }
273281 return subpackageName ;
0 commit comments