From: David ‘Bombe’ Roden Date: Sun, 20 Nov 2016 15:00:50 +0000 (+0100) Subject: Simplify code, improve coverage X-Git-Tag: 0.9.7^2~406 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b288daa865b8cf02ceedfac8484ce9a8182c5ac8;p=Sone.git Simplify code, improve coverage --- diff --git a/src/main/java/net/pterodactylus/sone/template/FilesystemTemplate.java b/src/main/java/net/pterodactylus/sone/template/FilesystemTemplate.java index 473c191..ad753a7 100644 --- a/src/main/java/net/pterodactylus/sone/template/FilesystemTemplate.java +++ b/src/main/java/net/pterodactylus/sone/template/FilesystemTemplate.java @@ -2,7 +2,7 @@ package net.pterodactylus.sone.template; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -18,8 +18,6 @@ import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateException; import net.pterodactylus.util.template.TemplateParser; -import freenet.support.io.Closer; - import com.google.common.base.Charsets; /** @@ -46,28 +44,19 @@ public class FilesystemTemplate extends Template { private void loadTemplate() { File templateFile = new File(filename); - if (!templateFile.exists()) { - throw new TemplateFileNotFoundException(filename); - } if (templateWasLoaded() && !templateFileHasBeenModifiedAfterLoading(templateFile)) { return; } - InputStream templateInputStream = null; - Reader templateReader = null; - try { - templateInputStream = new FileInputStream(templateFile); - templateReader = new InputStreamReader(templateInputStream, Charsets.UTF_8); + try (InputStream templateInputStream = new FileInputStream(templateFile); + Reader templateReader = new InputStreamReader(templateInputStream, Charsets.UTF_8)) { Template template = TemplateParser.parse(templateReader); lastTemplate.set(new LastLoadedTemplate(template)); template.getInitialContext().mergeContext(initialContext); for (Part part : parts) { template.add(part); } - } catch (FileNotFoundException e) { + } catch (IOException e) { throw new TemplateFileNotFoundException(filename); - } finally { - Closer.close(templateReader); - Closer.close(templateInputStream); } } diff --git a/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java b/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java index cd989eb..7b93477 100644 --- a/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java +++ b/src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java @@ -2,6 +2,7 @@ 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; @@ -108,4 +109,19 @@ public class FilesystemTemplateTest { 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()); + } + }