|
44 | 44 | * @author Madhura Bhave
|
45 | 45 | * @author Phillip Webb
|
46 | 46 | * @author Moritz Halbritter
|
| 47 | + * @author Sijun Yang |
47 | 48 | */
|
48 | 49 | class StandardConfigDataLocationResolverTests {
|
49 | 50 |
|
@@ -254,8 +255,8 @@ void resolveWhenLocationUsesOptionalExtensionSyntaxResolves() throws Exception {
|
254 | 255 | @Test
|
255 | 256 | void resolveProfileSpecificReturnsProfileSpecificFiles() {
|
256 | 257 | ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/");
|
257 |
| - Profiles profiles = mock(Profiles.class); |
258 |
| - given(profiles.iterator()).willReturn(Collections.singletonList("dev").iterator()); |
| 258 | + this.environment.setActiveProfiles("dev"); |
| 259 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
259 | 260 | List<StandardConfigDataResource> locations = this.resolver.resolveProfileSpecific(this.context, location,
|
260 | 261 | profiles);
|
261 | 262 | assertThat(locations).hasSize(1);
|
@@ -293,6 +294,83 @@ void resolveWhenOptionalAndExtensionIsUnknownShouldNotFail() {
|
293 | 294 | assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location));
|
294 | 295 | }
|
295 | 296 |
|
| 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 resolveProfileSpecificWithNonAsciiCharactersShouldNotThrowException() { |
| 308 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 309 | + this.environment.setActiveProfiles("dev-테스트_123"); |
| 310 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 311 | + assertThatNoException() |
| 312 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + void resolveProfileSpecificWithAdditionalValidProfilesShouldNotThrowException() { |
| 317 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 318 | + this.environment.setActiveProfiles("dev-test"); |
| 319 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, List.of("prod-test", "stage-test")); |
| 320 | + assertThatNoException() |
| 321 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + void resolveProfileSpecificWhenProfileStartsWithSymbolThrowsException() { |
| 326 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 327 | + this.environment.setActiveProfiles("-dev"); |
| 328 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 329 | + assertThatIllegalStateException() |
| 330 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)) |
| 331 | + .withMessageStartingWith("Invalid profile '-dev': must not start with '-' or '_'"); |
| 332 | + } |
| 333 | + |
| 334 | + @Test |
| 335 | + void resolveProfileSpecificWhenProfileStartsWithUnderscoreThrowsException() { |
| 336 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 337 | + this.environment.setActiveProfiles("_dev"); |
| 338 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 339 | + assertThatIllegalStateException() |
| 340 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)) |
| 341 | + .withMessageStartingWith("Invalid profile '_dev': must not start with '-' or '_'"); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + void resolveProfileSpecificWhenProfileEndsWithSymbolThrowsException() { |
| 346 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 347 | + this.environment.setActiveProfiles("dev-"); |
| 348 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 349 | + assertThatIllegalStateException() |
| 350 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)) |
| 351 | + .withMessageStartingWith("Invalid profile 'dev-': must not end with '-' or '_'"); |
| 352 | + } |
| 353 | + |
| 354 | + @Test |
| 355 | + void resolveProfileSpecificWhenProfileEndsWithUnderscoreThrowsException() { |
| 356 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 357 | + this.environment.setActiveProfiles("dev_"); |
| 358 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 359 | + assertThatIllegalStateException() |
| 360 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)) |
| 361 | + .withMessageStartingWith("Invalid profile 'dev_': must not end with '-' or '_'"); |
| 362 | + } |
| 363 | + |
| 364 | + @Test |
| 365 | + void resolveProfileSpecificWhenProfileContainsInvalidCharactersThrowsException() { |
| 366 | + ConfigDataLocation location = ConfigDataLocation.of("classpath:/configdata/properties/"); |
| 367 | + this.environment.setActiveProfiles("dev*test"); |
| 368 | + Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList()); |
| 369 | + assertThatIllegalStateException() |
| 370 | + .isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles)) |
| 371 | + .withMessageStartingWith("Invalid profile 'dev*test': must contain only letters or digits or '-' or '_'"); |
| 372 | + } |
| 373 | + |
296 | 374 | private String filePath(String... components) {
|
297 | 375 | return "file [" + String.join(File.separator, components) + "]";
|
298 | 376 | }
|
|
0 commit comments