1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.notNullValue;
8 import java.io.IOException;
9 import java.io.StringWriter;
10 import java.io.Writer;
11 import java.util.Arrays;
12 import java.util.concurrent.atomic.AtomicReference;
14 import net.pterodactylus.util.template.Part;
15 import net.pterodactylus.util.template.TemplateContext;
16 import net.pterodactylus.util.template.TemplateException;
18 import com.google.common.base.Charsets;
19 import com.google.common.io.Files;
20 import org.junit.Before;
21 import org.junit.Test;
24 * Unit test for {@link FilesystemTemplate}.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class FilesystemTemplateTest {
30 private final File tempFile;
31 private final FilesystemTemplate filesystemTemplate;
32 private final AtomicReference<StringWriter> stringWriter = new AtomicReference<StringWriter>(new StringWriter());
33 private final TemplateContext templateContext = new TemplateContext();
35 public FilesystemTemplateTest() throws IOException {
36 tempFile = File.createTempFile("template-", ".dat");
37 writeTemplate("Text");
38 filesystemTemplate = new FilesystemTemplate(tempFile.getAbsolutePath());
41 private void writeTemplate(String text) throws IOException {
42 Files.write(text + ".<%foreach values value><% value><%/foreach>", tempFile, Charsets.UTF_8);
46 public void setupTemplateContext() {
47 templateContext.set("values", Arrays.asList("a", 1));
50 @Test(expected = FilesystemTemplate.TemplateFileNotFoundException.class)
51 public void loadingTemplateFromNonExistingFileThrowsException() throws IOException {
52 FilesystemTemplate filesystemTemplate = new FilesystemTemplate("/a/b/c.dat");
53 filesystemTemplate.getInitialContext();
57 public void templateCanBeLoadedFromTheFilesystem() {
58 filesystemTemplate.render(templateContext, stringWriter.get());
59 assertThat(getRenderedString(), is("Text.a1"));
63 public void templateCanBeReloaded() throws IOException, InterruptedException {
64 filesystemTemplate.render(templateContext, stringWriter.get());
65 assertThat(getRenderedString(), is("Text.a1"));
68 filesystemTemplate.render(templateContext, stringWriter.get());
69 assertThat(getRenderedString(), is("New.a1"));
73 public void templateIsNotReloadedIfNotChanged() {
74 filesystemTemplate.render(templateContext, stringWriter.get());
75 assertThat(getRenderedString(), is("Text.a1"));
76 filesystemTemplate.render(templateContext, stringWriter.get());
77 assertThat(getRenderedString(), is("Text.a1"));
80 private String getRenderedString() {
81 String renderedString = stringWriter.get().toString();
82 stringWriter.set(new StringWriter());
83 return renderedString;
87 public void initialContextIsCopiedToReloadedTemplates() throws IOException, InterruptedException {
88 filesystemTemplate.getInitialContext().set("values", "test");
91 assertThat(filesystemTemplate.getInitialContext().get("values"), is((Object) "test"));
95 public void partsAreCopiedToReloadedTemplates() throws InterruptedException, IOException {
96 filesystemTemplate.add(new Part() {
98 public void render(TemplateContext templateContext, Writer writer) throws TemplateException {
100 writer.write(".Test");
101 } catch (IOException e) {
102 throw new TemplateException(e);
107 writeTemplate("New");
108 filesystemTemplate.render(templateContext, stringWriter.get());
109 assertThat(getRenderedString(), is("New.a1.Test"));
113 public void columnOfReturnedTemplateIsReturnedAsZero() {
114 assertThat(filesystemTemplate.getColumn(), is(0));
118 public void lineOfReturnedTemplateIsReturnedAsZero() {
119 assertThat(filesystemTemplate.getLine(), is(0));
123 public void templateCanBeIteratedOver() {
124 assertThat(filesystemTemplate.iterator(), notNullValue());