Skip to content

Commit 2e99672

Browse files
committed
Add test about validateProfiles
1 parent 225382f commit 2e99672

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,75 @@ void resolveWhenOptionalAndExtensionIsUnknownShouldNotFail() {
294294
assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location));
295295
}
296296

297+
@Test
298+
void resolveProfileSpecificWhenProfileIsValidShouldNotThrowException() {
299+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
300+
this.environment.setActiveProfiles("dev-test_123");
301+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
302+
assertThatNoException()
303+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
304+
}
305+
306+
@Test
307+
void resolveProfileSpecificWithAdditionalValidProfilesShouldNotThrowException() {
308+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
309+
this.environment.setActiveProfiles("dev-test");
310+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, List.of("prod-test", "stage-test"));
311+
assertThatNoException()
312+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
313+
}
314+
315+
@Test
316+
void resolveProfileSpecificWhenProfileStartsWithSymbolThrowsException() {
317+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
318+
this.environment.setActiveProfiles("-dev");
319+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
320+
assertThatIllegalStateException()
321+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
322+
.withMessageStartingWith("Invalid profile '-dev': must not start with '-' or '_'");
323+
}
324+
325+
@Test
326+
void resolveProfileSpecificWhenProfileStartsWithUnderscoreThrowsException() {
327+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
328+
this.environment.setActiveProfiles("_dev");
329+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
330+
assertThatIllegalStateException()
331+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
332+
.withMessageStartingWith("Invalid profile '_dev': must not start with '-' or '_'");
333+
}
334+
335+
@Test
336+
void resolveProfileSpecificWhenProfileEndsWithSymbolThrowsException() {
337+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
338+
this.environment.setActiveProfiles("dev-");
339+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
340+
assertThatIllegalStateException()
341+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
342+
.withMessageStartingWith("Invalid profile 'dev-': must not end with '-' or '_'");
343+
}
344+
345+
@Test
346+
void resolveProfileSpecificWhenProfileEndsWithUnderscoreThrowsException() {
347+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
348+
this.environment.setActiveProfiles("dev_");
349+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
350+
assertThatIllegalStateException()
351+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
352+
.withMessageStartingWith("Invalid profile 'dev_': must not end with '-' or '_'");
353+
}
354+
355+
@Test
356+
void resolveProfileSpecificWhenProfileContainsInvalidCharactersThrowsException() {
357+
ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
358+
this.environment.setActiveProfiles("dev*test");
359+
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
360+
assertThatIllegalStateException()
361+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
362+
.withMessageStartingWith(
363+
"Invalid profile 'dev*test': must only contain alphanumeric characters, '-', or '_'");
364+
}
365+
297366
private String filePath(String... components) {
298367
return "file [" + String.join(File.separator, components) + "]";
299368
}

0 commit comments

Comments
 (0)