Make test timezone-independent
[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 import static org.hamcrest.Matchers.notNullValue;
6
7 import java.io.File;
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;
13
14 import net.pterodactylus.util.template.Part;
15 import net.pterodactylus.util.template.TemplateContext;
16 import net.pterodactylus.util.template.TemplateException;
17
18 import com.google.common.base.Charsets;
19 import com.google.common.io.Files;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 /**
24  * Unit test for {@link FilesystemTemplate}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class FilesystemTemplateTest {
29
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();
34
35         public FilesystemTemplateTest() throws IOException {
36                 tempFile = File.createTempFile("template-", ".dat");
37                 writeTemplate("Text");
38                 filesystemTemplate = new FilesystemTemplate(tempFile.getAbsolutePath());
39         }
40
41         private void writeTemplate(String text) throws IOException {
42                 Files.write(text + ".<%foreach values value><% value><%/foreach>", tempFile, Charsets.UTF_8);
43         }
44
45         @Before
46         public void setupTemplateContext() {
47                 templateContext.set("values", Arrays.asList("a", 1));
48         }
49
50         @Test(expected = FilesystemTemplate.TemplateFileNotFoundException.class)
51         public void loadingTemplateFromNonExistingFileThrowsException() throws IOException {
52                 FilesystemTemplate filesystemTemplate = new FilesystemTemplate("/a/b/c.dat");
53                 filesystemTemplate.getInitialContext();
54         }
55
56         @Test
57         public void templateCanBeLoadedFromTheFilesystem() {
58                 filesystemTemplate.render(templateContext, stringWriter.get());
59                 assertThat(getRenderedString(), is("Text.a1"));
60         }
61
62         @Test
63         public void templateCanBeReloaded() throws IOException, InterruptedException {
64                 filesystemTemplate.render(templateContext, stringWriter.get());
65                 assertThat(getRenderedString(), is("Text.a1"));
66                 Thread.sleep(1000);
67                 writeTemplate("New");
68                 filesystemTemplate.render(templateContext, stringWriter.get());
69                 assertThat(getRenderedString(), is("New.a1"));
70         }
71
72         @Test
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"));
78         }
79
80         private String getRenderedString() {
81                 String renderedString = stringWriter.get().toString();
82                 stringWriter.set(new StringWriter());
83                 return renderedString;
84         }
85
86         @Test
87         public void initialContextIsCopiedToReloadedTemplates() throws IOException, InterruptedException {
88                 filesystemTemplate.getInitialContext().set("values", "test");
89                 Thread.sleep(1000);
90                 writeTemplate("New");
91                 assertThat(filesystemTemplate.getInitialContext().get("values"), is((Object) "test"));
92         }
93
94         @Test
95         public void partsAreCopiedToReloadedTemplates() throws InterruptedException, IOException {
96                 filesystemTemplate.add(new Part() {
97                         @Override
98                         public void render(TemplateContext templateContext, Writer writer) throws TemplateException {
99                                 try {
100                                         writer.write(".Test");
101                                 } catch (IOException e) {
102                                         throw new TemplateException(e);
103                                 }
104                         }
105                 });
106                 Thread.sleep(1000);
107                 writeTemplate("New");
108                 filesystemTemplate.render(templateContext, stringWriter.get());
109                 assertThat(getRenderedString(), is("New.a1.Test"));
110         }
111
112         @Test
113         public void columnOfReturnedTemplateIsReturnedAsZero() {
114                 assertThat(filesystemTemplate.getColumn(), is(0));
115         }
116
117         @Test
118         public void lineOfReturnedTemplateIsReturnedAsZero() {
119                 assertThat(filesystemTemplate.getLine(), is(0));
120         }
121
122         @Test
123         public void templateCanBeIteratedOver() {
124             assertThat(filesystemTemplate.iterator(), notNullValue());
125         }
126
127 }