CLOUDSTACK-8660 - Adding a method to check if UTF-8 is supported

- Changing the test to call isUtf8Supported() before checking if the preferred charset is actually UTF-8

Signed-off-by: wilderrodrigues <wrodrigues@schubergphilis.com>
This commit is contained in:
wilderrodrigues 2015-07-22 13:37:43 +02:00
parent 59d7bc3573
commit 245c976ad0
2 changed files with 19 additions and 10 deletions

View File

@ -34,11 +34,11 @@ public class StringUtils {
private static final char[] hexChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static Charset preferredACSCharset;
private static final String UTF8 = "UTF-8";
static {
final String preferredCharset = "UTF-8";
if (Charset.isSupported(preferredCharset)) {
preferredACSCharset = Charset.forName(preferredCharset);
if (isUtf8Supported()) {
preferredACSCharset = Charset.forName(UTF8);
} else {
preferredACSCharset = Charset.defaultCharset();
}
@ -48,6 +48,10 @@ public class StringUtils {
return preferredACSCharset;
}
public static boolean isUtf8Supported() {
return Charset.isSupported(UTF8);
}
public static String join(final Iterable<? extends Object> iterable, final String delim) {
final StringBuilder sb = new StringBuilder();
if (iterable != null) {

View File

@ -20,24 +20,29 @@
package com.cloud.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import junit.framework.Assert;
import org.junit.Test;
public class StringUtilsTest {
@Test
public void testGetPrefferedCharset() {
assertEquals(StringUtils.getPreferredCharset(), Charset.forName("UTF-8"));
public void testGetPreferredCharset() {
final boolean ifUtf8Supported = StringUtils.isUtf8Supported();
if (ifUtf8Supported) {
assertEquals(StringUtils.getPreferredCharset(), Charset.forName("UTF-8"));
} else {
assertNotEquals(StringUtils.getPreferredCharset(), Charset.forName("UTF-8"));
}
}
@Test
public void testGetDefaultCharset() {
assertEquals(StringUtils.getPreferredCharset(),Charset.defaultCharset());
assertEquals(StringUtils.getPreferredCharset(), Charset.defaultCharset());
}
@Test
@ -238,7 +243,7 @@ public class StringUtilsTest {
@Test
public void listToCsvTags() {
Assert.assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c")));
Assert.assertEquals("", StringUtils.listToCsvTags(new ArrayList<String>()));
assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c")));
assertEquals("", StringUtils.listToCsvTags(new ArrayList<String>()));
}
}