Skip to content

Commit 45a88d8

Browse files
committed
ScanningClasspathEntry returns empty when not found
Closes gh-68
1 parent 05b3089 commit 45a88d8

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/ScanningClasspathEntry.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.experimental.boot.server.exec;
1818

19+
import java.io.FileNotFoundException;
1920
import java.io.IOException;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import java.util.Arrays;
24+
import java.util.Collections;
2325
import java.util.List;
2426
import java.util.function.Function;
2527
import java.util.regex.Pattern;
@@ -46,6 +48,8 @@ class ScanningClasspathEntry implements ClasspathEntry {
4648

4749
private final Function<String, String> renameResource;
4850

51+
private Resource[] resources;
52+
4953
private Path classpath;
5054

5155
ScanningClasspathEntry(String baseDir) {
@@ -69,17 +73,23 @@ Path getClasspath() {
6973

7074
@Override
7175
public List<String> resolve() {
72-
if (this.classpath == null) {
73-
this.classpath = createClasspath();
76+
if (this.resources == null) {
77+
this.resources = getResources();
78+
this.classpath = createClasspath(this.resources);
7479
}
75-
return Arrays.asList(this.classpath.toFile().getAbsolutePath());
80+
return this.classpath == null ? Collections.emptyList()
81+
: Arrays.asList(this.classpath.toFile().getAbsolutePath());
7682
}
7783

78-
private Path createClasspath() {
79-
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
84+
private Path createClasspath(Resource[] resources) {
85+
if (this.resources == null || this.resources.length == 0) {
86+
return null;
87+
}
8088
try {
8189
Path classpath = TempDir.tempDir();
82-
Resource[] resources = resolver.getResources(this.resourcePattern);
90+
if (this.logger.isDebugEnabled()) {
91+
this.logger.debug("Found " + resources.length + " resources for pattern " + this.resourcePattern);
92+
}
8393
for (Resource resource : resources) {
8494
String path = this.renameResource.apply(getPath(resource));
8595
if (!path.endsWith("/") && resource.isReadable()) {
@@ -98,6 +108,19 @@ private Path createClasspath() {
98108
}
99109
}
100110

111+
private Resource[] getResources() {
112+
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
113+
try {
114+
return resolver.getResources(this.resourcePattern);
115+
}
116+
catch (FileNotFoundException ex) {
117+
return new Resource[0];
118+
}
119+
catch (IOException ex) {
120+
throw new RuntimeException("Failed to resolve " + this.resourcePattern, ex);
121+
}
122+
}
123+
101124
private String getPath(Resource resource) {
102125
if (resource instanceof ClassPathResource classPathResource) {
103126
return classPathResource.getPath();
@@ -111,7 +134,9 @@ private String getPath(Resource resource) {
111134
@Override
112135
public void cleanup() {
113136
try {
114-
FileSystemUtils.deleteRecursively(this.classpath);
137+
if (this.classpath != null) {
138+
FileSystemUtils.deleteRecursively(this.classpath);
139+
}
115140
}
116141
catch (IOException ex) {
117142
throw new RuntimeException(ex);

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/ScanningClasspathEntryTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ void configServer() {
7979
assertThat(new File(path, "configrepo/configclient.properties")).exists();
8080
}
8181

82+
@Test
83+
void missingPathThenNoExceptionAndEmpty() {
84+
this.classpathEntry = new ScanningClasspathEntry("missing/path");
85+
assertThat(this.classpathEntry.resolve()).isEmpty();
86+
}
87+
8288
}

0 commit comments

Comments
 (0)