1 package net.pterodactylus.sone.main;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.startsWith;
6 import static org.mockito.Mockito.mock;
8 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.StringWriter;
14 import java.net.URISyntaxException;
16 import net.pterodactylus.sone.web.page.FreenetRequest;
17 import net.pterodactylus.util.template.Template;
18 import net.pterodactylus.util.template.TemplateContext;
19 import net.pterodactylus.util.template.TemplateProvider;
20 import net.pterodactylus.util.web.Method;
21 import net.pterodactylus.util.web.Page;
22 import net.pterodactylus.util.web.Response;
24 import freenet.clients.http.ToadletContext;
25 import freenet.support.api.HTTPRequest;
27 import com.google.common.base.Charsets;
28 import com.google.common.io.Files;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
35 * Unit test for {@link DebugLoaders}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class DebugLoadersTest {
42 public final TemporaryFolder temporaryFolder = new TemporaryFolder();
44 private final StringWriter stringWriter = new StringWriter();
45 private final TemplateContext templateContext = new TemplateContext();
46 private Loaders loaders;
49 public void setupLoader() throws IOException {
50 String templatePath = temporaryFolder.newFolder("temps").getPath();
51 loaders = new DebugLoaders(templatePath);
52 File templateFile = new File(templatePath, "template.txt");
53 Files.write("<%if foo>foo<%else>bar<%/if>", templateFile, Charsets.UTF_8);
54 new File(templatePath, "templates").mkdir();
55 File secondTemplateFile = new File(templatePath, "templates/template.txt");
56 Files.write("<%if foo>foo<%else>bar<%/if>", secondTemplateFile, Charsets.UTF_8);
60 public void debugLoaderCanLoadTemplatesFromFilesystem() throws IOException {
61 Template template = loaders.loadTemplate("/template.txt");
62 template.render(templateContext, stringWriter);
63 assertThat(stringWriter.toString(), is("bar"));
67 public void staticPageIsServedFromFilesystem() throws URISyntaxException, IOException {
68 Page<FreenetRequest> page = loaders.loadStaticPage("text/", "", "text/plain");
69 URI uri = new URI("http://some.host/text/template.txt");
70 Method method = Method.GET;
71 HTTPRequest httpRequest = mock(HTTPRequest.class);
72 ToadletContext toadletContext = mock(ToadletContext.class);
73 FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext);
74 OutputStream outputStream = new ByteArrayOutputStream();
75 Response response = new Response(outputStream);
76 page.handleRequest(request, response);
77 assertThat(response.getContentType(), startsWith("text/plain"));
78 assertThat(response.getStatusCode(), is(200));
82 public void templateProviderLocatesTemplatesInFileSystem() {
83 TemplateProvider templateProvider = loaders.getTemplateProvider();
84 Template template = templateProvider.getTemplate(templateContext, "template.txt");
85 template.render(templateContext, stringWriter);
86 assertThat(stringWriter.toString(), is("bar"));