Skip to content

Commit 4045ca8

Browse files
committed
Add all resources in testjars/beanName/**
Closes gh-61
1 parent 4f0c701 commit 4045ca8

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ public Class<?> getObjectType() {
148148

149149
@Override
150150
public void setBeanName(String beanName) {
151-
defaultApplicationConfiguration(beanName, "yaml");
152-
defaultApplicationConfiguration(beanName, "yml");
153-
defaultApplicationConfiguration(beanName, "properties");
151+
String basePath = "testjars/" + beanName;
152+
this.classpath.entries(new ScanningClasspathEntry(basePath));
154153
}
155154

156155
private void defaultApplicationConfiguration(String beanName, String extension) {

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Path;
2222
import java.util.Arrays;
2323
import java.util.List;
24+
import java.util.function.Function;
2425
import java.util.regex.Pattern;
2526

2627
import org.springframework.core.io.ClassPathResource;
@@ -36,12 +37,25 @@
3637
*/
3738
class ScanningClasspathEntry implements ClasspathEntry {
3839

39-
private final Class<?> clazz;
40+
private final String resourcePattern;
41+
42+
private final Function<String, String> renameResource;
4043

4144
private Path classpath;
4245

46+
ScanningClasspathEntry(String baseDir) {
47+
this.resourcePattern = baseDir + "/**";
48+
this.renameResource = (name) -> {
49+
String prefix = baseDir + "/";
50+
int index = name.lastIndexOf(prefix);
51+
return (index > -1) ? name.substring(index + prefix.length()) : name;
52+
};
53+
}
54+
4355
ScanningClasspathEntry(Class<?> clazz) {
44-
this.clazz = clazz;
56+
String resourcePath = toResourceName(clazz.getPackageName());
57+
this.resourcePattern = resourcePath + "/**";
58+
this.renameResource = (name) -> name.substring(name.lastIndexOf(resourcePath));
4559
}
4660

4761
Path getClasspath() {
@@ -57,16 +71,14 @@ public List<String> resolve() {
5771
}
5872

5973
private Path createClasspath() {
60-
String resourcePath = toResourceName(this.clazz.getPackageName());
61-
String resourcePattern = resourcePath + "/**";
6274
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
6375
try {
6476
Path classpath = Files.createTempDirectory("classpath-");
65-
Resource[] resources = resolver.getResources(resourcePattern);
77+
Resource[] resources = resolver.getResources(this.resourcePattern);
6678
for (Resource resource : resources) {
67-
String path = getPath(resource);
68-
if (!path.endsWith("/")) {
69-
Path destination = classpath.resolve(resourcePath).resolve(resource.getFilename());
79+
String path = this.renameResource.apply(getPath(resource));
80+
if (!path.endsWith("/") && resource.isReadable()) {
81+
Path destination = classpath.resolve(path);
7082
destination.getParent().toFile().mkdirs();
7183
Files.copy(resource.getInputStream(), destination);
7284
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void classpathWhenPropertiesDefaults() throws Exception {
139139
CommonsExecWebServerFactoryBean factory = CommonsExecWebServerFactoryBean.builder();
140140
factory.setBeanName("hasProperties");
141141
factory.classpath((cp) -> {
142+
cp.build();
142143
List<ClasspathEntry> classpath = cp.getClasspath();
143144
assertClasspathContainsResourceWithContent(classpath, "application.properties", expectedContent);
144145
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ void cleanup() {
6868
assertThat(this.classpathEntry.getClasspath()).doesNotExist();
6969
}
7070

71+
@Test
72+
void configServer() {
73+
this.classpathEntry = new ScanningClasspathEntry("testjars/configServer");
74+
List<String> classpath = this.classpathEntry.resolve();
75+
assertThat(classpath).hasSize(1);
76+
File path = new File(classpath.get(0));
77+
assertThat(path).exists();
78+
assertThat(new File(path, "application.yml")).exists();
79+
assertThat(new File(path, "configrepo/configclient.properties")).exists();
80+
}
81+
7182
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring:
2+
application.name: configserver
3+
profiles:
4+
active: native
5+
cloud:
6+
config:
7+
server:
8+
native:
9+
searchLocations: "classpath:configrepo"
10+
server:
11+
port: 8888
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo.prop=bar

0 commit comments

Comments
 (0)