code for BAEL-860 - Introduction to mustache (#1885)

This commit is contained in:
Mohamed Sanaulla 2017-05-20 20:21:33 +03:00 committed by maibin
parent d1cc70666e
commit cfc44ecbb6
10 changed files with 299 additions and 0 deletions

77
mustache/pom.xml Normal file
View File

@ -0,0 +1,77 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>mustache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mustache</name>
<dependencies>
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>${mustache.compiler.api.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<mustache.compiler.api.version>0.9.2</mustache.compiler.api.version>
<assertj.version>3.7.0</assertj.version>
<log4j.version>1.2.16</log4j.version>
<junit.version>4.12</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package com.baeldung.mustache;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.MustacheFactory;
public class MustacheUtil {
private static final MustacheFactory mf = new DefaultMustacheFactory();
public static MustacheFactory getMustacheFactory(){
return mf;
}
}

View File

@ -0,0 +1,84 @@
package com.baeldung.mustache.model;
import java.time.Duration;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.function.Function;
public class Todo {
public Todo(){}
public Todo(String title, String text){
this.title = title;
this.text = text;
createdOn = new Date();
}
private String title;
private String text;
private boolean done = false;
private Date createdOn;
private Date completedOn;
public void markAsDone(){
done = true;
completedOn = new Date();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean getDone() {
return done;
}
public Date getCreatedOn() {
return createdOn;
}
public Date getCompletedOn() {
return completedOn;
}
public void setDone(boolean done) {
this.done = done;
}
public void setCompletedOn(Date completedOn) {
this.completedOn = completedOn;
}
public long doneSince(){
if ( done ){
return Duration.between(createdOn.toInstant(), completedOn.toInstant()).toMinutes();
}
return 0;
}
public Function<Object, Object> handleDone(){
return (obj) -> {
if ( done ){
return String.format("<small>Done %s minutes ago<small>", obj);
}else{
return "";
}
};
}
}

View File

@ -0,0 +1,5 @@
{{#todo}}
<h2>{{title}}</h2>
<small>Created on {{createdOn}}</small>
<p>{{text}}</p>
{{/todo}}

View File

@ -0,0 +1,3 @@
<h2>{{title}}</h2>
<small>Created on {{createdOn}}</small>
<p>{{text}}</p>

View File

@ -0,0 +1,6 @@
{{#todos}}
<h2>{{title}}</h2>
{{/todos}}
{{^todos}}
<p>No todos!</p>
{{/todos}}

View File

@ -0,0 +1,3 @@
{{#todos}}
<h2>{{title}}{{#handleDone}}{{doneSince}}{{/handleDone}}</h2>
{{/todos}}

View File

@ -0,0 +1,3 @@
{{#todos}}
<h2>{{title}}</h2>
{{/todos}}

View File

@ -0,0 +1,102 @@
package com.baeldung.mustache;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.io.StringWriter;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.junit.Test;
import com.baeldung.mustache.model.Todo;
import com.github.mustachejava.Mustache;
public class TodoMustacheServiceTest {
private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException{
StringWriter writer = new StringWriter();
m.execute(writer, context).flush();
return writer.toString();
}
private String executeTemplate(Mustache m, Todo todo) throws IOException{
StringWriter writer = new StringWriter();
m.execute(writer, todo).flush();
return writer.toString();
}
@Test
public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException{
Todo todo = new Todo("Todo 1", "Todo description");
Mustache m = MustacheUtil.getMustacheFactory().compile("todo.mustache");
Map<String, Object> context = new HashMap<>();
context.put("todo", todo);
String expected = "<h2>Todo 1</h2>";
assertThat(executeTemplate(m, todo)).contains(expected);
}
@Test
public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException{
Mustache m = MustacheUtil.getMustacheFactory().compile("todo-section.mustache");
Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context)).isEmpty();
}
@Test
public void givenTodoList_whenGetHtml_thenSuccess() throws IOException{
Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache");
List<Todo> todos = Arrays.asList(
new Todo("Todo 1", "Todo description"),
new Todo("Todo 2", "Todo description another"),
new Todo("Todo 3", "Todo description another")
);
Map<String, Object> context = new HashMap<>();
context.put("todos", todos);
assertThat(executeTemplate(m, context))
.contains("<h2>Todo 1</h2>")
.contains("<h2>Todo 2</h2>")
.contains("<h2>Todo 3</h2>");
}
@Test
public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException{
Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache");
Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context)).isEmpty();;
}
@Test
public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException{
Mustache m = MustacheUtil.getMustacheFactory().compile("todos-inverted-section.mustache");
Map<String, Object> context = new HashMap<>();
assertThat(executeTemplate(m, context).trim()).isEqualTo("<p>No todos!</p>");
}
@Test
public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException{
Mustache m = MustacheUtil.getMustacheFactory().compile("todos-lambda.mustache");
List<Todo> todos = Arrays.asList(
new Todo("Todo 1", "Todo description"),
new Todo("Todo 2", "Todo description another"),
new Todo("Todo 3", "Todo description another")
);
todos.get(2).setDone(true);
todos.get(2).setCompletedOn(Date.from(Instant.now().plusSeconds(300)));
Map<String, Object> context = new HashMap<>();
context.put("todos", todos);
assertThat(executeTemplate(m, context).trim()).contains("Done 5 minutes ago");
}
}

View File

@ -100,6 +100,7 @@
<module>mockito</module>
<module>mockito2</module>
<module>mocks</module>
<module>mustache</module>
<module>orika</module>