From a45268f2b2e43a5fb27bdc5b5a643a9abfcef84a Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Wed, 22 Jul 2015 13:24:44 +0200 Subject: [PATCH 1/3] CLOUDSTACK-8660 - Formatting test and utility classes --- utils/src/com/cloud/utils/StringUtils.java | 128 ++++++++-------- .../test/com/cloud/utils/StringUtilsTest.java | 140 +++++++++--------- 2 files changed, 134 insertions(+), 134 deletions(-) diff --git a/utils/src/com/cloud/utils/StringUtils.java b/utils/src/com/cloud/utils/StringUtils.java index 1dc0f9f15004..d1225c5f2d46 100644 --- a/utils/src/com/cloud/utils/StringUtils.java +++ b/utils/src/com/cloud/utils/StringUtils.java @@ -36,7 +36,7 @@ public class StringUtils { private static Charset preferredACSCharset; static { - String preferredCharset = "UTF-8"; + final String preferredCharset = "UTF-8"; if (Charset.isSupported(preferredCharset)) { preferredACSCharset = Charset.forName(preferredCharset); } else { @@ -48,16 +48,16 @@ public static Charset getPreferredCharset() { return preferredACSCharset; } - public static String join(Iterable iterable, String delim) { - StringBuilder sb = new StringBuilder(); + public static String join(final Iterable iterable, final String delim) { + final StringBuilder sb = new StringBuilder(); if (iterable != null) { - Iterator iter = iterable.iterator(); + final Iterator iter = iterable.iterator(); if (iter.hasNext()) { - Object next = iter.next(); + final Object next = iter.next(); sb.append(next.toString()); } while (iter.hasNext()) { - Object next = iter.next(); + final Object next = iter.next(); sb.append(delim + next.toString()); } } @@ -68,7 +68,7 @@ public static String join(final String delimiter, final Object... components) { return org.apache.commons.lang.StringUtils.join(components, delimiter); } - public static boolean isNotBlank(String str) { + public static boolean isNotBlank(final String str) { if (str != null && str.trim().length() > 0) { return true; } @@ -78,8 +78,8 @@ public static boolean isNotBlank(String str) { public static String cleanupTags(String tags) { if (tags != null) { - String[] tokens = tags.split(","); - StringBuilder t = new StringBuilder(); + final String[] tokens = tags.split(","); + final StringBuilder t = new StringBuilder(); for (int i = 0; i < tokens.length; i++) { t.append(tokens[i].trim()).append(","); } @@ -94,11 +94,11 @@ public static String cleanupTags(String tags) { * @param tags * @return List of tags */ - public static List csvTagsToList(String tags) { - List tagsList = new ArrayList(); + public static List csvTagsToList(final String tags) { + final List tagsList = new ArrayList(); if (tags != null) { - String[] tokens = tags.split(","); + final String[] tokens = tags.split(","); for (int i = 0; i < tokens.length; i++) { tagsList.add(tokens[i].trim()); } @@ -113,8 +113,8 @@ public static List csvTagsToList(String tags) { * @return String containing a comma separated list of tags */ - public static String listToCsvTags(List tagsList) { - StringBuilder tags = new StringBuilder(); + public static String listToCsvTags(final List tagsList) { + final StringBuilder tags = new StringBuilder(); if (tagsList.size() > 0) { for (int i = 0; i < tagsList.size(); i++) { tags.append(tagsList.get(i)); @@ -127,12 +127,12 @@ public static String listToCsvTags(List tagsList) { return tags.toString(); } - public static String getExceptionStackInfo(Throwable e) { - StringBuffer sb = new StringBuffer(); + public static String getExceptionStackInfo(final Throwable e) { + final StringBuffer sb = new StringBuffer(); sb.append(e.toString()).append("\n"); - StackTraceElement[] elemnents = e.getStackTrace(); - for (StackTraceElement element : elemnents) { + final StackTraceElement[] elemnents = e.getStackTrace(); + for (final StackTraceElement element : elemnents) { sb.append(element.getClassName()).append("."); sb.append(element.getMethodName()).append("("); sb.append(element.getFileName()).append(":"); @@ -143,15 +143,15 @@ public static String getExceptionStackInfo(Throwable e) { return sb.toString(); } - public static String unicodeEscape(String s) { - StringBuilder sb = new StringBuilder(); + public static String unicodeEscape(final String s) { + final StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if ((c >> 7) > 0) { + final char c = s.charAt(i); + if (c >> 7 > 0) { sb.append("\\u"); - sb.append(hexChar[(c >> 12) & 0xF]); // append the hex character for the left-most 4-bits - sb.append(hexChar[(c >> 8) & 0xF]); // hex for the second group of 4-bits from the left - sb.append(hexChar[(c >> 4) & 0xF]); // hex for the third group + sb.append(hexChar[c >> 12 & 0xF]); // append the hex character for the left-most 4-bits + sb.append(hexChar[c >> 8 & 0xF]); // hex for the second group of 4-bits from the left + sb.append(hexChar[c >> 4 & 0xF]); // hex for the third group sb.append(hexChar[c & 0xF]); // hex for the last group, e.g., the right most 4-bits } else { sb.append(c); @@ -160,12 +160,12 @@ public static String unicodeEscape(String s) { return sb.toString(); } - public static String getMaskedPasswordForDisplay(String password) { + public static String getMaskedPasswordForDisplay(final String password) { if (password == null || password.isEmpty()) { return "*"; } - StringBuffer sb = new StringBuffer(); + final StringBuffer sb = new StringBuffer(); sb.append(password.charAt(0)); for (int i = 1; i < password.length(); i++) { sb.append("*"); @@ -187,14 +187,14 @@ public static String getMaskedPasswordForDisplay(String password) { private static final Pattern REGEX_REDUNDANT_AND = Pattern.compile("(&|%26)(&|%26)+"); // Responsible for stripping sensitive content from request and response strings - public static String cleanString(String stringToClean) { + public static String cleanString(final String stringToClean) { String cleanResult = ""; if (stringToClean != null) { cleanResult = REGEX_PASSWORD_QUERYSTRING.matcher(stringToClean).replaceAll(""); cleanResult = REGEX_PASSWORD_JSON.matcher(cleanResult).replaceAll(""); - Matcher detailsMatcher = REGEX_PASSWORD_DETAILS.matcher(cleanResult); + final Matcher detailsMatcher = REGEX_PASSWORD_DETAILS.matcher(cleanResult); while (detailsMatcher.find()) { - Matcher detailsIndexMatcher = REGEX_PASSWORD_DETAILS_INDEX.matcher(detailsMatcher.group()); + final Matcher detailsIndexMatcher = REGEX_PASSWORD_DETAILS_INDEX.matcher(detailsMatcher.group()); if (detailsIndexMatcher.find()) { cleanResult = cleanDetails(cleanResult, detailsIndexMatcher.group()); } @@ -203,9 +203,9 @@ public static String cleanString(String stringToClean) { return cleanResult; } - public static String cleanDetails(String stringToClean, String detailsIndexSting) { + public static String cleanDetails(final String stringToClean, final String detailsIndexSting) { String cleanResult = stringToClean; - for (String log : stringToClean.split("&|%26")) { + for (final String log : stringToClean.split("&|%26")) { if (log.contains(detailsIndexSting)) { cleanResult = cleanResult.replace(log, ""); } @@ -214,7 +214,7 @@ public static String cleanDetails(String stringToClean, String detailsIndexSting return cleanResult; } - public static boolean areTagsEqual(String tags1, String tags2) { + public static boolean areTagsEqual(final String tags1, final String tags2) { if (tags1 == null && tags2 == null) { return true; } @@ -229,28 +229,28 @@ public static boolean areTagsEqual(String tags1, String tags2) { final String delimiter = ","; - List lstTags1 = new ArrayList(); - String[] aTags1 = tags1.split(delimiter); + final List lstTags1 = new ArrayList(); + final String[] aTags1 = tags1.split(delimiter); - for (String tag1 : aTags1) { + for (final String tag1 : aTags1) { lstTags1.add(tag1.toLowerCase()); } - List lstTags2 = new ArrayList(); - String[] aTags2 = tags2.split(delimiter); + final List lstTags2 = new ArrayList(); + final String[] aTags2 = tags2.split(delimiter); - for (String tag2 : aTags2) { + for (final String tag2 : aTags2) { lstTags2.add(tag2.toLowerCase()); } return lstTags1.containsAll(lstTags2) && lstTags2.containsAll(lstTags1); } - public static String stripControlCharacters(String s) { + public static String stripControlCharacters(final String s) { return StringUtilities.stripControls(s); } - public static int formatForOutput(String text, int start, int columns, char separator) { + public static int formatForOutput(final String text, final int start, final int columns, final char separator) { if (start >= text.length()) { return -1; } @@ -259,24 +259,24 @@ public static int formatForOutput(String text, int start, int columns, char sepa if (end > text.length()) { end = text.length(); } - String searchable = text.substring(start, end); - int found = searchable.lastIndexOf(separator); + final String searchable = text.substring(start, end); + final int found = searchable.lastIndexOf(separator); return found > 0 ? found : end - start; } - public static Map stringToMap(String s) { - Map map = new HashMap(); - String[] elements = s.split(";"); - for (String parts : elements) { - String[] keyValue = parts.split(":"); + public static Map stringToMap(final String s) { + final Map map = new HashMap(); + final String[] elements = s.split(";"); + for (final String parts : elements) { + final String[] keyValue = parts.split(":"); map.put(keyValue[0], keyValue[1]); } return map; } - public static String mapToString(Map map) { + public static String mapToString(final Map map) { String s = ""; - for (Map.Entry entry : map.entrySet()) { + for (final Map.Entry entry : map.entrySet()) { s += entry.getKey() + ":" + entry.getValue() + ";"; } if (s.length() > 0) { @@ -285,25 +285,25 @@ public static String mapToString(Map map) { return s; } - public static List applyPagination(List originalList, Long startIndex, Long pageSizeVal) { + public static List applyPagination(final List originalList, final Long startIndex, final Long pageSizeVal) { // Most likely pageSize will never exceed int value, and we need integer to partition the listToReturn - boolean applyPagination = startIndex != null && pageSizeVal != null + final boolean applyPagination = startIndex != null && pageSizeVal != null && startIndex <= Integer.MAX_VALUE && startIndex >= Integer.MIN_VALUE && pageSizeVal <= Integer.MAX_VALUE && pageSizeVal >= Integer.MIN_VALUE; - List listWPagination = null; - if (applyPagination) { - listWPagination = new ArrayList<>(); - int index = startIndex.intValue() == 0 ? 0 : startIndex.intValue() / pageSizeVal.intValue(); - List> partitions = StringUtils.partitionList(originalList, pageSizeVal.intValue()); - if (index < partitions.size()) { - listWPagination = partitions.get(index); - } - } - return listWPagination; + List listWPagination = null; + if (applyPagination) { + listWPagination = new ArrayList<>(); + final int index = startIndex.intValue() == 0 ? 0 : startIndex.intValue() / pageSizeVal.intValue(); + final List> partitions = StringUtils.partitionList(originalList, pageSizeVal.intValue()); + if (index < partitions.size()) { + listWPagination = partitions.get(index); + } + } + return listWPagination; } - private static List> partitionList(List originalList, int chunkSize) { - List> listOfChunks = new ArrayList>(); + private static List> partitionList(final List originalList, final int chunkSize) { + final List> listOfChunks = new ArrayList>(); for (int i = 0; i < originalList.size() / chunkSize; i++) { listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize + chunkSize)); } diff --git a/utils/test/com/cloud/utils/StringUtilsTest.java b/utils/test/com/cloud/utils/StringUtilsTest.java index 58b8b90d19ae..7671cffb9fb2 100644 --- a/utils/test/com/cloud/utils/StringUtilsTest.java +++ b/utils/test/com/cloud/utils/StringUtilsTest.java @@ -32,7 +32,7 @@ public class StringUtilsTest { @Test public void testGetPrefferedCharset() { - assertEquals(StringUtils.getPreferredCharset(),Charset.forName("UTF-8")); + assertEquals(StringUtils.getPreferredCharset(), Charset.forName("UTF-8")); } @Test @@ -42,91 +42,91 @@ public void testGetDefaultCharset() { @Test public void testCleanPasswordFromJsonObjectAtEnd() { - String input = "{\"foo\":\"bar\",\"password\":\"test\"}"; + final String input = "{\"foo\":\"bar\",\"password\":\"test\"}"; //TODO: It would be nice to clean up the regex in question to not //have to return the trailing comma in the expected string below - String expected = "{\"foo\":\"bar\",}"; - String result = StringUtils.cleanString(input); + final String expected = "{\"foo\":\"bar\",}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromJsonObjectInMiddle() { - String input = "{\"foo\":\"bar\",\"password\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"foo\":\"bar\",\"password\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromJsonObjectAlone() { - String input = "{\"password\":\"test\"}"; - String expected = "{}"; - String result = StringUtils.cleanString(input); + final String input = "{\"password\":\"test\"}"; + final String expected = "{}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromJsonObjectAtStart() { - String input = "{\"password\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"password\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromJsonObjectWithMultiplePasswords() { - String input = "{\"description\":\"foo\"}],\"password\":\"bar\",\"nic\":[{\"password\":\"bar2\",\"id\":\"1\"}]}"; - String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; - String result = StringUtils.cleanString(input); + final String input = "{\"description\":\"foo\"}],\"password\":\"bar\",\"nic\":[{\"password\":\"bar2\",\"id\":\"1\"}]}"; + final String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromRequestString() { - String input = "username=foo&password=bar&url=foobar"; - String expected = "username=foo&url=foobar"; - String result = StringUtils.cleanString(input); + final String input = "username=foo&password=bar&url=foobar"; + final String expected = "username=foo&url=foobar"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromEncodedRequestString() { - String input = "name=SS1&provider=SMB&zoneid=5a60af2b-3025-4f2a-9ecc-8e33bf2b94e3&url=cifs%3A%2F%2F10.102.192.150%2FSMB-Share%2Fsowmya%2Fsecondary%3Fuser%3Dsowmya%26password%3DXXXXX%40123%26domain%3DBLR"; - String expected = "name=SS1&provider=SMB&zoneid=5a60af2b-3025-4f2a-9ecc-8e33bf2b94e3&url=cifs%3A%2F%2F10.102.192.150%2FSMB-Share%2Fsowmya%2Fsecondary%3Fuser%3Dsowmya%26domain%3DBLR"; - String result = StringUtils.cleanString(input); + final String input = "name=SS1&provider=SMB&zoneid=5a60af2b-3025-4f2a-9ecc-8e33bf2b94e3&url=cifs%3A%2F%2F10.102.192.150%2FSMB-Share%2Fsowmya%2Fsecondary%3Fuser%3Dsowmya%26password%3DXXXXX%40123%26domain%3DBLR"; + final String expected = "name=SS1&provider=SMB&zoneid=5a60af2b-3025-4f2a-9ecc-8e33bf2b94e3&url=cifs%3A%2F%2F10.102.192.150%2FSMB-Share%2Fsowmya%2Fsecondary%3Fuser%3Dsowmya%26domain%3DBLR"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromRequestStringWithMultiplePasswords() { - String input = "username=foo&password=bar&url=foobar&password=bar2&test=4"; - String expected = "username=foo&url=foobar&test=4"; - String result = StringUtils.cleanString(input); + final String input = "username=foo&password=bar&url=foobar&password=bar2&test=4"; + final String expected = "username=foo&url=foobar&test=4"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromRequestStringMatchedAtEndSingleQuote() { - String input = "'username=foo&password=bar'"; - String expected = "'username=foo'"; - String result = StringUtils.cleanString(input); + final String input = "'username=foo&password=bar'"; + final String expected = "'username=foo'"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromRequestStringMatchedAtEndDoubleQuote() { - String input = "\"username=foo&password=bar\""; - String expected = "\"username=foo\""; - String result = StringUtils.cleanString(input); + final String input = "\"username=foo&password=bar\""; + final String expected = "\"username=foo\""; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanPasswordFromRequestStringMatchedAtMiddleDoubleQuote() { - String input = "\"username=foo&password=bar&goo=sdf\""; - String expected = "\"username=foo&goo=sdf\""; - String result = StringUtils.cleanString(input); + final String input = "\"username=foo&password=bar&goo=sdf\""; + final String expected = "\"username=foo&goo=sdf\""; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @@ -138,101 +138,101 @@ public void testJoin() { @Test public void testCleanSecretkeyFromJsonObjectAtEnd() { - String input = "{\"foo\":\"bar\",\"secretkey\":\"test\"}"; + final String input = "{\"foo\":\"bar\",\"secretkey\":\"test\"}"; // TODO: It would be nice to clean up the regex in question to not // have to return the trailing comma in the expected string below - String expected = "{\"foo\":\"bar\",}"; - String result = StringUtils.cleanString(input); + final String expected = "{\"foo\":\"bar\",}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanSecretkeyFromJsonObjectInMiddle() { - String input = "{\"foo\":\"bar\",\"secretkey\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"foo\":\"bar\",\"secretkey\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanSecretkeyFromJsonObjectAlone() { - String input = "{\"secretkey\":\"test\"}"; - String expected = "{}"; - String result = StringUtils.cleanString(input); + final String input = "{\"secretkey\":\"test\"}"; + final String expected = "{}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanSecretkeyFromJsonObjectAtStart() { - String input = "{\"secretkey\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"secretkey\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanSecretkeyFromJsonObjectWithMultiplePasswords() { - String input = "{\"description\":\"foo\"}],\"secretkey\":\"bar\",\"nic\":[{\"secretkey\":\"bar2\",\"id\":\"1\"}]}"; - String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; - String result = StringUtils.cleanString(input); + final String input = "{\"description\":\"foo\"}],\"secretkey\":\"bar\",\"nic\":[{\"secretkey\":\"bar2\",\"id\":\"1\"}]}"; + final String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromJsonObjectAtEnd() { - String input = "{\"foo\":\"bar\",\"accesskey\":\"test\"}"; + final String input = "{\"foo\":\"bar\",\"accesskey\":\"test\"}"; // TODO: It would be nice to clean up the regex in question to not // have to return the trailing comma in the expected string below - String expected = "{\"foo\":\"bar\",}"; - String result = StringUtils.cleanString(input); + final String expected = "{\"foo\":\"bar\",}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromJsonObjectInMiddle() { - String input = "{\"foo\":\"bar\",\"accesskey\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"foo\":\"bar\",\"accesskey\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromJsonObjectAlone() { - String input = "{\"accesskey\":\"test\"}"; - String expected = "{}"; - String result = StringUtils.cleanString(input); + final String input = "{\"accesskey\":\"test\"}"; + final String expected = "{}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromJsonObjectAtStart() { - String input = "{\"accesskey\":\"test\",\"test\":\"blah\"}"; - String expected = "{\"test\":\"blah\"}"; - String result = StringUtils.cleanString(input); + final String input = "{\"accesskey\":\"test\",\"test\":\"blah\"}"; + final String expected = "{\"test\":\"blah\"}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromJsonObjectWithMultiplePasswords() { - String input = "{\"description\":\"foo\"}],\"accesskey\":\"bar\",\"nic\":[{\"accesskey\":\"bar2\",\"id\":\"1\"}]}"; - String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; - String result = StringUtils.cleanString(input); + final String input = "{\"description\":\"foo\"}],\"accesskey\":\"bar\",\"nic\":[{\"accesskey\":\"bar2\",\"id\":\"1\"}]}"; + final String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanAccesskeyFromRequestString() { - String input = "username=foo&accesskey=bar&url=foobar"; - String expected = "username=foo&url=foobar"; - String result = StringUtils.cleanString(input); + final String input = "username=foo&accesskey=bar&url=foobar"; + final String expected = "username=foo&url=foobar"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } @Test public void testCleanSecretkeyFromRequestString() { - String input = "username=foo&secretkey=bar&url=foobar"; - String expected = "username=foo&url=foobar"; - String result = StringUtils.cleanString(input); + final String input = "username=foo&secretkey=bar&url=foobar"; + final String expected = "username=foo&url=foobar"; + final String result = StringUtils.cleanString(input); assertEquals(result, expected); } From c6057961bc808a344f2858233598044cc9e4012c Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Wed, 22 Jul 2015 13:37:43 +0200 Subject: [PATCH 2/3] 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 --- utils/src/com/cloud/utils/StringUtils.java | 10 +++++++--- .../test/com/cloud/utils/StringUtilsTest.java | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/utils/src/com/cloud/utils/StringUtils.java b/utils/src/com/cloud/utils/StringUtils.java index d1225c5f2d46..02a616b93a9a 100644 --- a/utils/src/com/cloud/utils/StringUtils.java +++ b/utils/src/com/cloud/utils/StringUtils.java @@ -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 static Charset getPreferredCharset() { return preferredACSCharset; } + public static boolean isUtf8Supported() { + return Charset.isSupported(UTF8); + } + public static String join(final Iterable iterable, final String delim) { final StringBuilder sb = new StringBuilder(); if (iterable != null) { diff --git a/utils/test/com/cloud/utils/StringUtilsTest.java b/utils/test/com/cloud/utils/StringUtilsTest.java index 7671cffb9fb2..a9eeac752a4a 100644 --- a/utils/test/com/cloud/utils/StringUtilsTest.java +++ b/utils/test/com/cloud/utils/StringUtilsTest.java @@ -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 void testCleanSecretkeyFromRequestString() { @Test public void listToCsvTags() { - Assert.assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c"))); - Assert.assertEquals("", StringUtils.listToCsvTags(new ArrayList())); + assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c"))); + assertEquals("", StringUtils.listToCsvTags(new ArrayList())); } } From 4d6b9acf2549b34ecdb57d29bf47b082b630201c Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Wed, 22 Jul 2015 16:27:56 +0200 Subject: [PATCH 3/3] CLOUDSTACK-8660 - Adding new StringUtils.getDefaultCharset() in order to wrap the Charset equivalet method. - This test was added in order to cover the new StringUtils.getDefaultCharset(). - One cannot be sure that StringUtils.getPreferredCharset() will always be equals to Charset.defaultCharset() --- utils/src/com/cloud/utils/StringUtils.java | 4 ++++ utils/test/com/cloud/utils/StringUtilsTest.java | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/utils/src/com/cloud/utils/StringUtils.java b/utils/src/com/cloud/utils/StringUtils.java index 02a616b93a9a..76f7806befcf 100644 --- a/utils/src/com/cloud/utils/StringUtils.java +++ b/utils/src/com/cloud/utils/StringUtils.java @@ -52,6 +52,10 @@ public static boolean isUtf8Supported() { return Charset.isSupported(UTF8); } + public static Charset getDefaultCharset() { + return Charset.defaultCharset(); + } + public static String join(final Iterable iterable, final String delim) { final StringBuilder sb = new StringBuilder(); if (iterable != null) { diff --git a/utils/test/com/cloud/utils/StringUtilsTest.java b/utils/test/com/cloud/utils/StringUtilsTest.java index a9eeac752a4a..3619ede84b2b 100644 --- a/utils/test/com/cloud/utils/StringUtilsTest.java +++ b/utils/test/com/cloud/utils/StringUtilsTest.java @@ -42,7 +42,11 @@ public void testGetPreferredCharset() { @Test public void testGetDefaultCharset() { - assertEquals(StringUtils.getPreferredCharset(), Charset.defaultCharset()); + // Is this test irrelevant? Is wrapping the Charset.defaultCharset() too much? + // This test was added in order to cover the new StringUtils.getDefaultCharset(). + // One cannot be sure that StringUtils.getPreferredCharset() will always be + // equals to Charset.defaultCharset() + assertEquals(StringUtils.getDefaultCharset(), Charset.defaultCharset()); } @Test