🎨 Replace UpdateFoundEvent with Kotlin version
[Sone.git] / src / test / java / net / pterodactylus / sone / main / DefaultLoadersTest.java
1 package net.pterodactylus.sone.main;
2
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;
8
9 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.StringWriter;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15
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;
23
24 import freenet.clients.http.SessionManager;
25 import freenet.clients.http.ToadletContext;
26 import freenet.l10n.BaseL10n;
27 import freenet.support.api.HTTPRequest;
28
29 import org.junit.Test;
30
31 /**
32  * Unit test for {@link DefaultLoaders}.
33  */
34 public class DefaultLoadersTest {
35
36         private final BaseL10n l10n = mock(BaseL10n.class);
37         private final Loaders loaders = new DefaultLoaders();
38         private final StringWriter stringWriter = new StringWriter();
39         private final TemplateContext templateContext = new TemplateContext();
40
41         @Test
42         public void templateCanBeLoadedFromTheClasspath() {
43                 Template template = loaders.loadTemplate("/net/pterodactylus/sone/main/template.txt");
44                 template.render(templateContext, stringWriter);
45                 assertThat(stringWriter.toString(), is("Template. bar\n"));
46         }
47
48         @Test
49         public void staticPageIsServedFromClasspath() throws IOException, URISyntaxException {
50                 Page<FreenetRequest> staticPage = loaders.loadStaticPage("text/", "/net/pterodactylus/sone/main/", "text/plain");
51                 URI uri = new URI("http://some.host/text/template.txt");
52                 Method method = Method.GET;
53                 HTTPRequest httpRequest = mock(HTTPRequest.class);
54                 ToadletContext toadletContext = mock(ToadletContext.class);
55                 SessionManager sessionManager = mock(SessionManager.class);
56                 FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager);
57                 OutputStream outputStream = new ByteArrayOutputStream();
58                 Response response = new Response(outputStream);
59                 staticPage.handleRequest(request, response);
60                 assertThat(response.getContentType(), startsWith("text/plain"));
61                 assertThat(response.getStatusCode(), is(200));
62         }
63
64         @Test
65         public void templateIsLocatedInClasspath() {
66                 TemplateProvider templateProvider = loaders.getTemplateProvider();
67                 Template template = templateProvider.getTemplate(templateContext, "about.html");
68                 assertThat(template, notNullValue());
69         }
70
71 }