|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Optional;
|
20 | 20 | import java.util.Properties;
|
21 | 21 |
|
| 22 | +import org.junit.jupiter.api.Nested; |
22 | 23 | import org.junit.jupiter.api.Test;
|
23 | 24 |
|
24 | 25 | import org.springframework.beans.factory.BeanCreationException;
|
25 | 26 | import org.springframework.beans.factory.BeanDefinitionStoreException;
|
26 | 27 | import org.springframework.beans.factory.annotation.Value;
|
| 28 | +import org.springframework.beans.factory.config.BeanDefinition; |
27 | 29 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
28 | 30 | import org.springframework.beans.testfixture.beans.TestBean;
|
29 | 31 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
@@ -432,6 +434,150 @@ void optionalPropertyWithoutValue() {
|
432 | 434 | }
|
433 | 435 |
|
434 | 436 |
|
| 437 | + /** |
| 438 | + * Tests that use the escape character (or disable it) with nested placeholder |
| 439 | + * resolution. |
| 440 | + */ |
| 441 | + @Nested |
| 442 | + class EscapedNestedPlaceholdersTests { |
| 443 | + |
| 444 | + @Test // gh-34861 |
| 445 | + void singleEscapeWithDefaultEscapeCharacter() { |
| 446 | + MockEnvironment env = new MockEnvironment() |
| 447 | + .withProperty("user.home", "admin") |
| 448 | + .withProperty("my.property", "\\DOMAIN\\${user.home}"); |
| 449 | + |
| 450 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 451 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 452 | + ppc.setEnvironment(env); |
| 453 | + ppc.postProcessBeanFactory(bf); |
| 454 | + |
| 455 | + // \DOMAIN\${user.home} resolves to \DOMAIN${user.home} instead of \DOMAIN\admin |
| 456 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("\\DOMAIN${user.home}"); |
| 457 | + } |
| 458 | + |
| 459 | + @Test // gh-34861 |
| 460 | + void singleEscapeWithCustomEscapeCharacter() { |
| 461 | + MockEnvironment env = new MockEnvironment() |
| 462 | + .withProperty("user.home", "admin\\~${nested}") |
| 463 | + .withProperty("my.property", "DOMAIN\\${user.home}\\~${enigma}"); |
| 464 | + |
| 465 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 466 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 467 | + ppc.setEnvironment(env); |
| 468 | + // Set custom escape character. |
| 469 | + ppc.setEscapeCharacter('~'); |
| 470 | + ppc.postProcessBeanFactory(bf); |
| 471 | + |
| 472 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("DOMAIN\\admin\\${nested}\\${enigma}"); |
| 473 | + } |
| 474 | + |
| 475 | + @Test // gh-34861 |
| 476 | + void singleEscapeWithEscapeCharacterDisabled() { |
| 477 | + MockEnvironment env = new MockEnvironment() |
| 478 | + .withProperty("user.home", "admin\\") |
| 479 | + .withProperty("my.property", "\\DOMAIN\\${user.home}"); |
| 480 | + |
| 481 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 482 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 483 | + ppc.setEnvironment(env); |
| 484 | + // Disable escape character. |
| 485 | + ppc.setEscapeCharacter(null); |
| 486 | + ppc.postProcessBeanFactory(bf); |
| 487 | + |
| 488 | + // \DOMAIN\${user.home} resolves to \DOMAIN\admin |
| 489 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("\\DOMAIN\\admin\\"); |
| 490 | + } |
| 491 | + |
| 492 | + @Test // gh-34861 |
| 493 | + void tripleEscapeWithDefaultEscapeCharacter() { |
| 494 | + MockEnvironment env = new MockEnvironment() |
| 495 | + .withProperty("user.home", "admin\\\\\\") |
| 496 | + .withProperty("my.property", "DOMAIN\\\\\\${user.home}#${user.home}"); |
| 497 | + |
| 498 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 499 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 500 | + ppc.setEnvironment(env); |
| 501 | + ppc.postProcessBeanFactory(bf); |
| 502 | + |
| 503 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("DOMAIN\\\\${user.home}#admin\\\\\\"); |
| 504 | + } |
| 505 | + |
| 506 | + @Test // gh-34861 |
| 507 | + void tripleEscapeWithCustomEscapeCharacter() { |
| 508 | + MockEnvironment env = new MockEnvironment() |
| 509 | + .withProperty("user.home", "admin\\~${enigma}") |
| 510 | + .withProperty("my.property", "DOMAIN~~~${user.home}#${user.home}"); |
| 511 | + |
| 512 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 513 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 514 | + ppc.setEnvironment(env); |
| 515 | + // Set custom escape character. |
| 516 | + ppc.setEscapeCharacter('~'); |
| 517 | + ppc.postProcessBeanFactory(bf); |
| 518 | + |
| 519 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("DOMAIN~~${user.home}#admin\\${enigma}"); |
| 520 | + } |
| 521 | + |
| 522 | + @Test // gh-34861 |
| 523 | + void singleEscapeWithDefaultEscapeCharacterAndIgnoreUnresolvablePlaceholders() { |
| 524 | + MockEnvironment env = new MockEnvironment() |
| 525 | + .withProperty("user.home", "${enigma}") |
| 526 | + .withProperty("my.property", "\\${DOMAIN}${user.home}"); |
| 527 | + |
| 528 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 529 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 530 | + ppc.setEnvironment(env); |
| 531 | + ppc.setIgnoreUnresolvablePlaceholders(true); |
| 532 | + ppc.postProcessBeanFactory(bf); |
| 533 | + |
| 534 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("${DOMAIN}${enigma}"); |
| 535 | + } |
| 536 | + |
| 537 | + @Test // gh-34861 |
| 538 | + void singleEscapeWithCustomEscapeCharacterAndIgnoreUnresolvablePlaceholders() { |
| 539 | + MockEnvironment env = new MockEnvironment() |
| 540 | + .withProperty("user.home", "${enigma}") |
| 541 | + .withProperty("my.property", "~${DOMAIN}\\${user.home}"); |
| 542 | + |
| 543 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 544 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 545 | + ppc.setEnvironment(env); |
| 546 | + // Set custom escape character. |
| 547 | + ppc.setEscapeCharacter('~'); |
| 548 | + ppc.setIgnoreUnresolvablePlaceholders(true); |
| 549 | + ppc.postProcessBeanFactory(bf); |
| 550 | + |
| 551 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("${DOMAIN}\\${enigma}"); |
| 552 | + } |
| 553 | + |
| 554 | + @Test // gh-34861 |
| 555 | + void tripleEscapeWithDefaultEscapeCharacterAndIgnoreUnresolvablePlaceholders() { |
| 556 | + MockEnvironment env = new MockEnvironment() |
| 557 | + .withProperty("user.home", "${enigma}") |
| 558 | + .withProperty("my.property", "X:\\\\\\${DOMAIN}${user.home}"); |
| 559 | + |
| 560 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 561 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 562 | + ppc.setEnvironment(env); |
| 563 | + ppc.setIgnoreUnresolvablePlaceholders(true); |
| 564 | + ppc.postProcessBeanFactory(bf); |
| 565 | + |
| 566 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("X:\\\\${DOMAIN}${enigma}"); |
| 567 | + } |
| 568 | + |
| 569 | + private static DefaultListableBeanFactory createBeanFactory() { |
| 570 | + BeanDefinition beanDefinition = genericBeanDefinition(TestBean.class) |
| 571 | + .addPropertyValue("name", "${my.property}") |
| 572 | + .getBeanDefinition(); |
| 573 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 574 | + bf.registerBeanDefinition("testBean",beanDefinition); |
| 575 | + return bf; |
| 576 | + } |
| 577 | + |
| 578 | + } |
| 579 | + |
| 580 | + |
435 | 581 | private static class OptionalTestBean {
|
436 | 582 |
|
437 | 583 | private Optional<String> name;
|
|
0 commit comments