1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
7 import java.io.IOException;
8 import java.io.StringWriter;
10 import java.util.Arrays;
11 import java.util.concurrent.atomic.AtomicReference;
13 import net.pterodactylus.util.template.Part;
14 import net.pterodactylus.util.template.TemplateContext;
15 import net.pterodactylus.util.template.TemplateException;
17 import com.google.common.base.Charsets;
18 import com.google.common.io.Files;
19 import org.junit.Before;
20 import org.junit.Test;
23 * Unit test for {@link FilesystemTemplate}.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class FilesystemTemplateTest {
29 private final File tempFile;
30 private final FilesystemTemplate filesystemTemplate;
31 private final AtomicReference<StringWriter> stringWriter = new AtomicReference<StringWriter>(new StringWriter());
32 private final TemplateContext templateContext = new TemplateContext();
34 public FilesystemTemplateTest() throws IOException {
35 tempFile = File.createTempFile("template-", ".dat");
36 writeTemplate("Text");
37 filesystemTemplate = new FilesystemTemplate(tempFile.getAbsolutePath());
40 private void writeTemplate(String text) throws IOException {
41 Files.write(text + ".<%foreach values value><% value><%/foreach>", tempFile, Charsets.UTF_8);
45 public void setupTemplateContext() {
46 templateContext.set("values", Arrays.asList("a", 1));
49 @Test(expected = FilesystemTemplate.TemplateFileNotFoundException.class)
50 public void loadingTemplateFromNonExistingFileThrowsException() throws IOException {
51 FilesystemTemplate filesystemTemplate = new FilesystemTemplate("/a/b/c.dat");
52 filesystemTemplate.getInitialContext();
56 public void templateCanBeLoadedFromTheFilesystem() {
57 filesystemTemplate.render(templateContext, stringWriter.get());
58 assertThat(getRenderedString(), is("Text.a1"));
62 public void templateCanBeReloaded() throws IOException, InterruptedException {
63 filesystemTemplate.render(templateContext, stringWriter.get());
64 assertThat(getRenderedString(), is("Text.a1"));
67 filesystemTemplate.render(templateContext, stringWriter.get());
68 assertThat(getRenderedString(), is("New.a1"));
72 public void templateIsNotReloadedIfNotChanged() {
73 filesystemTemplate.render(templateContext, stringWriter.get());
74 assertThat(getRenderedString(), is("Text.a1"));
75 filesystemTemplate.render(templateContext, stringWriter.get());
76 assertThat(getRenderedString(), is("Text.a1"));
79 private String getRenderedString() {
80 String renderedString = stringWriter.get().toString();
81 stringWriter.set(new StringWriter());
82 return renderedString;
86 public void initialContextIsCopiedToReloadedTemplates() throws IOException, InterruptedException {
87 filesystemTemplate.getInitialContext().set("values", "test");
90 assertThat(filesystemTemplate.getInitialContext().get("values"), is((Object) "test"));
94 public void partsAreCopiedToReloadedTemplates() throws InterruptedException, IOException {
95 filesystemTemplate.add(new Part() {
97 public void render(TemplateContext templateContext, Writer writer) throws TemplateException {
99 writer.write(".Test");
100 } catch (IOException e) {
101 throw new TemplateException(e);
106 writeTemplate("New");
107 filesystemTemplate.render(templateContext, stringWriter.get());
108 assertThat(getRenderedString(), is("New.a1.Test"));