From 85301f4186af405d2cf9531ce73499099035e7db Mon Sep 17 00:00:00 2001 From: Jonathan Bullock Date: Sun, 23 May 2021 18:26:17 +0100 Subject: [PATCH 1/2] Added extra method so build plugins don't need to depend on Apache Commons Configuration library. --- .../jbake/app/configuration/DefaultJBakeConfiguration.java | 6 ++++++ .../org/jbake/app/configuration/JBakeConfiguration.java | 3 +++ 2 files changed, 9 insertions(+) diff --git a/jbake-core/src/main/java/org/jbake/app/configuration/DefaultJBakeConfiguration.java b/jbake-core/src/main/java/org/jbake/app/configuration/DefaultJBakeConfiguration.java index 9388ba89..5de7d8e3 100644 --- a/jbake-core/src/main/java/org/jbake/app/configuration/DefaultJBakeConfiguration.java +++ b/jbake-core/src/main/java/org/jbake/app/configuration/DefaultJBakeConfiguration.java @@ -2,6 +2,7 @@ import org.apache.commons.configuration2.CompositeConfiguration; import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.MapConfiguration; import org.apache.commons.configuration2.SystemConfiguration; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -678,6 +679,11 @@ public List getJbakeProperties() { return jbakeKeys; } + @Override + public void addConfiguration(Properties properties) { + compositeConfiguration.addConfiguration(new MapConfiguration(properties)); + } + @Override public String getAbbreviatedGitHash() { return getAsString(GIT_HASH.getKey()); diff --git a/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfiguration.java b/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfiguration.java index 5c3cd0a0..3a18536b 100644 --- a/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfiguration.java +++ b/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfiguration.java @@ -4,6 +4,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Properties; /** * JBakeConfiguration gives you access to the project configuration. Typically located in a file called jbake.properties. @@ -367,5 +368,7 @@ public interface JBakeConfiguration { Map asHashMap(); List getJbakeProperties(); + + void addConfiguration(Properties properties); } From 87c5cea57d27ee2dab1d7bc02df5ecc66bb78a75 Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Mon, 24 May 2021 16:14:15 +0200 Subject: [PATCH 2/2] add some tests. throw JBakeException to hide commons configuration dependency --- .../jbake/app/configuration/ConfigUtil.java | 12 ++++--- .../JBakeConfigurationFactory.java | 34 +++++++++++-------- .../main/java/org/jbake/launcher/Main.java | 4 +-- .../JBakeConfigurationFactoryTest.java | 16 +++++++++ .../java/org/jbake/launcher/MainTest.java | 8 +++++ 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/jbake-core/src/main/java/org/jbake/app/configuration/ConfigUtil.java b/jbake-core/src/main/java/org/jbake/app/configuration/ConfigUtil.java index 9ce72da1..c0c6260f 100644 --- a/jbake-core/src/main/java/org/jbake/app/configuration/ConfigUtil.java +++ b/jbake-core/src/main/java/org/jbake/app/configuration/ConfigUtil.java @@ -97,11 +97,15 @@ private void displayLegacyConfigFileWarningIfRequired() { * @param source the source directory of the project * @param propertiesFile the properties file for the project * @return the configuration - * @throws ConfigurationException if unable to configure + * @throws JBakeException if unable to configure */ - public JBakeConfiguration loadConfig(File source, File propertiesFile) throws ConfigurationException { - CompositeConfiguration configuration = load(source, propertiesFile); - return new DefaultJBakeConfiguration(source, configuration); + public JBakeConfiguration loadConfig(File source, File propertiesFile) throws JBakeException { + try { + CompositeConfiguration configuration = load(source, propertiesFile); + return new DefaultJBakeConfiguration(source, configuration); + } catch (ConfigurationException e) { + throw new JBakeException(SystemExit.CONFIGURATION_ERROR, e.getMessage(), e); + } } /** diff --git a/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfigurationFactory.java b/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfigurationFactory.java index 990b5f45..1ff3fa76 100644 --- a/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfigurationFactory.java +++ b/jbake-core/src/main/java/org/jbake/app/configuration/JBakeConfigurationFactory.java @@ -2,7 +2,7 @@ import org.apache.commons.configuration2.CompositeConfiguration; -import org.apache.commons.configuration2.ex.ConfigurationException; +import org.jbake.app.JBakeException; import java.io.File; @@ -18,16 +18,15 @@ public JBakeConfigurationFactory() { } /** - * Creates a {@link DefaultJBakeConfiguration} + * Creates a {@link DefaultJBakeConfiguration} using default.properties and jbake.properties + * * @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 configuration by given parameters - * @throws ConfigurationException if loading the configuration fails - * @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead + * @throws JBakeException if loading the configuration fails */ - @Deprecated - public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, boolean isClearCache) throws ConfigurationException { + public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, boolean isClearCache) throws JBakeException { return createDefaultJbakeConfiguration(sourceFolder, destination, (File) null, isClearCache); } @@ -38,9 +37,9 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold * @param propertiesFile The properties file for the project * @param isClearCache Whether to clear database cache or not * @return A configuration by given parameters - * @throws ConfigurationException if loading the configuration fails + * @throws JBakeException if loading the configuration fails */ - public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, File propertiesFile, boolean isClearCache) throws ConfigurationException { + public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, File propertiesFile, boolean isClearCache) throws JBakeException { DefaultJBakeConfiguration configuration = (DefaultJBakeConfiguration) getConfigUtil().loadConfig(sourceFolder, propertiesFile); configuration.setDestinationFolder(destination); configuration.setClearCache(isClearCache); @@ -57,8 +56,10 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold * @param compositeConfiguration A given {@link CompositeConfiguration} * @param isClearCache Whether to clear database cache or not * @return A configuration by given parameters + * @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead */ - public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration, boolean isClearCache) { + @Deprecated + public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration, boolean isClearCache) throws JBakeException { DefaultJBakeConfiguration configuration = new DefaultJBakeConfiguration(sourceFolder, compositeConfiguration); configuration.setDestinationFolder(destination); configuration.setClearCache(isClearCache); @@ -74,8 +75,10 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold * @param destination The destination folder to render and copy files to * @param compositeConfiguration A given {@link CompositeConfiguration} * @return A configuration by given parameters + * @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead */ - public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration) { + @Deprecated + public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration) throws JBakeException { DefaultJBakeConfiguration configuration = new DefaultJBakeConfiguration(sourceFolder, compositeConfiguration); configuration.setDestinationFolder(destination); return configuration; @@ -89,7 +92,8 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold * @param config A {@link CompositeConfiguration} * @return A configuration by given parameters */ - public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, CompositeConfiguration config) { + @Deprecated + public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, CompositeConfiguration config) throws JBakeException { return new DefaultJBakeConfiguration(sourceFolder,config); } @@ -102,11 +106,11 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold * @param destinationFolder The destination folder to render and copy files to * @param isClearCache Whether to clear database cache or not * @return A configuration by given parameters - * @throws ConfigurationException if loading the configuration fails + * @throws JBakeException if loading the configuration fails * @deprecated use {@link #createJettyJbakeConfiguration(File, File, File, boolean)} instead */ @Deprecated - public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, boolean isClearCache) throws ConfigurationException { + public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, boolean isClearCache) throws JBakeException { return createJettyJbakeConfiguration(sourceFolder, destinationFolder, (File)null, isClearCache); } @@ -120,9 +124,9 @@ public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder * @param propertiesFile The properties file for the project * @param isClearCache Whether to clear database cache or not * @return A configuration by given parameters - * @throws ConfigurationException if loading the configuration fails + * @throws JBakeException if loading the configuration fails */ - public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, File propertiesFile, boolean isClearCache) throws ConfigurationException { + public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, File propertiesFile, boolean isClearCache) throws JBakeException { DefaultJBakeConfiguration configuration = (DefaultJBakeConfiguration) getConfigUtil().loadConfig(sourceFolder, propertiesFile); configuration.setDestinationFolder(destinationFolder); configuration.setClearCache(isClearCache); diff --git a/jbake-core/src/main/java/org/jbake/launcher/Main.java b/jbake-core/src/main/java/org/jbake/launcher/Main.java index 2577a216..c244848f 100644 --- a/jbake-core/src/main/java/org/jbake/launcher/Main.java +++ b/jbake-core/src/main/java/org/jbake/launcher/Main.java @@ -82,8 +82,8 @@ public void run(String[] args) throws JBakeException { config = getJBakeConfigurationFactory().setEncoding(res.getPropertiesEncoding()).createDefaultJbakeConfiguration(res.getSource(), res.getDestination(), res.getConfig(), res.isClearCache()); } run(res, config); - } catch (final ConfigurationException e) { - throw new JBakeException(SystemExit.CONFIGURATION_ERROR, "Configuration error: " + e.getMessage(), e); + } catch (final JBakeException e) { + throw e; } catch (MissingParameterException mex) { throw new JBakeException(SystemExit.CONFIGURATION_ERROR, mex.getMessage(), mex); } catch (final Throwable e) { diff --git a/jbake-core/src/test/java/org/jbake/app/configuration/JBakeConfigurationFactoryTest.java b/jbake-core/src/test/java/org/jbake/app/configuration/JBakeConfigurationFactoryTest.java index 88a2a3e5..5fd60a39 100644 --- a/jbake-core/src/test/java/org/jbake/app/configuration/JBakeConfigurationFactoryTest.java +++ b/jbake-core/src/test/java/org/jbake/app/configuration/JBakeConfigurationFactoryTest.java @@ -7,6 +7,7 @@ import java.io.File; import java.io.FileWriter; +import java.util.Properties; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.Mockito.spy; @@ -111,4 +112,19 @@ public void shouldUseCustomEncoding() throws Exception { assertThat(factory.getConfigUtil().getEncoding()).isEqualTo("latin1"); verify(util).loadConfig(sourceFolder, null); } + + @Test + void shouldBeAbleToAddCustomProperties() { + File sourceFolder = root; + File destinationFolder = TestUtils.newFolder(root, "output"); + DefaultJBakeConfiguration config = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true); + Properties properties = new Properties(); + properties.setProperty("custom.key", "custom value"); + properties.setProperty("custom.key2", "custom value 2"); + + config.addConfiguration(properties); + + assertThat(config.get("custom.key")).isEqualTo("custom value"); + assertThat(config.get("custom.key2")).isEqualTo("custom value 2"); + } } diff --git a/jbake-core/src/test/java/org/jbake/launcher/MainTest.java b/jbake-core/src/test/java/org/jbake/launcher/MainTest.java index 3aa61a99..0d208392 100644 --- a/jbake-core/src/test/java/org/jbake/launcher/MainTest.java +++ b/jbake-core/src/test/java/org/jbake/launcher/MainTest.java @@ -219,6 +219,14 @@ void shouldThrowJBakeExceptionWithSystemExitCodeOnUnexpectedError(@TempDir Path assertThat(e.getExit()).isEqualTo(SystemExit.ERROR.getStatus()); } + @Test + void shouldThrowAJBakeExceptionWithConfigurationErrorIfLoadThrowsAnCompositeException() { + when(factory.setEncoding(any())).thenReturn(factory); + doThrow(new JBakeException(SystemExit.CONFIGURATION_ERROR, "something went wrong")).when(factory).createDefaultJbakeConfiguration(any(File.class), any(File.class), any(File.class), anyBoolean()); + JBakeException e = assertThrows(JBakeException.class, () -> main.run(new String[]{"-b"})); + assertThat(e.getExit()).isEqualTo(SystemExit.CONFIGURATION_ERROR.getStatus()); + } + @Test void shouldListCurrentSettings(@TempDir Path source) throws ConfigurationException { File src = newFolder(source, "src/jbake");