Merge branch 'master' into master
This commit is contained in:
@@ -4,4 +4,4 @@
|
||||
- [A Guide to jBPM with Java](https://www.baeldung.com/jbpm-java)
|
||||
- [Guide to Classgraph Library](https://www.baeldung.com/classgraph)
|
||||
- [Create a Java Command Line Program with Picocli](https://www.baeldung.com/java-picocli-create-command-line-program)
|
||||
|
||||
- [Guide to Java Parallel Collectors Library](https://www.baeldung.com/java-parallel-collectors)
|
||||
|
||||
+133
-130
@@ -1,65 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>libraries2</artifactId>
|
||||
<name>libraries2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-public-repository-group</id>
|
||||
<name>JBoss Public Repository Group</name>
|
||||
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
<version>${classgraph.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jbpm</groupId>
|
||||
<artifactId>jbpm-test</artifactId>
|
||||
<version>${jbpm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>${picocli.version}</version>
|
||||
</dependency>
|
||||
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>
|
||||
<artifactId>libraries2</artifactId>
|
||||
<name>libraries2</name>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-public-repository-group</id>
|
||||
<name>JBoss Public Repository Group</name>
|
||||
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mapdb</groupId>
|
||||
<artifactId>mapdb</artifactId>
|
||||
<version>${mapdb.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pivovarit</groupId>
|
||||
<artifactId>parallel-collectors</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
<version>${classgraph.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jbpm</groupId>
|
||||
<artifactId>jbpm-test</artifactId>
|
||||
<version>${jbpm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>${picocli.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ejml</groupId>
|
||||
<artifactId>ejml-all</artifactId>
|
||||
<version>${ejml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nd4j</groupId>
|
||||
<artifactId>nd4j-native</artifactId>
|
||||
<version>${nd4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nd4j</groupId>
|
||||
<artifactId>nd4j-native</artifactId>
|
||||
<version>${nd4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.la4j</groupId>
|
||||
<artifactId>la4j</artifactId>
|
||||
@@ -70,88 +76,85 @@
|
||||
<artifactId>colt</artifactId>
|
||||
<version>${colt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.openhft</groupId>
|
||||
<artifactId>chronicle-map</artifactId>
|
||||
<version>${chronicle.map.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.sun.java</groupId>
|
||||
<artifactId>tools</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Dependencies for response decoder with okhttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.openhft</groupId>
|
||||
<artifactId>chronicle-map</artifactId>
|
||||
<version>${chronicle.map.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.sun.java</groupId>
|
||||
<artifactId>tools</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- Dependencies for response decoder with okhttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Benchmarking -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.jknack</groupId>
|
||||
<artifactId>handlebars</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
<!-- Benchmarking -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.mesos</groupId>
|
||||
<artifactId>mesos</artifactId>
|
||||
<version>${mesos.library.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.2</assertj.version>
|
||||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<chronicle.map.version>3.17.2</chronicle.map.version>
|
||||
<crawler4j.version>4.4.0</crawler4j.version>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
<mesos.library.version>0.28.3</mesos.library.version>
|
||||
<ejml.version>0.38</ejml.version>
|
||||
<nd4j.version>1.0.0-beta4</nd4j.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<la4j.version>0.6.0</la4j.version>
|
||||
<jmh.version>1.19</jmh.version>
|
||||
</properties>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<mapdb.version>3.0.7</mapdb.version>
|
||||
<assertj.version>3.6.2</assertj.version>
|
||||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<chronicle.map.version>3.17.2</chronicle.map.version>
|
||||
<crawler4j.version>4.4.0</crawler4j.version>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
<ejml.version>0.38</ejml.version>
|
||||
<nd4j.version>1.0.0-beta4</nd4j.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<la4j.version>0.6.0</la4j.version>
|
||||
<jmh.version>1.19</jmh.version>
|
||||
<mesos.library.version>0.28.3</mesos.library.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import java.util.NavigableSet;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSetCreatedInDB_whenMultipleElementsAdded_checkOnlyOneExists() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
NavigableSet<String> set = db.
|
||||
treeSet("mySet")
|
||||
.serializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
String myString = "Baeldung!";
|
||||
|
||||
set.add(myString);
|
||||
set.add(myString);
|
||||
|
||||
assertEquals(1, set.size());
|
||||
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.mapdb.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class HTreeMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidDB_whenHTreeMapAddedToAndRetrieved_CheckedRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
HTreeMap<String, String> hTreeMap = db
|
||||
.hashMap("myTreMap")
|
||||
.keySerializer(Serializer.STRING)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.create();
|
||||
|
||||
hTreeMap.put("key1", "value1");
|
||||
hTreeMap.put("key2", "value2");
|
||||
|
||||
assertEquals(2, hTreeMap.size());
|
||||
|
||||
//add another value with the same key
|
||||
|
||||
hTreeMap.put("key1", "value3");
|
||||
|
||||
assertEquals(2, hTreeMap.size());
|
||||
assertEquals("value3", hTreeMap.get("key1"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.HTreeMap;
|
||||
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class HelloBaeldungUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInMemoryDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
String welcomeMessageKey = "Welcome Message";
|
||||
String welcomeMessageString = "Hello Baeldung!";
|
||||
|
||||
HTreeMap myMap = db.hashMap("myMap").createOrOpen();
|
||||
myMap.put(welcomeMessageKey, welcomeMessageString);
|
||||
|
||||
String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);
|
||||
|
||||
db.close();
|
||||
|
||||
assertEquals(welcomeMessageString, welcomeMessageFromDB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFileDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.fileDB("file.db").make();
|
||||
|
||||
String welcomeMessageKey = "Welcome Message";
|
||||
String welcomeMessageString = "Hello Baeldung!";
|
||||
|
||||
HTreeMap myMap = db.hashMap("myMap").createOrOpen();
|
||||
myMap.put(welcomeMessageKey, welcomeMessageString);
|
||||
|
||||
String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);
|
||||
|
||||
db.close();
|
||||
|
||||
assertEquals(welcomeMessageString, welcomeMessageFromDB);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.HTreeMap;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class InMemoryModesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedOnHeap_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.heapDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedBaseOnByteArray_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.memoryDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedBaseOnDirectByteBuffer_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.memoryDirectDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.Serializer;
|
||||
import org.mapdb.SortedTableMap;
|
||||
import org.mapdb.volume.MappedFileVol;
|
||||
import org.mapdb.volume.Volume;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class SortedTableMapUnitTest {
|
||||
|
||||
private static final String VOLUME_LOCATION = "sortedTableMapVol.db";
|
||||
|
||||
@Test
|
||||
public void givenValidSortedTableMapSetup_whenQueried_checkValuesCorrect() {
|
||||
|
||||
//create memory mapped volume, readonly false
|
||||
Volume vol = MappedFileVol.FACTORY.makeVolume(VOLUME_LOCATION, false);
|
||||
|
||||
//create sink to feed the map with data
|
||||
SortedTableMap.Sink<Integer, String> sink =
|
||||
SortedTableMap.create(
|
||||
vol,
|
||||
Serializer.INTEGER,
|
||||
Serializer.STRING
|
||||
).createFromSink();
|
||||
|
||||
//add content
|
||||
for(int i = 0; i < 100; i++){
|
||||
sink.put(i, "Value " + Integer.toString(i));
|
||||
}
|
||||
|
||||
sink.create();
|
||||
|
||||
//now open in read-only mode
|
||||
Volume openVol = MappedFileVol.FACTORY.makeVolume(VOLUME_LOCATION, true);
|
||||
|
||||
SortedTableMap<Integer, String> sortedTableMap = SortedTableMap.open(
|
||||
openVol,
|
||||
Serializer.INTEGER,
|
||||
Serializer.STRING
|
||||
);
|
||||
|
||||
assertEquals(100, sortedTableMap.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import java.util.NavigableSet;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class TransactionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidDBSetup_whenTransactionCommittedAndRolledBack_checkPreviousStateAchieved() {
|
||||
|
||||
DB db = DBMaker.memoryDB().transactionEnable().make();
|
||||
|
||||
NavigableSet<String> set = db
|
||||
.treeSet("mySet")
|
||||
.serializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
set.add("One");
|
||||
set.add("Two");
|
||||
|
||||
db.commit();
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
set.add("Three");
|
||||
|
||||
assertEquals(3, set.size());
|
||||
|
||||
db.rollback();
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
package com.baeldung.parallel_collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallel;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelOrdered;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToCollection;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToList;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToMap;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToStream;
|
||||
|
||||
public class ParallelCollectorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldProcessInParallelWithStreams() {
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.parallelStream()
|
||||
.map(i -> fetchById(i))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
System.out.println(results);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProcessInParallelWithParallelCollectors() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
CompletableFuture<List<String>> results = ids.stream()
|
||||
.collect(parallelToList(i -> fetchById(i), executor, 4));
|
||||
|
||||
System.out.println(results.join());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToList() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToList(i -> fetchById(i), executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToCollection() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToCollection(i -> fetchById(i), LinkedList::new, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToStream() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, List<String>> results = ids.stream()
|
||||
.collect(parallelToStream(i -> fetchById(i), executor, 4))
|
||||
.thenApply(stream -> stream.collect(Collectors.groupingBy(i -> i.length())))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStreamInCompletionOrder() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallel(i -> fetchByIdWithRandomDelay(i), executor, 4))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStreamInOriginalOrder() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallelOrdered(i -> fetchByIdWithRandomDelay(i), executor, 4))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToMap() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, i -> fetchById(i), executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToTreeMap() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, i -> fetchById(i), TreeMap::new, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToTreeMapAndResolveClashes() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, i -> fetchById(i), TreeMap::new, (s1, s2) -> s1, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
private static String fetchById(int id) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore shamelessly
|
||||
}
|
||||
|
||||
return "user-" + id;
|
||||
}
|
||||
|
||||
private static String fetchByIdWithRandomDelay(int id) {
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000));
|
||||
} catch (InterruptedException e) {
|
||||
// ignore shamelessly
|
||||
}
|
||||
|
||||
return "user-" + id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Hi {{name}}!
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#each friends}}
|
||||
<span>{{name}} is my friend.</span>
|
||||
{{/each}}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#each friends}}
|
||||
<span>{{name}} is my friend.</span>
|
||||
{{/each}}
|
||||
@@ -0,0 +1 @@
|
||||
Hi {{name}}!
|
||||
@@ -0,0 +1 @@
|
||||
<h4>Hi {{name}}!</h4>
|
||||
@@ -0,0 +1,5 @@
|
||||
{{#if busy}}
|
||||
<h4>{{name}} is busy.</h4>
|
||||
{{else}}
|
||||
<h4>{{name}} is not busy.</h4>
|
||||
{{/if}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{#if busy}}
|
||||
<h4>{{name}} is busy.</h4>
|
||||
{{^}}
|
||||
<h4>{{name}} is not busy.</h4>
|
||||
{{/if}}
|
||||
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<body>
|
||||
{{#block "intro"}}
|
||||
This is the intro
|
||||
{{/block}}
|
||||
{{#block "message"}}
|
||||
{{/block}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,2 @@
|
||||
{{>header}}
|
||||
<p>This is the page {{name}}</p>
|
||||
@@ -0,0 +1 @@
|
||||
{{#isBusy this}}{{/isBusy}}
|
||||
@@ -0,0 +1,4 @@
|
||||
{{#partial "message" }}
|
||||
Hi there!
|
||||
{{/partial}}
|
||||
{{> messagebase}}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#with address}}
|
||||
<h4>I live in {{street}}</h4>
|
||||
{{/with}}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#address}}
|
||||
<h4>I live in {{street}}</h4>
|
||||
{{/address}}
|
||||
Reference in New Issue
Block a user