From 8f442a9b829835c41746292c68cf80bbd5b80739 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 31 Jul 2019 09:46:26 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20FilesystemTemplateTest?= =?utf8?q?=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/template/FilesystemTemplateTest.java | 125 --------------------- .../sone/template/FilesystemTemplateTest.kt | 117 +++++++++++++++++++ 2 files changed, 117 insertions(+), 125 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt diff --git a/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java b/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java deleted file mode 100644 index 9259ef5..0000000 --- a/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package net.pterodactylus.sone.template; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; - -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Arrays; -import java.util.concurrent.atomic.AtomicReference; - -import net.pterodactylus.util.template.Part; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.template.TemplateException; - -import com.google.common.base.Charsets; -import com.google.common.io.Files; -import org.junit.Before; -import org.junit.Test; - -/** - * Unit test for {@link FilesystemTemplate}. - */ -public class FilesystemTemplateTest { - - private final File tempFile; - private final FilesystemTemplate filesystemTemplate; - private final AtomicReference stringWriter = new AtomicReference<>(new StringWriter()); - private final TemplateContext templateContext = new TemplateContext(); - - public FilesystemTemplateTest() throws IOException { - tempFile = File.createTempFile("template-", ".dat"); - writeTemplate("Text"); - filesystemTemplate = new FilesystemTemplate(tempFile.getAbsolutePath()); - } - - private void writeTemplate(String text) throws IOException { - Files.write(text + ".<%foreach values value><% value><%/foreach>", tempFile, Charsets.UTF_8); - } - - @Before - public void setupTemplateContext() { - templateContext.set("values", Arrays.asList("a", 1)); - } - - @Test(expected = FilesystemTemplate.TemplateFileNotFoundException.class) - public void loadingTemplateFromNonExistingFileThrowsException() throws IOException { - FilesystemTemplate filesystemTemplate = new FilesystemTemplate("/a/b/c.dat"); - filesystemTemplate.getInitialContext(); - } - - @Test - public void templateCanBeLoadedFromTheFilesystem() { - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("Text.a1")); - } - - @Test - public void templateCanBeReloaded() throws IOException, InterruptedException { - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("Text.a1")); - Thread.sleep(1000); - writeTemplate("New"); - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("New.a1")); - } - - @Test - public void templateIsNotReloadedIfNotChanged() { - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("Text.a1")); - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("Text.a1")); - } - - private String getRenderedString() { - String renderedString = stringWriter.get().toString(); - stringWriter.set(new StringWriter()); - return renderedString; - } - - @Test - public void initialContextIsCopiedToReloadedTemplates() throws IOException, InterruptedException { - filesystemTemplate.getInitialContext().set("values", "test"); - Thread.sleep(1000); - writeTemplate("New"); - assertThat(filesystemTemplate.getInitialContext().get("values"), is((Object) "test")); - } - - @Test - public void partsAreCopiedToReloadedTemplates() throws InterruptedException, IOException { - filesystemTemplate.add(new Part() { - @Override - public void render(TemplateContext templateContext, Writer writer) throws TemplateException { - try { - writer.write(".Test"); - } catch (IOException e) { - throw new TemplateException(e); - } - } - }); - Thread.sleep(1000); - writeTemplate("New"); - filesystemTemplate.render(templateContext, stringWriter.get()); - assertThat(getRenderedString(), is("New.a1.Test")); - } - - @Test - public void columnOfReturnedTemplateIsReturnedAsZero() { - assertThat(filesystemTemplate.getColumn(), is(0)); - } - - @Test - public void lineOfReturnedTemplateIsReturnedAsZero() { - assertThat(filesystemTemplate.getLine(), is(0)); - } - - @Test - public void templateCanBeIteratedOver() { - assertThat(filesystemTemplate.iterator(), notNullValue()); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt new file mode 100644 index 0000000..f6c704c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt @@ -0,0 +1,117 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.junit.rules.* +import java.io.* +import java.lang.Thread.* +import kotlin.test.Test + +/** + * Unit test for [FilesystemTemplate]. + */ +class FilesystemTemplateTest() { + + @Rule + @JvmField + val expectedException: ExpectedException = ExpectedException.none() + + private val tempFile = File.createTempFile("template-", ".dat") + private val filesystemTemplate: FilesystemTemplate + private val templateContext = TemplateContext() + + private val renderedString: String + get() = + StringWriter().use { stringWriter -> + filesystemTemplate.render(templateContext, stringWriter) + stringWriter + }.toString() + + init { + writeTemplate("Text") + filesystemTemplate = FilesystemTemplate(tempFile.absolutePath) + } + + private fun writeTemplate(text: String) { + tempFile.writer().use { + it.write("$text.<%foreach values value><% value><%/foreach>") + } + } + + @Before + fun setupTemplateContext() { + templateContext.set("values", listOf("a", 1)) + } + + @Test + fun `loading template from non existing file throws exception`() { + val filesystemTemplate = FilesystemTemplate("/a/b/c.dat") + expectedException.expect(FilesystemTemplate.TemplateFileNotFoundException::class.java) + filesystemTemplate.initialContext + } + + @Test + fun `template can be loaded from the filesystem`() { + assertThat(renderedString, equalTo("Text.a1")) + } + + @Test + fun `template can be reloaded`() { + assertThat(renderedString, equalTo("Text.a1")) + sleep(1000) + writeTemplate("New") + assertThat(renderedString, equalTo("New.a1")) + } + + @Test + fun `template is not reloaded if not changed`() { + assertThat(renderedString, equalTo("Text.a1")) + assertThat(renderedString, equalTo("Text.a1")) + } + + @Test + fun `initial context is copied to reloaded templates`() { + filesystemTemplate.initialContext.set("values", "test") + sleep(1000) + writeTemplate("New") + assertThat(filesystemTemplate.initialContext.get("values"), equalTo("test" as Any)) + } + + @Test + fun `parts are copied to currently loaded templates`() { + writeTemplate("New") + renderedString + filesystemTemplate.add { _, writer -> + writer.write(".Test") + } + assertThat(renderedString, equalTo("New.a1.Test")) + } + + @Test + fun `parts are copied to reloaded templates`() { + filesystemTemplate.add { _, writer -> + writer.write(".Test") + } + sleep(1000) + writeTemplate("New") + assertThat(renderedString, equalTo("New.a1.Test")) + } + + @Test + fun `column of returned template is returned as zero`() { + assertThat(filesystemTemplate.column, equalTo(0)) + } + + @Test + fun `line of returned template is returned as zero`() { + assertThat(filesystemTemplate.line, equalTo(0)) + } + + @Test + fun `template can be iterated over`() { + assertThat>(filesystemTemplate.iterator(), notNullValue()) + } + +} -- 2.7.4