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.notNullValue;
6 import static org.hamcrest.Matchers.startsWith;
7 import static org.mockito.Mockito.mock;
9 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 org.junit.Test;
30 * Unit test for {@link DefaultLoaders}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class DefaultLoadersTest {
36 private final Loaders loaders = new DefaultLoaders();
37 private final StringWriter stringWriter = new StringWriter();
38 private final TemplateContext templateContext = new TemplateContext();
41 public void templateCanBeLoadedFromTheClasspath() {
42 Template template = loaders.loadTemplate("/net/pterodactylus/sone/main/template.txt");
43 template.render(templateContext, stringWriter);
44 assertThat(stringWriter.toString(), is("Template. bar\n"));
48 public void staticPageIsServedFromClasspath() throws IOException, URISyntaxException {
49 Page<FreenetRequest> staticPage = loaders.loadStaticPage("text/", "/net/pterodactylus/sone/main/", "text/plain");
50 URI uri = new URI("http://some.host/text/template.txt");
51 Method method = Method.GET;
52 HTTPRequest httpRequest = mock(HTTPRequest.class);
53 ToadletContext toadletContext = mock(ToadletContext.class);
54 FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext);
55 OutputStream outputStream = new ByteArrayOutputStream();
56 Response response = new Response(outputStream);
57 staticPage.handleRequest(request, response);
58 assertThat(response.getContentType(), startsWith("text/plain"));
59 assertThat(response.getStatusCode(), is(200));
63 public void templateIsLocatedInClasspath() {
64 TemplateProvider templateProvider = loaders.getTemplateProvider();
65 Template template = templateProvider.getTemplate(templateContext, "about.html");
66 assertThat(template, notNullValue());