Last active
October 9, 2019 07:57
-
-
Save atsu85/5281319 to your computer and use it in GitHub Desktop.
This JUnit test shows that saving PropertiesConfiguration with UTF-8 charset results in file containing unicode escapes of UTF-8 characters instead of UTF-8 characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.util.Properties; | |
import org.apache.commons.configuration.PropertiesConfiguration; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.io.IOUtils; | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
import org.hamcrest.core.AllOf; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
public class PropertiesConfigurationTest { | |
private static final String ENCODING = "UTF-8"; | |
private PropertiesConfiguration c; | |
private static final String PROP_KEY = "testKey"; | |
private static final String PROP_VALUE = "example UTF-8 chars are š õ ä ö ü ž"; | |
@Rule | |
public TemporaryFolder tempFolder = new TemporaryFolder(); | |
private File tmpPropsFile; | |
@Before | |
public void setUp() throws Exception { | |
c = createUtf8Conf(); | |
c.setProperty(PROP_KEY, PROP_VALUE); | |
tmpPropsFile = tempFolder.newFile("test.properties"); | |
} | |
protected PropertiesConfiguration createUtf8Conf() { | |
PropertiesConfiguration c = new PropertiesConfiguration(); | |
c.setEncoding(ENCODING); | |
return c; | |
} | |
// XXX Start: failing tests | |
/** | |
* This test shows that after calling {@link PropertiesConfiguration#save(File)} on configuration that is configured to use UTF-8 encoding<br> | |
* file won't contain UTF-8 characters | |
*/ | |
@Test | |
public void testUtf8CharactersInFileAfterSaving() throws Exception { | |
c.save(tmpPropsFile); | |
checkFileContent(); | |
} | |
/** | |
* This test shows that after calling {@link PropertiesConfiguration#save(java.io.OutputStream, String)} with UTF-8 encoding<br> | |
* file won't contain UTF-8 characters | |
*/ | |
@Test | |
public void testUtf8CharactersInFileAfterSavingUsingWriter() throws Exception { | |
FileOutputStream fos = null; | |
Writer w = null; | |
try { | |
fos = new FileOutputStream(tmpPropsFile); | |
c.save(fos, ENCODING); | |
} | |
finally { | |
IOUtils.closeQuietly(fos); | |
IOUtils.closeQuietly(w); | |
} | |
checkFileContent(); | |
} | |
// XXX End: failing tests | |
@Test | |
public void testUtf8CharactersInFileAfterSavingJavaUtilProperties() throws Exception { | |
Properties props = new Properties(); | |
props.put(PROP_KEY, PROP_VALUE); | |
FileOutputStream fos = null; | |
OutputStreamWriter w = null; | |
try { | |
fos = new FileOutputStream(tmpPropsFile); | |
w = new OutputStreamWriter(fos, ENCODING); | |
props.store(w, null); | |
} | |
finally { | |
IOUtils.closeQuietly(fos); | |
IOUtils.closeQuietly(w); | |
} | |
checkFileContent(); | |
} | |
@Test | |
public void testThatLoadingIntoUtf8PropsIsSuccessful() throws Exception { | |
c.save(tmpPropsFile); | |
PropertiesConfiguration c2 = createUtf8Conf(); | |
c2.load(tmpPropsFile); | |
assertEquals(PROP_VALUE, c2.getString(PROP_KEY)); | |
} | |
/** | |
* This test shows that when you don't call {@link PropertiesConfiguration#setEncoding(String)} before loading file containing property values in UTF-8, <br> | |
* then property value returned by the configuration doesn't contain expected characters in utf-8 | |
*/ | |
@Test | |
public void testThatLoadingFromUtf8PropsFailsWithoutUtf8Encoding() throws Exception { | |
FileUtils.writeStringToFile(tmpPropsFile, utf8KeyValuePair()); | |
PropertiesConfiguration c2 = new PropertiesConfiguration(); | |
c2.load(tmpPropsFile); | |
String actualPropValue = c2.getString(PROP_KEY); | |
assertNotNull(actualPropValue); | |
assertTrue(!actualPropValue.contains(PROP_VALUE)); | |
} | |
@Test | |
public void testThatLoadingFromUtf8PropsIsSuccessful() throws Exception { | |
FileUtils.writeStringToFile(tmpPropsFile, utf8KeyValuePair()); | |
PropertiesConfiguration c2 = createUtf8Conf(); | |
c2.load(tmpPropsFile); | |
assertEquals(PROP_VALUE, c2.getString(PROP_KEY)); | |
} | |
protected String utf8KeyValuePair() { | |
return PROP_KEY + " = " + PROP_VALUE; | |
} | |
/** check that {@link #tmpPropsFile} contains {@value #PROP_VALUE} */ | |
protected void checkFileContent() throws IOException { | |
String fileContent = FileUtils.readFileToString(tmpPropsFile); | |
assertThat(fileContent, AllOf.allOf(new SubstringMatcher(PROP_KEY), new SubstringMatcher("="), new SubstringMatcher(PROP_VALUE))); | |
} | |
public static class SubstringMatcher extends BaseMatcher<String> { | |
private final String expectedSubstring; | |
public SubstringMatcher(String expectedSubstring) { | |
this.expectedSubstring = expectedSubstring; | |
} | |
public boolean matches(Object item) { | |
String input = (String) item; | |
return input.contains(expectedSubstring); | |
} | |
public void describeTo(Description description) { | |
description.appendValue("substring '" + expectedSubstring + "'"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment