Skip to content

Commit

Permalink
added more tests. log warnings in DefaultJBakeConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
ancho committed May 9, 2017
1 parent 590e7c2 commit 2113539
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public void bake() {
ContentStore contentStore = utensils.getContentStore();
JBakeConfiguration config = utensils.getConfiguration();
Crawler crawler = utensils.getCrawler();
Renderer renderer = utensils.getRenderer();
Asset asset = utensils.getAsset();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -17,6 +19,8 @@
*/
public class DefaultJBakeConfiguration implements JBakeConfiguration {

private Logger logger = LoggerFactory.getLogger(DefaultJBakeConfiguration.class);

private static final String SOURCE_FOLDER_KEY = "sourceFolder";
private static final String DESTINATION_FOLDER_KEY = "destinationFolder";
private static final String ASSET_FOLDER_KEY = "assetFolder";
Expand Down Expand Up @@ -71,6 +75,7 @@ public File getTemplateFileByDocType(String docType) {
if ( templateFileName != null ) {
return new File(getTemplateFolder(), templateFileName);
}
logger.warn("Cannot find configuration key '{}' for document type '{}'", templateKey,docType);
return null;
}

Expand Down Expand Up @@ -291,13 +296,6 @@ public void setProperty(String key, Object value) {
configuration.setProperty(key, value);
}

/*
@Override
public Configuration getCompositeConfiguration() {
return configuration;
}
*/

@Override
public Iterator<String> getKeys() {
return configuration.getKeys();
Expand All @@ -323,7 +321,7 @@ public Object getAsciidoctorOption(String optionKey) {
Object value = subConfig.getProperty(optionKey);

if ( value == null ) {
//TODO: log warning if option not found
logger.warn("Cannot find asciidoctor option '{}.{}'", ASCIIDOCTOR_OPTION, optionKey);
return "";
}
return value;
Expand Down Expand Up @@ -416,9 +414,7 @@ public void setDatabasePath(String path) {
}

private File getAsFolder(String key) {
File folder = (File) get(key);

return folder;
return (File) get(key);
}

private String getAsString(String key) {
Expand All @@ -429,8 +425,8 @@ private String getAsString(String key, String defaultValue) {
return configuration.getString(key, defaultValue);
}

private boolean getAsBoolean(String paginateIndex) {
return configuration.getBoolean(paginateIndex, false);
private boolean getAsBoolean(String key) {
return configuration.getBoolean(key, false);
}

private int getAsInt(String key, int defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JBakeConfigurationFactory {
* @param sourceFolder The source folder of the project
* @param destination The destination folder to render and copy files to
* @param isClearCache Whether to clear database cache or not
* @return A {@link DefaultJBakeConfiguration} by given parameters
* @return A {@link JBakeConfiguration} by given parameters
* @throws ConfigurationException if loading the configuration fails
*/
public static JBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, boolean isClearCache) throws ConfigurationException {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/jbake/app/LoggingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jbake.app;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;

@RunWith(MockitoJUnitRunner.class)
public abstract class LoggingTest {

@Mock
protected Appender<ILoggingEvent> mockAppender;

@Captor
protected ArgumentCaptor<LoggingEvent> captorLoggingEvent;

protected Logger root;

@Before
public void setup() {
root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

root.addAppender(mockAppender);
root.setLevel(Level.INFO);
}

@After
public void teardown() {
root.detachAppender(mockAppender);
}


}
88 changes: 68 additions & 20 deletions src/test/java/org/jbake/app/configuration/ConfigUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jbake.app.configuration;

import ch.qos.logback.classic.spi.LoggingEvent;
import org.jbake.app.JBakeException;
import org.jbake.app.LoggingTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -13,11 +16,13 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class ConfigUtilTest {

public class ConfigUtilTest extends LoggingTest {


@Rule
Expand All @@ -32,7 +37,7 @@ public void setUp() throws Exception {

@Test
public void should_load_a_default_configuration() throws Exception {
JBakeConfiguration config = util.loadConfig(new File(this.getClass().getResource("/").getFile()));
JBakeConfiguration config = util.loadConfig(getTestResourcesAsSourceFolder());

assertDefaultPropertiesPresent(config);
}
Expand Down Expand Up @@ -69,7 +74,7 @@ public void should_throw_an_exception_if_sourcefolder_does_not_exist() throws Ex
@Test
public void should_add_sourcefolder_to_configuration() throws Exception {

File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
JBakeConfiguration config = util.loadConfig(sourceFolder);

assertThat(config.getSourceFolder()).isEqualTo(sourceFolder);
Expand All @@ -95,7 +100,7 @@ public void should_throw_an_exception_if_sourcefolder_is_not_a_directory() throw

@Test
public void should_return_destination_folder_from_configuration() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
File expectedDestinationFolder = new File(sourceFolder,"output");
JBakeConfiguration config = util.loadConfig(sourceFolder);

Expand All @@ -104,7 +109,7 @@ public void should_return_destination_folder_from_configuration() throws Excepti

@Test
public void should_return_asset_folder_from_configuration() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
File expectedDestinationFolder = new File(sourceFolder,"assets");
JBakeConfiguration config = util.loadConfig(sourceFolder);

Expand All @@ -113,7 +118,7 @@ public void should_return_asset_folder_from_configuration() throws Exception {

@Test
public void should_return_template_folder_from_configuration() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
File expectedDestinationFolder = new File(sourceFolder,"templates");
JBakeConfiguration config = util.loadConfig(sourceFolder);

Expand All @@ -122,7 +127,7 @@ public void should_return_template_folder_from_configuration() throws Exception

@Test
public void should_get_template_file_doctype() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
File expectedTemplateFile = new File(sourceFolder, "templates/index.ftl");
JBakeConfiguration config = util.loadConfig(sourceFolder);

Expand All @@ -131,11 +136,26 @@ public void should_get_template_file_doctype() throws Exception {
assertThat(templateFile).isEqualTo( expectedTemplateFile );
}

@Test
public void should_log_warning_if_document_type_not_found() throws Exception {
File sourceFolder = getTestResourcesAsSourceFolder();
JBakeConfiguration config = util.loadConfig(sourceFolder);

File templateFile = config.getTemplateFileByDocType("none");

verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());

LoggingEvent loggingEvent = captorLoggingEvent.getValue();

assertThat(loggingEvent.getMessage()).isEqualTo("Cannot find configuration key '{}' for document type '{}'");

}

@Test
public void should_get_template_output_extension() throws Exception {

String docType = "masterindex";
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);
config.setTemplateExtensionForDocType(docType, ".xhtml");

Expand All @@ -146,7 +166,7 @@ public void should_get_template_output_extension() throws Exception {

@Test
public void should_get_markdown_extensions_as_list() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);

List<String> markdownExtensions = config.getMarkdownExtensions();
Expand All @@ -157,7 +177,7 @@ public void should_get_markdown_extensions_as_list() throws Exception {
@Test
public void should_return_configured_doc_types() throws Exception {

File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);

List<String> docTypes = config.getDocumentTypes();
Expand All @@ -168,7 +188,7 @@ public void should_return_configured_doc_types() throws Exception {

@Test
public void should_return_a_list_of_asciidoctor_options_keys() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);
config.setProperty("asciidoctor.option.requires", "asciidoctor-diagram");
config.setProperty("asciidoctor.option.template_dirs", "src/template1,src/template2");
Expand All @@ -180,7 +200,7 @@ public void should_return_a_list_of_asciidoctor_options_keys() throws Exception

@Test
public void should_return_an_asciidoctor_option() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);
config.setProperty("asciidoctor.option.requires", "asciidoctor-diagram");
config.setProperty("asciidoctor.option.template_dirs", "src/template1,src/template2");
Expand All @@ -192,7 +212,7 @@ public void should_return_an_asciidoctor_option() throws Exception {

@Test
public void should_return_an_asciidoctor_option_with_a_list_value() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);
config.setProperty("asciidoctor.option.requires", "asciidoctor-diagram");
config.setProperty("asciidoctor.option.template_dirs", "src/template1,src/template2");
Expand All @@ -205,23 +225,51 @@ public void should_return_an_asciidoctor_option_with_a_list_value() throws Excep

@Test
public void should_return_empty_string_if_option_not_available() throws Exception {
File sourceFolder = new File(this.getClass().getResource("/").getFile());
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);

Object option = config.getAsciidoctorOption("template_dirs");

assertThat(String.valueOf(option)).isEmpty();
}

private void assertDefaultPropertiesPresent(JBakeConfiguration config) throws IllegalAccessException {
@Test
public void should_log_a_warning_if_asciidoc_option_could_not_be_found() throws Exception {
File sourceFolder = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(sourceFolder);

Object option = config.getAsciidoctorOption("template_dirs");

verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());

LoggingEvent loggingEvent = captorLoggingEvent.getValue();

assertThat(loggingEvent.getMessage()).isEqualTo("Cannot find asciidoctor option '{}.{}'");
}

@Test
public void should_handle_non_existing_files() throws Exception {

File source = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(source);

config.setTemplateFolder(null);

File templateFolder = config.getTemplateFolder();

assertThat(templateFolder).isNull();
}

private void assertDefaultPropertiesPresent(JBakeConfiguration config) throws IllegalAccessException {
for(Field field : JBakeConfiguration.class.getFields() ) {
String key = (String) field.get(new String());
System.out.println("Key: " + key);
assertThat(config.get(key)).isNotNull();

Assert.assertThat("unable to get key " + key, config.get(key), is( notNullValue()));
}
}


private File getTestResourcesAsSourceFolder() {
return new File(this.getClass().getResource("/").getFile());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jbake.app.configuration;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Created by frank on 12.02.17.
*/
public class JBakeConfigurationFactoryTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void should_return_a_default_configuration() throws Exception {
File sourceFolder = folder.getRoot();
File destinationFolder = folder.newFolder("output");

JBakeConfiguration configuration = JBakeConfigurationFactory.createDefaultJbakeConfiguration(sourceFolder,destinationFolder, true);

assertThat(configuration.getSourceFolder()).isEqualTo(sourceFolder);
assertThat(configuration.getDestinationFolder()).isEqualTo(destinationFolder);
assertThat(configuration.getClearCache()).isEqualTo(true);
}

}
Loading

0 comments on commit 2113539

Please sign in to comment.