JMXReporter implements MetricReporter { static String replaceInvalidChars(String str) { char[] chars = null; final int strLen = str.length(); int pos = 0; for (int i = 0; i < strLen; i++) { final char c = str.charAt(i); switch (c) { case '>': case '<': case '"': if (chars == null) { chars = str.toCharArray(); } break; case ' ': if (chars == null) { chars = str.toCharArray(); } chars[pos++] = '_'; break; case ',': case '=': case ';': case ':': case '?': case '\'': case '*': if (chars == null) { chars = str.toCharArray(); } chars[pos++] = '-'; break; default: if (chars != null) { chars[pos] = c; } pos++; } } return chars == null ? str : new String(chars, 0, pos); } JMXReporter(); @Override void open(MetricConfig config); @Override void close(); int getPort(); @Override void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group); @Override void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group); static final String ARG_PORT; }
@Test public void testReplaceInvalidChars() { assertEquals("", JMXReporter.replaceInvalidChars("")); assertEquals("abc", JMXReporter.replaceInvalidChars("abc")); assertEquals("abc", JMXReporter.replaceInvalidChars("abc\"")); assertEquals("abc", JMXReporter.replaceInvalidChars("\"abc")); assertEquals("abc", JMXReporter.replaceInvalidChars("\"abc\"")); assertEquals("abc", JMXReporter.replaceInvalidChars("\"a\"b\"c\"")); assertEquals("", JMXReporter.replaceInvalidChars("\"\"\"\"")); assertEquals("____", JMXReporter.replaceInvalidChars(" ")); assertEquals("ab_-(c)-", JMXReporter.replaceInvalidChars("\"ab ;(c)'")); assertEquals("a_b_c", JMXReporter.replaceInvalidChars("a b c")); assertEquals("a_b_c_", JMXReporter.replaceInvalidChars("a b c ")); assertEquals("a-b-c-", JMXReporter.replaceInvalidChars("a;b'c*")); assertEquals("a------b------c", JMXReporter.replaceInvalidChars("a,=;:?'b,=;:?'c")); }