From b9d12d4a287728f062c541d02887c39c24117217 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 19 May 2019 20:57:25 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20interactions=20helper=20for?= =?utf8?q?=20page=20maker?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/page/PageMakerInteraction.kt | 31 ++++++++++ .../java/net/pterodactylus/sone/test/Dirty.java | 6 +- .../java/net/pterodactylus/sone/test/TestUtil.java | 11 ++++ .../sone/web/page/PageMakerInteractionTest.kt | 67 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/page/PageMakerInteraction.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/page/PageMakerInteractionTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/PageMakerInteraction.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/PageMakerInteraction.kt new file mode 100644 index 0000000..8d6884c --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/PageMakerInteraction.kt @@ -0,0 +1,31 @@ +package net.pterodactylus.sone.web.page + +import freenet.clients.http.* + +class PageMakerInteraction(toadletContext: ToadletContext, pageTitle: String) { + + private val pageMaker: PageMaker = toadletContext.pageMaker + private val pageNode: PageNode = pageMaker.getPageNode(pageTitle, toadletContext) + + fun addStyleSheet(styleSheet: String) { + pageNode.addCustomStyleSheet(styleSheet) + } + + fun addLinkNode(linkAttributes: Map) { + pageNode.headNode.addChild("link").let { + linkAttributes.forEach(it::addAttribute) + } + } + + fun addShortcutIcon(shortcutIcon: String) { + pageNode.addForwardLink("icon", shortcutIcon) + } + + fun setContent(content: String) { + pageNode.content.addChild("%", content) + } + + fun renderPage(): String = + pageNode.outer.generate() + +} diff --git a/src/test/java/net/pterodactylus/sone/test/Dirty.java b/src/test/java/net/pterodactylus/sone/test/Dirty.java index c7d351c..5d2873f 100644 --- a/src/test/java/net/pterodactylus/sone/test/Dirty.java +++ b/src/test/java/net/pterodactylus/sone/test/Dirty.java @@ -1,16 +1,18 @@ package net.pterodactylus.sone.test; import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.SOURCE; import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * This annotation marks test methods that are somehow not good test methods. + * This annotation marks tests or test methods that are dirty, + * i.e. written in a way no test should ever be written in. */ @Retention(SOURCE) -@Target(METHOD) +@Target(value = { TYPE, METHOD }) public @interface Dirty { String value() default ""; diff --git a/src/test/java/net/pterodactylus/sone/test/TestUtil.java b/src/test/java/net/pterodactylus/sone/test/TestUtil.java index 22c2057..35e8202 100644 --- a/src/test/java/net/pterodactylus/sone/test/TestUtil.java +++ b/src/test/java/net/pterodactylus/sone/test/TestUtil.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.test; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -43,4 +44,14 @@ public class TestUtil { } } + public static T createObject(Class clazz, Class[] parameterTypes, Object... arguments) { + try { + Constructor constructor = clazz.getDeclaredConstructor(parameterTypes); + constructor.setAccessible(true); + return constructor.newInstance(arguments); + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/PageMakerInteractionTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/PageMakerInteractionTest.kt new file mode 100644 index 0000000..178ef09 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/PageMakerInteractionTest.kt @@ -0,0 +1,67 @@ +package net.pterodactylus.sone.web.page + +import freenet.clients.http.* +import freenet.support.* +import freenet.support.HTMLNode.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.test.TestUtil.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +@Dirty +class PageMakerInteractionTest { + + private val toadletContext = deepMock() + private val pageMaker: PageMaker = toadletContext.pageMaker + private val outerNode = HTMLDoctype("html", "-//W3C//DTD XHTML 1.1//EN") + private val htmlNode: HTMLNode = outerNode.addChild("html") + private val headNode: HTMLNode = htmlNode.addChild("head") + private val contentNode: HTMLNode = htmlNode.addChild("body").addChild("div") + private val pageNode: PageNode = createObject(PageNode::class.java, arrayOf(HTMLNode::class.java, HTMLNode::class.java, HTMLNode::class.java), outerNode, headNode, contentNode) + + init { + whenever(pageMaker.getPageNode("page title", toadletContext)).thenReturn(pageNode) + } + + private val pageMakerInteractions = PageMakerInteraction(toadletContext, "page title") + + @Test + fun `interactions can add style sheet`() { + pageMakerInteractions.addStyleSheet("style.sheet") + assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains( + mapOf("rel" to "stylesheet", "href" to "style.sheet", "type" to "text/css", "media" to "screen") + )) + } + + @Test + fun `link nodes can be added`() { + pageMakerInteractions.addLinkNode(mapOf("foo" to "bar")) + assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains( + mapOf("foo" to "bar") + )) + } + + @Test + fun `shortcut icon can be added`() { + pageMakerInteractions.addShortcutIcon("shortcut.icon") + assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains( + mapOf("rel" to "icon", "href" to "shortcut.icon") + )) + } + + @Test + fun `content can be set`() { + pageMakerInteractions.setContent("foo