From 1de5fc5a0dab70e91a41788824251b2036aec827 Mon Sep 17 00:00:00 2001 From: isaolmez Date: Sun, 9 Jun 2019 18:10:18 +0300 Subject: [PATCH 1/4] BAEL-2981: Added code samples for Handlebars.java --- libraries-2/pom.xml | 18 +-- .../handlebars/BasicUsageUnitTest.java | 104 ++++++++++++++++++ .../handlebars/BuiltinHelperUnitTest.java | 101 +++++++++++++++++ .../handlebars/CustomHelperUnitTest.java | 54 +++++++++ .../com/baeldung/handlebars/HelperSource.java | 9 ++ .../java/com/baeldung/handlebars/Person.java | 58 ++++++++++ .../handlebars/ReusingTemplatesUnitTest.java | 44 ++++++++ libraries-2/src/test/resources/greeting.hbs | 1 + .../src/test/resources/handlebars/each.html | 3 + .../resources/handlebars/each_mustache.html | 3 + .../test/resources/handlebars/greeting.html | 1 + .../src/test/resources/handlebars/header.html | 1 + .../src/test/resources/handlebars/if.html | 5 + .../resources/handlebars/if_mustache.html | 5 + .../resources/handlebars/messagebase.html | 9 ++ .../src/test/resources/handlebars/page.html | 2 + .../src/test/resources/handlebars/person.html | 1 + .../resources/handlebars/simplemessage.html | 4 + .../src/test/resources/handlebars/with.html | 3 + .../resources/handlebars/with_mustache.html | 3 + 20 files changed, 422 insertions(+), 7 deletions(-) create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/Person.java create mode 100644 libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java create mode 100644 libraries-2/src/test/resources/greeting.hbs create mode 100644 libraries-2/src/test/resources/handlebars/each.html create mode 100644 libraries-2/src/test/resources/handlebars/each_mustache.html create mode 100644 libraries-2/src/test/resources/handlebars/greeting.html create mode 100644 libraries-2/src/test/resources/handlebars/header.html create mode 100644 libraries-2/src/test/resources/handlebars/if.html create mode 100644 libraries-2/src/test/resources/handlebars/if_mustache.html create mode 100644 libraries-2/src/test/resources/handlebars/messagebase.html create mode 100644 libraries-2/src/test/resources/handlebars/page.html create mode 100644 libraries-2/src/test/resources/handlebars/person.html create mode 100644 libraries-2/src/test/resources/handlebars/simplemessage.html create mode 100644 libraries-2/src/test/resources/handlebars/with.html create mode 100644 libraries-2/src/test/resources/handlebars/with_mustache.html diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index 8e493e2d05..686996686f 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -81,13 +81,17 @@ 3.14.2 test - - - edu.uci.ics - crawler4j - ${crawler4j.version} - - + + edu.uci.ics + crawler4j + ${crawler4j.version} + + + + com.github.jknack + handlebars + 4.1.2 + diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java new file mode 100644 index 0000000000..a2b535f4dd --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java @@ -0,0 +1,104 @@ +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; + +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 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 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; + } +} diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java new file mode 100644 index 0000000000..b44ac63b5a --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java @@ -0,0 +1,101 @@ +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; + +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

I live in World

\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

I live in World

\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("\nJava is my friend.\n" + + "\nSpring is my friend.\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("\nJava is my friend.\n" + + "\nSpring is my friend.\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

Baeldung is busy.

\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

Baeldung is busy.

\n"); + } + + private Person getPerson(String name) { + Person person = new Person(); + person.setName(name); + return person; + } +} diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java new file mode 100644 index 0000000000..ce74d55e70 --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java @@ -0,0 +1,54 @@ +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; + +public class CustomHelperUnitTest { + + private TemplateLoader templateLoader = new ClassPathTemplateLoader("/handlebars", ".html"); + + @Test + public void whenHelperIsCreated_ThenCanRegister() throws IOException { + Handlebars handlebars = new Handlebars(templateLoader); + handlebars.registerHelper("person", new Helper() { + @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; + } +} diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java b/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java new file mode 100644 index 0000000000..2b7d705af9 --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java @@ -0,0 +1,9 @@ +package com.baeldung.handlebars; + +public class HelperSource { + + public String person(Person context) { + String busyString = context.isBusy() ? "busy" : "available"; + return context.getName() + " - " + busyString; + } +} diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/Person.java b/libraries-2/src/test/java/com/baeldung/handlebars/Person.java new file mode 100644 index 0000000000..9ddc0fdffb --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/Person.java @@ -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 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 getFriends() { + return friends; + } + + public void setFriends(List friends) { + this.friends = friends; + } + + public static class Address { + + private String street; + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + } + +} diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java new file mode 100644 index 0000000000..1e2844ab1e --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java @@ -0,0 +1,44 @@ +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; + +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("

Hi Baeldung!

\n

This is the page Baeldung

"); + } + + @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\n" + + "\n" + + "\n This is the intro\n\n" + + "\n Hi there!\n\n" + + "\n" + + ""); + } +} diff --git a/libraries-2/src/test/resources/greeting.hbs b/libraries-2/src/test/resources/greeting.hbs new file mode 100644 index 0000000000..71a8266bce --- /dev/null +++ b/libraries-2/src/test/resources/greeting.hbs @@ -0,0 +1 @@ +Hi {{name}}! \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/each.html b/libraries-2/src/test/resources/handlebars/each.html new file mode 100644 index 0000000000..1570311bfc --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/each.html @@ -0,0 +1,3 @@ +{{#each friends}} +{{name}} is my friend. +{{/each}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/each_mustache.html b/libraries-2/src/test/resources/handlebars/each_mustache.html new file mode 100644 index 0000000000..1570311bfc --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/each_mustache.html @@ -0,0 +1,3 @@ +{{#each friends}} +{{name}} is my friend. +{{/each}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/greeting.html b/libraries-2/src/test/resources/handlebars/greeting.html new file mode 100644 index 0000000000..71a8266bce --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/greeting.html @@ -0,0 +1 @@ +Hi {{name}}! \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/header.html b/libraries-2/src/test/resources/handlebars/header.html new file mode 100644 index 0000000000..80cca699e4 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/header.html @@ -0,0 +1 @@ +

Hi {{name}}!

\ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/if.html b/libraries-2/src/test/resources/handlebars/if.html new file mode 100644 index 0000000000..ebdc724f66 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/if.html @@ -0,0 +1,5 @@ +{{#if busy}} +

{{name}} is busy.

+{{else}} +

{{name}} is not busy.

+{{/if}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/if_mustache.html b/libraries-2/src/test/resources/handlebars/if_mustache.html new file mode 100644 index 0000000000..d75f5fa8f9 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/if_mustache.html @@ -0,0 +1,5 @@ +{{#if busy}} +

{{name}} is busy.

+{{^}} +

{{name}} is not busy.

+{{/if}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/messagebase.html b/libraries-2/src/test/resources/handlebars/messagebase.html new file mode 100644 index 0000000000..7ee3257e06 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/messagebase.html @@ -0,0 +1,9 @@ + + +{{#block "intro"}} + This is the intro +{{/block}} +{{#block "message"}} +{{/block}} + + \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/page.html b/libraries-2/src/test/resources/handlebars/page.html new file mode 100644 index 0000000000..27b20737f3 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/page.html @@ -0,0 +1,2 @@ +{{>header}} +

This is the page {{name}}

\ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/person.html b/libraries-2/src/test/resources/handlebars/person.html new file mode 100644 index 0000000000..5207d49266 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/person.html @@ -0,0 +1 @@ +{{#person this}}{{/person}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/simplemessage.html b/libraries-2/src/test/resources/handlebars/simplemessage.html new file mode 100644 index 0000000000..3b3a01980a --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/simplemessage.html @@ -0,0 +1,4 @@ +{{#partial "message" }} + Hi there! +{{/partial}} +{{> messagebase}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/with.html b/libraries-2/src/test/resources/handlebars/with.html new file mode 100644 index 0000000000..90077b4835 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/with.html @@ -0,0 +1,3 @@ +{{#with address}} +

I live in {{street}}

+{{/with}} \ No newline at end of file diff --git a/libraries-2/src/test/resources/handlebars/with_mustache.html b/libraries-2/src/test/resources/handlebars/with_mustache.html new file mode 100644 index 0000000000..3adf6a7556 --- /dev/null +++ b/libraries-2/src/test/resources/handlebars/with_mustache.html @@ -0,0 +1,3 @@ +{{#address}} +

I live in {{street}}

+{{/address}} \ No newline at end of file From 684bc16314051832cb5f4e14efbf2c6afc50fe23 Mon Sep 17 00:00:00 2001 From: isaolmez Date: Mon, 17 Jun 2019 22:41:32 +0300 Subject: [PATCH 2/4] BAEL-2981: Added header for tests and formatted pom.xml --- libraries-2/pom.xml | 162 +++++++++--------- .../handlebars/BasicUsageUnitTest.java | 5 + .../handlebars/BuiltinHelperUnitTest.java | 5 + .../handlebars/CustomHelperUnitTest.java | 5 + .../handlebars/ReusingTemplatesUnitTest.java | 5 + 5 files changed, 101 insertions(+), 81 deletions(-) diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index 686996686f..a7aa3dc544 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -1,86 +1,86 @@ + 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"> - 4.0.0 - libraries2 - libraries2 + 4.0.0 + libraries2 + libraries2 - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - jboss-public-repository-group - JBoss Public Repository Group - http://repository.jboss.org/nexus/content/groups/public/ - - true - never - - - true - daily - - - + + + jboss-public-repository-group + JBoss Public Repository Group + http://repository.jboss.org/nexus/content/groups/public/ + + true + never + + + true + daily + + + - - - org.assertj - assertj-core - ${assertj.version} - - - io.github.classgraph - classgraph - ${classgraph.version} - - - org.jbpm - jbpm-test - ${jbpm.version} - - - info.picocli - picocli - ${picocli.version} - - - org.springframework.boot - spring-boot-starter - ${spring-boot-starter.version} - + + + org.assertj + assertj-core + ${assertj.version} + + + io.github.classgraph + classgraph + ${classgraph.version} + + + org.jbpm + jbpm-test + ${jbpm.version} + + + info.picocli + picocli + ${picocli.version} + + + org.springframework.boot + spring-boot-starter + ${spring-boot-starter.version} + - - - com.squareup.okhttp3 - okhttp - 3.14.2 - + + + com.squareup.okhttp3 + okhttp + 3.14.2 + - - com.fasterxml.jackson.core - jackson-databind - 2.9.9 - + + com.fasterxml.jackson.core + jackson-databind + 2.9.9 + - - com.google.code.gson - gson - 2.8.5 - + + com.google.code.gson + gson + 2.8.5 + - - com.squareup.okhttp3 - mockwebserver - 3.14.2 - test - + + com.squareup.okhttp3 + mockwebserver + 3.14.2 + test + edu.uci.ics crawler4j @@ -92,14 +92,14 @@ handlebars 4.1.2 - + - - 3.6.2 - 4.8.28 - 6.0.0.Final - 3.9.6 + + 3.6.2 + 4.8.28 + 6.0.0.Final + 3.9.6 4.4.0 - 2.1.4.RELEASE - + 2.1.4.RELEASE + diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java index a2b535f4dd..bf7afa0fc9 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java @@ -11,6 +11,11 @@ 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 diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java index b44ac63b5a..84529a74ce 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java @@ -9,6 +9,11 @@ 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"); diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java index ce74d55e70..18f02fe013 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java @@ -11,6 +11,11 @@ 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"); diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java index 1e2844ab1e..8741294703 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java @@ -9,6 +9,11 @@ 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"); From ca3ef57f043643bab9d1b8713d609d568e0e2e90 Mon Sep 17 00:00:00 2001 From: isaolmez Date: Sat, 22 Jun 2019 11:18:41 +0300 Subject: [PATCH 3/4] BAEL-2981: Changed helper name --- .../test/java/com/baeldung/handlebars/CustomHelperUnitTest.java | 2 +- .../src/test/java/com/baeldung/handlebars/HelperSource.java | 2 +- libraries-2/src/test/resources/handlebars/person.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java index 18f02fe013..6966d69cef 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java @@ -23,7 +23,7 @@ public class CustomHelperUnitTest { @Test public void whenHelperIsCreated_ThenCanRegister() throws IOException { Handlebars handlebars = new Handlebars(templateLoader); - handlebars.registerHelper("person", new Helper() { + handlebars.registerHelper("isBusy", new Helper() { @Override public Object apply(Person context, Options options) throws IOException { String busyString = context.isBusy() ? "busy" : "available"; diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java b/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java index 2b7d705af9..b98786c029 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/HelperSource.java @@ -2,7 +2,7 @@ package com.baeldung.handlebars; public class HelperSource { - public String person(Person context) { + public String isBusy(Person context) { String busyString = context.isBusy() ? "busy" : "available"; return context.getName() + " - " + busyString; } diff --git a/libraries-2/src/test/resources/handlebars/person.html b/libraries-2/src/test/resources/handlebars/person.html index 5207d49266..9df7850f60 100644 --- a/libraries-2/src/test/resources/handlebars/person.html +++ b/libraries-2/src/test/resources/handlebars/person.html @@ -1 +1 @@ -{{#person this}}{{/person}} \ No newline at end of file +{{#isBusy this}}{{/isBusy}} \ No newline at end of file From 2e2af71a58fff5ad98068e4c332d72438420e732 Mon Sep 17 00:00:00 2001 From: isaolmez Date: Mon, 24 Jun 2019 23:13:05 +0300 Subject: [PATCH 4/4] BAEL-2981: Updated tests --- .../com/baeldung/handlebars/BasicUsageUnitTest.java | 12 ++++++------ .../baeldung/handlebars/BuiltinHelperUnitTest.java | 12 ++++++------ .../baeldung/handlebars/CustomHelperUnitTest.java | 4 ++-- .../handlebars/ReusingTemplatesUnitTest.java | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java index bf7afa0fc9..3eb325dbb6 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BasicUsageUnitTest.java @@ -32,9 +32,9 @@ public class BasicUsageUnitTest { public void whenParameterMapIsSupplied_thenDisplays() throws IOException { Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline("Hi {{name}}!"); - Map parameterMap = new HashMap<>(); parameterMap.put("name", "Baeldung"); + String templateString = template.apply(parameterMap); assertThat(templateString).isEqualTo("Hi Baeldung!"); @@ -44,9 +44,9 @@ public class BasicUsageUnitTest { 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!"); @@ -56,10 +56,10 @@ public class BasicUsageUnitTest { public void whenMultipleParametersAreSupplied_ThenDisplays() throws IOException { Handlebars handlebars = new Handlebars(); Template template = handlebars.compileInline("Hi {{name}}! This is {{topic}}."); - Map parameterMap = new HashMap<>(); parameterMap.put("name", "Baeldung"); parameterMap.put("topic", "Handlebars"); + String templateString = template.apply(parameterMap); assertThat(templateString).isEqualTo("Hi Baeldung! This is Handlebars."); @@ -69,8 +69,8 @@ public class BasicUsageUnitTest { 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!"); @@ -81,8 +81,8 @@ public class BasicUsageUnitTest { 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!"); @@ -94,8 +94,8 @@ public class BasicUsageUnitTest { 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!"); diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java index 84529a74ce..6749f7fe0a 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/BuiltinHelperUnitTest.java @@ -22,9 +22,9 @@ public class BuiltinHelperUnitTest { 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

I live in World

\n"); @@ -34,9 +34,9 @@ public class BuiltinHelperUnitTest { 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

I live in World

\n"); @@ -46,12 +46,12 @@ public class BuiltinHelperUnitTest { 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("\nJava is my friend.\n" @@ -62,12 +62,12 @@ public class BuiltinHelperUnitTest { 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("\nJava is my friend.\n" @@ -78,9 +78,9 @@ public class BuiltinHelperUnitTest { 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

Baeldung is busy.

\n"); @@ -90,9 +90,9 @@ public class BuiltinHelperUnitTest { 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

Baeldung is busy.

\n"); diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java index 6966d69cef..a3c6667654 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/CustomHelperUnitTest.java @@ -31,8 +31,8 @@ public class CustomHelperUnitTest { } }); Template template = handlebars.compile("person"); - Person person = getPerson("Baeldung"); + String templateString = template.apply(person); assertThat(templateString).isEqualTo("Baeldung - busy"); @@ -43,8 +43,8 @@ public class CustomHelperUnitTest { 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"); diff --git a/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java index 8741294703..36f78f486e 100644 --- a/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/handlebars/ReusingTemplatesUnitTest.java @@ -22,9 +22,9 @@ public class ReusingTemplatesUnitTest { 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("

Hi Baeldung!

\n

This is the page Baeldung

"); @@ -34,9 +34,9 @@ public class ReusingTemplatesUnitTest { 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\n"