Skip to content

Commit

Permalink
Support for MessageSource in tests
Browse files Browse the repository at this point in the history
Added support for testing messageSource elements such as
th-text="#{my.key}"
  • Loading branch information
Adam Perry committed Feb 19, 2015
1 parent d686bea commit 161bfc2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 22 deletions.
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
target/

.classpath

.project

*.prefs

thymeleaf-tdd.iml
thymeleaf-tdd.iml
/.idea/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.connect-group</groupId>
<artifactId>thymeleaf-tdd</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>

<name>Thymeleaf TDD</name>
<description>Test-Driven Development framework for Thymeleaf and Thymesheet</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String,String> messageOverrides = new HashMap<String,String>();

public void reset() {
messageOverrides = new HashMap<String,String>();
}

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
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 })
Expand All @@ -32,6 +32,9 @@ public class SampleUnitTest {

@Autowired
private ThymeleafTestEngine testEngine;

@Autowired
private TestMessageSource messageSource;

@Before
public void setup() {
Expand Down Expand Up @@ -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")));
}
}
2 changes: 2 additions & 0 deletions src/test/resources/sampleUnitTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ <h2>A sub-heading</h2>
<p data-th-each="para : ${section2Paragraphs}" data-th-text="${para}">Sub text</p>
<p data-th-remove="all">More sub text</p>
<a data-th-href="${href}" href="http://www.example.com/">A Link</a>

<pre data-th-text="#{my_resource_message}">Text should be replaced with copy from i18n resource bundle.</pre>
</body>
</html>

0 comments on commit 161bfc2

Please sign in to comment.