From 334798949fc7a0585f861c60ce93f4f2dc670d65 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 2 Nov 2014 12:49:02 +0100 Subject: [PATCH] Add unit test for get-page plugin. --- .../sone/template/GetPagePluginTest.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java diff --git a/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java b/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java new file mode 100644 index 0000000..febd06a --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java @@ -0,0 +1,79 @@ +package net.pterodactylus.sone.template; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.template.TemplateContext; + +import freenet.support.api.HTTPRequest; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link GetPagePlugin}. + * + * @author David ‘Bombe’ Roden + */ +public class GetPagePluginTest { + + private final GetPagePlugin plugin = new GetPagePlugin(); + private final TemplateContext context = mock(TemplateContext.class); + private final FreenetRequest request = mock(FreenetRequest.class); + private final Map parameters = + new HashMap(); + private HTTPRequest httpRequest = mock(HTTPRequest.class); + + @Before + public void setupTemplateContext() { + when(context.get("request")).thenReturn(request); + when(request.getHttpRequest()).thenReturn(httpRequest); + when(httpRequest.getParam("page")).thenReturn("1"); + } + + @Test + public void fullySpecifiedPluginCallSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("parameter", "page"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingRequestParameterStillSetsCorrectValue() { + parameters.put("parameter", "page"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingParameterParameterStillSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingKeyParameterStillSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("parameter", "page"); + plugin.execute(context, parameters); + verify(context).set("page", 1); + } + + @Test + public void unparseablePageSetsPageZero() { + parameters.put("parameter", "wrong-parameter"); + plugin.execute(context, parameters); + verify(context).set("page", 0); + } + +} -- 2.7.4