From 161bfc2308cbc15ca8c4d74d17178761dffccc30 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Thu, 19 Feb 2015 18:54:46 +0000 Subject: [PATCH] Support for MessageSource in tests Added support for testing messageSource elements such as th-text="#{my.key}" --- .gitignore | 7 +--- pom.xml | 2 +- .../config/ExtendableTestSpringContext.java | 6 +++ .../testing/config/TestMessageSource.java | 38 +++++++++++++++++ .../thymeleaf/testing/SampleUnitTest.java | 41 +++++++++++-------- src/test/resources/sampleUnitTest.html | 2 + 6 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/connect_group/thymeleaf/testing/config/TestMessageSource.java diff --git a/.gitignore b/.gitignore index 301255a..62b0c92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ target/ - .classpath - .project - *.prefs - -thymeleaf-tdd.iml \ No newline at end of file +thymeleaf-tdd.iml +/.idea/ diff --git a/pom.xml b/pom.xml index 2126a4e..9ce5676 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ com.connect-group thymeleaf-tdd - 1.0.3 + 1.0.4 Thymeleaf TDD Test-Driven Development framework for Thymeleaf and Thymesheet diff --git a/src/main/java/com/connect_group/thymeleaf/testing/config/ExtendableTestSpringContext.java b/src/main/java/com/connect_group/thymeleaf/testing/config/ExtendableTestSpringContext.java index faaf432..c94de92 100644 --- a/src/main/java/com/connect_group/thymeleaf/testing/config/ExtendableTestSpringContext.java +++ b/src/main/java/com/connect_group/thymeleaf/testing/config/ExtendableTestSpringContext.java @@ -50,6 +50,12 @@ public static PropertySourcesPlaceholderConfigurer propertyConfigurer() throws I return configurer; } + @Bean + public TestMessageSource messageSource() { + TestMessageSource messageSource = new TestMessageSource(); + return messageSource; + } + public static String getTestResourcesHtmlPath(String filename) { String path; URL url = ThymesheetTestSpringContext.class.getResource(filename); diff --git a/src/main/java/com/connect_group/thymeleaf/testing/config/TestMessageSource.java b/src/main/java/com/connect_group/thymeleaf/testing/config/TestMessageSource.java new file mode 100644 index 0000000..708be0f --- /dev/null +++ b/src/main/java/com/connect_group/thymeleaf/testing/config/TestMessageSource.java @@ -0,0 +1,38 @@ +package com.connect_group.thymeleaf.testing.config; + +import org.springframework.context.support.AbstractMessageSource; + +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Locale; + +public class TestMessageSource extends AbstractMessageSource { + + HashMap messageOverrides = new HashMap(); + + public void reset() { + messageOverrides = new HashMap(); + } + + public void givenMessageWithKey(String code, String message) { + messageOverrides.put(code, message); + } + + @Override + protected MessageFormat resolveCode(String key, Locale locale) { + if(messageOverrides.containsKey(key)) { + return new MessageFormat(messageOverrides.get(key), locale); + } else { + return new MessageFormat("??"+key+"_"+locale.toString()+"??", locale); + } + } + + @Override + protected String resolveCodeWithoutArguments(String code, Locale locale) { + if(messageOverrides.containsKey(code)) { + return messageOverrides.get(code); + } else { + return super.resolveCodeWithoutArguments(code, locale); + } + } +} diff --git a/src/test/java/com/connect_group/thymeleaf/testing/SampleUnitTest.java b/src/test/java/com/connect_group/thymeleaf/testing/SampleUnitTest.java index e87158b..31dcb41 100644 --- a/src/test/java/com/connect_group/thymeleaf/testing/SampleUnitTest.java +++ b/src/test/java/com/connect_group/thymeleaf/testing/SampleUnitTest.java @@ -1,17 +1,8 @@ package com.connect_group.thymeleaf.testing; -import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.exists; -import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.hasAttribute; -import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.hasOnlyText; -import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.isSingleElementThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertThat; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - +import com.connect_group.thymeleaf.testing.config.TestMessageSource; +import com.connect_group.thymeleaf.testing.config.ThymesheetTestSpringContext; +import com.connect_group.thymesheet.query.HtmlElements; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -19,8 +10,17 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.connect_group.thymeleaf.testing.config.ThymesheetTestSpringContext; -import com.connect_group.thymesheet.query.HtmlElements; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.exists; +import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.hasAttribute; +import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.hasOnlyText; +import static com.connect_group.thymeleaf.testing.hamcrest.ThymeleafMatchers.isSingleElementThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ThymesheetTestSpringContext.class }) @@ -32,6 +32,9 @@ public class SampleUnitTest { @Autowired private ThymeleafTestEngine testEngine; + + @Autowired + private TestMessageSource messageSource; @Before public void setup() { @@ -83,7 +86,13 @@ public void shouldNotOutputAnyParagraphs_WhenNoTextInModel() throws Exception { public void shouldSpecifyExpectedUrlInLink() throws Exception { model.put("href", "http://expected/target/url"); HtmlElements tags = testEngine.process(HTML_PATH_IN_TEST_RESOURCES_FOLDER, model); - assertThat(tags.matching("body > a"), isSingleElementThat(hasAttribute("href", "http://expected/target/url"))); - + assertThat(tags.matching("body > a"), isSingleElementThat(hasAttribute("href", "http://expected/target/url"))); } + + @Test + public void shouldUseTextFromResourceBundle_WhenPreTagRefersToMessageSourceKey() throws Exception { + messageSource.givenMessageWithKey("my_resource_message", "Expected i18n copy"); + HtmlElements tags = testEngine.process(HTML_PATH_IN_TEST_RESOURCES_FOLDER, model); + assertThat(tags.matching("body > pre"), isSingleElementThat(hasOnlyText("Expected i18n copy"))); + } } diff --git a/src/test/resources/sampleUnitTest.html b/src/test/resources/sampleUnitTest.html index 0cf28bc..ca9df80 100644 --- a/src/test/resources/sampleUnitTest.html +++ b/src/test/resources/sampleUnitTest.html @@ -11,5 +11,7 @@

A sub-heading

Sub text

More sub text

A Link + +
Text should be replaced with copy from i18n resource bundle.
\ No newline at end of file