From 129cbe2a17e45919039cd514e111a731c0a1439f Mon Sep 17 00:00:00 2001 From: Jonathan Bullock Date: Tue, 31 Jan 2017 22:00:45 +0000 Subject: [PATCH] Fixes #300. --- src/main/java/org/jbake/app/Renderer.java | 18 ++++- .../java/org/jbake/render/RendererTest.java | 69 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/jbake/render/RendererTest.java diff --git a/src/main/java/org/jbake/app/Renderer.java b/src/main/java/org/jbake/app/Renderer.java index 31b97296f..81131c066 100644 --- a/src/main/java/org/jbake/app/Renderer.java +++ b/src/main/java/org/jbake/app/Renderer.java @@ -146,6 +146,22 @@ public Renderer(ContentStore db, File destination, File templatesPath, Composite this.renderingEngine = new DelegatingTemplateEngine(config, db, destination, templatesPath); this.db = db; } + + /** + * Creates a new instance of Renderer with supplied references to folders and the instance of DelegatingTemplateEngine to use. + * + * @param db The database holding the content + * @param destination The destination folder + * @param templatesPath The templates folder + * @param config + * @param renderingEngine The instance of DelegatingTemplateEngine to use + */ + public Renderer(ContentStore db, File destination, File templatesPath, CompositeConfiguration config, DelegatingTemplateEngine renderingEngine) { + this.destination = destination; + this.config = config; + this.renderingEngine = renderingEngine; + this.db = db; + } private String findTemplateName(String docType) { String templateKey = "template." + docType + ".file"; @@ -162,7 +178,7 @@ private String findTemplateName(String docType) { public void render(Map content) throws Exception { String docType = (String) content.get(Crawler.Attributes.TYPE); String outputFilename = destination.getPath() + File.separatorChar + content.get(Attributes.URI); - if (outputFilename.lastIndexOf(".") > 0) { + if (outputFilename.lastIndexOf(".") > outputFilename.lastIndexOf(File.separatorChar)) { outputFilename = outputFilename.substring(0, outputFilename.lastIndexOf(".")); } diff --git a/src/test/java/org/jbake/render/RendererTest.java b/src/test/java/org/jbake/render/RendererTest.java new file mode 100644 index 000000000..5e74e3b31 --- /dev/null +++ b/src/test/java/org/jbake/render/RendererTest.java @@ -0,0 +1,69 @@ +package org.jbake.render; + +import java.io.File; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.jbake.app.ConfigUtil; +import org.jbake.app.ContentStore; +import org.jbake.app.Crawler; +import org.jbake.app.Renderer; +import org.jbake.app.ConfigUtil.Keys; +import org.jbake.template.DelegatingTemplateEngine; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith( MockitoJUnitRunner.class ) +public class RendererTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private CompositeConfiguration config; + private File rootPath; + private File outputPath; + + @Mock private ContentStore db; + @Mock private DelegatingTemplateEngine renderingEngine; + + @Before + public void setup() throws Exception { + URL sourceUrl = this.getClass().getResource("/"); + rootPath = new File(sourceUrl.getFile()); + if (!rootPath.exists()) { + throw new Exception("Cannot find base path for test!"); + } + outputPath = folder.newFolder("output"); + config = ConfigUtil.load(rootPath); + } + + /** + * See issue #300 + * + * @throws Exception + */ + @Test + public void testRenderFileWorksWhenPathHasDotInButFileDoesNot() throws Exception { + final String FOLDER = "real.path"; + final String FILENAME = "about"; + config.setProperty(Keys.OUTPUT_EXTENSION, ""); + Renderer renderer = new Renderer(db, outputPath, folder.newFolder("templates"), config, renderingEngine); + + Map content = new HashMap(); + content.put(Crawler.Attributes.TYPE, "page"); + content.put(Crawler.Attributes.URI, "/" + FOLDER + "/" + FILENAME); + content.put(Crawler.Attributes.STATUS, "published"); + + renderer.render(content); + + File outputFile = new File(outputPath.getAbsolutePath() + File.separatorChar + FOLDER + File.separatorChar + FILENAME); + assertThat(outputFile).isFile(); + } +}