Skip to content

Commit

Permalink
Fixes #300.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Jan 31, 2017
1 parent 7d100ba commit 129cbe2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -162,7 +178,7 @@ private String findTemplateName(String docType) {
public void render(Map<String, Object> 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("."));
}

Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/jbake/render/RendererTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> content = new HashMap<String, Object>();
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();
}
}

0 comments on commit 129cbe2

Please sign in to comment.