Simplify code, improve coverage
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 15:00:50 +0000 (16:00 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 15:00:50 +0000 (16:00 +0100)
src/main/java/net/pterodactylus/sone/template/FilesystemTemplate.java
src/test/java/net/pterodactylus/sone/template/FilesystemTemplateTest.java

index 473c191..ad753a7 100644 (file)
@@ -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);
                }
        }
 
index cd989eb..7b93477 100644 (file)
@@ -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());
+       }
+
 }