Add test for create album page
[Sone.git] / src / test / java / net / pterodactylus / sone / template / FilesystemTemplateTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.StringWriter;
9 import java.io.Writer;
10 import java.util.Arrays;
11 import java.util.concurrent.atomic.AtomicReference;
12
13 import net.pterodactylus.util.template.Part;
14 import net.pterodactylus.util.template.TemplateContext;
15 import net.pterodactylus.util.template.TemplateException;
16
17 import com.google.common.base.Charsets;
18 import com.google.common.io.Files;
19 import org.junit.Before;
20 import org.junit.Test;
21
22 /**
23  * Unit test for {@link FilesystemTemplate}.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class FilesystemTemplateTest {
28
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();
33
34         public FilesystemTemplateTest() throws IOException {
35                 tempFile = File.createTempFile("template-", ".dat");
36                 writeTemplate("Text");
37                 filesystemTemplate = new FilesystemTemplate(tempFile.getAbsolutePath());
38         }
39
40         private void writeTemplate(String text) throws IOException {
41                 Files.write(text + ".<%foreach values value><% value><%/foreach>", tempFile, Charsets.UTF_8);
42         }
43
44         @Before
45         public void setupTemplateContext() {
46                 templateContext.set("values", Arrays.asList("a", 1));
47         }
48
49         @Test(expected = FilesystemTemplate.TemplateFileNotFoundException.class)
50         public void loadingTemplateFromNonExistingFileThrowsException() throws IOException {
51                 FilesystemTemplate filesystemTemplate = new FilesystemTemplate("/a/b/c.dat");
52                 filesystemTemplate.getInitialContext();
53         }
54
55         @Test
56         public void templateCanBeLoadedFromTheFilesystem() {
57                 filesystemTemplate.render(templateContext, stringWriter.get());
58                 assertThat(getRenderedString(), is("Text.a1"));
59         }
60
61         @Test
62         public void templateCanBeReloaded() throws IOException, InterruptedException {
63                 filesystemTemplate.render(templateContext, stringWriter.get());
64                 assertThat(getRenderedString(), is("Text.a1"));
65                 Thread.sleep(1000);
66                 writeTemplate("New");
67                 filesystemTemplate.render(templateContext, stringWriter.get());
68                 assertThat(getRenderedString(), is("New.a1"));
69         }
70
71         @Test
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"));
77         }
78
79         private String getRenderedString() {
80                 String renderedString = stringWriter.get().toString();
81                 stringWriter.set(new StringWriter());
82                 return renderedString;
83         }
84
85         @Test
86         public void initialContextIsCopiedToReloadedTemplates() throws IOException, InterruptedException {
87                 filesystemTemplate.getInitialContext().set("values", "test");
88                 Thread.sleep(1000);
89                 writeTemplate("New");
90                 assertThat(filesystemTemplate.getInitialContext().get("values"), is((Object) "test"));
91         }
92
93         @Test
94         public void partsAreCopiedToReloadedTemplates() throws InterruptedException, IOException {
95                 filesystemTemplate.add(new Part() {
96                         @Override
97                         public void render(TemplateContext templateContext, Writer writer) throws TemplateException {
98                                 try {
99                                         writer.write(".Test");
100                                 } catch (IOException e) {
101                                         throw new TemplateException(e);
102                                 }
103                         }
104                 });
105                 Thread.sleep(1000);
106                 writeTemplate("New");
107                 filesystemTemplate.render(templateContext, stringWriter.get());
108                 assertThat(getRenderedString(), is("New.a1.Test"));
109         }
110
111 }