Skip to content

Commit b85a890

Browse files
committed
AppUtils: recognize target/test-classes, too
Otherwise, base directory detection for test classes is wrong in some cases -- e.g., when running a test class from Eclipse.
1 parent cfd3e73 commit b85a890

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/main/java/org/scijava/util/AppUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,13 @@ public static File getBaseDirectory(final File classLocation,
234234
if (baseSubdirectory != null) basePrefix += baseSubdirectory + "/";
235235

236236
final String targetClassesSuffix = basePrefix + "target/classes";
237-
if (path.endsWith(targetClassesSuffix)) {
238-
// NB: The class is a file beneath the Maven build directory
239-
// ("target/classes").
240-
path = path.substring(0, path.length() - targetClassesSuffix.length());
237+
final String targetTestClassesSuffix = basePrefix + "target/test-classes";
238+
final String[] suffixes = {targetClassesSuffix, targetTestClassesSuffix};
239+
for (final String suffix : suffixes) {
240+
if (!path.endsWith(suffix)) continue;
241+
242+
// NB: The class is a file beneath a Maven build directory.
243+
path = path.substring(0, path.length() - suffix.length());
241244

242245
File dir = new File(path);
243246
if (baseSubdirectory == null) {

src/test/java/org/scijava/util/AppUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void testBaseDirectory() {
5353
final String tmp = PlatformUtils.isWindows() ? "c:/tmp" : "/tmp";
5454
assertEquals(new File(tmp), AppUtils.getBaseDirectory(new File(
5555
tmp + "/app/target/classes"), "app"));
56+
assertEquals(new File(tmp), AppUtils.getBaseDirectory(new File(
57+
tmp + "/app/target/test-classes"), "app"));
5658
assertEquals(new File(tmp), AppUtils.getBaseDirectory(new File(
5759
tmp + "/app/target/ij-app-1.57.jar"), "app"));
5860
}

0 commit comments

Comments
 (0)