Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-14274-19

This commit is contained in:
amit2103 2019-06-30 00:28:46 +05:30
commit b9ca1c4f4b
20 changed files with 437 additions and 0 deletions

View File

@ -124,6 +124,12 @@
<version>${crawler4j.version}</version>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.1.2</version>
</dependency>
<!-- Benchmarking -->
<dependency>
<groupId>org.openjdk.jmh</groupId>

View File

@ -0,0 +1,109 @@
package com.baeldung.handlebars;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
/**
* Showcases the tag usage and different template loading mechanisms.
*
* @author isaolmez
*/
public class BasicUsageUnitTest {
@Test
public void whenThereIsNoTemplateFile_ThenCompilesInline() throws IOException {
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hi {{this}}!");
String templateString = template.apply("Baeldung");
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
@Test
public void whenParameterMapIsSupplied_thenDisplays() throws IOException {
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hi {{name}}!");
Map<String, String> parameterMap = new HashMap<>();
parameterMap.put("name", "Baeldung");
String templateString = template.apply(parameterMap);
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
@Test
public void whenParameterObjectIsSupplied_ThenDisplays() throws IOException {
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hi {{name}}!");
Person person = new Person();
person.setName("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
@Test
public void whenMultipleParametersAreSupplied_ThenDisplays() throws IOException {
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline("Hi {{name}}! This is {{topic}}.");
Map<String, String> parameterMap = new HashMap<>();
parameterMap.put("name", "Baeldung");
parameterMap.put("topic", "Handlebars");
String templateString = template.apply(parameterMap);
assertThat(templateString).isEqualTo("Hi Baeldung! This is Handlebars.");
}
@Test
public void whenNoLoaderIsGiven_ThenSearchesClasspath() throws IOException {
Handlebars handlebars = new Handlebars();
Template template = handlebars.compile("greeting");
Person person = getPerson("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
@Test
public void whenClasspathTemplateLoaderIsGiven_ThenSearchesClasspathWithPrefixSuffix() throws IOException {
TemplateLoader loader = new ClassPathTemplateLoader("/handlebars", ".html");
Handlebars handlebars = new Handlebars(loader);
Template template = handlebars.compile("greeting");
Person person = getPerson("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
@Test
public void whenMultipleLoadersAreGiven_ThenSearchesSequentially() throws IOException {
TemplateLoader firstLoader = new ClassPathTemplateLoader("/handlebars", ".html");
TemplateLoader secondLoader = new ClassPathTemplateLoader("/templates", ".html");
Handlebars handlebars = new Handlebars().with(firstLoader, secondLoader);
Template template = handlebars.compile("greeting");
Person person = getPerson("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Hi Baeldung!");
}
private Person getPerson(String name) {
Person person = new Person();
person.setName(name);
return person;
}
}

View File

@ -0,0 +1,106 @@
package com.baeldung.handlebars;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import java.io.IOException;
import org.junit.Test;
/**
* Showcases the built-in template helpers.
*
* @author isaolmez
*/
public class BuiltinHelperUnitTest {
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
@Test
public void whenUsedWith_ThenContextChanges() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("with");
Person person = getPerson("Baeldung");
person.getAddress().setStreet("World");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<h4>I live in World</h4>\n");
}
@Test
public void whenUsedWithMustacheStyle_ThenContextChanges() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("with_mustache");
Person person = getPerson("Baeldung");
person.getAddress().setStreet("World");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<h4>I live in World</h4>\n");
}
@Test
public void whenUsedEach_ThenIterates() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("each");
Person person = getPerson("Baeldung");
Person friend1 = getPerson("Java");
Person friend2 = getPerson("Spring");
person.getFriends().add(friend1);
person.getFriends().add(friend2);
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<span>Java is my friend.</span>\n"
+ "\n<span>Spring is my friend.</span>\n");
}
@Test
public void whenUsedEachMustacheStyle_ThenIterates() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("each_mustache");
Person person = getPerson("Baeldung");
Person friend1 = getPerson("Java");
Person friend2 = getPerson("Spring");
person.getFriends().add(friend1);
person.getFriends().add(friend2);
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<span>Java is my friend.</span>\n"
+ "\n<span>Spring is my friend.</span>\n");
}
@Test
public void whenUsedIf_ThenPutsCondition() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("if");
Person person = getPerson("Baeldung");
person.setBusy(true);
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<h4>Baeldung is busy.</h4>\n");
}
@Test
public void whenUsedIfMustacheStyle_ThenPutsCondition() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("if_mustache");
Person person = getPerson("Baeldung");
person.setBusy(true);
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<h4>Baeldung is busy.</h4>\n");
}
private Person getPerson(String name) {
Person person = new Person();
person.setName(name);
return person;
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.handlebars;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import java.io.IOException;
import org.junit.Test;
/**
* Showcases implementing a custom template helper.
*
* @author isaolmez
*/
public class CustomHelperUnitTest {
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
@Test
public void whenHelperIsCreated_ThenCanRegister() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
handlebars.registerHelper("isBusy", new Helper<Person>() {
@Override
public Object apply(Person context, Options options) throws IOException {
String busyString = context.isBusy() ? "busy" : "available";
return context.getName() + " - " + busyString;
}
});
Template template = handlebars.compile("person");
Person person = getPerson("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Baeldung - busy");
}
@Test
public void whenHelperSourceIsCreated_ThenCanRegister() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
handlebars.registerHelpers(new HelperSource());
Template template = handlebars.compile("person");
Person person = getPerson("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("Baeldung - busy");
}
private Person getPerson(String name) {
Person person = new Person();
person.setName(name);
person.setBusy(true);
return person;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.handlebars;
public class HelperSource {
public String isBusy(Person context) {
String busyString = context.isBusy() ? "busy" : "available";
return context.getName() + " - " + busyString;
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.handlebars;
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private boolean busy;
private Address address = new Address();
private List<Person> friends = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isBusy() {
return busy;
}
public void setBusy(boolean busy) {
this.busy = busy;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Person> getFriends() {
return friends;
}
public void setFriends(List<Person> friends) {
this.friends = friends;
}
public static class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.handlebars;
import static org.assertj.core.api.Assertions.assertThat;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import java.io.IOException;
import org.junit.Test;
/**
* Showcases reusing the existing templates.
*
* @author isaolmez
*/
public class ReusingTemplatesUnitTest {
private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html");
@Test
public void whenOtherTemplateIsReferenced_ThenCanReuse() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("page");
Person person = new Person();
person.setName("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("<h4>Hi Baeldung!</h4>\n<p>This is the page Baeldung</p>");
}
@Test
public void whenBlockIsDefined_ThenCanOverrideWithPartial() throws IOException {
Handlebars handlebars = new Handlebars(templateLoader);
Template template = handlebars.compile("simplemessage");
Person person = new Person();
person.setName("Baeldung");
String templateString = template.apply(person);
assertThat(templateString).isEqualTo("\n<html>\n"
+ "<body>\n"
+ "\n This is the intro\n\n"
+ "\n Hi there!\n\n"
+ "</body>\n"
+ "</html>");
}
}

View File

@ -0,0 +1 @@
Hi {{name}}!

View File

@ -0,0 +1,3 @@
{{#each friends}}
<span>{{name}} is my friend.</span>
{{/each}}

View File

@ -0,0 +1,3 @@
{{#each friends}}
<span>{{name}} is my friend.</span>
{{/each}}

View File

@ -0,0 +1 @@
Hi {{name}}!

View File

@ -0,0 +1 @@
<h4>Hi {{name}}!</h4>

View File

@ -0,0 +1,5 @@
{{#if busy}}
<h4>{{name}} is busy.</h4>
{{else}}
<h4>{{name}} is not busy.</h4>
{{/if}}

View File

@ -0,0 +1,5 @@
{{#if busy}}
<h4>{{name}} is busy.</h4>
{{^}}
<h4>{{name}} is not busy.</h4>
{{/if}}

View File

@ -0,0 +1,9 @@
<html>
<body>
{{#block "intro"}}
This is the intro
{{/block}}
{{#block "message"}}
{{/block}}
</body>
</html>

View File

@ -0,0 +1,2 @@
{{>header}}
<p>This is the page {{name}}</p>

View File

@ -0,0 +1 @@
{{#isBusy this}}{{/isBusy}}

View File

@ -0,0 +1,4 @@
{{#partial "message" }}
Hi there!
{{/partial}}
{{> messagebase}}

View File

@ -0,0 +1,3 @@
{{#with address}}
<h4>I live in {{street}}</h4>
{{/with}}

View File

@ -0,0 +1,3 @@
{{#address}}
<h4>I live in {{street}}</h4>
{{/address}}