Skip to content

Commit

Permalink
add cli option --prop-encoding to configure property encoding that sh…
Browse files Browse the repository at this point in the history
…ould be used
  • Loading branch information
ancho committed Mar 8, 2021
1 parent 2781ff8 commit 9ab23dd
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.File;
import java.net.URL;
import java.nio.charset.Charset;

/**
* Provides Configuration related functions.
Expand All @@ -22,11 +23,12 @@
public class ConfigUtil {

public static final char LIST_DELIMITER = ',';
public static final String DEFAULT_ENCODING = "UTF8";
public static final String DEFAULT_ENCODING = "UTF-8";
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
private static final String LEGACY_CONFIG_FILE = "custom.properties";
private static final String CONFIG_FILE = "jbake.properties";
private static final String DEFAULT_CONFIG_FILE = "default.properties";
private String encoding = DEFAULT_ENCODING;

private CompositeConfiguration load(File source) throws ConfigurationException {

Expand Down Expand Up @@ -64,7 +66,7 @@ private PropertiesConfiguration getFileBasedPropertiesConfiguration(File propert
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
.configure(new Parameters().properties()
.setFile(propertiesFile)
.setEncoding(DEFAULT_ENCODING)
.setEncoding(encoding)
.setThrowExceptionOnMissing(true)
.setListDelimiterHandler(new DefaultListDelimiterHandler(LIST_DELIMITER))
.setIncludesAllowed(false));
Expand All @@ -76,7 +78,7 @@ private PropertiesConfiguration getFileBasedPropertiesConfiguration(URL properti
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
.configure(new Parameters().properties()
.setURL(propertiesFile)
.setEncoding(DEFAULT_ENCODING)
.setEncoding(encoding)
.setThrowExceptionOnMissing(true)
.setListDelimiterHandler(new DefaultListDelimiterHandler(LIST_DELIMITER))
.setIncludesAllowed(false));
Expand All @@ -93,4 +95,17 @@ public JBakeConfiguration loadConfig(File source) throws ConfigurationException
return new DefaultJBakeConfiguration(source, configuration);
}

public String getEncoding() {
return this.encoding;
}

public ConfigUtil setEncoding(String encoding) {
if (Charset.isSupported(encoding)) {
this.encoding = encoding;
} else {
this.encoding = DEFAULT_ENCODING;
LOGGER.warn("Unsupported encoding '{}'. Using default encoding '{}'", encoding, this.encoding);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ public ConfigUtil getConfigUtil() {
public void setConfigUtil(ConfigUtil configUtil) {
this.configUtil = configUtil;
}

public JBakeConfigurationFactory setEncoding(String charset) {
this.configUtil.setEncoding(charset);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class LaunchOptions {
@Option(name = "--reset", usage = "clears the local cache, enforcing rendering from scratch")
private boolean clearCache;

@Option(name = "--prop-encoding", usage = "use given encoding to load properties file. default: utf-8")
private String propertiesEncoding = "utf-8";

public String getTemplate() {
if (template != null) {
return template;
Expand Down Expand Up @@ -81,4 +84,8 @@ public boolean isClearCache() {
public boolean isBake() {
return bake || (source != null && destination != null);
}

public String getPropertiesEncoding() {
return propertiesEncoding;
}
}
4 changes: 2 additions & 2 deletions jbake-core/src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ protected void run(String[] args) {
try {
LaunchOptions res = parseArguments(args);
if (res.isRunServer()) {
config = getJBakeConfigurationFactory().createJettyJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache());
config = getJBakeConfigurationFactory().setEncoding(res.getPropertiesEncoding()).createJettyJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache());
} else {
config = getJBakeConfigurationFactory().createDefaultJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache());
config = getJBakeConfigurationFactory().setEncoding(res.getPropertiesEncoding()).createDefaultJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache());
}
run(res, config);
} catch (final ConfigurationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.List;

import static ch.qos.logback.classic.Level.WARN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -330,13 +326,36 @@ void shouldSetCustomFoldersWithAbsolutePaths() throws Exception {
}

@Test
void shouldLoadPropertiesWithUtf8Encoding() throws Exception {
File source = TestUtils.getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(source);
public void shouldUseUtf8EncodingAsDefault() throws Exception{
String unicodeString = "中文属性使用默认Properties编码";
JBakeConfiguration config = util.loadConfig(TestUtils.getTestResourcesAsSourceFolder());

String siteAbout = (String) config.get("site.about");
assertThat(util.getEncoding()).isEqualTo("UTF-8");
assertThat(siteAbout).inUnicode().startsWith(unicodeString);
}

@Test
public void shouldBePossibleToSetCustomEncoding() throws Exception {
String expected = "Latin1 encoded file äöü";
JBakeConfiguration config = util.setEncoding("ISO8859_1").loadConfig(TestUtils.getTestResourcesAsSourceFolder("/fixtureLatin1"));

assertThat((String)config.get("site.about")).contains("中文属性使用默认Properties编码");
String siteAbout = (String) config.get("site.about");
assertThat(siteAbout).contains(expected);
}

@Test
public void shouldLogAWarningAndFallbackToUTF8IfEncodingIsNotSupported() throws Exception {
JBakeConfiguration config = util.setEncoding("UNSUPPORTED_ENCODING").loadConfig(TestUtils.getTestResourcesAsSourceFolder("/fixtureLatin1"));
verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());

LoggingEvent loggingEvent = captorLoggingEvent.getValue();

assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
assertThat(loggingEvent.getFormattedMessage()).isEqualTo("Unsupported encoding 'UNSUPPORTED_ENCODING'. Using default encoding 'UTF-8'");
}


private void assertDefaultPropertiesPresent(JBakeConfiguration config) throws IllegalAccessException {
for (Field field : JBakeConfiguration.class.getFields()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import java.io.File;
import java.io.FileWriter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

public class JBakeConfigurationFactoryTest {

@TempDir File root;
@TempDir
File root;

@Test
public void shouldReturnDefaultConfigurationWithDefaultFolders() throws Exception {
Expand All @@ -33,7 +36,7 @@ public void shouldReturnDefaultConfigurationWithDefaultFolders() throws Exceptio
@Test
public void shouldReturnDefaultConfigurationWithCustomFolders() throws Exception {
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root,"output/custom");
File destinationFolder = TestUtils.newFolder(root, "output/custom");
File templateFolder = TestUtils.newFolder(root, "templates/custom");
File assetFolder = TestUtils.newFolder(root, "assets/custom");
File contentFolder = TestUtils.newFolder(root, "content/custom");
Expand Down Expand Up @@ -63,12 +66,10 @@ public void shouldReturnDefaultConfigurationWithCustomFolders() throws Exception
}




@Test
public void shouldReturnADefaultConfigurationWithSitehost() throws Exception {
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root,"output");
File destinationFolder = TestUtils.newFolder(root, "output");
String siteHost = "http://www.jbake.org";

JBakeConfiguration configuration = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true);
Expand All @@ -79,12 +80,35 @@ public void shouldReturnADefaultConfigurationWithSitehost() throws Exception {
@Test
public void shouldReturnAJettyConfiguration() throws Exception {
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root,"output");
File destinationFolder = TestUtils.newFolder(root, "output");
String siteHost = "http://localhost:8820/";

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

assertThat(configuration.getSiteHost()).isEqualTo(siteHost);
}

@Test
public void shouldUseDefaultEncodingUTF8() throws Exception {
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root, "output");
JBakeConfigurationFactory factory = new JBakeConfigurationFactory();
JBakeConfiguration configuration = factory.createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true);

assertThat(factory.getConfigUtil().getEncoding()).isEqualTo("UTF-8");
}

@Test
public void shouldUseCustomEncoding() throws Exception {

ConfigUtil util = spy(ConfigUtil.class);
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root, "output");
JBakeConfigurationFactory factory = new JBakeConfigurationFactory();
factory.setConfigUtil(util);
JBakeConfiguration configuration = factory.setEncoding("latin1").createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true);

assertThat(factory.getConfigUtil().getEncoding()).isEqualTo("latin1");
verify(util).loadConfig(sourceFolder);
}
}
21 changes: 21 additions & 0 deletions jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ public void bake() throws Exception {
assertThat(res.isBake()).isTrue();
}

@Test
public void customPropertiesEncoding() throws Exception {
String[] args = {"--prop-encoding", "utf-16"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);

assertThat(res.getPropertiesEncoding()).isEqualTo("utf-16");
}

@Test
public void defaultEncodingIsUtf8() throws Exception {
String[] args = {};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);

assertThat(res.getPropertiesEncoding()).isEqualTo("utf-8");
}


@Test
public void bakeNoArgs() throws Exception {
String[] args = {};
Expand Down
32 changes: 27 additions & 5 deletions jbake-core/src/test/java/org/jbake/launcher/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ public void launchJetty(@TempDir Path source) throws Exception {
verify(mockJetty).run(expectedOutput.getPath(),configuration);
}

@Test
public void launchBakeWithCustomPropertiesEncoding(@TempDir Path source) throws Exception {
File currentWorkingdir = newFolder(source, "jbake");
mockDefaultJbakeConfiguration(currentWorkingdir);

String[] args = {"-b", "--prop-encoding", "latin1"};
main.run(args);

verify(factory).setEncoding("latin1");
}

@Test
public void launchBakeWithDefaultUtf8PropertiesEncoding(@TempDir Path source) throws Exception {
File currentWorkingdir = newFolder(source, "jbake");
mockDefaultJbakeConfiguration(currentWorkingdir);

String[] args = {"-b"};
main.run(args);

verify(factory).setEncoding("utf-8");
}

@Test
public void launchBakeAndJetty(@TempDir Path source) throws Exception {
File sourceFolder = newFolder(source, "src/jbake");
Expand All @@ -83,7 +105,7 @@ public void launchBakeAndJetty(@TempDir Path source) throws Exception {


@Test
public void launchBakeAndJettyWithCustomDirForJetty(@TempDir Path source) throws ConfigurationException, IOException {
public void launchBakeAndJettyWithCustomDirForJetty(@TempDir Path source) throws ConfigurationException {
File sourceFolder = newFolder(source,"src/jbake");
String expectedRunPath = "src" + File.separator + "jbake" + File.separator + "output";
File output = newFolder(source,expectedRunPath);
Expand Down Expand Up @@ -159,7 +181,7 @@ public void launchJettyWithCmdlineOverridingProperties(@TempDir Path source, @Te
}

@Test
public void shouldTellUserThatTemplateOptionRequiresInitOption() throws Exception {
public void shouldTellUserThatTemplateOptionRequiresInitOption() {

String[] args = {"-t", "groovy-mte"};

Expand Down Expand Up @@ -188,14 +210,14 @@ private DefaultJBakeConfiguration stubConfig() throws ConfigurationException {
private void mockDefaultJbakeConfiguration(File sourceFolder) throws ConfigurationException {
DefaultJBakeConfiguration configuration = new JBakeConfigurationFactory().createJettyJbakeConfiguration(sourceFolder,null,false);
System.setProperty("user.dir", sourceFolder.getPath());

when(factory.createJettyJbakeConfiguration(any(File.class),any(File.class),anyBoolean())).thenReturn( configuration );
when(factory.setEncoding(any())).thenReturn(factory);
when(factory.createDefaultJbakeConfiguration(any(File.class),any(File.class),anyBoolean())).thenReturn( configuration );
}

private JBakeConfiguration mockJettyConfiguration(File sourceFolder, File destinationFolder) throws ConfigurationException {
DefaultJBakeConfiguration configuration = new JBakeConfigurationFactory().createJettyJbakeConfiguration(sourceFolder,destinationFolder,false);
System.setProperty("user.dir", sourceFolder.getPath());

when(factory.setEncoding(any())).thenReturn(factory);
when(factory.createJettyJbakeConfiguration(any(File.class),any(File.class),anyBoolean())).thenReturn( configuration );
return configuration;
}
Expand Down
19 changes: 19 additions & 0 deletions jbake-core/src/test/resources/fixtureLatin1/jbake.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
template.folder=templates
content.folder=content
asset.folder=assets
render.index=true
index.file=index.html
render.feed=true
feed.file=feed.xml
render.archive=true
render.encoding=UTF-8
archive.file=archive.html
render.tags=false
tag.path=tags
test.property=testing123
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
site.host=http://www.jbake.org
template.feed.thymeleaf.mode=XML
template.sitemap.thymeleaf.mode=XML
template.allcontent.file=allcontent.ftl
site.about=Latin1 encoded file äöü

0 comments on commit 9ab23dd

Please sign in to comment.