Skip to content

Instantly share code, notes, and snippets.

@ancho
Forked from jonbullock/Renderer.java
Last active January 31, 2017 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ancho/f61643f93d6bdb9a73c90dca32202d3e to your computer and use it in GitHub Desktop.
Save ancho/f61643f93d6bdb9a73c90dca32202d3e to your computer and use it in GitHub Desktop.
RendererTest for path with dot in it and output file without extension
interface Configuration {
File getSourcePath();
void setSourcePath(File path);
File getTemplatePath();
void setTemplatePath(File path);
File getDestinationPath();
void setDestinationPath(File path);
Object getProperty(String key);
void setProperty(String key, Object value);
}
class JbakeConfiguration implements Configuration {
private CompositeConfiguration config;
public JbakeConfiguration(CompositeConfiguration configuration) {
this.config = configuration
}
// assuming source folder is added to the config during ConfigUtil.load(sourceFile)
File getSourcePath() {
return getRequiredFolderFromConfig(Keys.SOURCE_FOLDER);
}
void setSourcePath(File path) {
config.setProperty(Keys.SOURCE_FOLDER, path);
}
public File getTemplatePath() {
return setupRequiredFolderFromConfig(Keys.TEMPLATE_FOLDER);
}
void setTemplatePath(File path) {
config.setProperty(Keys.TEMPLATE_FOLDER);
}
File getDestinationPath() {
return setupRequiredFolderFromConfig(Keys.DESTINATION_FOLDER);
}
void setDestinationPath(File path) {
config.setProperty(Keys.DESTINATION_FOLDER, path);
}
Object getProperty(String key) {
return config.getProperty(key);
}
void setProperty(String key, Object value) {
config.setProperty(key, value);
}
private File setupRequiredFolderFromConfig(String key) {
final File path = setupPathFromConfig(key);
if (!FileUtil.isExistingFolder(path)) {
throw new JBakeException("Error: Required folder cannot be found! Expected to find [" + key + "] at: " + path.getAbsolutePath());
}
return path;
}
private File getRequiredFolderFromConfig(String key) {
final File path = (File) getProperty(key);
if (!FileUtil.isExistingFolder(path)) {
throw new JBakeException("Error: Required folder cannot be found! Expected to find [" + key + "] at: " + path.getAbsolutePath());
}
return path;
}
private File setupPathFromConfig(String key) {
return new File(source, config.getString(key));
}
}
}
/**
* Creates a new instance of Renderer with supplied references to folders.
*
* @param db The database holding the content
* @param config
* @param renderingEngine The instance of DelegatingTemplateEngine to use
*/
public Renderer(ContentStore db, Configuration config, DelegatingTemplateEngine renderingEngine) {
this.db = db;
this.config = config;
this.renderingEngine = renderingEngine;
}
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 JbakeConfiguration 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 = new JbakeConfiguration( ConfigUtil.load(rootPath) );
config.setDestinationPath(outputPath)
}
/**
* 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, "");
config.setTemplatePath( folder.newFolder("templates") );
Renderer renderer = new Renderer(db, 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment